Search code examples
python-3.xopenglglslpyopengl

OpenGL shader compile error (C0000 and C0206) (PyOpenGL)


I'm still pretty new to opengl and i'm trying to compile my vertex and fragment shader but keep getting an error. Heres the shaders i'm compiling:

# Vertex shader
vert_shader = """
#version 330
in vec4 position
void main()
{
    gl_Position = vec4(position, 1.0f);
}

"""

# Fragment shader
frag_shader = """
#version 330
void main()
{
    gl_FragColor = vec4(1.0f, 0.0f, 0.0f, 1.0f);
}

"""

# Compiles the vertex and fragment shader
shader = OpenGL.GL.shaders.compileProgram(OpenGL.GL.shaders.compileShader(str(vert_shader), GL_VERTEX_SHADER),
                                          OpenGL.GL.shaders.compileShader(str(frag_shader), GL_FRAGMENT_SHADER))

When i run my program, i get this error:

RuntimeError: ('Shader compile failure (0): b\'0(4) : error C0000: syntax error, unexpected reserved word "void", expecting \\\',\\\' or \\\';\\\' at token "void"\\n\'', [b'\n    #version 330\n    in vec4 position\n    void main()\n    {\n        gl_Position = vec4(position, 1.0f);\n    }\n\n    '], GL_VERTEX_SHADER)

Initially i thought i was getting this error because i didn't parse the string and take out the new line indicators, but once i took those out using the 'replace' string function, i got this error:

RuntimeError: ('Shader compile failure (0): b\'0(1) : error C0205: invalid profile "in"\\n0(1) : error C0206: invalid token "vec4" in version line\\n\'', [b'    #version 330    in vec4 position    void main()    {        gl_Position = vec4(position, 1.0f);    }    '], GL_VERTEX_SHADER)

I even tried encoding the string as ascii after parsing it but that didn't seem to work either.


Solution

  • Your vertex shader had syntax errors, try again with this fixed one:

    #version 330
    
    in vec4 position;
    
    void main()
    {
        gl_Position = position;
    }
    

    1) You were missing ';' in the line in vec4 position

    2) Line gl_Position = vec4(position, 1.0f); was trying to create a vec4 instance with the first argument position, which happens to be a vec4. To fix it you can just assigning directly the vec4 position like gl_Position = position; or using swizzling like gl_Position = vec4(position.xyz, 1.0);