-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFunctions.h
302 lines (242 loc) · 9.91 KB
/
Functions.h
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#pragma once
// ********************* INCLUDES *********************
#include <GLFW/glfw3.h>
#include <glad/glad.h>
#include <iostream>
// ********************* GLOBALS *********************
bool wireframe = false;
// ********************* CHAPTER PREREQUISITES *********************
// ********************* HELLO TRIANGLE *********************
const char* vertexShaderSource = "#version 330 core\n"
"layout (location = 0) in vec3 aPos;\n"
"void main()\n"
"{\n"
" gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
"}\0";
const char* fragmentShaderSource = "#version 330 core\n"
"out vec4 FragColor;\n"
"void main()\n"
"{\n"
" FragColor = vec4(1.0f, 1.0f, 1.0f, 1.0f);\n"
"}\0";
const char* fragmentShaderSecondSource = "#version 330 core\n"
"out vec4 FragColor;\n"
"void main()\n"
"{\n"
" FragColor = vec4(0.0f, 1.0f, 1.0f, 1.0f);\n"
"}\0";
// ********************* SHADERS *********************
const char* vertexShaderSecondSource = "#version 330 core\n"
"layout (location = 0) in vec3 aPos;\n"
"void main()\n"
"{\n"
" gl_Position = vec4(aPos, 1.0);\n"
"}\0";
const char* vertexShaderColoredVertexSource = "#version 330 core\n"
"layout (location = 0) in vec3 aPos;\n" // Position variable
"layout (location = 1) in vec3 aColor;\n" // Color variable
"out vec3 ourColor;\n"
"void main()\n"
"{\n"
" gl_Position = vec4(aPos, 1.0);\n"
" ourColor = aColor;\n" // Set ourColor to input color from the vertex data
"}\0";
const char* UniformFragmentShaderSource = "#version 330 core\n"
"out vec4 FragColor;\n"
"uniform vec4 ourColor;\n"
"void main()\n"
"{\n"
" FragColor = ourColor;\n"
"}\0";
const char* fragmentShaderColoredVertexSource = "#version 330 core\n"
"out vec4 FragColor;\n"
"in vec3 ourColor;\n"
"void main()\n"
"{\n"
" FragColor = vec4(ourColor, 1.0);\n"
"}\0";
// ********************* FUNCTION DECLARATIONS *********************
// Callback function that is performed when a frame is created
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
// Function to process keyboard inputs
void processInput(GLFWwindow* window);
// Function to initialise the main window
GLFWwindow* Initialise();
// Function to initialise shaders
void InitialiseShaders(unsigned int* vertexShader, unsigned int* fragmentShader);
// Function to initilaise shader program
void InitialiseShaderProgram(unsigned int* vertexShader, unsigned int* fragmentShader, unsigned int* shaderProgram);
// Function to configure vertex buffer and arrray objects with overload function for indices compatibility
void BufferAndArrayConfiguration(unsigned int* VBO, unsigned int* VAO, float* vertices, unsigned long long size_vertices);
void BufferAndArrayConfiguration(unsigned int* VBO, unsigned int* VAO, unsigned int* EBO, float* vertices, unsigned int* indices, unsigned long long size_vertices, unsigned long long size_indices);
// ********************* FUNCTION IMPLEMENTATION *********************
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
// Assign respective width and height of window
glViewport(0, 0, width, height);
}
void processInput(GLFWwindow* window)
{
// Check for escape key press
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
// Check for wireframe key press
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
{
if (!wireframe)
{
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
wireframe = true;
}
else
{
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
wireframe = false;
}
}
}
// ********************* HELPER FUNCTIONS *********************
GLFWwindow* Initialise()
{
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
//glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
// Attempt to create a window
GLFWwindow* window = glfwCreateWindow(800, 600, "OpenGL Window", NULL, NULL);
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
}
glfwMakeContextCurrent(window);
// Attempt to intialise GLAD
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
}
return window;
}
void InitialiseShaders(unsigned int* vertexShader, unsigned int* fragmentShader)
{
// ********************* VERTEX SHADER *********************
// Create and compile vertex shader
*vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(*vertexShader, 1, &vertexShaderSource, NULL);
glCompileShader(*vertexShader);
// Check for compilation error
int successful_vertex_compilation;
glGetShaderiv(*vertexShader, GL_COMPILE_STATUS, &successful_vertex_compilation);
if (!successful_vertex_compilation)
{
char infoLog[512];
glGetShaderInfoLog(*vertexShader, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl;
}
// ********************* FRAGMENT SHADER *********************
// Create and compile fragment shader
*fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(*fragmentShader, 1, &fragmentShaderSource, NULL);
glCompileShader(*fragmentShader);
// Check for compilation error
int successful_fragment_compilation;
glGetShaderiv(*fragmentShader, GL_COMPILE_STATUS, &successful_fragment_compilation);
if (!successful_fragment_compilation)
{
char infoLog[512];
glGetShaderInfoLog(*fragmentShader, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl;
}
}
void InitialiseShaders(unsigned int* vertexShader, unsigned int* fragmentShader, const char* vertexShaderSource, const char* fragmentShaderSource)
{
// ********************* VERTEX SHADER *********************
// Create and compile vertex shader
*vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(*vertexShader, 1, &vertexShaderSource, NULL);
glCompileShader(*vertexShader);
// Check for compilation error
int successful_vertex_compilation;
glGetShaderiv(*vertexShader, GL_COMPILE_STATUS, &successful_vertex_compilation);
if (!successful_vertex_compilation)
{
char infoLog[512];
glGetShaderInfoLog(*vertexShader, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl;
}
// ********************* FRAGMENT SHADER *********************
// Create and compile fragment shader
*fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(*fragmentShader, 1, &fragmentShaderSource, NULL);
glCompileShader(*fragmentShader);
// Check for compilation error
int successful_fragment_compilation;
glGetShaderiv(*fragmentShader, GL_COMPILE_STATUS, &successful_fragment_compilation);
if (!successful_fragment_compilation)
{
char infoLog[512];
glGetShaderInfoLog(*fragmentShader, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl;
}
}
void InitialiseShaderProgram(unsigned int* vertexShader, unsigned int* fragmentShader, unsigned int* shaderProgram)
{
// ********************* SHADER PROGRAM *********************
*shaderProgram = glCreateProgram();
glAttachShader(*shaderProgram, *vertexShader);
glAttachShader(*shaderProgram, *fragmentShader);
glLinkProgram(*shaderProgram);
// Check for successfull compilation
int full_shader_compilation;
glGetProgramiv(*shaderProgram, GL_LINK_STATUS, &full_shader_compilation);
if (!full_shader_compilation)
{
char infoLog[512];
glGetProgramInfoLog(*shaderProgram, 512, NULL, infoLog);
}
// Perform cleanup of shaders
glDeleteShader(*vertexShader);
glDeleteShader(*fragmentShader);
}
void BufferAndArrayConfiguration(unsigned int* VBO, unsigned int* VAO, float* vertices, unsigned long long size_vertices)
{
// ********************* VERTICES AND BUFFER ARRAY CONFIGURATIONS *********************
// Generate buffers and vertex arrays
glGenBuffers(1, VBO);
glGenVertexArrays(1, VAO);
// Attempt to bind vertex array and buffer
glBindVertexArray(*VAO);
glBindBuffer(GL_ARRAY_BUFFER, *VBO);
// Attempt to copy the vertex data
glBufferData(GL_ARRAY_BUFFER, size_vertices, vertices, GL_STATIC_DRAW);
// Configure the vertex attributes and enable the vertex array
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
// Attempt to unbind buffer and vertex array
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
void BufferAndArrayConfiguration(unsigned int* VBO, unsigned int* VAO, unsigned int* EBO, float* vertices, unsigned int* indices, unsigned long long size_vertices, unsigned long long size_indices)
{
// ********************* VERTICES AND BUFFER ARRAY CONFIGURATIONS *********************
// Generate buffers and vertex arrays
glGenBuffers(1, VBO);
glGenVertexArrays(1, VAO);
glGenBuffers(1, EBO);
// Attempt to bind vertex array
glBindVertexArray(*VAO);
// Attempt to bind the buffer
glBindBuffer(GL_ARRAY_BUFFER, *VBO);
// Attempt to copy the vertex data
glBufferData(GL_ARRAY_BUFFER, size_vertices, vertices, GL_STATIC_DRAW);
// Attempt to bind buffer and data of the Element Buffer Object
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, *EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, size_indices, indices, GL_STATIC_DRAW);
// Configure the vertex attributes
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
// Attempt to bind buffer and vertex array
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}