Guest User

Untitled

a guest
Oct 16th, 2023
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.75 KB | None | 0 0
  1. #ifndef CANNED_GLFW_WINDOW_HPP
  2. #define CANNED_GLFW_WINDOW_HPP
  3.  
  4. #include "include/glad/glad.h"
  5. #include "GLFW/glfw3.h"
  6. #include <cstddef>
  7. #include <initializer_list>
  8. #include <iostream>
  9. #include <exception>
  10. #include <stdexcept>
  11. #include <string_view>
  12. #include <string>
  13. #include <memory>
  14. #include <array>
  15. #include <fstream>
  16.  
  17.  
  18. class BufferObject {
  19. public:
  20. BufferObject(size_t n, GLenum target, size_t size, const void *data, GLenum usage) noexcept : current_usage(usage) {
  21. glGenBuffers(n, &buffer_id);
  22. glBindBuffer(target, buffer_id);
  23. glBufferData(target, size, data, usage);
  24. }
  25.  
  26. ~BufferObject() {
  27. unbind();
  28. }
  29.  
  30. void unbind() {
  31. glBindBuffer(current_usage, 0);
  32. }
  33.  
  34. unsigned int get_vbo_id() const {
  35. return buffer_id;
  36. }
  37.  
  38. private:
  39. GLenum current_usage;
  40. unsigned int buffer_id;
  41. };
  42.  
  43.  
  44. class GL2DTexture {
  45. public:
  46. GL2DTexture(GLint wrap_s, GLint wrap_t, GLint min_filter, GLint mag_filter,
  47. GLenum target, GLint level, GLint internal_format, size_t width, size_t height,
  48. GLint border, GLenum format, GLenum type, const void* data) noexcept;
  49.  
  50. void bind_texture() const {
  51. glBindTexture(target, texture_id);
  52. }
  53.  
  54. private:
  55. unsigned int texture_id;
  56. GLenum target;
  57. };
  58.  
  59.  
  60. class VAObject {
  61. public:
  62. explicit VAObject(size_t n) noexcept {
  63. glGenVertexArrays(n, &vao_id);
  64. }
  65.  
  66. ~VAObject() noexcept {
  67. unbind_vao();
  68. }
  69.  
  70. void bind_vao() const {
  71. glBindVertexArray(vao_id);
  72. }
  73.  
  74. void unbind_vao() const {
  75. glBindVertexArray(0);
  76. }
  77.  
  78. unsigned int get_vao_id() const {
  79. return vao_id;
  80. }
  81.  
  82. private:
  83. unsigned int vao_id;
  84. };
  85.  
  86.  
  87. class GLShader {
  88. public:
  89. GLShader(const std::string &src, size_t count, GLenum shader_type, int *length) noexcept {
  90. const char *src_c_string = src.c_str();
  91.  
  92. shader_id = glCreateShader(shader_type);
  93. glShaderSource(shader_id, count, &src_c_string, length);
  94. glCompileShader(shader_id);
  95. glGetShaderiv(shader_id, GL_COMPILE_STATUS, &compile_status);
  96. }
  97.  
  98. GLShader(const std::string &file_location, GLenum shader_type);
  99.  
  100. int get_compile_status() const {
  101. return compile_status;
  102. }
  103.  
  104. unsigned int get_shader_id() const {
  105. return shader_id;
  106. }
  107.  
  108. ~GLShader() {
  109. glDeleteShader(shader_id);
  110. }
  111.  
  112. void get_shader_infolog() const {
  113. char *log;
  114. glGetShaderInfoLog(shader_id, 512, NULL, log);
  115. std::cout << log << std::endl;
  116. }
  117.  
  118. private:
  119. unsigned int shader_id;
  120. int compile_status;
  121. };
  122.  
  123.  
  124. class GLShaderProgram {
  125. public:
  126. GLShaderProgram(std::initializer_list<std::unique_ptr<GLShader>*> shaders) noexcept {
  127. shader_program_id = glCreateProgram();
  128.  
  129. for (auto shader : shaders)
  130. glAttachShader(shader_program_id, (*shader)->get_shader_id());
  131.  
  132. for (auto shader : shaders)
  133. shader->reset();
  134.  
  135. glLinkProgram(shader_program_id);
  136. }
  137.  
  138. int get_link_status() {
  139. return link_status;
  140. }
  141.  
  142. void get_program_infolog() {
  143. char *log;
  144. glGetProgramInfoLog(shader_program_id, 512, NULL, log);
  145. std::cout << log << std::endl;
  146. }
  147.  
  148. void bind_attribute_location(int location, const char *attr) {
  149. glBindAttribLocation(shader_program_id, location, attr);
  150. }
  151.  
  152. template<size_t matrix_size>
  153. void set_uniform_matrix(const char *uniform_name, GLsizei n, bool transpose, const float *data) {
  154. int uniform_location = glGetUniformLocation(shader_program_id, uniform_name);
  155.  
  156. if (uniform_location == -1)
  157. throw std::runtime_error("uniform does not exist");
  158.  
  159. if (matrix_size == 4)
  160. glUniformMatrix4fv(uniform_location, n, transpose, data);
  161. if (matrix_size == 3)
  162. glUniformMatrix3fv(uniform_location, n, transpose, data);
  163. if (matrix_size == 2)
  164. glUniformMatrix2fv(uniform_location, n, transpose, data);
  165. }
  166.  
  167. void set_uniform(const char *uniform_name, const std::array<float, 4> &values) {
  168. int uniform_location = glGetUniformLocation(shader_program_id, uniform_name);
  169.  
  170. if (uniform_location == -1)
  171. throw std::runtime_error("uniform does not exist");
  172.  
  173. glUniform4f(uniform_location, values[0], values[1], values[2], values[3]);
  174. }
  175.  
  176. void set_uniform(const char *uniform_name, const std::array<float, 3> &values) {
  177. int uniform_location = glGetUniformLocation(shader_program_id, uniform_name);
  178.  
  179. if (uniform_location == -1)
  180. throw std::runtime_error("uniform does not exist");
  181.  
  182. glUniform3f(uniform_location, values[0], values[1], values[2]);
  183. }
  184.  
  185. void set_uniform(const char *uniform_name, const std::array<float, 2> &values) {
  186. int uniform_location = glGetUniformLocation(shader_program_id, uniform_name);
  187.  
  188. if (uniform_location == -1)
  189. throw std::runtime_error("uniform does not exist");
  190.  
  191. glUniform2f(uniform_location, values[0], values[1]);
  192. }
  193.  
  194. void set_uniform(const char *uniform_name, const std::array<int, 4> &values) {
  195. int uniform_location = glGetUniformLocation(shader_program_id, uniform_name);
  196.  
  197. if (uniform_location == -1)
  198. throw std::runtime_error("uniform does not exist");
  199.  
  200. glUniform4i(uniform_location, values[0], values[1], values[2], values[3]);
  201. }
  202.  
  203. void set_uniform(const char *uniform_name, const std::array<int, 3> &values) {
  204. int uniform_location = glGetUniformLocation(shader_program_id, uniform_name);
  205.  
  206. if (uniform_location == -1)
  207. throw std::runtime_error("uniform does not exist");
  208.  
  209. glUniform3i(uniform_location, values[0], values[1], values[2]);
  210. }
  211.  
  212. void set_uniform(const char *uniform_name, const std::array<int, 2> &values) {
  213. int uniform_location = glGetUniformLocation(shader_program_id, uniform_name);
  214.  
  215. if (uniform_location == -1)
  216. throw std::runtime_error("uniform does not exist");
  217.  
  218. glUniform2i(uniform_location, values[0], values[1]);
  219. }
  220.  
  221. void set_uniform(const char *uniform_name, const std::array<unsigned int, 4> &values) {
  222. int uniform_location = glGetUniformLocation(shader_program_id, uniform_name);
  223.  
  224. if (uniform_location == -1)
  225. throw std::runtime_error("uniform does not exist");
  226.  
  227. glUniform4ui(uniform_location, values[0], values[1], values[2], values[3]);
  228. }
  229.  
  230. void set_uniform(const char *uniform_name, const std::array<unsigned int, 3> &values) {
  231. int uniform_location = glGetUniformLocation(shader_program_id, uniform_name);
  232.  
  233. if (uniform_location == -1)
  234. throw std::runtime_error("uniform does not exist");
  235.  
  236. glUniform3ui(uniform_location, values[0], values[1], values[2]);
  237. }
  238.  
  239. void set_uniform(const char *uniform_name, const std::array<unsigned int, 2> &values) {
  240. int uniform_location = glGetUniformLocation(shader_program_id, uniform_name);
  241.  
  242. if (uniform_location == -1)
  243. throw std::runtime_error("uniform does not exist");
  244.  
  245. glUniform2ui(uniform_location, values[0], values[1]);
  246. }
  247.  
  248. void set_uniform(const char *uniform_name, float value) {
  249. int uniform_location = glGetUniformLocation(shader_program_id, uniform_name);
  250.  
  251. if (uniform_location == -1)
  252. throw std::runtime_error("uniform does not exist");
  253.  
  254. glUniform1f(uniform_location, value);
  255. }
  256.  
  257. void set_uniform(const char *uniform_name, unsigned int value) {
  258. int uniform_location = glGetUniformLocation(shader_program_id, uniform_name);
  259.  
  260. if (uniform_location == -1)
  261. throw std::runtime_error("uniform does not exist");
  262.  
  263. glUniform1ui(uniform_location, value);
  264. }
  265.  
  266. void set_uniform(const char *uniform_name, int value) {
  267. int uniform_location = glGetUniformLocation(shader_program_id, uniform_name);
  268.  
  269. if (uniform_location == -1)
  270. throw std::runtime_error("uniform does not exist");
  271.  
  272. glUniform1i(uniform_location, value);
  273. }
  274.  
  275. unsigned int get_program_id() const {
  276. return shader_program_id;
  277. }
  278.  
  279. void use_shader_program() const {
  280. glUseProgram(shader_program_id);
  281. }
  282.  
  283. private:
  284. unsigned int shader_program_id;
  285. int link_status;
  286. };
  287.  
  288.  
  289. class GLFWInitFailed: public std::exception {
  290. public:
  291. GLFWInitFailed(int err_code, const char *msg) {
  292. full_error_message = "[" + std::to_string(err_code) + "] " + std::string(msg);
  293. }
  294.  
  295. const char *what() const noexcept override {
  296. return full_error_message.c_str();
  297. }
  298.  
  299. private:
  300. std::string full_error_message;
  301. };
  302.  
  303.  
  304. void error_callback(int err_code, const char *err_msg);
  305.  
  306.  
  307. void framebuffer_size_callback(GLFWwindow *window, int width, int height);
  308.  
  309.  
  310. class GLFWWindow {
  311. friend void error_callback(int err_code, const char *err_msg);
  312. friend void framebuffer_size_callback(GLFWwindow *window, size_t width, size_t height);
  313. public:
  314. GLFWWindow(size_t width, size_t height, const char *title, GLFWmonitor *monitor, GLFWwindow *share,
  315. int version_major, int version_minor, int profile);
  316.  
  317. ~GLFWWindow() noexcept {
  318. glfwTerminate();
  319. }
  320.  
  321. GLFWWindow(const GLFWWindow &rhs) = delete;
  322. GLFWWindow &operator=(const GLFWWindow &rhs) = delete;
  323.  
  324. void set_viewport(int x, int y, size_t width, size_t height) {
  325. glViewport(x, y, width, height);
  326. glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
  327. viewport_x = x;
  328. viewport_y = y;
  329. }
  330.  
  331. void main_loop_setup() {
  332. glfwSwapBuffers(window);
  333. glfwPollEvents();
  334. }
  335.  
  336. void get_gl_string(GLenum name) {
  337. std::cout << glGetString(name) << std::endl;
  338. }
  339.  
  340. bool should_close() {
  341. return glfwWindowShouldClose(window);
  342. }
  343.  
  344. void set_close() {
  345. glfwSetWindowShouldClose(window, true);
  346. }
  347.  
  348. void set_color(float r, float g, float b, float a) {
  349. glClearColor(r, g, b, a);
  350. }
  351.  
  352. void clear_buffer(GLbitfield mask) {
  353. glClear(mask);
  354. }
  355.  
  356. int get_event(int key) {
  357. return glfwGetKey(window, key);
  358. }
  359.  
  360. static int viewport_x;
  361. static int viewport_y;
  362.  
  363. private:
  364. GLFWwindow *window;
  365. };
  366. #endif
Advertisement
Add Comment
Please, Sign In to add comment