.. clipping!!!

Published September 16, 2008
Advertisement
I haven't had a whole lot of time to work on this lately, but the project is still alive and well!

I finally got around to implement frustum clipping. Specifically, I added clipping in NDC space for its efficiency and code simplicity.

There were several issues I had to overcome for this...

1) I had to totally re-organize the way vertex data flowed through my rendering pipeline. Previously, I sent all the vertices into the vertex stage at once, processing them all at once before proceeding to the next stage. This would be a problem, as the extra triangles/vertices that could be potentially be generated by the clipping would require extra memory to store and this space would either have to be pre-allocated buffer thats as big as the maximum number of possible clipped vertices, or a growing buffer (both bad news). Thus, I modified my pipeline so that only a single triangle is sent into the pipeline at a time, and then a small pre-allocated buffer (2^(# clip planes) triangles) would be used to store any extra vertices. The data related to this triangle (and the extra triangles resulting from its clipping) are then propagated through the rendering pipeline until fragments are drawn to the framebuffer.

2) For the actual clipping process, I just send each triangle through a clipping "pipeline", where the output of clipping to one plane, goes into the clipping of the next plane. I should also mention that I used the Sutherland Hodgeman algorithm for this (you basically traverse each edge of the primitive, perform an intersection test, and possibly output new vertices depending on the outcome). I clipped against all 6 frustum planes.

3) Adding the clipping process also revealed several earlier bugs in my code. For example, when mapping to screenspace, I was mapping NDC x,y coordinates from 0 to width/height instead of 0 to width/height minus 1, resulting in out of bounds screen coordinates.

Below is a picture of a cube being clipped to the negative X frustum plane...



.. and another picture of the same cube (camera slightly above cube this time) being clipped to the far plane..



I decided at this point to hold off on any sort of culling until a later point.

.. at last.. I can start tackling scan conversion!!!
0 likes 2 comments

Comments

Tiffany_Smith
This makes me think of something I often wonder, how much original code is typically written vs code that is rewritten. I know if I'm writing it, a vast majority of it is rewritten because I am constantly finding/needing to find a more optimal way to do things. It really is a "two steps forward, one step back" process.

Anyway, I'm glad you're able to move on to the next thing. Keep up the good work.
September 27, 2008 01:24 PM
Clapfoot
I find that for myself, it depends highly on the project. If the project is for educational purposes (such as this Software renderer i'm doing now), then I prefer to re-write things from scratch just so that I can verify my understanding of subject. Otherwise, I find there is not much point in re-inventing the wheel if good working code already exists out there, unless theres potential for optimization like you said.
October 14, 2008 12:07 AM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Profile
Author
Advertisement
Advertisement