Jump to content
  • Advertisement
Sign in to follow this  
TheStudent111

Why does the order of vertices and indices matter?

This topic is 974 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

  Why does the order, in which vertices are arranged matter. The following compiles fine

    const GLfloat vertices[] =

    {

        0.5f,  0.5f, 0.0f,   // Top Right    

        0.5f, -0.5f, 0.0f,   // Bottom Right

       -0.5f, -0.5f, 0.0f,   // Bottom Left

       -0.5f,  0.5f, 0.0f    // Top Left

    

    };
 
...
 
    const GLint indices[] =

    {

        0, 1, 3,  // 1st Triangle

        1, 2, 3   // 2nd Triangle

    

    };

while something like this does not,


    const GLfloat vertices[] =
    {        
        0.5f, -0.5f, 0.0f,   // Bottom Right
        -0.5f,  0.5f, 0.0f,   // Top Left
        0.5f,  0.5f, 0.0f,   // Top Right    
       -0.5f, -0.5f, 0.0f   // Bottom Left

    
    };
 
...
 
    const GLint indices[] =
    {
        0, 1, 2,  // 1st Triangle
        3, 1, 2   // 2nd Triangle
    
    };
 
 

  What am I missing in terms of a pattern, that I can get out of this.

Share this post


Link to post
Share on other sites
Advertisement


The following compiles fine

<snip>

while something like this does not,

<snip>

What is the error message you get when compiling?

Edited by Lactose!

Share this post


Link to post
Share on other sites

Not really an error message, its more of a deformed shape. The normal output should be a square that is composed of two triangles.  Used Element Buffer Objects to reduce the number of vertices that are stored in the array. Its a more efficient way of drawing something like a square, that uses the same vertices multiple times.

Edited by TheStudent111

Share this post


Link to post
Share on other sites


Not really an error message, its more of a deformed shape.

Then it is not a compilation error. Terminology matters :)

 

That said, it looks like your indices are incorrect.

You seem to have:

Bottom Right - Top Left - Top Right

Bottom Left - Top Left - Top Right

 

You probably want to swap Top Right with Bottom Right (so 3, 1, 0 for the second triangle).

Share this post


Link to post
Share on other sites

Sorry, for the confusion.

 

   I am aware the second code snippet is incorrect, but I just want to understand why it is incorrect. How does OpenGL read the vertices and indices? Why does it do it this way?

Share this post


Link to post
Share on other sites

Each set of three indices correspond to a triangle.

 

Look more closely at your second case. Your two triangles are overlapping, instead of being adjacent.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!