-1

I am making a room and i want to change the values of lookat because i want to move my camera up,down,right,left,forward and backward inside my room.

My code is given below with comments:

#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 に答える