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: 24209 $
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     private:
131         VertexLight::Description m_Description;
132     };
133 
134     //! @brief        頂点ライトを生成します。
135     //!
136     //! @param[in]    parent 親のノードです。
137     //! @param[in]    resource リソースです。
138     //! @param[in]    description 設定内容です。
139     //! @param[in]    allocator アロケータです。
140     //!
141     //! @return       生成された頂点ライトです。
142     //!
143     static VertexLight* Create(
144         SceneNode* parent,
145         ResSceneObject resource,
146         const VertexLight::Description& description,
147         os::IAllocator* allocator);
148 
149     //@}
150 
151     //----------------------------------------
152     //! @name トランスフォーム
153     //@{
154 
155     //! @brief 方向情報を更新します。
156     virtual void UpdateDirection();
157 
158     //@}
159     //----------------------------------------
160     //! @name シーンツリー
161     //@{
162 
163     //! @brief        ビジターを受け付けます。
164     //!
165     //! @param[in]    visitor ビジターです。
166     //!
167     virtual void Accept(ISceneVisitor* visitor);
168 
169     //@}
170 
171     //----------------------------------------
172     //! @name リソース
173     //@{
174 
175     //! 頂点ライトのリソースを取得します。
GetResVertexLight()176     ResVertexLight GetResVertexLight()
177     {
178         return ResStaticCast<ResVertexLight>(this->GetResSceneObject());
179     }
180 
181     //! 頂点ライトのリソースを取得します。
GetResVertexLight()182     const ResVertexLight GetResVertexLight() const
183     {
184         return ResStaticCast<ResVertexLight>(this->GetResSceneObject());
185     }
186 
187     //@}
188 
189 protected:
190     struct ResVertexLightDataDestroyer : public std::unary_function<ResVertexLightData*, void>
191     {
m_AllocatorResVertexLightDataDestroyer192         ResVertexLightDataDestroyer(os::IAllocator* allocator = 0) : m_Allocator(allocator)
193         {}
operatorResVertexLightDataDestroyer194         result_type operator()(argument_type data)
195         {
196             DestroyResVertexLight(m_Allocator, data);
197         }
198 
199         os::IAllocator* m_Allocator;
200     };
201 
202     //! 動的生成したライトリソースを破棄するための MovePtr の定義です。
203     typedef ut::MovePtr<ResVertexLightData, ResVertexLightDataDestroyer> ResPtr;
204 
205     //----------------------------------------
206     //! @name コンストラクタ/デストラクタ
207     //@{
208 
209     //! コンストラクタです。
VertexLight(os::IAllocator * allocator,ResVertexLight resObj,const VertexLight::Description & description)210     VertexLight(
211         os::IAllocator* allocator,
212         ResVertexLight resObj,
213         const VertexLight::Description& description)
214     : Light(
215         allocator,
216         resObj,
217         description),
218       m_Direction(resObj.GetDirection())
219     {}
220 
221     //! コンストラクタです。
VertexLight(os::IAllocator * allocator,ResPtr resource,const VertexLight::Description & description)222     VertexLight(
223         os::IAllocator* allocator,
224         ResPtr resource,
225         const VertexLight::Description& description)
226     : Light(
227         allocator,
228         ResVertexLight(resource.Get()),
229         description),
230       m_Resource(resource)
231     {
232         m_Direction = this->GetResVertexLight().GetDirection();
233     }
234 
235     //! デストラクタです。
~VertexLight()236     virtual ~VertexLight()
237     {
238         DestroyOriginalValue();
239     }
240 
241     //@}
242 
243 private:
244     virtual Result Initialize(os::IAllocator* allocator);
245 
246     //---------------------------------------------------------------------------
247     //! @brief        ResVertexLightData のリソースを生成します。
248     //!
249     //! @param[in]    allocator   リソース用のメモリを確保するアロケータです。
250     //! @param[in]    name        リソースにつける名前へのポインタです。名前が必要ない場合には NULL を指定してください。
251     //!
252     //! @return       ResVertexLightData へのポインタです。
253     //---------------------------------------------------------------------------
254     static ResVertexLightData*    CreateResVertexLight(os::IAllocator* allocator, const char* name = NULL);
255 
256     //---------------------------------------------------------------------------
257     //! @brief        ResVertexLightData のリソースを破棄します。
258     //!
259     //! @param[in]    resVertexLight ResVertexLightData へのポインタです。
260     //! @param[in]    allocator        リソース用のメモリを開放するアロケータです。
261     //---------------------------------------------------------------------------
262     static void DestroyResVertexLight(os::IAllocator* allocator, ResVertexLightData* resVertexLight);
263 
264     //! アニメーションを初期状態に戻すため、初期化時の状態を保存します。
265     Result CreateOriginalValue(os::IAllocator* allocator);
266 
267     ResPtr m_Resource;
268     math::VEC3 m_Direction;
269 };
270 
271 } // namespace gfx
272 } // namespace nw
273 #endif
274 #endif // NW_GFX_VERTEXLIGHT_H_
275