-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmesh.cpp
81 lines (59 loc) · 2.27 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
#include "mesh.h"
Mesh::Mesh()
{
m_drawCount = 0;
}
/// From meshdata constructor
Mesh::Mesh(const MeshData& m)
{
m_meshData = m;
m_drawCount = m_meshData.indices.size();
initMesh();
cout << "Passed initmesh" << endl;
}
Mesh::Mesh(Vertex* vertices, unsigned numVertices, unsigned* indices, unsigned numIndices)
{
for(unsigned i = 0; i < numVertices; i++)
{
m_meshData.positions.push_back(*vertices[i].getPos());
m_meshData.texCoords.push_back(*vertices[i].getTexCoord());
m_meshData.normals.push_back(*vertices[i].getNormal());
}
for(unsigned i = 0; i < numIndices; i++)
{
m_meshData.indices.push_back(indices[i]);
}
m_drawCount = m_meshData.indices.size();
initMesh();
}
void Mesh::initMesh()
{
glGenVertexArrays(1, &m_vertexArrayObject);
glBindVertexArray(m_vertexArrayObject);
glGenBuffers(NUM_BUFFERS, m_vertexArrayBuffers);
glBindBuffer(GL_ARRAY_BUFFER, m_vertexArrayBuffers[POSITION_VB]);
glBufferData(GL_ARRAY_BUFFER, m_meshData.positions.size() * sizeof(m_meshData.positions[0]), &m_meshData.positions[0], GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, m_vertexArrayBuffers[TEXCOORD_VB]);
glBufferData(GL_ARRAY_BUFFER, m_meshData.texCoords.size() * sizeof(m_meshData.texCoords[0]), &m_meshData.texCoords[0], GL_STATIC_DRAW);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, m_vertexArrayBuffers[NORMAL_VB]);
glBufferData(GL_ARRAY_BUFFER, m_meshData.normals.size() * sizeof(m_meshData.normals[0]), &m_meshData.normals[0], GL_STATIC_DRAW);
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_vertexArrayBuffers[INDEX_VB]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, m_meshData.indices.size() * sizeof(m_meshData.indices[0]), &m_meshData.indices[0], GL_STATIC_DRAW);
glBindVertexArray(0);
}
Mesh::~Mesh()
{
glDeleteVertexArrays(1, &m_vertexArrayObject);
}
void Mesh::draw()
{
glBindVertexArray(m_vertexArrayObject);
glDrawElements(GL_TRIANGLES, m_drawCount, GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
}