This repository has been archived by the owner on Apr 29, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMesh.h
262 lines (214 loc) · 5.79 KB
/
Mesh.h
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
#pragma once
#include<iostream>
#include<vector>
#include"Vertex.h"
#include"Primitive.h"
#include"Shader.h"
#include"Texture.h"
#include"Material.h"
class Mesh
{
private:
Vertex * vertexArray;
unsigned nrOfVertices;
GLuint* indexArray;
unsigned nrOfIndices;
GLuint VAO;
GLuint VBO;
GLuint EBO;
glm::vec3 position;
glm::vec3 origin;
glm::vec3 rotation;
glm::vec3 scale;
glm::mat4 ModelMatrix;
void initVAO()
{
//Create VAO
glCreateVertexArrays(1, &this->VAO);
glBindVertexArray(this->VAO);
//GEN VBO AND BIND AND SEND DATA
glGenBuffers(1, &this->VBO);
glBindBuffer(GL_ARRAY_BUFFER, this->VBO);
glBufferData(GL_ARRAY_BUFFER, this->nrOfVertices * sizeof(Vertex), this->vertexArray, GL_STATIC_DRAW);
//GEN EBO AND BIND AND SEND DATA
if (this->nrOfIndices > 0)
{
glGenBuffers(1, &this->EBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, this->nrOfIndices * sizeof(GLuint), this->indexArray, GL_STATIC_DRAW);
}
//SET VERTEXATTRIBPOINTERS AND ENABLE (INPUT ASSEMBLY)
//Position
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, position));
glEnableVertexAttribArray(0);
//Color
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, color));
glEnableVertexAttribArray(1);
//Texcoord
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, texcoord));
glEnableVertexAttribArray(2);
//Normal
glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, normal));
glEnableVertexAttribArray(3);
//BIND VAO 0
glBindVertexArray(0);
}
void updateUniforms(Shader* shader)
{
shader->setMat4fv(this->ModelMatrix, "ModelMatrix");
}
void updateModelMatrix()
{
this->ModelMatrix = glm::mat4(1.f);
this->ModelMatrix = glm::translate(this->ModelMatrix, this->origin);
this->ModelMatrix = glm::rotate(this->ModelMatrix, glm::radians(this->rotation.x), glm::vec3(1.f, 0.f, 0.f));
this->ModelMatrix = glm::rotate(this->ModelMatrix, glm::radians(this->rotation.y), glm::vec3(0.f, 1.f, 0.f));
this->ModelMatrix = glm::rotate(this->ModelMatrix, glm::radians(this->rotation.z), glm::vec3(0.f, 0.f, 1.f));
this->ModelMatrix = glm::translate(this->ModelMatrix, this->position - this->origin);
this->ModelMatrix = glm::scale(this->ModelMatrix, this->scale);
}
public:
Mesh(
Vertex* vertexArray,
const unsigned& nrOfVertices,
GLuint* indexArray,
const unsigned& nrOfIndices,
glm::vec3 position = glm::vec3(0.f),
glm::vec3 origin = glm::vec3(0.f),
glm::vec3 rotation = glm::vec3(0.f),
glm::vec3 scale = glm::vec3(1.f))
{
this->position = position;
this->origin = origin;
this->rotation = rotation;
this->scale = scale;
this->nrOfVertices = nrOfVertices;
this->nrOfIndices = nrOfIndices;
this->vertexArray = new Vertex[this->nrOfVertices];
for (size_t i = 0; i < nrOfVertices; i++)
{
this->vertexArray[i] = vertexArray[i];
}
this->indexArray = new GLuint[this->nrOfIndices];
for (size_t i = 0; i < nrOfIndices; i++)
{
this->indexArray[i] = indexArray[i];
}
this->initVAO();
this->updateModelMatrix();
}
Mesh(
Primitive* primitive,
glm::vec3 position = glm::vec3(0.f),
glm::vec3 origin = glm::vec3(0.f),
glm::vec3 rotation = glm::vec3(0.f),
glm::vec3 scale = glm::vec3(1.f))
{
this->position = position;
this->origin = origin;
this->rotation = rotation;
this->scale = scale;
this->nrOfVertices = primitive->getNumOfVertices();
this->nrOfIndices = primitive->getNumOfIndices();
this->vertexArray = new Vertex[this->nrOfVertices];
for (size_t i = 0; i < this->nrOfVertices; i++)
{
this->vertexArray[i] = primitive->getVertices()[i];
}
this->indexArray = new GLuint[this->nrOfIndices];
for (size_t i = 0; i < this->nrOfIndices; i++)
{
this->indexArray[i] = primitive->getIndices()[i];
}
this->initVAO();
this->updateModelMatrix();
}
Mesh(const Mesh& obj)
{
this->position = obj.position;
this->origin = obj.origin;
this->rotation = obj.rotation;
this->scale = obj.scale;
this->nrOfVertices = obj.nrOfVertices;
this->nrOfIndices = obj.nrOfIndices;
this->vertexArray = new Vertex[this->nrOfVertices];
for (size_t i = 0; i < this->nrOfVertices; i++)
{
this->vertexArray[i] = obj.vertexArray[i];
}
this->indexArray = new GLuint[this->nrOfIndices];
for (size_t i = 0; i < this->nrOfIndices; i++)
{
this->indexArray[i] = obj.indexArray[i];
}
this->initVAO();
this->updateModelMatrix();
}
~Mesh()
{
glDeleteVertexArrays(1, &this->VAO);
glDeleteBuffers(1, &this->VBO);
if (this->nrOfIndices > 0)
{
glDeleteBuffers(1, &this->EBO);
}
delete[] this->vertexArray;
delete[] this->indexArray;
}
//Accessors
glm::vec3 getPosition() {
return this->position;
}
//Modifiers
void setPosition(const glm::vec3 position)
{
this->position = position;
}
void setOrigin(const glm::vec3 origin)
{
this->origin = origin;
}
void setRotation(const glm::vec3 rotation)
{
this->rotation = rotation;
}
void setScale(const glm::vec3 setScale)
{
this->scale = scale;
}
//Functions
void move(const glm::vec3 position)
{
this->position += position;
}
void rotate(const glm::vec3 rotation)
{
this->rotation += rotation;
}
void scaleUp(const glm::vec3 scale)
{
this->scale += scale;
}
void update()
{
}
void render(Shader* shader)
{
//Update uniforms
this->updateModelMatrix();
this->updateUniforms(shader);
shader->use();
//Bind VAO
glBindVertexArray(this->VAO);
//RENDER
if (this->nrOfIndices == 0)
glDrawArrays(GL_TRIANGLES, 0, this->nrOfVertices);
else
glDrawElements(GL_TRIANGLES, this->nrOfIndices, GL_UNSIGNED_INT, 0);
//Cleanup
glBindVertexArray(0);
glUseProgram(0);
glActiveTexture(0);
glBindTexture(GL_TEXTURE_2D, 0);
}
};