Opengl culling confusion

Started by
9 comments, last by JohnnyCode 8 years, 2 months ago

I am wondering how opengl determines if face is clockwise or counterclockwise?

becasue if i manage to draw something that i think (from prespecitve that A(0.0.0) B(10.0.0) C(10.-10.0) D(0.-10.0) is clockwise when i move camera (negate cam position and rotate it to see the same quad it will appear to me that its counterclockwise, so what the heck?

how i am supposed to use culling then?

Advertisement

If you have decided that a front face has its vertices in counter clockwise order, then you will see a counter clockwise order when you look at the front face of the polygon and a clockwise order if you look at the back face. OpenGL performs backface culling by looking at the screen space winding order. If, after projection onto the screen, the face has a counter clockwise order, it has to be the front side. Similarly, if the order is clockwise, it has to be the back face and it will be culled. There is no way to view a triangle from the other side and not change the apparent winding order.

ok thanks i understand that now. but however i am confused aboud shadow volume creation, i have a code (that doesnt work for a big scenarios) and i think it has something to do with the culling problem because i may wrongly set the clockwise order, the problem now is how do i force clockwise as front for a side caps of shadow volume

if i consider clockwise ABCD quad then it should be something like:

(let A be top left vert of quad and D is bottom left)

A B

D C

A, EndA, EndB, B for top

EndD, EndC, C, D for bottom

but even if i think they are all in the same order, how to validate that?

do i only imagine that i see them from front and set vertices into proper order or do i use some additional voodoo?

What is "End[A-D]", and what is "top" and "bottom", in your example?

If you are confused you should enable only back faces and render the mesh with one color (e.g. red) then enable only the front faces and render a different color. It will help you work out what is and what isn't a backface when creating your shadow volumes and help you track down your issues. I personally had a great deal of trouble making sure my triangles were facing the correct direction for my shadow volumes.

In terms of working out winding order for the projected quad/triangles consider this image (attachment). You will notice that for every edge the windiing order for each face goes in opposite directions. For one face CA is an edge but for the adjoined face AC is the edge. You can use that when creating your shadow volume sides. If the original face goes ABC then the winding order of the projected edge/quad AB needs to be in the opposite direction, i.e. BnearAnearAfarBfar.

Interested in Fractals? Check out my App, Fractal Scout, free on the Google Play store.


i have a code (that doesnt work for a big scenarios) and i think it has something to do with the culling problem because i may wrongly set the clockwise order, the problem now is how do i force clockwise as front for a side caps of shadow volume

You can issue before a draw call in opengl what winding is culled, and which is rasterized. You can cull CCW, or CW, or none, or, both and render nothing.

Generaly a shadow volume of z-fail technique is performed on visible rendered frame in a way of getting occluder rendered twice, but altering only stencil buffer, not color and depth one. It should be done like this:

clear stencil buffer per every light with some reference value (for our example 127)

(for first extruded geometry pass )

-set culling to cull CW (render front faces of yours only)

- set depth test to pass when pixel depth is greater (gets behind depth buffer value)

- decrease stencil value by one

(for second extruded geometry pass )

-set culling to cull CCW instead (render back faces of yours only)

- set depth test to pass when pixel depth is greater (gets behind depth buffer value)

- increase stencil value by one

now everywhere where stencil buffer value is larger than 127, is a shadow for the light. You can continue render other occluders of the light to the stencil without worrying for this to be not true (>127 shadow, <=127 lit).

Winding of faces is something that stays consistent across geometry and that is all that really matters. But for correct artefact-less stencil shadows, you will need yet all your casting geometry to be an enclosed mesh, what is not a big problem, but what is worse, you will need to index additional two triangles per every edge, while having all position verticies unique (vertex count will equal 3*trianglecount), else you will not extrude geometry volume properly.

What is "End[A-D]", and what is "top" and "bottom", in your example?

End means end cap (the shadowvolume end = face that casts shadow moved in light direction to infinity) End(ABCD) isthe corresponding vertex

top is the top caplike i said

A B

D C

EndA EndB

EndD EndC

so A is connected to EndA B is to EndB, c to EndC etc.

giving

aboxe.png

What is "End[A-D]", and what is "top" and "bottom", in your example?

End means end cap (the shadowvolume end = face that casts shadow moved in light direction to infinity) End(ABCD) isthe corresponding vertex

top is the top caplike i said

A B

D C

EndA EndB

EndD EndC

so A is connected to EndA B is to EndB, c to EndC etc.

giving

aboxe.png

In that image if the front is defined as ABCD then for the top (AB edge) you need to reverse it (see my image) to BA then EndA, endB. For the right side it would be CB endB, endC.

Interested in Fractals? Check out my App, Fractal Scout, free on the Google Play store.

Assuming that "top" point upwards, the quad (A, EndA, EndB, B) is clockwise, since if you look at it from above face the vertices are in clockwise order. If you rotate the cube slightly around the X-axis so that the "bottom" face is visible, you'll see that (EndD, EndC, C, D) is counter clockwise if you look at it from below. Thus, the "bottom" and the "top" face of that cube has inconsistent winding order.

Two faces with the same winding order that shares and edge between two vertices must share that edge in opposite order. For example if "front" has the edge from A to B (in that order) then "top" must connect from B to A. And, indeed, that is the case; the last vertex B in your list connects back to the first vertex A. However, this is not the case for the "bottom" face and the edge between C and D; both the "front" and the "bottom" face shares the edge from C to D in the same direction and therefore they have different winding orders.

lets consider i have a 256x256 heightmap i make a 3d mesh out of it and cast shadow using shadow volumes,

now i would like to only draw 9 sections of the map considering i am at x,y point(corresponding to heightmap 2d field im in x,y cell)

i would like to know if theres any good approach(fast one for dynamic lighting) to determine which shadwowvolumes cast shadow on those sections

by 9 sections i mean x,y cell and every cell around it

This topic is closed to new replies.

Advertisement