两个输入变量的交换顺序会破坏渲染结果。那是为什么?
关于它的使用的信息很少:
正确的一个:
#version 130
// Input vertex data, different for all executions of this shader.
in vec3 vertexColor;
in vec3 vertexPosition_modelspace;
// Output data ; will be interpolated for each fragment.
out vec3 fragmentColor;
// Values that stay constant for the whole mesh.
uniform mat4 MVP;
void main(){
// Output position of the vertex, in clip space : MVP * position
gl_Position = MVP * vec4(vertexPosition_modelspace,1);
// The color of each vertex will be interpolated
// to produce the color of each fragment
fragmentColor = vertexColor;
}
错误之一:
#version 130
// Input vertex data, different for all executions of this shader.
in vec3 vertexPosition_modelspace; // <-- These are swapped.
in vec3 vertexColor; // <--
// Output data ; will be interpolated for each fragment.
out vec3 fragmentColor;
// Values that stay constant for the whole mesh.
uniform mat4 MVP;
void main(){
// Output position of the vertex, in clip space : MVP * position
gl_Position = MVP * vec4(vertexPosition_modelspace,1);
// The color of each vertex will be interpolated
// to produce the color of each fragment
fragmentColor = vertexColor;
}
同样的问题是texcoords,我花了一个小时来发现问题。如果我在位置后放置texcoord或颜色输入,为什么结果会被破坏?订单无关紧要。
答案 0 :(得分:0)
这是因为您将数据传递给着色器时使用的顺序。在OpenGL C或C ++代码中,您肯定会将顶点颜色作为第一个顶点attrib发送,然后发送位置。如果您打算在着色器中交换参数的顺序,则必须交换其初始化的顺序。