开发者

Why is this program not moving the camera correctly?

开发者 https://www.devze.com 2023-02-01 00:12 出处:网络
While pressing the down key, I expect the teapot to be drawn away as farther, yet it remains the same size. Why?

While pressing the down key, I expect the teapot to be drawn away as farther, yet it remains the same size. Why?

Note: this is a homework thing, I'm not allowed to use glTranslate.

#include <stdlib.h>
#include <stdio.h>
#include <GL/glut.h>
#开发者_如何学JAVAinclude <GL/gl.h>


void display(void);

class Camera {


public: float eyeX, eyeY, eyeZ, centerX, centerY, centerZ, upX, upY, upZ;
        float aimX, aimY, aimZ; 

        Camera () {

        eyeX = 0.0f ;
        eyeY = 0.0f;
        eyeZ = 0.5f ;

        centerX = 0.0f;
        centerY = 0.0f;
        centerZ = 0.0f;

        upX = 0.0f;
        upY = 1.0f;
        upZ = 0.0f; 

    }


    void move_camera(double speed) {

        aimX = centerX - eyeX;
        aimY = centerY - eyeY;
        aimZ = centerZ - eyeZ;



        eyeX += aimX * speed;
        eyeY += aimY * speed;
        eyeZ += aimZ * speed;

        centerX += aimX *speed;
        centerY += aimY *speed;
        centerZ += aimZ *speed;

    }

};






Camera camera; 

void init(void){

    glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);

    glMatrixMode(GL_MODELVIEW);

    glLoadIdentity();


}


void specialKeys(int key, int x, int y){


    if (key==GLUT_KEY_UP){
        camera.move_camera(0.03f);
        display();
    }

    if (key==GLUT_KEY_DOWN){
        camera.move_camera(-0.03f);
        display();

    }

}

void reshape(int w, int h){

    glViewport(0,0,w,h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45.0f, (float)w/(float)h, 0.0f, 200.0f); // fov, aspect ratio, ncp, fcp 
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();   

    //gluLookAt(camera.eyeX, camera.eyeY, camera.eyeZ, // eye 
    //        camera.centerX, camera.centerY, camera.centerZ, // center
    //        camera.upX, camera.upY, camera.upZ // up 
    //
    //);


}


void display(void){


    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();


    gluLookAt(camera.eyeX, camera.eyeY, camera.eyeZ, // eye 
              camera.centerX, camera.centerY, camera.centerZ, // center
              camera.upX, camera.upY, camera.upZ // up 
    );

    //glTranslatef(0.0,0.0,1.0f);
    glutWireTeapot(0.5f);



    glutSwapBuffers();
    glFlush();

}


int main (int argc, char *argv[]){

    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
    //glutCreateWindow(argv[0]);
    glutInitWindowPosition(500,200);
    glutInitWindowSize(800,600);
    glutCreateWindow("fgh");
    init();

    glutDisplayFunc(display);

    glutSpecialFunc(specialKeys);

    glutIdleFunc(display);

    glutMainLoop();

    return 0;

}


Your projection matrix is damaged.

gluPerspective(45.0f, (float)w/(float)h, 0.0f, 200.0f); // fov, aspect ratio, ncp, fcp 

The third argument is the distance of the near clipping plane. It cannot be equal to 0 as that would imply that you need an inifinite-precision depth buffer. Make it 0.1 or 0.01.


I was missing the call to glutReshapeFunc(reshape);. That's it.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号