-1

我正在制作一个房间,我想更改lookat 的值,因为我想在我的房间内向上、向下、向右、向左、向前和向后移动我的相机。

我的代码在下面给出了注释:

#include <iostream>
using namespace std;
#include "glut.h" 
#include "GL/gl.h"  
//--------------------------------------------------------------------- 
static int all =0;
static int allD =0;
static int a=0;
static int  d=0;
static int o=0;
static float w=0;
static float s=0;
//--------------------------------------------------------------------- 
void init(void)
{
    glClearColor(0.0,0.0,0.0,0.0);
    glShadeModel(GL_FLAT);
}
//--------------------------------------------------------------------- 
void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT);
    glEnable(GL_DEPTH_TEST);
    //glEnable(GL_DEPTH);
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    //glDisable(GL_DEPTH_TEST);
    glPushMatrix();
    glRotatef(a,0,1,0);
    glRotatef(d,0,1,0);
    //back wall glPushMatrix();
    glTranslatef(0,0,-2);
    glColor3f(0.5,1,0);
    glScalef (50.0, 20.0, 2);
    glutSolidCube (0.5);
    glPopMatrix();
    //left wall glPushMatrix();
    glTranslatef(-12,0,4.5);
    glColor3f(1,1,0);
    glScalef (2, 20, 25);
    glutSolidCube (0.5);
    glPopMatrix();
    //floor glPushMatrix();
    glTranslatef(0,-5,4.5);
    glColor3f(0.5,1,1);
    glScalef (50, 0.2, 25);
    glutSolidCube (0.5);
    glPopMatrix();
    //roof glPushMatrix();
    glTranslatef(0,4.5,4.5);
    glColor3f(0,0,1);
    glScalef (50, 2, 25);
    glutSolidCube (0.5);
    glPopMatrix();
    //right wall glPushMatrix();
    glTranslatef(12,0,4.5);
    glColor3f(0.8,0.2,1);
    glScalef (2, 20, 25);
    glutSolidCube (0.5);
    glPopMatrix();
    ////front wall 1  //glPushMatrix();
    // glTranslatef(-2,0,9);
    // glColor3f(0.5,0,0);
    //  glScalef (12.0, 20.0, 2);
    // glutSolidCube (0.5);
    //glPopMatrix();
    ////front wall 2 //glPushMatrix();
    // glTranslatef(2.5,2.5,9);
    //  glColor3f(0.5,0,0);
    // glScalef (10.0, 10.0, 2);
    // glutSolidCube  (0.5);
    //glPopMatrix();
    ////Door //glPushMatrix();
    //  glTranslatef(0.8,-2,9);
    // glRotatef(o,0,1,0);
    //  glColor3f(0.5,0.5,0.5);
    // glScalef (1.0, 8.0, 0.2);
    //  glutSolidCube (0.5);
    //glPushMatrix();
    // glTranslatef(1.7,0,0);
    //  glColor3f(0.5,0.5,0.5);
    // glScalef (6.0, 1.5, 0.2);
    //  glutSolidCube (0.5);
    //glPopMatrix();
    //glPopMatrix();
    //table  glPopMatrix();
    glutPostRedisplay();
    glutSwapBuffers();
}
//--------------------------------------------------------------------- 
void reshape (int w, int h)
{
    glViewport (0, 0, (GLsizei) w,  (GLsizei) h);
    glMatrixMode (GL_PROJECTION);
    glLoadIdentity ();
    gluPerspective(65, (GLfloat) w/(GLfloat) h, 1.0, 100.0);
    //glFrustum(-2,2,-2,2,2.5,20);
    //glOrtho(-2,2,-2,2,2.5,20);
    gluLookAt (0.0, 0.0, 25, w, s, 0.0, 0.0, 1.0, 0.0);
    //i made it 'w'  and 's' but its not working. glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}
//--------------------------------------------------------------------- 
void keyboard (unsigned char key, int x, int y)
{
    switch (key)
    {
        case 'a': a = (a - 1) % 360;
        glutPostRedisplay();
        break;
        case 'd': d  = (d + 1) % 360;
        glutPostRedisplay();
        break;
        case 'o': o = (o + 1) % 90;
        glutPostRedisplay();
        break;
        case 'w': w = (w + 0.1) ;
        glutPostRedisplay();
        break;
        case 's': s = (s - 0.1);
        glutPostRedisplay();
        break;
        default: break;
    }
}
//--------------------------------------------------------------------- 
void SpecialKeys(int key, int x, int y)
{
    switch (key)
    {
        case  GLUT_KEY_RIGHT : all = (all + 10) % 360;
        glutPostRedisplay();
        break;
        case GLUT_KEY_UP : allD = (allD - 10) % 360;
        glutPostRedisplay();
        break;
        case GLUT_KEY_LEFT : all = (all - 10) % 360;
        glutPostRedisplay();
        break;
        case GLUT_KEY_DOWN : allD = (allD + 10)  % 360;
        glutPostRedisplay();
        break;
        default: break;
    }
}
//--------------------------------------------------------------------- 
int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
    glutInitWindowSize  (700, 500);
    glutInitWindowPosition (0,0);
    glutCreateWindow  (argv[0]);
    init ();
    glutDisplayFunc(display);
    glutSpecialFunc(SpecialKeys);
    glutReshapeFunc(reshape);
    glutKeyboardFunc(keyboard);
    glutMainLoop();
    return 0;
}
//
4

1 回答 1

0

gluLookAt()每当您更新传递给它的值时,您都需要重新执行该函数。

执行这部分代码的更简洁的方法是:

static int W = 0;
static int H = 0;
static int S = 0;
//---------------------------------------------------------------------
void updateCamera() {
    gluLookAt (0.0, 0.0, 25, W, S, 0.0, 0.0, 1.0, 0.0);
}
//---------------------------------------------------------------------
void reshape (int w, int h)
{
    W = w;
    H = h;
    glViewport (0, 0, (GLsizei) w,  (GLsizei) h);
    glMatrixMode (GL_PROJECTION);
    glLoadIdentity ();
    gluPerspective(65, (GLfloat) w/(GLfloat) h, 1.0, 100.0);
    //glFrustum(-2,2,-2,2,2.5,20);
    //glOrtho(-2,2,-2,2,2.5,20);
    updateCamera();
    glLoadIdentity();
}
//--------------------------------------------------------------------- 
void keyboard (unsigned char key, int x, int y)
{
    switch (key)
    {
        case 'a':
          a = (a - 1) % 360;
          break;
        case 'd':
          d  = (d + 1) % 360;
          break;
        case 'o':
          o = (o + 1) % 90;
          break;
        case 'w':
          W = (W + 0.1) ;
          updateCamera();
          break;
        case 's':
          S = (S - 0.1);
          updateCamera();
          break;
        default: break;
    }

    glutPostRedisplay();
}

一般来说,将您经常和/或来自不同地方的调用抽象出来是个好主意,这样您就不会复制粘贴并使未来的更新或错误修复变得更加困难。

于 2013-04-22T21:14:36.637 回答