我有一个存储在String值中的着色器:
var myShader =
" attribute vec4 a_position;" +
" void main() {" +
" gl_Position = a_position;" +
" }"
glShaderSource(shader, GLsizei(1), myShader, nil)
函数glShaderSource具有绑定签名:
func glShaderSource(shader: GLuint, count: GLsizei, string: UnsafePointer<UnsafePointer<GLchar>>, length: UnsafePointer<GLint>)
当我尝试将着色器String直接传递给glShaderSource时,我收到错误消息:
'String' is not convertible to 'UnsafePointer<UnsafePointer<GLchar>>'
如何正确传递字符串? (xCode版本6.1(6A1052d))
答案 0 :(得分:8)
经过半天的搜索,我发现工作解决方案没有编译错误,内存访问不良失败了:
var cStringSource = (code as NSString).UTF8String
glShaderSource(shader, GLsizei(1), &cStringSource, nil)
答案 1 :(得分:2)
在OpenGLs Swift实现中,CLchar被类型化为CChar。进行如下转换:
var shaderString: String = "here's a String"
let cstring = shaderString.cStringUsingEncoding(NSUTF8StringEncoding)
glShaderSource(shader, GLsizei(1), UnsafePointer(cstring), nil)
答案 2 :(得分:2)
斯威夫特3:
let shaderStringUTF8 = shaderString.cString(using: String.defaultCStringEncoding)
var shaderStringUTF8Pointer = UnsafePointer<GLchar>(shaderStringUTF8)
glShaderSource(shaderHandle, GLsizei(1), &shaderStringUTF8Pointer, nil)