To my understanding, input attributes of vertex computed in vertex will be interpolated according to barycentric coordinate of current pixel. And being able to interpolate attributes or to compute barycentric coordinate of current pixel is because the vertex stream is transited to triangle stream after vertex shader. The barycentric coordinate of current pixel can be derived by the screen positions of triangle vertices provided by gl_Position
and the pixel position.
But I'm confused how to interpolate in
variables in fragment shader. Here is an example of shader:
layout(binding = 0) uniform WorldMVP {
mat4 worldMvp;
};
layout(binding = 0) uniform LightMVP{
mat4 lightMvp;
};
layout(location = 0) in vec3 aVertexPosition;
layout(location = 1) in vec3 aVertexNormal;
layout(location = 2) in vec2 aTextureCoord;
layout(location = 0) out vec4 vPositionFromLight;
layout(location = 1) out vec2 vTextureCoord;
layout(location = 2) out vec3 vNormal;
void main()
{
gl_Position = worldMvp * vec4(aVertexPosition, 1.0);
vPositionFromLight = lightMvp * vec4(aVertexPosition, 1.0);
vTextureCoord = aTextureCoord;
vNormal = aVertexNormal;
}
layout(location = 0) in vec4 vPositionFromLight;
layout(location = 1) in vec2 vTextureCoord;
layout(location = 2) in vec3 vNormal;
layout(location = 0) out vec4 outColor;
void main()
{
outColor = vec4(1.0, 1.0, 1.0, 1.0);
}
If the barycentric coordinate used in interpolating vPositionFromLight
is same as the one used in interpolating attributes like vTextureCoord
and vNormal
, it seems abnormal. Because vPositionFromLight
and gl_Position
are transformed into different clip spaces by different MVP.
How does the vPositionFromLight
is interpolated? What is the barycentric coordinate used in interpolating vPositionFromLight
?
Because vPositionFromLight and gl_Position are transformed into different clip spaces by different MVP.
As far as OpenGL is concerned, they're just numbers. Is vPositionFromLight
in a "clip space"? OpenGL doesn't care; they are a vec4
, and that vec4
will get the same interpolation math as any other vertex shader output.
The space of the post-interpolation value is the same space as the pre-interpolation result.