2013-04-22 19 views
-1

我正在創建一個房間,我想更改lookat的值,因爲我想在我的房間內向上,向下,向右,向左和向後移動相機。下面如何用鍵盤更改glLookat視圖元素的整數值?

我的代碼會加上一個註釋:

#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; 
} 
// 
+0

請格式化您的問題。 – 2013-04-22 09:37:14

回答

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(); 
} 

在一般情況下,這是一個好主意,抽象出來,這樣你不是活得你讓經常和/或來自不同地方的呼籲複製粘貼並使未來的更新或錯誤修復更加困難。

+0

謝謝,幫助我很多:) – 2013-04-27 08:24:12

+0

很高興幫助!你能將這個答案標記爲接受嗎? – Nolnoch 2013-04-27 16:11:17