views:

484

answers:

3

I'm working on a little game in OpenGL ES.

In the background, there is a world/map. The map is just a large texture. Zoom/pinch/pan is used to move around. And I'm using glOrthof (left, right, bottom, top, zNear, zFar) to implement the zoom/pinch. When I zoom in, the sprites on top of the map is also zoomed in. But I would like to have some sprites stay at a fixed size.

I could probably calculate a scale factor, depending on the parameters to glOrthof, but there must be a more natural and straightforward way of doing that, instead of scaling the sprites down when I zoom in.

If I add some text or some GUI elements on top of the map, they should definately have a fixed size.

Is there a solution to do this, or do I have to leave fixed values in glOrthof and implement zoom/pinch in another way?

EDIT: To be more clear: I want sprites that zoom in/out along with the map, but stay at the same size. I have some elements that are like the pins on the iPhone's map application. When you zoom, the pins stay the same size, but move around on the screen to stay on the same spot on the map. That is mainly what I want a solution for. Solutions for this already came below, thanks!

+1  A: 

First call glOrthof with the settings you have, then draw the things that scale. Then make another call to glOrthof with different settings (after glLoadIdentity probably), and then draw the things that should not be scaled.

Thomas
Did a quick test with this.The sprites stay the same size/position when i zoom on the map.This solution will work nicely for elements that should stay on a fixed position on the iPhone's screen, like UI elements and text.But I would like to have some sprites to stay on the same position on the map. Kind of like the pins on the iPhone's map application. When you zoom/pan on the map, the pins stay on the same position relative to the map, but the size of the pin stay the same.Is there a quick solution for this, also? :-)
OMH
Push a scale matrix onto the stack (with `glScalef` or some such) to reverse the scaling of the world, before you draw your sprites.
Thomas
Interesting, but I don't quite understand. Could you elaborate a little bit, please?
OMH
Suppose your world is zoomed out by a factor of 2. Then just do something like `glPushMatrix(); glScalef(2, 2, 2); drawSprite(); glPopMatrix();` for each sprite.
Thomas
A: 

you can use something like this to draw fixed size elements at a given 3D position, keeping the current projection settings :

// go to correct coordinates
double v[3] = { x , y , z };
glRasterPos3dv( v );
glBitmap( 0 , 0 , 0 , 0 , -center_pix_x , -center_pix_y , NULL );

// and draw pixels
glPixelStorei( GL_PACK_LSB_FIRST , true );
glPixelStorei( GL_PACK_ALIGNMENT , 1 );
glDrawPixels( img_width , img_height , GL_RGBA , GL_UNSIGNED_BYTE , img_data_ptr );

center_pix are the coordinates of the reference point in the sprite that will match the 3D point.

fa.
A: 

Found one solution in this thread: http://stackoverflow.com/questions/850988/drawing-point-like-shapes-in-opengl-indifferent-to-zoom

Point sprites... Apple's GLPaint example also uses this. Quite simple to use. Uses the current texture.

glEnable(GL_POINT_SPRITE_OES);
glTexEnvf(GL_POINT_SPRITE_OES, GL_COORD_REPLACE_OES, GL_TRUE);
glPointSize(40.0f);
glVertexPointer(2, GL_FLOAT, 0, vertexBuffer);
glDrawArrays(GL_POINTS, 0, 4);

These will move when the map moves, but does not change the size.

Edit: A small tip: Remember that the point coordinate is the middle of the texture, not a corner or anything. I struggled a bit with my sprites apparently "moving", because I used only the 35x35 upper left pixels in a 64x64 texture. Move your graphics to the middle of the texture and you'll be fine.

OMH