Sign in to follow this  
FarFromHomeFish

Render texture, not as bits but as a solid color

Recommended Posts

I have a simple sprite of an orc on a texture where magenta is transparent. When the orc is hit, I'd like to render this texture as a solid color while maintaining the transparent areas. Instead of a sprite, I'd like to render a blob of bright red with the same outline as the original sprite. What do I do to get this effect? Thanks!

Share this post


Link to post
Share on other sites
If you're using the fixed function pipeline you can use the texture stage states (DX8) or sampler states (DX9).

Set color to selectarg from diffuse (setting diffuse to the color) and alpha to selectarg from texture (or modulate from diffuse and texture to allow alpha blending effects).

Share this post


Link to post
Share on other sites
Sc4Freak: he's not using the alphachannel, just a colorkey. Switching to a .png or something that has an alpha channel would be a good idea, IMO, but if he's using shaders anyways, he could discard the pixel if it's magenta instead of using the alpha channel.

On the other hand, this is *For Beginners* so I doubt that he's using shaders. IMO the easiest way to do this is to make a second graphic which is just the red silhouette of the orc. The only downside to this is that you'd then have to maintain two images if you change just one.

@FarFromHomeFish: Can you tell us what you're using for graphics? DX? SDL? OpenGL? Something else?

Share this post


Link to post
Share on other sites
I'm using Direct3D from DX7. :) It's an older codebase that I haven't really updated in years. Whenever I start a new game, I import this DX7 class from years ago so I can have graphics on the screen from day one.

DX7 does have texture stage states, though. Thanks for the suggestion. It took me a while, but I got something that seems to work okay. Could you tell me if what I'm doing is sub-optimal?

Stage 0:
Texture: orc picture

Stage 1:
Texture: pure white hFFFFFF
Color op: D3DTOP_ADD
Color arg1: D3DTA_TEXTURE
Color arg2: D3DTA_CURRENT

Stage 2:
Texture: pure red hFF0000
Color op: D3DTOP_MODULATE
Color arg1: D3DTA_TEXTURE
Color arg2: D3DTA_CURRENT

I should note that I got this sequence through trial and error... I still don't exactly know what the TSS arguments ColorOp, ColorArg1, and ColorArg2 do. Can someone elucidate that for me? I wasn't able to find any really clear documentation on the internet.

Share this post


Link to post
Share on other sites
Actually that ought to work with one stage only (i had an error in the states i posted before):

Stage 0:
Texture: orc picture
Color op: D3DTOP_SELECTARG1
Color arg1: D3DTA_CURRENT (which is the same as D3DTA_DIFFUSE in stage 0)
Alpha op: D3DTOP_SELECTARG1
Alpha arg1: D3DTA_TEXTURE

Use the diffuse color of the vertex to do the coloring, enable alpha blending (and testing) and it ought to work. No need for stages 1 and 2.

Share this post


Link to post
Share on other sites
Endurion: That works perfectly. Thank you so much.

Obviously texture stages are really powerful. I can see so many different ways to use this: for example, I've always wanted to be able to slightly highlight objects that are being moused over.

But I still have only a very faint grasp of what each of the stages and values do to the textures. Can you point me at a online resource or a book that would list exactly what each value does so I can experiment with them.

Share this post


Link to post
Share on other sites
I have no concrete links but look for "fixed function pipeline" in the DirectX docs. I think there was a diagram that showed how the stages were connected.

Think of the stages as a mixer. For every stage you can use a texture and a diffuse color.

Now you can choose how the alpha and color component for this stage are put together with the color and alpha op commands. The result is passed on to the next stage (D3DTA_CURRENT) and can be blended with something else.

In the long run the fixed function will be replaced by shaders anyway but as long as Intel integrated cards are around the FFP is still useful.

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