如何让相机跟随opengl中的3D物体?

发布于 2024-11-10 16:20:52 字数 659 浏览 1 评论 0原文

我第一次使用opengl制作赛车,我面临的第一个问题是如何让相机以恒定的距离跟随汽车。这是键盘功能的代码。V是汽车的速度。

void OnSpecial(int key, int x, int y) 
{
    float step = 5;

    switch(key) {

    case GLUT_KEY_LEFTa:
        carAngle = step;
        V.z = carAngle ;
        camera.Strafe(-step/2);
        break;

    case GLUT_KEY_RIGHT:
        carAngle = -step;
        V.z = carAngle ;
        camera.Strafe(step/2);
        break;

    case GLUT_KEY_UP:
        V.x += (-step);
        camera.Walk(step/2);

        break;
    case GLUT_KEY_DOWN:
        if(V.x<0)
        {
            V.x += step;
            camera.Walk(-step/2);
        }
        break;
    }
}

i'm making a car race for the first time using opengl,the first problem i face is how to make the camera follow the car with constant distance..here is the code for keyboard function.V is the velocity of the car.

void OnSpecial(int key, int x, int y) 
{
    float step = 5;

    switch(key) {

    case GLUT_KEY_LEFTa:
        carAngle = step;
        V.z = carAngle ;
        camera.Strafe(-step/2);
        break;

    case GLUT_KEY_RIGHT:
        carAngle = -step;
        V.z = carAngle ;
        camera.Strafe(step/2);
        break;

    case GLUT_KEY_UP:
        V.x += (-step);
        camera.Walk(step/2);

        break;
    case GLUT_KEY_DOWN:
        if(V.x<0)
        {
            V.x += step;
            camera.Walk(-step/2);
        }
        break;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟火散人牵绊 2024-11-17 16:20:52

也许是这样的?

vec3 cameraPosition = carPosition + vec3(20*cos(carAngle), 10,20*sin(carAngle));
vec3 cameraTarget = carPosition;
vec3 cameraUp = vec3(0,1,0);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity()
gluLookAt(cameraPosition, cameraTarget, cameraUp);
glTranslate(carPosition);
drawCar();

如果您不使用旧的且已弃用的 openGL API(glBegin 等),您将不得不执行类似的操作

mat4 ViewMatrix = LookAt(cameraPosition, cameraTarget, cameraUp); // adapt depending on what math library you use

Something like that maybe ?

vec3 cameraPosition = carPosition + vec3(20*cos(carAngle), 10,20*sin(carAngle));
vec3 cameraTarget = carPosition;
vec3 cameraUp = vec3(0,1,0);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity()
gluLookAt(cameraPosition, cameraTarget, cameraUp);
glTranslate(carPosition);
drawCar();

It you're not using the old and deprecated openGL API (glBegin & stuff) you'll have to do something like

mat4 ViewMatrix = LookAt(cameraPosition, cameraTarget, cameraUp); // adapt depending on what math library you use
入怼 2024-11-17 16:20:52

答案很简单。您拥有玩家控制的对象(汽车),因此您可以通过世界空间中的 ModelViewMatrix 获得其位置和方向(通常指向 3D 模型的中心)
要将其转换为正确的 ModelViewMatrix,您必须:

  1. 获取或构造汽车 ModelMatrix 作为 double M[16]< /strong>


  2. 将其平移/旋转到新位置(驾驶舱内或在车后面)

    所以 Z 轴指向您想要看到的方向。通常跟随距离是速度的函数

  3. 反转M,因此M=Inverse(M)

  4. 使用M作为ModelViewMatrix

  5. 渲染

简而言之:

ModelViewMatrix = rendered_object_matrix * Inverse(following_object_matrix * local_view_offset)

有关您需要的其他内容,请查看我的答案:

The answer to that is simple. You have player controlled object (car) so you have its position and orientation via ModelViewMatrix in world space (usually pointed to the center of 3D model)
To transform it to the correct follow ModelViewMatrix you must:

  1. obtain or construct car ModelMatrix as double M[16]

  2. translate/rotate it to the new position (inside cockpit or behind car)

    so the Z axis is pointing the way you want to see. Its usual to have the follow distance as a function of speed

  3. Invert M so M=Inverse(M)

  4. use M as ModelViewMatrix

  5. render

so in an nutshell:

ModelViewMatrix = rendered_object_matrix * Inverse(following_object_matrix * local_view_offset)

for additional stuff you need for this look at my answer here:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文