在OpenGL C ++中使用glOrtho和gluPerspective

时间:2018-01-31 11:33:05

标签: c++ linux opengl freeglut

当我从glOrtho的欢迎屏幕转到另一个处于gluPerspective的屏幕时出现问题。 我可以渲染它们的唯一方法是在点击从欢迎屏幕播放后调整窗口大小,否则我会收到黑屏。

我认为我需要修复glVertex3f值才能正确渲染,因为当我直接移动到函数而不去欢迎屏幕时,多边形渲染正确

请检查下面的gif:

Gif run from VSCode

void myReshape(int w, int h)
{
    glViewport(0, 0, w, h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    if (key1 == 3){
        glOrtho(0.0, 100.0, 0.0, 100.0, -5.0 , 10.0);
        glMatrixMode(GL_MODELVIEW);
    }
    if (key1 == 1 || key1 == 2){
        gluPerspective(45.0, (float)w/(float)h, 0.1f, 200.0);
        gluLookAt(0.0, 0.0, 5.0,0.0, 0.0, 0.0,0.0, 1.0, 0.0); 
        glMatrixMode(GL_MODELVIEW);
    }
}
void par(float x1, float x2, float y1, float y2, float z1, float z2){
    glColor3f(0.3,0.56,0.84);

    glBegin(GL_POLYGON);

    glVertex3f(x1, y1, z1);
    glVertex3f(x2, y1, z1);
    glVertex3f(x2, y2, z1);
    glVertex3f(x1, y2, z1);

    glEnd();
}
void Drawkey1/2(){

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity ();

    glTranslatef(0.0, 0.0, -22.0);
    int i;
    par(-8.7,  9.2,  9.0,  9.2, 0.0, 0.0);
    par(-8.7,  9.2, -8.5, -8.7, 0.0, 0.0);
    par(-8.5, -8.7, -8.7,  9.2, 0.0, 0.0);
    par( 9.2,  9.0, -8.7,  9.2, 0.0, 0.0);
    while(p != NULL){
        par((p -> x)/2.0,(p -> x)/2.0 + 0.4,(p -> y)/2.0,(p -> y)/2.0 + 0.4, 0.0, 0.0);
        p = p -> nexploration_ratet;
    }
    par(food_x/2.0, food_x/2.0 + 0.4 , food_y/2.0 , food_y/2.0 + 0.4, 0.0 , 0.0);
}

void Drawkey3(){
glColor3f(0.3,0.56,0.84);
glBegin(GL_POLYGON);
glVertex3f(0.0,0.0,0.0);
glColor3f(0.137,0.137,0.556);
glVertex3f(100.0,0.0,0.0);
glColor3f(0.196,0.196,0.8);
glVertex3f(100.0,100.0,0.0);
glVertex3f(0.0,100.0,0.0);
glEnd();

glColor3f(0.196,0.196,0.8);
glRectf(39.5,39.5,60.5,45.5);

glColor3f(0.8,0.8,0.8);
glRectf(40,40,60,45);
glColor3f(0.137,0.137,0.556);
drawString(47,42,0,GLUT_BITMAP_HELVETICA_18,"USER");

glColor3f(0.196,0.196,0.8);
glRectf(39.5,29.5,60.5,35.5);

glColor3f(0.8,0.8,0.8);
glRectf(40,30,60,35);
glColor3f(0.137,0.137,0.556);
drawString(41,31,0,GLUT_BITMAP_HELVETICA_18,"NETWORK_PLAY");

glColor3f(0.196,0.196,0.8);
glRectf(39.5,19.5,60.5,25.5);

glColor3f(0.8,0.8,0.8);
glRectf(40,20,60,25);
glColor3f(0.137,0.137,0.556);
drawString(46,21,0,GLUT_BITMAP_HELVETICA_18,"HOW_TO");

glColor3f(0.196,0.196,0.8);
glRectf(39.5,9.5,60.5,15.5);

glColor3f(0.8,0.8,0.8);
glRectf(40,10,60,15);
glColor3f(0.137,0.137,0.556);
drawString(47,11,0,GLUT_BITMAP_HELVETICA_18,"EXIT");


glPushMatrix();

glColor3f(0.8,0.8,0.8);
drawString(25.5,92,0,GLUT_BITMAP_TIMES_ROMAN_24,"COMPUTER GRAPHICS PROJECT ");
drawString(35.5,80,0,GLUT_BITMAP_TIMES_ROMAN_24,"SRIJANA");
glPopMatrix();
glColor3f(0.137,0.137,0.556);

}

1 个答案:

答案 0 :(得分:0)

<强>更新

通过确保glMatrixMode在从GL_PROJECTION转换时无法获取GL_MODELVIEW来修复错误