Thursday, January 8, 2015

Phong Coloring Model

Now i will tell you how to render the color of object once we had found the intersection point with the object in the scene. If the ray hit the object we need to pick the color of that object and fill the pixel on image plane with that color using PHONG ILLUMINATION MODEL.
Phong Illumination Model is a global illumination model. Global illumination is a color scheme that take into account the colors reflected, refracted from other objects.
There are three types of color of an object:
1. Ambient Color - is a color from the indirect light hitting a object after reflecting off of from other surfaces. This is a color reflected from the object.
2. Diffuse Color - reflected color of the object. This color is reflected in all direction and is not view dependent means it is reflected in all directions.
3. Specular Color - This is light color which is not absorbed by the object. It is view dependent means it is reflected in around one particular direction. Because of specular component we see the white color (or color of light like blue, green etc) spot on the images
CALCULATING DIFFUSE COLOR:

Diffuse color=Id*Light Color*max((normal.dot(L),0.0))*objectDiffuseColor

Id = light intensity (given in .iv file) 
L = light vector (light position - intersection point)

In C++:
float Id = 1;
colorFactor = normal.dot(L);
dRed = Id*light.getColor()*max(colorFactor,0.0)*sphere.getDiffuseColor()[0];    
dGreen = Id*light.getColor()*max(colorFactor,0.0)*sphere.getDiffuseColor()[1];
 dBlue = Id*light.getColor()*max(colorFactor,0.0)*sphere.getDiffuseColor()[2];
diffuse_component.setValue(dRed,dGreen,dBlue);

CALCULATING AMBIENT COLOR:

Ambient color = Ia * Light Color * Object Diffuse Color 
Ia = intensity of light (given in .iv file)

In C++:
float Ia = 1;
aRed = Ia * light.getColor() * sphere.getDiffuseColor()[0];
aGreen = Ia * light.getColor() * sphere.getDiffuseColor()[1];
aBlue = Ia * light.getColor() * sphere.getDiffuseColor()[2];
ambient_component.setValue(aRed,aGreen,aBlue);
CALCULATING SPECULAR COLOR:
Using Vector Algebra we can calculate S and R in above diagram
S = (N.dor(L))*N - L
R = (N.dor(L))*N + (N.dor(L))*N - L = 2(N.dor(L))*N - L
V = intersection point - camera position

Cosine Fall-off (q): Glossiness of the surface 
Cos(a)^q = (V.R)^q
In C++:
R = 2*((normal.dot(L))*normal) - L;
V = rayStart - point;
R.normalize();
V.normalize();
  if(vVector.dot(rVector) < 0){
 R.setValue(0,0,0);
 V.setValue(0,0,0);
  }
  float spec_float = pow((vVector.dot(rVector)),50);
  float Is = 1;
  sRed = Is * light.getColor() * spec_float * sphere.getSpecularColor()[0];
  sGreen = Is * light.getColor() * spec_float * sphere.getSpecularColor()[1];
  sBlue = Is * light.getColor() * spec_float * sphere.getSpecularColor()[2];
specular_component.setValue(sRed,sGreen,sBlue);
NOTE: In coding it is necessary to check whether R.dot(V) is negative. If it is negative set the dot product to zero. Otherwise you will see multiple spots of light on the object

No comments:

Post a Comment