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