Jump to content
  • Advertisement
Sign in to follow this  
Akatsua

OpenGL Help: GL_QUADS Explanation

This topic is 1485 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

Hey everyone.

 

So, i've run into a problem.

Well, its not a problem, because i know how to fix it, but, i want to know why it is happening.

 

I was playing with OpenGL, making squares, and this happened:

 

7e8340c32e.png

 

Keep in mind, im using gluOrtho2D(-1, 1, -1, 1) , and this shape is drawn on the 3rd quadrant of a 2D plane.

 

(Interestingly, if i change GL_QUADS to GL_POLYGON, the shape gets turned 180º)

 

I tried to figure out what was happening, and changed GL_QUADS to GL_LINE_LOOP

 

67cfbe09c4.png

 

So far so good, i understand that i have to change the order of the vertexes, and doing that, makes the square perfect.

 

2735efc319.png

 

3bd1bf9748.png

 

Now, my questions are:

 

In the first image, instead of a weird 5 sided shape, shouldnt it be something like an hourglass, like the second image?

 

Assuming that GL_QUADS work something like GL_TRIANGLE_FAN that paints the first triangle (first three coordinates) and then paints a second triangle, using the the first coordinate, the last one, and the "new" one, shouldnt be the shape in the 1st picture be rotated 180º ?

 

How does GL_QUADS really work?

 

Thank you.

Edited by Akatsua

Share this post


Link to post
Share on other sites
Advertisement

"In the first image, instead of a weird 5 sided shape, shouldnt it be something like an hourglass, like the second image?"

To get something like that you would need 5 vertex, since those triangles would only share the middle one. The image you get makes more sense, you build that image with 2 triangles (vertex 1, 2, 4 and vertex 2, 3, 4). Maybe the assumption you made is just not true (I couldn't find anywhere something relatin GL_QUADS and GL_TRIANGLE_FAN).

 

I don't know why a quad is split into those 2 triangles and not others, but you'll always get something weird like that if you setup the vertex in the wrong order using GL_QUADS. My recommendation is to use only triangles, since you can't be sure that a quad has the four vertex on a plane and may produce more strange results when doing 3D stuff.

Share this post


Link to post
Share on other sites

Yea the second stuff you have proper vertices the other is not. See the image in the post above this, your vertices are not in a winding order they are corner to corner.

Share this post


Link to post
Share on other sites

Hello,
do you have backface culling activated?
Would look like:

glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);

GL_BACK is the initial value, therefore it dont have to appear in your code, but it would be good to know if you use "glEnable(GL_CULLFACE)".
I am asking this because even the second order of vertices should not give you the right result if backface culling is enabled and

glFrontFace(GL_CW);

is not used. glFrontFace sets the winding order of vertices in OpenGL and its default is GL_CCW. Therefore by default the order of vertices should be counter clockwise.
In your second example they are clockwise, which would mean that the quad is oriented away from the screen(viewer) and if backface culling is enabled you should not see anything. (backface culling means that only the front face is rendered and visible)
I think the right order should be as an example:

glBegin(GL_QUADS)
  glVertex2f(-0.5,-0.5);//0
  glVertex2f(-0.2,-0.5);//1
  glVertex2f(-0.2,-0.2);//2
  glVertex2f(-0.5,-0.2);//3
glEnd();

every other counter clockwise order is fine too.
Even if the quad is divided (which is the only way in modern opengl, where GL_QUADS is deprecated and deleted) in two triangles botth of them (0,1,2) and (2,3,0) have the right winding.
 

I have no clue why the first result is looking like this, but I dont found a specification how exactly GL_QUADS works.

What I would do is enable backface culling and then try it, because then you are learning it the right way and you dont have to struggle again when you need to use backface culling. And like DiegoSLTS suggests I would use triangles, because this is the way to go in OpenGL 3.0 and higher.

I know that were perhaps alot of information and I did not explain everything in depth, please feel free to ask.
 

Share this post


Link to post
Share on other sites

Maybe the assumption you made is just not true (I couldn't find anywhere something relatin GL_QUADS and GL_TRIANGLE_FAN).

 

The assumption i made was just an idea to try to explain what was happening. I did not believe that GL_QUADS and TRIANGLE_FAN had a relation. Like i said, just an idea ^^

 

 

 


What I would do is enable backface culling and then try it, because then you are learning it the right way and you dont have to struggle again when you need to use backface culling. And like DiegoSLTS suggests I would use triangles, because this is the way to go in OpenGL 3.0 and higher.

 

Yeah, i know i should only use triangles. In fact, im learning OpenGL 4.3, but, in college, i have to use 2.0, unfortunatly.

 

Maybe it's breaking it like this:

Quad_break.png

 

I don't know if there's any requirement for how a videocard breaks quads apart, or whether it even has to break quads up.

 

Yea the second stuff you have proper vertices the other is not. See the image in the post above this, your vertices are not in a winding order they are corner to corner.

 

Yes, i though that was probably happening. But i find it interesting that to create a quad, the vertexes need to be strictly ordered for it to work as intended!

 

 

Hello,
do you have backface culling activated?
Would look like:

glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);

GL_BACK is the initial value, therefore it dont have to appear in your code, but it would be good to know if you use "glEnable(GL_CULLFACE)".
I am asking this because even the second order of vertices should not give you the right result if backface culling is enabled and

glFrontFace(GL_CW);

is not used. glFrontFace sets the winding order of vertices in OpenGL and its default is GL_CCW. Therefore by default the order of vertices should be counter clockwise.
In your second example they are clockwise, which would mean that the quad is oriented away from the screen(viewer) and if backface culling is enabled you should not see anything. (backface culling means that only the front face is rendered and visible)
I think the right order should be as an example:

glBegin(GL_QUADS)
  glVertex2f(-0.5,-0.5);//0
  glVertex2f(-0.2,-0.5);//1
  glVertex2f(-0.2,-0.2);//2
  glVertex2f(-0.5,-0.2);//3
glEnd();

every other counter clockwise order is fine too.
Even if the quad is divided (which is the only way in modern opengl, where GL_QUADS is deprecated and deleted) in two triangles botth of them (0,1,2) and (2,3,0) have the right winding.
 

I have no clue why the first result is looking like this, but I dont found a specification how exactly GL_QUADS works.

(...)
I know that were perhaps alot of information and I did not explain everything in depth, please feel free to ask.
 

 

I do not have culling enabled. Interestingly, if i enable it, it transforms into a triangle only. xp

 

Your post was very interesting, no need for further explanations. Im satisfied!

 

 

Thank you everyone for sharing your knowledge

Good night

Edited by Akatsua

Share this post


Link to post
Share on other sites

Your post was very interesting, no need for further explanations. Im satisfied!

That's the expected behaviour. Imagine the resulting picture as a piece of paper folded this way:

   (-2,-2)
 /|
/ |
--- folded through this line
| /
|/
(-5,-8)

With culling off you get the result for your first picture, both sides of each triangle can be seen any time, ignoring the order of the vertices.

 

With culling on you get the first triangle visible, but for the second you're looking at the back face, so it's not drawn.

Edited by DiegoSLTS

Share this post


Link to post
Share on other sites

 

But i find it interesting that to create a quad, the vertexes need to be strictly ordered for it to work as intended!

 

The answer to that is that the GPU is not going to try and find the mix/max vertex of your quad so that it knows which one is the lower left vertex or the upper right vertex.........GPU doesn't have time to do this.

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!