Skip to main content
gamedev.net
🔒 Locked

Changing colors

Started by Jower 33 replies 1.3k views
Original Post
Jower
Jower
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 ?
Cosmic One
Cosmic One
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]
Jower
Jower
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.
The Frugal Gourmet
The Frugal Gourmet
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]
Co-creator of Star Bandits -- a graphical Science Fiction multiplayer online game, in the style of "Trade Wars'.
Jower
Jower
What limitations are there with palettes ?
Got any links to some palette tutorials ?
Wicked Ewok
Wicked Ewok
another option might be - pixel shader!
-=~''^''~=-.,Wicked,.-=~''^''~=-
DrunkenHyena
DrunkenHyena
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
Stay Casual,KenDrunken Hyena</FON
Jower
Jower
What´s this "alphatest" ?
How do I use it ?
Sounds nifty
The Frugal Gourmet
The Frugal Gourmet
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 );
Co-creator of Star Bandits -- a graphical Science Fiction multiplayer online game, in the style of "Trade Wars'.
Jower
Jower
It acctually seems to work !
Amazing
hehe
Though putting an alpha value on the teamcolors make them come out transparent...
Not really ideal
Jower
Jower
Anyone got a solution for this ?
Jower
Jower
I´m trying, I''m calling
SetRenderState(D3DRS_ALPHABLENDENABLE, false) 

15 times or so and it´s still transparent
lol
Anything else I need to do to disable it ?
CGameProgrammer
CGameProgrammer
Make sure the alpha value is correct for the function you''ve specified. In Frugal Gourmet''s code, only things with an alpha value greater than 1 will be rendered, and things with an alpha of 0 or 1 will be invisible.

That idea of Hyena''s is a very neat and fast solution and there''s no reason why it shouldn''t work.

~CGameProgrammer( );

Screenshots of your games or desktop captures -- Upload up to four 1600x1200 screenshots of your projects, registration optional. View all existing ones in the archives..
~CGameProgrammer( ); Developer Image Exchange -- New Features: Upload screenshots of your games (size is unlimited) and upload the game itself (up to 10MB). Free. No registration needed.
Jower
Jower
Everything renders correctly except for the team-colored areas getting transparent
And alphablending dosen´t wanna disable
CGameProgrammer
CGameProgrammer
OK, tell us what exactly you''re doing. You are drawing the models twice, right? You need to draw them once with alpha-testing set so the team-colored areas are transparent, and then you change the alpha-testing and draw the model a second time. The second time, the team-colored areas should be visible and everything else should be transparent.

~CGameProgrammer( );

Screenshots of your games or desktop captures -- Upload up to four 1600x1200 screenshots of your projects, registration optional. View all existing ones in the archives..
~CGameProgrammer( ); Developer Image Exchange -- New Features: Upload screenshots of your games (size is unlimited) and upload the game itself (up to 10MB). Free. No registration needed.
Jower
Jower
That´s exactly what I´m doing
But the second render keeps it´s alphavalue
CGameProgrammer
CGameProgrammer
We need more information. What do you set ALPHAREF and ALPHAFUNC to for the first render? What do you set ALPHAFUNC to for the second render? And what alpha values do the colored and noncolored parts of the textures use?

~CGameProgrammer( );

Screenshots of your games or desktop captures -- Upload up to four 1600x1200 screenshots of your projects, registration optional. View all existing ones in the archives..

[edited by - CGameProgrammer on February 28, 2004 5:20:31 PM]
~CGameProgrammer( ); Developer Image Exchange -- New Features: Upload screenshots of your games (size is unlimited) and upload the game itself (up to 10MB). Free. No registration needed.
Jower
Jower
I just do

SetRenderState(D3DRS_ALPHATESTENABLE, true)

SetRenderState(D3DRS_ALPHAFUNC, D3DCMP_NOTEQUAL)
//Render once


SetRenderState(D3DRS_ALPHAFUNC, D3DCMP_EQUAL)
//Render again with color


SetRenderState(D3DRS_ALPHATESTENABLE, false)


And it comes out with the right areas colored in the right colors but with transparency enabled

[edited by - Jower on February 28, 2004 6:34:57 PM]
CGameProgrammer
CGameProgrammer
I still don''t understand what you mean. Can you take a screenshot?

~CGameProgrammer( );

Screenshots of your games or desktop captures -- Upload up to four 1600x1200 screenshots of your projects, registration optional. View all existing ones in the archives..
~CGameProgrammer( ); Developer Image Exchange -- New Features: Upload screenshots of your games (size is unlimited) and upload the game itself (up to 10MB). Free. No registration needed.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.