SDK Shader Question
Can anyone give me a hand with this?
Thanks
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_pMaterials 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); float4 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. InvL =
.Specular ) );
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_vMatSpec; // Specular response of the material
For VertScene1x we'll modify the code to look like the following. This will result in per-vertex specular highlights.
oDiffuse = saturate( dot( N, InvL ) ) * g_vMatColor * g_vLightColor / LengthSq;
// Compute Specular lighting
For PixScene, the following code will result in per-pixel specular highlights: // Compute Specular lighting Don't forget the +S on the end. This is where we add the specular contribution.
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);