Monday, June 6, 2011

Direct3D9 FFP Lighting

Today I finally managed to get the cockpit model into the game.  I've decided to use the fixed function pipeline this time rather than shaders; it's probably a good idea to be experienced with both.

To use FFP (fixed function pipeline) lighting, you first need to fill in a D3DLIGHT9 structure.  The elements are pretty straight-forward.  I've found that you need values much greater than 1.0 for the light's color though; somewhere around 300 in R, G, and B for white lights is working well for the moment.  After you have the structure filled out, pass it to IDirect3DDevice9::SetLight().  After setting the light information, you need to enable to light as well using IDirect3DDevice9::LightEnable().

To take full advantage of the lighting system, I recommend setting a material as well.  To do this, fill out a D3DMATERIAL9 structure.  The material describes what components of the incoming light are reflected, and by how much.  It also describes how shiny a surface is with the Power element, and how much light the surface is emitting with the Emissive element.  Note that the emissive element does not actually act as a D3D light; it only makes it seem like the material is glowing.  After the material structure has been filled, pass it to IDirect3DDevice9::SetMaterial(). Now that the actual material is setup, we need to tell Direct3D that the diffuse and specular information is coming from the material and not the vertex buffer.  To do this, set the D3DRS_DIFFUSEMATERIALSOURCE and D3DRS_SPECULARMATERIALSOURCE render states to D3DMCS_MATERIAL.

No comments:

Post a Comment