View Full Version : Help on software implementation of gluLookAt()

02-21-2003, 06:36 PM
My code is like the following. All the data are proven to be correct and the single SoftTranslatef() is also correct.

* Get the look at matrix and store it into the matrix.
* NOTE : here the matrix is stored rather than multiplied.
* eye_x : the x coordinate of the eye point.
* eye_y : the y coordinate of the eye point.
* eye_z : the z coordinate of the eye point.
* center_x : the x coordinate of the center point.
* center_y : the y coordinate of the center point.
* center_z : the z coordinate of the center point.
* up_x : the x coordiante of the up vector.
* up_y : the y coordiante of the up vector.
* up_z : the z coordiante of the up vector.
* *m : the matrix to be stored.
void SoftGl::SoftLookAt(GLfloat eye_x, GLfloat eye_y, GLfloat eye_z,
GLfloat center_x, GLfloat center_y, GLfloat center_z,
GLfloat up_x, GLfloat up_y, GLfloat up_z, GLfloat *m)
GLfloat head_vector[3];
GLfloat roll_vector[3];
GLfloat up_vector[3];

head_vector[0] = center_x - eye_x;
head_vector[1] = center_y - eye_y;
head_vector[2] = center_z - eye_z;

up_vector[0] = up_x;
up_vector[1] = up_y;
up_vector[2] = up_z;

//get the effect of rotate first
GeoUtility::XProduct(head_vector, up_vector, roll_vector);

memset(m, 0, 16 * sizeof(GLfloat));
memcpy(m, roll_vector, 3 * sizeof(GLfloat));
memcpy(m + 4, up_vector, 3 * sizeof(GLfloat));
memcpy(m + 8, head_vector, 3 * sizeof(GLfloat));
m[15] = 1;

//then multiply the translation
//notice that the translation is to the reverse direction
SoftTranslatef(-eye_x, -eye_y, -eye_z, m);

However, when I tried this code with a single translation to the positive direction of z axis, the [10] position of the matrix is -1, while the matrix got from OpenGL stack is 1.

Anyone can tell me what is the problem of the above code? I really was confused.

Thank you very much.

02-21-2003, 09:05 PM
You can drop an eye in the glu library source code ;) it's available in most of the Mesa releases -> http://www.mesa3d.org

02-22-2003, 01:46 AM
well im not exactely recreating the look at function, but the class i implemented can create quite similar matrices:

TKoo3d<GLfloat> n;
TKoo3d<GLfloat> u;
TKoo3d<GLfloat> v;


viewMatrix.v[0][0]=u.v[0]; viewMatrix.v[1][0]=u.v[1]; viewMatrix.v[2][0]=u.v[2];viewMatrix.v[3][0]=-pos.v[0];
viewMatrix.v[0][1]=v.v[0]; viewMatrix.v[1][1]=v.v[1]; viewMatrix.v[2][1]=v.v[2];viewMatrix.v[3][1]=-pos.v[1];
viewMatrix.v[0][2]=n.v[0]; viewMatrix.v[1][2]=n.v[1]; viewMatrix.v[2][2]=n.v[2];viewMatrix.v[3][2]=-pos.v[2];

the only thing to do to set the camera is to load the viewMatrix

oh and if the difference in your matrix is only a -1 instead of a 1 then maybe you should try to reverse some cross products (they arent kommutative (hope thats the right word in english http://www.opengl.org/discussion_boards/ubb/smile.gif ))

btw are you assuming that head and up vector are always perpendicular?

[This message has been edited by Chuck0 (edited 02-22-2003).]