Phong Shading

Started by
9 comments, last by Willywig 22 years, 5 months ago
// CROSSES FINGERS HOPS THIS POSTS OKAY...


// if the ray from the light hits the current object we ARE NOT IN THE SHADOW
// and we have to take into account the effects of the light.
if( Light_Results->HitObject == Ray_Results->HitObject )
{

// When we made the intersection from the original ray and out Ray Type was RAY_LIGHT we set
// a pointer to the light source.. get that pointer now.
Base_Light* CurrentLight = (*Ray_itor)->SourceLight;

// make sure the pointer is valid.
assert( Current_Light );


// Now we need to Make normalized 3 vectors.

Vector3f Int_To_eye; // Intersection Point To The Eye
Vector3f Int_To_lit; // Intersection Point to The Light we are current working with;
Vector3f lit_To_Int; // Light to the Intersection Point


// Make Intersection Point to Eye
Int_To_eye = (*Ray_Results->Casted_Ray->Ray_Origin) - (*Ray_Results->IntersectionPt);
Int_To_eye.Normalise();

// Make Intersection Point to Light
Int_To_lit = (*CurrentLight->GetPosition()) - (*Ray_Results->IntersectionPt);
Int_To_lit.Normalise();

// Make Light to the Intersection Point
lit_To_Int = (*Ray_Results->IntersectionPt) - (*CurrentLight->GetPosition());
lit_To_Int.Normalise();



// Find the Dot Product of the Normal and the Ray from the Intersection Point To the Eye
float n_dot_eye = (Ray_Results->Normal)->Dot( Int_To_eye );

// Find the Dot Product of the Normal and the Ray from the Light To the Intersection Point
float n_dot_light = (Ray_Results->Normal)->Dot( lit_To_Int );

// Check to see if both the EYE and the Light are on the same side of the object.
if((n_dot_eye > 0 && n_dot_light < 0) || (n_dot_eye < 0 && n_dot_light > 0))
{
// By adding the Int_To_lit & Int_To_Eye we are in effect getting the magnitude
// of the angle between them. The Smaller the angle between them the larger
// the result vector is. (also not that if you divde this by 2 you get the a
// point halfway between the two vectors.. thus it is called the ''Half'' vector!
Vector3f Half;
Half = Int_To_lit + Int_To_eye;

// Normalize it
Half.Normalise();

// We now take the Dot Product of the Half to see how close this resultant
// vector is to the Normal.. the close to the normal the brigher that point is.
float Spec = (*Ray_Results->Normal).Dot( Half );

// recap.. the closer the Eye and Light angles are together the brighter the
// point on the object is.. and also the closer that is to the normal the
// brighter the point is as well.


// take the abs value.. (need to verify we still need this..)
Spec = fabs( Spec );

// Use the Material Properties to get the shinness. (there are better light models out here..)
Spec = CurrentMaterial->GetShine_1() * pow( Spec, CurrentMaterial->GetShine_2() );

// Add the light & specular properties to the point.
CurrentColor.Red += MaterialColor->Red * Spec * CurrentLight->GetColor()->Red;
CurrentColor.Green += MaterialColor->Green * Spec * CurrentLight->GetColor()->Green;
CurrentColor.Blue += MaterialColor->Blue * Spec * CurrentLight->GetColor()->Blue;


// Add in the diffuse lighting to the object.

// The closer the Ray from the Intersection Point to the Normal the brighter the point.
float Diffuse = (*Ray_Results->Normal).Dot( (Int_To_lit) );
Diffuse = fabs( Diffuse );

CurrentColor.Red += MaterialColor->Red * Diffuse * CurrentLight->GetColor()->Red;
CurrentColor.Green += MaterialColor->Green * Diffuse * CurrentLight->GetColor()->Green;
CurrentColor.Blue += MaterialColor->Blue * Diffuse * CurrentLight->GetColor()->Blue;

}
}
}

This topic is closed to new replies.

Advertisement