1 /*---------------------------------------------------------------------------*
2   Project:  NintendoWare
3   File:     gfx_LightSet.h
4 
5   Copyright (C)2009-2011 Nintendo/HAL Laboratory, Inc.  All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain proprietary
8   information of Nintendo and/or its licensed developers and are protected by
9   national and international copyright laws. They may not be disclosed to third
10   parties or copied or duplicated in any form, in whole or in part, without the
11   prior written consent of Nintendo.
12 
13   The content herein is highly confidential and should be handled accordingly.
14 
15   $Revision: 31311 $
16  *---------------------------------------------------------------------------*/
17 
18 #ifndef NW_GFX_LIGHTSET_H_
19 #define NW_GFX_LIGHTSET_H_
20 
21 #include <nw/gfx/gfx_Common.h>
22 #include <nw/ut/ut_MoveArray.h>
23 #include <nw/gfx/res/gfx_ResSceneEnvironmentSetting.h>
24 #include <nw/gfx/gfx_SceneContext.h>
25 
26 namespace nw
27 {
28 namespace gfx
29 {
30 
31 class AmbientLight;
32 class VertexLight;
33 class FragmentLight;
34 class HemiSphereLight;
35 
36 //! @brief フラグメントライトの配列です。
37 typedef ut::FixedSizeArray<FragmentLight*, LIGHT_COUNT> FixedFragmentLightArray;
38 
39 //---------------------------------------------------------------------------
40 //! @brief        ライトセットを表すクラスです。
41 //---------------------------------------------------------------------------
42 class LightSet : public GfxObject
43 {
44 private:
45     NW_DISALLOW_COPY_AND_ASSIGN(LightSet);
46 
47 public:
48     NW_UT_RUNTIME_TYPEINFO;
49 
50     enum
51     {
52         DEFAULT_MAX_VERTEX_LIGHTS = 4
53     };
54 
55     //! @brief 設定内容です。
56     struct Description
57     {
58         //! @brief コンストラクタです。
DescriptionDescription59         Description()
60             : isFixedSizeMemory(true),
61             maxVertexLights(DEFAULT_MAX_VERTEX_LIGHTS)
62         {}
63 
64         bool isFixedSizeMemory;
65         s32  maxVertexLights;
66     };
67 
68     //----------------------------------------
69     //! @name 作成
70     //@{
71 
72     //! ライトセットを動的に構築するためのクラスです。
73     //!
74     //! IsFixedSizeMemory の初期値は true です。false に変更すると、各種最大数の設定は無視されます。
75     class DynamicBuilder
76     {
77     public:
78         //! コンストラクタです。
DynamicBuilder()79         DynamicBuilder() {}
80 
81         //! デストラクタです。
~DynamicBuilder()82         ~DynamicBuilder() {}
83 
84         //! @brief 生成時以外にもメモリを確保するかどうかのフラグを設定します。
85         //!
86         //!        true を指定すると、生成時のみ固定サイズのメモリ確保を行います。
87         //!
88         //!        false を指定すると、生成時以外にも必要に応じて動的にメモリ確保が行われます。
IsFixedSizeMemory(bool isFixedSizeMemory)89         DynamicBuilder& IsFixedSizeMemory(bool isFixedSizeMemory)
90         {
91             m_Description.isFixedSizeMemory = isFixedSizeMemory;
92             return *this;
93         }
94 
95         //! 設定可能な頂点ライトの数を設定します。
MaxVertexLights(s32 maxVertexLights)96         DynamicBuilder& MaxVertexLights(s32 maxVertexLights)
97         {
98             m_Description.maxVertexLights = maxVertexLights;
99             return *this;
100         }
101 
102         //! @brief        ライトセット生成します。
103         //!
104         //! @param[in]    allocator アロケータです。
105         //!
106         //! @return       生成した頂点ライトを返します。
107         //!
108         LightSet* Create(os::IAllocator* allocator);
109 
110     private:
111         LightSet::Description m_Description;
112     };
113 
114     //! @brief        ライトセットを生成します。
115     //!
116     //! @param[in]    resource リソースです。
117     //! @param[in]    allocator アロケータです。
118     //!
119     //! @return       生成された頂点ライトです。
120     //!
121     static LightSet* Create(
122         ResLightSet resource,
123         os::IAllocator* allocator);
124 
125     //! @details :private
GetMemorySizeInternal(os::MemorySizeCalculator * pSize,ResLightSet resource)126     static void GetMemorySizeInternal(
127         os::MemorySizeCalculator* pSize,
128         ResLightSet resource)
129     {
130         os::MemorySizeCalculator& size = *pSize;
131 
132         // LightSet::Create
133         size += sizeof(LightSet);
134 
135         // CreateEnvironmentArray
136         size += sizeof(VertexLight*) * resource.GetLightsCount();
137     }
138 
139     //@}
140 
141     //----------------------------------------
142     //! @name 取得/設定
143     //@{
144 
145     //! @brief アンビエントライトを設定します。
146     //!
147     //! @param[in] light 設定するアンビエントライトです。
148     //!
SetAmbientLight(AmbientLight * light)149     void SetAmbientLight(AmbientLight* light)
150     {
151         this->m_AmbientLight = light;
152     }
153 
154     //! @brief アンビエントライトを取得します。
155     //!
156     //! @return アンビエントライトを返します。
157     //!
GetAmbientLight()158     AmbientLight* GetAmbientLight()
159     {
160         return this->m_AmbientLight;
161     }
162 
163     //! @brief アンビエントライトを取得します。
164     //!
165     //! @return アンビエントライトを返します。
166     //!
GetAmbientLight()167     const AmbientLight* GetAmbientLight() const
168     {
169         return this->m_AmbientLight;
170     }
171 
172     //! @brief 半球ライトを設定します。
173     //!
174     //! @param[in] light 設定する半球ライトです。
175     //!
SetHemiSphereLight(HemiSphereLight * light)176     void SetHemiSphereLight(HemiSphereLight* light)
177     {
178         this->m_HemiSphereLight = light;
179     }
180 
181     //! @brief 半球ライトを取得します。
182     //!
183     //! @return 半球ライトを返します。
184     //!
GetHemiSphereLight()185     HemiSphereLight* GetHemiSphereLight()
186     {
187         return this->m_HemiSphereLight;
188     }
189 
190     //! @brief 半球ライトを取得します。
191     //!
192     //! @return 半球ライトを返します。
193     //!
GetHemiSphereLight()194     const HemiSphereLight* GetHemiSphereLight() const
195     {
196         return this->m_HemiSphereLight;
197     }
198 
199     //! @brief 頂点ライトを設定します。
200     //!
201     //! @param[in] light 設定する頂点ライトです。
202     //!
SetVertexLight(VertexLight * light)203     void SetVertexLight(VertexLight* light)
204     {
205         this->m_VertexLights.push_back(light);
206     }
207 
208     //!  @brief        頂点ライトの先頭を指すイテレータを取得します。
209     //!
210     //!  @return       頂点ライトの先頭を指すイテレータです。
211     //!
GetVertexLightBegin()212     VertexLightArray::iterator GetVertexLightBegin()
213     {
214         return m_VertexLights.begin();
215     }
216 
217     //!  @brief        頂点ライトの先頭を指すイテレータを取得します。
218     //!
219     //!  @return       頂点ライトの先頭を指すイテレータです。
220     //!
GetVertexLightBegin()221     VertexLightArray::const_iterator GetVertexLightBegin() const
222     {
223         return m_VertexLights.begin();
224     }
225 
226     //!  @brief        頂点ライトの終端を指すイテレータを取得します。
227     //!
228     //!  @return       頂点ライトの終端を指すイテレータです。
229     //!
GetVertexLightEnd()230     VertexLightArray::iterator GetVertexLightEnd()
231     {
232         return m_VertexLights.end();
233     }
234 
235     //!  @brief        頂点ライトの終端を指すイテレータを取得します。
236     //!
237     //!  @return       頂点ライトの終端を指すイテレータです。
238     //!
GetVertexLightEnd()239     VertexLightArray::const_iterator GetVertexLightEnd() const
240     {
241         return m_VertexLights.end();
242     }
243 
244     //!  @brief        頂点ライトの数を取得します。
245     //!
246     //!  @return       頂点ライトの数です。
247     //!
GetVertexLightCount()248     s32 GetVertexLightCount() const
249     {
250         return m_VertexLights.size();
251     }
252 
253     //! @brief フラグメントライトを設定します。
254     //!
255     //! @param[in] light 設定するフラグメントライトです。
256     //!
SetFragmentLight(FragmentLight * light)257     void SetFragmentLight(FragmentLight* light)
258     {
259         this->m_FragmentLights.push_back(light);
260     }
261 
262     //!  @brief        フラグメントライトの先頭を指すイテレータを取得します。
263     //!
264     //!  @return       フラグメントライトの先頭を指すイテレータです。
265     //!
GetFragmentLightBegin()266     FixedFragmentLightArray::iterator GetFragmentLightBegin()
267     {
268         return m_FragmentLights.begin();
269     }
270 
271     //!  @brief        フラグメントライトの先頭を指すイテレータを取得します。
272     //!
273     //!  @return       フラグメントライトの先頭を指すイテレータです。
274     //!
GetFragmentLightBegin()275     FixedFragmentLightArray::const_iterator GetFragmentLightBegin() const
276     {
277         return m_FragmentLights.begin();
278     }
279 
280     //!  @brief        フラグメントライトの終端を指すイテレータを取得します。
281     //!
282     //!  @return       フラグメントライトの終端を指すイテレータです。
283     //!
GetFragmentLightEnd()284     FixedFragmentLightArray::iterator GetFragmentLightEnd()
285     {
286         return m_FragmentLights.end();
287     }
288 
289     //!  @brief        フラグメントライトの終端を指すイテレータを取得します。
290     //!
291     //!  @return       フラグメントライトの終端を指すイテレータです。
292     //!
GetFragmentLightEnd()293     FixedFragmentLightArray::const_iterator GetFragmentLightEnd() const
294     {
295         return m_FragmentLights.end();
296     }
297 
298     //!  @brief        フラグメントライトの数を取得します。
299     //!
300     //!  @return       フラグメントライトの数です。
301     //!
GetFragmentLightCount()302     s32 GetFragmentLightCount() const
303     {
304         return m_FragmentLights.size();
305     }
306 
307     //! @brief ライトをクリアします。
ClearAll()308     void ClearAll()
309     {
310         this->m_AmbientLight = NULL;
311         this->m_HemiSphereLight = NULL;
312         this->m_VertexLights.clear();
313         this->m_FragmentLights.clear();
314     }
315 
316     //@}
317 
318 protected:
319 
320     //----------------------------------------
321     //! @name コンストラクタ/デストラクタ
322     //@{
323 
324     //! コンストラクタです。
LightSet(os::IAllocator * allocator,ResLightSet resObj,const LightSet::Description & description)325     LightSet(
326         os::IAllocator* allocator,
327         ResLightSet resObj,
328         const LightSet::Description& description)
329     : GfxObject(allocator),
330       m_Resource(resObj),
331       m_AmbientLight(NULL),
332       m_HemiSphereLight(NULL)
333     {
334         NW_UNUSED_VARIABLE(description);
335     }
336 
337     //! デストラクタです。
~LightSet()338     virtual ~LightSet() {}
339 
340     //@}
341 
342 private:
343     ResLightSet m_Resource;
344 
345     AmbientLight* m_AmbientLight;
346     HemiSphereLight* m_HemiSphereLight;
347     VertexLightArray m_VertexLights;
348     FixedFragmentLightArray m_FragmentLights;
349 };
350 
351 } // namespace gfx
352 } // namespace nw
353 
354 #endif // NW_GFX_LIGHTSET_H_
355