-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMesh.cpp
More file actions
78 lines (63 loc) · 2.53 KB
/
Copy pathMesh.cpp
File metadata and controls
78 lines (63 loc) · 2.53 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
#include "Mesh.h"
Mesh::Mesh()
{
VAO = 0;
VBO = 0;
IBO = 0;
indexCount = 0;
}
void Mesh::CreateMesh(GLfloat* vertices, unsigned int* indices, unsigned int numOfVertices, unsigned int numOfIndices)
{
indexCount = numOfIndices;
glGenVertexArrays(1, &VAO); //creates a vertex array on the graphics card. 1 refers to the amount of arrays that we want to create
glBindVertexArray(VAO);
glGenBuffers(1, &IBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices[0]) * numOfIndices, indices, GL_STATIC_DRAW);
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
//Connect the buffer data to the vertices
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices[0]) * numOfVertices, vertices, GL_STATIC_DRAW); //STATIC_DRAW usually means that the values are static, they cannot be altered
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(vertices[0])*8, 0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(vertices[0])*8, (void*)(sizeof(vertices[0])*3)); //Here the attribute pointer is ar location 1 and index 2
glEnableVertexAttribArray(1);
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, sizeof(vertices[0])*8, (void*)(sizeof(vertices[0]) * 5)); //Here the attribute pointer is ar location 2 and index 3. We are multiplying by 5 since 5 is the offset of the normal data. Offset - how far is the value from the other data
glEnableVertexAttribArray(2);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
//Note: You should unbind the IBO/EBO AFTER YOU UNBIND THE VAO!
glBindVertexArray(0); //Unbind the vertex array
}
void Mesh::RenderMesh()
{
glBindVertexArray(VAO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
//glDrawArrays(GL_TRIANGLES, 0, 3); //0 is the first point of the triangle and 3 refers to the amount of vertices we want to draw
glDrawElements(GL_TRIANGLES, indexCount, GL_UNSIGNED_INT, 0); //12 because it is in 3D, 3 sides*3 sides*3 sides*3 sides
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
void Mesh::ClearMesh()
{
if (IBO != 0)
{
glDeleteBuffers(1, &IBO); //Deletes the buffer from your graphics card memory, for garbage collection
IBO = 0;
}
if (VBO != 0)
{
glDeleteBuffers(1, &VBO); //Deletes the buffer from your graphics card memory, for garbage collection
VBO = 0;
}
if (VAO != 0)
{
glDeleteVertexArrays(1, &VAO); //Deletes the Vertex array from your graphics card memory, for garbage collection
VAO = 0;
}
indexCount = 0;
}
Mesh::~Mesh()
{
ClearMesh();
}