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 6 of 6

Thread: Weird Glitch Drawing Every Other Triangle

Hybrid View

  1. #1
    Newbie Newbie
    Join Date
    Sep 2013
    Posts
    3

    Weird Glitch Drawing Every Other Triangle

    Can anyone help me out with what might be causing this to happen:
    d (dot) pr/i/GB1Q -- Sorry, can't post links yet
    This is my first time using indices and I don't know what's wrong. I made a simple shape in Cinema 4D and exported it, then imported it into the program. Any OBJ I import does the same thing. Does anyone have an idea what's causing this? I can post any code (java) if necessary.
    Thanks in advance!

  2. #2
    Advanced Member Frequent Contributor
    Join Date
    Apr 2010
    Posts
    790
    OBJ files often use different indices for each vertex attribute (position, normal, color, tex coord, etc.). This is not supported by OpenGL, you can only have one index that selects all enabled attributes for the vertex. You have to convert the data to use just a single index. If you search this forum you'll find many threads with instructions how to do that.

  3. #3
    Newbie Newbie
    Join Date
    Sep 2013
    Posts
    3
    That makes a lot of sense, but I can't seem to find any of these threads. Could you link me to one?

  4. #4
    Advanced Member Frequent Contributor
    Join Date
    Apr 2010
    Posts
    790
    See this one for example.

  5. #5
    Newbie Newbie
    Join Date
    Sep 2013
    Posts
    3
    I'm having trouble following that, sorry. I'm really new to OpenGL and can only understand Java. Anything you can suggest for me?

  6. #6
    Advanced Member Frequent Contributor
    Join Date
    Apr 2010
    Posts
    790
    The problem is basically this: an object in OBJ has a bunch of attributes for each vertex (say position, normal, tex coords), so we have 3 arrays (or similar data structure):
    Code :
    vec3f position[50];
    vec3f normal[100];
    vec2f texCoord[80];

    The array sizes are completely arbitrary, but note that they can be different for the different attributes. Additionally, for an object with 3 attributes per vertex, a face is defined by a sequence of index triples. Each triple defines one vertex of the face and the first element of the triple is an index into the positions array, the second into the normal array, the third into the texCoord array. A triangle would be given by 3 triples, for example: 3/9/2 5/11/17 4/2/18. As mentioned before OpenGL only supports a single index for all active attributes, so the attributes and the index have to be rearranged to fulfil that requirement. Pseudo code (very similar to tonyo_au's version):

    Code :
    vec3f newPosition[100];
    vec3f newNormal[100];
    vec2f newTexCoord[100];
    uint  newIndex[200];
     
    uint nextAttrib = 0;
    uint nextIndex = 0;
     
    for each index tuple:
       if index tuple has NOT occured before:
           // append attributes referenced by index tuple to new attributes
           newPosition[nextAttrib] = position[index.pos];
           newNormal[nextAttrib]  = normal[index.normal];
           newTexCoord[nextAttrib] = texCoord[index.texCoord];
     
          remember that index tuple is associated with nextAttrib
     
          // store a single new index that references the same attributes
          newIndex[nextIndex] = nextAttrib;
          ++nextIndex;
          ++nextAttrib;
       else:
          newIndex[nextIndex] = index associated with index tuple
          ++nextIndex;

    The tricky part is the "remember that index tuple is associated with nextIndex" bit, for that you need some kind of (hash-) map data structure that allows you to use an index tuple as key and a new index as value.

    The algorithm becomes simpler if you don't want to generate a new index and don't mind the extra memory use of (unnecessarily) duplicated vertex attributes: You simply assume the if branch is always taken and don't bother remembering if you've seen an index tuple before and what new index value it is associated with.

Posting Permissions

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