-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImporterModel.cpp
108 lines (95 loc) · 3.73 KB
/
ImporterModel.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
#include "ImporterModel.h"
#include "rapidjson/document.h"
#include "rapidjson/stringbuffer.h"
#include "rapidjson/writer.h"
#include "GameObject.h"
#include "Application.h"
#include "ModuleFileSystem.h"
#include "ModuleScene.h"
#include "ComponentMeshRenderer.h"
#include "Material.h"
#include "ImporterMaterial.h"
#include "ImporterMesh.h"
#include "Mesh.h"
#include "Quadtree.h"
#include <vector>
#include <map>
#include "Leaks.h"
void ImporterModel::Save(GameObject* model, const char* path) {
rapidjson::Document d;
std::vector<GameObject*> childs;
model->GetAllChilds(childs);
d.SetObject();
rapidjson::Document::AllocatorType& alloc = d.GetAllocator();
rapidjson::Value objects(rapidjson::kArrayType);
{
rapidjson::Value object(rapidjson::kObjectType);
model->WriteToJSON(object, alloc);
objects.PushBack(object, alloc);
for (std::vector<GameObject*>::const_iterator it = childs.begin(); it != childs.end(); ++it) {
rapidjson::Value child(rapidjson::kObjectType);
(*it)->WriteToJSON(child, alloc);
objects.PushBack(child, alloc);
}
}
d.AddMember("Game Objects", objects, alloc);
rapidjson::StringBuffer buffer;
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
d.Accept(writer);
const char* a = buffer.GetString();
std::string filename;
App->FS->GetFileName(path, filename);
App->FS->Save(std::string("Assets/Library/Models/").append(filename).c_str(), a, buffer.GetSize());
}
void ImporterModel::Load(const std::string& name) {
std::map<std::uint32_t, GameObject*> nodeSceneID;
char* buffer;
unsigned read = App->FS->Load((std::string("Assets/Library/Models/") + name).c_str(), &buffer);
rapidjson::Document model;
model.Parse<rapidjson::kParseStopWhenDoneFlag>(buffer).GetParseError(); //With this flag works- Null terminated missing?
LOG("Model parsed");
assert(model.IsObject());
const rapidjson::Value& objects = model["Game Objects"];
assert(objects.IsArray());
for (rapidjson::SizeType i = 0; i < objects.Size(); ++i) {
const rapidjson::Value& object = objects[i];
uint32_t uuid = object["UUID"].GetUint();
uint32_t parentUUID = object["Parent UUID"].GetUint();
std::string name = object["Name"].GetString();
const rapidjson::Value& components = object["Components"];
const rapidjson::Value& component = components[0]; // It's always transform
float3 position;
position.x = component["Position"][0].GetFloat();
position.y = component["Position"][1].GetFloat();
position.z = component["Position"][2].GetFloat();
Quat rotation;
rotation.x = component["Rotation"][0].GetFloat();
rotation.y = component["Rotation"][1].GetFloat();
rotation.z = component["Rotation"][2].GetFloat();
rotation.w = component["Rotation"][3].GetFloat();
float3 scale;
scale.x = component["Scale"][0].GetFloat();
scale.y = component["Scale"][1].GetFloat();
scale.z = component["Scale"][2].GetFloat();
GameObject* parent;
auto search = nodeSceneID.find(parentUUID);
if (search != nodeSceneID.end()) parent = search->second;
else parent = App->scene->GetRoot();
GameObject* node = new GameObject(parent, name.c_str(), position, rotation, scale);
nodeSceneID.insert(std::pair<std::uint32_t, GameObject*>(uuid, node));
for (rapidjson::SizeType i = 1; i < components.Size(); ++i) {
const rapidjson::Value& component = components[i];
int type = component["Component Type"].GetInt();
switch (type) {
case ComponentType::CTMeshRenderer:
ComponentMeshRenderer* meshRenderer = (ComponentMeshRenderer*)node->CreateComponent((ComponentType)component["Component Type"].GetInt());
meshRenderer->LoadFromJSON(component);
App->scene->GetQuadTree()->InsertGameObject(node);
break;
}
}
node->GenerateAABB();
}
RELEASE(buffer);
LOG("Model %s loaded from custom file format", name.c_str());
}