-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobject.h
321 lines (246 loc) · 12 KB
/
object.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#ifndef OBJECT_H
#define OBJECT_H
#include <vector>
#include <assimp/Importer.hpp>
#include <assimp/scene.h>
#include <assimp/postprocess.h>
#include <glm/glm.hpp>
#include "texture.h"
#include "shader.h"
#define ARRAY_SIZE_IN_ELEMENTS(a) (sizeof(a)/sizeof(a[0]))
//if texture inversed add aiProcess_FlipUVs
#define ASSIMP_LOAD_FLAGS (aiProcess_Triangulate | aiProcess_GenSmoothNormals| aiProcess_JoinIdenticalVertices | aiProcess_CalcTangentSpace)
#define INVALID_MATERIAL 0xFFFFFFFF
/* Variables in vertex shader should be defined as:
layout(location = 0) in vec3 position;
layout(location = 1) in vec3 normal;
layout(location = 2) in vec2 textureCoord;
layout(location = 3) in vec3 tangent;
*/
#define POSITION_LOC 0
#define NORMAL_LOC 1
#define TEXTURECOORD_LOC 2
#define TANGENT_LOC 3
class Material {
public:
Texture* pDiffuse = NULL;
Texture* pNormal = NULL;
Texture* pSpecularExponent = NULL;
};
//class based on tutorial "Loading Models Using Assimp": https://www.youtube.com/watch?v=sP_kiODC25Q
class Object{
public:
enum BUFFER_TYPE {
INDEX_BUFFER = 0,
POS_VB = 1,
TEXCOORD_VB = 2,
NORMAL_VB = 3,
TANGENT_VB = 4,
NUM_BUFFERS = 5
};
struct BasicMeshEntry {
BasicMeshEntry(){
numIndices = 0;
baseVertex = 0;
baseIndex = 0;
materialIndex = INVALID_MATERIAL;
}
unsigned int numIndices;
unsigned int baseVertex;
unsigned int baseIndex;
unsigned int materialIndex;
};
GLuint VAO;
GLuint buffers[NUM_BUFFERS] = {0};
std::vector<BasicMeshEntry> meshes;
std::vector<glm::vec3> positions;
std::vector<glm::vec2> textCoords;
std::vector<glm::vec3> normals;
std::vector<glm::vec3> tangents;
std::vector<unsigned int > indices;
std::vector<Material> materials;
Object(const char* path) {
std::cout << "Loading object" << path << std::endl;
Assimp::Importer importer;
const aiScene* pScene = importer.ReadFile(path, ASSIMP_LOAD_FLAGS);
if(pScene){
meshes.resize(pScene->mNumMeshes);
materials.resize(pScene->mNumMaterials);
//find total number of vertices/indices over all meshes to reserve space
unsigned int numVertices = 0;
unsigned int numIndices = 0;
countVerticesAndIndices(pScene, numVertices, numIndices);
positions.reserve(numVertices);
materials.reserve(numVertices);
normals.reserve(numVertices);
tangents.reserve(numVertices);
indices.reserve(numIndices);
//populate buffers for every meshes
for(unsigned int meshIdx =0; meshIdx < meshes.size(); meshIdx++){
const aiMesh* paiMesh = pScene->mMeshes[meshIdx];
const aiVector3D zero3D(0.0f, 0.0f, 0.0f);
//populate vertex attribute buffers for this mesh
for(unsigned int i = 0; i < paiMesh->mNumVertices; i++){
const aiVector3D& pPos = paiMesh->mVertices[i];
positions.push_back(glm::vec3(pPos.x,pPos.y,pPos.z));
const aiVector3D& pNormal = paiMesh->mNormals[i];
normals.push_back(glm::vec3(pNormal.x,pNormal.y,pNormal.z));
if(paiMesh->HasTangentsAndBitangents()){
const aiVector3D& pTangent = paiMesh->mTangents[i];
tangents.push_back(glm::vec3(pTangent.x,pTangent.y,pTangent.z));
}
const aiVector3D& pTexture = paiMesh->HasTextureCoords(0) ? paiMesh->mTextureCoords[0][i] : zero3D;
textCoords.push_back(glm::vec2(pTexture.x,pTexture.y));
}
//populate index buffers for this mesh
for(unsigned int i = 0; i < paiMesh->mNumFaces; i++){
const aiFace& face = paiMesh->mFaces[i];
assert(face.mNumIndices == 3);
indices.push_back(face.mIndices[0]);
indices.push_back(face.mIndices[1]);
indices.push_back(face.mIndices[2]);
}
}
//now, init all materials of this scene
initMaterials(pScene, path);
}else{
std::cerr << "Error importing file " << path << ": " << importer.GetErrorString() << std::endl;
}
}
void makeObject(Shader shader) {
//Create the VAO
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
//Create the buffers containing vertices attributes
glGenBuffers(ARRAY_SIZE_IN_ELEMENTS(buffers), buffers);
glBindBuffer(GL_ARRAY_BUFFER, buffers[POS_VB]);
glBufferData(GL_ARRAY_BUFFER, sizeof(positions[0])* positions.size(), &positions[0], GL_STATIC_DRAW);
glEnableVertexAttribArray(POSITION_LOC);
glVertexAttribPointer(POSITION_LOC, 3, GL_FLOAT, GL_FALSE, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, buffers[TEXCOORD_VB]);
glBufferData(GL_ARRAY_BUFFER, sizeof(textCoords[0])* textCoords.size(), &textCoords[0], GL_STATIC_DRAW);
glEnableVertexAttribArray(TEXTURECOORD_LOC);
glVertexAttribPointer(TEXTURECOORD_LOC, 2, GL_FLOAT, GL_FALSE, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, buffers[NORMAL_VB]);
glBufferData(GL_ARRAY_BUFFER, sizeof(normals[0])* normals.size(), &normals[0], GL_STATIC_DRAW);
glEnableVertexAttribArray(NORMAL_LOC);
glVertexAttribPointer(NORMAL_LOC, 3, GL_FLOAT, GL_FALSE, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, buffers[TANGENT_VB]);
glBufferData(GL_ARRAY_BUFFER, sizeof(tangents[0])* tangents.size(), &tangents[0], GL_STATIC_DRAW);
glEnableVertexAttribArray(TANGENT_LOC);
glVertexAttribPointer(TANGENT_LOC, 3, GL_FLOAT, GL_FALSE, 0, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffers[INDEX_BUFFER]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices[0])* indices.size(), &indices[0], GL_STATIC_DRAW);
//unbind VAO
glBindVertexArray(0);
//unbind the buffers
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
this->hasTextureLocation = glGetUniformLocation(shader.ID, "hasTexture");
this->hasSpecularMapLocation = glGetUniformLocation(shader.ID, "hasSpecularMap");
this->hasNormalMapLocation = glGetUniformLocation(shader.ID, "hasNormalMap");
}
void draw() {
glBindVertexArray(this->VAO);
for(unsigned int i=0; i< meshes.size(); i++){
Material& mat = materials[meshes[i].materialIndex];
if(mat.pDiffuse){
mat.pDiffuse->bind(GL_TEXTURE0);
if(hasTextureLocation != -1)
glUniform1i(hasTextureLocation, 1);
}else if(hasTextureLocation != -1)
glUniform1i(hasTextureLocation, 0);
if(mat.pSpecularExponent){
mat.pSpecularExponent->bind(GL_TEXTURE1);
if(hasSpecularMapLocation != -1)
glUniform1i(hasSpecularMapLocation, 1);
}else if(hasSpecularMapLocation != -1)
glUniform1i(hasSpecularMapLocation, 0);
if(mat.pNormal){
mat.pNormal->bind(GL_TEXTURE2);
if(hasNormalMapLocation != -1)
glUniform1i(hasNormalMapLocation, 1);
}else if(hasNormalMapLocation != -1)
glUniform1i(hasNormalMapLocation, 0);
glDrawElementsBaseVertex(GL_TRIANGLES,
meshes[i].numIndices,
GL_UNSIGNED_INT,
(void*)(sizeof(unsigned int) * meshes[i].baseIndex),
meshes[i].baseVertex);
}
// unbind VAO
glBindVertexArray(0);
}
private:
GLuint hasTextureLocation;
GLuint hasSpecularMapLocation;
GLuint hasNormalMapLocation;
void countVerticesAndIndices(const aiScene* pScene, unsigned int& numVertices, unsigned int& numIndices){
for (unsigned int i = 0 ; i < meshes.size() ; i++) {
meshes[i].materialIndex = pScene->mMeshes[i]->mMaterialIndex;
meshes[i].numIndices = pScene->mMeshes[i]->mNumFaces * 3;//flag aiProcess_Triangulate force triangles only
meshes[i].baseVertex = numVertices;
meshes[i].baseIndex = numIndices;
numVertices += pScene->mMeshes[i]->mNumVertices;
numIndices += meshes[i].numIndices;
}
}
void initMaterials(const aiScene* pScene, const char* path){
//loop over every material
for(unsigned int i = 0; i < pScene->mNumMaterials; i ++){
const aiMaterial* pMaterial = pScene->mMaterials[i];
//load diffuse texture
if(pMaterial->GetTextureCount(aiTextureType_DIFFUSE) > 0){
aiString path;
if(pMaterial->GetTexture(aiTextureType_DIFFUSE, 0, &path, NULL, NULL, NULL, NULL, NULL) == AI_SUCCESS){
std::string p(path.data);
std::string::size_type slashIndex = p.find_last_of("/") + 1;
std::string fullPath = PATH_TO_OBJECTS "/textures/" + p.substr(slashIndex);
materials[i].pDiffuse = new Texture(fullPath.c_str());
if (!materials[i].pDiffuse->load(GL_TEXTURE0)) {
std::cout << "Error loading diffuse texture " << fullPath.c_str() << std::endl;
} else {
std::cout << "Loaded diffuse texture " << fullPath.c_str() << " at index " << i << std::endl;
}
}
}else{
std::cout << "No diffuse texture" << std::endl;
}
//load normal texture
if(pMaterial->GetTextureCount(aiTextureType_NORMALS) > 0){
aiString path;
if(pMaterial->GetTexture(aiTextureType_NORMALS, 0, &path, NULL, NULL, NULL, NULL, NULL) == AI_SUCCESS){
std::string p(path.data);
std::string::size_type slashIndex = p.find_last_of("/") + 1;
std::string fullPath = PATH_TO_OBJECTS "/textures/" + p.substr(slashIndex);
materials[i].pNormal = new Texture(fullPath.c_str());
if (!materials[i].pNormal->load(GL_TEXTURE1)) {
std::cout << "Error loading normal texture " << fullPath.c_str() << std::endl;
} else {
std::cout << "Loaded normal texture " << fullPath.c_str() << " at index " << i << std::endl;
}
}
}else{
std::cout << "No normal texture" << std::endl;
}
//load specular map
if(pMaterial->GetTextureCount(aiTextureType_SPECULAR) > 0){
aiString path;
if(pMaterial->GetTexture(aiTextureType_SPECULAR, 0, &path, NULL, NULL, NULL, NULL, NULL) == AI_SUCCESS){
std::string p(path.data);
std::string::size_type slashIndex = p.find_last_of("/") + 1;
std::string fullPath = PATH_TO_OBJECTS "/textures/" + p.substr(slashIndex);
materials[i].pSpecularExponent = new Texture(fullPath.c_str());
if (!materials[i].pSpecularExponent->load(GL_TEXTURE2)) {
std::cout << "Error loading specular texture " << fullPath.c_str() << std::endl;
} else {
std::cout << "Loaded specular texture " << fullPath.c_str() << " at index " << i << std::endl;
}
}
}else{
std::cout << "No specular texture" << std::endl;
}
}
}
};
#endif