Sign in to follow this  
DrunkenBrit

Resizing window

Recommended Posts

Hey, im having trouble trying to get a simple quad to resize when i resize the window, ive got it working but it works when afaik it shouldn't work... glViewport(0, 0, width, height); glMatrixMode( GL_PROJECTION ); glLoadIdentity(); gluOrtho2D( 0, width, 0, height ); glMatrixMode( GL_MODELVIEW ); glLoadIdentity(); The code above doesnt work... BUT this does... glViewport(0, 0, width, height); gluOrtho2D( 0, width, 0, height ); glMatrixMode( GL_MODELVIEW ); glLoadIdentity(); Im sure im ment to select the GL_PROJECTION matrix stack before calling gluOrtho2D? Explanations please? Thanks:)

Share this post


Link to post
Share on other sites
AFAIK gluOrtho2D invokes directly glOrtho, and glOrtho assembles a projection matrix and multiplies it with the top of the _current_ matrix stack. So yes, you have to activate GL_PROJECTION before calling gluOrtho2D to use it correctly.

OpenGL uses the projection and viewport roughly this way: A length in world space is normalized by dividing by the projection plane's size. That normalized space is then scaled to the viewport size for actually rendering pixels. Obviously, if both sizes are altered so that the quotient isn't changed, then the world length is rendered onto the same screen length. E.g. for a viewport width of p pixels and a projection plane width of w, a horizontal world space length l gets
l' := l / w * p

So, your 1st code snippet works well, although not in the sense you've expected! If you want the behaviour of fitting, you have to adapt the viewport to the window size but let the projection fixed. Then w remains the same but p increases (decreases) letting l' increase (decrease) proportionally.


EDIT: One normally set the viewport and orthographic projection you did for getting a pixelwise rendering. That is, one length unit in world is mapped to one pixel on screen. This obviously _requires_ the behaviour that resizing the window doesn't change the quad's rendered size.

Share this post


Link to post
Share on other sites
It will work if yo supply constant values

gluOrtho2D( 0, 500, 0, 500 );

What explanation? It's just math. Look at the red book or some online giude how vertices are transformed.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this