-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMesh.cpp
More file actions
95 lines (80 loc) · 3.21 KB
/
Mesh.cpp
File metadata and controls
95 lines (80 loc) · 3.21 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
#include "Mesh.h"
Mesh::Mesh(GLenum primitive_type, std::vector<Vertex>& vertices, std::vector<GLuint>& indices, GLuint texture_id) :
primitive_type(primitive_type),
vertices(vertices),
indices(indices),
texture_id(texture_id)
{
vertices = vertices;
indices = indices;
// Get the Vertex Array Object
glGenVertexArrays(1, &VAO);
// Get the Vertex Buffer Object
glGenBuffers(1, &VBO);
// Get the Element Buffer Object
glGenBuffers(1, &EBO);
// Set Vertex Array Buffer as the current Vertex Array Object
glBindVertexArray(VAO);
// Set the Vertex Buffer Object as the current buffer
glBindBuffer(GL_ARRAY_BUFFER, VBO);
// Send the vertices data to vertex buffer
// Allocate buffer as number of vertices * size of one vertex
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vertex), vertices.data(), GL_STATIC_DRAW);
// Set Element Buffer as current buffer
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
// Send data into EBO
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(GLuint), indices.data(), GL_STATIC_DRAW);
//GLint attrib_location;
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<void*>(0 + offsetof(Vertex, Position)));
// Enable the Vertex Attribute for position
glEnableVertexAttribArray(0);
// Set end enable Vertex Attribute for Normal
//attrib_location = glGetAttribLocation(shader_prog_ID, "aNormal"); //name in shader src
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<void*>(0 + offsetof(Vertex, Normal)));
glEnableVertexAttribArray(1);
// Set end enable Vertex Attribute for Texture Coordinates
//attrib_location = glGetAttribLocation(shader_prog_ID, "aTex"); //name in shader src
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<void*>(0 + offsetof(Vertex, UVs)));
glEnableVertexAttribArray(2);
// Bind VBO and VAO to 0 to prevent unintended modification of VAO/VBO
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
void Mesh::draw(Shader& shader)
{
shader.activate();
//TODO: draw mesh: bind vertex array object, draw all elements with selected primitive type, unbind vertex array object
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
}
void Mesh::draw(Shader& shader, glm::mat4 mx_model)
{
if (texture_id > 0) {
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture_id);
//shader.setUniform("uTexture", 0);
shader.setUniform("u_material.textura", 0); // We're only using texturing unit no. 0
}
shader.setUniform("uMx_model", mx_model);
glBindVertexArray(VAO);
glDrawElements(primitive_type, static_cast<GLsizei>(indices.size()), GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
}
void Mesh::clear(void)
{
vertices.clear();
indices.clear();
//texture_id = 0;
primitive_type = GL_POINTS;
// delete all allocations
//glDeleteBuffers... //VBO a EBO
glDeleteBuffers(1, &VBO);
glDeleteBuffers(1, &EBO);
//glDeleteVertexArrays... // VAO
//glDeleteVertexArrays(1, &VAO);
if (VAO) { glDeleteVertexArrays(1, &VAO); VAO = 0; }
if (texture_id) { glDeleteTextures(1, &texture_id); texture_id = 0; }
// Destruktor ne-e
};