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