What is wrong with my specular phong shading

Posted by Thijser on Game Development See other posts from Game Development or by Thijser
Published on 2014-06-03T13:48:08Z Indexed on 2014/06/03 21:42 UTC
Read the original article Hit count: 338

Filed under:
|
|
|
|

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

© Game Development or respective owner

Related posts about opengl

Related posts about shaders