Thread: Using texture stage states for shadows

  1. #1
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607

    Using texture stage states for shadows

    I'm currently working on a tile engine and I want to create shadows w/o a lot of runtime overhead. I was wondering if I could use the Direct3D->SetTextureStageState() function to do this.

    I'm using Direct3D to render the tiles as 2 triangles - a triangle strip and then texturing it with the correct texture. All other tiles are rendered as non-iso flat quads - again 2 triangles being textured.

    If I have the first texture and the second texture, how can I overlay the first one onto the second, w/o obliterating the base tile? My idea is to overlay the silhouette of the object tile ( a tree in this case) over the base iso tile in such a way that when the colors are blended, the base tile shows a darkened silhouette of the tree tile. Hard to explain but I'm basicaly re-texturing the base tile with the shadow using texture stages. However it does not seem to be working at all. Either the first texture stage obliterates the second or vice versa.

    Anyone have any ideas on how I might do this? Perhaps I could use a lightmap of the tree and texture that onto the base tile?

  2. #2
    Registered User Coder's Avatar
    Join Date
    Aug 2001
    Location
    Cairo, Egypt
    Posts
    128
    Well, try this ( sorry in advance if it doesn't work ) :

    Assuming :
    - Your tree texture has a diffuse color component and is color keyed ( using an alpha texture, where useless/transparent pixels have an alpha of 0 ):
    - You're using D3DBLEND_SRCALPHA for D3DRS_SRCBLEND, and D3DBLEND_INVSRCALPHA for D3DRS_DESTBLEND

    - The diffuse color component is set to the degree of blackness you want ( i.e. how dark the shadow is going to look like )

    Code:
    SetTextureStageState( Stage,D3DTSS_COLORARG1, D3DTA_DIFFUSE )
    SetTextureStageState( Stage,D3DTSS_COLOROP,D3DTOP_MODULATE );
    SetTextureStageState( Stage,D3DTSS_COLORARG2,D3DTA_TEXTURE );
    
    SetTextureStageState( Stage,D3DTSS_ALPHAARG1,D3DTA_TEXTURE );
    SetTextureStageState( Stage,D3DTSS_ALPHAOP,D3DTOP_MODULATE );
    SetTextureStageState( Stage,D3DTSS_ALPHAARG2,D3DTA_DIFFUSE );
    This tells Direct3D to :
    - Modulate ( multiply ) the diffuse component ( degree of blackness ) by the texture color component ( thus resulting in the shadow )
    - Modulate the alpha value in the texture & the diffuse component.
    Thus, if a transparent pixel ( on the texture ) is being processed, the result is 0. i.e. When alpha blending, the original pixel in the back buffer remains as is.
    If, however, a non-transparent ( a data ) pixel is being taken from the tree texture, its alpha value ( typically 1 ) is to be multipled by the alpha in the diffuse ( 0.5 is good ) resulting in 0.5, which means the final pixel in the back buffer is going to be :
    FinalPixel = ( 1 - 0.5 ) * OriginalPixel + 0.5 * TreeTexturePixel

    Note that : TreeTexturePixel = TreeTextureDiffuseComponent * TreeTexturePixelAtUV

    I know there are possibly millions of bugs here. I hope you get the idea I wanted to present. Test it, and if it runs tell me
    Last edited by Coder; 06-16-2002 at 03:03 PM.
    Muhammad Haggag

  3. #3
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Yeah that looks exactly like what I came up with. Problem now is how do I create an alpha texture in Paint Shop Pro 4? Could I simply use a grey-scale texture for the alpha? I would really like to shift the alphas 24 bits to the left so that DirectX can simply use the alphas w/o having to place 2 textures down to get 1 job done.

    I could probably load the grey-scale alpha representation of the image into the actual BMP image during the load process. This would only require a few more lines of code and one more function in my Texture class. Heck I might even just derive off of that class and call it AlphaTexture or something.

    Thanks for the texture stage states though - there are so many of them they can become quite confusing.

  4. #4
    Registered User Coder's Avatar
    Join Date
    Aug 2001
    Location
    Cairo, Egypt
    Posts
    128
    Modulate ( multiply ) the diffuse component ( degree of blackness ) by the texture color component ( thus resulting in the shadow )
    This is wrong. This will result in having colored shadows, which is not what a shadow is. The color texture stage states should be :

    Code:
    SetTextureStageState( Stage,D3DTSS_COLOROP,D3DTOP_SELECTARG1 );
    SetTextureStageState( Stage,D3DTSS_COLORARG1, D3DTA_DIFFUSE );
    This will make Direct3D plot a pixel that has the shadow color - specified in the diffuse component - if the alpha value isn't 0 ( the alpha tex stage states cull useless pixels )

    About the alpha in Psp4 thing
    Well, you have your tree image, right? it has to be stored in some rectangular image file. That means you'll get additional - background - pixels around your tree texture. Give these pixels a unique color - a color that's not used in the tree. Then you can load the image using D3DX's texture loading functions that support color keying, supplying the background color as the color key.

    If you wish, you can do this manually by locking your texture memory and looping through the texels, zeroing the alpha of the texels having the background color.
    Muhammad Haggag

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. d3d9 c++ texture wrapper... again...
    By yaya in forum Game Programming
    Replies: 0
    Last Post: 04-01-2009, 01:08 PM
  2. D3d Texture Wrapper == ARRRGGGGHHH
    By yaya in forum Game Programming
    Replies: 1
    Last Post: 03-25-2009, 06:41 PM
  3. Replies: 4
    Last Post: 02-27-2008, 03:10 PM
  4. OpenGL - look trough surface -nehe lesson
    By GanglyLamb in forum Game Programming
    Replies: 8
    Last Post: 08-24-2006, 11:06 PM
  5. Pong is completed!!!
    By Shamino in forum Game Programming
    Replies: 11
    Last Post: 05-26-2005, 10:50 AM