-
Notifications
You must be signed in to change notification settings - Fork 1
/
Mesh.h
129 lines (112 loc) · 4 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
#pragma once
#include "Includes.h"
#include "Shader.h"
#include <QOpenGLWidget>
///
/// \brief The Vertex struct contains position normal and texture coordinates used to index each of the vertex attributes
///
struct Vertex
{
glm::vec3 Position;
glm::vec3 Normal;
glm::vec2 TextureCoordinates;
};
///
/// \brief The Texture struct contains the id,type and the file path
///
struct Texture
{
GLuint Id;
string Type;
aiString Path;
};
///
/// \brief The Mesh class
/// Provide the mesh with all the necessery variables vertices,indices, and textures and then draw the mesh.
///
class Mesh
{
public:
// Mesh data
vector<Vertex> Vertices;
vector<GLuint> Indices;
vector<Texture> Textures;
// Functions
///
/// \brief Mesh Provide the mesh with all the necessery data
/// \param targetWidget the target width
/// \param vertices the vertices
/// \param indices the indices
/// \param textures the texture
///
Mesh(QOpenGLWidget* targetWidget, vector<Vertex> vertices, vector<GLuint> indices, vector<Texture> textures) :
Vertices{ vertices }, Indices{ indices }, Textures{ textures }, targetWidget{ targetWidget }
{
SetupMesh();
}
GLuint GetVao() const { return vao; }
///
/// \brief Draw the drawing of the loaded mesh
/// \param shader
///
void Draw(Shader shader)
{
GLuint diffuseNumber = 1;
GLuint specularNumber = 1;
for (GLuint i = 0; i < Textures.size(); i++)
{
glActiveTexture(GL_TEXTURE0 + i);
// Retrieve texture number (the N in 'diffuse_textureN')
string number;
string name = Textures[i].Type;
if (name == "texture_diffuse")
number = std::to_string(diffuseNumber++);
else if (name == "texture_specular")
number = std::to_string(specularNumber++);
// glUniform1i(glGetUniformLocation(shader.GetProgram(), ("material." + name + number).c_str()), i);
glUniform1i(glGetUniformLocation(shader.GetProgram(), (name + number).c_str()), i);
glBindTexture(GL_TEXTURE_2D, Textures[i].Id);
}
glUniform1f(glGetUniformLocation(shader.GetProgram(), "material.shininess"), 16.0f);
// Draw mesh
glBindVertexArray(vao);
glDrawElements(GL_TRIANGLES, Indices.size(), GL_UNSIGNED_INT, nullptr);
glBindVertexArray(0);
// Good practice to set everything back to defaults once configured
for (GLuint i = 0; i < Textures.size(); i++)
{
glActiveTexture(GL_TEXTURE0 + i);
glBindTexture(GL_TEXTURE_2D, 0);
}
}
private:
GLuint vao, vbo, ebo;
QOpenGLWidget* targetWidget;
void SetupMesh()
{
targetWidget->makeCurrent();
glGenVertexArrays(1, &vao);
glGenBuffers(1, &vbo);
glGenBuffers(1, &ebo);
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, Vertices.size() * sizeof(Vertex), &Vertices[0], GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, Indices.size() * sizeof(GLuint), &Indices[0], GL_STATIC_DRAW);
// Vertex positions
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<GLvoid*>(0));
// Vertex normals
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<GLvoid*>(offsetof(Vertex, Normal)));
// Vertex texture coordinates
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<GLvoid*>(offsetof(Vertex, TextureCoordinates)));
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glDeleteBuffers(1, &vbo);
glDeleteBuffers(1, &ebo);
targetWidget->doneCurrent();
}
};