1

I have the following fragment and vertex shader.

Vertex:

    #version 450

    layout(location = 0) in vec2 Position;
    layout(location = 1) in vec4 Color;

    layout(location = 0) out vec2 fPosition;

    void main()
    {
        gl_Position = vec4(Position, 0, 1);
        fPosition = Position;
    }

Fragment:

    #version 450
    
    layout(location = 0) in vec2 fPosition;
    layout(location = 0) out vec4 fColor;

    void main() {
        vec4 colors[4] = vec4[](
            vec4(1.0, 0.0, 0.0, 1.0), 
            vec4(0.0, 1.0, 0.0, 1.0), 
            vec4(0.0, 0.0, 1.0, 1.0), 
            vec4(0.0, 0.0, 0.0, 1.0)
        );

        fColor = vec4(1.0);

        for(int row = 0; row < 2; row++) {
            for(int col = 0; col < 2; col++) {
                float dist = distance(fPosition, vec2(-0.50 + col, 0.50 - row));
                float delta = fwidth(dist);
                float alpha = smoothstep(0.45-delta, 0.45, dist);
                fColor = mix(colors[row*2+col], fColor, alpha);
            }
        }
    }

But when compiling this I am getting the following error:

cannot convert from ' gl_Position 4-component vector of float Position' to 'layout( location=0) smooth out highp 2-component vector of float'

And i have no clue how to fix it. (this is my first time doing shader programming).
If additional information is needed please let me know.

FutureCake
  • 2,614
  • 3
  • 27
  • 70

1 Answers1

0

1. You do not need to specify layouts when transferring variables between vertex shader and fragment shader. Remove the layout(location = 0) parameter for the fPosition variable in the vertex and fragment shader.

2. You only need to specify layout if you passing the variables (your position buffers) to the vertex shader through buffers. To add on, variables like positions, normals and textureCoords must always pass through the vertex shader first and then to the fragment shader.

3. When exporting your final colour (fColor in your case) from the fragment shader, you do not need to pass a location, just specify the vector4 variable as out vec4 fColor; openGL detects it automatically.

4. The error you actually got was telling you that you were assigning vector4 variable (fColor) to your already stored vec2 variables (fPosition). Note: In your vertex shader at attribute (location) "0", you had accessed the vertices that you had loaded, but you tried to assign a vector4 to the same location later in the fragment shader. OpenGL does not automatically overwrite data like that.

Dstarred
  • 321
  • 2
  • 10
  • I would highly recommend you look at `GLSL Shading Language` (OpenGL) tutorials. There are many of them on the internet and if you do not like reading a lot, search Youtube; many Youtubers have done 3D Game tutorials. See ThinMatrix's OpenGL tutorials on shaders (video 4 and 5) [link](https://www.youtube.com/playlist?list=PLRIWtICgwaX0u7Rf9zkZhLoLuZVfUksDP) – Dstarred Jul 27 '21 at 05:55