forked from apurvsinghdark/GraphicProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mesh.h
265 lines (208 loc) · 6.36 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
263
264
265
#pragma once
#include<iostream>
#include"Texture.h"
#include"Material.h"
#include<vector>
#include"Vertex.h"
#include"Primitives.h"
class Mesh
{
private:
Vertex* vertexArray;
unsigned noOfVertices;
GLuint* indexArray;
unsigned noOfIndices;
GLuint VAO;
GLuint VBO;
GLuint EBO;
glm::vec3 position;
glm::vec3 origin;
glm::vec3 rotation;
glm::vec3 scale;
glm::mat4 ModelMatrix;
void InitVAO();
void UpdateUniforms(Shader* shader);
void UpdateModelMatrix();
public:
Mesh(
Primitive* primitive,
glm::vec3 position = glm::vec3(0.0f),
glm::vec3 origin = glm::vec3(0.0f),
glm::vec3 rotation = glm::vec3(0.0f),
glm::vec3 scale = glm::vec3(1.0f)
);
Mesh(
Vertex* vertexArray,
const unsigned& noOfVertices,
GLuint* indexArray,
const unsigned& noOfIndices,
glm::vec3 position = glm::vec3(0.0f),
glm::vec3 origin = glm::vec3(0.0f),
glm::vec3 rotation = glm::vec3(0.0f),
glm::vec3 scale = glm::vec3(1.0f)
);
Mesh(const Mesh& obj);
~Mesh();
void Update();
void Render(Shader* shader);
//Modifiers
void SetPosition(const glm::vec3 position);
void SetOrigin(const glm::vec3 origin);
void SetRotation(const glm::vec3 rotation);
void SetScale(const glm::vec3 scale);
//Translation/Rotation/Scaling
void Move(const glm::vec3 position);
void Rotate(const glm::vec3 rotation);
void Scale(const glm::vec3 scale);
};
void Mesh::InitVAO()
{
//init VAO(Vertex Array) and Bind it
glGenVertexArrays(1, &this->VAO);
glBindVertexArray(this->VAO);
//init VBO and bind
glGenBuffers(1, &this->VBO);
glBindBuffer(GL_ARRAY_BUFFER, this->VBO);
glBufferData(GL_ARRAY_BUFFER, this->noOfVertices * sizeof(Vertex), this->vertexArray, GL_STATIC_DRAW);
//init EBO and bind
if ( this->noOfIndices > 0 )
{
glGenBuffers(1, &this->EBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, this->noOfIndices * sizeof(GLuint), this->indexArray, GL_STATIC_DRAW);
}
// Set ATTRIBPOINTERS and ATTRIBARRAYS (Input Assembly)
///FOR position
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, position));
glEnableVertexAttribArray(0);
/// For Color
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, color));
glEnableVertexAttribArray(1);
/// For texture(Texcoord)
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, texcoord));
glEnableVertexAttribArray(2);
//Normal
glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, normal));
glEnableVertexAttribArray(3);
//Bind Array
glBindVertexArray(this->VAO);
}
void Mesh::UpdateUniforms(Shader* shader)
{
shader->SetMat4("ModelMatrix", this->ModelMatrix);
}
void Mesh::UpdateModelMatrix()
{
this->ModelMatrix = glm::mat4(1.0f);
this->ModelMatrix = glm::translate(this->ModelMatrix, this->origin);
this->ModelMatrix = glm::rotate(this->ModelMatrix, glm::radians(this->rotation.x), glm::vec3(1.0f, 0.0f, 0.0f));
this->ModelMatrix = glm::rotate(this->ModelMatrix, glm::radians(this->rotation.y), glm::vec3(0.0f, 1.0f, 0.0f));
this->ModelMatrix = glm::rotate(this->ModelMatrix, glm::radians(this->rotation.z), glm::vec3(0.0f, 0.0f, 1.0f));
this->ModelMatrix = glm::translate(this->ModelMatrix, this->position - this->origin);
this->ModelMatrix = glm::scale(this->ModelMatrix, this->scale);
}
Mesh::Mesh(Primitive* primitive, glm::vec3 position, glm::vec3 origin, glm::vec3 rotation, glm::vec3 scale)
{
this->position = position;
this->origin = origin;
this->rotation = rotation;
this->scale = scale;
this->noOfVertices = primitive->GetNoOfVertices();
this->noOfIndices = primitive->GetNoOfIndices();
this->vertexArray = new Vertex[this->noOfVertices];
for (size_t i = 0; i < this->noOfVertices; i++)
{
this->vertexArray[i] = primitive->GetVertices()[i];
}
this->indexArray = new GLuint[this->noOfIndices];
for (size_t i = 0; i < this->noOfIndices; i++)
{
this->indexArray[i] = primitive->GetIndices()[i];
}
this->InitVAO();
this->UpdateModelMatrix();
}
Mesh::Mesh(
Vertex* vertexArray,
const unsigned& noOfVertices,
GLuint* indexArray,
const unsigned& noOfIndices,
glm::vec3 position,
glm::vec3 origin,
glm::vec3 rotation,
glm::vec3 scale
)
{
this->position = position;
this->rotation = rotation;
this->scale = scale;
this->noOfVertices = noOfVertices;
this->noOfIndices = noOfIndices;
this->vertexArray = new Vertex[this->noOfVertices];
for (size_t i = 0; i < noOfVertices; i++)
{
this->vertexArray[i] = vertexArray[i];
}
this->indexArray = new GLuint[this->noOfIndices];
for (size_t i = 0; i < noOfIndices; i++)
{
this->indexArray[i] = indexArray[i];
}
this->InitVAO();
this->UpdateModelMatrix();
}
Mesh::Mesh(const Mesh& obj)
{
this->position = obj.position;
this->origin = obj.origin;
this->rotation = obj.rotation;
this->scale = obj.scale;
this->noOfVertices = obj.noOfVertices;
this->noOfIndices = obj.noOfIndices;
this->vertexArray = new Vertex[this->noOfVertices];
for (size_t i = 0; i < this->noOfVertices; i++)
{
this->vertexArray[i] = obj.vertexArray[i];
}
this->indexArray = new GLuint[this->noOfIndices];
for (size_t i = 0; i < this->noOfIndices; i++)
{
this->indexArray[i] = obj.indexArray[i];
}
this->InitVAO();
this->UpdateModelMatrix();
}
void Mesh::Render(Shader* shader)
{
//UpdateUniforms
this->UpdateModelMatrix();
this->UpdateUniforms(shader);
//BindingShader
shader->Use();
//Arrays BINDING
glBindVertexArray(this->VAO);
//DRAW
if (this->noOfIndices == 0)
glDrawArrays(GL_TRIANGLES, 0, this->noOfVertices);
else
glDrawElements(GL_TRIANGLES, this->noOfIndices, GL_UNSIGNED_INT, 0);
}
inline void Mesh::SetPosition(const glm::vec3 position) { this->position = position; }
inline void Mesh::SetOrigin(const glm::vec3 origin) { this->origin = origin; }
inline void Mesh::SetRotation(const glm::vec3 rotation) { this->rotation = rotation; }
inline void Mesh::SetScale(const glm::vec3 scale) { this->scale = scale; }
inline void Mesh::Move(const glm::vec3 position) { this->position += position; }
inline void Mesh::Rotate(const glm::vec3 rotation) { this->rotation += rotation; }
inline void Mesh::Scale(const glm::vec3 scale) { this->scale += scale; }
Mesh::~Mesh()
{
glDeleteVertexArrays(1, &this->VAO);
glDeleteBuffers(1, &this->VBO);
if (this->noOfIndices > 0)
glDeleteBuffers(1, &this->EBO);
delete[] this->vertexArray;
delete[] this->indexArray;
}
void Mesh::Update()
{
}