/
SceneFactory.cpp
445 lines (374 loc) · 13.7 KB
/
SceneFactory.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
/*******************************************************************************
* Ruthenium Engine *
* Copyright (c) 2013-2017 Stepanov Dmitriy aka mrDIMAS *
* *
* This file is part of Ruthenium Engine. *
* *
* Ruthenium Engine is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* Ruthenium Engine is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with Ruthenium Engine. If not, see <http://www.gnu.org/licenses/>. *
* *
*******************************************************************************/
#include "Precompiled.h"
#include "SceneFactory.h"
#include "SceneNode.h"
#include "Mesh.h"
#include "SpotLight.h"
#include "PointLight.h"
#include "Camera.h"
#include "ParticleSystem.h"
#include "DirectionalLight.h"
#include "Fog.h"
#include "FastReader.h"
#include "Engine.h"
#include "CubeTexture.h"
// API Methods
IEngine * const SceneFactory::GetEngineInterface() const {
return mEngine;
}
shared_ptr<ISceneNode> SceneFactory::CreateSceneNode() {
auto sceneNode = make_shared<SceneNode>(this);
msNodeList.push_back(sceneNode);
return sceneNode;
}
shared_ptr<ISceneNode> SceneFactory::CreateSceneNodeDuplicate(shared_ptr<ISceneNode> src) {
auto source = dynamic_pointer_cast<SceneNode>(src);
auto duplicate = dynamic_pointer_cast<SceneNode>(CreateSceneNode());
duplicate->mInFrustum = source->mInFrustum;
duplicate->mTotalFrameCount = 0;
duplicate->mIsSkinned = false;
duplicate->mVisible = true;
duplicate->mLocalTransform = source->mLocalTransform;
duplicate->mGlobalTransform = duplicate->mLocalTransform;
duplicate->mContactCount = source->mContactCount;
duplicate->mFrozen = source->mFrozen;
duplicate->mDepthHack = source->mDepthHack;
duplicate->mAlbedo = source->mAlbedo;
duplicate->mCurrentAnimation = nullptr;
// copy surfaces
for(auto pMesh : source->mMeshList) {
pMesh->LinkTo(duplicate);
duplicate->mMeshList.push_back(pMesh);
}
// create body
switch(source->GetBodyType()) {
case BodyType::Box:
duplicate->SetBoxBody();
break;
case BodyType::Convex:
duplicate->SetConvexBody();
break;
case BodyType::Sphere:
duplicate->SetSphereBody();
break;
case BodyType::Trimesh:
duplicate->SetTrimeshBody();
break;
}
if(duplicate->mFrozen) {
duplicate->Freeze();
}
// copy childs
for(auto & pChild : source->mChildren) {
shared_ptr<SceneNode> & pNewChild = dynamic_pointer_cast<SceneNode>(SceneFactory::CreateSceneNodeDuplicate(pChild));
pNewChild->mParent = duplicate;
duplicate->mChildren.push_back(pNewChild);
}
return duplicate;
}
shared_ptr<IPointLight> SceneFactory::CreatePointLight() {
auto pointLight = make_shared<PointLight>(this);
msNodeList.push_back(pointLight);
msPointLightList.push_back(pointLight);
return pointLight;
}
shared_ptr<ISpotLight> SceneFactory::CreateSpotLight() {
auto spotLight = make_shared<SpotLight>(this);
msNodeList.push_back(spotLight);
msSpotLightList.push_back(spotLight);
return spotLight;
}
shared_ptr<IDirectionalLight> SceneFactory::CreateDirectionalLight() {
auto dirLight = make_shared<DirectionalLight>(this);
msNodeList.push_back(dirLight);
msDirectionalLightList.push_back(dirLight);
return dirLight;
}
vector<weak_ptr<PointLight>> & SceneFactory::GetPointLightList() {
RemoveUnreferenced(msPointLightList);
return msPointLightList;
}
vector<weak_ptr<DirectionalLight>>& SceneFactory::GetDirectionalLightList() {
RemoveUnreferenced(msDirectionalLightList);
return msDirectionalLightList;
}
vector<weak_ptr<SpotLight>> & SceneFactory::GetSpotLightList() {
RemoveUnreferenced(msSpotLightList);
return msSpotLightList;
}
SceneFactory::SceneFactory(Engine * engine) : mEngine(engine) {
}
Engine * SceneFactory::GetEngine() const {
return mEngine;
}
vector<weak_ptr<SceneNode>> & SceneFactory::GetNodeList() {
RemoveUnreferenced(msNodeList);
return msNodeList;
}
shared_ptr<ICamera> SceneFactory::CreateCamera(float fov) {
auto camera = make_shared<Camera>(this, fov);
msNodeList.push_back(camera);
Camera::msCurrentCamera = camera;
return camera;
}
shared_ptr<IParticleSystem> SceneFactory::CreateParticleSystem(int particleCount) {
auto particleSystem = make_shared<ParticleSystem>(this, particleCount);
msNodeList.push_back(particleSystem);
msParticleEmitters.push_back(particleSystem);
return particleSystem;
}
shared_ptr<IFog> SceneFactory::CreateFog(const Vector3 & min, const Vector3 & max, const Vector3 & color, float density) {
auto fog = make_shared<Fog>(this, min, max, color, density);
msNodeList.push_back(fog);
msFogList.push_back(fog);
return fog;
}
shared_ptr<ISceneNode> SceneFactory::FindByName(const string & name) {
for(auto & pWeak : msNodeList) {
shared_ptr<SceneNode> & node = pWeak.lock();
if(node) {
if(node->mName == name) {
return node;
}
}
}
return nullptr;
}
shared_ptr<ISceneNode> SceneFactory::LoadScene(const string & file) {
FastReader reader;
if(!reader.ReadFile(file)) {
Log::Error(StringBuilder("Unable to load '") << file << "' scene!");
}
int numObjects = reader.GetInteger();
int numMeshes = reader.GetInteger();
int numLights = reader.GetInteger();
int framesCount = reader.GetInteger();
auto scene = dynamic_pointer_cast<SceneNode>(CreateSceneNode());
scene->mTotalFrameCount = framesCount;
for(int meshObjectNum = 0; meshObjectNum < numMeshes; meshObjectNum++) {
auto node = dynamic_pointer_cast<SceneNode>(CreateSceneNode());
node->mLocalTransform.setOrigin(reader.GetVector());
node->mLocalTransform.setRotation(reader.GetQuaternion());
node->mGlobalTransform = node->mLocalTransform;
int hasAnimation = reader.GetInteger();
int isSkinned = reader.GetInteger();
int meshCount = reader.GetInteger();
int keyframeCount = reader.GetInteger();
node->mIsSkinned = isSkinned;
ParseString(reader.GetString(), node->mProperties);
node->mName = reader.GetString();
for(int i = 0; i < keyframeCount; i++) {
auto keyframe = make_unique<btTransform>();
keyframe->setOrigin(reader.GetVector());
keyframe->setRotation(reader.GetQuaternion());
node->mKeyframeList.push_back(std::move(keyframe));
}
if(keyframeCount) {
node->mLocalTransform = *node->mKeyframeList[0];
}
node->mTotalFrameCount = framesCount - 1;
for(int i = 0; i < meshCount; i++) {
shared_ptr<Mesh> & mesh = make_shared<Mesh>();
mesh->LinkTo(node);
int vertexCount = reader.GetInteger();
int indexCount = reader.GetInteger();
Vector3 aabbMin = reader.GetBareVector();
Vector3 aabbMax = reader.GetBareVector();
Vector3 aabbCenter = reader.GetBareVector(); // odd
float aabbRadius = reader.GetFloat(); // odd
string diffuse = reader.GetString();
string normal = reader.GetString();
mesh->SetOpacity(reader.GetFloat() / 100.0f);
for(int vertexNum = 0; vertexNum < vertexCount; vertexNum++) {
Vertex v;
v.mPosition = reader.GetBareVector();
v.mNormal = reader.GetBareVector();
v.mTexCoord = reader.GetBareVector2();
Vector2 tc2 = reader.GetBareVector2(); // odd
v.mTangent = reader.GetBareVector();
mesh->AddVertex(v);
}
for(int indexNum = 0; indexNum < indexCount; indexNum += 3) {
unsigned short a = reader.GetShort();
unsigned short b = reader.GetShort();
unsigned short c = reader.GetShort();
mesh->AddTriangle(Triangle(a, b, c));
}
mesh->mDiffuseTexture = dynamic_pointer_cast<Texture>(mEngine->GetRenderer()->GetTexture(mEngine->GetRenderer()->GetTextureStoragePath() + diffuse));
if(mesh->GetOpacity() > 0.95f) {
mesh->mNormalTexture = dynamic_pointer_cast<Texture>(mEngine->GetRenderer()->GetTexture(mEngine->GetRenderer()->GetTextureStoragePath() + normal));
// try to load height map
string height = diffuse.substr(0, diffuse.find_first_of('.')) + "_height" + diffuse.substr(diffuse.find_first_of('.'));
if(FileExist(mEngine->GetRenderer()->GetTextureStoragePath() + height)) {
mesh->mHeightTexture = dynamic_pointer_cast<Texture>(mEngine->GetRenderer()->GetTexture(mEngine->GetRenderer()->GetTextureStoragePath() + height));
}
}
node->AddMesh(mesh);
if(node->mIsSkinned) {
for(int k = 0; k < vertexCount; k++) {
Mesh::BoneGroup boneGroup;
boneGroup.mBoneCount = reader.GetInteger();
for(int j = 0; j < boneGroup.mBoneCount; j++) {
// number of scene node represents bone in the scene
boneGroup.mBone[j].mID = reader.GetInteger();
boneGroup.mBone[j].mWeight = reader.GetFloat();
boneGroup.mBone[j].mRealBone = nullptr;
}
mesh->AddBoneGroup(boneGroup);
}
}
if(vertexCount != 0) {
if(!mesh->IsSkinned()) {
mesh->CreateHardwareBuffers();
}
mEngine->GetRenderer()->AddMesh(mesh);
}
}
node->mParent = scene;
scene->mChildren.push_back(node);
node->mScene = scene;
node->ApplyProperties();
}
// remap bone id's to real scene nodes
for(auto & child : scene->mChildren) {
if(child->mIsSkinned) {
for(auto pMesh : child->mMeshList) {
for(auto & w : pMesh->GetBoneTable()) {
for(int i = 0; i < w.mBoneCount; ++i) {
shared_ptr<SceneNode> & bone = scene->mChildren[w.mBone[i].mID];
if(bone) {
w.mBone[i].mRealBone = pMesh->AddBone(bone);
}
}
}
pMesh->CreateHardwareBuffers();
}
}
}
for(int lightObjectNum = 0; lightObjectNum < numLights; lightObjectNum++) {
shared_ptr<Light> light;
string name = reader.GetString();
int type = reader.GetInteger();
if(type == 0) {
light = dynamic_pointer_cast<Light>(CreatePointLight());
} else if(type == 1) {
light = dynamic_pointer_cast<Light>(CreateSpotLight());
} else if(type == 2) {
light = dynamic_pointer_cast<Light>(CreateDirectionalLight());
}
light->mName = name;
light->SetColor(reader.GetBareVector());
light->SetRange(reader.GetFloat());
float brightness = reader.GetFloat();
light->mLocalTransform.setOrigin(reader.GetVector());
light->mScene = scene;
light->mParent = scene;
scene->mChildren.push_back(light);
if(type == 1) { // spot
shared_ptr<SpotLight> & spot = std::dynamic_pointer_cast<SpotLight>(light);
float in = reader.GetFloat();
float out = reader.GetFloat();
spot->SetConeAngles(in, out);
light->mLocalTransform.setRotation(reader.GetQuaternion());
} else if(type == 2) { // directional
light->mLocalTransform.setRotation(reader.GetQuaternion());
}
}
for(auto child : scene->mChildren) {
string objectName = reader.GetString();
string parentName = reader.GetString();
auto object = SceneNode::FindChildInNodeNonRecursive(scene, objectName);
auto parent = SceneNode::FindChildInNodeNonRecursive(scene, parentName);
if(parent) {
parent->mChildren.push_back(object);
object->mParent = parent;
}
}
for(auto & node : scene->mChildren) {
node->mInverseBindTransform = node->CalculateGlobalTransform().inverse();
}
return scene;
}
int SceneFactory::GetNodeCount() {
return GetNodeList().size();
}
int SceneFactory::GetSpotLightCount() {
return GetSpotLightList().size();
}
shared_ptr<ISpotLight> SceneFactory::GetSpotLight(int n) {
return GetSpotLightList()[n].lock();
}
int SceneFactory::GetPointLightCount() {
return GetPointLightList().size();
}
shared_ptr<IPointLight> SceneFactory::GetPointLight(int n) {
return GetPointLightList()[n].lock();
}
int SceneFactory::GetDirectionalLightCount() {
return GetDirectionalLightList().size();
}
shared_ptr<IDirectionalLight> SceneFactory::GetDirectionalLight(int n) {
return GetDirectionalLightList()[n].lock();
}
void SceneFactory::SetPointLightDefaultTexture(const shared_ptr<ICubeTexture>& defaultPointTexture) {
mDefaultPointCubeTexture = dynamic_pointer_cast<CubeTexture>(defaultPointTexture);
}
shared_ptr<ICubeTexture> SceneFactory::GetPointLightDefaultTexture() const {
return mDefaultPointCubeTexture;
}
void SceneFactory::SetSpotLightDefaultTexture(const shared_ptr<ITexture>& defaultSpotTexture) {
mDefaultSpotTexture = dynamic_pointer_cast<Texture>(defaultSpotTexture);
}
shared_ptr<ITexture> SceneFactory::GetSpotLightDefaultTexture() const {
return mDefaultSpotTexture;
}
shared_ptr<ISceneNode> SceneFactory::GetNode(int i) {
return GetNodeList()[i].lock();
}
vector<shared_ptr<ISceneNode>> SceneFactory::GetTaggedObjects(const string & tag) {
vector<shared_ptr<ISceneNode>> tagged;
for(auto weakNode : GetNodeList()) {
auto node = weakNode.lock();
if(node->GetTag() == tag) {
tagged.push_back(node);
}
}
return tagged;
}
template<typename Type>
void SceneFactory::RemoveUnreferenced(vector<weak_ptr<Type>> & objList) {
for(auto iter = objList.begin(); iter != objList.end(); ) {
if((*iter).use_count()) {
++iter;
} else {
iter = objList.erase(iter);
}
}
}
vector<weak_ptr<ParticleSystem>> & SceneFactory::GetParticleSystemList() {
RemoveUnreferenced(msParticleEmitters);
return msParticleEmitters;
}
vector<weak_ptr<Fog>>& SceneFactory::GetFogList() {
RemoveUnreferenced(msFogList);
return msFogList;
}