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, side, up;
GLfloat m;

forward = centerx - eyex;
forward = centery - eyey;
forward = centerz - eyez;

up = upx;
up = upy;
up = 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);
m = side;
m = side;
m = side;

m = up;
m = up;
m = up;

m = -forward;
m = -forward;
m = -forward;

glMultMatrixf(&m);
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.

``````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 …

## All 2 Replies

``````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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.