Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef CANNED_GLFW_WINDOW_HPP
- #define CANNED_GLFW_WINDOW_HPP
- #include "include/glad/glad.h"
- #include "GLFW/glfw3.h"
- #include <cstddef>
- #include <initializer_list>
- #include <iostream>
- #include <exception>
- #include <stdexcept>
- #include <string_view>
- #include <string>
- #include <memory>
- #include <array>
- #include <fstream>
- class BufferObject {
- public:
- BufferObject(size_t n, GLenum target, size_t size, const void *data, GLenum usage) noexcept : current_usage(usage) {
- glGenBuffers(n, &buffer_id);
- glBindBuffer(target, buffer_id);
- glBufferData(target, size, data, usage);
- }
- ~BufferObject() {
- unbind();
- }
- void unbind() {
- glBindBuffer(current_usage, 0);
- }
- unsigned int get_vbo_id() const {
- return buffer_id;
- }
- private:
- GLenum current_usage;
- unsigned int buffer_id;
- };
- class GL2DTexture {
- public:
- GL2DTexture(GLint wrap_s, GLint wrap_t, GLint min_filter, GLint mag_filter,
- GLenum target, GLint level, GLint internal_format, size_t width, size_t height,
- GLint border, GLenum format, GLenum type, const void* data) noexcept;
- void bind_texture() const {
- glBindTexture(target, texture_id);
- }
- private:
- unsigned int texture_id;
- GLenum target;
- };
- class VAObject {
- public:
- explicit VAObject(size_t n) noexcept {
- glGenVertexArrays(n, &vao_id);
- }
- ~VAObject() noexcept {
- unbind_vao();
- }
- void bind_vao() const {
- glBindVertexArray(vao_id);
- }
- void unbind_vao() const {
- glBindVertexArray(0);
- }
- unsigned int get_vao_id() const {
- return vao_id;
- }
- private:
- unsigned int vao_id;
- };
- class GLShader {
- public:
- GLShader(const std::string &src, size_t count, GLenum shader_type, int *length) noexcept {
- const char *src_c_string = src.c_str();
- shader_id = glCreateShader(shader_type);
- glShaderSource(shader_id, count, &src_c_string, length);
- glCompileShader(shader_id);
- glGetShaderiv(shader_id, GL_COMPILE_STATUS, &compile_status);
- }
- GLShader(const std::string &file_location, GLenum shader_type);
- int get_compile_status() const {
- return compile_status;
- }
- unsigned int get_shader_id() const {
- return shader_id;
- }
- ~GLShader() {
- glDeleteShader(shader_id);
- }
- void get_shader_infolog() const {
- char *log;
- glGetShaderInfoLog(shader_id, 512, NULL, log);
- std::cout << log << std::endl;
- }
- private:
- unsigned int shader_id;
- int compile_status;
- };
- class GLShaderProgram {
- public:
- GLShaderProgram(std::initializer_list<std::unique_ptr<GLShader>*> shaders) noexcept {
- shader_program_id = glCreateProgram();
- for (auto shader : shaders)
- glAttachShader(shader_program_id, (*shader)->get_shader_id());
- for (auto shader : shaders)
- shader->reset();
- glLinkProgram(shader_program_id);
- }
- int get_link_status() {
- return link_status;
- }
- void get_program_infolog() {
- char *log;
- glGetProgramInfoLog(shader_program_id, 512, NULL, log);
- std::cout << log << std::endl;
- }
- void bind_attribute_location(int location, const char *attr) {
- glBindAttribLocation(shader_program_id, location, attr);
- }
- template<size_t matrix_size>
- void set_uniform_matrix(const char *uniform_name, GLsizei n, bool transpose, const float *data) {
- int uniform_location = glGetUniformLocation(shader_program_id, uniform_name);
- if (uniform_location == -1)
- throw std::runtime_error("uniform does not exist");
- if (matrix_size == 4)
- glUniformMatrix4fv(uniform_location, n, transpose, data);
- if (matrix_size == 3)
- glUniformMatrix3fv(uniform_location, n, transpose, data);
- if (matrix_size == 2)
- glUniformMatrix2fv(uniform_location, n, transpose, data);
- }
- void set_uniform(const char *uniform_name, const std::array<float, 4> &values) {
- int uniform_location = glGetUniformLocation(shader_program_id, uniform_name);
- if (uniform_location == -1)
- throw std::runtime_error("uniform does not exist");
- glUniform4f(uniform_location, values[0], values[1], values[2], values[3]);
- }
- void set_uniform(const char *uniform_name, const std::array<float, 3> &values) {
- int uniform_location = glGetUniformLocation(shader_program_id, uniform_name);
- if (uniform_location == -1)
- throw std::runtime_error("uniform does not exist");
- glUniform3f(uniform_location, values[0], values[1], values[2]);
- }
- void set_uniform(const char *uniform_name, const std::array<float, 2> &values) {
- int uniform_location = glGetUniformLocation(shader_program_id, uniform_name);
- if (uniform_location == -1)
- throw std::runtime_error("uniform does not exist");
- glUniform2f(uniform_location, values[0], values[1]);
- }
- void set_uniform(const char *uniform_name, const std::array<int, 4> &values) {
- int uniform_location = glGetUniformLocation(shader_program_id, uniform_name);
- if (uniform_location == -1)
- throw std::runtime_error("uniform does not exist");
- glUniform4i(uniform_location, values[0], values[1], values[2], values[3]);
- }
- void set_uniform(const char *uniform_name, const std::array<int, 3> &values) {
- int uniform_location = glGetUniformLocation(shader_program_id, uniform_name);
- if (uniform_location == -1)
- throw std::runtime_error("uniform does not exist");
- glUniform3i(uniform_location, values[0], values[1], values[2]);
- }
- void set_uniform(const char *uniform_name, const std::array<int, 2> &values) {
- int uniform_location = glGetUniformLocation(shader_program_id, uniform_name);
- if (uniform_location == -1)
- throw std::runtime_error("uniform does not exist");
- glUniform2i(uniform_location, values[0], values[1]);
- }
- void set_uniform(const char *uniform_name, const std::array<unsigned int, 4> &values) {
- int uniform_location = glGetUniformLocation(shader_program_id, uniform_name);
- if (uniform_location == -1)
- throw std::runtime_error("uniform does not exist");
- glUniform4ui(uniform_location, values[0], values[1], values[2], values[3]);
- }
- void set_uniform(const char *uniform_name, const std::array<unsigned int, 3> &values) {
- int uniform_location = glGetUniformLocation(shader_program_id, uniform_name);
- if (uniform_location == -1)
- throw std::runtime_error("uniform does not exist");
- glUniform3ui(uniform_location, values[0], values[1], values[2]);
- }
- void set_uniform(const char *uniform_name, const std::array<unsigned int, 2> &values) {
- int uniform_location = glGetUniformLocation(shader_program_id, uniform_name);
- if (uniform_location == -1)
- throw std::runtime_error("uniform does not exist");
- glUniform2ui(uniform_location, values[0], values[1]);
- }
- void set_uniform(const char *uniform_name, float value) {
- int uniform_location = glGetUniformLocation(shader_program_id, uniform_name);
- if (uniform_location == -1)
- throw std::runtime_error("uniform does not exist");
- glUniform1f(uniform_location, value);
- }
- void set_uniform(const char *uniform_name, unsigned int value) {
- int uniform_location = glGetUniformLocation(shader_program_id, uniform_name);
- if (uniform_location == -1)
- throw std::runtime_error("uniform does not exist");
- glUniform1ui(uniform_location, value);
- }
- void set_uniform(const char *uniform_name, int value) {
- int uniform_location = glGetUniformLocation(shader_program_id, uniform_name);
- if (uniform_location == -1)
- throw std::runtime_error("uniform does not exist");
- glUniform1i(uniform_location, value);
- }
- unsigned int get_program_id() const {
- return shader_program_id;
- }
- void use_shader_program() const {
- glUseProgram(shader_program_id);
- }
- private:
- unsigned int shader_program_id;
- int link_status;
- };
- class GLFWInitFailed: public std::exception {
- public:
- GLFWInitFailed(int err_code, const char *msg) {
- full_error_message = "[" + std::to_string(err_code) + "] " + std::string(msg);
- }
- const char *what() const noexcept override {
- return full_error_message.c_str();
- }
- private:
- std::string full_error_message;
- };
- void error_callback(int err_code, const char *err_msg);
- void framebuffer_size_callback(GLFWwindow *window, int width, int height);
- class GLFWWindow {
- friend void error_callback(int err_code, const char *err_msg);
- friend void framebuffer_size_callback(GLFWwindow *window, size_t width, size_t height);
- public:
- GLFWWindow(size_t width, size_t height, const char *title, GLFWmonitor *monitor, GLFWwindow *share,
- int version_major, int version_minor, int profile);
- ~GLFWWindow() noexcept {
- glfwTerminate();
- }
- GLFWWindow(const GLFWWindow &rhs) = delete;
- GLFWWindow &operator=(const GLFWWindow &rhs) = delete;
- void set_viewport(int x, int y, size_t width, size_t height) {
- glViewport(x, y, width, height);
- glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
- viewport_x = x;
- viewport_y = y;
- }
- void main_loop_setup() {
- glfwSwapBuffers(window);
- glfwPollEvents();
- }
- void get_gl_string(GLenum name) {
- std::cout << glGetString(name) << std::endl;
- }
- bool should_close() {
- return glfwWindowShouldClose(window);
- }
- void set_close() {
- glfwSetWindowShouldClose(window, true);
- }
- void set_color(float r, float g, float b, float a) {
- glClearColor(r, g, b, a);
- }
- void clear_buffer(GLbitfield mask) {
- glClear(mask);
- }
- int get_event(int key) {
- return glfwGetKey(window, key);
- }
- static int viewport_x;
- static int viewport_y;
- private:
- GLFWwindow *window;
- };
- #endif
Advertisement
Add Comment
Please, Sign In to add comment