Domanda

I write the following simple code to learn how to use vbo, but doesn't appear anything:

GLuint vbo=0;
GLfloat data[] = {175.0, 25.0, 0.0, 1.0, 0.0, 0.0, 100.0, 325.0, 0.0, 0.0,1.0,0.0,
250.0, 25.0, 0.0, 0.0, 0.0, 1.0};

void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(data),data, GL_STATIC_DRAW);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, 0);
glEnableClientState(GL_COLOR_ARRAY);
glColorPointer(3, GL_FLOAT, 0, (void*)(3*sizeof(float)) );
glDrawArrays(GL_TRIANGLES, 0,3);
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glutSwapBuffers();
glFlush();
}
void reshape(int w, int h){
 glClearColor(0.5,0.2,0.5,1);
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, (GLsizei) w, 0, (GLsizei) h, -1,1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

}
int main(int argc, char** argv){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(350, 350);
glutInitWindowPosition(100, 100);
glutCreateWindow("VBO");
glutDisplayFunc(&display);
glutReshapeFunc(reshape);
glutMainLoop();

}

Where are the errors?

È stato utile?

Soluzione

You're passing a stride of 0, which means the data should be tightly packed (all vertices together, all colours together etc). But your data array appears to be interleaved. Change the pointer calls to:

glVertexPointer(3, GL_FLOAT, 6 * sizeof(GLfloat), 0);

glColorPointer(3, GL_FLOAT, 6 * sizeof(GLfloat), (void*)(3 * sizeof(GLfloat)));

As a side note, you definitely shouldn't be generating a brand new VBO in each call to display().

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top