0

I have a pyramid which has 5 vertex and 18 indices. As I want to add normals to each face I just found solution for normals for each vertex. That means I can't use indices to define my pyramid I need to have 18 vertex (and 3 times the same vertex for the same point in space).

There must be a solution to use normals not on vertex base but on index base.

Some code (javascript):

var vertices = [
    -half, -half,  half, // 0 front left
     half, -half,  half, // 1 front right
     half, -half, -half, // 2 back right
    -half, -half, -half, // 3 back left
      0.0,  Math.sqrt((size * size) - (2 * (half * half))) - half,   0.0  // 4 top
];

var vertexNormals = [
    // front face
     normaleFront[0],  normaleFront[1],  normaleFront[2],
     normaleFront[0],  normaleFront[1],  normaleFront[2],
     normaleFront[0],  normaleFront[1],  normaleFront[2],

    // back face
     normaleBack[0],  normaleBack[1],  normaleBack[2],
     normaleBack[0],  normaleBack[1],  normaleBack[2],
     normaleBack[0],  normaleBack[1],  normaleBack[2],

    // left face
     normaleLeft[0],  normaleLeft[1],  normaleLeft[2],
     normaleLeft[0],  normaleLeft[1],  normaleLeft[2],
     normaleLeft[0],  normaleLeft[1],  normaleLeft[2],

    // right face
     normaleRight[0],  normaleRight[1],  normaleRight[2],
     normaleRight[0],  normaleRight[1],  normaleRight[2],
     normaleRight[0],  normaleRight[1],  normaleRight[2],

    // bottom face
     0.0, -1.0, 0.0,
     0.0, -1.0, 0.0,
     0.0, -1.0, 0.0,
     0.0, -1.0, 0.0,
     0.0, -1.0, 0.0,
     0.0, -1.0, 0.0,
];

var pyramidVertexIndices = [
    0, 1, 4, // Front face
    2, 3, 4, // Back face
    3, 0, 4, // Left face
    1, 2, 4, // Right face
    0, 1, 2,   2, 3, 0, // Bottom face
];
WarrenFaith
  • 57,492
  • 25
  • 134
  • 150
  • Just to add: it can't be done... what can be done is to create a second array filled with the vertex in the order and dimension of the index array... so you can change one vertex and it will be changed at all places in the generated second array – WarrenFaith Jan 05 '11 at 21:04

1 Answers1

1

Each vertex of a pyramid has three different normals, depending on which face it belongs to. So you need to pass each of the vertices three times, each time with a different normal, to use glDrawElements.

Alternatively you could call

// Face 1
glNormal
glVertex
glVertex
glVertex

// Face 2 glNormal glVertex glVertex glVertex

// ...

Roman Zenka
  • 3,514
  • 3
  • 31
  • 36
  • Isn't the idea of indices to use them to point to the same vertex instead of creating the same vertex over and over again? I need a possibility to define a normal for an index and not for the vertex. I want to use the indices to reduce the number of vertices... – WarrenFaith Jan 02 '11 at 22:42
  • 2
    This is well explained here: http://stackoverflow.com/questions/1664402/how-can-i-specify-per-face-colors-when-using-indexed-vertex-arrays-in-opengl-3-x - a Vertex is a set of vertex attributes - all attributes, not just the position. You cannot mix and match them from several places, that is not how the GPU works internally. – Roman Zenka Jan 02 '11 at 22:58