OpenGL - maintaining aspect ratio when resizing a window

I draw a polygon in a square window. When I resize the window, for example, when fully shielded, the aspect ratio is violated. From the link, I found one way to keep the aspect ratio. Here is the code:

    void reshape (int width, int height) {
    float cx, halfWidth = width*0.5f;
    float aspect = (float)width/(float)height; 
    glViewport (0, 0, (GLsizei) width, (GLsizei) height);
    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 
    glFrustum(cx-halfWidth*aspect, cx+halfWidth*aspect, bottom, top, zNear, zFar);
    glMatrixMode (GL_MODELVIEW);
}

Here cx is the center of the eye space of the zNear plane in X. I ask if someone can explain how I could calculate this. I believe this should be the middle of the first two first arguments to glFrustum (). I'm right? Any help would be greatly appreciated.

+3
source share
2 answers

, . . 9.085. glFrustum(), gluPerspective()? FAQ OpenGL , . :

fov*0.5 = arctan ((top-bottom)*0.5 / near)
top = tan(fov*0.5) * near
bottom = -top
left = aspect * bottom
right = aspect * top

. .

+6

- X . (, ), . cx .

glFrustrum, gluPerspective, .

+1

All Articles