# problem with specular highlight shader...

This topic is 3434 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

hi im trying to implement a simple specular highlight+ diffuse shader .. the diffuse component seems to be working but the specular component isnt showing up properly... if i make the specular power very high (say 200) then nothing shows up at all.. this could mean that there are no values in the range 0.9-1.0 being generated in the dot product... what could be wrong???
DV2P DiffuseVS(float4 inPos :POSITION,float4 inNor :NORMAL)
{
DV2P Output=(DV2P)0;
Output.Position=mul(inPos,xViewProjection);
Output.Position3D=mul(inPos,xViewProjection);
Output.Normal=mul(inNor,xRot);
return Output;

}

DP2F DiffusePS(DV2P PSin)
{
DP2F Output=(DP2F)0;
float3 vector1;
float3 vector2;
float4 xLightPos={20.0f,10.0f,5.0f,0.0f};
vector1=normalize(xLightPos);
vector2=normalize(PSin.Normal);
float DiffuseFactor=dot(vector1,vector2);
Output.Color.r=0.9f*DiffuseFactor;
Output.Color.g=1.0f*DiffuseFactor;
Output.Color.b=1.0f*DiffuseFactor;
Output.Color.a=1.0f;
float4 v={0.0f,10.0f,10.0f,0.0f};
v=-PSin.Position3D;
float4 V=normalize(v);
float4 R=normalize(reflect((xLightPos),(PSin.Normal)));
float specular=saturate(dot(V,R));
Output.Color+=pow(specular,50);
return Output;

}


*edit* : As you can see in this picture the specular highlighting "seems" right on the model in front... however the model at a the back appears to have hardly any specular highlighting on it... am i doing something wrong with my matrices, coordinate spaces etc?? PS :Dont mind the blur .. its in a separate pass.. also there seems to be pixelation of the image due to the half pixel offset thingy have to look into that... [Edited by - rohith291991 on December 24, 2008 3:54:53 AM]

##### Share on other sites
Well is there a problem with this shader at all??

##### Share on other sites
In your pixel shader code, you normalize the xLightPos and PSin.Normal to vector1 and vector2 respectively (which is good), but you later use the unnormalized values anyway (which is bad). This effectively results in vertex-level interpolation, but I assume that your intention is to actually evaluate lighting per pixel.

##### Share on other sites
Could you explain?? i used vector1 and vector2 in the reflection part but this produced no specular highlight whatsoever... also why would not normalizing them result in vertex level interpolation?? sorry but im noob... could you modify my code in such a way that it would be correct??

##### Share on other sites
vector1=normalize(xLightPos);  vector2=normalize(PSin.Normal);  float DiffuseFactor=dot(vector1,vector2);  Output.Color.r=0.9f*DiffuseFactor;  Output.Color.g=1.0f*DiffuseFactor;  Output.Color.b=1.0f*DiffuseFactor;  Output.Color.a=1.0f; float4 v={0.0f,10.0f,10.0f,0.0f}; // I presume this is camera position  v=-PSin.Position3D;  float4 V=normalize(v);  float4 R=normalize(reflect((vector1),(vector2))); // here was the problem that Nik02 told you about - you had  xLightPos and PSin.Normal  float specular=saturate(dot(V,R));  Output.Color+=pow(specular,50);  return Output;

Try this, if it doesn't work then make sure that all the values are set properly

• 23
• 10
• 19
• 15
• 14