Tizen 2.1 base
[framework/osp/uifw.git] / src / ui / effects / inc / FUiEffects_RendererSpotLight.h
1 //
2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Flora License, Version 1.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://floralicense.org/license/
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an AS IS BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 /**
18  * @file        FUiEffects_RendererSpotLight.h
19  * @brief               This is the header file for the SpotLight class
20  */
21
22 #ifndef _FUI_EFFECTS_INTERNAL_RENDERER_SPOTLIGHT_H_
23 #define _FUI_EFFECTS_INTERNAL_RENDERER_SPOTLIGHT_H_
24
25 #include <FBaseSysLog.h>
26
27 #include <renderer/engine-model/FUiEffects_RendererEngineModelIntPropertyHolder.h>
28 #include <renderer/engine-model/FUiEffects_RendererEngineModelFloatPropertyHolder.h>
29 #include <renderer/engine-model/FUiEffects_RendererEngineModelVector3fPropertyHolder.h>
30 #include <utils/FUiEffects_Utils.h>
31
32 namespace Tizen { namespace Ui { namespace Effects { namespace _Renderer
33 {
34
35 /**
36  * @class       SpotLight
37  * @brief       This class contains data for spot light in scene
38  *
39  */
40 class SpotLight
41 {
42 public:
43
44         /**
45          * Constructor
46          *
47          *  * @param [in]       attenuation                             Global attenuation pointer
48          *
49          * @remark              Sets default values for members
50          */
51         SpotLight(System::FloatPtr attenuation)
52         {
53                 result r = GetLastResult();
54                 SysTryReturnVoidResult(NID_UI_EFFECT, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
55                 SysTryReturnVoidResult(NID_UI_EFFECT, System::GetImpl(attenuation) != null, E_INVALID_ARG, "[E_INVALID_ARG] PointLight attenuation equals null");
56
57                 __position = EngineModel::Vector3fPropertyHolderPtr(new (std::nothrow) EngineModel::Vector3fPropertyHolder);
58                 __direction = EngineModel::Vector3fPropertyHolderPtr(new (std::nothrow) EngineModel::Vector3fPropertyHolder);
59                 __k1 = EngineModel::FloatPropertyHolderPtr(new (std::nothrow) EngineModel::FloatPropertyHolder);
60                 __k2 = EngineModel::FloatPropertyHolderPtr(new (std::nothrow) EngineModel::FloatPropertyHolder);
61                 __colorI = EngineModel::Vector3fPropertyHolderPtr(new (std::nothrow) EngineModel::Vector3fPropertyHolder);
62                 __Rk = EngineModel::FloatPropertyHolderPtr(new (std::nothrow) EngineModel::FloatPropertyHolder);
63
64                 r = GetLastResult();
65                 SysTryReturnVoidResult(NID_UI_EFFECT, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
66                 SysTryReturnVoidResult(NID_UI_EFFECT, System::GetImpl(__position) != null, E_OUT_OF_MEMORY, _UiEffectError::OUT_OF_MEMORY);
67                 SysTryReturnVoidResult(NID_UI_EFFECT, System::GetImpl(__direction) != null, E_OUT_OF_MEMORY, _UiEffectError::OUT_OF_MEMORY);
68                 SysTryReturnVoidResult(NID_UI_EFFECT, System::GetImpl(__k1) != null, E_OUT_OF_MEMORY, _UiEffectError::OUT_OF_MEMORY);
69                 SysTryReturnVoidResult(NID_UI_EFFECT, System::GetImpl(__k2) != null, E_OUT_OF_MEMORY, _UiEffectError::OUT_OF_MEMORY);
70                 SysTryReturnVoidResult(NID_UI_EFFECT, System::GetImpl(__colorI) != null, E_OUT_OF_MEMORY, _UiEffectError::OUT_OF_MEMORY);
71                 SysTryReturnVoidResult(NID_UI_EFFECT, System::GetImpl(__Rk) != null, E_OUT_OF_MEMORY, _UiEffectError::OUT_OF_MEMORY);
72
73                 __position->Set(0.f, 0.f, 0.f);
74                 __target.set(0.f, 0.f, -1.f);
75                 __direction->Set(0.f, 0.f, -1.f);
76                 __halfOpeningAngle = 0.6f;
77                 __fadeOutAngle = 0.1f;
78                 __color.set(1.f, 0.f, 0.f);
79                 __intensity = 1.f;
80                 __enabled = true;
81                 __attenuation = attenuation;
82                 __maxCi = 0.0f;
83
84                 UpdateColorI();
85                 UpdateK1K2();
86         }
87
88         /**
89         * Sets position property
90         *
91         * @param [in]   position                                New position
92         *
93         * @return               void
94         */
95         void SetPosition(const Tizen::Ui::Effects::_Utils::Vec3f& position)
96         {
97                 __position->Set(position.x, position.y, position.z);
98                 __direction->Set(Math::Vector3f(__target.x - __position->Get().X(), __target.y - __position->Get().Y(), __target.z - __position->Get().Z()).Normalize());
99
100                 return;
101         }
102
103         /**
104         * Sets target property
105         *
106         * @param [in]   target                          New target
107         *
108         * @return               void
109         */
110         void SetTarget(const Tizen::Ui::Effects::_Utils::Vec3f& target)
111         {
112                 __target = target;
113                 __direction->Set(Math::Vector3f(__target.x - __position->Get().X(), __target.y - __position->Get().Y(), __target.z - __position->Get().Z()).Normalize());
114
115                 return;
116         }
117
118         /**
119         * Sets opening angle value
120         *
121         * @param [in]   openingAngle                            New opening angle
122         *
123         * @return               void
124         */
125         void SetOpeningAngle(float openingAngle)
126         {
127                 __halfOpeningAngle = openingAngle * 0.5f;
128                 UpdateK1K2();
129
130                 return;
131         }
132
133         /**
134         * Sets fadeout angle value
135         *
136         * @param [in]   fadeOutAngle                            New fadeout angle
137         *
138         * @return               void
139         */
140         void SetFadeOutAngle(float fadeOutAngle)
141         {
142                 __fadeOutAngle = fadeOutAngle;
143                 UpdateK1K2();
144
145                 return;
146         }
147
148         /**
149         * Sets color property
150         *
151         * @param [in]   color                           New color value
152         *
153         * @return               void
154         */
155         void SetColor(const Tizen::Ui::Effects::_Utils::Vec3f& color)
156         {
157                 __color = color;
158                 UpdateColorI();
159
160                 return;
161         }
162
163         /**
164         * Sets intensity property
165         *
166         * @param [in]   intensity                               New intensity value
167         *
168         * @return               void
169         */
170         void SetIntensity(float intensity)
171         {
172                 __intensity = intensity;
173                 UpdateColorI();
174
175                 return;
176         }
177
178         /**
179         * Sets enabled value
180         *
181         * @param [in]   enabled                         New enabled value
182         *
183         * @return               void
184         */
185         void SetEnabled(bool enabled)
186         {
187                 __enabled = enabled;
188
189                 return;
190         }
191
192         /**
193          * Gets position property
194          *
195          * @return              position property
196          */
197         EngineModel::Vector3fPropertyHolderPtr GetPositionHolder(void) const
198         {
199                 return __position;
200         }
201
202         /**
203         * Gets direction vector property
204         *
205         * @return               direction vector property
206         */
207         EngineModel::Vector3fPropertyHolderPtr GetDirectionHolder(void) const
208         {
209                 return __direction;
210         }
211
212         /**
213         * Gets k1 property
214         *
215         * @return               k1 property
216         */
217         EngineModel::FloatPropertyHolderPtr GetK1Holder(void) const
218         {
219                 return __k1;
220         }
221
222         /**
223         * Gets k2 property
224         *
225         * @return               k2 property
226         */
227         EngineModel::FloatPropertyHolderPtr GetK2Holder(void) const
228         {
229                 return __k2;
230         }
231
232         /**
233         * Gets color*intensity value property
234         *
235         * @return               color*intensity value property
236         */
237         EngineModel::Vector3fPropertyHolderPtr GetColorIHolder(void) const
238         {
239                 return __colorI;
240         }
241
242         /**
243         * Gets enabled value
244         *
245         * @return               enabled value
246         */
247         bool GetEnabled(void) const
248         {
249                 return __enabled;
250         }
251
252         /**
253         * Gets Rk value holder
254         *
255         * @return               Rk value holder
256         */
257         EngineModel::FloatPropertyHolderPtr GetRkHolder(void) const
258         {
259                 return __Rk;
260         }
261
262         /**
263         * Does source emitt any light?
264         *
265         * @return               true if color*intensity = 0
266         */
267         bool IsBlack(void) const
268         {
269                 return __maxCi <= std::numeric_limits<float>::epsilon() || __Rk->Get() <= std::numeric_limits<float>::epsilon();
270         }
271
272         /**
273         * Updates precalculated values related to Attenuation
274         *
275         * @return               void
276         */
277         void UpdateAttenuation(void)
278         {
279                 UpdateRk();
280         }
281
282 private:
283
284         /**
285          * Hidden copy constructor
286          */
287         SpotLight(const SpotLight &rhs);
288
289
290         /**
291          * Hidden assignment operator
292          */
293         SpotLight& operator=(const SpotLight &rhs);
294
295         /**
296         * Calculates parameters for shader
297         *
298         * @return               void
299         */
300         void UpdateK1K2(void)
301         {
302                 __k1->Set(Math::EffectsCos(__halfOpeningAngle + __fadeOutAngle));
303                 __k2->Set(1.0f / (Math::EffectsCos(__halfOpeningAngle) - __k1->Get()));
304
305                 return;
306         }
307
308         /**
309         * Sets colorI property which is premultiplied color x intensity for use in shader. @n
310         * Also calculates 1 / Rmax, where Rmax is maxim distance light value is >= 1/255
311         *
312         * @return               void
313         */
314         void UpdateColorI(void)
315         {
316                 __colorI->Set(__color.x * __intensity, __color.y * __intensity, __color.z * __intensity);
317                 __maxCi = Math::EffectsAbs(__intensity * Math::EffectsMax(__color.x, Math::EffectsMax(__color.y, __color.z)));
318
319                 UpdateRk();
320
321                 return;
322         }
323
324         /**
325         * Updates precalculated values related to light emission radius
326         *
327         * @return               void
328         */
329         void UpdateRk(void)
330         {
331                 if(__maxCi > std::numeric_limits<float>::epsilon())
332                 {
333                         __Rk->Set(Math::EffectsSqrt(*__attenuation * (1.0f/255.f) / __maxCi));
334                 }
335         }
336
337 private:
338         Tizen::Ui::Effects::_Utils::Vec3f __target;
339         float __halfOpeningAngle;
340         float __fadeOutAngle;
341         _Utils::Vec3f __color;
342         float __intensity;
343         bool __enabled;
344         float __maxCi;
345
346         EngineModel::Vector3fPropertyHolderPtr __position;
347         EngineModel::Vector3fPropertyHolderPtr __direction;
348         EngineModel::FloatPropertyHolderPtr __k1;                               /**< precalculated parameter = cos(OpeningAngle/2 + FadeOutAngle) */
349         EngineModel::FloatPropertyHolderPtr __k2;                               /**< precalculated parameter = 1 / (cos(OpeningAngle/2) - __k1) */
350         EngineModel::Vector3fPropertyHolderPtr __colorI;                /**< __color * __intensity */
351         EngineModel::FloatPropertyHolderPtr __Rk;
352         System::FloatPtr __attenuation;
353 }; // SpotLight
354
355 typedef System::SmartPtr<SpotLight> SpotLightPtr;
356
357 } } } } // Tizen::Ui::Effects::_EffectRenderer
358
359 #endif //_FUI_EFFECTS_INTERNAL_RENDERER_DIRECTIONALLIGHT_H_