00001
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
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
00041 setMode (DirectionalLight);
00042
00043
00044 setAmbiant (ambiant);
00045 setDiffuse (diffuse);
00046 setSpecular (specular);
00047
00048
00049 setDirection (direction);
00050
00051
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
00063 setMode (PointLight);
00064
00065
00066 setAmbiant (ambiant);
00067 setDiffuse (diffuse);
00068 setSpecular (specular);
00069
00070
00071 setPosition (position);
00072 setDirection (direction);
00073
00074
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
00086 setMode (SpotLight);
00087
00088
00089 setAmbiant (ambiant);
00090 setDiffuse (diffuse);
00091 setSpecular (specular);
00092
00093
00094 setPosition (position);
00095 setDirection (direction);
00096
00097
00098 setExponent (exponent);
00099 setCutoff (cutoff);
00100
00101
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
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
00125 float d0_1Lin=1.f / ( _ConstantAttenuation + _LinearAttenuation*farAttenuationEnd );
00126 float d0_1Quad=1.f / ( _ConstantAttenuation + _QuadraticAttenuation*farAttenuationEnd*farAttenuationEnd );
00127
00128
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
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 }