1 /*---------------------------------------------------------------------------*
2   Project:  NintendoWare
3   File:     gfx_SceneEnvironmentSetting.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: 28677 $
14  *---------------------------------------------------------------------------*/
15 
16 #ifndef NW_GFX_SCENEENVIRONMENTSETTING_H_
17 #define NW_GFX_SCENEENVIRONMENTSETTING_H_
18 
19 #include <nw/gfx/res/gfx_ResSceneEnvironmentSetting.h>
20 #include <nw/ut/ut_MoveArray.h>
21 #include <nw/gfx/gfx_GfxObject.h>
22 #include <functional>
23 
24 namespace nw
25 {
26 namespace gfx
27 {
28 
29 class Fog;
30 class Camera;
31 class LightSet;
32 
33 //---------------------------------------------------------------------------
34 //! @brief        シーン環境設定を表すクラスです。
35 //---------------------------------------------------------------------------
36 class SceneEnvironmentSetting : public SceneObject
37 {
38 private:
39     NW_DISALLOW_COPY_AND_ASSIGN(SceneEnvironmentSetting);
40 
41 public:
42     NW_UT_RUNTIME_TYPEINFO;
43 
44     //! @brief ライトセットをバインドするための構造体です。
45     struct LightSetBinder
46     {
LightSetBinderLightSetBinder47         LightSetBinder()
48             : index(-1)
49         {}
50 
51         s32 index;
52         GfxPtr<LightSet> lightSet;
53     };
54 
55     //! @brief カメラをバインドするための構造体です。
56     struct CameraBinder
57     {
CameraBinderCameraBinder58         CameraBinder()
59             : index(-1),
60               camera(NULL)
61         {}
62 
63         s32 index;
64         Camera* camera;
65     };
66 
67     //! @brief フォグをバインドするための構造体です。
68     struct FogBinder
69     {
FogBinderFogBinder70         FogBinder()
71             : index(-1),
72               fog(NULL)
73         {}
74 
75         s32 index;
76         Fog* fog;
77     };
78 
79     typedef ut::MoveArray<LightSetBinder> LightSetBinderArray;
80     typedef ut::MoveArray<CameraBinder> CameraBinderArray;
81     typedef ut::MoveArray<FogBinder> FogBinderArray;
82 
83     //! @brief 設定内容です。
84     struct Description
85     {
86         //! @brief コンストラクタです。
DescriptionDescription87         Description(){}
88     };
89 
90     //----------------------------------------
91     //! @name 作成/破棄
92     //@{
93 
94     //! @brief        シーン環境設定を生成します。
95     //!
96     //! @param[in]    resource リソースです。
97     //! @param[in]    description 設定内容です。
98     //! @param[in]    allocator アロケータです。
99     //!
100     //! @return       生成したシーン環境設定です。
101     //!
102     static SceneEnvironmentSetting* Create(
103         ResSceneObject resource,
104         const SceneEnvironmentSetting::Description& description,
105         os::IAllocator* allocator);
106 
107     //! @brief        生成時に必要なメモリサイズを取得します。
108     //!
109     //! @param[in]    resource リソースです。
110     //! @param[in]    description 設定内容です。
111     //! @param[in]    alignment 計算に用いるアライメントです。2 のべき乗である必要があります。
112     static size_t GetMemorySize(
113         ResSceneEnvironmentSetting resource,
114         Description description,
115         size_t alignment = os::IAllocator::DEFAULT_ALIGNMENT
116     )
117     {
118         os::MemorySizeCalculator size(alignment);
119 
120         GetMemorySizeInternal(&size, resource, description);
121 
122         return size.GetSizeWithPadding(alignment);
123     }
124 
125     //! @details :private
126     static void GetMemorySizeInternal(
127         os::MemorySizeCalculator* pSize,
128         ResSceneEnvironmentSetting resource,
129         Description description);
130 
131     //@}
132 
133     //----------------------------------------
134     //! @name 更新
135     //@{
136 
137     //! @brief        シーンコンテキストを用いて参照解決を行います。
138     //!
139     //! @param[in]    sceneContext 参照解決に用いるシーンコンテキストです。
140     //!
141     void ResolveReference(const SceneContext& sceneContext);
142 
143     //! @brief        参照解決したシーン環境設定をクリアします。
144     //!               SceneContext を変更して再度 ResolveReference を行う場合に用いて下さい。
145     //!
146     void Clear();
147 
148     //@}
149 
150     //----------------------------------------
151     //! @name 取得/設定
152     //@{
153 
154     //! @brief リソースを取得します。
155     //!
156     //! @return ResSceneEnvironmentSetting です。
157     //!
GetResSceneEnvironmentSetting()158     ResSceneEnvironmentSetting GetResSceneEnvironmentSetting()
159     {
160         return ResDynamicCast<ResSceneEnvironmentSetting>(this->GetResSceneObject());
161     }
162 
163     //! @brief リソースを取得します。
164     //!
165     //! @return ResSceneEnvironmentSetting です。
166     //!
GetResSceneEnvironmentSetting()167     const ResSceneEnvironmentSetting GetResSceneEnvironmentSetting() const
168     {
169         return ResDynamicCast<ResSceneEnvironmentSetting>(this->GetResSceneObject());
170     }
171 
172     //!  @brief        カメラの先頭を指すイテレータを取得します。
173     //!
174     //!  @return       カメラの先頭を指すイテレータです。
175     //!
GetCameraBegin()176     CameraBinderArray::iterator GetCameraBegin()
177     {
178         return m_Cameras.begin();
179     }
180 
181     //!  @brief        カメラの先頭を指すイテレータを取得します。
182     //!
183     //!  @return       カメラの先頭を指すイテレータです。
184     //!
GetCameraBegin()185     CameraBinderArray::const_iterator GetCameraBegin() const
186     {
187         return m_Cameras.begin();
188     }
189 
190     //!  @brief        カメラの終端を指すイテレータを取得します。
191     //!
192     //!  @return       カメラの終端を指すイテレータです。
193     //!
GetCameraEnd()194     CameraBinderArray::iterator GetCameraEnd()
195     {
196         return m_Cameras.end();
197     }
198 
199     //!  @brief        カメラの終端を指すイテレータを取得します。
200     //!
201     //!  @return       カメラの終端を指すイテレータです。
202     //!
GetCameraEnd()203     CameraBinderArray::const_iterator GetCameraEnd() const
204     {
205         return m_Cameras.end();
206     }
207 
208     //!  @brief        フォグの先頭を指すイテレータを取得します。
209     //!
210     //!  @return       フォグの先頭を指すイテレータです。
211     //!
GetFogBegin()212     FogBinderArray::iterator GetFogBegin()
213     {
214         return m_Fogs.begin();
215     }
216 
217     //!  @brief        フォグの先頭を指すイテレータを取得します。
218     //!
219     //!  @return       フォグの先頭を指すイテレータです。
220     //!
GetFogBegin()221     FogBinderArray::const_iterator GetFogBegin() const
222     {
223         return m_Fogs.begin();
224     }
225 
226     //!  @brief        フォグの終端を指すイテレータを取得します。
227     //!
228     //!  @return       フォグの終端を指すイテレータです。
229     //!
GetFogEnd()230     FogBinderArray::iterator GetFogEnd()
231     {
232         return m_Fogs.end();
233     }
234 
235     //!  @brief        フォグの終端を指すイテレータを取得します。
236     //!
237     //!  @return       フォグの終端を指すイテレータです。
238     //!
GetFogEnd()239     FogBinderArray::const_iterator GetFogEnd() const
240     {
241         return m_Fogs.end();
242     }
243 
244     //!  @brief        ライトセットの先頭を指すイテレータを取得します。
245     //!
246     //!  @return       ライトセットの先頭を指すイテレータです。
247     //!
GetLightSetBegin()248     LightSetBinderArray::iterator GetLightSetBegin()
249     {
250         return m_LightSets.begin();
251     }
252 
253     //!  @brief        ライトセットの先頭を指すイテレータを取得します。
254     //!
255     //!  @return       ライトセットの先頭を指すイテレータです。
256     //!
GetLightSetBegin()257     LightSetBinderArray::const_iterator GetLightSetBegin() const
258     {
259         return m_LightSets.begin();
260     }
261 
262     //!  @brief        ライトセットの終端を指すイテレータを取得します。
263     //!
264     //!  @return       ライトセットの終端を指すイテレータです。
265     //!
GetLightSetEnd()266     LightSetBinderArray::iterator GetLightSetEnd()
267     {
268         return m_LightSets.end();
269     }
270 
271     //!  @brief        ライトセットの終端を指すイテレータを取得します。
272     //!
273     //!  @return       ライトセットの終端を指すイテレータです。
274     //!
GetLightSetEnd()275     LightSetBinderArray::const_iterator GetLightSetEnd() const
276     {
277         return m_LightSets.end();
278     }
279 
280     //@}
281 
282 protected:
283     //----------------------------------------
284     //! @name コンストラクタ/デストラクタ
285     //@{
286 
287     //! コンストラクタです。
SceneEnvironmentSetting(os::IAllocator * allocator,ResSceneEnvironmentSetting resSetting,const SceneEnvironmentSetting::Description & description)288     SceneEnvironmentSetting(
289         os::IAllocator* allocator,
290         ResSceneEnvironmentSetting resSetting,
291         const SceneEnvironmentSetting::Description& description
292     )
293     : SceneObject(allocator, resSetting)
294     {
295         NW_UNUSED_VARIABLE(description);
296     }
297 
298     //! デストラクタです。
~SceneEnvironmentSetting()299     virtual ~SceneEnvironmentSetting() {}
300 
301     //@}
302 
303 private:
304     //! シーン環境の配列を作成します。
305     void CreateEnvironmentArray(os::IAllocator* allocator, ResSceneEnvironmentSetting );
306 
307     //! @brief ResReferenceSceneObject の名前比較用構造体です。
308     template<typename TObject>
309     struct SceneObjectCompare: public std::unary_function<TObject, bool>
310     {
SceneObjectCompareSceneObjectCompare311         SceneObjectCompare(ResReferenceSceneObject referenceSceneObject): m_Object(referenceSceneObject){}
312         ResReferenceSceneObject m_Object;
operatorSceneObjectCompare313         bool operator()(TObject* lhs) const
314         {
315             if (lhs->GetName() != NULL &&
316                 m_Object.GetPath() != NULL &&
317                 std::strcmp(lhs->GetName(), m_Object.GetPath()) == 0)
318             {
319                 return true;
320             }
321             return false;
322         }
323     };
324 
325     LightSetBinderArray m_LightSets;
326     CameraBinderArray m_Cameras;
327     FogBinderArray m_Fogs;
328 };
329 
330 } // namespace gfx
331 } // namespace nw
332 
333 #endif // NW_GFX_SCENEENVIRONMENTSETTING_H_
334