cignox1

09-15-2007, 04:35 AM

Hi all, I don't know if this is the right forum where to post my question. I'm not even using openGL, but I'm writing a matrix class that follows the same layout and conventions than openGL, so I hope to receive halp here.

I'm writing a LookAt method with the following code:

void matrix::LookAt(const vector4 &eye, const vector4 &target, const vector4 &up)

{

if(eye == target)

{

Identity();

return; //eye and target are the same

}

vector4 z(eye - target);

z.Normalize();

vector4 u(up);

if(IsZero(z.x) && !IsZero(z.y) && IsZero(z.z)) u = vector4(0, 0, 1.0);

vector4 x(u^z);

x.Normalize();

vector4 y(z^x);

mat[0][0] = x.x;

mat[0][1] = x.y;

mat[0][2] = x.z;

mat[0][3] = 0;

mat[1][0] = y.x;

mat[1][1] = y.y;

mat[1][2] = y.z;

mat[1][3] = 0;

mat[2][0] = z.x;

mat[2][1] = z.y;

mat[2][2] = z.z;

mat[2][3] = 0;

mat[3][0] = -(x*eye);

mat[3][1] = -(y*eye);

mat[3][2] = -(z*eye);

mat[3][3] = 1;

}

I searched all over the web for the formula. I used the one specified in the DX reference for right handed matrix, as well as many other places. This one just flips my y axis.

With the following parameters I expect to ge the identity matrix:

eye = (0,0,0)

target = (0,0,-1)

up = (0,1,0).

But the element in (1,1) is -1 instead than 1.

Where am I wrong?

Thank you!

I'm writing a LookAt method with the following code:

void matrix::LookAt(const vector4 &eye, const vector4 &target, const vector4 &up)

{

if(eye == target)

{

Identity();

return; //eye and target are the same

}

vector4 z(eye - target);

z.Normalize();

vector4 u(up);

if(IsZero(z.x) && !IsZero(z.y) && IsZero(z.z)) u = vector4(0, 0, 1.0);

vector4 x(u^z);

x.Normalize();

vector4 y(z^x);

mat[0][0] = x.x;

mat[0][1] = x.y;

mat[0][2] = x.z;

mat[0][3] = 0;

mat[1][0] = y.x;

mat[1][1] = y.y;

mat[1][2] = y.z;

mat[1][3] = 0;

mat[2][0] = z.x;

mat[2][1] = z.y;

mat[2][2] = z.z;

mat[2][3] = 0;

mat[3][0] = -(x*eye);

mat[3][1] = -(y*eye);

mat[3][2] = -(z*eye);

mat[3][3] = 1;

}

I searched all over the web for the formula. I used the one specified in the DX reference for right handed matrix, as well as many other places. This one just flips my y axis.

With the following parameters I expect to ge the identity matrix:

eye = (0,0,0)

target = (0,0,-1)

up = (0,1,0).

But the element in (1,1) is -1 instead than 1.

Where am I wrong?

Thank you!