简体   繁体   English

gluLookAt替代方案不起作用

[英]gluLookAt alternative doesn't work

I'm trying to calculate a lookat matrix myself, instead of using gluLookAt(). 我正在尝试自己计算一个lookat矩阵,而不是使用gluLookAt()。 My problem is that my matrix doesn't work. 我的问题是我的矩阵不起作用。 using the same parameters on gluLookAt does work however. 然而,在gluLookAt上使用相同的参数确实有效。

my way of creating a lookat matrix: 我创建一个lookat矩阵的方法:

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. 现在,当我尝试使用这个矩阵来调用glMultMatrixf时,我的引擎中没有显示任何内容,而使用相同的眼睛,gluLookAt上的lookat和up值就像我之前说的那样完美。

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. 问题必须在我发布的代码中的某个地方,我知道问题不在我的Vector3 / Matrix类中,因为它们在创建投影矩阵时工作正常。

I assume you have a right handed coordinate system (it is default in OpenGL). 我假设你有一个右手坐标系(它在OpenGL中是默认的)。 Try the following code. 请尝试以下代码。 I think you forgot to normalize up and you have to put "-zaxis" in the matrix. 我想你忘了规范化了,你必须在矩阵中放入“-zaxis”。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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