Jump to content
  • Advertisement

Archived

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

Jower

Changing colors

This topic is 5391 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´m making a 2D-game in DX9 and I need to be able to change a color to another (like red to blue for example) for team colors. This should be really simple but I just CAN´T find a way to do it. Anyone got some ideas ?

Share this post


Link to post
Share on other sites
Advertisement
I'm no expert on the matter, but if your textures are pretty uniform, you can load them, reading them as RGB values, then do a conversion to HLS mode (hue, luminosity, saturation) and add a set value to the hue, convert back to RGB, and then use them. This would only have to be done once when the texture is loaded, so it can save you space and not bog down the system. This is also a good method to allow the user to customize colors :D

hope that helps,

*Cosmic*

p.s. anybody can feel free to shut me up if i'm totally wrong about this subject

[edited by - Cosmic One on February 27, 2004 1:45:32 PM]

Share this post


Link to post
Share on other sites
That sounds like it´d only change the color once (?). Kinda the whole point in changing it at runtime is to use one file for multiple colors.

Share this post


Link to post
Share on other sites
There are several ways of doing this, and I have fiddled with almost all of them in multiplayer games before.

1) Use palettes: This requires utilizing a particular graphics format that supports palettes. Change the palette according to which team a player is on. Pretty self-explanatory. Easiest solution.

2) Diffuse colors: Probably not recommended, as it will tint the whole image. However, if you draw something separate (like a flag or something) this would be work.

3) Multiple textures (resource intensive): Have one major texture for each object (space ship, army, whatever) and leave transparent (alpha) sections where the color would be (and use semi-transparents sections in parts where you want shading). Before drawing the texture, draw a texture (or color-filed box) that is the correct color according to team and then draw the final object on top of it. The object will appear colored by the correct team.

4) Texel re-colorization (speed intensive): This actually (IMO) generates the best-looking results outside of palettes, but it is not recommended if you have say 100 or more objects needing re-colorization on the screen. It is speed intensive, as it requires locking the buffers and manually changing the color of particular texels. The reason I think this is best looking is because you can generate new colors that have bright and shadowed spots in precisely the same places as your original image. Basically, it involves taking a set range of colors in your original texture (let's say, all colors close to "absolute red" (255,0,0)) and toggling those colors by a mathematical formula. So, to make your item appear to be blue, you'd swap the red and blue channel for all red colors. To get purple, you'd mix in the equivalent amount of blue... etc. As a warning, this requires a fair amount of coding and testing...


[edited by - The Frugal Gourmet on February 27, 2004 2:06:59 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by The Frugal Gourmet
1) Use palettes: This requires utilizing a particular graphics format that supports palettes. Change the palette according to which team a player is on. Pretty self-explanatory. Easiest solution.



There are almost no cards on the market which support paletted textures. It *would* be an easy way, but not viable.

Here is another (relatively) simple method. Set the part that you want to be coloured by the team to be grayscale. Set the alpha of this area to be an arbitrary value (say 50). Draw your first pass with ALPHATEST >50 which will render the sprite except in the tinted area. Then set AlphaTest == 50, set the Diffuse colour to be the colour you want to tint with and draw that modulating the texture.

OR

Modifying #3 above, rather than having a colour fill, have the Team Colour area done in grayscale and draw that modulated by the Diffuse.

There are a lot of variations. I''m assuming you''re doing something with 2D sprites rather than meshes since you said it''s 2D, though these will work with meshes too.


Stay Casual,

Ken
Drunken Hyena

Share this post


Link to post
Share on other sites
No palettes? Oh well, I thought it seemed too easy...

In any case, your idea sounds brilliant. I love it.

BTW, Jower, to activate alpha testing...


m_pd3dDevice->SetRenderState( D3DRS_ALPHATESTENABLE, TRUE );
m_pd3dDevice->SetRenderState( D3DRS_ALPHAREF, 0x01 ); // whatever value you want here...

m_pd3dDevice->SetRenderState( D3DRS_ALPHAFUNC, D3DCMP_GREATER );

Share this post


Link to post
Share on other sites
It acctually seems to work !
Amazing
hehe
Though putting an alpha value on the teamcolors make them come out transparent...
Not really ideal

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!