View Full Version : Zooming
Hello,
Whats the best way to zoom in and out.
Thanks for helping
baldurk
02-09-2003, 01:43 AM
there is no way to really 'zoom' in OpenGL. The camera stays at 0, 0, 0 looking along the z axis. Everything else moves around it. The best way to zoom out object a is to translate it further away, eg.
glTranslatef(APosX, APosY, APosZ);
glTranslatef(0.0f, 0.0f, -Zoom);
DrawObjectA();
then as zoom increases, the object gets further away
watch out though, anything drawn after that will be 'zoomed' as well.
Actually u can zoom with OpenGL by altering ur viewing matrix using the gluPerspective(..) function.
The actuall function parameter is as follows:
void gluPerspective(
float angle, // the viewable anlge infront of the camera/eye
float aspect, // typically width/height of window or screen
float near, // min distance from camera that is renderable
float far, // max distance from camera that is renderable
)
To do zooming the first parameter is the one u wnat to alter. The smaller the angle u specify the greater the zoom. Try experimenting and u'll see this in action.
The advantage of using this technique over the one above is that u won't get into a situation where u zoom past an object because u aren't actually repositioning any objects.
The disadvantage is that everytime u want to zoom or animate zooming and zooming out u have to recreate ur perspective viewing transformation. Well this isn't too bad.
donBerto
02-09-2003, 11:26 AM
the non-glu way is to use glOrtho and glMatrixMode
here are references to both:
http://www.cevis.uni-bremen.de/~uwe/opengl...gl/glOrtho.html (http://www.cevis.uni-bremen.de/~uwe/opengl/glOrtho.html)
http://www.cevis.uni-bremen.de/~uwe/opengl...MatrixMode.html (http://www.cevis.uni-bremen.de/~uwe/opengl/glMatrixMode.html)
example:
void window_scene2D()
{
glViewport(left, top, width, height);
glOrtho(left, right, bottom, top, near, far);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
}
/////////////////////////////////////////
void window_scene3D()
{
glViewport(left, top, width, height);
glMatrixMode(GL_MODELVIEW);
glOrtho(left, right, bottom, top, near, far);
glLoadIdentity();
}
remember, in glMatrixMode, you can only get the effect of zooming in or out with GL_MODELVIEW. only use GL_PROJECTION to do non-zooming effects like a menu system or the like.
If I offended you with any of this, please accept my apologies. I've seen way too many questions as to why people can't zoom in and out and when they show their code, they're using GL_PROJECTION.
cheers.
Morgoth
02-10-2003, 05:16 AM
The disadvantage is that everytime u want to zoom or animate zooming and zooming out u have to recreate ur perspective viewing transformation. Well this isn't too bad.
Correct me if i'm wrong, but performance-wise it's usually much heavier to recalculate the view (projection e.t.c) matrices than perform translations in model-space. I'm not quite sure about the actual performance impact, but, well, fps's don't do it that way :-)
donBerto
02-10-2003, 07:48 AM
interesting point.
the fewer the number of objects in space, the easier it is to translate the objects instead of recalculating the perspective matrix. the greater the number of objects, the more likely that it will be eaiser to recalc the perspective matrix than it is to translate all the objects.
of course, you can come up with algorithms the decide which of the two OR maybe even make it so that if you just translate, to only translate in the view frustum.
cheers.
vBulletin, Copyright ©2000-2010, Jelsoft Enterprises Ltd.