How to pass String parameter as GLchar ** (char **) in glShaderSource?

I have a shader that is stored in a string value:

var myShader = " attribute vec4 a_position;" + " void main() {" + " gl_Position = a_position;" + " }" glShaderSource(shader, GLsizei(1), myShader, nil) 

The glShaderSource function has a binding signature:

  func glShaderSource(shader: GLuint, count: GLsizei, string: UnsafePointer<UnsafePointer<GLchar>>, length: UnsafePointer<GLint>) 

When I try to pass the shader string to the glShaderSource file, I get an error message:

  'String' is not convertible to 'UnsafePointer<UnsafePointer<GLchar>>' 

How to pass a string? (xCode Version 6.1 (6A1052d))

+5
source share
3 answers

After searching for half a day, I found a solution for working without compilation, and the failure of unsuccessful access to memory failed:

 var cStringSource = (code as NSString).UTF8String glShaderSource(shader, GLsizei(1), &cStringSource, nil) 
+5
source

CLchar typealiased as CChar in the OpenGL Swift implementation. Make the conversion as follows:

 var shaderString: String = "here a String" let cstring = shaderString.cStringUsingEncoding(NSUTF8StringEncoding) glShaderSource(shader, GLsizei(1), UnsafePointer(cstring), nil) 
+2
source

Swift 3:

 let shaderStringUTF8 = shaderString.cString(using: String.defaultCStringEncoding) var shaderStringUTF8Pointer = UnsafePointer<GLchar>(shaderStringUTF8) glShaderSource(shaderHandle, GLsizei(1), &shaderStringUTF8Pointer, nil) 
+2
source

Source: https://habr.com/ru/post/1208983/


All Articles