/* * * Copyright (C) 2023 Brett Terpstra * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #ifndef BLT_WITH_GRAPHICS_STATE_H #define BLT_WITH_GRAPHICS_STATE_H #include #include #include namespace blt::gfx { /** * layout (std140) uniform GlobalMatrices * { * mat4 perspective; // full 3d perspective matrix * mat4 ortho; // full 2d orthographic matrix * mat4 view; // view matrix * mat4 pvm; // perspective view matrix * mat4 ovm; // ortho view matrix * }; */ class matrix_state_manager { private: blt::mat4x4 perspective; blt::mat4x4 ortho; blt::mat4x4 view; blt::mat4x4 pvm; blt::mat4x4 ovm; blt::mat4x4 view_2d; blt::vec3 scale_2d; std::int32_t last_width = 0, last_height = 0; uniform_buffer* global_matrix_ubo = nullptr; public: [[nodiscard]] inline const blt::mat4x4& computedPVM() const { return pvm; } [[nodiscard]] inline const blt::mat4x4& computedOVM() const { return ovm; } void create_internals() { global_matrix_ubo = new uniform_buffer(sizeof(blt::mat4x4) * 5); } void cleanup() { delete global_matrix_ubo; } template void setView(U&& mat) { view = (mat); } template void setView2D(U&& mat, G&& scale) { view_2d = (mat); scale_2d = (scale); } template void setPerspective(U&& mat) { perspective = (mat); } template void setOrtho(U&& mat) { ortho = (mat); } void update_perspectives(std::int32_t width, std::int32_t height, float fov = 90, float near = 0.1f, float far = 500.0f, float ortho_near = 0, float ortho_far = 1.1); void update(); const mat4x4& getView() const; const mat4x4& getOrtho() const; const mat4x4& getPerspective() const; const mat4x4& getPvm() const; const mat4x4& getOvm() const; const mat4x4& getView2D() const; const vec3& getScale2D() const; }; } #endif //BLT_WITH_GRAPHICS_STATE_H