pythonopenglglslpyopengl

Writing down a shader for OpenGL


I am writing down my own shaders for the OpenGL in python:

I have written down these 2 shaders:

Vertex Shader:

#version 330 core

layout(location=0) in vec3 vertexPos;

out vec4 position;

void main()
{
position = vec4(vertexPos,1.0);
}

Fragment Shader:

#version 330 core

layout(location=1) in vec3 vertexColor;

out vec4 color;

void main()
{
color = vec4(vertexColor,1.0);
}

The shaders are correct right? OpenGL wont produce a error,right?

Edit:

I have debugged it and it prints me out this message:

OpenGL.GL.shaders.ShaderCompilationError: ('Shader compile failure (0): b\'Fragment shader failed to compile with the following errors:\\nERROR: 2:1: error(#5) Extension: "explicit attribute location in fragment shader is only supported for the out storage qualifier"\\nERROR: error(#273) 1 compilation errors.  No code generated\\n\\n\'', [b'#version 330 core\n', b'\n', b'layout(location=1) in vec3 vertexColor;\n', b'\n', b'out vec4 color;\n', b'\n', b'void main()\n', b'{\n', b'color = vec4(vertexColor,1.0);\n', b'}\n', b'\n'], GL_FRAGMENT_SHADER)

But why is the vertex shader compilable while the fragment shader is not compilable?


Solution

  • The fragment shader cannot directly process the vertex attribute. You need to pass the attributes from the vertex to the fragment shader. Additionally the vertx shader must write to gl_Position (regardless of the version of the shader):

    Vertex Shader:

    #version 330 core
    
    layout(location=0) in vec3 vertexPos;
    layout(location=1) in vec3 vertexColor;
    
    out vec3 vColor;
    
    void main()
    {
        vColor = vertexColor;
        gl_Position = vec4(vertexPos,1.0);
    }
    

    Fragment Shader:

    #version 330 core
    
    in vec3 vColor;
    out vec3 color;
    
    void main()
    {
        color = vec4(vColor,1.0);
    }
    

    OpenGL Shading Language 4.60 Specification - Vertex Shader Special Variables:

    The variable gl_Position is intended for writing the homogeneous vertex position. It can be written at any time during shader execution. This value will be used by primitive assembly, clipping, culling, and other fixed functionality operations, if present, that operate on primitives after vertex processing has occurred. Its value is undefined after the vertex processing stage if the vertex shader executable does not write gl_Position.