1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include "nel/3d/light.h"
21 using namespace NLMISC
;
30 // ***************************************************************************
32 void CLight::setupDirectional (const CRGBA
& ambiant
, const CRGBA
& diffuse
, const CRGBA
& specular
, const CVector
& direction
,
33 float constant
, float linear
, float quadratic
)
36 setMode (DirectionalLight
);
41 setSpecular (specular
);
44 setDirection (direction
);
47 setConstantAttenuation (constant
);
48 setLinearAttenuation (linear
);
49 setQuadraticAttenuation (quadratic
);
51 // Dummy to avoid uninit data, and problems of cache
52 setPosition(CVector::Null
);
57 // ***************************************************************************
59 void CLight::setupPointLight (const CRGBA
& ambiant
, const CRGBA
& diffuse
, const CRGBA
& specular
, const CVector
& position
,
60 const CVector
& direction
, float constant
, float linear
, float quadratic
)
68 setSpecular (specular
);
70 // Set the position and direction
71 setPosition (position
);
72 setDirection (direction
);
75 setConstantAttenuation (constant
);
76 setLinearAttenuation (linear
);
77 setQuadraticAttenuation (quadratic
);
79 // Dummy to avoid uninit data, and problems of cache
84 // ***************************************************************************
86 void CLight::setupSpotLight (const CRGBA
& ambiant
, const CRGBA
& diffuse
, const CRGBA
& specular
, const CVector
& position
,
87 const CVector
& direction
, float exponent
, float cutoff
, float constant
, float linear
, float quadratic
)
95 setSpecular (specular
);
97 // Set the position and direction
98 setPosition (position
);
99 setDirection (direction
);
101 // Set spotlight parameters
102 setExponent (exponent
);
106 setConstantAttenuation (constant
);
107 setLinearAttenuation (linear
);
108 setQuadraticAttenuation (quadratic
);
111 // ***************************************************************************
112 void CLight::setupAttenuation (float farAttenuationBegin
, float farAttenuationEnd
)
114 /* Yoyo: I changed this method because it did not work well for me
115 The most important in a light is its farAttenuationEnd (anything beyond should not be lighted)
116 The old compute had too smooth decrease, regarding this (at farAttenuationEnd, the light could be
117 attenuated by a factor of 0.7 (instead of 0) and slowly decreased).
121 if(farAttenuationEnd
<=0)
123 _ConstantAttenuation
= 1000000;
124 _LinearAttenuation
= 0;
125 _QuadraticAttenuation
= 0;
129 // The following factors are "it feels good for farAttenuationBegin=0/farAttenuationEnd=1" factors.
130 // btw, at r=farAttenuationEnd=1, att= 1/11 ~= 0.
131 const float constant
= 1.0f
;
132 const float linear
= 0.f
;
133 const float quadratic
= 10.0f
;
136 With GL/D3D 'att=1/(c+l*r+q*r2)' formula, I think it is impossible to simulate correctly
137 farAttenuationBegin (very big decrase if for instance farAttenuationBegin is near farAttenuationEnd),
138 hence I simulate it very badly by multiplying the farAttenuationEnd by some factor
141 if(farAttenuationBegin
/farAttenuationEnd
>0.5f
)
143 else if(farAttenuationBegin
>0)
144 factor
= 1.f
+ 2*farAttenuationBegin
/farAttenuationEnd
;
145 farAttenuationEnd
*= factor
;
147 // scale according to farAttenuationEnd.
148 _ConstantAttenuation
= constant
;
149 _LinearAttenuation
= linear
/farAttenuationEnd
;
150 _QuadraticAttenuation
= quadratic
/sqr(farAttenuationEnd
);
154 // ***************************************************************************
156 void CLight::setupSpotExponent (float hotSpotAngle
)
158 float divid
=(float)log (cos (hotSpotAngle
));
161 setExponent ((float)(log (0.9)/divid
));
164 // ***************************************************************************
166 void CLight::setNoAttenuation ()
168 _ConstantAttenuation
=1.f
;
169 _QuadraticAttenuation
=0.f
;
170 _LinearAttenuation
=0.f
;
173 // ***************************************************************************