Archived

This topic is now archived and is closed to further replies.

CW & CCW polygon culling

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

how do you cull a polygon that is behind the camera? whenever i render a polygon (with my own software renderer of course) that is behind the camera (which is at the origin), it just reappears in kind of a mirrored effect. how is this fixed? i think culling the polygon if it is not in ClockWise vertex order then not render it, but i dont know how to implement that. all help is greatly appreciated.

Share this post


Link to post
Share on other sites
You can use frustrum culling to get rid of polygons that aren''t going to be on screen anyway. This involves finding the clipping planes, and calculating whether any point of the polygon is in front of all of the planes. If no points are, then the polygon will not show up on screen, and you simply don''t draw it. www.gametutorials.com has a good tutorial on this under their OpenGL section.

Share this post


Link to post
Share on other sites
In a renderer there are two ways to cull polygons not facing the camera.

1. The dot product. After you transform the polygons into the camera space(or the camera into the polygon space), get the dot product of the triangle normal and the ray from the camera to the triangle. Then, if the dot product is positive/negative your triangles will be culled (depending on which triangles you need to cull).
2. The clockwise/counter-clockwise rule. After you transform the triangles to screenspace coordinates check if the order of the vertices is clockwise/counter-clockwise. Another words you check if the vertices are flipped.

In a software renderer I would use a second choice. The problem with the first one is that you have extra transformations. That means that every time your object matriceses change, you need to transform the camera into the object space and do the dot product. With the second option, you just cancatenate all the matreces together, transform all vertices once and then implement the culling algorithm.

Frustum culling is generally a high level algorithm. It doesn''t belong to the software renderer, more to the application that uses it or a higher level library written on top of it. Also, frustum culling has nothing to do with removing polygons that aren''t facing the camera. It removes polygons that aren''t seen by the viewer because they aren''t in the camera''s pyramid.

Share this post


Link to post
Share on other sites
I think he just wanted to know how to remove faces behind the camera plane ?

OK, because of the perspective projection equation, face (or better: vertices) behind the origin plane will be mirrored. Vertices exactly on that plane will even be undefined (degenerated), and will create weird (and highly undesirable) effects.

That''s why you need to make sure, that no vertex will ever be behind that point. You define a camera clip plane, slightly infront of the origin. Perhaps at 0.01. Now you clip every face against that plane, before rendering it. You keep the part of the face that is infront of the plane, and remove the part that is behind. A good clipping algorithm for that task would be the Sutherland-Hodgman algorithm, it''s fast and easy to implement.

/ Yann

Share this post


Link to post
Share on other sites
i guess i didn''t even know i was asking two seperate questions, but they both definetly got answered. Thx to both, explianing back face surface removal and near camera clip planes. one thing though, do you have any links to a site better expliaining the Sutherland-Hodgman algorithm?

Share this post


Link to post
Share on other sites
Look here !
They do it for the 4 screen sides, you have to do for a single plane only. The principle is the same though (it's even easier in your case).

[edited by - Yann L on June 1, 2002 12:35:04 AM]

Share this post


Link to post
Share on other sites
By the way, how is back face culling done in different APIs? As far as I know, OpenGL requires a normal to cull back faces, so it should use the dot product method, and DirectX (8) checks for clockwise/counterclockwise triangles. I was under impression that it''s done in hardware. Does that mean that cards support both methods?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
OpenGL cull faces based on whether the vertices are in CW or CCW order. glCullFace and glFrontFace set which faces should be culled.

Share this post


Link to post
Share on other sites
Actually the 3D hardware computes the orientation of a face by calculating it''s area. A positive area means frontfacing, a negative one backfacing. That''s automatically done in hardware, so it''s the same for OpenGL and D3D. None requires an explicit normal to do backface culling.

/ Yann

Share this post


Link to post
Share on other sites
I ran into this problem when I was writing a software renderer. Specifically, the problem is this situation: You''re standing on a floor made up of a large polygon. Some of the polygon is behind you, and other parts are in front of you. You can''t cull the polygon obviously, but if you just go ahead and draw it, the parts behind you will get drawn on-screen but flipped/mirrored.

So you have to clip the polygon, converting it into one or two polygons which end at the near camera plane.

~CGameProgrammer( );

Share this post


Link to post
Share on other sites