-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathMyMesh.cpp
446 lines (356 loc) · 10.3 KB
/
MyMesh.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
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
#include "MyMesh.h"
using namespace BTX;
// Mesh Constructor
MyMesh::MyMesh()
{
// Initialize default fields
Init();
}
// Method for intializing default fields
void MyMesh::Init(void)
{
m_bBinded = false;
m_uVertexCount = 0;
m_VAO = 0;
m_VBO = 0;
m_pShaderMngr = ShaderManager::GetInstance();
}
// Destructor
MyMesh::~MyMesh() { Release(); }
// Method that releases the mesh's memory once the destructor is run.
// This will release memory from the graphics card and heap by deleting
// data from the VAO and VBO as well as clearing the vectors containing any
// vertex information.
void MyMesh::Release(void)
{
m_pShaderMngr = nullptr;
if (m_VBO > 0)
glDeleteBuffers(1, &m_VBO);
if (m_VAO > 0)
glDeleteVertexArrays(1, &m_VAO);
m_lVertex.clear();
m_lVertexPos.clear();
m_lVertexCol.clear();
}
// Copy constructor
MyMesh::MyMesh(MyMesh& other)
{
m_bBinded = other.m_bBinded;
m_pShaderMngr = other.m_pShaderMngr;
m_uVertexCount = other.m_uVertexCount;
m_VAO = other.m_VAO;
m_VBO = other.m_VBO;
}
// Copy assignment operator
MyMesh& MyMesh::operator=(MyMesh& other)
{
if (this != &other)
{
Release();
Init();
MyMesh temp(other);
Swap(temp);
}
return *this;
}
// Method for swapping data upon making copies
void MyMesh::Swap(MyMesh& other)
{
std::swap(m_bBinded, other.m_bBinded);
std::swap(m_uVertexCount, other.m_uVertexCount);
std::swap(m_VAO, other.m_VAO);
std::swap(m_VBO, other.m_VBO);
std::swap(m_lVertex, other.m_lVertex);
std::swap(m_lVertexPos, other.m_lVertexPos);
std::swap(m_lVertexCol, other.m_lVertexCol);
std::swap(m_pShaderMngr, other.m_pShaderMngr);
}
void MyMesh::AddVertexPosition(vector3 a_v3Input)
{
m_lVertexPos.push_back(a_v3Input);
m_uVertexCount = m_lVertexPos.size();
}
void MyMesh::AddVertexColor(vector3 a_v3Input)
{
m_lVertexCol.push_back(a_v3Input);
}
void MyMesh::CompleteMesh(vector3 a_v3Color)
{
uint uColorCount = m_lVertexCol.size();
for (uint i = uColorCount; i < m_uVertexCount; ++i)
{
m_lVertexCol.push_back(a_v3Color);
}
}
void MyMesh::CompileOpenGL3X(void)
{
if (m_bBinded)
return;
if (m_uVertexCount == 0)
return;
CompleteMesh();
for (uint i = 0; i < m_uVertexCount; i++)
{
//Position
m_lVertex.push_back(m_lVertexPos[i]);
//Color
m_lVertex.push_back(m_lVertexCol[i]);
}
glGenVertexArrays(1, &m_VAO);//Generate vertex array object
glGenBuffers(1, &m_VBO);//Generate Vertex Buffered Object
glBindVertexArray(m_VAO);//Bind the VAO
glBindBuffer(GL_ARRAY_BUFFER, m_VBO);//Bind the VBO
glBufferData(GL_ARRAY_BUFFER, m_uVertexCount * 2 * sizeof(vector3), &m_lVertex[0], GL_STATIC_DRAW);//Generate space for the VBO
// Position attribute
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 2 * sizeof(vector3), (GLvoid*)0);
// Color attribute
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 2 * sizeof(vector3), (GLvoid*)(1 * sizeof(vector3)));
m_bBinded = true;
glBindVertexArray(0); // Unbind VAO
}
void MyMesh::Render(matrix4 a_mProjection, matrix4 a_mView, matrix4 a_mModel)
{
if (!m_bBinded)
return;
// Use the buffer and shader
GLuint nShader = m_pShaderMngr->GetShaderID("Basic");
glUseProgram(nShader);
//Bind the VAO of this object
glBindVertexArray(m_VAO);
// Get the GPU variables by their name and hook them to CPU variables
GLuint MVP = glGetUniformLocation(nShader, "MVP");
GLuint wire = glGetUniformLocation(nShader, "wire");
//Final Projection of the Camera
matrix4 m4MVP = a_mProjection * a_mView * a_mModel;
glUniformMatrix4fv(MVP, 1, GL_FALSE, glm::value_ptr(m4MVP));
//Solid
glUniform3f(wire, -1.0f, -1.0f, -1.0f);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glDrawArrays(GL_TRIANGLES, 0, m_uVertexCount);
//Wire
glUniform3f(wire, 1.0f, 0.0f, 1.0f);
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glEnable(GL_POLYGON_OFFSET_LINE);
glPolygonOffset(-1.f, -1.f);
glDrawArrays(GL_TRIANGLES, 0, m_uVertexCount);
glDisable(GL_POLYGON_OFFSET_LINE);
glBindVertexArray(0);// Unbind VAO so it does not get in the way of other objects
}
void MyMesh::AddTri(vector3 a_vBottomLeft, vector3 a_vBottomRight, vector3 a_vTopLeft)
{
//C
//| \
//A--B
//This will make the triangle A->B->C
AddVertexPosition(a_vBottomLeft);
AddVertexPosition(a_vBottomRight);
AddVertexPosition(a_vTopLeft);
}
void MyMesh::AddQuad(vector3 a_vBottomLeft, vector3 a_vBottomRight, vector3 a_vTopLeft, vector3 a_vTopRight)
{
//C--D
//| |
//A--B
//This will make the triangle A->B->C and then the triangle C->B->D
AddVertexPosition(a_vBottomLeft);
AddVertexPosition(a_vBottomRight);
AddVertexPosition(a_vTopLeft);
AddVertexPosition(a_vTopLeft);
AddVertexPosition(a_vBottomRight);
AddVertexPosition(a_vTopRight);
}
void MyMesh::GenerateCube(float a_fSize, vector3 a_v3Color)
{
if (a_fSize < 0.01f)
a_fSize = 0.01f;
Release();
Init();
float fValue = a_fSize * 0.5f;
//3--2
//| |
//0--1
vector3 point0(-fValue, -fValue, fValue); //0
vector3 point1(fValue, -fValue, fValue); //1
vector3 point2(fValue, fValue, fValue); //2
vector3 point3(-fValue, fValue, fValue); //3
vector3 point4(-fValue, -fValue, -fValue); //4
vector3 point5(fValue, -fValue, -fValue); //5
vector3 point6(fValue, fValue, -fValue); //6
vector3 point7(-fValue, fValue, -fValue); //7
//F
AddQuad(point0, point1, point3, point2);
//B
AddQuad(point5, point4, point6, point7);
//L
AddQuad(point4, point0, point7, point3);
//R
AddQuad(point1, point5, point2, point6);
//U
AddQuad(point3, point2, point7, point6);
//D
AddQuad(point4, point5, point0, point1);
// Adding information about color
CompleteMesh(a_v3Color);
CompileOpenGL3X();
}
void MyMesh::GenerateCuboid(vector3 a_v3Dimensions, vector3 a_v3Color)
{
Release();
Init();
vector3 v3Value = a_v3Dimensions * 0.5f;
//3--2
//| |
//0--1
vector3 point0(-v3Value.x, -v3Value.y, v3Value.z); //0
vector3 point1(v3Value.x, -v3Value.y, v3Value.z); //1
vector3 point2(v3Value.x, v3Value.y, v3Value.z); //2
vector3 point3(-v3Value.x, v3Value.y, v3Value.z); //3
vector3 point4(-v3Value.x, -v3Value.y, -v3Value.z); //4
vector3 point5(v3Value.x, -v3Value.y, -v3Value.z); //5
vector3 point6(v3Value.x, v3Value.y, -v3Value.z); //6
vector3 point7(-v3Value.x, v3Value.y, -v3Value.z); //7
//F
AddQuad(point0, point1, point3, point2);
//B
AddQuad(point5, point4, point6, point7);
//L
AddQuad(point4, point0, point7, point3);
//R
AddQuad(point1, point5, point2, point6);
//U
AddQuad(point3, point2, point7, point6);
//D
AddQuad(point4, point5, point0, point1);
// Adding information about color
CompleteMesh(a_v3Color);
CompileOpenGL3X();
}
void MyMesh::GenerateCone(float a_fRadius, float a_fHeight, int a_nSubdivisions, vector3 a_v3Color)
{
if (a_fRadius < 0.01f)
a_fRadius = 0.01f;
if (a_fHeight < 0.01f)
a_fHeight = 0.01f;
if (a_nSubdivisions < 3)
a_nSubdivisions = 3;
if (a_nSubdivisions > 360)
a_nSubdivisions = 360;
Release();
Init();
// Replace this with your code
Mesh* pMesh = new Mesh();
pMesh->GenerateCone(a_fRadius, a_fHeight, a_nSubdivisions, a_v3Color);
m_lVertexPos = pMesh->GetVertexList();
m_uVertexCount = m_lVertexPos.size();
SafeDelete(pMesh);
// -------------------------------
// Adding information about color
CompleteMesh(a_v3Color);
CompileOpenGL3X();
}
void MyMesh::GenerateCylinder(float a_fRadius, float a_fHeight, int a_nSubdivisions, vector3 a_v3Color)
{
if (a_fRadius < 0.01f)
a_fRadius = 0.01f;
if (a_fHeight < 0.01f)
a_fHeight = 0.01f;
if (a_nSubdivisions < 3)
a_nSubdivisions = 3;
if (a_nSubdivisions > 360)
a_nSubdivisions = 360;
Release();
Init();
// Replace this with your code
Mesh* pMesh = new Mesh();
pMesh->GenerateCylinder(a_fRadius, a_fHeight, a_nSubdivisions, a_v3Color);
m_lVertexPos = pMesh->GetVertexList();
m_uVertexCount = m_lVertexPos.size();
SafeDelete(pMesh);
// -------------------------------
// Adding information about color
CompleteMesh(a_v3Color);
CompileOpenGL3X();
}
void MyMesh::GenerateTube(float a_fOuterRadius, float a_fInnerRadius, float a_fHeight, int a_nSubdivisions, vector3 a_v3Color)
{
if (a_fOuterRadius < 0.01f)
a_fOuterRadius = 0.01f;
if (a_fInnerRadius < 0.005f)
a_fInnerRadius = 0.005f;
if (a_fInnerRadius > a_fOuterRadius)
std::swap(a_fInnerRadius, a_fOuterRadius);
if (a_fHeight < 0.01f)
a_fHeight = 0.01f;
if (a_nSubdivisions < 3)
a_nSubdivisions = 3;
if (a_nSubdivisions > 360)
a_nSubdivisions = 360;
Release();
Init();
// Replace this with your code
Mesh* pMesh = new Mesh();
pMesh->GenerateTube(a_fOuterRadius, a_fInnerRadius, a_fHeight, a_nSubdivisions, a_v3Color);
m_lVertexPos = pMesh->GetVertexList();
m_uVertexCount = m_lVertexPos.size();
SafeDelete(pMesh);
// -------------------------------
// Adding information about color
CompleteMesh(a_v3Color);
CompileOpenGL3X();
}
void MyMesh::GenerateTorus(float a_fOuterRadius, float a_fInnerRadius, int a_nSubdivisionsA, int a_nSubdivisionsB, vector3 a_v3Color)
{
if (a_fOuterRadius < 0.01f)
a_fOuterRadius = 0.01f;
if (a_fInnerRadius < 0.005f)
a_fInnerRadius = 0.005f;
if (a_fInnerRadius > a_fOuterRadius)
std::swap(a_fInnerRadius, a_fOuterRadius);
if (a_nSubdivisionsA < 3)
a_nSubdivisionsA = 3;
if (a_nSubdivisionsA > 360)
a_nSubdivisionsA = 360;
if (a_nSubdivisionsB < 3)
a_nSubdivisionsB = 3;
if (a_nSubdivisionsB > 360)
a_nSubdivisionsB = 360;
Release();
Init();
// Replace this with your code
Mesh* pMesh = new Mesh();
pMesh->GenerateTorus(a_fOuterRadius, a_fInnerRadius, a_nSubdivisionsA, a_nSubdivisionsB, a_v3Color);
m_lVertexPos = pMesh->GetVertexList();
m_uVertexCount = m_lVertexPos.size();
SafeDelete(pMesh);
// -------------------------------
// Adding information about color
CompleteMesh(a_v3Color);
CompileOpenGL3X();
}
void MyMesh::GenerateSphere(float a_fRadius, int a_nSubdivisions, vector3 a_v3Color)
{
if (a_fRadius < 0.01f)
a_fRadius = 0.01f;
//Sets minimum and maximum of subdivisions
if (a_nSubdivisions < 1)
{
GenerateCube(a_fRadius * 2.0f, a_v3Color);
return;
}
if (a_nSubdivisions > 6)
a_nSubdivisions = 6;
Release();
Init();
// Replace this with your code
Mesh* pMesh = new Mesh();
pMesh->GenerateSphere(a_fRadius, a_nSubdivisions, a_v3Color);
m_lVertexPos = pMesh->GetVertexList();
m_uVertexCount = m_lVertexPos.size();
SafeDelete(pMesh);
// -------------------------------
// Adding information about color
CompleteMesh(a_v3Color);
CompileOpenGL3X();
}