I am trying to pass the vertex positions as a simple out vec3 pos
from the vertex shader to the fragment shader in order to color vertices based on their position. Here is my vertex shader code:
#version 330 core
in vec3 position;
out vec3 pos;
uniform mat4 u_model;
void main(){
pos = position;
gl_Position = u_model * vec4(position, 1.0f);
}
Fragment shader:
#version 330 core
out vec4 color;
in vec4 pos;
void main(){
color = vec4(pos.x, 0.0f, 0.0f, 1.0f);
}
Now this only works when I run the program with the Nsight Graphics Debugger in Visual Studio 2019.
When I execute normally (x64, Debug) it seems to fall back to a default shader, which renders everything white. The vertex and fragment shader posted above stop working.
The type of the vertex shader output has to match exactly the type of the corresponding input in the next shader stage (fragment shader in this case).
See interface matching rules between shader stages.
In your case the type of the vertex shader output is vec3
:
out vec3 pos;
but the type of the fragment shader input is vec4
:
in vec4 pos;
Change the type of the fragment shader input to vec3
, to solve the issue.
I don't know why this code works with Nsight Graphics Debugger in Visual Studio 2019. The bug is that it should not do so.