2009-08-02 96 views
6

我只是嘗試使用VBOs渲染第一個紅皮書示例(白色四邊形)。
它適用於即時模式和頂點數組。使用OpenGL的VBO問題

但是,當使用VBOs屏幕保持黑屏。我想我一定錯過了一些重要的東西。

初始化:

unsigned int bufIds[2]; 
glGenBuffers(2, bufIds); 
GLfloat vertices[] = { 
    0.25, 0.25, 0.0, 
    0.75, 0.25, 0.0, 
    0.75, 0.75, 0.0, 
    0.25, 0.75, 0.0 
}; 
glBindBuffer(GL_ARRAY_BUFFER, bufIds[0]); 
glBufferData(GL_ARRAY_BUFFER, 12, vertices, GL_STATIC_DRAW); 
glBindBuffer(GL_ARRAY_BUFFER, 0); 

glClearColor(0, 0, 0, 1); 
glColor3f(1, 1, 1); 
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0); 

renderloop的VBO(不工作):

glClear(GL_COLOR_BUFFER_BIT); 
glEnableClientState(GL_VERTEX_ARRAY); 
glBindBuffer(GL_ARRAY_BUFFER, bufIds[0]); 
glVertexPointer(3, GL_FLOAT, 0, 0); 
glDrawArrays(GL_QUADS, 0, 12); 
glBindBuffer(GL_ARRAY_BUFFER, 0); 
glDisableClientState(GL_VERTEX_ARRAY); 

renderloop頂點數組(工作):

glClear(GL_COLOR_BUFFER_BIT); 
glEnableClientState(GL_VERTEX_ARRAY); 
glBindBuffer(GL_ARRAY_BUFFER, 0); 
glVertexPointer(3, GL_FLOAT, 0, vertices); 
glDrawArrays(GL_QUADS, 0, 12); 
glDisableClientState(GL_VERTEX_ARRAY); 

回答

5

哎呀我剛琢磨出來的試圖讀回緩衝區的內容:

我需要用12 *的sizeof(GLfloat)分配的緩衝區,而不是隻有12

glBufferData(GL_ARRAY_BUFFER, 12 * sizeof(GLfloat), vertices, GL_STATIC_DRAW); 

我回讀碼

GLfloat vertices2[12]; 
glBindBuffer(GL_ARRAY_BUFFER, bufIds[0]); 
glGetBufferSubData (GL_ARRAY_BUFFER, 0, 12 * sizeof(GLfloat), vertices2); 
glBindBuffer(GL_ARRAY_BUFFER, 0); 

for (int i = 0; i < 4; i ++) { 
    LOG_DEBUG << "point " << i << ": " << vertices2[ i * 3 + 0 ] << "/" << vertices2[ i * 3 + 1 ] << "/" << vertices2[ i * 3 + 2 ]; 
} 
+0

+1真棒!我有完全相同的問題,非常感謝你! – 2012-11-11 11:40:31