1 /*---------------------------------------------------------------------------*
2   Project:  NintendoWare
3   File:     gfx_VertexLight.cpp
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: 25352 $
14  *---------------------------------------------------------------------------*/
15 
16 #include "precompiled.h"
17 
18 #include <nw/os/os_Memory.h>
19 
20 #include <nw/gfx/gfx_VertexLight.h>
21 #include <nw/gfx/gfx_ISceneVisitor.h>
22 
23 #include <cstring>
24 
25 #if defined(NW_GFX_VERTEX_LIGHT_ENABLED)
26 namespace nw
27 {
28 namespace gfx
29 {
30 
31 NW_UT_RUNTIME_TYPEINFO_DEFINITION(VertexLight, Light);
32 
33 //----------------------------------------
34 VertexLight*
Create(os::IAllocator * allocator)35 VertexLight::DynamicBuilder::Create(
36     os::IAllocator* allocator
37 )
38 {
39     NW_NULL_ASSERT(allocator);
40 
41     ResPtr resource(
42         CreateResVertexLight(allocator),
43         ResVertexLightDataDestroyer(allocator));
44 
45     void* memory = allocator->Alloc(sizeof(VertexLight));
46     NW_NULL_ASSERT(memory);
47     VertexLight* light = new(memory) VertexLight(
48         allocator,
49         resource,
50         m_Description);
51 
52     Result result = light->Initialize(allocator);
53     NW_ASSERT(result.IsSuccess());
54 
55     return light;
56 }
57 
58 //----------------------------------------
59 VertexLight*
Create(SceneNode * parent,ResSceneObject resource,const VertexLight::Description & description,os::IAllocator * allocator)60 VertexLight::Create(
61     SceneNode* parent,
62     ResSceneObject resource,
63     const VertexLight::Description& description,
64     os::IAllocator* allocator
65 )
66 {
67     NW_NULL_ASSERT(allocator);
68 
69     ResVertexLight resNode = ResDynamicCast<ResVertexLight>(resource);
70     NW_ASSERT(resNode.IsValid());
71     NW_ASSERT( internal::ResCheckRevision( resNode ) );
72 
73     void* memory = allocator->Alloc(sizeof(VertexLight));
74     NW_NULL_ASSERT(memory);
75 
76     VertexLight* light = new(memory) VertexLight(
77         allocator,
78         resNode,
79         description);
80 
81     Result result = light->Initialize(allocator);
82     NW_ASSERT(result.IsSuccess());
83 
84     if (parent)
85     {
86         bool isAttached = parent->AttachChild(light);
87         NW_ASSERT(isAttached);
88     }
89 
90     return light;
91 }
92 
93 //----------------------------------------
94 void
UpdateDirection()95 VertexLight::UpdateDirection()
96 {
97     ResVertexLight resLight = this->GetResVertexLight();
98 
99     if (ut::CheckFlag(resLight.GetFlags(), ResFragmentLightData::FLAG_IS_INHERITING_DIRECTION_ROTATE))
100     {
101         this->CalcInheritingDiretion(this->Direction(), resLight.GetDirection());
102     }
103     else
104     {
105         this->Direction() = resLight.GetDirection();
106     }
107 }
108 
109 //----------------------------------------
110 void
Accept(ISceneVisitor * visitor)111 VertexLight::Accept(
112     ISceneVisitor* visitor
113 )
114 {
115     visitor->VisitVertexLight(this);
116     AcceptChildren(visitor);
117 }
118 
119 
120 //-----------------------------------------
121 /* static*/ ResVertexLightData*
CreateResVertexLight(os::IAllocator * allocator,const char * name)122 VertexLight::CreateResVertexLight(os::IAllocator* allocator, const char* name /* = NULL */)
123 {
124     const int MAX_NAME_LENGTH = 256;
125 
126     // TODO: 細かくアロケートする実装になっているが、まとめて確保できる仕組みも追加予定。
127     ResVertexLightData* resVertexLight =
128         AllocateAndFillN<ResVertexLightData>(allocator, sizeof(ResVertexLightData), 0);
129 
130     //--------------------------------
131     // ResSceneObjectData のメンバ初期化
132     resVertexLight->typeInfo = ResVertexLight::TYPE_INFO;
133     resVertexLight->m_Header.revision = ResLight::BINARY_REVISION;
134     resVertexLight->m_Header.signature = ResLight::SIGNATURE;
135 
136     resVertexLight->m_UserDataDicCount = 0;
137     resVertexLight->toUserDataDic.set_ptr( NULL );
138 
139     resVertexLight->toName.set_ptr(AllocateAndCopyString(name, allocator, MAX_NAME_LENGTH));
140 
141     //--------------------------------
142     // ResSceneNodeData のメンバ初期化
143     resVertexLight->m_ChildrenTableCount = 0;
144     resVertexLight->toChildrenTable.set_ptr( NULL );
145     resVertexLight->m_AnimGroupsDicCount = 0;
146     resVertexLight->toAnimGroupsDic.set_ptr( NULL );
147 
148     //--------------------------------
149     // ResTransformNode のメンバ初期化
150     const math::VEC3 scale(1.0f, 1.0f, 1.0f);
151     const math::VEC3 rotate(0.0f, 0.0f, 0.0f);
152     const math::VEC3 translate(0.0f, 0.0, 0.0f);
153     resVertexLight->m_Transform = math::Transform3(scale, rotate, translate);
154     resVertexLight->m_WorldMatrix = math::MTX34::Identity();
155 
156     return resVertexLight;
157 }
158 
159 
160 //-----------------------------------------
161 /* static */ void
DestroyResVertexLight(os::IAllocator * allocator,ResVertexLightData * resVertexLight)162 VertexLight::DestroyResVertexLight(os::IAllocator* allocator, ResVertexLightData* resVertexLight)
163 {
164     NW_NULL_ASSERT( allocator );
165     NW_NULL_ASSERT( resVertexLight );
166 
167     if ( resVertexLight->toName.to_ptr() != NULL )
168     {
169         allocator->Free( const_cast<char*>( resVertexLight->toName.to_ptr() ) );
170     }
171     allocator->Free( resVertexLight );
172 }
173 
174 
175 //-----------------------------------------
176 Result
CreateOriginalValue(os::IAllocator * allocator)177 VertexLight::CreateOriginalValue(os::IAllocator* allocator)
178 {
179     Result result = INITIALIZE_RESULT_OK;
180 
181     void* buffer = allocator->Alloc(sizeof(ResVertexLightData));
182     NW_NULL_ASSERT(buffer);
183 
184     // リソースをコピー
185     ResVertexLightData* originalValue = new(buffer) ResVertexLightData(GetResVertexLight().ref());
186     m_OriginalValue = ResVertexLight(originalValue);
187 
188     m_OriginalTransform = this->GetResTransformNode().GetTransform();
189 
190     return result;
191 }
192 
193 //----------------------------------------
194 Result
Initialize(os::IAllocator * allocator)195 VertexLight::Initialize(os::IAllocator* allocator)
196 {
197     Result result = INITIALIZE_RESULT_OK;
198 
199     result |= TransformNode::Initialize(allocator);
200     NW_ENSURE_AND_RETURN(result);
201 
202     result |= CreateOriginalValue(allocator);
203     NW_ENSURE_AND_RETURN(result);
204 
205     result |= CreateAnimGroup(allocator);
206     NW_ENSURE_AND_RETURN(result);
207 
208     return result;
209 }
210 
211 } // namespace gfx
212 } // namespace nw
213 #endif
214