2016-12-28 56 views
1

創建我的緩衝區下面的代碼:glDrawElements不拉絲

//generate buffers 
glGenVertexArrays(1, &VAO); 
//glGenBuffers(1, &EBO); 
glGenBuffers(1, &VBO_vertices); 
glGenBuffers(1, &VBO_colors); 
glGenBuffers(1, &VBO_normals); 

// Bind the Vertex Array Object first, then bind and set vertex buffer(s) and attribute pointer(s). 
glBindVertexArray(VAO); 

// Copy our vertices array in a buffer for OpenGL to use 
glBindBuffer(GL_ARRAY_BUFFER, VBO_vertices); 
glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*vertices.size(), &vertices[0], GL_STATIC_DRAW); 


glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); 
glBufferData(GL_ELEMENT_ARRAY_BUFFER, vTable.size() * sizeof(int), &vTable[0], GL_STATIC_DRAW); 

// Position attribute 
glVertexAttribPointer((GLuint)0, 3, GL_FLOAT, GL_FALSE, 0, (GLvoid*)0); //size = 3 (X,Y,Z) 
glEnableVertexAttribArray(0); 


//Buffer for color 
glBindBuffer(GL_ARRAY_BUFFER, VBO_colors); 
glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*vertices.size(), &v_color[0], GL_STATIC_DRAW); 

// Color attribute 
glVertexAttribPointer((GLuint)1, 3, GL_FLOAT, GL_FALSE, 0, (GLvoid*)0); //size = 3 (R,G,B) 
glEnableVertexAttribArray(1); 


//Buffer for normals 
glBindBuffer(GL_ARRAY_BUFFER, VBO_normals); 
glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*vertices.size(), &v_normals[0], GL_STATIC_DRAW); 

//normal attribute 
glVertexAttribPointer((GLuint)2, 3, GL_FLOAT, GL_FALSE, 0, (GLvoid*)0); //size = 3 (R,G,B) 
glEnableVertexAttribArray(2); 


// Unbind the VAO 
glBindVertexArray(0); 

我的數據是:

vector<vec3> vertices, v_normals,v_color; 
vector<int> vTable; 

我有頂點,法線和每頂點顏色和索引表與索引頂點每個三角形。

當我嘗試渲染這個時,窗口上沒有任何東西出現。

glBindVertexArray(VAO); //Bind VAO 
glDrawElements(GL_TRIANGLES, vTable.size(), GL_UNSIGNED_INT, &vTable[0]); 
glBindVertexArray(0); //Unbind VAO 

如果我用這個:

glDrawArrays(GL_TRIANGLES,0,vTable.size()); 

它借鑑的東西,但一個不完整的對象,如鏈接形象。

image

任何人知道會發生什麼?在此先感謝

+0

'glDrawElements'的最後一個參數是索引緩衝區中的偏移量。所以你應該傳遞一個空指針。 –

回答

2

glDrawElements電話是錯的,最後一個參數應該是一個字節偏移到您GL_ELEMENT_ARRAY_BUFFER持有的指數,而不是指向的系統內存。

glDrawElements(GL_TRIANGLES, vTable.size(), GL_UNSIGNED_INT, 0); 
+0

這是真的!謝謝 – ladymac