简体   繁体   中英

OpenGL Vertex Buffer Object does not work

I have a bunny.h which contains the following data:

bunny[] | vertex coordinates xyz

NUM_TRIANGLES | the amount of triangles for the bunny

normals[] | the normals for each triangle

triangles[] | indices for the triangles

I want to use the data for a vertex buffer object.

here is how I try to load the data

GLfloat values[NUM_TRIANGLES*3];
    for(int i = 0; i < NUM_TRIANGLES*3; i++)
        values[i] = bunny[i];

  //  init and bind a VBO (vertex buffer object) //
    glGenBuffers(1, &bunnyVBO);
    glBindBuffer(GL_ARRAY_BUFFER, bunnyVBO);


  //  copy data into the VBO //
    glBufferData(GL_ARRAY_BUFFER, sizeof(values), &values, GL_STATIC_DRAW);

  // init and bind a IBO (index buffer object) //
    glGenBuffers(1, &bunnyIBO);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bunnyIBO);

  //  copy data into the IBO //
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(triangles), &triangles, GL_STATIC_DRAW);
  // unbind active buffers //
  glBindVertexArray(0);
  glBindBuffer(GL_ARRAY_BUFFER, 0);
  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

Later in the program I want to render the buffers using this code:

glBindBuffer(GL_ARRAY_BUFFER, bunnyVBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bunnyIBO);
glEnableClientState(GL_VERTEX_ARRAY);
glDrawElements(GL_TRIANGLES, NUM_TRIANGLES, GL_UNSIGNED_INT, triangles);
glDisableClientState(GL_VERTEX_ARRAY);

OpenGL is working fine, but I dont see the bunny... (the data is not corrupted or anything like that, the error is in my code) Can some please help me?

I don't see any call to glVertexPointer . And if you want to use the elements from the VBO, it should be

glDrawElements(GL_TRIANGLES, NUM_TRIANGLES, GL_UNSIGNED_INT, 0);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM