-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopengl.cpp
334 lines (282 loc) · 9.17 KB
/
opengl.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
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#define GLFW_INCLUDE_NONE
#include <GLFW/glfw3.h>
#include <glad/glad.h>
#include "glad.c"
#define FMT_HEADER_ONLY
#include <fmt/core.h>
#include <fmt/ranges.h>
#include <iostream>
#include <cassert>
#include <cstdint>
#include <vector>
#include <fstream>
#include <unordered_map>
#include <memory>
#include <iterator>
#include <algorithm>
#include <string_view>
#include <numbers>
#define GLM_FORCE_RADIANS
#include <glm/glm.hpp>
#include <glm/vec2.hpp>
#include <glm/vec3.hpp>
#include <glm/vec4.hpp>
#include <glm/mat4x4.hpp>
#include <glm/gtc/matrix_transform.hpp>
#if 0
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"
#define STB_TRUETYPE_IMPLEMENTATION
#include "stb_truetype.h"
#endif
#define exit_message(message) \
do \
{ \
fmt::print("{}\n", (message)); \
assert(false); \
} while (0)
static const std::string vertex_shader_text =
"#version 460 core\n"
"layout (location = 0) in vec2 vPos;\n"
"layout (location = 1) in vec3 vCol;\n"
"uniform mat4 MVP;\n"
"out vec3 color;\n"
"void main()\n"
"{\n"
" gl_Position = MVP * vec4(vPos, 0.0, 1.0);\n"
" color = vCol;\n"
"}\n";
static const std::string fragment_shader_text =
"#version 460 core\n"
"out vec4 FragColor;"
"in vec3 color;\n"
"void main()\n"
"{\n"
" FragColor = vec4(color, 1.0);\n"
"}\n";
namespace fsys
{
template <class T>
static T read_file(const std::string_view &filename)
{
std::ifstream file(std::string(filename), std::ios::ate | std::ios::binary);
if (!file.is_open())
{
assert(!"Failed to open file");
}
auto size = static_cast<size_t>(file.tellg());
T buffer;
buffer.reserve(size);
file.seekg(0);
file.read(buffer.data(), size);
file.close();
return buffer;
}
}
namespace sigl
{
template <typename T>
auto identity()
{
return T(1.0f);
}
}
namespace glsys
{
static const auto _glErrorCodes = std::unordered_map<GLenum, const std::string_view>{
{GL_INVALID_ENUM, "GL_INVALID_ENUM"},
{GL_INVALID_VALUE, "GL_INVALID_VALUE"},
{GL_INVALID_OPERATION, "GL_INVALID_OPERATION: Given when the set of state for a command is not legal for the parameters given to that command"},
{GL_STACK_OVERFLOW, "GL_STACK_OVERFLOW: Given when a stack pushing operation cannot be done because it would overflow the limit of that stack's size"},
{GL_STACK_UNDERFLOW, "GL_STACK_UNDERFLOW: Given when a stack popping operation cannot be done because the stack is already at its lowest point"},
{GL_OUT_OF_MEMORY, "GL_OUT_OF_MEMORY:"},
{GL_INVALID_FRAMEBUFFER_OPERATION, "GL_INVALID_FRAMEBUFFER_OPERATION"},
{GL_CONTEXT_LOST, "GL_CONTEXT_LOST"},
{GL_TABLE_TOO_LARGE, "GL_TABLE_TOO_LARGE"},
};
const std::vector<GLenum> get_errors()
{
std::vector<GLenum> errors;
GLenum err;
while ((err = glGetError()) != GL_NO_ERROR)
{
errors.push_back(err);
}
return errors;
}
bool report_errors()
{
auto errors = glsys::get_errors();
for (const auto &e : errors)
{
fmt::print(stderr, "{}\n", glsys::_glErrorCodes.at(e));
}
return errors.size() > 0;
}
GLuint create_shader(GLenum type, const std::string &code)
{
auto shader = glCreateShader(type);
auto size = static_cast<GLint>(code.size());
const char *c = code.c_str();
glShaderSource(shader, 1, &c, nullptr);
glCompileShader(shader);
int success = 0;
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
if (!success)
{
int infoSize;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoSize);
auto log = std::vector<char>(infoSize);
glGetShaderInfoLog(shader, infoSize, nullptr, log.data());
fmt::print(stderr, "{}\n", log.data());
assert(false);
}
assert(!glsys::report_errors());
return shader;
}
GLuint create_program(GLuint vShader, GLuint fShader)
{
auto program = glCreateProgram();
glAttachShader(program, vShader);
glAttachShader(program, fShader);
glLinkProgram(program);
int success;
glGetProgramiv(program, GL_LINK_STATUS, &success);
if (!success)
{
int infoSize;
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &infoSize);
auto log = std::vector<char>(infoSize);
glGetProgramInfoLog(program, infoSize, nullptr, log.data());
fmt::print(stderr, "{}\n", log.data());
assert(false);
}
assert(!glsys::report_errors());
return program;
}
GLuint create_program_from_files(const std::string_view &vertexShaderPath, const std::string_view &fragmentShaderPath)
{
auto vCode = fsys::read_file<std::string>(vertexShaderPath);
auto fCode = fsys::read_file<std::string>(fragmentShaderPath);
auto vShader = glsys::create_shader(GL_VERTEX_SHADER, vCode);
auto fShader = glsys::create_shader(GL_FRAGMENT_SHADER, fCode);
auto program = glsys::create_program(vShader, fShader);
glDeleteShader(vShader);
glDeleteShader(fShader);
assert(!glsys::report_errors());
return program;
}
GLuint create_program_from_strings(const std::string &vCode, const std::string &fCode)
{
auto vShader = glsys::create_shader(GL_VERTEX_SHADER, vCode);
auto fShader = glsys::create_shader(GL_FRAGMENT_SHADER, fCode);
auto program = glsys::create_program(vShader, fShader);
glDeleteShader(vShader);
glDeleteShader(fShader);
assert(!glsys::report_errors());
return program;
}
GLint get_uniform_location(GLuint program, const char *str)
{
GLint result = glGetUniformLocation(program, str);
if (result < 0)
{
fmt::print(stderr, "Failed to find uniform: {}", str);
assert(false);
}
return result;
}
}
void error_callback(int error, const char *description)
{
fmt::print(stderr, "GLFW Error[{}]: {}\n", error, description);
}
static void key_callback(GLFWwindow *window, int key, int scancode, int action, int mods)
{
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
{
glfwSetWindowShouldClose(window, GLFW_TRUE);
}
}
template <typename T>
size_t sizeof_vector(const std::vector<T> &vec)
{
return sizeof(T) * vec.size();
}
const GLfloat *matrix_raw(const auto &m)
{
return &m[0][0];
}
struct vertex_data
{
glm::vec2 v;
glm::vec3 c;
};
//Test triangle
static const std::vector<vertex_data> vertices = {
{.v = {-0.6f, -0.4f}, .c = {1.f, 0.f, 0.f}},
{.v = {0.6f, -0.4f}, .c = {0.f, 1.f, 0.f}},
{.v = {0.0f, 0.6f}, .c = {0.f, 0.f, 1.f}},
};
struct render_object
{
GLuint vbo;
GLuint vao;
GLuint program;
GLint uniMvp;
GLint uniPos;
GLint uniCol;
glm::mat4x4 model;
};
int main()
{
if (!glfwInit())
{
exit_message("failed to initialized glfw");
}
glfwSetErrorCallback(error_callback);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow *window = glfwCreateWindow(1920, 1080, "Endless Money", nullptr, nullptr);
if (!window)
{
exit_message("failed to crerate window");
}
glfwMakeContextCurrent(window);
gladLoadGLLoader(reinterpret_cast<GLADloadproc>(glfwGetProcAddress));
glfwSwapInterval(1);
glfwSetKeyCallback(window, key_callback);
int32_t width, height;
glfwGetFramebufferSize(window, &width, &height);
glViewport(0, 0, width, height);
GLuint program = glsys::create_program_from_strings(vertex_shader_text, fragment_shader_text);
GLuint vao, vbo;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof_vector(vertices), vertices.data(), GL_STATIC_DRAW);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(vertices[0]), static_cast<void *>(0));
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(vertices[0]), reinterpret_cast<void *>(sizeof(float) * 2));
glEnableVertexAttribArray(1);
assert(!glsys::report_errors());
while (!glfwWindowShouldClose(window))
{
float ratio = width / static_cast<float>(height);
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(program);
glm::mat4x4 perspective = glm::ortho(-ratio, ratio, -1.0f, 1.0f, -1.0f, 1.0f);
glm::mat4x4 model = glm::mat4x4(1.0f);
glm::mat4x4 mvp = perspective * model;
GLint uniMvp = glsys::get_uniform_location(program, "MVP");
glUniformMatrix4fv(uniMvp, 1, GL_FALSE, matrix_raw(mvp));
glDrawArrays(GL_TRIANGLES, 0, 3);
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}