-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmeshbuffer.cpp
More file actions
407 lines (336 loc) · 10.8 KB
/
meshbuffer.cpp
File metadata and controls
407 lines (336 loc) · 10.8 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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
#include "meshbuffer.h"
#include <assert.h>
#include <stdlib.h>
#include <iostream>
#include "vertexattributeindices.h"
MeshBuffer::MeshBuffer()
: UsesNormals(false)
, UsesUVs(false)
, UsesIndices(false)
, UsesGenerics(5, false)
, VertCnt(0)
, IdxCnt(0)
, Generics(5)
{
}
MeshBuffer::~MeshBuffer()
{
cleanUp();
}
MeshBuffer::MeshBuffer(const MeshBuffer & ref)
: UsesNormals(false)
, UsesUVs(false)
, UsesIndices(false)
, VertCnt(0)
, IdxCnt(0)
{
this->operator =(ref);
}
MeshBuffer& MeshBuffer::operator=(const MeshBuffer & ref)
{
if (this == &ref) return *this;
cleanUp();
setVerts(ref.VertCnt, (float*)&ref.Verts[0]);
setNorms(ref.VertCnt, (float*)&ref.Norms[0]);
setTexCoords(0, ref.VertCnt, (float*)&ref.TexCoords[0]);
for (size_t i=0; i<5; ++i)
if (ref.UsesGenerics[i])
setGenerics(i, ref.getGenerics(i));
setIndices(ref.IdxCnt, (uint32_t*)&ref.Indices[0]);
return *this;
}
void MeshBuffer::loadFileObj(const char * fileName)
{
// get the mesh
#ifdef HIDEME
AttributeContainer<Attributes::Position> constPosition = mesh->GetAttributes<Attributes::Position>( size_t(0u) );
AttributeContainer<Attributes::Position>::const_iterator pos_itr = constPosition.begin();
AttributeContainer<Attributes::Normal> constNormal = mesh->GetAttributes<Attributes::Normal>( size_t(0u) );
AttributeContainer<Attributes::Normal>::const_iterator norm_itr = constNormal.begin();
AttributeContainer<Attributes::TexCoord> constTexCoord = mesh->GetAttributes<Attributes::TexCoord>( size_t(0u) );
AttributeContainer<Attributes::TexCoord>::const_iterator tex_itr = constTexCoord.begin();
FaceContainer faces = mesh->GetFaces();
FaceContainer::const_iterator face_itr = faces.begin();
/* The Obj file format description
# comments
# positions
v f f f
# texture coords
vt f f
# normals
vn f f f
# faces (faces start at 1, not 0)
# faces with positions only
f 1 2 3
# faces with positions and norms
f 1//1 2//2 3//3
# faces with positions and textures
f 1/1 2/2 3/3
# faces with positions, norms and textures
f 1/1/1 2/2/2 3/3/3
*/
std::ofstream out_file(filename.c_str());
out_file << "# Medical Simulation Corparation" << std::endl;
// send in the positions
for (; pos_itr != constPosition.end(); ++pos_itr)
{
out_file << "v " << (*pos_itr)[0] << " " << (*pos_itr)[1] << " " << (*pos_itr)[2] << "\n";
}
out_file << std::endl;
// send in the texCoords
for (; tex_itr != constTexCoord.end(); ++tex_itr)
{
out_file << "vt " << tex_itr->u << " " << tex_itr->v << "\n";
}
out_file << std::endl;
// and now the normals
for (; norm_itr != constNormal.end(); ++norm_itr)
{
out_file << "vn " << (*norm_itr)[0] << " " << (*norm_itr)[1] << " " << (*norm_itr)[2] << "\n";
}
out_file << std::endl;
// set up the faces
// Our meshes are kept interleaved, so the relationship between vert attributes are on a 1 to 1 basis
if (!constNormal.empty() && !constTexCoord.empty())
{
for (; face_itr != faces.end(); ++face_itr)
{
unsigned short a = (*face_itr)[0]+1;
unsigned short b = (*face_itr)[1]+1;
unsigned short c = (*face_itr)[2]+1;
out_file << "f " << a << "/" << a << "/" << a << " "
<< b << "/" << b << "/" << b << " "
<< c << "/" << c << "/" << c << "\n";
}
}
else if (!constTexCoord.empty())
{
for (; face_itr != faces.end(); ++face_itr)
{
unsigned short a = (*face_itr)[0]+1;
unsigned short b = (*face_itr)[1]+1;
unsigned short c = (*face_itr)[2]+1;
out_file << "f " << a << "/" << a << " "
<< b << "/" << b << " "
<< c << "/" << c << "\n";
}
}
else if (!constNormal.empty())
{
for (; face_itr != faces.end(); ++face_itr)
{
unsigned short a = (*face_itr)[0]+1;
unsigned short b = (*face_itr)[1]+1;
unsigned short c = (*face_itr)[2]+1;
out_file << "f " << a << "//" << a << " "
<< b << "//" << b << " "
<< c << "//" << c << "\n";
}
}
else
{
for (; face_itr != faces.end(); ++face_itr)
{
out_file << "f " << (*face_itr)[0] << " " << (*face_itr)[1] << " " << (*face_itr)[2] << "\n";
}
}
out_file << std::endl;
out_file.close();
#endif
}
void MeshBuffer::loadFileStl(const char * fileName)
{
std::ifstream in_file(filename, std::ifstream::binary);
/*
The Stl file format description
From: https://en.wikipedia.org/wiki/STL_(file_format)
UINT8[80] – Header, ignored by most applications
UINT32 – Number of triangles
foreach triangle
REAL32[3] – Normal vector, if the normal is (0,0,0) most applications will generate the facet normal
REAL32[3] – Vertex 1
REAL32[3] – Vertex 2
REAL32[3] – Vertex 3
UINT16 – Attribute byte count, almost no applications use this and should be 0
end
*/
/*
unsigned int VertCnt;
unsigned int IdxCnt;
std::vector<glm::vec3> Verts;
std::vector<glm::vec3> Norms;
std::vector<glm::vec2> TexCoords;
std::vector<uint32_t> Indices;
*/
uint8_t header[80] = { 0 };
uint32_t num_triangles = 0;
in_file.read((const char*)&header[0], 80);
in_file.read((const char*)&num_triangles, sizeof(uint32_t));
VertCnt = num_triangles / 3;
IdxCnt = ;
for (uint32_t i = 0; i < num_triangles; ++i)
{
glm::vec3 verta, vertb, vertc;
glm::vec3 normal;
in_file.write((const char*)&normal, sizeof(glm::vec3));
in_file.write((const char*)&verta, sizeof(glm::vec3));
in_file.write((const char*)&vertb, sizeof(glm::vec3));
in_file.write((const char*)&vertc, sizeof(glm::vec3));
// move to the next bytes.
uint16_t null;
in_file.write((const char*)&null, sizeof(uint16_t));
}
in_file.close();
}
void MeshBuffer::setVerts(unsigned int count, const float* verts)
{
if (!verts) return;
VertCnt = count;
Verts.resize(VertCnt);
int src_num_componets = 3;
for (unsigned int i=0; i<VertCnt; ++i)
{
glm::vec3 vec;
vec[0] = verts[i * src_num_componets + 0];
vec[1] = verts[i * src_num_componets + 1];
vec[2] = verts[i * src_num_componets + 2];
Verts[i] = vec;
}
}
void MeshBuffer::setNorms(unsigned int count, const float* normals)
{
if (count != VertCnt)
{
std::cout << "Vert count does not match the number normals to create" << std::endl;
exit(1);
}
if (!normals) return;
UsesNormals = true;
Norms.clear();
Norms.resize(count);
int src_num_componets = 3;
for (unsigned int i=0; i<VertCnt; ++i)
{
glm::vec3 vec;
vec[0] = normals[i * src_num_componets + 0];
vec[1] = normals[i * src_num_componets + 1];
vec[2] = normals[i * src_num_componets + 2];
Norms[i] = vec;
}
}
void MeshBuffer::setTexCoords(unsigned int layer, unsigned int count, const float* coords)
{
if (count != VertCnt)
{
std::cout << "Vert count does not match the number uvs to create" << std::endl;
exit(1);
}
if (!coords) return;
UsesUVs = true;
TexCoords.clear();
TexCoords.resize(count);
int src_num_componets = 2;
for (unsigned int i=0; i<VertCnt; ++i)
{
glm::vec2 vec;
vec[0] = coords[i * src_num_componets + 0];
vec[1] = coords[i * src_num_componets + 1];
TexCoords[i] = vec;
}
}
void MeshBuffer::setGenerics(unsigned int index, const std::vector<glm::vec4>& values)
{
if (values.size() != VertCnt)
{
std::cout << "Vert count does not match the number generic values to add to vbo" << std::endl;
exit(1);
}
if (index >= (unsigned int)UsesGenerics.size())
{
std::cout << "setGenerics index is not within the valid range of [0-4]" << std::endl;
exit(1);
}
UsesGenerics[index] = true;
Generics[index].clear();
Generics[index].resize(values.size());
Generics[index] = values;
}
void MeshBuffer::setIndices(unsigned int count, const unsigned int * indices)
{
if (!indices) return;
UsesIndices = true;
IdxCnt = count;
Indices.clear();
Indices.resize(IdxCnt);
for (unsigned int i=0; i<IdxCnt; ++i)
{
Indices[i] = indices[i];
}
}
const std::vector<glm::vec3>& MeshBuffer::getVerts() const
{
return Verts;
}
const std::vector<glm::vec3>& MeshBuffer::getNorms() const
{
return Norms;
}
const std::vector<glm::vec2>& MeshBuffer::getTexCoords(unsigned int layer) const
{
return TexCoords;
}
const std::vector<glm::vec4>& MeshBuffer::getGenerics(unsigned int index) const
{
return Generics[index];
}
const std::vector<uint32_t>& MeshBuffer::getIndices() const
{
return Indices;
}
void MeshBuffer::generateFaceNormals()
{
assert(IdxCnt);
UsesNormals = true;
Norms.clear();
Norms.resize(VertCnt);
for (unsigned int i=0; i<IdxCnt; i+=3){
unsigned int idx_a = i+0;
unsigned int idx_b = i+1;
unsigned int idx_c = i+2;
unsigned int vert_idx_a = Indices[idx_a];
unsigned int vert_idx_b = Indices[idx_b];
unsigned int vert_idx_c = Indices[idx_c];
glm::vec3 vec_a = Verts[vert_idx_a];
glm::vec3 vec_b = Verts[vert_idx_b];
glm::vec3 vec_c = Verts[vert_idx_c];
glm::vec3 edge_ab = vec_b - vec_a;
glm::vec3 edge_ac = vec_c - vec_a;
glm::vec3 norm = glm::normalize( glm::cross( edge_ab, edge_ac ) );
Norms[vert_idx_a] = norm;
Norms[vert_idx_b] = norm;
Norms[vert_idx_c] = norm;
}
}
unsigned int MeshBuffer::getVertCnt() const
{
return VertCnt;
}
unsigned int MeshBuffer::getIdxCnt() const
{
return IdxCnt;
}
void MeshBuffer::cleanUp()
{
UsesNormals = false;
UsesUVs = false;
UsesIndices = false;
VertCnt = 0;
IdxCnt = 0;
Verts.clear();
Norms.clear();
TexCoords.clear();
for (auto& gen: Generics)
gen.clear();
Generics.clear();
Indices.clear();
}