Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 4 of 4

Thread: Sending UV/Normal index info to glDrawElements

  1. #1

    Sending UV/Normal index info to glDrawElements

    When I parse out 3d model info from almost any format, I get 6 different arrays like so.

    Code :
    Vector* vertArray;
    int*    vertIndicies;
     
    Vector* normalArray;
    int*    normalIndicies;
     
    Vector* uvArray;
    int*    uvIndicies;

    When I first set off to making my 3D engine, I used OpenGL's immediate mode (SLOW). I am drawing my objects like so.

    Code :
    //Psuedo Code
    for each face
       for each vert in face
          glTexCoord2dv(getUV(  face,vert))
          glNormal3dv(  getNorm(face,vert))
          glVertex3dv(  getVert(face,vert))

    Just for clarity, here is an example of how the getUV()
    function will get the UV. I have an array of faces (aka polygons), The face knows what index its first vert is, and then I offest by the index of the vert in that face (usually 0-3).

    Code :
    getUV(int faceIndex, int vertIndex) {
    return uvArray[ uvIndicies[ faceArray[faceIndex].startIndex + vertIndex ] ];
    }

    Long story short, this works. But only because I have separate index arrays from my verts, UVs and Normals. As we all know a single vertex can have different UVs or normals, depending on which face we are associating it with.

    My problem is when I switched over to vertex arrays I have code like this:

    Code :
    glVertexPointer(3, GL_DOUBLE, sizeof(Vector), vertArray);
    glNormalPointer(GL_DOUBLE, sizeof(Vector), normalsArray);
    glTexCoordPointer(2, GL_INT, sizeof(Vector), uvArray);
    glDrawElements(GL_QUADS, numVertIndices, GL_UNSIGNED_INT, vertIndicies);

    Oh, great, my model draws corrrectly, but the UVs, and normals are all wrong.

    Is OpenGL using the vertIndices to index into my normal and uv array?! How can I tell OpenGL to use used my other index arrays to get UV and normal data?

    Thanks All,
    ~GameQ

  2. #2
    Member Regular Contributor DmitryM's Avatar
    Join Date
    Mar 2009
    Location
    Toronto
    Posts
    436

    Re: Sending UV/Normal index info to glDrawElements

    You can't. Either don't use indexes or agree about 1 each vertex attribute per index.

    You can duplicate the vertexes that are used with different UV sets to make them follow the rule.

    Alternatively, you can supply more then one UV sets and choose one of them on the fly.

  3. #3

    Re: Sending UV/Normal index info to glDrawElements

    Ok... are you talking about something like this then

    Code :
    struct vert
    {
       Vector pos;
       Vector norm;
       Vector uv;
    }

    and then say for a Cube, I'd have to have 24 of those vert structs (4 verts for each of the 6 faces).

    I thought the whole point of using index arrays was to avoid having to repeat shared verts?

    Thanks,
    ~GameQ

  4. #4
    Member Regular Contributor DmitryM's Avatar
    Join Date
    Mar 2009
    Location
    Toronto
    Posts
    436

    Re: Sending UV/Normal index info to glDrawElements

    You can get rid of duplicated vertices *only* if all vertex attributes are duplicated.

    For a solid cube there are no 2 vertices sharing both normal & vertex, so indexing gives you nothing (24 vertices).

    Indexing works for smooth models (almost any character model is smooth), because vertices start sharing normal in this case. The only trouble left is UV.

    For a smooth cube you'll have 14 vertices in case of a standard cube unwrapping.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •