-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMeshImporter.cpp
More file actions
102 lines (76 loc) · 3.09 KB
/
MeshImporter.cpp
File metadata and controls
102 lines (76 loc) · 3.09 KB
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
#include "MeshImporter.h"
#include "Application.h"
#include "ModuleFilesystem.h"
using namespace std;
bool MeshImporter::Import(const char * file, const MeshData & mesh, string & output_file)
{
unsigned int ranges[2] = { mesh.num_indices, mesh.num_vertices };
unsigned int size = sizeof(ranges) //ranges
+ sizeof(unsigned int) * mesh.num_indices //indices
+ sizeof(float) * mesh.num_vertices * 3 //vertex positions
+ sizeof(float) * mesh.num_vertices * 3 //vertex normals
+ sizeof(float) * mesh.num_vertices * 2; //vertex texture coord
// + sizeof(float) * 6; //AABB
//TODO: add AABB and color to save and to load
char* data = new char[size]; // Allocate
char* cursor = data;
unsigned int bytes = sizeof(ranges); // First store ranges
memcpy(cursor, ranges, bytes);
cursor += bytes; // Store indices
bytes = sizeof(unsigned int) * mesh.num_indices;
memcpy(cursor, mesh.indices, bytes);
cursor += bytes; // Store vertex positions
bytes = sizeof(float) * mesh.num_vertices * 3;
memcpy(cursor, mesh.positions, bytes);
cursor += bytes; // Store vertex normals
bytes = sizeof(float) * mesh.num_vertices * 3;
memcpy(cursor, mesh.normals, bytes);
cursor += bytes; // Store vertex texture coords
bytes = sizeof(float) * mesh.num_vertices * 2;
memcpy(cursor, mesh.texture_coords, bytes);
bool succes = Import(file, data, size, output_file);
delete[] data;
return succes;
}
bool MeshImporter::Import(const char * file, const void * buffer, unsigned int size, std::string & output_file)
{
if (!App->filesystem->IsDirectory("../Library"))
App->filesystem->MakeDirectory("../Library");
if (!App->filesystem->IsDirectory("../Library/Meshes"))
App->filesystem->MakeDirectory("../Library/Meshes");
string filename = file; filename += ".mesh";
output_file = file;
return App->filesystem->Save("../Library/Meshes/", filename.c_str(), buffer, size, false);
}
bool MeshImporter::Load(const char * exported_file, MeshData & mesh)
{
string mesh_file = exported_file; mesh_file += ".mesh";
char* buffer = App->filesystem->Load("../Library/Meshes/", mesh_file.c_str());
if (buffer == NULL)
return false;
char* cursor = buffer;
unsigned int ranges[2]; //Load ranges
unsigned int bytes = sizeof(ranges);
memcpy(ranges, cursor, bytes);
mesh.num_indices = ranges[0];
mesh.num_vertices = ranges[1];
cursor += bytes; // Load indices
bytes = sizeof(unsigned int) * mesh.num_indices;
mesh.indices = new unsigned int[mesh.num_indices];
memcpy(mesh.indices, cursor, bytes);
cursor += bytes; // Load vertex positions
bytes = sizeof(float) * mesh.num_vertices * 3;
mesh.positions = new float[mesh.num_vertices * 3];
memcpy(mesh.positions, cursor, bytes);
cursor += bytes; // Load vertex normals
bytes = sizeof(float) * mesh.num_vertices * 3;
mesh.normals = new float[mesh.num_vertices * 3];
memcpy(mesh.normals, cursor, bytes);
cursor += bytes; // Load vertex texture coords
bytes = sizeof(float) * mesh.num_vertices * 2;
mesh.texture_coords = new float[mesh.num_vertices * 2];
memcpy(mesh.texture_coords, cursor, bytes);
mesh.name = exported_file;
delete[] buffer;
return true;
}