# What is wrong with my specular phong shading

I'm sorry if this should be placed on stackoverflow instead however seeing as this is graphics related I was hoping you guys could help me: I'm attempting to write a phong shader and currently working on the specular. I came acros the following formula: base*pow(dot(V,R),shininess) and attempted to implement it (V is the posion of the viewer and R the reflective vector). This gave the following result and code:

Vec3Df phongSpecular(const Vec3Df & vertexPos, Vec3Df & normal, const Vec3Df & lightPos, const Vec3Df & cameraPos, unsigned int index)
{
Vec3Df relativeLightPos=(lightPos-vertexPos);
relativeLightPos.normalize();
Vec3Df relativeCameraPos= (cameraPos-vertexPos);
relativeCameraPos.normalize();
int DotOfNormalAndLight = Vec3Df::dotProduct(normal,relativeLightPos);
Vec3Df reflective =(relativeLightPos-(2*DotOfNormalAndLight*normal))*-1;
reflective.normalize();
float phongyness= Vec3Df::dotProduct(reflective,relativeCameraPos);
if (phongyness<0){
phongyness=0;
}
float shininess= Shininess[index];
float speculair = powf(phongyness,shininess);
return Ks[index]*speculair;
}


I'm looking for something more like this:

• What is exactly wrong? What is the effect you're trying to reach? What have you tried, and what are the values of the parameters shininess and speculair? (which should be spelled specular btw ;) ) Without this info we can't help you. Unfortunately we're not wizards :(. – Roy T. Jun 3 '14 at 14:37
• I have added what I'm trying to reach shininess is 11 right now (but could later be changed to allow for multiple materials.) the specular is calculated inside the function – Thijser Jun 3 '14 at 14:50
• Normal is in correct space? – Archy Jun 3 '14 at 15:06
• Looks good. VertexPos, lightPos and normal must all be in the same space. Shininess[index] and Ks[index] must be valid. Try to input fixed, extreme values for these. Your output looks like it is diffuse only, did you actually add the specular result? – Archy Jun 3 '14 at 15:23
• This is only the specular term. I know that the vertex lightpos and normal are all correct because the diffuse term does work (which is implemented with all the same values except it uses kd instead of ks). printing ks gives a set the vertex (0.5,0.5,0.5) for ever index right now – Thijser Jun 3 '14 at 16:00

I have found the answer (and now I feel stupid)

int DotOfNormalAndLight = Vec3Df::dotProduct(normal,relativeLightPos);


should have been

float DotOfNormalAndLight = Vec3Df::dotProduct(normal,relativeLightPos);


the end result looks like this:

• That's actually funny. Thanks for sharing the fix. – ashes999 Jun 3 '14 at 17:03
• The compiler would have told you this is a poor idea if it was more sensitive because dotProduct obviously returns a floating point type while int would require rounding and casting. For instance in Haxe which I prefer for prototyping that would never fly unless you use explicit casting. – wolfdawn Jun 3 '14 at 17:10