Archived

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

paulcoz

All Aussies help me with 3D Clipping!!

Recommended Posts

paulcoz    230
I really want to learn how to do full 3d-clipping, and I've tried numerous times to do this by looking at code samples / reading poorly written tutorials without success. I'm getting really desperate so I appeal to the Aussies who visit this site to help me out. If you aren't from Oz don't despair - you can still help me if you like. I have a basic 3d-engine working, but I really want to do the clipping properly, because at the moment I am only doing MinZ / MaxZ tests, then using 2d-Sutherland-Hodgeman clipping to dispatch with the remaining unseen objects. I know that I need to clip more efficiently if I want the engine to run really well later on. I am also familiar with things like vectors, normals, dot products, cross products etc.. so don't be afraid to bombard me with some maths. I need someone to explain: (a) how I represent the remaining frustrum planes that look like warped rectangles (MinX, MaxX, MinY, MaxY) with the Ax + By + Cz + D = 0 (correct?) equation. (b) How to work out whether a point is on one side or the other of this frustrum plane, and also given the x, y, and z coordinates of two points on opposite sides, calculate these values for the intersection point on the plane. It would be great if you could explain what mathematical steps are taken, and why each of them are taken (eg. if you say that I have to calculate a normal, please tell me why it is needed, and what it is used for, OR if you substitute something into that scary plane equation, help me understand why you need to do that). A step-by-step demonstration of an edge being clipped to one of the planes would be really helpful. Please assist, Paulcoz. Edited by - paulcoz on 10/4/00 1:47:00 AM

Share this post


Link to post
Share on other sites
Hotstone    133
a)
Well, the first thing you need to understand here is what A,B,C and D in your plane equation are. (A,B,C) is the normal vector of the plane, and D is the distance between the plane and the origin.

So, in answer to your question, all you have to do is calculate the normal vectors (because you render with your camera at the origin, and all of these four planes pass through the camera, so D will be zero). You can work the vectors out yourself by drawing a top view (for the side planes) and a side view (for the top and bottom planes), and then doing some basic trig. You should get something like:

For side planes:
A = (+/-) cos( 90 - HALF_HFOV )
B = 0 (of course)
C = sin( 90 - HALF_HFOV )

For top/bottom planes:
A = 0 (of course)
B = (+/-) cos( 90 - HALF_VFOV )
C = sin( 90 - HALF_VFOV )


(b) This one''s easy actually. Look at your equation of a plane: you have = 0. That is, you''re saying "any (x,y,z) triplet that, when substituted in here, returns zero is part of the plane". What is actually returned is the distance from the plane (basically). Positive if the point is on one side, negative if it is on the other and, of course, zero if it is actually on the plane. So basically sub in your values and check the sign of the result.

As for the intersection thing, I think I used to do something like:

1) Calculate percentage of line on point1''s side of plane.
2) Get point2 relative to point1 (ie. point2.x -= point1.x, etc.).
3) intersectionpoint.x = point1.x + (point2.x * percentage_from_step_1), etc. for y and z.

To determine the percentage of the line in I used this (which I think I stole from somewhere or other):

dot1 = dotproduct of point1 and plane''s normal (ie. [A,B,C]).
dot2 = dotproduct of point2 and plane''s normal.
percentage = (D - dot1) / (dot2 - dot1)

Obviously, either point can be "point1" so long as you''re consistent through-out the calculations.

Hope that helps.

-Hotstone

Share this post


Link to post
Share on other sites
MatsG    122
Just to complete Hotstone with some source that i think is fairly readable:
    
int Clipp2Plane(CLIPPOLY *in,CLIPPOLY *out,PLANE *pl)
{
int edges=0;
CLIPVERT *v1;
CLIPVERT *v2;
int a,b,curin,nextin;
double curdot,nextdot,scale;
double x1,y1,z1,x2,y2,z2;

v1=in->Vert;
v2=out->Vert;


x1=v1[0].x;
y1=v1[0].y;
z1=v1[0].z;
x2=pl->x;
y2=pl->y;
z2=pl->z;

curdot=(x1*x2)+(y1*y2)+(z1*z2);
curin=(curdot>=pl->dist);

for(a=0;a<in->NrVerts;a++)
{
b=(a+1)%in->NrVerts;

if(curin)
{
*v2++=*v1;
edges++;
}
x1=in->Vert<b>.x;
y1=in->Vert[b].y;
z1=in->Vert[b].z;
nextdot=(x1*x2)+(y1*y2)+(z1*z2);
nextin=(nextdot>=pl->dist);

if(nextin!=curin)
{
scale=(pl->dist-curdot)/(nextdot-curdot);

v2->x=v1->x+((in->Vert[b].x-v1->x)*scale);
v2->y=v1->y+((in->Vert[b].y-v1->y)*scale);
v2->z=v1->z+((in->Vert[b].z-v1->z)*scale);
v2++;
edges++;
}

curdot=nextdot;
curin=nextin;
v1++;

}

out->NrVerts=edges;
if(out->NrVerts<3)return 0;
return 1;
}

The scale value can be used for texture coordinates and normals too



Edited by - MatsG on October 4, 2000 9:00:01 AM

Share this post


Link to post
Share on other sites
paulcoz    230
I finally got a chance to look at this:

Hotstone - when you substitute a point (x,y,z) into the equation and get a number either positive or negative - is the number the distance the point is from the plane along the plane''s normal, or the distance the point is from somewhere else? (maybe from the origin, or a particular axis).

I think I need this clarified before I can understand the rest of your description.

Thanks,
(sorry for taking so long to reply)
Paulcoz.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
So, what you are asking is, what does the D value in the plane equation
represent?

The plane D value is the distance, as in the shortest distance, or
perpendicular distance from the plane to the origin(0,0,0). Hence, if
we had a plane that actually passed through the origin, we would know
instantly that its D value would be zero.

Also, it''s sign is negative when the plane''s normal points away from the
origin, and positive when the plane''s normal points toward the origin.

Share this post


Link to post
Share on other sites
paulcoz    230
I think I understand.

All you are doing is rotating the points you want to test around the camera-space origin by (90 - half the field-of-view angle) so that the clip plane is aligned with the plane made by the x-y axis, then checking whether the points are greater, less, or equal to z=0 in camera-space, right?

Paulcoz.

Edited by - paulcoz on October 18, 2000 6:06:58 PM

Share this post


Link to post
Share on other sites