2012-10-31 35 views
0

我有一些關於如何通過按一個鍵移動對象的問題。我想要做的就是按下鍵盤上的向上按鈕,使物體移動一個單位。openGL 2D移動和1個單元的對象

void display(){ 

    //Clear Window 
    glClear(GL_COLOR_BUFFER_BIT); 

    glPushMatrix(); 
    glBegin(GL_POLYGON); 
    glColor3f(1.0, 0.0, 0.0); 
    glVertex2f(-0.1, -0.2); 
    glVertex2f(-0.1, 0.2); 
    glVertex2f(0.1, 0.2); 
    glVertex2f(0.1, -0.2); 
    glEnd(); 
    glPopMatrix(); 
    glFlush(); 
} 


void keyboardListener(int key) 
{ 
    if(key == GLUT_KEY_UP) 
    { 
     glTranslatef(1.0, 0.0, 0.0); 
     glutPostRedisplay(); 
    } 
} 

有什麼缺失或什麼概念我不理解?

+0

您是否收到關鍵事件?怎麼了 ? – ixe013

+0

1st:您需要告訴我們什麼不適用於您的解決方案。第二:嘗試將你的問題改爲一般問題,而不是「調試我的代碼」。 – Kromster

+0

翻譯正在使對象在X軸上向上移動1個單位嗎?所以在「x」中多邊形的所有值都將上升一個單位而不是-0.1,圖像中的一個將爲0.0,0.1將爲0.2。我也發現奇怪的是,如果我按下右邊的按鈕,廣場將保持指向90度。哪種功能可以讓我在這種情況下向右轉90度,讓它指向0度?所有這些都沒有改變escene,因爲我認爲rotatef改變了視圖,而不是真正的對象的物理位置 –

回答

0

最簡單的方法是做一些像這樣

float posX = 0, posY = 0, posZ = 0; 

void display(){ 
    glClear(GL_COLOR_BUFFER_BIT); 
    glTranslate(posX,posY,posZ); 
    drawPolygon(); 
    //... 
} 


void keyboardListener(int key){ 
    if(key == GLUT_KEY_RIGHT){ posX++; } 
    else if(key == GLUT_KEY_LEFT){ posX--; } 
    //..similar for up/down 
    glutPostRedisplay(); 
} 
+0

感謝您的迴應,它看起來更容易理解,所以我沒有必要使用push和pop? –

+0

沒有在這種情況下,只要確保drawPolygon()繪製在原點居中,然後從那裏繼續 – dchhetri

+0

在哪些情況下,我使用push和pop?對不起,很煩人。當我嘗試應用此圖像時,圖像不會重新繪製,對象也不會移動。 –

1

使用本:

#include <stdio.h> 
#include <gl/glut.h> 

GLfloat rotation = 90.0; 
float posX = 0, posY = 0, posZ = 0; 

void reshape(int width, int heigth){ 
    /* window ro reshape when made it bigger or smaller*/ 

    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 

    //clip the windows so its shortest side is 2.0 
    if (width < heigth) { 
     glOrtho(-2.0, 2.0, -2.0 * (GLfloat)heigth/(GLfloat)width, 2.0 * (GLfloat)heigth/(GLfloat)width, 2.0, 2.0); 
    } 
    else{ 
     glOrtho(-2.0, 2.0, -2.0 * (GLfloat)width/(GLfloat)heigth, 2.0 * (GLfloat)width/(GLfloat)heigth,2.0 , 2.0); 
    } 
    // set viewport to use the entire new window 
    glViewport(0, 0, width, heigth); 
} 

void rect(){ 
    glBegin(GL_POLYGON); 
    glColor3f(1.0, 0.0, 0.0); 
    glVertex2f(-0.1, -0.2); 
    glVertex2f(-0.1, 0.2); 
    glVertex2f(0.1, 0.2); 
    glVertex2f(0.1, -0.2); 
    glEnd(); 

} 

void display(){ 
    //Clear Window 
    glClear(GL_COLOR_BUFFER_BIT); 
    glMatrixMode(GL_MODELVIEW); 
    glLoadIdentity(); 
    glPushMatrix(); 
    glTranslatef(posX,posY,posZ); 
    rect(); 
    glPopMatrix(); 
    glFlush(); 
} 


void init(){ 
    // set clear color to black 
    glClearColor(0.0, 0.0, 0.0, 0.0); 

    // set fill color to white 
    glColor3f(1.0, 1.0, 1.0); 

    //set up standard orthogonal view with clipping 
    //box as cube of side 2 centered at origin 
    //This is the default view and these statements could be removed 
    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 
    gluOrtho2D(-1.0, 1.0, -1.0, 1.0); 

} 
float move_unit = 0.1f; 
void keyboardown(int key, int x, int y) 
{ 
    switch (key){ 
     case GLUT_KEY_RIGHT: 
      posX+=move_unit;; 
      break; 

     case GLUT_KEY_LEFT: 
      posX-=move_unit;; 
     break; 

     case GLUT_KEY_UP: 
      posY+=move_unit;; 
      break; 

     case GLUT_KEY_DOWN: 
      posY-=move_unit;; 
     break; 

     default: 
     break; 
    } 
    glutPostRedisplay(); 
} 


int main(int argc, char** argv){ 

    //initialize mode and open a windows in upper left corner of screen 
    //Windows tittle is name of program 

    glutInit(&argc, argv); 
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); 
    glutInitWindowSize(500,500); 
    glutInitWindowPosition(0, 0); 
    glutCreateWindow("Practice 1"); 
    glutDisplayFunc(display); 
    init(); 
    glutSpecialFunc(keyboardown); 
    glutMainLoop(); 

} 
0

該代碼繪製一個圓,在左,右,上或下按鍵移動。

#include <cmath> 
#include <stdio.h> 
float posX = 0.01, posY = -0.1, posZ = 0; 

void circ() { 
    glColor3f(0.0, 0.0, 1.0); 
    glBegin(GL_TRIANGLE_FAN); 
    for (int i = 0; i <= 300; i++) { 
     angle = 2 * PI * i/300; 
     x = cos(angle)/20; 
     y = sin(angle)/20; 
     glVertex2d(x, y); 
    } 
    glEnd(); 
} 


void display() { 
    glClearColor(1.0, 1.0, 1.0, 0.0); 
    glClear(GL_COLOR_BUFFER_BIT); 

    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 
    gluOrtho2D(-1.0, 1.0, -1.0, 1.0); 

    glMatrixMode(GL_MODELVIEW); 
    glLoadIdentity(); 

    glPushMatrix(); 
    glTranslatef(posX, posY, posZ); 
    circ(); 
    glPopMatrix(); 

    glutSwapBuffers(); 
} 

**float move_unit = 0.02f; 
void keyboardown(int key, int x, int y) { 
    switch (key) { 
    case GLUT_KEY_RIGHT: 
     posX += move_unit; 
     break; 
    case GLUT_KEY_LEFT: 
     posX -= move_unit; 
     break; 
    case GLUT_KEY_UP: 
     posY += move_unit; 
     break; 
    case GLUT_KEY_DOWN: 
     posY -= move_unit; 
     break; 
    default: 
     break; 
    } 
glutPostRedisplay(); 
}** 



int main(int argc, char** argv) { 
    glutInit(&argc, argv); 
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); 
    glutInitWindowSize(600, 500); 
    glutInitWindowPosition(0, 0); 
    glutCreateWindow("Example"); 
    glutDisplayFunc(display); 
    glutSpecialFunc(keyboardown); 
    glutMainLoop(); 
} 
+0

每次按下箭頭鍵時,循環的座標都會更新。 – Umair