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

前端 未结 5 1140
眼角桃花
眼角桃花 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条回答
  •  猫巷女王i
    2020-12-16 01:33

    There are many methods to do that. Following example shows you how I'm doing it in my projects and its working. The example shows you simply a rectangle box which do not changes it size when window is resizing. You can directly copy and paste it in your project(consider that i'm a openGl beginner)

    #include
    #include
    
    GLfloat x1=100.0f;
    GLfloat y1=150.0f;
    GLsizei rsize = 50;
    
    GLfloat xstep=1.0f;
    GLfloat ystep=1.0f;
    
    GLfloat windowWidth;
    GLfloat windowHeight;
    
    
    void scene(void){
       glClear(GL_COLOR_BUFFER_BIT);
       glColor3f(1.0f,0.0f,0.0f);
    
       glRectf(x1,y1,x1+rsize,y1-rsize);
    
       glutSwapBuffers();
    }
    
    void setupRc(void){
    
      glClearColor(0.0f,0.0f,1.0f,0.0f);
    
    }
    
    void changeSize(GLsizei w,GLsizei h){
      if(h==0)
      h=1;
      glViewport(0,0,w,h);
      glMatrixMode(GL_PROJECTION);
      glLoadIdentity();
       if(w>=h){
         windowWidth=250.0f*w/h;
         windowHeight=250.f;
       }
       else{
         windowWidth=250.0f;
         windowHeight=250.f*h/w;
       } 
    
       glOrtho(0.0f,windowWidth,0.0f,windowHeight,1.0f,-1.0f);
    
    
       glMatrixMode(GL_MODELVIEW);
       glLoadIdentity();
    
    
    }
    
    
    
    
    void main(void){
    
       glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
    
       glutCreateWindow("Rectangle");
    
       glutReshapeFunc(changeSize);
       glutDisplayFunc(scene);
       setupRc();
    
       glutMainLoop();
    
    }
    

提交回复
热议问题