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