2009-08-02 7 views
6

J'ai juste essayé de rendre le premier exemple redbook (le Quad blanc) en utilisant les VBO.
Cela fonctionne très bien avec le mode immédiat et les tableaux de vertex.Problème avec l'utilisation de VBO OpenGL

Mais lorsque vous utilisez des VBO, l'écran reste noir. Je pense que j'ai dû manquer quelque chose d'important.

INIT:

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); 

boucle de rendu pour VBO (ne fonctionne pas):

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); 

boucle de rendu pour les tableaux de sommet (travail):

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); 

Répondre

5

Argh je viens figured it out par essayer de relire le contenu de la mémoire tampon:

i besoin d'affecter le tampon avec 12 * sizeof (GLfloat) au lieu de seulement 12

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

mon relue Code

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 Impressionnant! J'ai eu exactement le même problème, merci beaucoup! –