2013-02-22 68 views
0

交换两个输入变量的顺序会破坏渲染结果。这是为什么?输入属性的顺序如何影响渲染结果?

关于它的一点信息的使用:

  • vertexPosition_modelspace具有位置0和顶点颜色具有位置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或颜色输入,为什么结果会损坏?订单不应该紧。

回答

0

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

+0

是的。我绑定缓冲区并为位置先设置指针(位置= 0),然后设置颜色(位置= 1)。为什么在着色器中的顺序相反? – kravemir 2013-02-22 10:27:40

+0

@Miro:因为你没有强制编译器将它们放到特定的位置。使用'布局(位置= ...)'存储限定符来固定它们。 – datenwolf 2013-02-22 10:32:18

+0

感谢您的回答。它以正确的方式指出了我。但这并不完全正确。在指定attrib位置之前,问题是我链接的程序。 – kravemir 2013-02-22 10:41:13