We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,818 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

LookAt Matrix source code

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.

2
Contributors
1
Reply
3 Weeks
Discussion Span
2 Years Ago
Last Updated
2
Views
jakesee
Junior Poster
130 posts since Jul 2008
Reputation Points: 21
Solved Threads: 5
Skill Endorsements: 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.

Valaraukar
Junior Poster
130 posts since Mar 2009
Reputation Points: 16
Solved Threads: 10
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.5858 seconds using 2.65MB