-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMesh.cpp
More file actions
106 lines (92 loc) · 4.3 KB
/
Copy pathMesh.cpp
File metadata and controls
106 lines (92 loc) · 4.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
#include "Mesh.h"
#include "ResourceManager.h"
Mesh::Mesh(QVector<Vertex> vertices,QVector<unsigned int> indices,QVector<Texture> textures)
:isInitialized(false)
{
this->vertices = vertices;
this->indices = indices;
this->textures = textures;
core = QOpenGLContext::currentContext()->versionFunctions<QOpenGLFunctions_3_3_Core>();
}
Mesh::~Mesh()
{
core->glDeleteBuffers(1,&VAO);
core->glDeleteBuffers(1,&VBO);
core->glDeleteBuffers(1,&EBO);
vertices.clear();
indices.clear();
textures.clear();
}
void Mesh::draw(QOpenGLShaderProgram* shaderProgram)
{
if(!isInitialized){
setupMesh(shaderProgram);
isInitialized = true;
}
// bind appropriate textures
unsigned int diffuseNr = 1;
unsigned int specularNr = 1;
unsigned int normalNr = 1;
unsigned int heightNr = 1;
for(int i = 0; i < textures.size(); i++)
{
//core->glActiveTexture(GL_TEXTURE0 + static_cast<unsigned int>(i)); // active proper texture unit before binding
// retrieve texture number (the N in diffuse_textureN)
QString number;
QString name = textures[i].type;
if(name == "texture_diffuse")
number = QString::number(diffuseNr++);
else if(name == "texture_specular")
number = QString::number(specularNr++); // transfer unsigned int to stream
else if(name == "texture_normal")
number = QString::number(normalNr++); // transfer unsigned int to stream
else if(name == "texture_height")
number = QString::number(heightNr++); // transfer unsigned int to stream
// now set the sampler to the correct texture unit
//core->glUniform1i(glGetUniformLocation(shader.ID, (name + number).c_str()), i);
QString uniformName = QString(name + number);
shaderProgram->setUniformValue(QString(name + number).toLocal8Bit().constData(), i);
//ResourceManager::getShader("modelLoad").use().setInteger(uniformName,i);
// and finally bind the texture
textures[i].texture->bind(static_cast<unsigned int>(i));
}
// draw mesh
core->glBindVertexArray(VAO);
core->glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, reinterpret_cast<void*>(0));
core->glBindVertexArray(0);
// always good practice to set everything back to defaults once configured.
//core->glActiveTexture(GL_TEXTURE0);
}
void Mesh::setupMesh(QOpenGLShaderProgram* shaderProgram)
{
// create buffers/arrays
core->glGenVertexArrays(1, &VAO);
core->glGenBuffers(1, &VBO);
core->glGenBuffers(1, &EBO);
core->glBindVertexArray(VAO);
// load data into vertex buffers
core->glBindBuffer(GL_ARRAY_BUFFER, VBO);
// A great thing about structs is that their memory layout is sequential for all its items.
// The effect is that we can simply pass a pointer to the struct and it translates perfectly to a glm::vec3/2 array which
// again translates to 3/2 floats which translates to a byte array.
core->glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vertex), &vertices[0], GL_STATIC_DRAW);
core->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
core->glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * static_cast<int>(sizeof(unsigned int)) , &indices[0], GL_STATIC_DRAW);
// set the vertex attribute pointers
// vertex Positions
core->glEnableVertexAttribArray(0);
core->glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<void*>(0));
// vertex normals
core->glEnableVertexAttribArray(1);
core->glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<void*>(offsetof(Vertex, Normal)));
// vertex texture coords
core->glEnableVertexAttribArray(2);
core->glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<void*>(offsetof(Vertex, TexCoords)));
// vertex tangent
//core->glEnableVertexAttribArray(3);
//core->glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<void*>(offsetof(Vertex, Tangent)));
// vertex bitangent
//core->glEnableVertexAttribArray(4);
//core->glVertexAttribPointer(4, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<void*>(offsetof(Vertex, Bitangent)));
core->glBindVertexArray(0);
}