Tizen 2.1 base
[framework/osp/uifw.git] / src / ui / effects / inc / FUiEffects_RendererPointLight.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_RendererPointLight.h
19  * @brief               This is the header file for the PointLight class
20  */
21
22 #ifndef _FUI_EFFECTS_INTERNAL_RENDERER_POINTLIGHT_H_
23 #define _FUI_EFFECTS_INTERNAL_RENDERER_POINTLIGHT_H_
24
25 #include <FBaseSysLog.h>
26
27 #include <limits>
28
29 #include <renderer/engine-model/FUiEffects_RendererEngineModelIntPropertyHolder.h>
30 #include <renderer/engine-model/FUiEffects_RendererEngineModelFloatPropertyHolder.h>
31 #include <renderer/engine-model/FUiEffects_RendererEngineModelVector3fPropertyHolder.h>
32 #include <utils/FUiEffects_Utils.h>
33
34 namespace Tizen { namespace Ui { namespace Effects { namespace _Renderer
35 {
36
37 /**
38  * @class       PointLight
39  * @brief       This class contains data for point light in scene
40  *
41  */
42 class PointLight
43 {
44 public:
45
46         /**
47          * Constructor
48          *
49          * @param [in]  attenuation                             Global attenuation pointer
50          *
51          * @remark              Sets default values for members
52          */
53         PointLight(System::FloatPtr attenuation)
54         {
55                 result r = GetLastResult();
56                 SysTryReturnVoidResult(NID_UI_EFFECT, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
57                 SysTryReturnVoidResult(NID_UI_EFFECT, System::GetImpl(attenuation) != null, E_INVALID_ARG, "[E_INVALID_ARG] PointLight attenuation equals null");
58
59                 __position = EngineModel::Vector3fPropertyHolderPtr(new (std::nothrow) EngineModel::Vector3fPropertyHolder);
60                 __colorI = EngineModel::Vector3fPropertyHolderPtr(new (std::nothrow) EngineModel::Vector3fPropertyHolder);
61                 __Rk = EngineModel::FloatPropertyHolderPtr(new (std::nothrow) EngineModel::FloatPropertyHolder);
62
63                 r = GetLastResult();
64                 SysTryReturnVoidResult(NID_UI_EFFECT, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
65                 SysTryReturnVoidResult(NID_UI_EFFECT, System::GetImpl(__position) != null, E_OUT_OF_MEMORY, _UiEffectError::OUT_OF_MEMORY);
66                 SysTryReturnVoidResult(NID_UI_EFFECT, System::GetImpl(__colorI) != null, E_OUT_OF_MEMORY, _UiEffectError::OUT_OF_MEMORY);
67                 SysTryReturnVoidResult(NID_UI_EFFECT, System::GetImpl(__Rk) != null, E_OUT_OF_MEMORY, _UiEffectError::OUT_OF_MEMORY);
68
69                 __position->Set(0.f, 0.f, 0.f);
70                 __color.set(1.f, 0.f, 0.f);
71                 __intensity = 1.f;
72                 __enabled = true;
73                 __attenuation = attenuation;
74                 __maxCi = 0.0f;
75
76                 UpdateColorI();
77         }
78
79         /**
80         * Sets position property
81         *
82         * @param [in]   position                                New position
83         *
84         * @return               void
85         */
86         void SetPosition(const Tizen::Ui::Effects::_Utils::Vec3f& position)
87         {
88                 __position->Set(position.x, position.y, position.z);
89
90                 return;
91         }
92
93         /**
94         * Sets color property
95         *
96         * @param [in]   color                           New color value
97         *
98         * @return               void
99         */
100         void SetColor(const Tizen::Ui::Effects::_Utils::Vec3f& color)
101         {
102                 __color = color;
103                 UpdateColorI();
104
105                 return;
106         }
107
108         /**
109         * Sets intensity property
110         *
111         * @param [in]   intensity                               New intensity value
112         *
113         * @return               void
114         */
115         void SetIntensity(float intensity)
116         {
117                 __intensity = intensity;
118                 UpdateColorI();
119
120                 return;
121         }
122
123         /**
124         * Sets enabled value
125         *
126         * @param [in]   enabled                         New enabled value
127         *
128         * @return               void
129         */
130         void SetEnabled(bool enabled)
131         {
132                 __enabled = enabled;
133
134                 return;
135         }
136
137         /**
138          * Gets position property
139          *
140          * @return              position property
141          */
142         EngineModel::Vector3fPropertyHolderPtr GetPositionHolder(void) const
143         {
144                 return __position;
145         }
146
147         /**
148         * Gets color*intensity value property
149         *
150         * @return               color*intensity value property
151         */
152         EngineModel::Vector3fPropertyHolderPtr GetColorIHolder(void) const
153         {
154                 return __colorI;
155         }
156
157
158         /**
159         * Gets Rk value holder
160         *
161         * @return               Rk value holder
162         */
163         EngineModel::FloatPropertyHolderPtr GetRkHolder(void) const
164         {
165                 return __Rk;
166         }
167
168         /**
169         * Gets enabled value
170         *
171         * @return               enabled value
172         */
173         bool GetEnabled(void) const
174         {
175                 return __enabled;
176         }
177
178         /**
179         * Does source emitt any light?
180         *
181         * @return               true if color*intensity = 0
182         */
183         bool IsBlack(void) const
184         {
185                 return __maxCi <= std::numeric_limits<float>::epsilon() || __Rk->Get() <= std::numeric_limits<float>::epsilon();
186         }
187
188         /**
189         * Updates precalculated values related to Attenuation
190         *
191         * @return               void
192         */
193         void UpdateAttenuation(void)
194         {
195                 UpdateRk();
196         }
197
198 private:
199
200         /**
201          * Hidden copy constructor
202          */
203         PointLight(const PointLight &rhs);
204
205
206         /**
207          * Hidden assignment operator
208          */
209         PointLight& operator=(const PointLight &rhs);
210
211         /**
212         * Sets colorI property which is premultiplied color x intensity for use in shader. @n
213         * Also calculates 1 / Rmax, where Rmax is maxim distance light value is >= 1/255
214         *
215         * @return               void
216         */
217         void UpdateColorI(void)
218         {
219                 __colorI->Set(__color.x * __intensity, __color.y * __intensity, __color.z * __intensity);
220                 __maxCi = Math::EffectsAbs((float)__intensity * Math::EffectsMax(__color.x, Math::EffectsMax(__color.y, __color.z)));
221
222                 UpdateRk();
223
224                 return;
225         }
226
227         /**
228         * Updates precalculated values related to light emission radius
229         *
230         * @return               void
231         */
232         void UpdateRk(void)
233         {
234                 if(__maxCi > std::numeric_limits<float>::epsilon())
235                 {
236                         __Rk->Set(Math::EffectsSqrt(*__attenuation * (1.0f/255.f) / __maxCi));
237                 }
238         }
239
240 private:
241         _Utils::Vec3f __color;
242         float __intensity;
243         bool __enabled;
244         float __maxCi;
245
246         EngineModel::Vector3fPropertyHolderPtr __position;
247         EngineModel::Vector3fPropertyHolderPtr __colorI;                /**<  __color * __intensity */
248         EngineModel::FloatPropertyHolderPtr __Rk;
249         System::FloatPtr __attenuation;
250 }; // PointLight
251
252 typedef System::SmartPtr<PointLight> PointLightPtr;
253
254 } } } } // Tizen::Ui::Effects::_EffectRenderer
255
256 #endif //_FUI_EFFECTS_INTERNAL_RENDERER_POINTLIGHT_H_