How to pass an std::string to glShaderSource?

后端 未结 7 1824
感情败类
感情败类 2020-12-05 04:47

I have the following code:

glShaderSource(shader, 1, (const char **)data.c_str(), NULL);

But it makes my program crash. How do I convert

相关标签:
7条回答
  • 2020-12-05 05:17

    glShaderSource signature is, according to glShaderSource doc:

    void glShaderSource(
        GLuint shader,
        GLsizei count,
        const GLchar** string,
        const GLint* length);
    

    where string "Specifies an array of pointers to strings containing the source code to be loaded into the shader". What you're trying to pass is a pointer to a NULL terminated string (that is, a pointer to a const char*).

    Unfortunately, I am not familiar with glShaderSource, but I can guess it's not expected a pointer to "some code" but something like this instead:

    const char** options =
    {
        "option1",
        "option2"
        // and so on
    };
    

    From opengl-redbook, you can read an example (I've shortened it in purpose):

    const GLchar* shaderSrc[] = {
        "void main()",
        "{",
        "    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;",
        "}"
    };
    shader = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(shader, NumberOfLines(shaderSrc), shaderSrc, NULL);
    
    0 讨论(0)
提交回复
热议问题