2013-02-22 63 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