문제 설명
정점 셰이더의 out 변수는 어떻게 보간됩니까? (How are the out variables of vertex shader interpolated?)
꼭짓점에서 계산된 꼭짓점의 입력 속성은 현재 픽셀의 무게 중심 좌표에 따라 보간됩니다. 그리고 속성
을 보간하거나 현재 픽셀의 무게 중심 좌표를 계산할 수 있는 것은 정점 셰이더 이후 정점 스트림이 삼각형 스트림으로 전환되기 때문입니다. 현재 픽셀의 무게 중심 좌표는 gl_Position
에서 제공하는 삼각형 꼭짓점의 화면 위치와 픽셀 위치에 의해 파생될 수 있습니다.
하지만 in 프래그먼트 셰이더의
변수. 다음은 셰이더의 예입니다.
- 정점 셰이더
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 mian()
{
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);
}
vPositionFromLight
보간에 사용된 무게 중심 좌표가 vTextureCoord
및 vNormal
과 같은 속성 보간에서 사용된 것과 동일하면 비정상적으로 보입니다. vPositionFromLight
및 gl_Position
은 다른 MVP에 의해 다른 클립 공간으로 변환되기 때문입니다.
vPositionFromLight
는 어떻게 보간됩니까? vPositionFromLight
를 보간하는 데 사용되는 무게 중심 좌표는 무엇입니까?
gl_Position
은 다른 MVP에 의해 다른 클립 공간으로 변환됩니다. vPositionFromLight
는 어떻게 보간됩니까? vPositionFromLight
를 보간하는 데 사용되는 무게 중심 좌표는 무엇입니까?
gl_Position
은 다른 MVP에 의해 다른 클립 공간으로 변환됩니다. vPositionFromLight
는 어떻게 보간됩니까? vPositionFromLight
를 보간하는 데 사용되는 무게 중심 좌표는 무엇입니까?
참조 솔루션
방법 1:
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.
(by wangsy、Nicol Bolas)