1

I've been trying to produce a glossy shading using the Phong model but for some reason instead of glossy appearance, all I get is big white blotch on the front of the sphere. Initially the model worked for a single sphere, but now I've updated the code so I can draw multiple spheres and the model has started to fail despite applying the same logic and I don't know why.

single sphere: diffuse and specular single sphere: diffuse and specular

diffuse, multiple

diffuse, multiple

diffuse+specular, multiple

diffuse+specular, multiple

main part

vec color(const ray& r)
{
    vector <sphere> objects;
    vector <Light> lighting;

    objects.push_back(sphere(vec(0,-100.5,-3), 100, vec(0, 1, 0)));
    objects.push_back(sphere(vec(0, 0, -1), 0.5, vec(1, 0, 0)));
    objects.push_back(sphere(vec(0, 1 ,-1), 0.5, vec(1, 0, 1)));
    lighting.push_back(Light(vec(0, 0, -1), vec(0, -1, 0)));

    float infinity = 2000.0;
    sphere* closest = NULL;
    vec background_color( .678, .847, .902);
    vec totalLight(0.0, 0.0, 0.0);
    int pos = 0;
    for(int j = 0; j < objects.size(); j++)
    {
        float t = objects[j].intersect(r);
        if(t > 0.0)
        {
            if(t < 2000.0)
            {
                infinity = t;
                closest = &objects[j];
                pos = j;
            }
        }
    }
    if(infinity == 2000.0)
        return background_color;
    else
    {
        float a = objects[pos].intersect(r);
        vec view_dir = vec(-2, 2, 10) - r.p_at_par(a);
        vec normal = unit_vector((r.p_at_par(a) - closest->centre)/closest->radius);
        vec light = unit_vector(vec(-2, 0, 0) - r.p_at_par(a));
        vec reflection = 2.0*dot(light, normal)*normal - light;
        vec specular = vec(1, 1, 1)*pow(max(0.f, dot(reflection, view_dir)), 256);
        vec diffuse = (closest->color)*max(0.f, dot(normal, light));
        vec total = diffuse + specular;
        return total;

    }
}

as I understand, specular = white * dot(view_dir, L_dir)^n * ks and the total lighting is = specular + diffuse + ambient.

JerSci
  • 167
  • 1
  • 9

1 Answers1

0

You are indeed right on your specular contribution. You can see it as how much light is reflected into my viewing direction.

First of all I don't see you normalising view_dir. Make sure all vectors are normalised. If a and b have length 1 the next is true

Also to help debugging in the future you may want to generate false color images. This images can help you see what's going on. e.g you can just render your flat color, surface normals (xyz to rgb), the number of light sources affecting a certain pixel, ... . This may help you spotting unexpected behaviours.

Hope this helps.

theVortr3x
  • 70
  • 10