Frage

I have a working Vertex-Buffer-Object but I need to add the normals. The normales are stored in the same array as the vertex positons. They are interleaved

Vx Vy Vz Nx Ny Nz

This is my code so far:

GLfloat values[NUM_POINTS*3 + NUM_POINTS*3]; void initScene() {

for(int i = 0; i < (NUM_POINTS) ; i = i+6){
    values[i+0] = bunny[i];
    values[i+1] = bunny[i+1];
    values[i+2] = bunny[i+2];
    values[i+3] = normals[i];
    values[i+4] = normals[i+1];
    values[i+5] = normals[i+2];
}

glGenVertexArrays(1,&bunnyVAO);
glBindVertexArray(bunnyVAO);


glGenBuffers(1, &bunnyVBO);
glBindBuffer(GL_ARRAY_BUFFER, bunnyVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(bunny), bunny, GL_STATIC_DRAW);

glVertexAttribPointer(0,3, GL_FLOAT, GL_FALSE, 0,0);
glEnableVertexAttribArray(0);

glGenBuffers(1, &bunnyIBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bunnyIBO);
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);
}

void renderScene() {
  if (bunnyVBO != 0) {
    // x: bind VAO //
      glEnableClientState(GL_VERTEX_ARRAY);
      glBindVertexArray(bunnyVAO);
      glDrawElements(GL_TRIANGLES, NUM_TRIANGLES, GL_UNSIGNED_INT, NULL);
      glDisableClientState(GL_VERTEX_ARRAY);
    // unbind active buffers //
    glBindVertexArray(0);
  }
}

I can see something on the screen but it is not right as the normals are not used correctly... How can I use the values array correctly connected with my code so far.

War es hilfreich?

Lösung

You need to call glVertexAttribPointer two times, once for the vertices and once for the normals. This is how you tell OpenGL how your data is layed out inside your vertex buffer.

// Vertices consist of 3 floats, occurring every 24 bytes (6 floats), 
// starting at byte 0.
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 24, 0);

// Normals consist of 3 floats, occurring every 24 bytes starting at byte 12.
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 24, 12);

This is assuming that your normal attribute in your shader has an index of 1.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top