1 /*---------------------------------------------------------------------------*
2   Project:  NintendoWare
3   File:     gfx_VertexLight.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_VERTEXLIGHT_H_
17 #define NW_GFX_VERTEXLIGHT_H_
18 
19 #include <nw/gfx/gfx_Light.h>
20 #include <nw/gfx/res/gfx_ResLight.h>
21 
22 #include <nw/ut/ut_MovePtr.h>
23 #include <functional>
24 
25 #if defined(NW_GFX_VERTEX_LIGHT_ENABLED)
26 namespace nw
27 {
28 namespace gfx
29 {
30 
31 //---------------------------------------------------------------------------
32 //! @brief        頂点ライトを表すクラスです。
33 //---------------------------------------------------------------------------
34 class VertexLight : public Light
35 {
36 private:
37     NW_DISALLOW_COPY_AND_ASSIGN(VertexLight);
38 
39 public:
40     NW_UT_RUNTIME_TYPEINFO;
41 
42     //----------------------------------------
43     //! @name 取得/設定
44     //@{
45 
46     //! @brief ライト方向を取得します。
47     //!
48     //! SceneUpdater で ResLight の m_Direction から親ノードの回転を継承フラグに応じて計算もしくはコピーされます。
49     //! Direction を変更する場合は、SceneUpdater::UpdateTransformNode の実行前ならば ResLight::SetDirection を用い、
50     //! SceneUpdater の実行後ではこちらの Direction を直接編集してください。
51     //!
52     //! @return ライト方向です。
53     //!
Direction()54     const math::VEC3& Direction() const
55     {
56         return this->m_Direction;
57     }
58 
59     //! @brief ライト方向を取得します。
60     //!
61     //! SceneUpdater で ResLight の m_Direction から親ノードの回転を継承フラグに応じて計算もしくはコピーされます。
62     //! Direction を変更する場合は、SceneUpdater::UpdateTransformNode の実行前ならば ResLight::SetDirection を用い、
63     //! SceneUpdater の実行後ではこちらの Direction を直接編集してください。
64     //!
65     //! @return ライト方向です。
66     //!
Direction()67     math::VEC3& Direction()
68     {
69         return this->m_Direction;
70     }
71 
72     //@}
73 
74     //! @brief 設定内容です。
75     struct Description : public Light::Description
76     {
77         //! @brief コンストラクタです。
DescriptionDescription78         Description(){}
79     };
80 
81     //----------------------------------------
82     //! @name 作成/破棄
83     //@{
84 
85     //! @brief 頂点ライトを動的に構築するためのクラスです。
86     //!
87     //! IsFixedSizeMemory の初期値は true です。false に変更すると、各種最大数の設定は無視されます。
88     class DynamicBuilder
89     {
90     public:
91         //! コンストラクタです。
DynamicBuilder()92         DynamicBuilder() {}
93 
94         //! デストラクタです。
~DynamicBuilder()95         ~DynamicBuilder() {}
96 
97         //! @brief 生成時以外にもメモリを確保するかどうかのフラグを設定します。
98         //!
99         //!        true を指定すると、生成時のみ固定サイズのメモリ確保を行います。
100         //!
101         //!        false を指定すると、生成時以外にも必要に応じて動的にメモリ確保が行われます。
IsFixedSizeMemory(bool isFixedSizeMemory)102         DynamicBuilder& IsFixedSizeMemory(bool isFixedSizeMemory)
103         {
104             m_Description.isFixedSizeMemory = isFixedSizeMemory;
105             return *this;
106         }
107 
108         //! 子の最大数を設定します。
MaxChildren(int maxChildren)109         DynamicBuilder& MaxChildren(int maxChildren)
110         {
111             m_Description.maxChildren = maxChildren;
112             return *this;
113         }
114 
115         //! 管理できるコールバックの最大数を設定します。
MaxCallbacks(int maxCallbacks)116         DynamicBuilder& MaxCallbacks(int maxCallbacks)
117         {
118             m_Description.maxCallbacks = maxCallbacks;
119             return *this;
120         }
121 
122         //! @brief        頂点ライトを生成します。
123         //!
124         //! @param[in]    allocator アロケータです。
125         //!
126         //! @return       生成した頂点ライトを返します。
127         //!
128         VertexLight* Create(os::IAllocator* allocator);
129 
130         //! @brief 生成時に必要なメモリサイズを取得します。
131         //!
132         //! メモリサイズは Builder の設定によって変化します。
133         //! すべての設定が終わった後にこの関数を呼び出してください。
134         //!
135         //! @param[in] alignment 計算に用いるアライメントです。2 のべき乗である必要があります。
136         size_t GetMemorySize(size_t alignment = os::IAllocator::DEFAULT_ALIGNMENT) const;
137 
138     private:
139         VertexLight::Description m_Description;
140     };
141 
142     //! @brief        頂点ライトを生成します。
143     //!
144     //! @param[in]    parent 親のノードです。
145     //! @param[in]    resource リソースです。
146     //! @param[in]    description 設定内容です。
147     //! @param[in]    allocator アロケータです。
148     //!
149     //! @return       生成された頂点ライトです。
150     //!
151     static VertexLight* Create(
152         SceneNode* parent,
153         ResSceneObject resource,
154         const VertexLight::Description& description,
155         os::IAllocator* allocator);
156 
157     //! @brief        生成時に必要なメモリサイズを取得します。
158     //!
159     //! @param[in]    resource リソースです。
160     //! @param[in]    description 設定内容です。
161     //! @param[in]    alignment 計算に用いるアライメントです。2 のべき乗である必要があります。
162     static size_t GetMemorySize(
163         ResVertexLight resource,
164         Description description,
165         size_t alignment = os::IAllocator::DEFAULT_ALIGNMENT
166     )
167     {
168         os::MemorySizeCalculator size(alignment);
169 
170         GetMemorySizeInternal(&size, resource, description);
171 
172         return size.GetSizeWithPadding(alignment);
173     }
174 
175     //! @details :private
176     static void GetMemorySizeInternal(
177         os::MemorySizeCalculator* pSize,
178         ResVertexLight resource,
179         Description description);
180 
181     //@}
182 
183     //----------------------------------------
184     //! @name トランスフォーム
185     //@{
186 
187     //! @brief 方向情報を更新します。
188     virtual void UpdateDirection();
189 
190     //@}
191     //----------------------------------------
192     //! @name シーンツリー
193     //@{
194 
195     //! @brief        ビジターを受け付けます。
196     //!
197     //! @param[in]    visitor ビジターです。
198     //!
199     virtual void Accept(ISceneVisitor* visitor);
200 
201     //@}
202 
203     //----------------------------------------
204     //! @name リソース
205     //@{
206 
207     //! 頂点ライトのリソースを取得します。
GetResVertexLight()208     ResVertexLight GetResVertexLight()
209     {
210         return ResStaticCast<ResVertexLight>(this->GetResSceneObject());
211     }
212 
213     //! 頂点ライトのリソースを取得します。
GetResVertexLight()214     const ResVertexLight GetResVertexLight() const
215     {
216         return ResStaticCast<ResVertexLight>(this->GetResSceneObject());
217     }
218 
219     //@}
220 
221 protected:
222     struct ResVertexLightDataDestroyer : public std::unary_function<ResVertexLightData*, void>
223     {
m_AllocatorResVertexLightDataDestroyer224         ResVertexLightDataDestroyer(os::IAllocator* allocator = 0) : m_Allocator(allocator)
225         {}
operatorResVertexLightDataDestroyer226         result_type operator()(argument_type data)
227         {
228             DestroyResVertexLight(m_Allocator, data);
229         }
230 
231         os::IAllocator* m_Allocator;
232     };
233 
234     //! 動的生成したライトリソースを破棄するための MovePtr の定義です。
235     typedef ut::MovePtr<ResVertexLightData, ResVertexLightDataDestroyer> ResPtr;
236 
237     //----------------------------------------
238     //! @name コンストラクタ/デストラクタ
239     //@{
240 
241     //! コンストラクタです。
VertexLight(os::IAllocator * allocator,ResVertexLight resObj,const VertexLight::Description & description)242     VertexLight(
243         os::IAllocator* allocator,
244         ResVertexLight resObj,
245         const VertexLight::Description& description)
246     : Light(
247         allocator,
248         resObj,
249         description),
250       m_Direction(resObj.GetDirection())
251     {}
252 
253     //! コンストラクタです。
VertexLight(os::IAllocator * allocator,ResPtr resource,const VertexLight::Description & description)254     VertexLight(
255         os::IAllocator* allocator,
256         ResPtr resource,
257         const VertexLight::Description& description)
258     : Light(
259         allocator,
260         ResVertexLight(resource.Get()),
261         description),
262       m_Resource(resource)
263     {
264         m_Direction = this->GetResVertexLight().GetDirection();
265     }
266 
267     //! デストラクタです。
~VertexLight()268     virtual ~VertexLight()
269     {
270         DestroyOriginalValue();
271     }
272 
273     //@}
274 
275 private:
276     virtual Result Initialize(os::IAllocator* allocator);
277 
278     //---------------------------------------------------------------------------
279     //! @brief        ResVertexLightData のリソースを生成します。
280     //!
281     //! @param[in]    allocator   リソース用のメモリを確保するアロケータです。
282     //! @param[in]    name        リソースにつける名前へのポインタです。名前が必要ない場合には NULL を指定してください。
283     //!
284     //! @return       ResVertexLightData へのポインタです。
285     //---------------------------------------------------------------------------
286     static ResVertexLightData*    CreateResVertexLight(os::IAllocator* allocator, const char* name = NULL);
287 
288     //---------------------------------------------------------------------------
289     //! @brief        ResVertexLightData のリソースを破棄します。
290     //!
291     //! @param[in]    resVertexLight ResVertexLightData へのポインタです。
292     //! @param[in]    allocator        リソース用のメモリを開放するアロケータです。
293     //---------------------------------------------------------------------------
294     static void DestroyResVertexLight(os::IAllocator* allocator, ResVertexLightData* resVertexLight);
295 
296     //! アニメーションを初期状態に戻すため、初期化時の状態を保存します。
297     Result CreateOriginalValue(os::IAllocator* allocator);
298 
299     //! :private
GetLightType()300     virtual u32 GetLightType() const
301     {
302         return anim::ResLightAnimData::LIGHT_TYPE_VERTEX;
303     }
304 
305     //! :private
GetLightKind()306     virtual u32 GetLightKind() const
307     {
308         return GetResVertexLight().GetLightKind();
309     }
310 
311     ResPtr m_Resource;
312     math::VEC3 m_Direction;
313 };
314 
315 } // namespace gfx
316 } // namespace nw
317 #endif
318 #endif // NW_GFX_VERTEXLIGHT_H_
319