Jump to content
  • Advertisement
Sign in to follow this  

Clipping lines and triangles in a software renderer

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

I'm trying to get clipping and culling working for my software renderer, but am having a bit of trouble understanding how to do the actual clipping.

I've been following this paper on clipping but can't quite wrap my head around what space the clipping needs to be performed in. I was expecting to clip in normalised device coords, with their nice easy -1 to 1 range, but apparently that throws up some unpleasant edge cases, so it's better to clip in homogeneous space before the w divide to NDC space. Am I right in thinking this?

I'm starting with lines, so I figure I've got to do something like:

1. Transform vertices from world to homogeneous space by transforming by model, view and projection matrices.
2. Classify each vertex
3. Reject the whole line, accept the whole line or clip the line
4. Rasterise

1 and 4 I've got, but how do I start on 2 and 3? What planes am I actually classifying/clipping against?

Pointer's appreciated, I'm a unsure where I'm supposed to be going next...

Share this post

Link to post
Share on other sites
So I also found this reference, which seems to be the same technique. My understanding is that the equation for the right plane in clip space is (w + x = 0). So using the rearranged equation I can find 'a' which gives me the intersection distance of the plane against my line segment to be clipped.

However when I do this, my 'a' ends up being a negative number. Surely this isn't correct as it means the intersection point is not actually on the line.

Test code currently looks like this:
[font=Monaco][size=2] if ((startClassification & RIGHT_CODE) != 0[/font] || (endClassification & RIGHT_CODE) != 0)


// For right plane:

final float top = clipStart.w + clipStart.x;

final float bottom = (clipStart.w + clipStart.x) - (clipEnd.w + clipEnd.x);

final float a = top / bottom;

final float invA = 1f - a;

// Clip start or end?

final float dx = clipEnd.x - clipStart.x;

final float dy = clipEnd.y - clipStart.y;

final float dz = clipEnd.z - clipStart.z;

final float dw = clipEnd.w - clipStart.w;

final float len = (float)Math.sqrt(dx*dx + dy*dy + dz*dz + dw*dw);

clipEnd.x = clipStart.x * invA + clipEnd.x * a;

clipEnd.y = clipStart.y * invA + clipEnd.y * a;

clipEnd.z = clipStart.z * invA + clipEnd.z * a;

clipEnd.w = clipStart.w * invA + clipEnd.w * a;

Is my understanding of this 'a' value correct? And is a negative number valid or does it mean I've done something else wrong?

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!