views:

1847

answers:

2

I have an OpenGL ES game that I am hacking together. One part of it involves looking at a large "map-like" area and then double-tapping on one part to "zoom into" it. How would you use OpenGL ES to provide this effect (given that it may need to zoom in on different parts of the map).

I've heard of glScale and glOrtho, but I'm unclear on how they actually work since the whole openGL world is very new to me.

A: 

Respectfully, the answer is to take a few days to learn the basics of OpenGL, and there are much better places for that on the net than here.

I tend to learn by trying to answer a few basic questions. This seems like a great place to get this specific question answered since it is, in all likelihood, 1-4 lines of code.
Mark S.
Fair enough, but the answer to your question is necessarily a longish tutorial on OpenGL transforms and suchlike. it's worth taking the time to get the fundamentals down; http://nehe.gamedev.net/ is a good place to start.
+2  A: 

The 2-D zooming you describe might be better achieved using Core Animation. NSView (and its NDA'd iPhone counterpart) provide implicit animation when you change their frame. All you'd need to do in this case would be to set the frame's origin.x and origin.y and size.width and size.height to such values to make the view larger than the screen. If you did this and wrapped it in the appropriate calls to start and commit an animation, you'd get a zooming animation for free. Core Animation uses OpenGL behind the scenes for its animations.

If, however, you feel that you have to do this in OpenGL, may I suggest a little writeup I did at http://www.sunsetlakesoftware.com/2008/08/05/lessons-molecules-opengl-es? I'm the author of Molecules, a free 3-D molecular visualizer for the iPhone, and I knew nothing about OpenGL ES before I started that project. 3 weeks later, it was in the App Store as it launched.

OpenGL calls are pretty simple, it's the math surrounding them that can give you headaches. Zooming in on objects is actually pretty simple, and can be done either by moving the camera or by actually physically scaling objects. For Molecules, I went the route of scaling the object using the glScalef(x,y,z) function, where x, y, and z are the scale factors you wish to apply to your model object. I do my scaling incrementally. That is, I don't reset the transformation matrix at the start of each rendered frame (using glLoadIdentity()), but just scale it a little bit based on user input. If the user moves their fingers apart by 5%, I increase the scale by 5%.

Again, I'd suggest Core Animation for the 2-D zooming you describe, but it isn't too hard to achieve the same results in OpenGL ES.

Brad Larson
I would have thought that scaling the molecules is actually a lot slower than moving the camera as scaling molecules means all the data has to be modified, but moving the camera is just changing one matrix that will be sent to the graphics CPU.
danio
@danio I don't think that's necessarily true. Scaling all objects would entail a one-time modification to the model-to-world transformation matrix applied to those objects. Transforming each object from model to world coordinates would still only be a single matrix multiplication.
Andrew Garrison