# Home    # nevrax.com   
Nevrax
Nevrax.org
#News
#Mailing-list
#Documentation
#CVS
#Bugs
#License
Docs
 
Documentation  
Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages   Search  

light.cpp

Go to the documentation of this file.
00001 
00007 /* Copyright, 2001 Nevrax Ltd.
00008  *
00009  * This file is part of NEVRAX NEL.
00010  * NEVRAX NEL is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2, or (at your option)
00013  * any later version.
00014 
00015  * NEVRAX NEL is distributed in the hope that it will be useful, but
00016  * WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00018  * General Public License for more details.
00019 
00020  * You should have received a copy of the GNU General Public License
00021  * along with NEVRAX NEL; see the file COPYING. If not, write to the
00022  * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00023  * MA 02111-1307, USA.
00024  */
00025 
00026 #include "std3d.h"
00027 
00028 #include "3d/light.h"
00029 
00030 using namespace NLMISC;
00031 
00032 namespace NL3D 
00033 {
00034 
00035 // ***************************************************************************
00036 
00037 void CLight::setupDirectional (const CRGBA& ambiant, const CRGBA& diffuse, const CRGBA& specular, const CVector& direction,
00038                                                            float constant, float linear, float quadratic)
00039 {
00040         // Set the mode
00041         setMode (DirectionalLight);
00042 
00043         // Set the colors
00044         setAmbiant (ambiant);
00045         setDiffuse (diffuse);
00046         setSpecular (specular);
00047 
00048         // Set the direction
00049         setDirection (direction);
00050 
00051         // Set attenuation
00052         setConstantAttenuation (constant);
00053         setLinearAttenuation (linear);
00054         setQuadraticAttenuation (quadratic);
00055 }
00056         
00057 // ***************************************************************************
00058 
00059 void CLight::setupPointLight (const CRGBA& ambiant, const CRGBA& diffuse, const CRGBA& specular, const CVector& position, 
00060                                                 const CVector& direction, float constant, float linear, float quadratic)
00061 {
00062         // Set the mode
00063         setMode (PointLight);
00064 
00065         // Set the colors
00066         setAmbiant (ambiant);
00067         setDiffuse (diffuse);
00068         setSpecular (specular);
00069 
00070         // Set the position and direction
00071         setPosition (position);
00072         setDirection (direction);
00073 
00074         // Set attenuation
00075         setConstantAttenuation (constant);
00076         setLinearAttenuation (linear);
00077         setQuadraticAttenuation (quadratic);
00078 }
00079 
00080 // ***************************************************************************
00081 
00082 void CLight::setupSpotLight (const CRGBA& ambiant, const CRGBA& diffuse, const CRGBA& specular, const CVector& position, 
00083                                                 const CVector& direction, float exponent, float cutoff, float constant, float linear, float quadratic)
00084 {
00085         // Set the mode
00086         setMode (SpotLight);
00087 
00088         // Set the colors
00089         setAmbiant (ambiant);
00090         setDiffuse (diffuse);
00091         setSpecular (specular);
00092 
00093         // Set the position and direction
00094         setPosition (position);
00095         setDirection (direction);
00096 
00097         // Set spotlight parameters
00098         setExponent (exponent);
00099         setCutoff (cutoff);
00100 
00101         // Set attenuation
00102         setConstantAttenuation (constant);
00103         setLinearAttenuation (linear);
00104         setQuadraticAttenuation (quadratic);
00105 }
00106 
00107 // ***************************************************************************
00108 
00109 void CLight::setupAttenuation (float farAttenuationBegin, float farAttenuationEnd)
00110 {
00111         _ConstantAttenuation=1.f;
00112         _QuadraticAttenuation=(float)(0.1/(0.9*farAttenuationBegin*farAttenuationBegin));
00113         _LinearAttenuation=(float)(0.1/(0.9*farAttenuationBegin));
00114 
00115         // blend factor
00116         float factor = (0.1f*_LinearAttenuation*farAttenuationEnd-0.1f*_QuadraticAttenuation*farAttenuationEnd*farAttenuationEnd);
00117 
00118         if (factor == 0.0f)
00119                 factor = 0.0001f;
00120         factor = (0.9f-0.1f*_QuadraticAttenuation*farAttenuationEnd*farAttenuationEnd)/factor;
00121 
00122         if ((factor<0.f)||(factor>1.f))
00123         {
00124                 // Better factor
00125                 float d0_1Lin=1.f / ( _ConstantAttenuation + _LinearAttenuation*farAttenuationEnd );
00126                 float d0_1Quad=1.f / ( _ConstantAttenuation + _QuadraticAttenuation*farAttenuationEnd*farAttenuationEnd );
00127 
00128                 // Better
00129                 if (fabs (d0_1Lin-0.1f)<fabs (d0_1Quad-0.1f))
00130                         _QuadraticAttenuation=0.f;
00131                 else
00132                         _LinearAttenuation=0.f;
00133         }
00134         else
00135         {
00136                 _LinearAttenuation*=factor;
00137                 _QuadraticAttenuation*=(1.f-factor);
00138         }
00139 
00140 #ifdef NL_DEBUG
00141         // Should be near previous result
00142         float d1_0=1.f / ( _ConstantAttenuation + _LinearAttenuation*0.f + _QuadraticAttenuation*0.f*0.f );
00143         float d0_9=1.f / ( _ConstantAttenuation + _LinearAttenuation*farAttenuationBegin + _QuadraticAttenuation*farAttenuationBegin*farAttenuationBegin );
00144         float d0_1=1.f / ( _ConstantAttenuation + _LinearAttenuation*farAttenuationEnd + _QuadraticAttenuation*farAttenuationEnd*farAttenuationEnd );
00145 #endif // NL_DEBUG*/
00146 }
00147 
00148 // ***************************************************************************
00149 
00150 void CLight::setupSpotExponent (float hotSpotAngle)
00151 {
00152         float divid=(float)log (cos (hotSpotAngle));
00153         if (divid==0.f)
00154                 divid=0.0001f;
00155         setExponent ((float)(log (0.9)/divid));
00156 }
00157 
00158 // ***************************************************************************
00159 
00160 void CLight::setNoAttenuation ()
00161 {
00162         _ConstantAttenuation=1.f;
00163         _QuadraticAttenuation=0.f;
00164         _LinearAttenuation=0.f;
00165 }
00166 
00167 // ***************************************************************************
00168 
00169 } // NL3D