Not logged in, Join Here! or Log In Below:  
 
News Articles Search    
 

 Home / 3D Theory & Graphics / ID3DXSprite Interface: How to enable lighting? Account Manager
 
Archive Notice: This thread is old and no longer active. It is here for reference purposes. This thread was created on an older version of the flipcode forums, before the site closed in 2005. Please keep that in mind as you view this thread, as many of the topics and opinions may be outdated.
 
philkr

May 10, 2005, 12:55 PM

I'm coding a tile-based 2D game just to learn some Direct3D and have some fun. I already can display and scroll the map and other sprites, for which I use the ID3DXSprite interface. Now I want to add some lighting, because if I only work with 2D graphics than I could at least have some cool light effects. Since I have no clue about lights, I tried the sample in the SDK. I only got ambient lighting to work. Adding light sources does not work. Maybe you guys can help me this is my setup and the code where I add the light:

  1.  
  2.   m_lpD3DD->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
  3.   m_lpD3DD->SetRenderState(D3DRS_ALPHAFUNC, D3DCMP_GREATER);
  4.   m_lpD3DD->SetRenderState(D3DRS_ALPHAREF, 0x00);
  5.   m_lpD3DD->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
  6.   m_lpD3DD->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD);
  7.   m_lpD3DD->SetRenderState(D3DRS_CLIPPING, TRUE);
  8.   m_lpD3DD->SetRenderState(D3DRS_CLIPPLANEENABLE, FALSE);
  9.   m_lpD3DD->SetRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_ALPHA | D3DCOLORWRITEENABLE_BLUE | D3DCOLORWRITEENABLE_GREEN | D3DCOLORWRITEENABLE_RED);
  10.   m_lpD3DD->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
  11.   m_lpD3DD->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
  12.   m_lpD3DD->SetRenderState(D3DRS_DIFFUSEMATERIALSOURCE, D3DMCS_COLOR1);
  13.   m_lpD3DD->SetRenderState(D3DRS_AMBIENTMATERIALSOURCE, D3DMCS_COLOR1);
  14.   m_lpD3DD->SetRenderState(D3DRS_AMBIENT, 0x00202020);
  15.   m_lpD3DD->SetRenderState(D3DRS_ENABLEADAPTIVETESSELLATION, FALSE);
  16.   m_lpD3DD->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
  17.   m_lpD3DD->SetRenderState(D3DRS_FOGENABLE, FALSE);
  18.   m_lpD3DD->SetRenderState(D3DRS_INDEXEDVERTEXBLENDENABLE, FALSE);
  19.   m_lpD3DD->SetRenderState(D3DRS_LIGHTING, TRUE);
  20.   m_lpD3DD->SetRenderState(D3DRS_RANGEFOGENABLE, FALSE);
  21.   m_lpD3DD->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
  22.   m_lpD3DD->SetRenderState(D3DRS_SHADEMODE, D3DSHADE_GOURAUD);
  23.   m_lpD3DD->SetRenderState(D3DRS_SPECULARENABLE, FALSE);
  24.   m_lpD3DD->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
  25.   m_lpD3DD->SetRenderState(D3DRS_SRGBWRITEENABLE, FALSE);
  26.   m_lpD3DD->SetRenderState(D3DRS_STENCILENABLE, FALSE);
  27.   m_lpD3DD->SetRenderState(D3DRS_VERTEXBLEND, FALSE);
  28.   m_lpD3DD->SetRenderState(D3DRS_WRAP0, 0);
  29.   m_lpD3DD->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
  30.   m_lpD3DD->SetTextureStageState(0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE);
  31.   m_lpD3DD->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
  32.   m_lpD3DD->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
  33.   m_lpD3DD->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
  34.   m_lpD3DD->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
  35.   m_lpD3DD->SetTextureStageState(0, D3DTSS_TEXCOORDINDEX, 0);
  36.   m_lpD3DD->SetTextureStageState(0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_DISABLE);
  37.   m_lpD3DD->SetTextureStageState(1, D3DTSS_ALPHAOP, D3DTOP_DISABLE);
  38.   m_lpD3DD->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_DISABLE);
  39.   m_lpD3DD->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_MIRROR);
  40.   m_lpD3DD->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_MIRROR);
  41.   m_lpD3DD->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
  42.   m_lpD3DD->SetSamplerState(0, D3DSAMP_MAXMIPLEVEL, 0);
  43.   m_lpD3DD->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
  44.   m_lpD3DD->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_POINT);
  45.   m_lpD3DD->SetSamplerState(0, D3DSAMP_MIPMAPLODBIAS, 0);
  46.   m_lpD3DD->SetSamplerState(0, D3DSAMP_SRGBTEXTURE, 0);
  47.  
  48.   D3DXVECTOR3 vecLight(100.0f, 100.0f, 1.0f);
  49.   D3DLIGHT9 light;
  50.   ZeroMemory(&light, sizeof(light));
  51.   light.Type = D3DLIGHT_POINT;
  52.   light.Position = vecLight;
  53.   light.Attenuation0 = 0.1f;
  54.   light.Range = 400.0f;
  55.   light.Diffuse.r = 1.0f;
  56.   light.Diffuse.g = 1.0f;
  57.   light.Diffuse.b = 1.0f;
  58.   m_lpD3DD->SetLight(0, &light);
  59.   m_lpD3DD->LightEnable(0, TRUE);
  60.  
  61.   // I think this is useless because I set DIFFUSEMATERIALSOURCE to COLOR1
  62.   D3DMATERIAL9 mtrl;
  63.   ZeroMemory( &mtrl, sizeof(mtrl) );
  64.   mtrl.Diffuse.r = mtrl.Ambient.r = 1.0f;
  65.   mtrl.Diffuse.g = mtrl.Ambient.g = 1.0f;
  66.   mtrl.Diffuse.b = mtrl.Ambient.b = 1.0f;
  67.   mtrl.Diffuse.a = mtrl.Ambient.a = 1.0f;
  68.   m_lpD3DD->SetMaterial( &mtrl );
  69.  


 
philkr

May 13, 2005, 11:14 AM

Well, nobody answered yet and I have thought about the problem myself and I can find three possible error sources, but I don't know which is causing it:

1)
The ID3DXSprite internly does use a vertex format without normals, so the light can not be calculated. This would be very bad, because I couldn't change it and would have to create my own interface.

2)
There is an error in my render setup which does not allow lighting. There are so many render states and I'm new to Direct3D so maybe I made a mistake there.

3)
The coordinates I provided are not in the right format. I use screen pixel coordinates, but I think it should work because I use orthogonal projection and I also use screen coordinates when displaying the sprites. Nevertheless I have no clue which z-value to provide.

4)
Adding the light does not work. This is very unprobable because I copied the code from the tutorial. But maybe I paste it in the wrong place?

 
This thread contains 2 messages.
 
 
Hosting by Solid Eight Studios, maker of PhotoTangler Collage Maker.