-
Notifications
You must be signed in to change notification settings - Fork 451
/
mesh.h
165 lines (159 loc) · 4.34 KB
/
mesh.h
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#ifndef _MESH_H_
#define _MESH_H_
#include <GLEW/glew.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtx/string_cast.hpp>
#include <string>
#include <vector>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <assimp/Importer.hpp>
#include <assimp/scene.h>
#include <assimp/postprocess.h>
#include "shader.h"
// 表示一个顶点属性
struct Vertex
{
glm::vec3 position;
glm::vec2 texCoords;
glm::vec3 normal;
};
// 表示一个Texture
struct Texture
{
GLuint id;
aiTextureType type;
std::string path;
};
// 表示一个用于渲染的最小实体
class Mesh
{
public:
void draw(const Shader& shader) const// 绘制Mesh
{
if (VAOId == 0
||VBOId == 0
|| EBOId == 0)
{
return;
}
glBindVertexArray(this->VAOId);
int texUnitCnt = this->bindTextures(shader);
glDrawElements(GL_TRIANGLES, this->indices.size(), GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
// 一个好的习惯是 在这里移除纹理绑定
this->unBindTextures(texUnitCnt);
}
int bindTextures(const Shader& shader) const
{
int diffuseCnt = 0, specularCnt = 0, texUnitCnt = 0;
for (std::vector<Texture>::const_iterator it = this->textures.begin();
this->textures.end() != it; ++it)
{
switch (it->type)
{
case aiTextureType_DIFFUSE:
{
glActiveTexture(GL_TEXTURE0 + texUnitCnt);
glBindTexture(GL_TEXTURE_2D, it->id);
std::stringstream samplerNameStr;
samplerNameStr << "texture_diffuse" << diffuseCnt++;
glUniform1i(glGetUniformLocation(shader.programId,
samplerNameStr.str().c_str()), texUnitCnt++);
}
break;
case aiTextureType_SPECULAR:
{
glActiveTexture(GL_TEXTURE0 + texUnitCnt);
glBindTexture(GL_TEXTURE_2D, it->id);
std::stringstream samplerNameStr;
samplerNameStr << "texture_specular" << specularCnt++;
glUniform1i(glGetUniformLocation(shader.programId,
samplerNameStr.str().c_str()), texUnitCnt++);
}
break;
default:
std::cerr << "Warning::Mesh::draw, texture type" << it->type
<< " current not supported." << std::endl;
break;
}
}
return texUnitCnt;
}
void unBindTextures(const int texUnitCnt) const
{
for (int i = 0; i < texUnitCnt; ++i)
{
glActiveTexture(GL_TEXTURE0 + i);
glBindTexture(GL_TEXTURE_2D, 0);
}
}
Mesh():VAOId(0), VBOId(0), EBOId(0){}
Mesh(const std::vector<Vertex>& vertData,
const std::vector<Texture> & textures,
const std::vector<GLuint>& indices):VAOId(0), VBOId(0), EBOId(0) // 构造一个Mesh
{
setData(vertData, textures, indices);
}
void setData(const std::vector<Vertex>& vertData,
const std::vector<Texture> & textures,
const std::vector<GLuint>& indices)
{
this->vertData = vertData;
this->indices = indices;
this->textures = textures;
if (!vertData.empty() && !indices.empty())
{
this->setupMesh();
}
}
void final() const
{
glDeleteVertexArrays(1, &this->VAOId);
glDeleteBuffers(1, &this->VBOId);
glDeleteBuffers(1, &this->EBOId);
}
~Mesh()
{
// 不要再这里释放VBO等空间 因为Mesh对象传递时 临时对象销毁后这里会清理VBO等空间
}
GLuint getVAOId() const { return this->VAOId; }
const std::vector<Vertex>& getVertices() const { return this->vertData; }
const std::vector<GLuint>& getIndices() const { return this->indices; }
private:
std::vector<Vertex> vertData;
std::vector<GLuint> indices;
std::vector<Texture> textures;
GLuint VAOId, VBOId, EBOId;
void setupMesh() // 建立VAO,VBO等缓冲区
{
glGenVertexArrays(1, &this->VAOId);
glGenBuffers(1, &this->VBOId);
glGenBuffers(1, &this->EBOId);
glBindVertexArray(this->VAOId);
glBindBuffer(GL_ARRAY_BUFFER, this->VBOId);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex) * this->vertData.size(),
&this->vertData[0], GL_STATIC_DRAW);
// 顶点位置属性
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE,
sizeof(Vertex), (GLvoid*)0);
glEnableVertexAttribArray(0);
// 顶点纹理坐标
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE,
sizeof(Vertex), (GLvoid*)(3 * sizeof(GL_FLOAT)));
glEnableVertexAttribArray(1);
// 顶点法向量属性
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE,
sizeof(Vertex), (GLvoid*)(5 * sizeof(GL_FLOAT)));
glEnableVertexAttribArray(2);
// 索引数据
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->EBOId);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLuint)* this->indices.size(),
&this->indices[0], GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
};
#endif