Search Wiki:
Resource Page Description
Implementing Ambient, Diffuse and Specular lights

Introduction

In this short article we will discuss and implement a basic illumination model using raytracing.

By definition we have 3 types of light:

*Ambient
*Diffuse
*Specular

Ambient

It is considered as the light distributed by the environment, which contributes to the global illumination independently of the light position, objects or observer.

Diffuse

It is the light which contribution depends on its incidence angle. Diffuse light hit can be reflected in all directions.

Specular

Specular lights represents the bright spots in the objects, as more reflective we get smaller bright spot.

Background

To proceed with an illumination model algorithm we need to get the ambient, diffuse and specular constants for the material we want to model, for example here we use brass constants which are:

  • Ambient / Diffuse / Specular*
RED 0.329412 0.780392 0.992157
GREEN 0.223529 0.568627 0.941176
BLUE 0.027451 0.113725 0.807843

Brass exponent: 27.8974

The Equations

*Light Source is defined as an R3 (x,y,z) point with a (vx,vy,vz) direction vector.

*Observer is defined as an R3 (x,y,z) point with a (vx,vy,vz) direction vector.

*Theta is defined as the angle between light ray and normal vector at the intersection point P on surface.

*Phi is defined as the angle between the reflected light ray at intersection point P on surface and the viewer ray to the same point P.

The Sphere Equation

(r)2 = (x-cx)2+(y-cy)2+(z-cz)2

where

*r is the sphere radius
*(cx,cy,cz) is the center of the sphere

Illumination equation on a given pixel:

IAmbient = I * KAmbient
IDiffuse = I * KDiffuse * cos(theta)
ISpecular = I * KSpecular * cos(phi)n
I = IAmbient + IDiffuse + ISpecular

Reflection equation:

i' = i - (2 * n * dot(i, n))

where

*i = incidence light ray
*n = normal at intersection
*i' = reflected ray

The Code

...
if (spherehit != null)
     {
     double intersx  = px + t * vx, intersy = py + t * vy,
                       intersz = pz + t * vz;
     double vNormalX = intersx - spherehit.cx,
                       vNormalY=intersy - spherehit.cy,
                       vNormalZ=intersz - spherehit.cz;
     double cost     = tAlgebra.GetCosAngleV1V2(lvx, lvy, lvz, vNormalX,
                       vNormalY, vNormalZ);
     if (cost < 0) cost = 0;
 
     double vReflX = 0, vReflY = 0, vReflZ = 0;
     double vEye2IntersX = px - intersx, vEye2IntersY = py - intersy,
                           vEye2IntersZ = pz - intersz;
 
     tAlgebra.Reflect(lvx,lvy,lvz, vNormalX,vNormalY,vNormalZ,ref vReflX,
                      ref vReflY, ref vReflZ);
     double cosf = tAlgebra.GetCosAngleV1V2(vReflX, vReflY, vReflZ,
                   vEye2IntersX, vEye2IntersY, vEye2IntersZ);
     if (cosf < 0) cosf = 0;
 
     double result1 = cost * 255.0;
     double result2 = Math.Pow(cosf, spherehit.shininess) * 255.0;
     double rgbR = (spherehit.ambientR * 255.0)+(spherehit.diffuseR *
                    result1) + (spherehit.specularR * result2);
     double rgbG = (spherehit.ambientG * 255.0) +(spherehit.diffuseG *
                    result1) + spherehit.specularG * result2);
     double rgbB = (spherehit.ambientB * 255.0) +(spherehit.diffuseB *
                    result1) + (spherehit.specularB * result2);
     rgbR = Math.Min(rgbR, 255);
     rgbG = Math.Min(rgbG, 255);
     rgbB = Math.Min(rgbB, 255);
     color = Color.FromArgb((int)rgbR, (int)rgbG, (int)rgbB);
     ...
     }
...

Mode details and resulting images at:

http://www.codeproject.com/KB/graphics/Basic_Illumination_Model.aspx
Last edited Nov 28 2008 at 5:02 PM  by andalmeida, version 7
Updating...
Page view tracker