Archived

This topic is now archived and is closed to further replies.

Eddie Zehoo

Multiple sprites in a single texture

Recommended Posts

Eddie Zehoo    122
Hi, I''m trying to do 2D in Direct3D. I have multiple sprites (of animation) that are combined into one single 256x256 texture. For each sprite, I have a RECT , which is basically the pixel coordinates of the sprite WITHIN this texture. I know texture coordinates range from 0 to 1... So how do I convert these exact pixel coordinates into u and v (texture coordinates?) ... Could I do the following below? Is there any better and faster way ? u = MySprite.x / 256; v = MySprite.y / 256; Thanks! Regards, Eddie 00000100-00100011-10000000

Share this post


Link to post
Share on other sites
Fidelio66    164
I''d suggest using ID3DXSprite (Sprite in managed directx). It allows you to specify the position in screen coordinates and the source rect, eg (0,0,32,32) and it''ll draw a 32x32 sprite on screen.

Share this post


Link to post
Share on other sites
Teric    204
Yes, the MySprite.x / 256 idea will give you the proper percentage in the texture. Just make sure you''re using floats (or even doubles for better precision) for your uv coordinates.

Suggestion: record the uv coordinates of each sprite on your texture at load time, and record them in variables. That way, you''re not doing MySprite.x / 256 every time you draw the frame.

Share this post


Link to post
Share on other sites
Eddie Zehoo    122
Thanks for the suggestion guys
I''d most likely stick with the texture.x\256 method like Teric said, but isn''t there really a better way, like maybe set the texture addressing mode to something else other than (0.0 -1.0) ?

Thanks again anyway ! Appreciate the help
Regards,
Eddie





00000100-00100011-10000000

Share this post


Link to post
Share on other sites
Krylloan    142
Just a side note:

If your sprite is to be copied at 1:1 scale your method is fine. However, if you are stretching/compressing/rotating etc your sprite, or even not aligning the edges of the pixels (ie drawing the sprite 20.5 pixels out from X) you might need to be careful that any filtering algorithms don''t start mixing bits of your sprites together.

Linear / Bilinear / Trilinear / Anisotropic filtering occasionally use pixels outside the specified rectangle (especially if you are using mip-mapping).

I used a single texture containing all the glyphs in a font. The text size is scalable. I found that when I drew the characters at unusual positions and sizes that bits of the glyphs next to the one I was drawing sometimes got included in the on-screen text.

If you have this problem but aren''t using mip-mapping, using a border of 1 pixel around your sprites is enough to ensure this doesn''t happen. However, if you are using mip-mapping you can''t get away from this without somehow specifying a maximum depth mipmap that can be used (I don''t know if this is possible).

Share this post


Link to post
Share on other sites
Krylloan    142
I know OpenGL has just introduced some sort of fixed-width number types that can be used. Maybe Direcked3D has some sort of fixed-width number type you can use also? (Fixed width should be faster than floating point if you really need the speed)

Share this post


Link to post
Share on other sites