2013-04-28 91 views
0

我正在嘗試繪製一個簡單的立方體,並沿着屏幕上的左箭頭鍵和右鍵向下(分別向左和右方向)移動它。我無法確切知道gluOrtho2d(leftortho,rightortho,bottomortho,toportho)。我用六個參數嘗試過glOrtho(leftortho,rightortho,bottomortho,toportho,-1.0,1.0),但結果是一樣的。立方體移動到右上角消失。我需要幫助在這方面..Open GL設置正射投影

double leftortho=5.0,rightortho=1.0,bottomortho=5.0,toportho=1.0; 

void init(void) 
{ 
    glClearColor (0.0, 0.0, 0.0, 0.0); 
    glShadeModel (GL_FLAT); 
} 

void display(void) 
{ 
    glClear (GL_COLOR_BUFFER_BIT); 
    glColor3f (1.0, 1.0, 1.0); 
    glutWireCube (1.0); 

    gluOrtho2D(leftortho,rightortho,bottomortho,toportho); 
    glFlush(); 
} 

void reshape (int w, int h) 
{ 
    glViewport (0, 0, (GLsizei) w, (GLsizei) h); 
} 

float move_unit = 0.001; 
void keyboardown(int key, int x, int y) 
{ 
    switch (key) 
    { 
    case GLUT_KEY_RIGHT: 
     { 
     leftortho+=move_unit; 
     rightortho+=move_unit; 
     //bottomortho+=move_unit; 
     //toportho+=move_unit; 

     break; 
     } 

    case GLUT_KEY_LEFT: 
     { 
     leftortho-=move_unit; 
     rightortho-=move_unit; 
     //bottomortho-=move_unit; 
     //toportho-=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_SINGLE | GLUT_RGB); 
    glutInitWindowSize (500, 500); 
    glutInitWindowPosition (100, 100); 
    glutCreateWindow (argv[0]); 
    init(); 
    glutDisplayFunc(display); 
    glutReshapeFunc(reshape); 
    glutSpecialFunc(keyboardown); 
    glutMainLoop(); 
    return 0; 
} 

回答

0

我認爲問題是,你是不是要求gluOrtho2d()前的投影矩陣重置,讓您的動作正在積累。我認爲你需要這樣做:

glMatrixMode(GL_PROJECTION); 
glLoadIdentity(); 
gluOrtho2d(left,right,bottom,top); 

請注意,你的左,右似乎是交換,你的底部和頂部。這是合法的,但最終可能會導致左右箭頭鍵反轉。它基本上將您的轉換設置爲倒退和顛倒。

1

這給一個鏡頭:

#include <GL/glut.h> 

double leftortho=-1.0; 
double rightortho=1.0; 
double bottomortho=-1.0; 
double toportho=1.0; 
float move_unit = 0.01; 
void keyboardown(int key, int x, int y) 
{ 
    switch (key) 
    { 
    case GLUT_KEY_RIGHT: 
     leftortho-=move_unit; 
     rightortho-=move_unit; 
     break; 

    case GLUT_KEY_LEFT: 
     leftortho+=move_unit; 
     rightortho+=move_unit; 
     break; 

    case GLUT_KEY_UP: 
     toportho-=move_unit; 
     bottomortho-=move_unit; 
     break; 

    case GLUT_KEY_DOWN: 
     toportho+=move_unit; 
     bottomortho+=move_unit; 
     break; 

    default: 
     break; 
    } 
    glutPostRedisplay(); 
} 

void display(void) 
{ 
    glClearColor (0.0, 0.0, 0.0, 0.0); 
    glClear (GL_COLOR_BUFFER_BIT); 
    glShadeModel (GL_FLAT); 

    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 
    glOrtho(leftortho,rightortho,bottomortho,toportho, -10, 10); 

    glMatrixMode(GL_MODELVIEW); 
    glLoadIdentity(); 

    glColor3f (1.0, 1.0, 1.0); 
    glutWireCube (1.0); 

    glutSwapBuffers(); 
} 

int main(int argc, char** argv) 
{ 
    glutInit(&argc, argv); 
    glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB); 
    glutInitWindowSize (500, 500); 
    glutInitWindowPosition (100, 100); 
    glutCreateWindow (argv[0]); 
    glutDisplayFunc(display); 
    glutSpecialFunc(keyboardown); 
    glutMainLoop(); 
    return 0; 
}