-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmesh.cpp
114 lines (97 loc) · 2.99 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
#include "mesh.h"
#include <glm/gtc/matrix_transform.hpp>
#include <assimp/Importer.hpp>
#include <assimp/scene.h>
#include <assimp/postprocess.h>
#include <vector>
Mesh::Mesh()
: scale(1.0f, 1.0f, 1.0f), vao(0)
{
}
Mesh::~Mesh()
{
if(vao)
{
glDeleteVertexArrays(1, &vao);
}
}
bool Mesh::load(const aiMesh* mesh)
{
if(vao)
{
// Already loaded
return false;
}
// We only load the first mesh from the Assimp scene here
std::vector<float> vertices;
std::vector<GLuint> indices;
for(int i = 0; i < mesh->mNumVertices; ++i)
{
const aiVector3D* pos = &(mesh->mVertices[i]);
const aiVector3D* texCoord = &(mesh->mTextureCoords[0][i]);
const aiVector3D* normal = &(mesh->mNormals[i]);
vertices.push_back(pos->x);
vertices.push_back(pos->y);
vertices.push_back(pos->z);
vertices.push_back(normal->x);
vertices.push_back(normal->y);
vertices.push_back(normal->z);
vertices.push_back(texCoord->x);
vertices.push_back(1.0-texCoord->y);
}
for(int i = 0; i < mesh->mNumFaces; ++i)
{
const aiFace* face = &(mesh->mFaces[i]);
indices.push_back(face->mIndices[0]);
indices.push_back(face->mIndices[1]);
indices.push_back(face->mIndices[2]);
}
numIndices = indices.size();
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
GLuint vbo;
glGenBuffers(1, &vbo);
GLuint ebo;
glGenBuffers(1, &ebo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * vertices.size(), &vertices[0], GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLuint) * indices.size(), &indices[0], GL_STATIC_DRAW);
glEnableVertexAttribArray(0); // position
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), 0);
glEnableVertexAttribArray(1); // normal
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (void*)(3 * sizeof(GLfloat)));
glEnableVertexAttribArray(2); // texture coordinates
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (void*)(6 * sizeof(GLfloat)));
glBindVertexArray(0);
glDeleteBuffers(1, &vbo);
glDeleteBuffers(1, &ebo);
return true;
}
void Mesh::setPosition(float x, float y, float z)
{
position = glm::vec3(x, y, z);
}
void Mesh::setScale(float x, float y, float z)
{
scale = glm::vec3(x, y, z);
}
void Mesh::setAngle(float x, float y, float z)
{
angle = glm::vec3(x, y, z);
}
glm::mat4 Mesh::getModelMatrix()
{
glm::mat4 m;
m = glm::scale(m, scale);
m = glm::rotate(m, angle.x, glm::vec3(1.0f, 0.0f, 0.0f));
m = glm::rotate(m, angle.y, glm::vec3(0.0f, 1.0f, 0.0f));
m = glm::rotate(m, angle.z, glm::vec3(0.0f, 0.0f, 1.0f));
m = glm::translate(m, position);
return m;
}
void Mesh::render()
{
glBindVertexArray(vao);
glDrawElements(GL_TRIANGLES, numIndices, GL_UNSIGNED_INT, 0);
}