PDA

View Full Version : 3d backface culling

Theo
12-18-2000, 07:42 PM
I am stuck with this problem at the moment.

I'm trying to order my vertixes so that OpenGL can do backface culling
for me, I can't specify my points in order manually because my code
just doesn't work like that. The points need to be reordered
programatically automatically.

For example, my GLCuboidObj can accept just two points, a width float
and height float. It builds all the GLQuad3dObj 's with their vertexes
all in the right place. But what it can't know YET, is how to order the
vertexes for backface culling. This is what I'm completely stuck on.

What I want is to reorder my polygons to be drawn in the clockwise
order (or reverse, I forget which but its not important right now as
its simple to change) when they have their front face viewed. I don't
actually have them drawn in that order at the moment, and I need to
adapt the code so that I can take 2 triangles, and let the code reorder
the points of both of them so that they are both drawn as opposite
sides of an object. I thought I could do it with dot product, but
unfortunately dot product never returns an angle bigger than 180, so I
can't tell which direction two vectors starting from the origin are
winded at. Unless perhaps you know of a way to find the winding angle
even if its bigger than 180? Screen coordinates don't come into this of
course. Manual culling I'll leave to OpenGL.

dmy
12-19-2000, 01:12 AM
i didn't understood completely.

is early culling what you need to do?
or you simply want a way to wind up your triangles in a way that will allow gl to cull your faces?

DMY

El Jefe
12-19-2000, 07:43 AM
I'm assuming GL takes your vertices, calculates the normal for the face, then does a dot product to figure out what way it's facing..

If this is true, then supplying the normal on your own would solve the problem. But then again, to generate a proper normal, you'll need to have the winding order set properly....

DFrey
12-19-2000, 08:19 AM
This is easy to do for simple, convex solids. First you need to find a point that is inside the primitive, this will be the reference point, r. Then take any 3 adjacent vertexes, e.g. a, b, and c. Create 2 ordered vectors AB=b-a, BC=c-b. Take the cross product of AB and BC, call it N. Next take the dot product of (r-a) and N, call it d. If d<0 then the polygon's normal vector N points outside and the order of the vertices is ccw. If d>0 then N points inside and the order of the vertices is cw. So you can check the value of d and reverse the vertex order if d>0. This would ensure that the ordering was always ccw. This assumes the polygon's vertices are in some order to begin with. If your vertices are not in any order, then you have a different task to undertake. For each unordered set of polygon vertices, I would use the wrapping paper or sorted angles algorithim to find the ordered polygon.