Blender  V2.59
ntl_lighting.cpp
Go to the documentation of this file.
00001 
00004 /******************************************************************************
00005  *
00006  * El'Beem - Free Surface Fluid Simulation with the Lattice Boltzmann Method
00007  * Copyright 2003-2006 Nils Thuerey
00008  *
00009  * a light object
00010  *
00011  *****************************************************************************/
00012 
00013 
00014 #include "ntl_lighting.h"
00015 #include "ntl_ray.h"
00016 #include "ntl_world.h"
00017 
00018 
00019 /******************************************************************************
00020  * Default Constructor
00021  *****************************************************************************/
00022 ntlLightObject::ntlLightObject(ntlRenderGlobals *glob) :
00023         mpGlob( glob ),
00024         mActive( 1 ),
00025         mCastShadows( 1 ),
00026         mcColor( ntlColor(1.0) ),
00027         mvPosition( ntlVec3Gfx(0.0) )
00028 {
00029         // nothing to do...
00030 }
00031 
00032 
00033 /******************************************************************************
00034  * Constructor with parameters 
00035  *****************************************************************************/
00036 ntlLightObject::ntlLightObject(ntlRenderGlobals *glob, const ntlColor& col) :
00037         mpGlob( glob ),
00038         mActive( 1 ),
00039         mCastShadows( 1 ),
00040         mcColor( col )
00041 {
00042         // nothing to do...
00043 }
00044 
00045 
00046 
00047 /******************************************************************************
00048  * Destructor
00049  *****************************************************************************/
00050 ntlLightObject::~ntlLightObject()
00051 {
00052         // nothing to do...
00053 }
00054 
00055 
00056 
00057 /******************************************************************************
00058  * Determine color contribution of a lightsource (Phong model)
00059  * Specular part is returned in seperate parameter and added later
00060  *****************************************************************************/
00061 const ntlColor
00062 ntlLightObject::getShadedColor(const ntlRay &reflectedRay, const ntlVec3Gfx lightDir,
00063                                                                                                                          ntlMaterial *surf, ntlColor &highlight) const
00064 {
00065   gfxReal ldot = dot(lightDir, reflectedRay.getNormal()); /* equals cos( angle(L,N) ) */
00066   ntlColor reflected_color = ntlColor(0.0);  /* adds up to total reflected color */
00067         if(mpGlob->getDebugOut() > 5) errorOut("Lighting dir:"<<lightDir<<"  norm:"<<reflectedRay.getNormal()<<"  "<<ldot );
00068 
00069   /* lambertian reflection model */
00070   if (ldot > 0.0) {
00071                 //ldot *= -1.0;
00072     reflected_color += surf->getDiffuseRefl() * (getColor() * ldot );
00073 
00074     /* specular part */
00075     /* specular reflection only makes sense, when the light is facing the surface,
00076        as the highlight is supposed to be a reflection of the lightsource, it cannot
00077        be reflected on surfaces with ldot<=0, as this means the arc between light 
00078        and normal is more than 90 degrees. If this isn't done, ugly moiree patterns appear
00079        in the highlights, and refractions have strange patterns due to highlights on the
00080        inside of the surface */
00081     gfxReal spec = dot(reflectedRay.getDirection(), lightDir); // equals cos( angle(R,L) )
00082     if((spec > 0.0) && (surf->getSpecular()>0)) {
00083       spec = pow( spec, surf->getSpecExponent() ); /* phong exponent */
00084       highlight += getColor() * surf->getSpecular() * spec;
00085                         //errorOut( " "<< surf->getName() <<" S "<<highlight<<" "<<spec<<" "<<surf->getSpecular()<<" "<<surf->getSpecExponent() );
00086     }
00087 
00088   }
00089 
00090   return ntlColor(reflected_color);
00091 }
00092 
00093 
00094 // omni light implementation
00095 
00096 
00097 /******************************************************************************
00098  *! prepare shadow maps if necessary 
00099  *****************************************************************************/
00100 void ntlLightObject::prepare( bool doCaustics )
00101 {
00102         doCaustics = false; // unused
00103         if(!mActive) { return; }
00104 }
00105 
00106 
00107 /******************************************************************************
00108  * Illuminate the given point on an object
00109  *****************************************************************************/
00110 ntlColor ntlLightObject::illuminatePoint(ntlRay &reflectedRay, ntlGeometryObject *closest,
00111                                                                                                                                                          ntlColor &highlight )
00112 {
00113         /* is this light active? */
00114         if(!mActive) { return ntlColor(0.0); }
00115 
00116         gfxReal visibility = 1.0;   // how much of light is visible
00117         ntlVec3Gfx intersectionPos = reflectedRay.getOrigin();
00118         ntlColor current_color = ntlColor(0.0);
00119         ntlMaterial *clossurf = closest->getMaterial();
00120 
00121         ntlVec3Gfx lightDir = (mvPosition - intersectionPos);
00122         gfxReal lightDirNorm = normalize(lightDir);
00123 
00124         // where is the lightsource ?
00125         ntlRay rayOfLight(intersectionPos, lightDir, 0, 1.0, mpGlob );
00126         
00127         if( (1) && (mCastShadows)&&(closest->getReceiveShadows()) ) {
00128                 ntlTriangle *tri;
00129                 ntlVec3Gfx triNormal;
00130                 gfxReal trit;
00131                 mpGlob->getRenderScene()->intersectScene(rayOfLight, trit, triNormal, tri, TRI_CASTSHADOWS);
00132                 if(( trit>0 )&&( trit<lightDirNorm )) visibility = 0.0;
00133                 if(mpGlob->getDebugOut() > 5) errorOut("Omni lighting with "<<visibility );
00134         }
00135         
00136         /* is light partly visible ? */
00137 //? visibility=1.;
00138         if (visibility>0.0) {
00139                 ntlColor highTemp(0.0); // temporary highlight color to multiply highTemp with offFac
00140                 current_color = getShadedColor(reflectedRay, lightDir, clossurf, highTemp) * visibility;
00141                 highlight += highTemp * visibility;
00142                 if(mpGlob->getDebugOut() > 5) errorOut("Omni lighting color "<<current_color );
00143         }
00144         return current_color;
00145 }
00146 
00147 
00148 
00149 /******************************************************************************
00150  * Default constructor
00151  *****************************************************************************/
00152 ntlMaterial::ntlMaterial( void ) : 
00153         mName( "default" ),
00154   mDiffuseRefl(0.5,0.5,0.5),  mAmbientRefl(0.0,0.0,0.0),
00155   mSpecular(0.0), mSpecExponent(0.0), mMirror(0.0),
00156   mTransparence(0.0), mRefracIndex(1.05), mTransAdditive(0.0), mTransAttCol(0.0),
00157         mFresnel( 0 ) { 
00158   // just do default init...
00159 }
00160 
00161 
00162 
00163 /******************************************************************************
00164  * Init constructor
00165  *****************************************************************************/
00166 ntlMaterial::ntlMaterial( string name,
00167                                                                                                         const ntlColor& Ref, const ntlColor& Amb,
00168                                                                                                         gfxReal Spec, gfxReal SpecEx, gfxReal Mirr,
00169                                                                                                         gfxReal Trans, gfxReal Refrac, gfxReal TAdd,
00170                                                                                                         const ntlColor& Att, int fres)
00171 {
00172         mName                                   = name;
00173         mDiffuseRefl  = Ref;
00174         mAmbientRefl  = Amb;
00175         mSpecular     = Spec;
00176         mSpecExponent = SpecEx;
00177         mMirror       = Mirr;
00178         mTransparence = Trans;
00179         mRefracIndex  = Refrac;
00180         mTransAdditive = TAdd;
00181         mTransAttCol   = Att;
00182         mFresnel                        = fres;
00183 }
00184