Repository navigation
Expand file tree
/
Copy pathMesh.cpp
More file actions
70 lines (58 loc) · 1.8 KB
/
Copy pathMesh.cpp
File metadata and controls
70 lines (58 loc) · 1.8 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
#include "Mesh.h"
#include "Shader.h"
// Initializer
Mesh::Mesh(std::vector<Vertex> vertices, std::vector<unsigned int> indices, std::vector<Texture> textures)
{
this->vertices = vertices;
this->indices = indices;
this->textures = textures;
// setup
SetupMesh();
}
// Functions
void Mesh::Render(Shader& shader)
{
shader.use();
for (unsigned int i = 0; i < textures.size(); i++)
{
glActiveTexture(GL_TEXTURE0 + i);
std::string name = this->textures[i].type;
// material.diffuse = ...
if (name == "texture_diffuse")
{
shader.setInt("material.diffuse", i);
}
else if (name == "texture_specular")
{
shader.setInt("material.specular", i);
}
glBindTexture(GL_TEXTURE_2D, textures[i].id);
}
// Reset
glActiveTexture(GL_TEXTURE0);
// Render
glBindVertexArray(this->VAO);
glDrawElements(GL_TRIANGLES, (GLsizei)(this->indices.size()), GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
}
void Mesh::SetupMesh()
{
glGenVertexArrays(1, &this->VAO);
glGenBuffers(1, &this->VBO);
glGenBuffers(1, &this->EBO);
glBindVertexArray(this->VAO);
glBindBuffer(GL_ARRAY_BUFFER, this->VBO);
glBufferData(GL_ARRAY_BUFFER, this->vertices.size() * sizeof(Vertex), this->vertices.data(), GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), this->indices.data(), GL_STATIC_DRAW);
// Position
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)0);
glEnableVertexAttribArray(0);
// Normal
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)(offsetof(Vertex, Normal)));
glEnableVertexAttribArray(1);
// Texture Coords
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)(offsetof(Vertex, TexCoords)));
glEnableVertexAttribArray(2);
glBindVertexArray(0);
}