PDA

View Full Version : 3D projections, line clipping, and transformations

capn_midnight
04-03-2004, 07:37 PM
So, for the past few weeks, I've been working on implementing projections for arbitrary 3D view volumes
my code, and a win32 executable (http://cutter.ship.edu/~smcbeth/c++/gfxapi/gfxapi.zip)

This demonstration rotates a solid about in space. However, you will quickly notice that it does not do it correctly. Something goes terribly wrong, and the bugger starts to distort. It does eventually come back to normal, so I'm uncertain what is happening. Perhaps it's the rotation function, buried deep in the dark pits of hell.

I know it's a lot of code, but I am at my wits end. I spent 12 straight hours completely rewriting the projection code from a week before (luckily it actually does something now). It's only parallel (orthogonal) projections at this moment. I do have code for perspective, however I'm fairly certain it is more broken than my bank account. Any advice would be appreciated.

howie
04-04-2004, 12:17 PM
It works now. You were a little off on how you handled your rotation. I modified your rotation function.

void rotate( const real ax, const real ay, const real az){

matrix rot;

long double c2r = (long double)3.1415926435898F / (long double)180;

float cosX = cos(ax * c2r);
float sinX = sin(ax * c2r);

float cosY = cos(ay * c2r);
float sinY = sin(ay * c2r);

float cosZ = cos(az * c2r);
float sinZ = sin(az * c2r);

identMatrix(rot);

rot.a[0][0]=cosZ;
rot.a[0][1]=sinZ;
rot.a[1][0]=-sinZ;
rot.a[1][1]=cosZ;

multMatrix(transMatrix, rot);
identMatrix(rot);

rot.a[1][1]=cosX;
rot.a[1][2]=sinX;
rot.a[2][1]=-sinX;
rot.a[2][2]=cosX;

multMatrix(transMatrix, rot);
identMatrix(rot);

rot.a[0][0]=cosY;
rot.a[0][2]=-sinY;
rot.a[2][0]=sinY;
rot.a[2][2]=cosY;

multMatrix(transMatrix, rot);
}

You have to rotate each axis seperately and rest the matrix after each axis but you have to do the Z plain first. Each axis requires two sin and cos values and you had the wrong fields set for each. There is overlap from one axis to the other.

This is how I'm calling your "rotate" function now.

float test1 = 0;
void display (void)
{

rotate( test1, -test1, test1 );
test1 += 20;

works like a charm!

howie

capn_midnight
04-04-2004, 03:00 PM
that's weird. i had decided that it was the rotation function, but everything I found was saying my math was absolutely correct, including my book "Computer Graphics: Principles and Practice" by Foley, van Dam, Feiner, and Hughes.

howie
04-04-2004, 07:09 PM
I don't know about that book. Perhaps what they were doing doesn't translate to a 4x4 matrix. There are 2x2, 3x3 matrixes as well. I don't know.

capn_midnight
04-04-2004, 08:47 PM
figured it out, wasn't normalizing my axis of rotation.