SDK Shader Question

I started using the "ShadowVolume.fx" shader from the ShadowVolume c++ sample in the SDK and it works great in my program. Now I want to extend that shader to add specular highlighting, and possibly environment mapping. I am at a loss on how to do that though. I have found some examples on how to implement the specular highlighting, but it looks like they declare a light object inside the shader, which the sample doesn't do at all, and yet the lighting still seems to work. Does this mean the sample uses some kind of custom light routine, instead of the DX one?
Can anyone give me a hand with this?
Thanks
[627 byte] By [GremlinX] at [2008-2-27]
# 1
The ShadowVolume sample uses the FX file to do lighting in the VertexShader (for ps 1.1 cards) or in the PixelShader (for ps 2.0 cards). The samples contains enough information to create specular highlights with very few modifications. The application already passes in the view-space light position to the effect file in OnFrameRender. This is g_vLightView.

To add specular highlights to the sample, we first need to get the specular property from the material into the shader. The last section of RenderScene renders the mesh with all of it's materials. The diffuse material color is already passed in as g_vMatColor to the shader. To pass the specular component for this material to the FX file, the following line must be added after the diffuse material is set.

V( g_pEffect->SetVector( "g_vMatSpec", (D3DXVECTOR4*)&g_Mesh.m_pMaterialsIdea.Specular ) );

Note, that dwarf mesh in the sample does not contain a specular color in the material, so using that particular mesh will result in no highlight. To show specular highlighting on the dwarf mesh, use the following code instead:

D3DXVECTOR4 vecOne(1,1,1,1);
V( g_pEffect->SetVector( "g_vMatSpec", (D3DXVECTOR4*)&vecOne );
This is all that is needed in the .cpp file. For the FX file, the following changes need to be added. First, there needs to be a global variable called g_vMatSpec that corresponds to the specular value that is passed in from the .cpp file. The top of your .fx file should have this in it now:

float4 g_vMatColor; // Color of the material
float4 g_vMatSpec; // Specular response of the material

Secondly, depending on the shader model you're using, you need to either modify VertScene1x or PixScene. The specular highlight code for both is very similar. We'll be using the Blinn specular model which uses the half-angle vector to calculate where the highlight goes.

For VertScene1x we'll modify the code to look like the following. This will result in per-vertex specular highlights.

InvL = normalize( InvL );
oDiffuse =
saturate( dot( N, InvL ) ) * g_vMatColor * g_vLightColor / LengthSq;

// Compute Specular lighting
float3 H = normalize( InvL + float3(0,0,-1) );
float4 S = g_vMatSpec*pow( dot(H, N ), 32 );
oDiffuse += S;

For PixScene, the following code will result in per-pixel specular highlights:

// Compute Specular lighting
float3 H = normalize( L + float3(0,0,-1) );
float4 S = g_vMatSpec*pow( dot(H, ViewNormal ), 32 );

// Lookup mesh texture and modulate it with diffuse and specular
return float4( tex2D( g_samScene, Tex0 ).xyz, 1.0f ) * (I + S);

Don't forget the +S on the end. This is where we add the specular contribution.

SHANOND at 2007-9-9 > top of Msdn Tech,Game Technologies: DirectX, XNA, XACT, etc.,Game Technologies: Graphics...