OpenGL:使用 lookAt 功能时相机未按预期移动
OpenGL: Camera not moving as expected when using lookAt function
我目前正在尝试绕着我创建的迷宫的周边飞行。这是我的 idle
函数的代码,我在其中调用 lookAt
函数:
void idle()
{
GLfloat angle_radian = D2R*angle;
float eye_x = 10 * -1*cos(angle_radian);
float eye_z = 10 * sin(angle_radian);
vec4 eye = {eye_x, 12, eye_z, 0};
vec4 at = {5, 0, 5, 0};
vec4 up = {0, 1, 0, 0};
model_view = look_at(eye, at, up);
angle += 0.5;
glutPostRedisplay();
}
摄像机正确地绕着迷宫转了一圈,但是,摄像机似乎也在旋转过程中改变了向下看的角度。这让我很困惑,因为我正在看的地方的 Y 坐标没有改变(迷宫是在 y=0 平面上构建的)。我相当确定我对 lookAt
函数的实现是正确的,但这里是代码,以防我忽略了一些东西:
mat4 look_at(vec4 eye, vec4 at, vec4 up)
{
vec4 vpn = vec4_sub(eye, at);
vec4 n = vec4_norm(vpn);
vec4 u = vec4_norm(vec4_cross(up, n));
vec4 v = vec4_norm(vec4_cross(n, u));
vec4 p = { eye.x, eye.y, eye.z, 1 };
mat4 m;
m.x.x = u.x; m.y.x = u.y; m.z.x = u.z; m.w.x = u.w;
m.x.y = v.x; m.y.y = v.y; m.z.y = v.z; m.w.y = v.w;
m.x.z = n.x; m.y.z = n.y; m.z.z = n.z; m.w.z = n.w;
m.x.w = p.x; m.y.w = p.y; m.z.w = p.z; m.w.w = p.w;
mat4 m_t = mat4_trans(m);
mat4 m_t_inv= mat4_inv(m_t);
return m_t_inv;
}
你不看(0, 0, 0),但你看(5, 0, 5) .因此,需要根据目标计算旋转 (5, 0, 5):
vec4 at = {5, 0, 5, 0};
float eye_x = 10 * -cos(angle_radian) + at[0];
float eye_y = 12 + at[1];
float eye_z = 10 * sin(angle_radian) + at[1];
vec4 eye = {eye_x, eye_y, eye_z, 0};
vec4 up = {0, 1, 0, 0};
model_view = look_at(eye, at, up);
我目前正在尝试绕着我创建的迷宫的周边飞行。这是我的 idle
函数的代码,我在其中调用 lookAt
函数:
void idle()
{
GLfloat angle_radian = D2R*angle;
float eye_x = 10 * -1*cos(angle_radian);
float eye_z = 10 * sin(angle_radian);
vec4 eye = {eye_x, 12, eye_z, 0};
vec4 at = {5, 0, 5, 0};
vec4 up = {0, 1, 0, 0};
model_view = look_at(eye, at, up);
angle += 0.5;
glutPostRedisplay();
}
摄像机正确地绕着迷宫转了一圈,但是,摄像机似乎也在旋转过程中改变了向下看的角度。这让我很困惑,因为我正在看的地方的 Y 坐标没有改变(迷宫是在 y=0 平面上构建的)。我相当确定我对 lookAt
函数的实现是正确的,但这里是代码,以防我忽略了一些东西:
mat4 look_at(vec4 eye, vec4 at, vec4 up)
{
vec4 vpn = vec4_sub(eye, at);
vec4 n = vec4_norm(vpn);
vec4 u = vec4_norm(vec4_cross(up, n));
vec4 v = vec4_norm(vec4_cross(n, u));
vec4 p = { eye.x, eye.y, eye.z, 1 };
mat4 m;
m.x.x = u.x; m.y.x = u.y; m.z.x = u.z; m.w.x = u.w;
m.x.y = v.x; m.y.y = v.y; m.z.y = v.z; m.w.y = v.w;
m.x.z = n.x; m.y.z = n.y; m.z.z = n.z; m.w.z = n.w;
m.x.w = p.x; m.y.w = p.y; m.z.w = p.z; m.w.w = p.w;
mat4 m_t = mat4_trans(m);
mat4 m_t_inv= mat4_inv(m_t);
return m_t_inv;
}
你不看(0, 0, 0),但你看(5, 0, 5) .因此,需要根据目标计算旋转 (5, 0, 5):
vec4 at = {5, 0, 5, 0};
float eye_x = 10 * -cos(angle_radian) + at[0];
float eye_y = 12 + at[1];
float eye_z = 10 * sin(angle_radian) + at[1];
vec4 eye = {eye_x, eye_y, eye_z, 0};
vec4 up = {0, 1, 0, 0};
model_view = look_at(eye, at, up);