Colored light to recolor a texture?

Started by
1 comment, last by flaXen 22 years, 5 months ago
I need the ability to recolor a sprite in my 2D engine (using D3D). I can tint it (somewhat) by changing the diffuse color of the vertices of the polygons I''m drawing, but what I need is the ability specify a color and effectively convert the texture to greyscale and then recolor it to the specified color. It''s for highlighting things... I figured a colored light source may do the trick, but I''m having a hard time even getting one to work. I want to use a directional light source since it won''t have any falloff or attenuation like point and spotlights do. I tried setting one up but it doesn''t affect the sprite at all. It shouldn''t be too tricky to get a light to affect a sprite in a 2D engine... I''d hope... I used this code after creating the light to enable it: g_pd3dDevice->SetLight(0, &light); g_pd3dDevice->LightEnable(0, TRUE); g_pd3dDevice->SetRenderState(D3DRS_LIGHTING, TRUE); I tried searching the forums for colored lighting articles, but it returned no results after a long delay (broken search engine?). Any help would be great! -- Dan (flaXen)
Advertisement
Instead of a light, which affects everything in the scene, consider using a material for the object you want to tint:
IDirect3DDevice8::SetMaterial
IDirect3DDevice8::SetRenderState (D3DRS_DIFFUSEMATERIALSOURCE, D3DMCS_MATERIAL)

You can also use the diffuse in the vertices, if you want to specify it per vertex:
IDirect3DDevice8::SetRenderState (D3DRS_DIFFUSEMATERIALSOURCE, D3DMCS_COLOR1)


Then blend the diffuse value with the texture value. Something like (maybe you want a different op than add):

d3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
d3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
d3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_ADD);

Actually, i just tried it on my computer, and add really sucks. I get better results with D3DTOP_MULTIPLYADD (it tends to desaturate and brighten if I use white light and white material, or tint with a colored light ot colored material).

This topic is closed to new replies.

Advertisement