为什么这个程序不能正确移动相机?

发布于 2024-10-09 08:35:01 字数 2633 浏览 2 评论 0原文

当按下向下键时,我预计茶壶会被拉得更远,但它的大小保持不变。为什么?

注意:这是一个家庭作业,我不允许使用 glTranslate

#include <stdlib.h>
#include <stdio.h>
#include <GL/glut.h>
#include <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;

}

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>
#include <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;

}

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

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

发布评论

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

评论(2

暮色兮凉城 2024-10-16 08:35:01

您的投影矩阵已损坏。

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

第三个参数是近剪裁平面的距离。它不能等于0,因为这意味着您需要无限精度的深度缓冲区。将其设为 0.10.01

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.

爱已欠费 2024-10-16 08:35:01

我错过了对 glutReshapeFunc(reshape); 的调用。就是这样。

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

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