forked from gameprogcpp/code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMesh.cpp
More file actions
362 lines (316 loc) · 9.01 KB
/
Mesh.cpp
File metadata and controls
362 lines (316 loc) · 9.01 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
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
// ----------------------------------------------------------------
// From Game Programming in C++ by Sanjay Madhav
// Copyright (C) 2017 Sanjay Madhav. All rights reserved.
//
// Released under the BSD License
// See LICENSE in root directory for full details.
// ----------------------------------------------------------------
#include "Mesh.h"
#include "Renderer.h"
#include "Texture.h"
#include "VertexArray.h"
#include <rapidjson/document.h>
#include <SDL/SDL_log.h>
#include "Math.h"
#include "LevelLoader.h"
#include <fstream>
namespace
{
union Vertex
{
float f;
uint8_t b[4];
};
const int BinaryVersion = 1;
struct MeshBinHeader
{
// Signature for file type
char mSignature[4] = { 'G', 'M', 'S', 'H' };
// Version
uint32_t mVersion = BinaryVersion;
// Vertex layout type
VertexArray::Layout mLayout = VertexArray::PosNormTex;
// Info about how many of each we have
uint32_t mNumTextures = 0;
uint32_t mNumVerts = 0;
uint32_t mNumIndices = 0;
// Box/radius of mesh, used for collision
AABB mBox{ Vector3::Zero, Vector3::Zero };
float mRadius = 0.0f;
float mSpecPower = 100.0f;
};
}
Mesh::Mesh()
:mBox(Vector3::Infinity, Vector3::NegInfinity)
,mVertexArray(nullptr)
,mRadius(0.0f)
,mSpecPower(100.0f)
{
}
Mesh::~Mesh()
{
}
bool Mesh::Load(const std::string& fileName, Renderer* renderer)
{
mFileName = fileName;
// Try loading the binary file first
if (LoadBinary(fileName + ".bin", renderer))
{
return true;
}
rapidjson::Document doc;
if (!LevelLoader::LoadJSON(fileName, doc))
{
SDL_Log("Failed to load mesh %s", fileName.c_str());
return false;
}
int ver = doc["version"].GetInt();
// Check the version
if (ver != 1)
{
SDL_Log("Mesh %s not version 1", fileName.c_str());
return false;
}
mShaderName = doc["shader"].GetString();
// Set the vertex layout/size based on the format in the file
VertexArray::Layout layout = VertexArray::PosNormTex;
size_t vertSize = 8;
std::string vertexFormat = doc["vertexformat"].GetString();
if (vertexFormat == "PosNormSkinTex")
{
layout = VertexArray::PosNormSkinTex;
// This is the number of "Vertex" unions, which is 8 + 2 (for skinning)s
vertSize = 10;
}
// Load textures
const rapidjson::Value& textures = doc["textures"];
if (!textures.IsArray() || textures.Size() < 1)
{
SDL_Log("Mesh %s has no textures, there should be at least one", fileName.c_str());
return false;
}
mSpecPower = static_cast<float>(doc["specularPower"].GetDouble());
std::vector<std::string> textureNames;
for (rapidjson::SizeType i = 0; i < textures.Size(); i++)
{
// Is this texture already loaded?
std::string texName = textures[i].GetString();
textureNames.emplace_back(texName);
Texture* t = renderer->GetTexture(texName);
if (t == nullptr)
{
// If it's null, use the default texture
t = renderer->GetTexture("Assets/Default.png");
}
mTextures.emplace_back(t);
}
// Load in the vertices
const rapidjson::Value& vertsJson = doc["vertices"];
if (!vertsJson.IsArray() || vertsJson.Size() < 1)
{
SDL_Log("Mesh %s has no vertices", fileName.c_str());
return false;
}
std::vector<Vertex> vertices;
vertices.reserve(vertsJson.Size() * vertSize);
mRadius = 0.0f;
for (rapidjson::SizeType i = 0; i < vertsJson.Size(); i++)
{
// For now, just assume we have 8 elements
const rapidjson::Value& vert = vertsJson[i];
if (!vert.IsArray())
{
SDL_Log("Unexpected vertex format for %s", fileName.c_str());
return false;
}
Vector3 pos(vert[0].GetDouble(), vert[1].GetDouble(), vert[2].GetDouble());
mRadius = Math::Max(mRadius, pos.LengthSq());
mBox.UpdateMinMax(pos);
if (layout == VertexArray::PosNormTex)
{
Vertex v;
// Add the floats
for (rapidjson::SizeType j = 0; j < vert.Size(); j++)
{
v.f = static_cast<float>(vert[j].GetDouble());
vertices.emplace_back(v);
}
}
else
{
Vertex v;
// Add pos/normal
for (rapidjson::SizeType j = 0; j < 6; j++)
{
v.f = static_cast<float>(vert[j].GetDouble());
vertices.emplace_back(v);
}
// Add skin information
for (rapidjson::SizeType j = 6; j < 14; j += 4)
{
v.b[0] = vert[j].GetUint();
v.b[1] = vert[j + 1].GetUint();
v.b[2] = vert[j + 2].GetUint();
v.b[3] = vert[j + 3].GetUint();
vertices.emplace_back(v);
}
// Add tex coords
for (rapidjson::SizeType j = 14; j < vert.Size(); j++)
{
v.f = vert[j].GetDouble();
vertices.emplace_back(v);
}
}
}
// We were computing length squared earlier
mRadius = Math::Sqrt(mRadius);
// Load in the indices
const rapidjson::Value& indJson = doc["indices"];
if (!indJson.IsArray() || indJson.Size() < 1)
{
SDL_Log("Mesh %s has no indices", fileName.c_str());
return false;
}
std::vector<unsigned int> indices;
indices.reserve(indJson.Size() * 3);
for (rapidjson::SizeType i = 0; i < indJson.Size(); i++)
{
const rapidjson::Value& ind = indJson[i];
if (!ind.IsArray() || ind.Size() != 3)
{
SDL_Log("Invalid indices for %s", fileName.c_str());
return false;
}
indices.emplace_back(ind[0].GetUint());
indices.emplace_back(ind[1].GetUint());
indices.emplace_back(ind[2].GetUint());
}
// Now create a vertex array
unsigned int numVerts = static_cast<unsigned>(vertices.size()) / vertSize;
mVertexArray = new VertexArray(vertices.data(), numVerts,
layout, indices.data(), static_cast<unsigned>(indices.size()));
// Save the binary mesh
SaveBinary(fileName + ".bin", vertices.data(),
numVerts, layout, indices.data(),
static_cast<unsigned>(indices.size()),
textureNames, mBox, mRadius,
mSpecPower);
return true;
}
void Mesh::Unload()
{
delete mVertexArray;
mVertexArray = nullptr;
}
Texture* Mesh::GetTexture(size_t index)
{
if (index < mTextures.size())
{
return mTextures[index];
}
else
{
return nullptr;
}
}
void Mesh::SaveBinary(const std::string& fileName, const void* verts,
uint32_t numVerts, VertexArray::Layout layout,
const uint32_t* indices, uint32_t numIndices,
const std::vector<std::string>& textureNames,
const AABB& box, float radius,
float specPower)
{
// Create header struct
MeshBinHeader header;
header.mLayout = layout;
header.mNumTextures =
static_cast<unsigned>(textureNames.size());
header.mNumVerts = numVerts;
header.mNumIndices = numIndices;
header.mBox = box;
header.mRadius = radius;
// Open binary file for writing
std::ofstream outFile(fileName, std::ios::out
| std::ios::binary);
if (outFile.is_open())
{
// Write the header
outFile.write(reinterpret_cast<char*>(&header), sizeof(header));
// For each texture, we need to write the size of the name
// followed by the string (null-terminated)
for (const auto& tex : textureNames)
{
// (Assume file names won't have more than 32k characters)
uint16_t nameSize = static_cast<uint16_t>(tex.length()) + 1;
outFile.write(reinterpret_cast<char*>(&nameSize), sizeof(nameSize));
outFile.write(tex.c_str(), nameSize - 1);
outFile.write("\0", 1);
}
// Figure out number of bytes for each vertex, based on layout
unsigned vertexSize = VertexArray::GetVertexSize(layout);
// Write vertices
outFile.write(reinterpret_cast<const char*>(verts),
numVerts * vertexSize);
// Write indices
outFile.write(reinterpret_cast<const char*>(indices),
numIndices * sizeof(uint32_t));
}
}
bool Mesh::LoadBinary(const std::string& fileName, Renderer* renderer)
{
std::ifstream inFile(fileName, std::ios::in |
std::ios::binary);
if (inFile.is_open())
{
// Read in header
MeshBinHeader header;
inFile.read(reinterpret_cast<char*>(&header), sizeof(header));
// Validate the header signature and version
char* sig = header.mSignature;
if (sig[0] != 'G' || sig[1] != 'M' || sig[2] != 'S' ||
sig[3] != 'H' || header.mVersion != BinaryVersion)
{
return false;
}
// Read in the texture file names
for (uint32_t i = 0; i < header.mNumTextures; i++)
{
// Get the file name size
uint16_t nameSize = 0;
inFile.read(reinterpret_cast<char*>(&nameSize), sizeof(nameSize));
// Make a buffer of this size
char* texName = new char[nameSize];
// Read in the texture name
inFile.read(texName, nameSize);
// Get this texture
Texture* t = renderer->GetTexture(texName);
if (t == nullptr)
{
// If it's null, use the default texture
t = renderer->GetTexture("Assets/Default.png");
}
mTextures.emplace_back(t);
delete[] texName;
}
// Now read in the vertices
unsigned vertexSize = VertexArray::GetVertexSize(header.mLayout);
char* verts = new char[header.mNumVerts * vertexSize];
inFile.read(verts, header.mNumVerts * vertexSize);
// Now read in the indices
uint32_t* indices = new uint32_t[header.mNumIndices];
inFile.read(reinterpret_cast<char*>(indices),
header.mNumIndices * sizeof(uint32_t));
// Now create the vertex array
mVertexArray = new VertexArray(verts, header.mNumVerts,
header.mLayout, indices, header.mNumIndices);
// Cleanup memory
delete[] verts;
delete[] indices;
// Set mBox/mRadius/specular from header
mBox = header.mBox;
mRadius = header.mRadius;
mSpecPower = header.mSpecPower;
return true;
}
return false;
}