tags:

views:

97

answers:

1

Hey guys.

I'm trying to calculate a lookat matrix myself, instead of using gluLookAt(). My problem is that my matrix doesn't work. using the same parameters on gluLookAt does work however.

my way of creating a lookat matrix:

Vector3 Eye, At, Up; //these should be parameters =)

Vector3 zaxis = At - Eye;           zaxis.Normalize();
Vector3 xaxis = Vector3::Cross(Up, zaxis);  xaxis.Normalize();
Vector3 yaxis = Vector3::Cross(zaxis, xaxis);   yaxis.Normalize();

float r[16] = 
{
    xaxis.x,    yaxis.x,    zaxis.x,    0,
    xaxis.y,    yaxis.y,    zaxis.y,    0,
    xaxis.z,    yaxis.z,    zaxis.z,    0,
    0,          0,          0,          1,
};
Matrix Rotation;
memcpy(Rotation.values, r, sizeof(r));

float t[16] = 
{
     1,      0,      0,     0,
     0,      1,      0,     0,
     0,      0,      1,     0,
    -Eye.x, -Eye.y, -Eye.z, 1,
};
    Matrix Translation;
    memcpy(Translation.values, t, sizeof(t));


View = Rotation * Translation; // i tried reversing this as well (translation*rotation)

now, when i try to use this matrix be calling glMultMatrixf, nothing shows up in my engine, while using the same eye, lookat and up values on gluLookAt works perfect as i said before.

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glMultMatrixf(View);

the problem must be in somewhere in the code i posted here, i know the problem is not in my Vector3/Matrix classes, because they work fine when creating a projection matrix.

A: 

I assume you have a right handed coordinate system (default in OpenGL). Try the following code. I think you forgot to normalize up and you have to put "-zaxis" in the matrix.

Vector3 Eye, At, Up; //these should be parameters =)

Vector3 zaxis = At - Eye; zaxis.Normalize();
Up.Normalize();
Vector3 xaxis = Vector3::Cross(Up, zaxis);  xaxis.Normalize();
Vector3 yaxis = Vector3::Cross(zaxis, xaxis);   yaxis.Normalize();

float r[16] = 
{
    xaxis.x,    yaxis.x,    -zaxis.x,    0,
    xaxis.y,    yaxis.y,    -zaxis.y,    0,
    xaxis.z,    yaxis.z,    -zaxis.z,    0,
    0,          0,          0,          1,
};
Lars Schneider