-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMeshImporter.cpp
229 lines (177 loc) · 5.85 KB
/
MeshImporter.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
#include "Application.h"
#include "MeshImporter.h"
#include "ModuleImporter.h"
#include "MeshObject.h"
#include "Mesh_R.h"
MeshImporter::MeshImporter(Application* app, bool start_enabled) : Module(app, start_enabled)
{
}
MeshImporter::~MeshImporter()
{
}
Mesh_R* MeshImporter::ImportMeshResource(aiMesh* mesh, const char* path, const char* fileName, UID ID)
{
Mesh_R* newmesh = new Mesh_R();
newmesh->buffersSize[vertices_size] = mesh->mNumVertices;
newmesh->_vertices = new float[mesh->mNumVertices * 3];
memcpy(newmesh->_vertices, mesh->mVertices, sizeof(float) * mesh->mNumVertices * 3);
if (mesh->HasFaces())
{
newmesh->buffersSize[indices_size] = mesh->mNumFaces;
newmesh->_indices = new uint[mesh->mNumFaces * 3];
for (uint i = 0; i < mesh->mNumFaces; i++)
{
memcpy(&newmesh->_indices[i * 3], &mesh->mFaces[i].mIndices[0], sizeof(uint));
memcpy(&newmesh->_indices[i * 3 + 1], &mesh->mFaces[i].mIndices[1], sizeof(uint));
memcpy(&newmesh->_indices[i * 3 + 2], &mesh->mFaces[i].mIndices[2], sizeof(uint));
}
}
else
newmesh->buffersSize[indices_size] = 0;
if (mesh->HasNormals())
{
newmesh->buffersSize[normals_size] = mesh->mNumVertices;
newmesh->_normals = new float[mesh->mNumVertices * 3];
memcpy(newmesh->_normals, mesh->mNormals, sizeof(float) * mesh->mNumVertices * 3);
}
else
newmesh->buffersSize[normals_size] = 0;
//Loading mesh texture coordinates -----------
if (mesh->HasTextureCoords(0))
{
newmesh->buffersSize[tex_coords_size] = mesh->mNumVertices;
newmesh->_tex_coords = new float[mesh->mNumVertices * 2];
for (unsigned int i = 0; i < mesh->mNumVertices; i++)
{
newmesh->_tex_coords[i * 2] = mesh->mTextureCoords[0][i].x;
newmesh->_tex_coords[i * 2 + 1] = mesh->mTextureCoords[0][i].y;
}
}
else
newmesh->buffersSize[tex_coords_size] = 0;
std::string _path("/Library/Meshes/");
_path.append(to_string(ID));
newmesh->name = fileName;
newmesh->ID = ID;
newmesh->resource_path = _path;
newmesh->original_path.append(path);
SaveMeshResource(newmesh, ID);
return newmesh;
}
bool MeshImporter::SaveMeshResource(const Mesh_R *mesh, UID ID)
{
uint size = mesh->original_path.size() + mesh->name.size() + sizeof(mesh->buffersSize) + (sizeof(uint) * mesh->buffersSize[indices_size] * 3) + (sizeof(float) * mesh->buffersSize[vertices_size] * 3)
+ (sizeof(float) * mesh->buffersSize[normals_size] * 3) + (sizeof(float) * mesh->buffersSize[tex_coords_size] * 2) + sizeof(uint) * 2;
char* data = new char[size];
char* cursor = data;
//Save paths
uint length = mesh->original_path.size();
memcpy(cursor, &length,sizeof(uint));
cursor += sizeof(uint);
memcpy(cursor, mesh->original_path.c_str(), mesh->original_path.size());
cursor += mesh->original_path.size();
length = mesh->name.size();
memcpy(cursor, &length, sizeof(uint));
cursor += sizeof(uint);
memcpy(cursor, mesh->name.c_str(), mesh->name.size());
cursor += mesh->name.size();
//Save Ranges
uint bytes = sizeof(mesh->buffersSize);
memcpy(cursor, mesh->buffersSize, bytes);
cursor += bytes;
//Save Indices
bytes = sizeof(uint) * mesh->buffersSize[indices_size] * 3;
memcpy(cursor, mesh->_indices, bytes);
cursor += bytes;
//Save Vertices
bytes = sizeof(float) * mesh->buffersSize[vertices_size] * 3;
memcpy(cursor, mesh->_vertices, bytes);
cursor += bytes;
//Save Normals
if (mesh->buffersSize[normals_size] > 0) {
bytes = sizeof(float) *mesh->buffersSize[normals_size] * 3;
memcpy(cursor, mesh->_normals, bytes);
cursor += bytes;
}
//Save Normals
if (mesh->buffersSize[tex_coords_size] > 0) {
bytes = sizeof(float) * mesh->buffersSize[tex_coords_size] * 2;
memcpy(cursor, mesh->_tex_coords, bytes);
cursor += bytes;
}
uint ret = App->filesystem->Save(mesh->resource_path.c_str(), data, size);
RELEASE_ARRAY(data);
return true;
}
Mesh_R * MeshImporter::LoadMeshResource(UID ID)
{
std::string path = "/Library/Meshes/";
path.append(std::to_string(ID));
char* buffer;
uint size = App->filesystem->Load(path.c_str(), &buffer);
if (size > 0)
{
Mesh_R* mesh = new Mesh_R();
char * cursor = buffer;
uint stringSize = 0;
uint bytes = sizeof(uint);
memcpy(&stringSize, cursor, bytes);
cursor += bytes;
if (stringSize > 0)
{
char* string = new char[stringSize + 1];
bytes = sizeof(char) * stringSize;
memcpy(string, cursor, bytes);
cursor += bytes;
string[stringSize] = '\0';
mesh->original_path = string;
RELEASE_ARRAY(string);
}
uint nameSize = 0;
memcpy(&nameSize, cursor, sizeof(uint));
cursor += sizeof(uint);
if (nameSize > 0)
{
char* string = new char[nameSize + 1];
bytes = sizeof(char) * nameSize;
memcpy(string, cursor, bytes);
cursor += bytes;
string[nameSize] = '\0';
mesh->name = string;
RELEASE_ARRAY(string);
}
//Load Ranges
bytes = sizeof(mesh->buffersSize);
memcpy(&mesh->buffersSize, cursor, bytes);
cursor += bytes;
//Load Indices
bytes = sizeof(uint) * mesh->buffersSize[indices_size] * 3;
mesh->_indices = new uint[mesh->buffersSize[indices_size] * 3];
memcpy(mesh->_indices, cursor, bytes);
cursor += bytes;
//Load VErtices
bytes = sizeof(float) * mesh->buffersSize[vertices_size] * 3;
mesh->_vertices = new float[mesh->buffersSize[vertices_size] * 3];
memcpy(mesh->_vertices, cursor, bytes);
cursor += bytes;
if (mesh->buffersSize[normals_size] > 0)
{
bytes = sizeof(float) * mesh->buffersSize[normals_size] * 3;
mesh->_normals = new float[mesh->buffersSize[normals_size] * 3];
memcpy(mesh->_normals, cursor, bytes);
cursor += bytes;
}
if (mesh->buffersSize[tex_coords_size] > 0)
{
bytes = sizeof(float) * mesh->buffersSize[tex_coords_size] * 2;
mesh->_tex_coords = new float[mesh->buffersSize[tex_coords_size] * 2];
memcpy(mesh->_tex_coords, cursor, bytes);
cursor += bytes;
}
mesh->ID = ID;
mesh->resource_path = path;
RELEASE_ARRAY(buffer);
return mesh;
}
return nullptr;
}