1.11M Members

LookAt Matrix source code

 
0
 

Hi everyone, I need some help with the lookat matrix...

This is my code

Matrix Matrix::CreateLookAt(Vector3 eye, Vector3 target, Vector3 up)
{
	Vector3 forward = target - eye;
	forward.normalize();
	Vector3 side = Vector3::cross(forward, up);
	side.normalize();
	up = Vector3::cross(side, forward);

	Matrix m; // Defaults to identity matrix
	m.m11 = side.x;		m.m12 = up.x;		m.m13 = -forward.x;	
	m.m21 = side.y;		m.m22 = up.y;		m.m23 = -forward.y;	
	m.m31 = side.z;		m.m32 = up.z;		m.m33 = -forward.z;	

	m.m14 = -eye.x;
         m.m24 = -eye.y;
         m.m34 = -eye.z;

	return m;
}

And this is the mesa opensource opengl code

void GLAPIENTRY
gluLookAt(GLdouble eyex, GLdouble eyey, GLdouble eyez, GLdouble centerx,
	  GLdouble centery, GLdouble centerz, GLdouble upx, GLdouble upy,
	  GLdouble upz)
{
    float forward[3], side[3], up[3];
    GLfloat m[4][4];

    forward[0] = centerx - eyex;
    forward[1] = centery - eyey;
    forward[2] = centerz - eyez;

    up[0] = upx;
    up[1] = upy;
    up[2] = upz;

    normalize(forward);

    /* Side = forward x up */
    cross(forward, up, side);
    normalize(side);

    /* Recompute up as: up = side x forward */
    cross(side, forward, up);

    __gluMakeIdentityf(&m[0][0]);
    m[0][0] = side[0];
    m[1][0] = side[1];
    m[2][0] = side[2];

    m[0][1] = up[0];
    m[1][1] = up[1];
    m[2][1] = up[2];

    m[0][2] = -forward[0];
    m[1][2] = -forward[1];
    m[2][2] = -forward[2];

    glMultMatrixf(&m[0][0]);
    glTranslated(-eyex, -eyey, -eyez);
}

I started out with my own version which didn't work, and now i try to follow mesa's code but still failed. Can help me see what is wrong with my maths at all?

All my cross products are correct.

Unfortunately i cannot find the source for glTranslated() but assumed it is just the 4th column values applied. But I am obviously wrong. But that's not the only error, as can seen when I compare both matrix after the gluLookAt function.

 
0
 
Vector3 forward = target - eye;
forward.normalize();
Vector3 side = Vector3::cross(forward, up);
side.normalize();
up = Vector3::cross(side, forward);

I don't understand why you are recalculating the up direction.....
Surely you already know it at this point otherwise your side direction would be calculated incorrectly?
If you already know it then there is no point in trying to recalculate it.

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article