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

前端 未结 5 1138
眼角桃花
眼角桃花 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:28

    You definitely don't want to make the size of your objects explicitly dependent on the window size.

    As already suggested by genpfault, adjust your projection matrix whenever the window size changes.

    Things to do on window resize:

    1. Adjust viewport

      glViewport(0, 0, width, height)
      
    2. Adjust scissor rectangle (only if you have GL_SCISSOR_TEST enabled)

      glScissor(0, 0, width, height)
      
    3. Adjust projection matrix

      In case of legacy (fixed function pipeline) OpenGL, you can do it the following ways:

      glFrustum(left * ratio, right * ratio, bottom, top, nearClip,farClip)
      

      or

      glOrtho(left * ratio, right * ratio, bottom, top, nearClip,farClip)
      

      or

      gluOrtho2D(left * ratio, right * ratio, bottom, top)
      

      (assuming that left, right, bottom and top are all equal and ratio=width/height)

提交回复
热议问题