Sign in to follow this  

How to lighten a sprite ?

This topic is 3491 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

I am using sprites to do all my drawing and i am looking for a way to programmatically lighten a sprite. Currently, I only know how to do alphaBlend and image darkening through the color attribute of this method (Managed DirectX 9) Sprite.Draw(srcTexture,srcRectangle,scaling,rotationCenter,rotation,translation,color) I have thought of a way to achieve it, but I'd like to know if theres a better way to do it, because what i have in mind seems overly complicated for what I'm trying to do. What i have in mind is to draw a white version of my sprite, and then alpha blend my regular sprite over it. The main problem with this solution is that i d need to load twice the amount of textures (for each frame, one normal image and one in white) What I'm trying to do is make my sprites (ennemies and other stuff) blink a bit when they get hit. Up until now, I've done it by darkening some colors of the images, but i think it could be prettier by lightening the image instead. Any suggestions?

Share this post


Link to post
Share on other sites
Rambo,

Have you tried drawing the Sprite twice using Additive Blending? I'm not familiar with Managed DirectX, only DirectX and XNA. But, drawing the sprite twice with additive blending enabled should make it significantly brighter, while still maintaining some kind of contrast ratio.

Let me know how it works out!

Share this post


Link to post
Share on other sites
The docs for BeginSprite tell you how D3DX sets up the device for sprite rendering. You can modify this however you want. The default color operation is to MODULATE (ie: multiply) texture color and vertex color. (note, when doing color math the 0..255 range is treated as 0..1)

To flash a sprite to a pure color, we'll tell D3D to use our color directly, rather than mixing it with the texture color.

Draw all the sprites you want normal first, the call Flush on the sprite interface.
Set:
pDev->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG2);
Then:
Draw your sprites, then Flush. All those sprites should come out whatever color you wanted.


To just add to the existing texture color:
Draw all the sprites you want normal first, the call Flush on the sprite interface.
Set:
pDev->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_ADD);
Then:
Draw your sprites, then Flush. All those sprites should come out brightened.


If you still want black blacks, perhaps you just want to multiply by more than 1, rather than add.
Set:
pDev->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE2X);
Then:
Draw your sprites.
If your vertex color was 128,128,128, the sprite would be unchanged. If your vertex color is 255,255,255, everything will be twice as bright. Select a color just above mid-grey to give a more subtle brightening.


Because flushing often is slow, if you are flashing lots of items at once (like hundreds), you should draw all regular sprites first, then all flashing ones. With techniques like modulate2X you can draw regular and bright at once just by changing your color and skip the flushing altogether. You only need to flush when you're changing render states, to ensure all the previous sprites are actually drawn, not just queued up, before you tell D3D a different way to draw.

Share this post


Link to post
Share on other sites

This topic is 3491 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.

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