OpenGL - 使用动态相机闪烁

时间:2017-12-27 14:21:06

标签: c++ opengl glut

我正在尝试使用跟随我的 Car 对象的动态相机,以一直指向汽车前方的方式旋转。汽车一直在移动,这意味着它的原点属性在每一帧都会发生变化。

struct Point
{
    float x, y, z;
};  

struct Vector
{
    float dx, dy, dz;
public:
    Vector normalized() const; // returns the normalized vector   
    Point operator+(const Vector&) const; // Adds up a vector 
}


void draw() {

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glClearColor (1.0, 1.0, 1,0);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity ();
    gluPerspective(60.0, 1.0, 1.0, 1000.0);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    Point origin = car.origin; // The car's origin changes at every frame, 
                               // becase the car is moving

    // The direction vector points outwards the car'origin
    Vector direction = Vector(-origin.x, -origin.y, -origin.z).normalized();
    // The eye should be at a constant distance along the direction vector
    Point eye = origin + direction * 125.0;
    gluLookAt(eye.x, eye.y, eye.z, origin.x, origin.y, origin.z, 0, 1, 0);

    car.draw(); // calls the primitives that actually draw the car on the screen
    glutSwapBuffers();
}

这会导致很多闪烁。如果不是将摄像头指向汽车的前部,而是将其朝向-z轴并以这种方式计算眼睛点:

Point eye = origin + Vector(0,0,125);

闪烁消失了。我在上面的代码中做错了什么?

0 个答案:

没有答案