glRect() gets culled when GL_CULL_FACE is enabled

Started by Rene, September 04, 2009, 10:21:38

Previous topic - Next topic

Rene

Hi all,

I noticed that some of the 2D drawing functions of my renderer didn't work anymore, the ones using glRect() to be precise. After some searching I found glEnable(GL_CULL_FACE) to be the culprit. When I call glCullFace(GL_FRONT), the rectangle is drawn again.

I can't imagine this is how it's supposed to be, and I can't find anything on the net or in the redbook stating this behavior. So I first thought it was another driver bug. Strange thing is that both my desktop(ATI) and laptop(Intel) show this behavior.

Does anyone know if this is correct, and if not, should I report a driver bug to ATI/Intel?

Rene
When I am king, they shall not have bread and shelter only, but also teachings out of books, for a full belly is little worth where the mind is starved - Mark Twain

Kai

Hi,

glRect(x1,y1,x2,y2) is exactly equivalent to:
begin
vertex(x1,y1)
vertex(x2,y1)
vertex(x2,y2)
vertex(x1,y2)
end

as stated in http://www.opengl.org/documentation/specs/man_pages/hardcopy/GL/html/gl/rect.html.

So it does culling. Check what your front face is (specify via glFrontFace).
If the rectangle was culled before you set cull face to GL_FRONT (which means, all faces with their front oriented to the viewer gets culled), then your front face was clockwise, since glRect() specifies a rectangle in counter-clockwise order.

Rene

Quote from: Kai on September 04, 2009, 12:48:02
If the rectangle was culled before you set cull face to GL_FRONT (which means, all faces with their front oriented to the viewer gets culled), then your front face was clockwise, since glRect() specifies a rectangle in counter-clockwise order.

That's the strange thing. Even if I set glFrontFace explicitly to GL_CCW, the rectangle gets culled. When I then set glCullFace to GL_FRONT, it is visible, and when it is set to GL_BACK, it is invisible. The opposite of how it should be.

Maybe something is wrong with my matrices? Before drawing 2D shapes, I reset the modelview matrix to the identity. As projection matrix I use
2, 0, 0, -1,
0, -2, 0, 1,
0, 0, 1, 0,
0, 0, 0, 1
so the top-left corner is 0, 0 and the bottom right corner is 1, 1. (I load it using glLoadTransposeMatrix()).

That should be right, shouldn't it?
When I am king, they shall not have bread and shelter only, but also teachings out of books, for a full belly is little worth where the mind is starved - Mark Twain

broumbroum

hi,
using GL_FRONT gets drawn then if GL_BACK gets culled, that means your transposedmatrix reverses z-axis somehow... Well I don't really understand what you're doing... see the documentation http://www.opengl.org/documentation/specs/man_pages/hardcopy/GL/html/gl/cullface.html


Kai

Quote...that means your transposedmatrix reverses z-axis somehow
Well, actually that happens with any reflection (be it x, y, z or any other plane)

Quote2, 0, 0, -1,
0, -2, 0, 1,
0, 0, 1, 0,
0, 0, 0, 1
I see that you reflect all values along the XZ-plane (-2 for y, second row, second column). I guess that leads to the strange behaviour.

Rene

I always learned to write matrices in the way I wrote it down in my previous post. If I do it the way OpenGL wants I would have to write it as

2, 0, 0, 0,
0, -2, 0, 0,
0, 0, 1, 0,
-1, 1, 0, 1

but I always get confused when I write them down that way. So, I just define the matrix in the way I'm used to, and load it transposed so OpenGL receives it the way it wants. As you can see, the z-coordinates aren't altered. I've also tried to define the matrix in the 'GL way' and load using the normal method, but it gives the exact same result.

Basically, what I'm doing is:

glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadMatrix(PROJECTION_2D); (where PROJECTION_2D is a FloatBuffer filled with the matrix)
glDrawRect(.25f, .25f, .75f, .75f);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();

This is done by the renderer, but I've checked the stack traces and those really are the only GL commands when I draw a rectangle. Like I said before, even when I set glFrontFace() to GL_CCW and glCullFace() to GL_BACK, it doesn't work. When I set glCullFace to GL_FRONT or disable GL_CULL_FACE, it works.

Thanks for your help so far, but any more ideas what could be wrong?
When I am king, they shall not have bread and shelter only, but also teachings out of books, for a full belly is little worth where the mind is starved - Mark Twain

Kai

QuoteI always learned to write matrices in the way I wrote it down in my previous post....
I'm afraid, that wasn't the point.

You flipped the Y-scale co-ordinate! No matter how you write it down, the y-co-ordinate (second row, second column) (2. row is always 2. and 2. column is always 2., no matter how you transpose the matrix) is negative, which means your coordinates will be flipped around the XZ-plane.
That is what is causing the trouble.

Rene

While I was writing my last post, you posted yours, so that's why I didn't take it in account ;)

Changed the matrix so (0,0) is in the lower left corner and (1,1) is in the upper right corner. I was thinking about doing that anyway, as it makes more sense IMO, but at first I was planning to use the Java2D coordinate style. Anyway, everything works fine now. Thanks for your help!
When I am king, they shall not have bread and shelter only, but also teachings out of books, for a full belly is little worth where the mind is starved - Mark Twain