-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmesh.cpp
123 lines (99 loc) · 3.62 KB
/
mesh.cpp
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
#include "mesh.h"
#include <fstream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <QFile>
Mesh::Mesh(std::string mesh_path):
indexBuf(QOpenGLBuffer::IndexBuffer),
m_mesh_path(mesh_path)
{
initializeOpenGLFunctions();
// Generate 2 VBOs
arrayBuf.create();
indexBuf.create();
normalBuf.create();
// Initializes cube geometry and transfers it to VBOs
initMesh();
color = QVector4D(0,0,1,1);
}
Mesh::~Mesh()
{
arrayBuf.destroy();
indexBuf.destroy();
normalBuf.destroy();
}
void Mesh::initMesh()
{
loadMesh();
arrayBuf.bind();
arrayBuf.allocate(m_vertices.data(), m_vertices.size() * sizeof(QVector3D));
indexBuf.bind();
indexBuf.allocate(m_faces.data(), m_faces.size() * sizeof(unsigned int));
normalBuf.bind();
std::cout << "norm size " << m_normals.size() << std::endl;
normalBuf.allocate(m_normals.data(), m_normals.size() * sizeof(QVector3D));
}
void Mesh::loadMesh()
{
const aiScene* scene = aiImportFile( "/home/mathias/Documents/QOpenGLWidgetTriangle/teapot.obj",
aiProcess_CalcTangentSpace |
aiProcess_GenNormals |
aiProcess_Triangulate |
aiProcess_JoinIdenticalVertices |
aiProcess_SortByPType);
if( !scene)
{
std::cout << aiGetErrorString() << std::endl;
exit(1);
}
//loop over meshes
for (int i = 0; i < scene->mNumMeshes; ++i)
{
//loop into current mesh for vertices
for (int j = 0; j < scene->mMeshes[i]->mNumVertices; ++j)
{
QVector3D p = QVector3D(scene->mMeshes[i]->mVertices[j].x,
scene->mMeshes[i]->mVertices[j].y,
scene->mMeshes[i]->mVertices[j].z);
m_vertices.push_back(p);
}
//loop into current mesh for indices
for (int j = 0; j < scene->mMeshes[i]->mNumFaces; ++j)
{
aiFace f = scene->mMeshes[i]->mFaces[j];
for (int k = 0; k < f.mNumIndices; ++k)
{
m_faces.push_back(f.mIndices[k]);
}
}
//loop into current mesh for normals, one normal per vertex
for (int j = 0; j < scene->mMeshes[i]->mNumVertices; ++j)
{
QVector3D m = QVector3D(scene->mMeshes[i]->mNormals[j].x,
scene->mMeshes[i]->mNormals[j].y,
scene->mMeshes[i]->mNormals[j].z);
m_normals.push_back(m);
}
}
aiReleaseImport( scene);
}
void Mesh::drawMesh(QOpenGLShaderProgram *program)
{
// Tell OpenGL which VBOs to use
arrayBuf.bind();
indexBuf.bind();
program->setUniformValue("objectColor", color);
// Offset for position
quintptr offset = 0;
// Tell OpenGL programmable pipeline how to locate vertex position data
int vertexLocation = program->attributeLocation("a_position");
int normalLocation = program->attributeLocation("a_normal");
program->enableAttributeArray(vertexLocation);
program->setAttributeBuffer(vertexLocation, GL_FLOAT, offset, 3, sizeof(QVector3D));
normalBuf.bind();
program->enableAttributeArray(normalLocation);
program->setAttributeBuffer(normalLocation, GL_FLOAT, 0, 3, sizeof(QVector3D));
// Draw cube geometry using indices from VBO 1
glDrawElements(GL_TRIANGLES, m_faces.size(), GL_UNSIGNED_INT, 0);
}