-
Notifications
You must be signed in to change notification settings - Fork 1
/
model_mesh.cpp
88 lines (72 loc) · 2.42 KB
/
model_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
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
#include "model_mesh.h"
ModelMesh::ModelMesh(std::vector<vertex>& vertices, std::vector<texture>& textures, std::vector<GLuint>& indices) :
vertices(vertices), textures(textures), indices(indices)
{
loadMesh();
}
void ModelMesh::loadMesh()
{
loadVertexAttribs();
}
void ModelMesh::loadVertexAttribs()
{
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, vertices.size()*sizeof(vertex), &vertices[0], GL_STATIC_DRAW);
glGenBuffers(1, &ebo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(GLuint), &indices[0], GL_STATIC_DRAW);
glVertexAttribPointer(POSITION_LOC, 3, GL_FLOAT, GL_FALSE, sizeof(vertex), (GLvoid*)0);
glEnableVertexAttribArray(POSITION_LOC);
glVertexAttribPointer(NORMAL_LOC, 3, GL_FLOAT, GL_FALSE, sizeof(vertex), (GLvoid*)offsetof(vertex, normal)); // offsetof(vertex, normal)
glEnableVertexAttribArray(NORMAL_LOC);
glVertexAttribPointer(TEXTCOORD_LOC, 2, GL_FLOAT, GL_FALSE, sizeof(vertex), (GLvoid*)offsetof(vertex, texCoord));
glEnableVertexAttribArray(TEXTCOORD_LOC);
glBindVertexArray(0);
}
void ModelMesh::render(GLuint programm)
{
std::string textureTypeStr, number;
int diffTexCount = 0, specTexCount = 0;
for (size_t i = 0, texturesSize = textures.size(); i < texturesSize; i++)
{
glActiveTexture(GL_TEXTURE0 + i);
textureTypeStr = textures[i].type;
if (textureTypeStr == "diffuseTexture") {
diffTexCount++;
number = std::to_string(diffTexCount);
}
if (textureTypeStr == "specularTexture") {
specTexCount++;
number = std::to_string(specTexCount);
}
glBindTexture(GL_TEXTURE_2D, textures[i].ID);
glUniform1i(glGetUniformLocation(programm, ("material." + textureTypeStr + number).c_str()), i);
glUniform1f(glGetUniformLocation(programm, "material.shininess"), 16.0);
}
glActiveTexture(GL_TEXTURE0);
glBindVertexArray(vao);
glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
for (size_t i = 0; i < textures.size(); i++)
{
glActiveTexture(GL_TEXTURE0 + i);
glBindTexture(GL_TEXTURE_2D, 0); // unbind
}
}
const std::vector<vertex>& ModelMesh::getVertices() const
{
return vertices;
}
const std::vector<texture>& ModelMesh::getTextures() const
{
return textures;
}
ModelMesh::~ModelMesh()
{
glDeleteBuffers(1, &vao);
glDeleteBuffers(1, &vbo);
glDeleteBuffers(1, &ebo);
}