gpt4 book ai didi

c++ - 相机旋转 (OpenGL)

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:36:26 25 4
gpt4 key购买 nike

我在尝试在我的程序中使用相机类时遇到问题。当我更改 gluLookAt 调用的 camera_target 时,我的整个地形都在旋转,而不仅仅是相机像它应该的那样旋转。

这是我的渲染方法中的一些代码:

camera->Place();

ofSetColor(255, 255, 255, 255);

//draw axis lines
//x-axis
glBegin(GL_LINES);
glColor3f(1.0f,0.0f,0.0f);
glVertex3f(0.0f,0.0f,0.0f);
glVertex3f(100.0f, 0.0f,0.0f);
glEnd();

//y-axis
glBegin(GL_LINES);
glColor3f(0.0f,1.0f,0.0f);
glVertex3f(0.0f,0.0f,0.0f);
glVertex3f(0.0f, 100.0f,0.0f);
glEnd();

//z-axis
glBegin(GL_LINES);
glColor3f(0.0f,0.0f,1.0f);
glVertex3f(0.0f,0.0f,0.0f);
glVertex3f(0.0f, 0.0f,100.0f);
glEnd();

glColor3f(1,1,1);

terrain->Draw();

还有我的相机类中的旋转和放置方法:

void Camera::RotateCamera(float h, float v){
hRadians += h;
vRadians += v;

cam_target.y = cam_position.y+(float)(radius*sin(vRadians));
cam_target.x = cam_position.x+(float)(radius*cos(vRadians)*cos(hRadians));
cam_target.z = cam_position.z+(float)(radius*cos(vRadians)*sin(hRadians));

cam_up.x = cam_position.x-cam_target.x;
cam_up.y = ABS(cam_position.y+(float)(radius*sin(vRadians+PI/2))) ;
cam_up.z = cam_position.z-cam_target.z;
}

void Camera::Place() {
//position, camera target, up vector
gluLookAt(cam_position.x, cam_position.y, cam_position.z, cam_target.x, cam_target.y, cam_target.z, cam_up.x, cam_up.y, cam_up.z);
}

问题是整个地形都在围绕相机移动,而相机应该只是旋转。

感谢您的帮助!

编辑 - 找到一些很棒的教程并考虑到此处的答案,我制作了一个更好的相机类(class)。谢谢大家

最佳答案

从地形的 POV 来看,是的,相机在旋转。但是,由于您的 View 来自相机的 POV,因此当您旋转相机时,地形似乎在旋转。这是 gluLookAt() 的行为旨在生产。如果有其他您期望的东西,您将只需要旋转您想要旋转的几何体,而不是尝试使用 gluLookAt() 进行旋转。

更新 1:根据下面的讨论,试试这个:

void Camera::RotateCamera(float h, float v)
{
hRadians += h;
vRadians += v;

cam_norm.x = cos(vRadians) * sin(hRadians);
cam_norm.y = -sin(vRadians);
cam_norm.z = cos(vRadians) * sin(hRadians);

cam_up.x = sin(vRadians) * sin(hRadians);
cam_up.y = cos(vRadians);
cam_up.z = sin(vRadians) * cos(hRadians);
}

void Camera::Place()
{
//position, camera target, up vector
gluLookAt(cam_pos.x, cam_pos.y, cam_pos.z,
cam_pos.x+cam_norm.x, cam+pos.y+cam_norm.y, camp_pos.z+cam_norm.z,
cam_up.x, cam_up.y, cam_up.z);
}

将相机法线(相机的观察方向)与位置分开,可以让您独立更改位置、平移和倾斜……也就是说,您可以更改位置而无需重新计算法线和向上 vector 。

免责声明:这是未经测试的,只是我可以在一张纸的背面做的。它采用右手坐标系,并且在俯仰之前应用平移旋转。

更新 2:使用线性代数而不是几何推导......同样,未经测试,但我对此更有信心。

关于c++ - 相机旋转 (OpenGL),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3494520/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com