-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMesh.cpp
More file actions
81 lines (71 loc) · 2.5 KB
/
Mesh.cpp
File metadata and controls
81 lines (71 loc) · 2.5 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
#include "Mesh.h"
Mesh::Mesh(std::vector <Vertex>& vertices, std::vector <GLuint>& indices, std::vector <Texture>& textures)
{
Mesh::vertices = vertices;
Mesh::indices = indices;
Mesh::textures = textures;
VAO.Bind();
// Generates Vertex Buffer Object and links it to vertices
VBO VBO(vertices);
// Generates Element Buffer Object and links it to indices
EBO EBO(indices);
// Links VBO attributes such as coordinates and colors to VAO
VAO.LinkAttrib(VBO, 0, 3, GL_FLOAT, sizeof(Vertex), (void*)0);
VAO.LinkAttrib(VBO, 1, 3, GL_FLOAT, sizeof(Vertex), (void*)(3 * sizeof(float)));
VAO.LinkAttrib(VBO, 2, 3, GL_FLOAT, sizeof(Vertex), (void*)(6 * sizeof(float)));
VAO.LinkAttrib(VBO, 3, 2, GL_FLOAT, sizeof(Vertex), (void*)(9 * sizeof(float)));
// Unbind all to prevent accidentally modifying them
VAO.Unbind();
VBO.Unbind();
EBO.Unbind();
}
void Mesh::Draw
(
Shader& shader,
Camera& camera,
glm::mat4 matrix,
glm::vec3 translation,
glm::quat rotation,
glm::vec3 scale
)
{
// Bind shader to be able to access uniforms
shader.Activate();
VAO.Bind();
// Keep track of how many of each type of textures we have
unsigned int numDiffuse = 0;
unsigned int numSpecular = 0;
for (unsigned int i = 0; i < textures.size(); i++)
{
std::string num;
std::string type = textures[i].type;
if (type == "diffuse")
{
num = std::to_string(numDiffuse++);
}
else if (type == "specular")
{
num = std::to_string(numSpecular++);
}
textures[i].texUnit(shader, (type + num).c_str(), i);
textures[i].Bind();
}
// Take care of the camera Matrix
glUniform3f(glGetUniformLocation(shader.ID, "camPos"), camera.Position.x, camera.Position.y, camera.Position.z);
camera.Matrix(shader, "camMatrix");
// Initialize matrices
glm::mat4 trans = glm::mat4(1.0f);
glm::mat4 rot = glm::mat4(1.0f);
glm::mat4 sca = glm::mat4(1.0f);
// Transform the matrices to their correct form
trans = glm::translate(trans, translation);
rot = glm::mat4_cast(rotation);
sca = glm::scale(sca, scale);
// Push the matrices to the vertex shader
glUniformMatrix4fv(glGetUniformLocation(shader.ID, "translation"), 1, GL_FALSE, glm::value_ptr(trans));
glUniformMatrix4fv(glGetUniformLocation(shader.ID, "rotation"), 1, GL_FALSE, glm::value_ptr(rot));
glUniformMatrix4fv(glGetUniformLocation(shader.ID, "scale"), 1, GL_FALSE, glm::value_ptr(sca));
glUniformMatrix4fv(glGetUniformLocation(shader.ID, "model"), 1, GL_FALSE, glm::value_ptr(matrix));
// Draw the actual mesh
glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0);
}