I have a 3d scene drawn by OpenGL to a resizeable window. Now, when the window gets resized, I do not want to scale the viewport, rather I want to keep the scene at a fixed size and show a larger portion of it (or crop the scene image). This is my current code:
GLfloat ratio;
// Protect against a divide by zero
if ( height == 0 )
height = 1;
ratio = ( GLfloat )width / ( GLfloat )height;
// Setup our viewport.
glViewport( 0, 0, ( GLint )width, ( GLint )height );
// change to the projection matrix and set our viewing volume.
glMatrixMode( GL_PROJECTION );
glLoadIdentity( );
gluPerspective( 60.0f, ratio, 0.1f, 1000.0f );
// Switch back to the modelview
glMatrixMode( GL_MODELVIEW );
If I keep the ratio fixed, then the scene image simply gets scaled, but I want to keep it at fixed size and simply show a wider view. Any ideas on this?
Adjust the fov parameters. Technically what you want to do is easier if done using glFrustum instead of gluPerspective.
Take note that this code belongs into the display function. Any tutorial that sets viewport and projection in a window reshape handler is very bad style; don’t follow it.