如何使相机跟随一个3d对象在opengl? [英] how to make camera follow a 3d object in opengl?

查看:207
本文介绍了如何使相机跟随一个3d对象在opengl?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我第一次使用opengl赛车,我面临的第一个问题是如何使相机跟随车与恒定的距离。其中是键盘功能的代码.V是速度的

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


推荐答案

这可能吗?

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& stuff)必须像

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

这篇关于如何使相机跟随一个3d对象在opengl?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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