forked from Polytonic/Glitter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmesh.cpp
More file actions
169 lines (148 loc) · 7.3 KB
/
mesh.cpp
File metadata and controls
169 lines (148 loc) · 7.3 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
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
// Preprocessor Directives
#define STB_IMAGE_IMPLEMENTATION
// Local Headers
#include "mesh.hpp"
// System Headers
#include <stb_image.h>
// Define Namespace
namespace Mirage
{
Mesh::Mesh(std::string const & filename) : Mesh()
{
// Load a Model from File
Assimp::Importer loader;
aiScene const * scene = loader.ReadFile(
PROJECT_SOURCE_DIR "/Mirage/Models/" + filename,
aiProcessPreset_TargetRealtime_MaxQuality |
aiProcess_OptimizeGraph |
aiProcess_FlipUVs);
// Walk the Tree of Scene Nodes
auto index = filename.find_last_of("/");
if (!scene) fprintf(stderr, "%s\n", loader.GetErrorString());
else parse(filename.substr(0, index), scene->mRootNode, scene);
}
Mesh::Mesh(std::vector<Vertex> const & vertices,
std::vector<GLuint> const & indices,
std::map<GLuint, std::string> const & textures)
: mIndices(indices)
, mVertices(vertices)
, mTextures(textures)
{
// Bind a Vertex Array Object
glGenVertexArrays(1, & mVertexArray);
glBindVertexArray(mVertexArray);
// Copy Vertex Buffer Data
glGenBuffers(1, & mVertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, mVertexBuffer);
glBufferData(GL_ARRAY_BUFFER,
mVertices.size() * sizeof(Vertex),
& mVertices.front(), GL_STATIC_DRAW);
// Copy Index Buffer Data
glGenBuffers(1, & mElementBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mElementBuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER,
mIndices.size() * sizeof(GLuint),
& mIndices.front(), GL_STATIC_DRAW);
// Set Shader Attributes
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid *) offsetof(Vertex, position));
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid *) offsetof(Vertex, normal));
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid *) offsetof(Vertex, uv));
glEnableVertexAttribArray(0); // Vertex Positions
glEnableVertexAttribArray(1); // Vertex Normals
glEnableVertexAttribArray(2); // Vertex UVs
// Cleanup Buffers
glBindVertexArray(0);
glDeleteBuffers(1, & mVertexBuffer);
glDeleteBuffers(1, & mElementBuffer);
}
void Mesh::draw(GLuint shader)
{
unsigned int unit = 0, diffuse = 0, specular = 0;
for (auto &i : mSubMeshes) i->draw(shader);
for (auto &i : mTextures)
{ // Set Correct Uniform Names Using Texture Type (Omit ID for 0th Texture)
std::string uniform = i.second;
if (i.second == "diffuse") uniform += (diffuse++ > 0) ? std::to_string(diffuse) : "";
else if (i.second == "specular") uniform += (specular++ > 0) ? std::to_string(specular) : "";
// Bind Correct Textures and Vertex Array Before Drawing
glActiveTexture(GL_TEXTURE0 + unit);
glBindTexture(GL_TEXTURE_2D, i.first);
glUniform1f(glGetUniformLocation(shader, uniform.c_str()), ++unit);
} glBindVertexArray(mVertexArray);
glDrawElements(GL_TRIANGLES, mIndices.size(), GL_UNSIGNED_INT, 0);
}
void Mesh::parse(std::string const & path, aiNode const * node, aiScene const * scene)
{
for (unsigned int i = 0; i < node->mNumMeshes; i++)
parse(path, scene->mMeshes[node->mMeshes[i]], scene);
for (unsigned int i = 0; i < node->mNumChildren; i++)
parse(path, node->mChildren[i], scene);
}
void Mesh::parse(std::string const & path, aiMesh const * mesh, aiScene const * scene)
{
// Create Vertex Data from Mesh Node
std::vector<Vertex> vertices; Vertex vertex;
for (unsigned int i = 0; i < mesh->mNumVertices; i++)
{ if (mesh->mTextureCoords[0])
vertex.uv = glm::vec2(mesh->mTextureCoords[0][i].x, mesh->mTextureCoords[0][i].y);
vertex.position = glm::vec3(mesh->mVertices[i].x, mesh->mVertices[i].y, mesh->mVertices[i].z);
vertex.normal = glm::vec3(mesh->mNormals[i].x, mesh->mNormals[i].y, mesh->mNormals[i].z);
vertices.push_back(vertex);
}
// Create Mesh Indices for Indexed Drawing
std::vector<GLuint> indices;
for (unsigned int i = 0; i < mesh->mNumFaces; i++)
for (unsigned int j = 0; j < mesh->mFaces[i].mNumIndices; j++)
indices.push_back(mesh->mFaces[i].mIndices[j]);
// Load Mesh Textures into VRAM
std::map<GLuint, std::string> textures;
auto diffuse = process(path, scene->mMaterials[mesh->mMaterialIndex], aiTextureType_DIFFUSE);
auto specular = process(path, scene->mMaterials[mesh->mMaterialIndex], aiTextureType_SPECULAR);
textures.insert(diffuse.begin(), diffuse.end());
textures.insert(specular.begin(), specular.end());
// Create New Mesh Node
mSubMeshes.push_back(std::unique_ptr<Mesh>(new Mesh(vertices, indices, textures)));
}
std::map<GLuint, std::string> Mesh::process(std::string const & path,
aiMaterial * material,
aiTextureType type)
{
std::map<GLuint, std::string> textures;
for(unsigned int i = 0; i < material->GetTextureCount(type); i++)
{
// Define Some Local Variables
GLenum format;
GLuint texture;
std::string mode;
// Load the Texture Image from File
aiString str; material->GetTexture(type, i, & str);
std::string filename = str.C_Str(); int width, height, channels;
filename = PROJECT_SOURCE_DIR "/Mirage/Models/" + path + "/" + filename;
unsigned char * image = stbi_load(filename.c_str(), & width, & height, & channels, 0);
if (!image) fprintf(stderr, "%s %s\n", "Failed to Load Texture", filename.c_str());
// Set the Correct Channel Format
switch (channels)
{
case 1 : format = GL_ALPHA; break;
case 2 : format = GL_LUMINANCE; break;
case 3 : format = GL_RGB; break;
case 4 : format = GL_RGBA; break;
}
// Bind Texture and Set Filtering Levels
glGenTextures(1, & texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, format,
width, height, 0, format, GL_UNSIGNED_BYTE, image);
glGenerateMipmap(GL_TEXTURE_2D);
// Release Image Pointer and Store the Texture
stbi_image_free(image);
if (type == aiTextureType_DIFFUSE) mode = "diffuse";
else if (type == aiTextureType_SPECULAR) mode = "specular";
textures.insert(std::make_pair(texture, mode));
} return textures;
}
};