Clipping, 2D or 3D ?

Started by
12 comments, last by Peter19852001 20 years, 8 months ago
Do you suggest 2D or 3D clipping for a 3d engine? 3D clipping seems a lot of calculation, would 2D clipping be simpler?
Q:"Why it doesn''t work?"A:"There is a mistake."Q:"Who made that silly mistake?"A:"The one who write it."
Advertisement
I assume that you are writing a software renderer, right?
3d clipping isn''t that expensive really, if you implement it correctly anyway. if you go for 2d clipping, you will still have to clip in 3d for the near clipping plane, so why not do all clipping in 3d then and there...

Oh, and 3d clipping is earlier in the pipeline, so triangles that are outside the view frustum are culled away earlier.
You can use both. Clip against the near and far clip planes in 3D then perform the remaining clipping in image space.
You absolutely need 3D clipping. Once implemented correctly it will save you from a lot of headaches with 2D clipping. Even Blivvy''s suggestion doesn''t work, because precision limitations cause serious artifacts. Besides, correctly doing 2D clipping is not simpler nor faster because of prestepping and sub-pixel accuracy.

The only way to do ''2D clipping'' is with guard band clipping. But trust me, you don''t want that, and you need your 3D clipper working correctly anyway.

Have a look at swShader/Renderer/Clipper for a very efficient implementation. Note that it''s a homogeneous clipper so first implement 4D projection matrices.
Infact Blivvy is right,both methods can be used for successful clipping.But u guys are right for the speed.No speed difference between the methods.

"Tonight we strike,there is thunder in the sky,together we''ll fight,some of us will die,but they''ll always remember that we''ve made a stand and many will die by hand!" - ManOwaR
Stop sprouting garbage, homogenous clipping is the way to go, just like Nick said. It''s way more elegant, it avoids precision issues and it''s fast, all at the same time. How could you ask for more? Read this and you''ll learn all the gory technical details.
Ok ok just don''t beat me...

but i have the following question.Let''s suppose we don''t use 4D projection but simple equations like the following:

dist = scr_width / 2 * (cos(fov/2) * sin(fov/2))

scr_x = ((p_x * dist) / p_z) + scr_width / 2;
scr_y = ((p_y * dist) / p_z) + scr_width / 2;

this is perfectly valid way to map 3D points on viewing plane as Yann pointed some time ago.Using these u will successed to clip agains the near/far but what about the other sides?It''s impossible to define a unit-cube using this equations in which you can clip your polygons.The only other option is the viewport clipping which also gives good results.But of course if u use advanced projection techinuqes it''ll be better to clip in homogeneous space...

"Tonight we strike,there is thunder in the sky,together we''ll fight,some of us will die,but they''ll always remember that we''ve made a stand and many will die by hand!" - ManOwaR
quote:Original post by Mihail121
It's impossible to define a unit-cube using this equations in which you can clip your polygons.The only other option is the viewport clipping which also gives good results.But of course if u use advanced projection techinuqes it'll be better to clip in homogeneous space...

It's impossible to define a unit cube, but it's not impossible to define a 'unit frustum'. By scaling x and y you can make the frustum planes 90 degrees, which simplify the distance calculations a little. It's called affine clipping if I recall correctly and it's the first step towards homogenous clipping. If you write down the formulas of clipping and projection you automatically go to homogeneous clipping:

-1 < X < 1
-1 < Y < 1

Where X and Y are affine screen coordinates scaled into a [-1, 1]x[-1, 1] viewport.

-1 < x/z < 1
-1 < y/z < 1

Where x and y are unprojected, scaled camera view coordinates. Z is the distance from the point of view. Because we often also want to scale z in [0, 1] for the depth-buffer, let's call that coordinate w:

-1 < x/w < 1
-1 < y/w < 1
0 < z/w < 1

Now the step to homogeneous clipping is trivial:

-w < x < w
-w < y < w
0 < z < w

Exegant, efficient, robust. And the real pretty part is that all projection information can be stored in a 4x4 matrix. No more explicit working with projection plane distance, etc. You can do any transformation on your geometry you want, it will always be clipped correctly. 2D clipping might look simpler at first but it's a pain...

[edited by - c0d1f1ed on August 11, 2003 7:40:50 AM]
Damn i didn''t knew that.10x!

"Tonight we strike,there is thunder in the sky,together we''ll fight,some of us will die,but they''ll always remember that we''ve made a stand and many will die by hand!" - ManOwaR
Thanks for all your replies.
Today I finished making a simple clipper.
It first does 3d near z plane clipping, then project the polygon, and do 2d clipping.

While doing 2d clipping for a polygon, I do it 4 times, each time clipping against one edge only (left, top, right, bottom). I know that this is inefficient, but I want to get it done first.

I would like to ask in 3d clipping, is a polygon clipped 5(or 6) times, each time against one clipping plane(near,left,top,right,bottom,far)? Or is each line clipped against all the planes at a time?

I hope you understand what I am asking.
Q:"Why it doesn''t work?"A:"There is a mistake."Q:"Who made that silly mistake?"A:"The one who write it."

This topic is closed to new replies.

Advertisement