2015-12-03 81 views
1

在OpenGL中着色器,我想呈現兩個對象。每個由三角形的一組頂點位置和頂點索引定義。當我把我的緩衝區,我使用下面的代碼:繪製在OpenGL多個對象用不同的緩衝區

// Object 1 vertex positions 
glBindBuffer(GL_ARRAY_BUFFER, object1_vertex_buffer); 
glBufferData(GL_ARRAY_BUFFER, object1_vertices_size, object1_vertices, GL_STATIC_DRAW); 
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0); 

// Object 1 vertex indices 
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, object1_index_buffer); 
glBufferData(GL_ELEMENT_ARRAY_BUFFER, object1_indices_size, object1_indices, GL_STATIC_DRAW); 

// Object 2 vertex positions 
glBindBuffer(GL_ARRAY_BUFFER, object2_vertex_buffer); 
glBufferData(GL_ARRAY_BUFFER, object2_vertices_size, object2_vertices, GL_STATIC_DRAW); 
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0); 

// Object 2 vertex indices 
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, object2_index_buffer); 
glBufferData(GL_ELEMENT_ARRAY_BUFFER, object2_indices_size, object2_indices, GL_STATIC_DRAW); 

,然後當我使我的場景,我使用下面的代碼:

// Object 1 
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, object1_index_buffer); 
glDrawElements(GL_TRIANGLES, object1_num_indices, GL_UNSIGNED_INT, (void*)0); 

// Object 2 
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, object2_index_buffer); 
glDrawElements(GL_TRIANGLES, object2_num_indices, GL_UNSIGNED_INT, (void*)0); 

然而,這僅導致物體2被抽。我究竟做錯了什麼?

回答

1

如果你有openGL的3.3+你應該使用VAO:

glBindVertexArray(vao1); 
// Object 1 vertex positions 
glBindBuffer(GL_ARRAY_BUFFER, object1_vertex_buffer); 
glBufferData(GL_ARRAY_BUFFER, object1_vertices_size, object1_vertices, GL_STATIC_DRAW); 
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0); 

// Object 1 vertex indices 
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, object1_index_buffer); 
glBufferData(GL_ELEMENT_ARRAY_BUFFER, object1_indices_size, object1_indices, GL_STATIC_DRAW); 

glBindVertexArray(vao2); 
// Object 2 vertex positions 
glBindBuffer(GL_ARRAY_BUFFER, object2_vertex_buffer); 
glBufferData(GL_ARRAY_BUFFER, object2_vertices_size, object2_vertices, GL_STATIC_DRAW); 
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0); 

// Object 2 vertex indices 
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, object2_index_buffer); 
glBufferData(GL_ELEMENT_ARRAY_BUFFER, object2_indices_size, object2_indices, GL_STATIC_DRAW); 

,然後你只需要正確的VAO結合繪圖時:

// Object 1 
glBindVertexArray(vao1); 
glDrawElements(GL_TRIANGLES, object1_num_indices, GL_UNSIGNED_INT, (void*)0); 

// Object 2 
glBindVertexArray(vao2); 
glDrawElements(GL_TRIANGLES, object2_num_indices, GL_UNSIGNED_INT, (void*)0); 

否則就需要重複綁定和調用glVertexAttribPointer之間繪製:

// Object 1 
glBindBuffer(GL_ARRAY_BUFFER, object1_vertex_buffer); 
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0); 
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, object1_index_buffer); 
glDrawElements(GL_TRIANGLES, object1_num_indices, GL_UNSIGNED_INT, (void*)0); 

// Object 2 
glBindBuffer(GL_ARRAY_BUFFER, object2_vertex_buffer); 
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0); 
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, object2_index_buffer); 
glDrawElements(GL_TRIANGLES, object2_num_indices, GL_UNSIGNED_INT, (void*)0); 
+0

感謝。這是有道理的,我給它一個去,但它仍然不能正常工作....我已經發布了一個新的問題,我的更新代碼在:http://stackoverflow.com/questions/34072056/rendering-two-對象 - 與的OpenGL和 - 頂點數組的對象 – Karnivaurus

1

如果這是所有本代碼,那麼有幾個問題:

  • 一個人,使頂點致電glEnableVertexAttribArray屬性。
  • 在繪圖代碼中,您只重新綁定索引緩衝區,而不是頂點緩衝區。 glBindBuffer(GL_ARRAY_BUFFER,...glVertexAttribPointer都改變了當前狀態,所以你將覆蓋第一個對象設置和第二個對象設置。如果你真的想不VAOs工作,你就必須結合每個繪圖調用之前正確的緩衝和更新glVertexAttribPointer
  • 沒有使用任何VAO(VAOs在覈心配置文件工作時是強制性的,但我會建議使用它們還兼容模式)
相關問題