-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mesh.cpp
61 lines (53 loc) · 1.95 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
55
56
57
58
59
60
61
#include "Mesh.h"
Mesh::Mesh(vector<Vertex> vertices, vector<unsigned int> indices, Material material, glm::vec3 center)
{
this->vertices = vertices;
this->indices = indices;
this->material = material;
this->center = center;
setupMesh();
}
void Mesh::setupMesh()
{
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);
// VAO
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vertex), &vertices[0], GL_STATIC_DRAW);
// VBO
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), &indices[0], GL_STATIC_DRAW);
// 顶点位置
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)0);
glEnableVertexAttribArray(0);
// 顶点法线
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, normal));
glEnableVertexAttribArray(1);
// 顶点纹理坐标
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, texCoord));
glEnableVertexAttribArray(2);
}
void Mesh::draw(Shader shader, bool center, glm::vec3 scale)
{
unsigned int diffuseNr = 1;
unsigned int specularNr = 1;
for (unsigned int i = 0; i < material.textures.size(); i++)
{
glActiveTexture(GL_TEXTURE0 + i);
string name = material.textures[i].type;
shader.setInt(("material." + name).c_str(), i);
glBindTexture(GL_TEXTURE_2D, material.textures[i].id);
}
shader.setInt("material.shininess", material.shininess);
glm::mat4 model = glm::mat4(1);
model = glm::scale(model, scale);
if (center)
{
model = glm::translate(model, -this->center);
}
shader.setMat4("model", model);
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0);
}