Search code examples
pythonopenglglslpygletvertex-shader

Can someone help vertex shader error in pyopengl


I have singled out this code in causing an error

    self.vertex_shader_source = """
    #version 130
    in layout (location = 0) vec3 position;
    in layout (location = 1) vec3 color;
            
    out vec3 newColor;
    void main()
    {
        gl_Position = vec4(position, 1.0f);
        newColor = color;
    }
    """
    
    self.fragment_shader_source = """
    #version 130
    in vec3 newColor;
    
    out vec4 outColor;
    void main()
    {
        outColor = vec4(newColor, 1.0f);
    }
    """

The Error

Traceback (most recent call last):
File "/home/awesomenoob/Coding/Python/Rendering/Opengl-tri.py", line 23, in <module>
window = MyWindow(1280, 720, "yoot", resizable=True)
File "/home/awesomenoob/Coding/Python/Rendering/Opengl-tri.py", line 10, in __init__
self.triangle = Triangle()
File "/home/awesomenoob/Coding/Python/Rendering/Triangle.py", line 35, in __init__
shader = 
OpenGL.GL.shaders.compileProgram(OpenGL.GL.shaders.compileShader(self.vertex_shader_source, GL_VERTEX_SHADER),
File "/home/awesomenoob/.local/lib/python3.7/site-packages/OpenGL/GL/shaders.py", line 241, in compileShader
shaderType,
OpenGL.GL.shaders.ShaderCompilationError: ('Shader compile failure (0): b"0:3(12): error: 
syntax error, unexpected \'(\', expecting \'{\'\\n"', [b'\n        #version 130\n        in 
layout (location = 0) vec3 position;\n        in layout (location = 1) vec3 color;\n                
\n        out vec3 newColor;\n        void main()\n        {\n            gl_Position = 
vec4(position, 1.0f);\n            newColor = color;\n        }\n        '], 35633)

if you could help me understand what i did wrong that would be a huge help. I am just trying to learn pyglet/opengl in python


Solution

  • Vertex shader attribute location Layout qualifiers are supported since GLSL Version 3.30. Compare OpenGL Shading Language 1.30 Specification and OpenGL Shading Language 3.30 Specification. If possible, use GLSL 3.00.

    Alternatively remove the layout qualifiers:

    #version 130
    in vec3 position;
    in vec3 color;
            
    out vec3 newColor;
    void main()
    {
        gl_Position = vec4(position, 1.0f);
        newColor = color;
    }
    

    Set the attribute locations with glBindAttribLocation before the program is linked or get the attribute locations with glGetAttribLocation after the program is linked.

    e.g.:

    shader = OpenGL.GL.shaders.compileProgram(...)
    position_loc = glGetAttribLocation(shader, b'position')
    color_loc = glGetAttribLocation(shader, b'color')