views:

885

answers:

4

Im using glScalef() for zooming in my program, now when i revert it back to the original size with code:

glScalef(1.0f/ZOOM, 1.0f/ZOOM, 1);

It doesnt work very well in high zooms... (maybe because of the lost decimals?)

How i can revert it back perfectly to the original size without any calculations like above?

+4  A: 

You could either do it like this:

ZOOMSQRT = sqrt(ZOOM);
glScalef(1.0f/ZOOMSQRT, 1.0f/ZOOMSQRT, 1);
glScalef(1.0f/ZOOMSQRT, 1.0f/ZOOMSQRT, 1);

or use glPushMatrix/glPopMatrix to restore the original matrix before zooming:

glPushMatrix();
glScalef(ZOOM, ZOOM, 1);
[do whatever you like here...]
glPopMatrix(); // now it's at normal scale again
[do other things here...]
schnaader
thanks! i totally forgot about glPushMatrix() :$
You shouldn't - it's very important :) But I guess this won't happen again ;)
schnaader
A: 

Store the previous size somewhere, say in a stack, and use that.

(animating the transition back to a previous zoom, pan and rotate setting is quite fun rather than zooming)

Pete Kirkham
+4  A: 

You can simply save your projection matrix. glPushMatrix(), glScalef(), ..., glPopMatrix()

Cody Brocious
+2  A: 

Use glPushMatrix() before your first glScalef() to push the current model matrix onto the stack, and then glPopMatrix() when you want to get back the original scale, that way there won't be any rounding errors as there are no calculations being done.

Ed Woodcock