输入属性的顺序如何影响渲染结果?

时间:2013-02-22 10:08:04

标签: opengl glsl

两个输入变量的交换顺序会破坏渲染结果。那是为什么?

关于它的使用的信息很少:

  • vertexPosition_modelspace的位置为0,vertexColor的位置为1
  • 我绑定缓冲区存储顶点位置并设置顶点attrib指针,然后我绑定并设置缓冲区颜色

正确的一个:

#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;
}

right order result

错误之一:

#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;
}

enter image description here

同样的问题是texcoords,我花了一个小时来发现问题。如果我在位置后放置texcoord或颜色输入,为什么结果会被破坏?订单无关紧要。

1 个答案:

答案 0 :(得分:0)

这是因为您将数据传递给着色器时使用的顺序。在OpenGL C或C ++代码中,您肯定会将顶点颜色作为第一个顶点attrib发送,然后发送位置。如果您打算在着色器中交换参数的顺序,则必须交换其初始化的顺序。

相关问题