0
\$\begingroup\$

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);
    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:

enter image description here

\$\endgroup\$
  • \$\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. Jun 3 '14 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 Jun 3 '14 at 14:50
  • \$\begingroup\$ Normal is in correct space? \$\endgroup\$ – Archy Jun 3 '14 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 Jun 3 '14 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 Jun 3 '14 at 16:00
2
\$\begingroup\$

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.