gluLookAt替代品不起作用 [英] gluLookAt alternative doesn't work

查看:153
本文介绍了gluLookAt替代品不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试自己计算一个lookat矩阵,而不是使用gluLookAt(). 我的问题是我的矩阵不起作用.但是,在gluLookAt上使用相同的参数确实可以工作.

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.

我创建lookat矩阵的方式:

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)

现在,当我尝试使用此矩阵调用glMultMatrixf时,引擎中什么都没有显示,同时使用相同的眼睛,look和up值对gluLookAt进行操作就像我之前所说的一样.

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);

问题一定存在于我在此处发布的代码中的某个地方,我知道问题不在我的Vector3/Matrix类中,因为在创建投影矩阵时它们可以正常工作.

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.

推荐答案

我假设您拥有右手坐标系(在OpenGL中是默认设置). 请尝试以下代码.我认为您忘了规范化,您必须在矩阵中添加"-zaxis".

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

这篇关于gluLookAt替代品不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆