Jump to content
  • Advertisement
Sign in to follow this  
load_bitmap_file

Flipping textures horizontally

This topic is 4794 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Every 2d sidescroller game I've seen only has image files for when the sprite is facing one way, usually right. This must mean that they generate a way to flip the sprites horizontally for when the player is facing left. So, does anyone know how to do this in DirectX, specifically for the ID3DXSprite interface? I browsed around google and found two possible answers, that of changing the (u, v) texture coordinates or copying the texture to another texture byte by byte in reverse. As far as I know, you cannot access the internal (u, v) texture coordinate for ID3DXSprite so I can't toy with their values. (Someone please correct me if I'm wrong) The other method I would also like to avoid since it takes up space as another texture. At that point I might as well just save separate left/right images. So, does anyone know how to flip textures in DirectX?

Share this post


Link to post
Share on other sites
Advertisement
Could you not just flip the source or destination rect around?

So if your sprite is say 32,32 instead of:
Top: 0
Left: 0
Bottom: 32
Right: 32

Use:
Top: 0
Left: 32
Bottom: 32
Right: 0

I don't see why that wouldn't work.

Share this post


Link to post
Share on other sites
Doesn't seem to work. As far as I can tell, it moves the sprite (non flipped) over to the left a bit and nothing else. How can the right side of a rectangle be farther to the left than the left side of the rectangle is anyway? [smile]

Any ideas?

Share this post


Link to post
Share on other sites

g_pDevice->Clear(0, 0, D3DCLEAR_TARGET, 0xffaaaaaa, 1.0f, 0);
g_pDevice->BeginScene();
g_pSprite->Begin(0);

g_pDevice->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_MIRROR);
RECT SrcRect = {32, 0, 64, 32};
g_pSprite->Draw(g_pBaseImage, &SrcRect, 0, 0, 0xffffffff);
g_pSprite->End();

g_pDevice->EndScene();
g_pDevice->Present(0, 0, 0, 0);




I don't know if the sampler state will be reset by g_pSprite->End() or if it is bad to set states after the sprite or whatnot. But I tested that and it works.

Share this post


Link to post
Share on other sites
Quote:
Original post by Programmer16
*** Source Snippet Removed ***

I don't know if the sampler state will be reset by g_pSprite->End() or if it is bad to set states after the sprite or whatnot. But I tested that and it works.


Thanks Programmer16 [smile]. Is SetSamplerState a "slow" function? As in would it be okay to do something like this:

SetSamplerState(flip textures);
DrawSpriteThatNeedsToBeFlipped();
SetSamplerState(do not flip textures);


for every Sprite that needs to be flipped?

EDIT: It doesn't seem to be working..

EDIT2: The documentation says about D3DTADDRESS_MIRROR:

Quote:
For u values between 0 and 1, for example, the texture is addressed normally; between 1 and 2, the texture is flipped (mirrored); between 2 and 3, the texture is normal again; and so on.


I would think that ID3DXSprite only uses between 0 and 1 since it doesn't have any options for tiling. How did you get it to work?

Share this post


Link to post
Share on other sites
don´t know exactly how fast or slow changing a sampler state is, but you should minimize them if you´re going to have many mirrored sprites... just looked into the DX docs and SetSamplerState was not listed there at "Accurately profiling Direct3D API calls"... so no idea still how long a call of SetSamplerState might take.

Share this post


Link to post
Share on other sites
I used 0,32,64,32 as the source rectangle (which would set the value 1-2 not 0-1.) As for it being slow, I don't know (I assume its probably not very fast.)

Share this post


Link to post
Share on other sites
Yes! I solved my own problem! Behold:


D3DXVECTOR3 center(x, y, 0.0f);
D3DXVECTOR3 translation(textureWidth, 0.0f, 0.0f);
D3DXMATRIX rotationMatrix, finalMatrix;
D3DXMatrixRotationY(&rotationMatrix, D3DX_PI);
D3DXQUATERNION rotationQuaternion;
D3DXQuaternionRotationMatrix(&rotationQuaternion, &rotationMatrix);
D3DXMatrixTransformation(&finalMatrix, NULL, NULL, NULL, &center, &rotationQuaternion, &translation);
m_d3dSprite->SetTransform(&finalMatrix);




What it does: rotates the texture along the y axis pi radians so it appears to be reversed horizontally.

Thanks to those who gave responses!

EDIT: Fixed it.

Share this post


Link to post
Share on other sites
Quote:
Original post by Programmer16
Ok, you do all of that when it can be easily done with 3 lines? But congrats on finding a solution!


Ah, but less lines != "better". All the math going on here ought to be significantly faster than setting the sampler state for each and every Sprite that needs to be flipped, thanks to uber assembly optimiziation. Besides, I couldn't get the 3 line way to work for whatever reason. [wink]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!