Changing projection in OpenGL

前端 未结 1 1840
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-28 12:14

I\'m having trouble with resizing a GLUT window. I want to print a rectangle but when i use the resize function i cant seem to draw and shape in the window. I have the following

1条回答
  •  清酒与你
    2021-01-28 12:46

    Your problem lies in the use of a fixed value for the orthographic matrix:

    glOrtho(0.0,500,0.0,500,0.0,1.0);

    What this says is, however big my window is, make vertex position of 0 map to the bottom of the window, and 500 map to the top of the window.

    So if you have a 500 pixel high window, and you draw a square that covers 100 units, this will map to 100 pixels in the window.

    However if you stretch the window to be 1000 pixels high, now you have a region from 0 to 500 mapping to a window of height 1000 pixels. So your same square that used to cover 100 pixels in height now covers 200 pixels in height.

    If you want it to always be the same size when you resize, you need to update the orthographic matrix to map a larger area to the new larger window. So if you change your orthographic matrix to map region 0 to windowHeight to the window, a 100 unit square will always fill the exact same amount of height.

    glOrtho( 0.0, //left
             500, //right
             0.0, //bottom
             height_in_pixels, //top
             0.0,   //near 
             1.0);  //far
    

    0 讨论(0)
提交回复
热议问题