Sign in to follow this  
load_bitmap_file

Flipping textures horizontally

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
GroZZleR    820
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
Programmer16    2321

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
matches81    474
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
Programmer16    2321
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
Programmer16    2321
Quote:
Original post by load_bitmap_file
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]


Yeah, I thought about that after I posted (and I'm too busy working on my engine to edit it.)

Share this post


Link to post
Share on other sites
Syranide    375
Quicktip would be to just design your own quad/sprite-engine or download one, there are great tutorials on how to do them, and really it isn't hard, and it will give you more features than you could ever dream of. (and easily flipping them)

Ok, I agree it takes time as everything, but if you have time, it is really great experience and will allow you to add a lot of effects to your game. (e.g. such as flipping without any hacks)

Share this post


Link to post
Share on other sites
Programmer16    2321
Quote:
Original post by Syranide
Quicktip would be to just design your own quad/sprite-engine or download one, there are great tutorials on how to do them, and really it isn't hard, and it will give you more features than you could ever dream of. (and easily flipping them)

Ok, I agree it takes time as everything, but if you have time, it is really great experience and will allow you to add a lot of effects to your game. (e.g. such as flipping without any hacks)


While I agree with you, if you make you're own then you have to implement batching, texture sorting, etc. While with the ID3DXSprite interface you get all of that plus some.

But, I'm still going to make my own :D

Edit: And neither way would be considered a hack, his uses legitimate math and mine uses teture mirroring.

Edit 2: Also, if you're still interested, here is a list of the speed of DX function calls (thanks to good ol' Circlesoft) Here

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this