This repository has been archived by the owner on Mar 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.cpp
219 lines (192 loc) · 6.5 KB
/
main.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
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
#ifdef __APPLE__
/* Defined before OpenGL and GLUT includes to avoid deprecation messages */
#define GL_SILENCE_DEPRECATION
#define GLFW_INCLUDE_GLCOREARB
#include <GLFW/glfw3.h>
// #else
// #include <GL/gl.h>
// #include <GL/glut.h>
#endif
#include "logs.h"
#include "shader_utils.h"
#include "maths_utils.h"
#include <iostream>
#include <fstream>
const size_t WIDTH = 640;
const size_t HEIGHT = 480;
const char *WINDOW_NAME = "OpenGL Explorer";
auto shader_utils = ShaderUtils::Program{};
/*
* Callback to handle the "close window" event, once the user pressed the Escape key.
*/
static void quitCallback(GLFWwindow *window, int key, int scancode, int action, int _mods)
{
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GLFW_TRUE);
}
/**
* @brief Load the shaders, in order to display the result
*
* @param erase_if_program_registered Allow to erase the shader if it exists
* @return true The shader has been successfully registered
* @return false The shader has not been registered, due to an error
*/
const bool loadShaderProgram(const bool erase_if_program_registered);
/*
* Callback to handle the "reload" event, once the user pressed the 'r' key.
*/
static void reloadShaders(GLFWwindow *window, int key, int scancode, int action, int _mods)
{
if (key == GLFW_KEY_R && action == GLFW_PRESS)
{
debug("reloading...");
loadShaderProgram(true);
}
}
/*
* Initializes the window and viewport via GLFW.
* The viewport takes the all window.
* If an error happens, the function returns `NULL` but **does not** free / terminate the GLFW library.
* Then, do not forget to call `glfwTerminate` if this function returns `NULL`.
*/
GLFWwindow *initializeWindow()
{
// Minimum target is OpenGL 4.1
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow *window = glfwCreateWindow(HEIGHT, WIDTH, WINDOW_NAME, NULL, NULL);
if (!window)
{
error("window creation failed");
return NULL;
}
// Close the window as soon as the Escape key has been pressed
glfwSetKeyCallback(window, quitCallback);
// Easy reload
glfwSetKeyCallback(window, reloadShaders);
// Makes the window context current
glfwMakeContextCurrent(window);
// Enable the viewport
glViewport(0, 0, HEIGHT, WIDTH);
return window;
}
/**
* @brief Returns the all file, as a string, which the file path has been passed
* as parameter
*
* @param path The path of the file
* @return The content of the file, as a string (read all file)
*/
inline auto readFile(const std::string_view path) -> const std::string
{
// Avoid dynamic allocation: read the 4096 first bytes
constexpr auto read_size = std::size_t(4096);
auto stream = std::ifstream(path.data());
stream.exceptions(std::ios_base::badbit);
auto out = std::string();
auto buf = std::string(read_size, '\0');
while (stream.read(&buf[0], read_size))
{
out.append(buf, 0, stream.gcount());
}
out.append(buf, 0, stream.gcount());
return out;
}
const bool loadShaderProgram(const bool erase_if_program_registered = true)
{
const std::string basicVertexShaderSource = readFile("shaders/vertex_shader.glsl");
const std::string basicFragmentShaderSource = readFile("shaders/fragment_shader.glsl");
if (!shader_utils.registerShader(ShaderUtils::Type::VERTEX_SHADER_TYPE, basicVertexShaderSource.c_str()))
{
error("failed to register the vertex shader...");
return false;
}
if (!shader_utils.registerShader(ShaderUtils::Type::FRAGMENT_SHADER_TYPE, basicFragmentShaderSource.c_str()))
{
error("failed to register the fragment shader...");
return false;
}
if (!shader_utils.registerProgram(erase_if_program_registered))
{
error("failed to register the program...");
return false;
}
return true;
}
int main(void)
{
// Initialize the lib
if (!glfwInit())
{
error("could not start GLFW3");
return -1;
}
GLFWwindow *window = initializeWindow();
if (!window)
{
glfwTerminate();
return -1;
}
// Note: Once you have a current OpenGL context, you can use OpenGL normally
// get version info
const GLubyte *renderer = glGetString(GL_RENDERER);
const GLubyte *version = glGetString(GL_VERSION);
info("Renderer: " << renderer);
info("OpenGL version supported: " << version);
if (!loadShaderProgram(false))
{
error("can't load the shaders to initiate the program");
glfwTerminate();
return -1;
}
/* END OF SHADER PART */
/* DRAW THE TRIANGLE */
const MathsUtils::vertex vertices[3] = {
{0.0f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f},
{0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f},
{-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f}};
// Vertex Buffer Object = VBO
GLuint VBO = {};
glGenBuffers(1, &VBO);
// Something failed when generating buffers
if (glGetError() != GL_NO_ERROR)
{
error("error when generating buffers");
glDeleteBuffers(1, &VBO); // TODO: Needed?
glfwTerminate();
return -1;
}
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, MathsUtils::getNbElements(vertices) * sizeof(float), vertices, GL_STATIC_DRAW | GL_MAP_READ_BIT);
// Vertex Arrays Object = VAO
GLuint VAO = {};
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
// Specify position attribute -> 0 as offset
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, MathsUtils::VERTEX_ELEMENTS_NB * sizeof(float), (GLvoid *)0);
glEnableVertexAttribArray(0);
// Specify color attribute -> 3 as offset
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, MathsUtils::VERTEX_ELEMENTS_NB * sizeof(float), (GLvoid *)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, 0);
/* END OF DRAWING */
while (!glfwWindowShouldClose(window))
{
// Render
glClearColor(0.5, 0.5, 0.5, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(shader_utils.getProgram().value());
glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLES, 0, 3);
// Poll for and process events
glfwPollEvents();
// Swap front and back buffers
glfwSwapBuffers(window);
}
// ... here, the user closed the window
glDeleteBuffers(1, &VBO);
glDeleteVertexArrays(1, &VAO);
glfwTerminate();
return 0;
}