2014-04-06 42 views
0

我有下面的代碼片段:OpenGL的鍵盤事件不工作

float z, yrot = 0, xrot = 0; 
float x = 0.0; 
float y = 0.0; 

void display()             /* 2. */ 
{ 
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);   /* 3. */ 

glMatrixMode(GL_MODELVIEW_MATRIX); 
glLoadIdentity(); 

glMatrixMode (GL_MODELVIEW); 
glTranslatef(x, y, -0.5); 
glRotatef(rotation, 0, 0, -1); 
glColor3f(1.0,1.0,1.0); 
glutWireTeapot(0.2); 


glutSwapBuffers();           /* 4. */ 
} 

void keyPressed(unsigned char key, int x, int y) 
{ 

    float yrotrad = (yrot/(180 * 3.141592654f)); 
    float xrotrad = (xrot/(180 * 3.141592654f)); 

    switch (key) 
    { 

     case 'w': 

      x += (float)(sin(yrotrad)); 
      z -= (float)(cos(yrotrad)) ; 
      y -= (float)(sin(xrotrad)); 
      glutPostRedisplay(); 
      break; 
    } 
} 

int main(int argc, char **argv)         /* 5. */ 
{ 
    glutDisplayFunc(display); 
    glutKeyboardFunc(keyPressed); 
    glutMainLoop(); 
} 

什麼是應該當我按下瓦特關鍵的是,它應該傾斜相機發生。但是當我按w時,沒有任何反應。我究竟做錯了什麼?

+0

試「E」,區域設置問題也許 –

回答

2

xy參數被遮蔽的全局:

void keyPressed(unsigned char key, int x, int y) 

因此,那些的增量不會做你認爲它。打開你的編譯器警告 - 你可能會看到一些關於丟失精度的信息。

具有諷刺意味的是,它對z正常工作,但你似乎沒有在你的渲染代碼中的任何地方使用它。

(旁白:你可能也想看看GLFW這是相當相似的過剩和容易上手,但更好的在每一個方面)

+0

感謝幫助! –