简体   繁体   中英

gluLookAt alternative doesn't work

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.

I assume you have a right handed coordinate system (it is 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,
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM