In OpenGL, how can I adjust for the window being resized?

前端 未结 5 1139
眼角桃花
眼角桃花 2020-12-16 01:08

I am drawing several shapes (such as circles) that are keyed off of the window height & width. Since the window always starts at a given size, they are drawn correctly,

5条回答
  •  佛祖请我去吃肉
    2020-12-16 01:15

    I am learning opengl too. I faced this problem yesterday and I have not found the correct answer. Today, I have solve the problem. In my little program, the object changes size when resizing the window changing width or heigt. This is my code (please, forgive my errors):

    #include 
    
    void fnReshape(int ww, int hh)
    {
      GLdouble r;
      if(hh == 0)
        hh = 1.0;
      if(hh > ww)
        r = (double) hh / (double) ww;
      else
        r = (double) ww / (double) hh;
      glViewport(0, 0, ww, hh);
      glMatrixMode(GL_PROJECTION);
      glLoadIdentity();
      if(hh > ww)
        glFrustum(-1.0, 1.0, -1.0 * r, 1.0 * r, 2.0, 200.0);
      else
        glFrustum(-1.0 * r, 1.0 * r, -1.0, 1.0, 2.0, 200.0);
    }
    //////////////////////////////////////////////////////////////////
    void fnDisplay(void)
    {
      glClear(GL_COLOR_BUFFER_BIT);
      glMatrixMode(GL_MODELVIEW);
      glLoadIdentity();
      glTranslatef(0., 0., -5.);
      glBegin(GL_QUAD_STRIP);
      glColor3f(0., 0., 1.0);
      glVertex3f(-1., -1., 0.);
      glVertex3f(-1., 1., 0.);
      glVertex3f(1., -1., 0.);
      glVertex3f(1., 1., 0.);
      glEnd();
      glutSwapBuffers();
    }
    ////////////////////////////////////////////////////////////
    int main(int argc, char **argv) {
        glutInit(&argc, argv);
        glutInitDisplayMode(GLUT_RGBA |GLUT_DOUBLE);
        glutInitWindowSize(500, 500);
        glutCreateWindow("Sample window");
        glClearColor(1.0, 0, 0, 1.0);
        glutDisplayFunc(fnDisplay);
        glutReshapeFunc(fnReshape);
        glutMainLoop();
        return 0;
    }
    

提交回复
热议问题