-
Notifications
You must be signed in to change notification settings - Fork 0
/
Shaders.cpp
168 lines (134 loc) · 5.09 KB
/
Shaders.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include "Shaders.h"
void BasicGeoShader::Init()
{
this->_programID = CreateShader(
R"(
#version 330 core
layout(location = 0) in vec3 vertPosition;
layout(location = 1) in vec4 vertColor;
out vec3 fragPosition;
out vec4 fragColor;
uniform mat4 view;
void main() {
gl_Position = view * vec4(vertPosition, 1.0f);
fragPosition = vertPosition;
fragColor = vertColor;
}
)",
R"(
#version 330 core
in vec3 fragPosition;
in vec4 fragColor;
out vec4 color;
void main() {
color = fragColor;
}
)"
);
// Generating VAO, VBO, and IBO
if (this->_vaoID == 0)
glGenVertexArrays(1, &this->_vaoID);
if (this->_vboID == 0)
glGenBuffers(1, &this->_vboID);
if (this->_iboID == 0)
glGenBuffers(1, &this->_iboID);
// Binding the VAO and VBO, and IBO
glBindVertexArray(this->_vaoID);
glBindBuffer(GL_ARRAY_BUFFER, this->_vboID);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->_iboID);
// Pointing the VAO to the correct location in the given Vertex3D object
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex3D), (void*)offsetof(Vertex3D, position));
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(Vertex3D), (void*)offsetof(Vertex3D, color));
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex3D), (void*)offsetof(Vertex3D, normal));
glEnableVertexAttribArray(3);
glVertexAttribPointer(3, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex3D), (void*)offsetof(Vertex3D, uv));
// Unbinding the VAO
glBindVertexArray(this->_vaoID);
}
void BasicGeoShader::Destroy()
{
if (this->_vaoID)
glDeleteVertexArrays(1, &this->_vaoID);
if (this->_vboID)
glDeleteBuffers(1, &this->_vboID);
if (this->_iboID)
glDeleteBuffers(1, &this->_iboID);
glDeleteProgram(this->_programID);
}
void BasicGeoShader::End()
{
// Binding the VBO
glBindBuffer(GL_ARRAY_BUFFER, this->_vboID);
// Uploading vertex data to the VBO
glBufferData(GL_ARRAY_BUFFER, this->_vertices.size() * sizeof(Vertex3D), nullptr, GL_DYNAMIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, this->_vertices.size() * sizeof(Vertex3D), this->_vertices.data());
// Unbinding the VBO
glBindBuffer(GL_ARRAY_BUFFER, 0);
// Binding the IBO
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->_iboID);
// Uploading index data to the IBO
glBufferData(GL_ELEMENT_ARRAY_BUFFER, this->_indices.size() * sizeof(unsigned int), nullptr, GL_DYNAMIC_DRAW);
glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, this->_indices.size() * sizeof(unsigned int), this->_indices.data());
// Unbinding the IBO
glBindBuffer(GL_ARRAY_BUFFER, 0);
// Clearing vertex and index vectors
this->_numIndices = this->_indices.size();
this->_vertices.clear();
this->_indices.clear();
}
void BasicGeoShader::Render()
{
// Using GLSL program and binding the VAO
this->Use();
glBindVertexArray(this->_vaoID);
// Drawing vertices
glDrawElements(GL_TRIANGLES, this->_numIndices, GL_UNSIGNED_INT, 0);
// Unbinding the GLSL program and unbinding the VAO
glBindVertexArray(0);
this->Unuse();
}
void BasicGeoShader::DrawTri(Vertex3D a, Vertex3D b, Vertex3D c)
{
int firstIndex = this->_vertices.size();
// Pushing vertices into vector
this->_vertices.reserve(this->_vertices.size() + 3);
this->_vertices.push_back(a);
this->_vertices.push_back(b);
this->_vertices.push_back(c);
// Pushing corresponding indices into vector
this->_indices.reserve(this->_indices.size() + 3);
this->_indices.push_back(firstIndex);
this->_indices.push_back(firstIndex + 1);
this->_indices.push_back(firstIndex + 2);
}
void BasicGeoShader::DrawQuad(Vertex3D a, Vertex3D b, Vertex3D c, Vertex3D d)
{
int firstIndex = this->_vertices.size();
// Pushing vertices into vector
this->_vertices.reserve(this->_vertices.size() + 4);
this->_vertices.push_back(a);
this->_vertices.push_back(b);
this->_vertices.push_back(c);
this->_vertices.push_back(d);
// Pushing corresponding indices into vector
this->_indices.reserve(this->_indices.size() + 4);
this->_indices.push_back(firstIndex);
this->_indices.push_back(firstIndex + 1);
this->_indices.push_back(firstIndex + 2);
this->_indices.push_back(firstIndex);
this->_indices.push_back(firstIndex + 2);
this->_indices.push_back(firstIndex + 3);
}
void BasicGeoShader::DrawMesh(const Mesh& mesh)
{
int firstIndex = this->_vertices.size();
// Pushing vertices into vector
for (size_t i = 0; i < mesh.vertices.size(); i++)
this->_vertices.push_back(mesh.vertices[i]);
// Pushing corresponding indices into vector
for (size_t i = 0; i < mesh.indices.size(); i++)
this->_indices.push_back(firstIndex + mesh.indices[i]);
}