-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMesh.cpp
54 lines (44 loc) · 1.58 KB
/
Mesh.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
#include"Mesh.h"
// Setup mesh buffers of verticies and their atributes
void Mesh::setupMesh() {
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vertex), &vertices[0], GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int),
&indices[0], GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, Normal));
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, TexCoords));
glBindVertexArray(0);
}
// Setup textures to shader to uiniform material.diffuse/specular(n)
void Mesh::Draw(Shader& shader) const {
unsigned int diffuseNr = 1;
unsigned int specularNr = 1;
for (size_t i = 0; i < textures.size(); i++) {
glActiveTexture(GL_TEXTURE0 + i);
std::string name;
switch (textures[i].type) {
case TextureType::DIFFUSE:
name = "diffuse";
name += std::to_string(diffuseNr++);
break;
case TextureType::SPECULAR:
name = "specular";
name += std::to_string(specularNr++);
break;
}
shader.SetFloat(("material." + name).c_str(), i);
glBindTexture(GL_TEXTURE_2D, textures[i].id);
}
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
}