-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImporterMesh.cpp
142 lines (114 loc) · 3.78 KB
/
ImporterMesh.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
#include "ImporterMesh.h"
#include "Mesh.h"
#include "Assimp/scene.h"
#include "Leaks.h"
void ImporterMesh::Import(const aiMesh* mesh, Mesh* ourMesh) {
// Copy vertices
ourMesh->numVertex = mesh->mNumVertices;
ourMesh->vertices = new float[ourMesh->numVertex * 3];
memcpy(ourMesh->vertices, mesh->mVertices, sizeof(float) * ourMesh->numVertex * 3);
// Copy faces
if (mesh->HasFaces()) {
ourMesh->numIndices = mesh->mNumFaces * 3; //Each face has 3 vertices
ourMesh->indices = new unsigned[ourMesh->numIndices];
for (unsigned i = 0; i < mesh->mNumFaces; ++i) {
if (mesh->mFaces[i].mNumIndices == 3) {
memcpy(&ourMesh->indices[i * 3], mesh->mFaces[i].mIndices, 3 * sizeof(unsigned));
}
else if (mesh->mFaces[i].mNumIndices == 2) {
memcpy(&ourMesh->indices[i * 3], mesh->mFaces[i].mIndices, 2 * sizeof(unsigned));
ourMesh->indices[i * 3 + 2] = 0;
}
}
}
// Copy textures coordinates
if (mesh->HasTextureCoords(0)) {
ourMesh->textureCoords = new float[ourMesh->numVertex * 2]; //Each vertex has 2 texture coordinates
for (unsigned i = 0; i < mesh->mNumVertices; ++i) {
ourMesh->textureCoords[i * 2] = mesh->mTextureCoords[0][i].x;
ourMesh->textureCoords[i * 2 + 1] = mesh->mTextureCoords[0][i].y;
}
}
// Copy vertices normals
if (mesh->HasNormals()) {
ourMesh->normals = new float[ourMesh->numVertex * 3]; // Each vertex has 3 points
memcpy(ourMesh->normals, mesh->mNormals, sizeof(float) * ourMesh->numVertex * 3);
}
}
unsigned ImporterMesh::Save(const Mesh* ourMesh, char** buffer) {
unsigned numText = ourMesh->textureCoords ? ourMesh->numVertex * 2 : 0;
unsigned numNormals = ourMesh->normals ? ourMesh->numVertex * 3 : 0;
unsigned header[4] = {
ourMesh->numVertex, // Num. Vertices
ourMesh->numIndices, // Num. Indices
numText, // Num. Textures
numNormals // Num. Normals
};
unsigned size = sizeof(header)
+ sizeof(float) * ourMesh->numVertex * 3
+ sizeof(unsigned) * ourMesh->numIndices
+ sizeof(float) * numText
+ sizeof(float) * numNormals;
*buffer = new char[size];
char* cursor = *buffer;
// Copy header
unsigned bytes = sizeof(header);
memcpy(cursor, header, bytes);
cursor += bytes;
// Copy vertices
bytes = sizeof(float) * ourMesh->numVertex * 3;
memcpy(cursor, ourMesh->vertices, bytes);
cursor += bytes;
// Copy indices
bytes = sizeof(unsigned) * ourMesh->numIndices;
memcpy(cursor, ourMesh->indices, bytes);
cursor += bytes;
// Copy textures coordinates
if (ourMesh->textureCoords) {
bytes = sizeof(float) * numText;
memcpy(cursor, ourMesh->textureCoords, bytes);
cursor += bytes;
}
// Copy vertices normals
if (ourMesh->normals) {
bytes = sizeof(float) * numNormals;
memcpy(cursor, ourMesh->normals, bytes);
cursor += bytes;
}
return size;
}
void ImporterMesh::Load(const char* buffer, Mesh* ourMesh) {
const char* cursor = buffer;
unsigned header[4];
unsigned bytes = sizeof(header);
memcpy(header, cursor, bytes);
cursor += bytes;
ourMesh->numVertex = header[0];
ourMesh->numIndices = header[1];
unsigned numText = header[2];
unsigned numNorm = header[3];
// Copy vertices
ourMesh->vertices = new float[ourMesh->numVertex * 3];
bytes = sizeof(float) * ourMesh->numVertex * 3;
memcpy(ourMesh->vertices, cursor, bytes);
cursor += bytes;
// Copy indices
ourMesh->indices = new unsigned[ourMesh->numIndices];
bytes = sizeof(unsigned) * ourMesh->numIndices;
memcpy(ourMesh->indices, cursor, bytes);
cursor += bytes;
// Copy textures
if (numText > 0) {
ourMesh->textureCoords = new float[numText];
bytes = sizeof(float) * numText;
memcpy(ourMesh->textureCoords, cursor, bytes);
cursor += bytes;
}
// Copy normals
if (numNorm > 0) {
ourMesh->normals = new float[numNorm];
bytes = sizeof(float) * numNorm;
memcpy(ourMesh->normals, cursor, bytes);
cursor += bytes;
}
}