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