English 中文(简体)
OpenGL ES - how to keep some object at a fixed size?
原标题:

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!

最佳回答

Found one solution in this thread: 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.

问题回答

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.

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.





相关问题
Code sign Error

I have created a new iPhone application.I have two mach machines. I have created the certificate for running application in iPhone in one mac. Can I use the other mac for running the application in ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

将音频Clips从Peter改为服务器

我不禁要问,那里是否有任何实例表明从Peit向服务器发送音响。 I m不关心电话或SIP风格的解决办法,只是一个简单的袖珍流程......

• 如何将搜查线重新定位?

我正试图把图像放在搜索条左边。 但是,问题始于这里,搜索条线不能重新布署。

热门标签