Hello, I'm writing a raytracer. please take a look at the attachment. all of the balls should be grey but where the normals are almost perpendicular to the viewer, no color is assigned (and thus assigned the error value red). also the error seems to have an orientation towards the light (the light is the middle ball on the bottom); the light is in the middle above the middle ball.
Please look at my code. I know what does wrong (the dot product appears to be lower than 0, and so the if statement fails) but I have no clue as of why.
Any idea's?
Code:
// init color on black
Color* color = new Color( 0,0,0);
// walk through lights
for(int i = 0; i< world->GetNrSurfaces(); i++){
Surface* surface = world->GetSurface(i);
if(surface->IsLight()){
Surface* light = surface;
vector3 L = *((Sphere*) light)->GetCenter() - intersectionPoint;
L.Normalize();
vector3 N = *GetSurfaceNormal(&intersectionPoint);
// apply diffuse lighting
if( material->GetDiffuse() > 0){
float dot = DOT(N, L);
if(dot > 0){
float diff = dot * material->GetDiffuse();
// add the diffuse component
//*color += diff * *material->GetColor() * *light->GetMaterial()->GetColor();
*color += *light->GetMaterial()->GetColor();
}
else *color += new Color(1.0f,0,0);
}
}
}
Last edited by garma; November 17th, 2005 at 03:06 AM.
I will try to explain how i think i understood your problem:
See the attached picture:
the added white lines represent how i understtod your code:
L - the light vector from the center of the center of the light to the intersection point
N - normal vector from the intersection point
Θ - Angle Theta between the two vectors L & N
The DOT product is defined as: L * N = |L| * |N| * cos Θ
If the L & N vectors are NORMALIZED then |L| = 1 and |N| = 1 so
the DOT product then is: L * N = cos Θ
If the angle Θ is larger than 90 degrees as in the case of the attached picture i.e. your case, then the result of the DOT product IS A NEGATIVE VALUE !!!
Also you must know that the DOT product gives the SPACE ANGLE between the vectors i.e. it takes into account all coordinates from those vectors X,Y,Z
To put it simply: where the DOT product is NEGATIVE the light doesn't reach (the RED color in your picture) so that place is lighted ONLY by ambient light and (if any) radiosity resulted lighting i.e. bounced light from a near light source
the real physically correct lighting model is more complex than simple radiosity lighting, but it gives satisfactory results.
Hope this explains your problem , if not, sorry for this long explanation
In that case there is something else wrong in your code , or I didn't understand well what you are trying to do.
P.S. I'm developing a 3D Engine in TMT Pascal Multitarget 4, with software rendering only, with per pixel lighting system with raytraced shadows in real-time. It is in beta phase and i will post it soon
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.