point light illumination using Phong model

Posted by Myx on Stack Overflow See other posts from Stack Overflow or by Myx
Published on 2010-04-02T00:55:53Z Indexed on 2010/04/02 1:03 UTC
Read the original article Hit count: 484

Filed under:
|
|
|

Hello:

I wish to render a scene that contains one box and a point light source using the Phong illumination scheme. The following are the relevant code snippets for my calculation:

R3Rgb Phong(R3Scene *scene, R3Ray *ray, R3Intersection *intersection)
{
  R3Rgb radiance;
  if(intersection->hit == 0)
  {
    radiance = scene->background;
    return radiance;
  }

  ...
  // obtain ambient term
  ... // this is zero for my test

  // obtain emissive term
  ... // this is also zero for my test

  // for each light in the scene, obtain calculate the diffuse and specular terms
  R3Rgb intensity_diffuse(0,0,0,1);
  R3Rgb intensity_specular(0,0,0,1);
  for(unsigned int i = 0; i < scene->lights.size(); i++)
  {
    R3Light *light = scene->Light(i);
    R3Rgb light_color = LightIntensity(scene->Light(i), intersection->position);
    R3Vector light_vector = -LightDirection(scene->Light(i), intersection->position);

    // check if the light is "behind" the surface normal
    if(normal.Dot(light_vector)<=0)
      continue;

    // calculate diffuse reflection
    if(!Kd.IsBlack())
      intensity_diffuse += Kd*normal.Dot(light_vector)*light_color;

    if(Ks.IsBlack())
      continue;

    // calculate specular reflection
    ... // this I believe to be irrelevant for the particular test I'm doing

  }

  radiance = intensity_diffuse;
  return radiance;
}

R3Rgb LightIntensity(R3Light *light, R3Point position)
{
  R3Rgb light_intensity;
  double distance;
  double denominator;
  if(light->type != R3_DIRECTIONAL_LIGHT)
  {
    distance = (position-light->position).Length();
    denominator = light->constant_attenuation + 
                         (light->linear_attenuation*distance) + 
                         (light->quadratic_attenuation*distance*distance);
  }   

  switch(light->type)
  {
    ...

    case R3_POINT_LIGHT:
      light_intensity = light->color/denominator;
      break;

    ...
  }
  return light_intensity;
}

R3Vector LightDirection(R3Light *light, R3Point position)
{
  R3Vector light_direction;
  switch(light->type)
  {
    ...
    case R3_POINT_LIGHT:
      light_direction = position - light->position;
      break;
    ...
  }
  light_direction.Normalize();
  return light_direction;
}

I believe that the error must be somewhere in either LightDirection(...) or LightIntensity(...) functions because when I run my code using a directional light source, I obtain the desired rendered image (thus this leads me to believe that the Phong illumination equation is correct). Also, in Phong(...), when I computed the intensity_diffuse and while debugging, I divided light_color by 10, I was obtaining a resulting image that looked more like what I need. Am I calculating the light_color correctly?

Thanks.

© Stack Overflow or respective owner

Related posts about raytracing

Related posts about physics