2014-09-06 41 views
1

首先,我用OpenGL渲染點雲。glDrawElements不渲染所有的點

// The object pointCloud wraps some raw data in different buffers. 

// At this point, everything has been allocated, filled and enabled. 

glDrawArrays(GL_POINTS, 0, pointCloud->count()); 

這工作得很好。

但是,我需要渲染一個網格而不是點。爲了實現這一點,最明顯的方式似乎是使用GL_TRIANGLE_STRIP和glDrawElements以及良好的索引數組。

所以我開始通過應該呈現完全相同的東西來轉換我當前的代碼。

// Creates a set of indices of all the points, in their natural order 
std::vector<GLuint> indices; 
indices.resize(pointCloud->count()); 
for (GLuint i = 0; i < pointCloud->count(); i++) 
    indices[i] = i; 

// Populates the element array buffer with the indices 
GLuint ebo = -1; 
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo); 
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size(), indices.data(), GL_STATIC_DRAW); 

// Should draw the exact same thing as the previous example 
glDrawElements(GL_POINTS, indices.size(), GL_UNSIGNED_INT, 0); 

但它不能正常工作。它呈現的東西似乎只是第一季度的要點。
如果我將索引範圍縮小2或4倍,就會顯示相同的點。如果它小8倍,只有前半部分是。
如果我只用偶數索引填充它,則顯示同一組點的一半。
如果我在集合的一半處啓動它,則不顯示任何內容。

顯然,我錯過了有關glDrawElement與glDrawArrays相比的行爲。

在此先感謝您的幫助。

回答

3

作爲glBufferData()的第二個參數傳遞的大小以字節爲單位。發佈的代碼會傳遞索引的數量。通話需要是:

glBufferData(GL_ELEMENT_ARRAY_BUFFER, 
      indices.size() * sizeof(GLuint), indices.data(), GL_STATIC_DRAW); 
+0

非常感謝,我想我太盲目了,不能在正確的地方看! – etbh 2014-09-06 03:06:24