2012-07-20 70 views
1

當我將我的緩衝區綁定到我的着色器的屬性時,它們似乎正在翻轉。頂點屬性緩衝區綁定到錯誤的屬性

所以,我已經有了一個頂點着色器:

precision highp float; 

uniform mat4 projection_matrix; 
uniform mat4 modelview_matrix; 

in vec3 in_position; 
in vec3 in_color; 

out vec3 ex_Color; 

void main(void) 
{ 
    gl_Position = projection_matrix * modelview_matrix * vec4(in_position, 1); 
    ex_Color = in_color; 
} 

和碎片着色器

precision highp float; 

in vec3 ex_Color; 

out vec4 out_frag_color; 

void main(void) 
{ 
    out_frag_color = vec4(ex_Color, 1.0); 
} 

沒有什麼太複雜。有兩個輸入:一個用於頂點位置,另一個用於顏色。 (作爲一個新手,我不想處理紋理或光線。)

現在,在我的客戶端代碼中,我將數據放入兩個矢量數組positionVboData和colorVboData中,並創建了VBO .. 。

GL.GenBuffers(1, out positionVboHandle); 
GL.BindBuffer(BufferTarget.ArrayBuffer, positionVboHandle); 
GL.BufferData<Vector3>(BufferTarget.ArrayBuffer, 
         new IntPtr(positionVboData.Length * Vector3.SizeInBytes), 
         positionVboData, BufferUsageHint.StaticDraw); 

GL.GenBuffers(1, out colorVboHandle); 
GL.BindBuffer(BufferTarget.ArrayBuffer, colorVboHandle); 
GL.BufferData<Vector3>(BufferTarget.ArrayBuffer, 
       new IntPtr(colorVboData.Length * Vector3.SizeInBytes), 
       colorVboData, BufferUsageHint.StaticDraw); 

,然後,我會期望下面的代碼工作綁定駐維也納各組織的屬性爲着色器:

GL.EnableVertexAttribArray(0); 
    GL.BindBuffer(BufferTarget.ArrayBuffer, positionVboHandle); 
    GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, true, Vector3.SizeInBytes, 0); 
    GL.BindAttribLocation(shaderProgramHandle, 0, "in_position"); 

    GL.EnableVertexAttribArray(1); 
    GL.BindBuffer(BufferTarget.ArrayBuffer, colorVboHandle); 
    GL.VertexAttribPointer(1, 3, VertexAttribPointerType.Float, true, Vector3.SizeInBytes, 0); 
    GL.BindAttribLocation(shaderProgramHandle, 1, "in_color"); 

但是,事實上,我必須在最後一個代碼示例中交換positionVboHandle和colorVboHandle,然後它完美地工作。但那似乎是倒退給我的。我錯過了什麼?


更新

奇怪的事情是怎麼回事。如果我改變頂點着色器這樣的:

precision highp float; 

uniform mat4 projection_matrix; 
uniform mat4 modelview_matrix; 

in vec3 in_position; 
in vec3 in_color; 

out vec3 ex_Color; 

void main(void) 
{ 
    gl_Position = projection_matrix * modelview_matrix * vec4(in_position, 1); 
    //ex_Color = in_color; 
    ex_Color = vec3(1.0, 1.0, 1.0); 
}" 

並作出其他任何更改(不是修復其他建議所有的設置,它加載正確的屬性,頂點後移動程序鏈接。位置,進入IN_POSITION而不是進入in_color

回答

0

所以,在MārtiņšMožeiko的幫助下,我弄清楚了這一點。我正確地在LinkProgram之前調用BindAttribLocation。但是,在我綁定任何屬性位置之前,我並沒有調用GL.CreateProgram()。

+0

您應該使用glGetError函數驗證所有OpenGL調用。它會立即顯示錯誤的函數調用。 – 2012-07-23 00:41:03

4

GL.BindAttribLocation必須GL.LinkProgram之前執行你的代碼片段後調用GL.LinkProgram

編輯: 回答您的更新 - 因爲你不使用in_color,所以OpenGL只是忽略這個輸入。而你的頂點着色器只能以in_position作爲輸入。最有可能的是它在位置0綁定它。這就是爲什麼你的代碼有效。您應該在鏈接程序之前綁定位置,如上面鏈接中所述。

+0

最初,我沒有(我認爲我正在使用的一個例子也有錯誤),但我修復了它,並且看到了同樣的問題。 – jwrush 2012-07-20 08:42:31

+0

我搜索了BindAttribLocation,並在每個之前放置了一個調試輸出,並搜索了LinkProgram並在其前面放置了一個調試輸出。我得到: 綁定屬性... 綁定屬性... 正在鏈接... – jwrush 2012-07-22 17:02:55

+0

通過您的幫助指出我正確的方向。謝謝! – jwrush 2012-07-22 17:07:34