Repository navigation
Expand file tree
/
Copy pathMesh.cpp
More file actions
72 lines (54 loc) · 1.97 KB
/
Copy pathMesh.cpp
File metadata and controls
72 lines (54 loc) · 1.97 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
#include "Mesh.h"
Mesh::Mesh(float vertices[])
{
this->vertices.resize(36);
memcpy(&this->vertices[0], vertices, 36 * 8 * sizeof(float));
setupMesh();
printf("w");
}
Mesh::Mesh(std::vector<Vertex> vertices, std::vector<unsigned int> indices, std::vector<Texture> textures)
{
this->vertices = vertices;//this指类里的,区分同名的
this->indices = indices;
this->textures = textures;
setupMesh();
}
void Mesh::Draw(Shader* shader)
{
//遍历材质
for (unsigned int i = 0; i < textures.size(); i++) {
if (textures[i].type == "texture_diffuse") {
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, textures[i].id);
glUniform1i(glGetUniformLocation(shader->ID, "material.diffuse"), 0);
}
else if (textures[i].type == "texture_specular") {
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, textures[i].id);
glUniform1i(glGetUniformLocation(shader->ID, "material.specular"), 1);
}
}
glBindVertexArray(VAO);
//glDrawArrays(GL_TRIANGLES, 0, 36);
glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
glActiveTexture(GL_TEXTURE0);
}
void Mesh::setupMesh()
{
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex) * vertices.size(), &vertices[0], GL_STATIC_DRAW);
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
glGenBuffers(1, &EBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned) * indices.size(), &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*)(3 * sizeof(GL_FLOAT)));
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)(6 * sizeof(GL_FLOAT)));
glBindVertexArray(0);//解绑
}