PDA

View Full Version : Is the lookAt fuction correct?



08-29-2000, 11:57 AM
Hi, all

I impelemet look-at function as follow, the first part is the same of the gluLookAt function, but i use my own translate calculation, but as a result tt is different which used gltranslated? Please, help.
///////////////////////////////////////////

void lookAt(double eyex, double eyey, double eyez, double centerx, double centery, double centerz, double upx, double upy, double upz)
{
double m[16];
double x[3], y[3], z[3];
double mag;

/* Make rotation matrix */

/* Z vector */
z[0] = eyex - centerx;
z[1] = eyey - centery;
z[2] = eyez - centerz;
mag = sqrt(z[0] * z[0] + z[1] * z[1] + z[2] * z[2]);
if (mag) {
z[0] /= mag;
z[1] /= mag;
z[2] /= mag;
}

/* Y vector */
y[0] = upx;
y[1] = upy;
y[2] = upz;

/* X vector = Y cross Z */
x[0] = y[1] * z[2] - y[2] * z[1];
x[1] = -y[0] * z[2] + y[2] * z[0];
x[2] = y[0] * z[1] - y[1] * z[0];

/* Recompute Y = Z cross X */
y[0] = z[1] * x[2] - z[2] * x[1];
y[1] = -z[0] * x[2] + z[2] * x[0];
y[2] = z[0] * x[1] - z[1] * x[0];

/* cross product gives area of parallelogram, which is < 1.0 for
* non-perpendicular unit-length vectors; so normalize x, y here
*/

mag = sqrt(x[0] * x[0] + x[1] * x[1] + x[2] * x[2]);
if (mag) {
x[0] /= mag;
x[1] /= mag;
x[2] /= mag;
}

mag = sqrt(y[0] * y[0] + y[1] * y[1] + y[2] * y[2]);
if (mag) {
y[0] /= mag;
y[1] /= mag;
y[2] /= mag;
}

#define M(row,col) m[col*4+row]
M(0, 0) = x[0];
M(0, 1) = x[1];
M(0, 2) = x[2];
M(0, 3) = 0.0;
M(1, 0) = y[0];
M(1, 1) = y[1];
M(1, 2) = y[2];
M(1, 3) = 0.0;
M(2, 0) = z[0];
M(2, 1) = z[1];
M(2, 2) = z[2];
M(2, 3) = 0.0;
M(3, 0) = 0.0;
M(3, 1) = 0.0;
M(3, 2) = 0.0;
M(3, 3) = 1.0;
#undef M
mn[0] = t.m[0]*n[0] + t.m[4]*n[1] + t.m[8]*n[2] + t.m[12]*n[3];
mn[1] = t.m[1]*n[0] + t.m[5]*n[1] + t.m[9]*n[2] + t.m[13]*n[3];
mn[2] = t.m[2]*n[0] + t.m[6]*n[1] + t.m[10]*n[2] + t.m[14]*n[3];
mn[3] = t.m[3]*n[0] + t.m[7]*n[1] + t.m[11]*n[2] + t.m[15]*n[3];
mn[4] = t.m[0]*n[4] + t.m[4]*n[5] + t.m[8]*n[6] + t.m[12]*n[7];
mn[5] = t.m[1]*n[4] + t.m[5]*n[5] + t.m[9]*n[6] + t.m[13]*n[7];
mn[6] = t.m[2]*n[4] + t.m[6]*n[5] + t.m[10]*n[6] + t.m[14]*n[7];
mn[7] = t.m[3]*n[4] + t.m[7]*n[5] + t.m[11]*n[6] + t.m[15]*n[7];
mn[8] = t.m[0]*n[8] + t.m[4]*n[9] + t.m[8]*n[10] + t.m[12]*n[11];
mn[9] = t.m[1]*n[8] + t.m[5]*n[9] + t.m[9]*n[10] + t.m[13]*n[11];
mn[10]= t.m[2]*n[8] + t.m[6]*n[9] + t.m[10]*n[10] + t.m[14]*n[11];
mn[11]= t.m[3]*n[8] + t.m[7]*n[9] + t.m[11]*n[10] + t.m[15]*n[11];
mn[12]= t.m[0]*n[12] + t.m[4]*n[13] + t.m[8]*n[14] + t.m[12]*n[15];
mn[13]= t.m[1]*n[12] + t.m[5]*n[13] + t.m[9]*n[14] + t.m[13]*n[15];
mn[14]= t.m[2]*n[12] + t.m[6]*n[13] + t.m[10]*n[14] + t.m[14]*n[15];
mn[15]= t.m[3]*n[12] + t.m[7]*n[13] + t.m[11]*n[14] + t.m[15]*n[15];

/* Translate Eye to Origin */
mn[12] = mn[0]*(-eyex) + mn[4]*(-eyey) + mn[8]*(-eyez) + mn[12];
mn[13] = mn[1]*(-eyex) + mn[5]*(-eyey) + mn[9]*(-eyez) + mn[13];
mn[14] = mn[2]*(-eyex) + mn[6]*(-eyey) + mn[10]*(-eyez) + mn[14];
mn[15] = mn[3]*(-eyex) + mn[7]*(-eyey) + mn[11]*(-eyez) + mn[15];
}

foobar
08-30-2000, 09:18 AM
/* Translate Eye to Origin */
mn[12] = mn[0]*(-eyex) + mn[4]*(-eyey) + mn[8]*(-eyez) + mn[12];
mn[13] = mn[1]*(-eyex) + mn[5]*(-eyey) + mn[9]*(-eyez) + mn[13];
mn[14] = mn[2]*(-eyex) + mn[6]*(-eyey) + mn[10]*(-eyez) + mn[14];
mn[15] = mn[3]*(-eyex) + mn[7]*(-eyey) + mn[11]*(-eyez) + mn[15];
}

This is wrong because you are using mn[12]-mn[14] after modifying them - you need to remember the values before you began modifying the matrix (3 temp variables should do it), then it will work.



[This message has been edited by foobar (edited 08-30-2000).]