![]() |
Multimedia »
General Graphics »
Graphics
Intermediate
License: The GNU General Public License (GPL)
Basic Illumination Model in C#By andalmeidaBasic illumination model in C#. |
C#2.0, Windows, .NET2.0, ASP.NET, GDI+, WebForms, VS2005, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
|
Ambient
|
Diffuse
|
Specular
|
|
Ambient+Diffuse
|
Diffuse+Specular
|
Ambient+Diffuse+Specular
|
In my previous article Simple Ray Tracing in C#, we saw how to display spheres using a simple ray tracing algorithm. Now, we will start from the last point and will work with basic illumination models.
By definition, we have three types of light:
It is considered as the light distributed by the environment, which contributes to the global illumination independent of the light position, objects, or observer.
It is the light the contribution of which depends on its incidence angle. Diffuse light hit can be reflected in all directions.
Specular lights represent the bright spots in objects; the more reflective it is the smaller the bright spot.
To proceed with an illumination 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:
| K | Ambient | Diffuse | Specular |
| RED | 0.329412 | 0.780392 | 0.992157 |
| GREEN | 0.223529 | 0.568627 | 0.941176 |
| BLUE | 0.027451 | 0.113725 | 0.807843 |
Exponent 27.8974
The ray tracing algorithm used is the same as you can see on Simple Ray Tracing in C# with the improvements of a basic illumination model.
A Light Source is defined as an R3 (x,y,z) point with a (vx,vy,vz) direction vector.
An Observer is defined as an R3 (x,y,z) point with a (vx,vy,vz) direction vector.
Theta is defined as the angle between a light ray and a normal vector at the intersection point P on the surface.
Phi is defined as the angle between the reflected light ray at the intersection point P on the surface and the viewer ray to the same point P.
where
...
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);
...
}
...
| You must Sign In to use this message board. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads.
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 10 Aug 2007 Editor: Smitha Vijayan |
Copyright 2007 by andalmeida Everything else Copyright © CodeProject, 1999-2010 Web21 | Advertise on the Code Project |