开发者

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,
};
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜