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:

enter image description here

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

I'm looking for something more like this:

enter image description here

  • \$\begingroup\$ 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 :(. \$\endgroup\$
    – Roy T.
    Commented Jun 3, 2014 at 14:37
  • \$\begingroup\$ 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 \$\endgroup\$
    – Thijser
    Commented Jun 3, 2014 at 14:50
  • \$\begingroup\$ Normal is in correct space? \$\endgroup\$
    – Archy
    Commented Jun 3, 2014 at 15:06
  • \$\begingroup\$ 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? \$\endgroup\$
    – Archy
    Commented Jun 3, 2014 at 15:23
  • \$\begingroup\$ 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 \$\endgroup\$
    – Thijser
    Commented Jun 3, 2014 at 16:00

1 Answer 1


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: enter image description here

  • 1
    \$\begingroup\$ That's actually funny. Thanks for sharing the fix. \$\endgroup\$
    – ashes999
    Commented Jun 3, 2014 at 17:03
  • \$\begingroup\$ 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. \$\endgroup\$
    – AturSams
    Commented Jun 3, 2014 at 17:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .