Search code examples
openglverticesnormals

glDrawElements with indices applied to vertices and normals


Is it possible to use the glDrawElements method when you have lets say 2 arrays (one for Normals and one for Vertices) and use the Index-buffer interleaved between vertices and normals).

EXAMPLE: rendering a Cube

// 8 of vertex coords
GLfloat vertices[] = {...};
// 6 of normal vectors
GLfloat normals[] = {...};
// 48 of indices (even are vertex-indices, odd are normal-indices)
GLubyte indices[] = {0,0,1,0,2,0,3,0,
                     0,1,3,1,4,1,5,1,
                     0,2,5,2,6,2,1,2,
                     1,3,6,3,7,3,2,3,
                     7,4,4,4,3,4,2,4,
                     4,5,7,5,6,5,5,5};
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, vertices);
glNormalPointer(3, GL_FLOAT, 0, normals);
glDrawElements(GL_QUADS,...);//?see Question

Solution

  • No, see the documentation of glDrawElements().

    You can only achieve 'interleaving' by using interleaved data (not interleaved indices), either via glInterleavedArrays (see here):

    float data[] = { v1, v2, v3, n1, n2, n3 .... };
    glInterleavedArrays(GL_N3F_V3F, 0, data);
    glDrawElements(...);
    

    or via:

    float data[] = { v1, v2, v3, n1, n2, n3 };
    glVertexPointer(3, GL_FLOAT, sizeof(float) * 3, data);
    glNormalPointer(3, GL_FLOAT, sizeof(float) * 3, data + sizeof(float) * 3);
    glDrawElements(...);
    

    as you can see, glInterleavedArrays() is just some sugar around glInterleavedArrays() and friends.