0

The Problem arises when I go from Welcome screen which is in glOrtho to a other screen which is in gluPerspective. The only way I can render them both is when I resize the window after clicking to play from welcome screen, otherwise I receive black screen.

I think I need to fix the glVertex3f values for it to render properly as when I move directly to function without going to welcome screen, the polygon renders properly

Do check the gif below:

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

}
Sunim
  • 9
  • 1
  • 5
  • 2
    Call `myReshape()` when switching the screen to update the projection matrix. – Nico Schertler Jan 31 '18 at 12:01
  • done that, no success. Needs a small fix which I have not been able to find. Mind checking the github repo? – Sunim Jan 31 '18 at 13:59
  • @SunimAcharya: We don't check any external resources. Everything relevant to the question has to be in the question itself. – BDL Jan 31 '18 at 14:03
  • Does the code you provide here even compile? `void Drawkey1/2(){` doesn't look like valid C++. – BDL Jan 31 '18 at 14:07
  • I didn't provide all the code here so renamed the function as such to show which key1 value in myReshape results to which function – Sunim Jan 31 '18 at 14:13

1 Answers1

0

Update:

Fixed the error by making sure the glMatrixMode doesn't fetch GL_MODELVIEW when transition from GL_PROJECTION

Sunim
  • 9
  • 1
  • 5