diff --git a/include/engine/camera.hpp b/include/engine/camera.hpp index a9a11e1..7db0ab0 100644 --- a/include/engine/camera.hpp +++ b/include/engine/camera.hpp @@ -103,6 +103,18 @@ namespace egn{ void recalc_projection_matrix()const override; }; + class flat_camera : public ortho_camera + { + public: + flat_camera(float w, float h); + flat_camera(const flat_camera&) = default; + flat_camera(flat_camera&&) = default; + ~flat_camera(void) = default; + + flat_camera& operator=(const flat_camera&) = default; + flat_camera& operator=(flat_camera&&) = default; + }; + } #endif diff --git a/include/engine/font.hpp b/include/engine/font.hpp index 278ed8d..f4e7b4f 100644 --- a/include/engine/font.hpp +++ b/include/engine/font.hpp @@ -117,6 +117,7 @@ namespace egn{ } template font_atlas font::generate_atlas(size_t glyph_w, size_t glyph_h, Ts&&... ranges){ + debug_print_verbose("Generating font atlas for font '%s'\n", m_face->family_name); FT_Set_Pixel_Sizes(m_face, glyph_w, glyph_h); auto [atlas_width, atlas_height, max_glyph_width, max_glyph_height] = get_atlas_size_ranges_(std::forward(ranges)...); const auto format = (m_depth == 3) ? GL_RGB : GL_RED; diff --git a/include/engine/menu.hpp b/include/engine/menu.hpp new file mode 100644 index 0000000..701ac61 --- /dev/null +++ b/include/engine/menu.hpp @@ -0,0 +1,105 @@ +/** + This file is a part of our_dick + Copyright (C) 2022 rexy712 + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero 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 Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . +*/ + +#ifndef OUR_DICK_ENGINE_MENU_HPP +#define OUR_DICK_ENGINE_MENU_HPP + +#include "camera.hpp" +#include "font.hpp" +#include "menu_renderer.hpp" + +#include "math/vec.hpp" + +#include +#include //unique_ptr, shared_ptr + +namespace egn::gui{ + + class menu; + + class menu_item + { + protected: + menu* m_owner = nullptr; + protected: + menu_item(menu* owner); + public: + menu_item(const menu_item&) = default; + menu_item(menu_item&&) = default; + virtual ~menu_item(void) = default; + + menu_item& operator=(const menu_item&) = default; + menu_item& operator=(menu_item&&) = default; + + void set_owner(menu* owner); + + virtual void render(menu_renderer& r) = 0; + + virtual menu_item* clone(void)const = 0; + }; + + struct menu_node{ + math::vec2f scale; + }; + + struct position_node : public menu_node{ + std::unique_ptr item; + math::vec2f position; + }; + + class menu + { + private: + std::vector m_items; + std::shared_ptr m_default_font; + flat_camera m_camera; + + public: + menu(void); + menu(const menu&) = default; + menu(menu&&) = default; + ~menu(void) = default; + + menu& operator=(const menu&) = default; + menu& operator=(menu&&) = default; + + position_node& add_item(const menu_item& m); + template + position_node& emplace_item(Args&&... args); + + void set_default_font(const std::shared_ptr& font); + const std::shared_ptr& get_default_font(void)const; + + void resize(float w, float h); + + void render(menu_renderer& r); + }; + + + template + position_node& menu::emplace_item(Args&&... args){ + position_node tmp; + tmp.scale = math::vec2f{1, 1}; + tmp.item = std::unique_ptr(new T{this, std::forward(args)...}); + tmp.position = math::vec2f{0, 0}; + return m_items.emplace_back(std::move(tmp)); + } + +} + +#endif diff --git a/include/engine/menu_renderer.hpp b/include/engine/menu_renderer.hpp new file mode 100644 index 0000000..bbd4410 --- /dev/null +++ b/include/engine/menu_renderer.hpp @@ -0,0 +1,69 @@ +/** + This file is a part of our_dick + Copyright (C) 2022 rexy712 + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero 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 Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . +*/ + +#ifndef OUR_DICK_ENGINE_MENU_RENDERER_HPP +#define OUR_DICK_ENGINE_MENU_RENDERER_HPP + +#include "math/vec.hpp" + +#include "graphics/resource_manager.hpp" +#include "graphics/shader_program.hpp" +#include "graphics/fbo.hpp" +#include "graphics/ubo.hpp" + +namespace egn{ + + class renderer + { + private: + math::vec4f m_saved_viewport{0, 0, 0, 0}; + + public: + void set_viewport(float x, float y, float w, float h); + void resize_viewport(float w, float h); + void reposition_viewport(float x, float y); + void apply_viewport(void); + }; + + class menu_renderer : public renderer + { + private: + gfx::ubo m_vp_matrix; + std::shared_ptr m_font_shader; + public: + menu_renderer(gfx::resource_manager& resman); + menu_renderer(const menu_renderer&) = default; + menu_renderer(menu_renderer&&) = default; + ~menu_renderer(void) = default; + + menu_renderer& operator=(const menu_renderer&) = default; + menu_renderer& operator=(menu_renderer&&) = default; + + void set_font(const gfx::texture& atlas); + void set_position(const math::vec2f& pos); + void set_vp_matrix(const math::mat4f& matrix); + + void begin(void); + void draw_text(int count); + void draw_text_to(gfx::fbo& target, int count); + void end(void); + }; + +} + +#endif diff --git a/include/engine/text.hpp b/include/engine/text.hpp index 06d1454..6d2bc72 100644 --- a/include/engine/text.hpp +++ b/include/engine/text.hpp @@ -22,12 +22,15 @@ #include "graphics/vbo.hpp" #include "graphics/vao.hpp" #include "graphics/texture.hpp" -#include "graphics/shader_program.hpp" #include "font.hpp" #include "math/vec.hpp" #include "flat_object.hpp" #include +#include +#include + +class font_renderer; namespace egn{ @@ -49,10 +52,11 @@ namespace egn{ void set_text(const char* string); - void render(gfx::shader_program& sh, const math::vec2f& pos); + void render(font_renderer& sh, const math::vec2f& pos); }; + class text : public flat::object_base { private: @@ -64,7 +68,7 @@ namespace egn{ void set_text(const char* string); - void render(gfx::shader_program& sh); + void render(font_renderer& sh); }; } diff --git a/include/engine/text_field.hpp b/include/engine/text_field.hpp new file mode 100644 index 0000000..ea0067e --- /dev/null +++ b/include/engine/text_field.hpp @@ -0,0 +1,63 @@ +/** + This file is a part of our_dick + Copyright (C) 2022 rexy712 + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero 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 Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . +*/ + +#ifndef OUR_DICK_ENGINE_TEXT_FIELD_HPP +#define OUR_DICK_ENGINE_TEXT_FIELD_HPP + +#include "menu.hpp" + +#include "graphics/text_field_view.hpp" + +#include "engine/font.hpp" + +#include +#include //shared_ptr + +namespace egn::gui{ + + struct text_field_model{ + std::string text; + std::shared_ptr font; + }; + + class text_field : public menu_item + { + private: + text_field_model m_model; + gfx::text_field_view m_view; + + public: + text_field(menu* m); + text_field(menu* m, const char* data); + text_field(const text_field&); + text_field(text_field&&); + + text_field& operator=(const text_field&) = default; + text_field& operator=(text_field&&) = default; + + const std::string& get_text(void)const; + void set_font(const std::shared_ptr& f); + const std::shared_ptr& get_font(void)const; + + void render(menu_renderer& r)override; + text_field* clone(void)const override; + }; + +} + +#endif diff --git a/include/graphics/buffer_map.hpp b/include/graphics/buffer_map.hpp new file mode 100644 index 0000000..2636314 --- /dev/null +++ b/include/graphics/buffer_map.hpp @@ -0,0 +1,136 @@ +/** + This file is a part of our_dick + Copyright (C) 2020-2022 rexy712 + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero 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 Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . +*/ + +#ifndef OUR_DICK_GRAPHICS_BUFFER_MAP_HPP +#define OUR_DICK_GRAPHICS_BUFFER_MAP_HPP + +#include "gl_include.hpp" + +#include //size_t, ptrdiff_t + +namespace gfx{ + + namespace buffer{ + //strongly typed enum for different mapping styles + enum class maptype : GLenum{ + READ = GL_READ_ONLY, + WRITE = GL_WRITE_ONLY, + RW = GL_READ_WRITE, + }; + //strongly typed enum for different usages + enum class usage : GLenum{ + STATIC_DRAW = GL_STATIC_DRAW, + DYNAMIC_DRAW = GL_DYNAMIC_DRAW, + STREAM_DRAW = GL_STREAM_DRAW, + STATIC_READ = GL_STATIC_READ, + DYNAMIC_READ = GL_DYNAMIC_READ, + STREAM_READ = GL_STREAM_READ, + STATIC_COPY = GL_STATIC_COPY, + DYNAMIC_COPY = GL_DYNAMIC_COPY, + STREAM_COPY = GL_STREAM_COPY, + }; + } + + //RAII wrapper for mapping vbo's + template + class scoped_buffer_map + { + public: + using value_type = T; + using size_type = size_t; + using difference_type = ptrdiff_t; + using reference = T&; + using const_reference = const T&; + using pointer = T*; + using const_pointer = const T*; + + private: + pointer m_data = nullptr; //pointer to the mapped region + GLuint m_buffer; + + public: + //create a mapping for 'v' on 'targ' with map style 'm' + explicit scoped_buffer_map(GLuint bid, buffer::maptype m); + scoped_buffer_map(const scoped_buffer_map&) = delete; + scoped_buffer_map(scoped_buffer_map&&); + ~scoped_buffer_map(void); + + scoped_buffer_map& operator=(const scoped_buffer_map&) = delete; + scoped_buffer_map& operator=(scoped_buffer_map&&); + + //size of the mapped region as reported by glGetBufferParameteriv + size_type length(void)const; + + //release ownership of the mapping. unsafe + pointer release(void); + + //direct access to the underlaying data region + operator pointer(void); + operator const_pointer(void)const; + pointer raw(void); + const_pointer raw(void)const; + + bool valid(void)const; + + //indexed access to the data region + reference operator[](size_type i); + const_reference operator[](size_type i)const; + }; + + //specialization for void pointers. The same in every way except no subscript operators + template<> + class scoped_buffer_map + { + public: + using value_type = void; + using size_type = size_t; + using difference_type = ptrdiff_t; + using reference = void; + using const_reference = void; + using pointer = void*; + using const_pointer = const void*; + + private: + pointer m_data = nullptr; + GLuint m_buffer; + + public: + explicit scoped_buffer_map(GLuint bid, buffer::maptype m); + scoped_buffer_map(const scoped_buffer_map&) = delete; + scoped_buffer_map(scoped_buffer_map&&); + ~scoped_buffer_map(void); + + scoped_buffer_map& operator=(const scoped_buffer_map&) = delete; + scoped_buffer_map& operator=(scoped_buffer_map&&); + + size_type length(void)const; + + pointer release(void); + operator pointer(void); + operator const_pointer(void)const; + pointer raw(void); + const_pointer raw(void)const; + + bool valid(void)const; + }; + +} + +#include "buffer_map.tpp" + +#endif diff --git a/include/graphics/buffer_map.tpp b/include/graphics/buffer_map.tpp new file mode 100644 index 0000000..674d42a --- /dev/null +++ b/include/graphics/buffer_map.tpp @@ -0,0 +1,91 @@ +/** + This file is a part of our_dick + Copyright (C) 2020-2022 rexy712 + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero 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 Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . +*/ + +#ifndef OUR_DICK_GRAPHICS_BUFFER_MAP_TPP +#define OUR_DICK_GRAPHICS_BUFFER_MAP_TPP + +#include //exchange, swap +#include "gl_include.hpp" + +namespace gfx{ + + template + scoped_buffer_map::scoped_buffer_map(GLuint bid, buffer::maptype m): + m_buffer(bid) + { + m_data = reinterpret_cast(glMapNamedBuffer(m_buffer, static_cast(m))); + } + template + scoped_buffer_map::scoped_buffer_map(scoped_buffer_map&& m): + m_data(std::exchange(m.m_data, nullptr)), + m_buffer(std::exchange(m.m_buffer, 0)){} + template + scoped_buffer_map::~scoped_buffer_map(void){ + if(m_data){ + glUnmapNamedBuffer(m_buffer); + } + } + template + scoped_buffer_map& scoped_buffer_map::operator=(scoped_buffer_map&& m){ + std::swap(m_data, m.m_data); + std::swap(m_buffer, m.m_buffer); + return *this; + } + template + auto scoped_buffer_map::length(void)const -> size_type{ + GLint64 retval; + glGetNamedBufferParameteri64v(m_buffer, GL_BUFFER_MAP_LENGTH, &retval); + return retval; + } + template + auto scoped_buffer_map::release(void) -> pointer{ + m_buffer = 0; + return std::exchange(m_data, nullptr); + } + template + scoped_buffer_map::operator pointer(void){ + return m_data; + } + template + scoped_buffer_map::operator const_pointer(void)const{ + return m_data; + } + template + auto scoped_buffer_map::raw(void) -> pointer{ + return m_data; + } + template + auto scoped_buffer_map::raw(void)const -> const_pointer{ + return m_data; + } + template + bool scoped_buffer_map::valid(void)const{ + return m_data; + } + template + auto scoped_buffer_map::operator[](size_type i) -> reference{ + return m_data[i]; + } + template + auto scoped_buffer_map::operator[](size_type i)const -> const_reference{ + return m_data[i]; + } + +} + +#endif diff --git a/include/graphics/fbo.hpp b/include/graphics/fbo.hpp index fd3f1d8..3115502 100644 --- a/include/graphics/fbo.hpp +++ b/include/graphics/fbo.hpp @@ -20,7 +20,6 @@ #define OUR_DICK_GRAPHICS_FBO_HPP #include "gl_include.hpp" -#include "gl_buffers.hpp" #include "texture.hpp" #include "rbo.hpp" #include "math/vec.hpp" @@ -62,7 +61,6 @@ namespace gfx{ const math::vec4& get_viewport()const; bool bind()const; - bool bind(gl_frame_buffer_manager& b)const; private: template void i_attach(const rbo& r, GLenum point, Args&&... args); diff --git a/include/graphics/gl_buffers.hpp b/include/graphics/gl_buffers.hpp deleted file mode 100644 index ee72c6a..0000000 --- a/include/graphics/gl_buffers.hpp +++ /dev/null @@ -1,159 +0,0 @@ -/** - This file is a part of our_dick - Copyright (C) 2020 rexy712 - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Affero 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 Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License - along with this program. If not, see . -*/ - -#ifndef OUR_DICK_GRAPHICS_GL_BUFFERS_HPP -#define OUR_DICK_GRAPHICS_GL_BUFFERS_HPP - -#include "gl_include.hpp" - -namespace gfx{ - - class gl_buffer_manager; - class gl_frame_buffer_manager; - - //handle interfacing with the gl_buffer objects. Used as intermediary so that multiple classes have the ability to - //access gl_buffers easily - template - class buffer_accessor_iface - { - public: - using buffer_type = T; - using reference = buffer_type&; - using const_reference = const buffer_type&; - using pointer = buffer_type*; - using const_pointer = const buffer_type*; - protected: - mutable pointer m_bound = nullptr; //pointer to currently bound target - GLuint m_raw_id = 0; - protected: - buffer_accessor_iface() = default; - buffer_accessor_iface(GLuint raw_id); - buffer_accessor_iface(const buffer_accessor_iface&) = delete; - buffer_accessor_iface(buffer_accessor_iface&&); - ~buffer_accessor_iface(); - - buffer_accessor_iface& operator=(const buffer_accessor_iface&) = delete; - buffer_accessor_iface& operator=(buffer_accessor_iface&&); - - protected: - //set the given gl_buffer target to use this buffer - bool i_bind(reference b)const; - //same as bind, but also disallow rebinding until unlock is called - bool i_bind_lock(reference b)const; - //set the associated target to point to nothing. also calls unlock - void i_unbind()const; - public: - //release the lock held on the target - void lock()const; - void unlock()const; - //check if a lock is held on the associated target - bool is_locked()const; - //get raw access to opengl target id - GLenum get_bound_id()const; - //get raw access to opengl buffer id - GLuint get_buffer_id()const; - void set_buffer_id(GLuint raw_id); - //get access to opengl target buffer - pointer get_bound_buffer()const; - }; - //implement bind/unbind with glBindBuffer - class buffer_accessor : public buffer_accessor_iface - { - public: - using buffer_accessor_iface::buffer_accessor_iface; - bool bind(reference b)const; - bool bind_lock(reference b)const; - void unbind(GLuint id = 0)const; - }; - //implement bind/unbind with glBindFramebuffer - class frame_buffer_accessor : public buffer_accessor_iface - { - public: - using buffer_accessor_iface::buffer_accessor_iface; - bool bind(reference b)const; - bool bind_lock(reference b)const; - void unbind(GLuint id = 0)const; - }; - - template - class gl_buffer_manager_base - { - public: - protected: - const T* m_bound = nullptr; - const GLenum m_buffer; - bool m_lock = false; - protected: - gl_buffer_manager_base(GLenum buf); - ~gl_buffer_manager_base() = default; - //rely on static polymorphism with this parameter - void bind(const buffer_accessor_iface* newbind); - void lock(bool b); - bool is_locked()const; - public: - //return id of the target this represents - GLenum get_buffer_id()const; - const T* get_bound_accessor(); - }; - //Manage active opengl buffers and targets - class gl_buffer_manager : public gl_buffer_manager_base - { - friend class buffer; - friend buffer_accessor_iface; //base class of buffer_accessor - protected: - using gl_buffer_manager_base::gl_buffer_manager_base; - }; - class gl_frame_buffer_manager : public gl_buffer_manager_base - { - friend class buffer; - friend buffer_accessor_iface; //base class of frame_buffer_accessor - protected: - using gl_buffer_manager_base::gl_buffer_manager_base; - }; - - class buffer //glorified namespace for added access control - { - //list of all the standard opengl buffer targets - public: - static inline gl_buffer_manager array{GL_ARRAY_BUFFER}; - static inline gl_buffer_manager atomic_counter{GL_ATOMIC_COUNTER_BUFFER}; - static inline gl_buffer_manager copy_read{GL_COPY_READ_BUFFER}; - static inline gl_buffer_manager copy_write{GL_COPY_WRITE_BUFFER}; - static inline gl_buffer_manager dispatch_indirect{GL_DISPATCH_INDIRECT_BUFFER}; - static inline gl_buffer_manager draw_indirect{GL_DRAW_INDIRECT_BUFFER}; - static inline gl_buffer_manager element_array{GL_ELEMENT_ARRAY_BUFFER}; - static inline gl_buffer_manager pixel_pack{GL_PIXEL_PACK_BUFFER}; - static inline gl_buffer_manager pixel_unpack{GL_PIXEL_UNPACK_BUFFER}; - static inline gl_buffer_manager query{GL_QUERY_BUFFER}; - static inline gl_buffer_manager shader_storage{GL_SHADER_STORAGE_BUFFER}; - static inline gl_buffer_manager texture{GL_TEXTURE_BUFFER}; - static inline gl_buffer_manager transform_feedback{GL_TRANSFORM_FEEDBACK_BUFFER}; - static inline gl_buffer_manager uniform{GL_UNIFORM_BUFFER}; - static inline gl_frame_buffer_manager fb_draw{GL_DRAW_FRAMEBUFFER}; - static inline gl_frame_buffer_manager fb_read{GL_READ_FRAMEBUFFER}; - - private: - buffer() = default; - ~buffer() = default; - }; - -} - -#include "gl_buffers.tpp" - -#endif diff --git a/include/graphics/gl_buffers.tpp b/include/graphics/gl_buffers.tpp deleted file mode 100644 index 1b42837..0000000 --- a/include/graphics/gl_buffers.tpp +++ /dev/null @@ -1,138 +0,0 @@ -/** - This file is a part of our_dick - Copyright (C) 2020 rexy712 - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Affero 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 Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License - along with this program. If not, see . -*/ - -#ifndef OUR_DICK_GRAPHICS_GL_BUFFERS_TPP -#define OUR_DICK_GRAPHICS_GL_BUFFERS_TPP - -#include //exchange - -namespace gfx{ - - template - gl_buffer_manager_base::gl_buffer_manager_base(GLenum buf): - m_buffer(buf){} - template - void gl_buffer_manager_base::bind(const buffer_accessor_iface* newbind){ - if(m_bound){ - const T* tmp = std::exchange(m_bound, nullptr); - tmp->unbind(); - } - m_bound = static_cast(newbind); - } - template - void gl_buffer_manager_base::lock(bool b){ - m_lock = b; - } - template - bool gl_buffer_manager_base::is_locked()const{ - return m_lock; - } - template - GLenum gl_buffer_manager_base::get_buffer_id()const{ - return m_buffer; - } - template - const T* gl_buffer_manager_base::get_bound_accessor(){ - return m_bound; - } - - - template - buffer_accessor_iface::buffer_accessor_iface(GLuint raw_id): - m_raw_id(raw_id){} - template - buffer_accessor_iface::buffer_accessor_iface(buffer_accessor_iface&& b): - m_bound(std::exchange(b.m_bound, nullptr)), - m_raw_id(b.m_raw_id){} - template - buffer_accessor_iface::~buffer_accessor_iface(){ - if(m_bound) - i_unbind(); - } - template - buffer_accessor_iface& buffer_accessor_iface::operator=(buffer_accessor_iface&& b){ - std::swap(m_bound, b.m_bound); - std::swap(m_raw_id, b.m_raw_id); - return *this; - } - - template - bool buffer_accessor_iface::i_bind(reference b)const{ - if(b.is_locked()) - return false; - if(m_bound){ - pointer tmp = std::exchange(m_bound, nullptr); - tmp->bind(nullptr); - } - m_bound = &b; - m_bound->bind(this); - return true; - } - template - bool buffer_accessor_iface::i_bind_lock(reference b)const{ - if(!i_bind(b)) - return false; - m_bound->lock(true); - return true; - } - template - void buffer_accessor_iface::lock()const{ - if(m_bound) - m_bound->lock(true); - } - template - void buffer_accessor_iface::unlock()const{ - if(m_bound) - m_bound->lock(false); - } - template - void buffer_accessor_iface::i_unbind()const{ - //check for null pointers in derived implementation class - m_bound->lock(false); - m_bound->bind(nullptr); - m_bound = nullptr; - } - template - bool buffer_accessor_iface::is_locked()const{ - return m_bound && m_bound->is_locked(); - } - template - GLenum buffer_accessor_iface::get_bound_id()const{ - if(m_bound) - return m_bound->get_buffer_id(); - return 0; - } - template - GLuint buffer_accessor_iface::get_buffer_id()const{ - return m_raw_id; - } - - template - void buffer_accessor_iface::set_buffer_id(GLuint raw_id){ - m_raw_id = raw_id; - } - template - auto buffer_accessor_iface::get_bound_buffer()const -> pointer{ - if(m_bound) - return m_bound; - return nullptr; - } - -} - -#endif diff --git a/include/graphics/resource_manager.hpp b/include/graphics/resource_manager.hpp index 8537847..c83bb86 100644 --- a/include/graphics/resource_manager.hpp +++ b/include/graphics/resource_manager.hpp @@ -28,6 +28,7 @@ #include "graphics/shader_program.hpp" #include "engine/font.hpp" +#include "util/deferred.hpp" namespace gfx{ @@ -42,9 +43,11 @@ namespace gfx{ using const_reference = const T&; using pointer = T*; using const_pointer = const T*; + using node = std::shared_ptr; + using const_node = std::shared_ptr; private: - std::map m_map; + std::map> m_map; public: resource_manager(void) = default; @@ -58,18 +61,21 @@ namespace gfx{ bool has_value(const char* key)const; template - std::pair emplace_value(const char* key, Args&&... args); - pointer get_value(const char* file); + std::pair emplace_value(const char* key, Args&&... args); + node get_value(const char* file); bool erase_value(const char* key); }; } class resource_manager { + public: + template + using container_type = detail::resource_manager; private: - detail::resource_manager m_textures; - detail::resource_manager m_fonts; - detail::resource_manager m_texture_arrays; - detail::resource_manager m_shaders; + container_type m_textures; + container_type m_fonts; + container_type m_texture_arrays; + container_type m_shaders; public: resource_manager(void) = default; @@ -84,25 +90,25 @@ namespace gfx{ bool has_texture_array(const char* key)const; template auto emplace_texture_array(const char* key, Args&&... args); - gfx::texture_array* get_texture_array(const char* file); + container_type::node get_texture_array(const char* file); bool erase_texture_array(const char* key); bool has_texture(const char* key)const; template auto emplace_texture(const char* key, Args&&... args); - gfx::texture* get_texture(const char* file); + container_type::node get_texture(const char* file); bool erase_texture(const char* key); bool has_font(const char* key)const; template auto emplace_font(const char* key, Args&&... args); - egn::font_atlas* get_font(const char* file); + container_type::node get_font(const char* file); bool erase_font(const char* key); bool has_shader(const char* key)const; template auto emplace_shader(const char* key, Args&&... args); - gfx::shader_program* get_shader(const char* key); + container_type::node get_shader(const char* key); bool erase_shader(const char* key); }; @@ -115,16 +121,14 @@ namespace gfx{ } template template - auto resource_manager::emplace_value(const char* key, Args&&... args) -> std::pair{ - if(auto it = m_map.find(key);it != m_map.end()) - return {&(*it).second, false}; - auto ret = m_map.emplace(std::piecewise_construct, std::tuple(key), std::tuple(std::forward(args)...)); - return {&(*(ret.first)).second, true}; + auto resource_manager::emplace_value(const char* key, Args&&... args) -> std::pair{ + auto p = m_map.try_emplace(key, util::deferred_function(std::make_shared, std::forward(args)...)); + return {(*p.first).second, p.second}; } template - auto resource_manager::get_value(const char* key) -> pointer{ + auto resource_manager::get_value(const char* key) -> node{ if(auto it = m_map.find(key);it != m_map.end()) - return &(*it).second; + return (*it).second; return nullptr; } template diff --git a/include/graphics/scoped_buffer_bind.hpp b/include/graphics/scoped_buffer_bind.hpp deleted file mode 100644 index 1800311..0000000 --- a/include/graphics/scoped_buffer_bind.hpp +++ /dev/null @@ -1,56 +0,0 @@ -/** - This file is a part of our_dick - Copyright (C) 2020 rexy712 - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Affero 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 Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License - along with this program. If not, see . -*/ - -#ifndef OUR_DICK_GRAPHICS_SCOPED_BUFFER_BIND_HPP -#define OUR_DICK_GRAPHICS_SCOPED_BUFFER_BIND_HPP - -#include "gl_buffers.hpp" - -namespace gfx{ - - template - class scoped_buffer_bind - { - public: - using accessor_type = T; - using manager_type = typename T::buffer_type; - protected: - const accessor_type* m_accessor_handle; - manager_type* m_manager_handle; - const accessor_type* m_already_bound; - public: - explicit scoped_buffer_bind(const accessor_type& accessor, manager_type& buffer, bool lock = false); - scoped_buffer_bind(const scoped_buffer_bind&) = delete; - scoped_buffer_bind(scoped_buffer_bind&& s); - ~scoped_buffer_bind(); - - scoped_buffer_bind& operator=(const scoped_buffer_bind&) = delete; - scoped_buffer_bind& operator=(scoped_buffer_bind&& s); - - bool valid()const; - void release(); - - GLenum get_bound_id()const; - GLuint get_buffer_id()const; - }; - -} - -#include "scoped_buffer_bind.tpp" - -#endif diff --git a/include/graphics/scoped_buffer_bind.tpp b/include/graphics/scoped_buffer_bind.tpp deleted file mode 100644 index 9c67036..0000000 --- a/include/graphics/scoped_buffer_bind.tpp +++ /dev/null @@ -1,86 +0,0 @@ -/** - This file is a part of our_dick - Copyright (C) 2020 rexy712 - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Affero 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 Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License - along with this program. If not, see . -*/ - -#ifndef OUR_DICK_GRAPHICS_SCOPED_BUFFER_BIND_TPP -#define OUR_DICK_GRAPHICS_SCOPED_BUFFER_BIND_TPP - -#include //exchange, swap - -namespace gfx{ - - template - scoped_buffer_bind(T&, U&, GLuint) -> scoped_buffer_bind; - - template - scoped_buffer_bind::scoped_buffer_bind(const accessor_type& accessor, manager_type& buffer, bool lock): - m_accessor_handle(&accessor), - m_manager_handle(&buffer), - m_already_bound(buffer.get_bound_accessor()) - { - if(m_already_bound != m_accessor_handle){ - if(lock) - m_accessor_handle->bind_lock(*m_manager_handle); - else - m_accessor_handle->bind(*m_manager_handle); - }else if(lock){ - m_accessor_handle->lock(); - } - } - template - scoped_buffer_bind::scoped_buffer_bind(scoped_buffer_bind&& s): - m_accessor_handle(std::exchange(s.m_accessor_handle, nullptr)), - m_manager_handle(std::exchange(s.m_manager_handle, nullptr)), - m_already_bound(std::exchange(s.m_already_bound, nullptr)){} - template - scoped_buffer_bind::~scoped_buffer_bind(){ - if(m_already_bound != m_accessor_handle){ - release(); - if(m_already_bound) - m_already_bound->bind(*m_manager_handle); - } - } - template - scoped_buffer_bind& scoped_buffer_bind::operator=(scoped_buffer_bind&& s){ - std::swap(m_accessor_handle, s.m_accessor_handle); - std::swap(m_manager_handle, s.m_manager_handle); - std::swap(m_already_bound, s.m_already_bound); - return *this; - } - template - bool scoped_buffer_bind::valid()const{ - return m_accessor_handle->get_bound_buffer() == m_manager_handle; - } - template - void scoped_buffer_bind::release(){ - m_accessor_handle->unbind(); - } - template - GLenum scoped_buffer_bind::get_bound_id()const{ - return m_accessor_handle->get_bound_id(); - } - template - GLuint scoped_buffer_bind::get_buffer_id()const{ - return m_accessor_handle->get_buffer_id(); - } - - extern template class scoped_buffer_bind; - extern template class scoped_buffer_bind; - -} - -#endif diff --git a/include/graphics/shader_program.hpp b/include/graphics/shader_program.hpp index be055bc..1efbc95 100644 --- a/include/graphics/shader_program.hpp +++ b/include/graphics/shader_program.hpp @@ -22,6 +22,7 @@ #include "gl_include.hpp" #include "shader.hpp" #include "texture.hpp" +#include "ubo.hpp" #include //forward #include @@ -32,6 +33,189 @@ namespace gfx{ using namespace math; + class uniform + { + private: + GLuint m_shader_id; + GLint m_location; + + public: + uniform(GLuint owner, const char* name); + uniform(GLuint owner, GLuint location); + uniform(const uniform&) = default; + uniform(uniform&&) = default; + ~uniform(void) = default; + + uniform& operator=(const uniform&) = default; + uniform& operator=(uniform&&) = default; + + GLenum get_type(void)const; + + GLfloat get_float(void)const; + math::vec2 get_vec2(void)const; + math::vec3 get_vec3(void)const; + math::vec4 get_vec4(void)const; + + GLdouble get_double(void)const; + math::vec2 get_dvec2(void)const; + math::vec3 get_dvec3(void)const; + math::vec4 get_dvec4(void)const; + + GLint get_int(void)const; + math::vec2 get_ivec2(void)const; + math::vec3 get_ivec3(void)const; + math::vec4 get_ivec4(void)const; + + GLuint get_uint(void)const; + math::vec2 get_uvec2(void)const; + math::vec3 get_uvec3(void)const; + math::vec4 get_uvec4(void)const; + + GLboolean get_bool(void)const; + math::vec2 get_bvec2(void)const; + math::vec3 get_bvec3(void)const; + math::vec4 get_bvec4(void)const; + + math::mat2 get_mat2(void)const; + math::mat3 get_mat3(void)const; + math::mat4 get_mat4(void)const; + math::matrix get_mat2x3(void)const; + math::matrix get_mat2x4(void)const; + math::matrix get_mat3x2(void)const; + math::matrix get_mat3x4(void)const; + math::matrix get_mat4x2(void)const; + math::matrix get_mat4x3(void)const; + + math::mat2 get_dmat2(void)const; + math::mat3 get_dmat3(void)const; + math::mat4 get_dmat4(void)const; + math::matrix get_dmat2x3(void)const; + math::matrix get_dmat2x4(void)const; + math::matrix get_dmat3x2(void)const; + math::matrix get_dmat3x4(void)const; + math::matrix get_dmat4x2(void)const; + math::matrix get_dmat4x3(void)const; + + //float + void set(GLfloat); + void set(GLfloat, GLfloat); + void set(GLfloat, GLfloat, GLfloat); + void set(GLfloat, GLfloat, GLfloat, GLfloat); + + //int + void set(GLint); + void set(GLint, GLint); + void set(GLint, GLint, GLint); + void set(GLint, GLint, GLint, GLint); + + //unsigned int + void set(GLuint); + void set(GLuint, GLuint); + void set(GLuint, GLuint, GLuint); + void set(GLuint, GLuint, GLuint, GLuint); + + //Texture + void set(const texture&, GLuint tex_unit = 0); + void set(const texture_array& t, GLuint tex_unit = 0); + void set(const weak_texture&, GLuint tex_unit = 0); + + //Float vectors + void set(const vec2&); + void set(const vec3&); + void set(const vec4&); + + //Float vector array + void set(GLsizei count, const vec2*); + void set(GLsizei count, const vec3*); + void set(GLsizei count, const vec4*); + + //Explicit float vectors + void set_1(GLsizei count, const GLfloat*); + void set_2(GLsizei count, const GLfloat*); + void set_3(GLsizei count, const GLfloat*); + void set_4(GLsizei count, const GLfloat*); + + //Int vectors + void set(const vec2&); + void set(const vec3&); + void set(const vec4&); + + //Int vector array + void set(GLsizei count, const vec2*); + void set(GLsizei count, const vec3*); + void set(GLsizei count, const vec4*); + + //Explicit int vectors + void set_1(GLsizei count, const GLint*); + void set_2(GLsizei count, const GLint*); + void set_3(GLsizei count, const GLint*); + void set_4(GLsizei count, const GLint*); + + //Unsigned int vectors + void set(const vec2&); + void set(const vec3&); + void set(const vec4&); + + //Unsigned int vector array + void set(GLsizei count, const vec2*); + void set(GLsizei count, const vec3*); + void set(GLsizei count, const vec4*); + + //Explicit unsigned int vectors + void set_1(GLsizei count, const GLuint*); + void set_2(GLsizei count, const GLuint*); + void set_3(GLsizei count, const GLuint*); + void set_4(GLsizei count, const GLuint*); + + //Matrices + void set(const mat2&); + void set(const mat3&); + void set(const mat4&); + + //Matrix array + void set(GLsizei count, const mat2*); + void set(GLsizei count, const mat3*); + void set(GLsizei count, const mat4*); + + //Explicit matrices + void set_mat2(GLsizei count, const GLfloat*); + void set_mat3(GLsizei count, const GLfloat*); + void set_mat4(GLsizei count, const GLfloat*); + void set_mat2x3(GLsizei count, const GLfloat*); + void set_mat3x2(GLsizei count, const GLfloat*); + void set_mat2x4(GLsizei count, const GLfloat*); + void set_mat4x2(GLsizei count, const GLfloat*); + void set_mat3x4(GLsizei count, const GLfloat*); + void set_mat4x3(GLsizei count, const GLfloat*); + + private: + GLint get_location_from_name_(const char* name); + }; + + class uniform_block + { + private: + GLuint m_shader_id; + GLuint m_index; + + public: + uniform_block(GLuint owner, GLuint index); + uniform_block(GLuint owner, const char* name); + uniform_block(const uniform_block&) = default; + uniform_block(uniform_block&&) = default; + ~uniform_block(void) = default; + + uniform_block& operator=(const uniform_block&) = default; + uniform_block& operator=(uniform_block&&) = default; + + void set_binding(size_t binding); + template + void set_binding(size_t binding, const ubo& u); + + private: + GLuint get_index_from_name_(const char* name); + }; + //represents an opengl program (NOT a shader object) class shader_program { @@ -40,19 +224,19 @@ namespace gfx{ public: //create the program with no shaders attached - shader_program(); + shader_program(void); //create the program and attach 'args'... as shaders template shader_program(Args&&... args); shader_program(const shader_program&) = delete; shader_program(shader_program&&); - ~shader_program(); + ~shader_program(void); shader_program& operator=(const shader_program&) = delete; shader_program& operator=(shader_program&&); //release ownership of the program handle. unsafe - GLuint release(); + GLuint release(void); //attach a single shader to this program void attach_shader(const shader& s); @@ -61,283 +245,32 @@ namespace gfx{ void attach_shaders(const shader& s, Args&&... args); //attempt to link the program with the currently attached shaders. returns true on success - bool link(); + bool link(void); //set this program as the currently active one in the gl context - void use(); + void use(void); //search for a uniform by name in this program GLint get_uniform_loc(const char* u)const; + GLuint get_uniform_block_loc(const char* u)const; //raw access to the program handle - GLuint raw()const; + GLuint raw(void)const; //check if an error exists after a link attempt - bool has_link_error()const; + bool has_link_error(void)const; //get the most recently generated error from this program's infolog - std::string get_error()const; + std::string get_error(void)const; + + uniform get_uniform(const char* name); + uniform get_uniform(int uloc); GLenum get_uniform_type(const char* name)const; GLenum get_uniform_type(int uloc)const; - GLfloat get_uniform_float(int uloc)const; - math::vec2 get_uniform_vec2(int uloc)const; - math::vec3 get_uniform_vec3(int uloc)const; - math::vec4 get_uniform_vec4(int uloc)const; - - GLdouble get_uniform_double(int uloc)const; - math::vec2 get_uniform_dvec2(int uloc)const; - math::vec3 get_uniform_dvec3(int uloc)const; - math::vec4 get_uniform_dvec4(int uloc)const; - - GLint get_uniform_int(int uloc)const; - math::vec2 get_uniform_ivec2(int uloc)const; - math::vec3 get_uniform_ivec3(int uloc)const; - math::vec4 get_uniform_ivec4(int uloc)const; - - GLuint get_uniform_uint(int uloc)const; - math::vec2 get_uniform_uvec2(int uloc)const; - math::vec3 get_uniform_uvec3(int uloc)const; - math::vec4 get_uniform_uvec4(int uloc)const; - - GLboolean get_uniform_bool(int uloc)const; - math::vec2 get_uniform_bvec2(int uloc)const; - math::vec3 get_uniform_bvec3(int uloc)const; - math::vec4 get_uniform_bvec4(int uloc)const; - - math::mat2 get_uniform_mat2(int uloc)const; - math::mat3 get_uniform_mat3(int uloc)const; - math::mat4 get_uniform_mat4(int uloc)const; - math::matrix get_uniform_mat2x3(int uloc)const; - math::matrix get_uniform_mat2x4(int uloc)const; - math::matrix get_uniform_mat3x2(int uloc)const; - math::matrix get_uniform_mat3x4(int uloc)const; - math::matrix get_uniform_mat4x2(int uloc)const; - math::matrix get_uniform_mat4x3(int uloc)const; - - math::mat2 get_uniform_dmat2(int uloc)const; - math::mat3 get_uniform_dmat3(int uloc)const; - math::mat4 get_uniform_dmat4(int uloc)const; - math::matrix get_uniform_dmat2x3(int uloc)const; - math::matrix get_uniform_dmat2x4(int uloc)const; - math::matrix get_uniform_dmat3x2(int uloc)const; - math::matrix get_uniform_dmat3x4(int uloc)const; - math::matrix get_uniform_dmat4x2(int uloc)const; - math::matrix get_uniform_dmat4x3(int uloc)const; - - GLfloat get_uniform_float(const char* name)const; - math::vec2 get_uniform_vec2(const char* name)const; - math::vec3 get_uniform_vec3(const char* name)const; - math::vec4 get_uniform_vec4(const char* name)const; - - GLdouble get_uniform_double(const char* name)const; - math::vec2 get_uniform_dvec2(const char* name)const; - math::vec3 get_uniform_dvec3(const char* name)const; - math::vec4 get_uniform_dvec4(const char* name)const; - - GLint get_uniform_int(const char* name)const; - math::vec2 get_uniform_ivec2(const char* name)const; - math::vec3 get_uniform_ivec3(const char* name)const; - math::vec4 get_uniform_ivec4(const char* name)const; - - GLuint get_uniform_uint(const char* name)const; - math::vec2 get_uniform_uvec2(const char* name)const; - math::vec3 get_uniform_uvec3(const char* name)const; - math::vec4 get_uniform_uvec4(const char* name)const; - - GLboolean get_uniform_bool(const char* name)const; - math::vec2 get_uniform_bvec2(const char* name)const; - math::vec3 get_uniform_bvec3(const char* name)const; - math::vec4 get_uniform_bvec4(const char* name)const; - - math::mat2 get_uniform_mat2(const char* name)const; - math::mat3 get_uniform_mat3(const char* name)const; - math::mat4 get_uniform_mat4(const char* name)const; - math::matrix get_uniform_mat2x3(const char* name)const; - math::matrix get_uniform_mat2x4(const char* name)const; - math::matrix get_uniform_mat3x2(const char* name)const; - math::matrix get_uniform_mat3x4(const char* name)const; - math::matrix get_uniform_mat4x2(const char* name)const; - math::matrix get_uniform_mat4x3(const char* name)const; - - math::mat2 get_uniform_dmat2(const char* name)const; - math::mat3 get_uniform_dmat3(const char* name)const; - math::mat4 get_uniform_dmat4(const char* name)const; - math::matrix get_uniform_dmat2x3(const char* name)const; - math::matrix get_uniform_dmat2x4(const char* name)const; - math::matrix get_uniform_dmat3x2(const char* name)const; - math::matrix get_uniform_dmat3x4(const char* name)const; - math::matrix get_uniform_dmat4x2(const char* name)const; - math::matrix get_uniform_dmat4x3(const char* name)const; - - //Uniforms - //float - void set_uniform(const char* name, GLfloat); - void set_uniform(const char* name, GLfloat, GLfloat); - void set_uniform(const char* name, GLfloat, GLfloat, GLfloat); - void set_uniform(const char* name, GLfloat, GLfloat, GLfloat, GLfloat); - - void set_uniform(GLuint loc, GLfloat); - void set_uniform(GLuint loc, GLfloat, GLfloat); - void set_uniform(GLuint loc, GLfloat, GLfloat, GLfloat); - void set_uniform(GLuint loc, GLfloat, GLfloat, GLfloat, GLfloat); - - //int - void set_uniform(const char* name, GLint); - void set_uniform(const char* name, GLint, GLint); - void set_uniform(const char* name, GLint, GLint, GLint); - void set_uniform(const char* name, GLint, GLint, GLint, GLint); - - void set_uniform(GLuint loc, GLint); - void set_uniform(GLuint loc, GLint, GLint); - void set_uniform(GLuint loc, GLint, GLint, GLint); - void set_uniform(GLuint loc, GLint, GLint, GLint, GLint); - - //unsigned int - void set_uniform(const char* name, GLuint); - void set_uniform(const char* name, GLuint, GLuint); - void set_uniform(const char* name, GLuint, GLuint, GLuint); - void set_uniform(const char* name, GLuint, GLuint, GLuint, GLuint); - - void set_uniform(GLuint loc, GLuint); - void set_uniform(GLuint loc, GLuint, GLuint); - void set_uniform(GLuint loc, GLuint, GLuint, GLuint); - void set_uniform(GLuint loc, GLuint, GLuint, GLuint, GLuint); - - //Texture - void set_uniform(const char* name, const texture&, GLuint tex_unit = 0); - void set_uniform(GLuint loc, const texture&, GLuint tex_unit = 0); - void set_uniform(const char* name, const texture_array& t, GLuint tex_unit = 0); - void set_uniform(GLuint loc, const texture_array& t, GLuint tex_unit = 0); - void set_uniform(const char* name, const weak_texture&, GLuint tex_unit = 0); - void set_uniform(GLuint loc, const weak_texture&, GLuint tex_unit = 0); - - //Float vectors - void set_uniform(const char* name, const vec2&); - void set_uniform(const char* name, const vec3&); - void set_uniform(const char* name, const vec4&); - - void set_uniform(GLuint loc, const vec2&); - void set_uniform(GLuint loc, const vec3&); - void set_uniform(GLuint loc, const vec4&); - - //Float vector array - void set_uniform(const char* name, GLsizei count, const vec2*); - void set_uniform(const char* name, GLsizei count, const vec3*); - void set_uniform(const char* name, GLsizei count, const vec4*); - - void set_uniform(GLuint loc, GLsizei count, const vec2*); - void set_uniform(GLuint loc, GLsizei count, const vec3*); - void set_uniform(GLuint loc, GLsizei count, const vec4*); - - //Explicit float vectors - void set_uniform_1(const char* name, GLsizei count, const GLfloat*); - void set_uniform_2(const char* name, GLsizei count, const GLfloat*); - void set_uniform_3(const char* name, GLsizei count, const GLfloat*); - void set_uniform_4(const char* name, GLsizei count, const GLfloat*); - - void set_uniform_1(GLuint loc, GLsizei count, const GLfloat*); - void set_uniform_2(GLuint loc, GLsizei count, const GLfloat*); - void set_uniform_3(GLuint loc, GLsizei count, const GLfloat*); - void set_uniform_4(GLuint loc, GLsizei count, const GLfloat*); - - //Int vectors - void set_uniform(const char* name, const vec2&); - void set_uniform(const char* name, const vec3&); - void set_uniform(const char* name, const vec4&); - - void set_uniform(GLuint loc, const vec2&); - void set_uniform(GLuint loc, const vec3&); - void set_uniform(GLuint loc, const vec4&); - - //Int vector array - void set_uniform(const char* name, GLsizei count, const vec2*); - void set_uniform(const char* name, GLsizei count, const vec3*); - void set_uniform(const char* name, GLsizei count, const vec4*); - - void set_uniform(GLuint loc, GLsizei count, const vec2*); - void set_uniform(GLuint loc, GLsizei count, const vec3*); - void set_uniform(GLuint loc, GLsizei count, const vec4*); - - //Explicit int vectors - void set_uniform_1(const char* name, GLsizei count, const GLint*); - void set_uniform_2(const char* name, GLsizei count, const GLint*); - void set_uniform_3(const char* name, GLsizei count, const GLint*); - void set_uniform_4(const char* name, GLsizei count, const GLint*); - - void set_uniform_1(GLuint loc, GLsizei count, const GLint*); - void set_uniform_2(GLuint loc, GLsizei count, const GLint*); - void set_uniform_3(GLuint loc, GLsizei count, const GLint*); - void set_uniform_4(GLuint loc, GLsizei count, const GLint*); - - //Unsigned int vectors - void set_uniform(const char* name, const vec2&); - void set_uniform(const char* name, const vec3&); - void set_uniform(const char* name, const vec4&); - - void set_uniform(GLuint loc, const vec2&); - void set_uniform(GLuint loc, const vec3&); - void set_uniform(GLuint loc, const vec4&); - - //Unsigned int vector array - void set_uniform(const char* name, GLsizei count, const vec2*); - void set_uniform(const char* name, GLsizei count, const vec3*); - void set_uniform(const char* name, GLsizei count, const vec4*); - - void set_uniform(GLuint loc, GLsizei count, const vec2*); - void set_uniform(GLuint loc, GLsizei count, const vec3*); - void set_uniform(GLuint loc, GLsizei count, const vec4*); - - //Explicit unsigned int vectors - void set_uniform_1(const char* name, GLsizei count, const GLuint*); - void set_uniform_2(const char* name, GLsizei count, const GLuint*); - void set_uniform_3(const char* name, GLsizei count, const GLuint*); - void set_uniform_4(const char* name, GLsizei count, const GLuint*); - - void set_uniform_1(GLuint loc, GLsizei count, const GLuint*); - void set_uniform_2(GLuint loc, GLsizei count, const GLuint*); - void set_uniform_3(GLuint loc, GLsizei count, const GLuint*); - void set_uniform_4(GLuint loc, GLsizei count, const GLuint*); - - //Matrices - void set_uniform(const char* name, const mat2&); - void set_uniform(const char* name, const mat3&); - void set_uniform(const char* name, const mat4&); - - void set_uniform(GLuint loc, const mat2&); - void set_uniform(GLuint loc, const mat3&); - void set_uniform(GLuint loc, const mat4&); - - //Matrix array - void set_uniform(const char* name, GLsizei count, const mat2*); - void set_uniform(const char* name, GLsizei count, const mat3*); - void set_uniform(const char* name, GLsizei count, const mat4*); - - void set_uniform(GLuint loc, GLsizei count, const mat2*); - void set_uniform(GLuint loc, GLsizei count, const mat3*); - void set_uniform(GLuint loc, GLsizei count, const mat4*); - - //Explicit matrices - void set_uniform_mat2(const char* name, GLsizei count, const GLfloat*); - void set_uniform_mat3(const char* name, GLsizei count, const GLfloat*); - void set_uniform_mat4(const char* name, GLsizei count, const GLfloat*); - void set_uniform_mat2x3(const char* name, GLsizei count, const GLfloat*); - void set_uniform_mat3x2(const char* name, GLsizei count, const GLfloat*); - void set_uniform_mat2x4(const char* name, GLsizei count, const GLfloat*); - void set_uniform_mat4x2(const char* name, GLsizei count, const GLfloat*); - void set_uniform_mat3x4(const char* name, GLsizei count, const GLfloat*); - void set_uniform_mat4x3(const char* name, GLsizei count, const GLfloat*); - - void set_uniform_mat2(GLuint loc, GLsizei count, const GLfloat*); - void set_uniform_mat3(GLuint loc, GLsizei count, const GLfloat*); - void set_uniform_mat4(GLuint loc, GLsizei count, const GLfloat*); - void set_uniform_mat2x3(GLuint loc, GLsizei count, const GLfloat*); - void set_uniform_mat3x2(GLuint loc, GLsizei count, const GLfloat*); - void set_uniform_mat2x4(GLuint loc, GLsizei count, const GLfloat*); - void set_uniform_mat4x2(GLuint loc, GLsizei count, const GLfloat*); - void set_uniform_mat3x4(GLuint loc, GLsizei count, const GLfloat*); - void set_uniform_mat4x3(GLuint loc, GLsizei count, const GLfloat*); + uniform_block get_uniform_block(const char* name); + uniform_block get_uniform_block(unsigned int uloc); }; template @@ -355,6 +288,12 @@ namespace gfx{ } } + template + void uniform_block::set_binding(size_t binding, const ubo& u){ + u.bind(binding); + set_binding(binding); + } + } #endif diff --git a/include/graphics/text_field_view.hpp b/include/graphics/text_field_view.hpp new file mode 100644 index 0000000..dba86f1 --- /dev/null +++ b/include/graphics/text_field_view.hpp @@ -0,0 +1,68 @@ +/** + This file is a part of our_dick + Copyright (C) 2022 rexy712 + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero 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 Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . +*/ + +#ifndef OUR_DICK_GRAPHICS_TEXT_FIELD_VIEW_HPP +#define OUR_DICK_GRAPHICS_TEXT_FIELD_VIEW_HPP + +#include "engine/font.hpp" +#include "engine/menu_renderer.hpp" + +#include "vao.hpp" +#include "vbo.hpp" + +#include //shared_ptr + +namespace egn::gui{ + struct text_field_model; +} + +namespace gfx{ + + class text_field_view + { + private: + static constexpr int s_size_per_char = sizeof(float) * 12; + private: + egn::gui::text_field_model* m_model; + gfx::vbo m_buffer; + gfx::vao m_vao; + mutable unsigned char m_dirty_bit:1 = 0; + + public: + text_field_view(egn::gui::text_field_model& m); + text_field_view(egn::gui::text_field_model& m, const char* data); + text_field_view(const text_field_view&); + text_field_view(text_field_view&&) = default; + ~text_field_view(void) = default; + + text_field_view& operator=(const text_field_view&); + text_field_view& operator=(text_field_view&&) = default; + + bool set_text(const char* text); + + void set_model(egn::gui::text_field_model& t); + + void render(egn::menu_renderer& r); + + private: + void init_arrays_(void); + }; + +} + +#endif diff --git a/include/graphics/ubo.hpp b/include/graphics/ubo.hpp new file mode 100644 index 0000000..366d001 --- /dev/null +++ b/include/graphics/ubo.hpp @@ -0,0 +1,153 @@ +/** + This file is a part of our_dick + Copyright (C) 2022 rexy712 + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero 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 Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . +*/ + +#ifndef OUR_DICK_GRAPHICS_UBO_HPP +#define OUR_DICK_GRAPHICS_UBO_HPP + +#include "gl_include.hpp" + +#include "math/mat.hpp" +#include "math/vec.hpp" + +#include //a lot of stuff + +#include "buffer_map.hpp" + +namespace gfx{ + + namespace detail{ + template + struct is_bounded_array{ + static constexpr bool value = false; + }; + template + struct is_bounded_array{ + static constexpr bool value = true; + }; + + template + struct is_valid_uniform_scalar{ + static constexpr bool value = std::is_same_v || + std::is_same_v || + std::is_same_v || + std::is_same_v || + std::is_same_v; + }; + template + struct is_valid_uniform_matrix{ + template + static constexpr bool test(math::matrix_base*){ + return is_valid_uniform_scalar::value && T::Columns <= 4 && T::Rows <= 4; + } + static constexpr bool test(void*){ + return false; + } + static constexpr bool value = test(static_cast*>(nullptr)); + }; + template + struct is_valid_uniform_type{ + static constexpr bool value = is_valid_uniform_scalar::value || is_valid_uniform_matrix::value || is_bounded_array::value; + }; + template + struct get_type{ + using type = typename get_type::type; + }; + template + struct get_type<0, Type, Types...>{ + using type = Type; + }; + template + struct get_type<0,bool,Types...>{ + using type = int; + }; + + template + using get_type_t = typename get_type::type; + + + template + constexpr size_t get_alignment_of_(void); + template + constexpr size_t get_alignment_multiple_(void); + template + constexpr size_t size_of_(void); + } + + + template + class ubo + { + private: + static_assert((detail::is_valid_uniform_type::value && ...)); + + public: + static constexpr size_t num_elements = sizeof...(Types); + + template + using type_of = detail::get_type_t; + + private: + GLuint m_buffer = 0; + + public: + ubo(buffer::usage usage = buffer::usage::DYNAMIC_DRAW); + ubo(const ubo&); + ubo(ubo&&); + ~ubo(void); + + ubo& operator=(const ubo&); + ubo& operator=(ubo&&); + + + //raw modify the data (NOT A GOOD IDEA) + scoped_buffer_map map(buffer::maptype m); + void buffer(const void* data, size_t datasize, size_t start); + void initialize(unsigned char value = 0); + + //Safely modify the data to comply with GLSL std140 layout + template + auto get(void); + template + void set(T&& t); + + GLuint raw(void)const; + + size_t get_size(void)const; + size_t get_cap(void)const; + buffer::usage get_usage(void)const; + + void bind(size_t index)const; + private: + template + static constexpr size_t get_offset_of_(void); + template + static constexpr size_t get_alignment_of_(void); + static constexpr size_t get_total_size_(void); + + public: + template + static constexpr size_t offset_of = get_offset_of_(); + template + static constexpr size_t alignment_of = get_alignment_of_(); + }; + +} + +#include "ubo.tpp" + +#endif diff --git a/include/graphics/ubo.tpp b/include/graphics/ubo.tpp new file mode 100644 index 0000000..5105c51 --- /dev/null +++ b/include/graphics/ubo.tpp @@ -0,0 +1,263 @@ +/** + This file is a part of our_dick + Copyright (C) 2022 rexy712 + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero 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 Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . +*/ + +#ifndef OUR_DICK_GRAPHICS_UBO_TPP +#define OUR_DICK_GRAPHICS_UBO_TPP + +#include //swap, exchange +#include //a lot of stuff + +#include +#include //memcpy, memset + +namespace gfx{ + + template + ubo::ubo(buffer::usage usage){ + glCreateBuffers(1, &m_buffer); + glNamedBufferData(m_buffer, get_total_size_(), NULL, static_cast(usage)); + } + template + ubo::ubo(const ubo& u){ + glCreateBuffers(1, &m_buffer); + auto data = u.map(buffer::maptype::READ); + if(!data) + return; + glNamedBufferData(m_buffer, u.get_size(), data, static_cast(u.get_usage())); + } + template + ubo::ubo(ubo&& u): + m_buffer(std::exchange(u.m_buffer, 0)){} + template + ubo::~ubo(void){ + if(m_buffer) + glDeleteBuffers(1, &m_buffer); + } + + template + auto ubo::operator=(const ubo& u) -> ubo&{ + return (*this = ubo(u)); + } + template + auto ubo::operator=(ubo&& u) -> ubo&{ + std::swap(m_buffer, u.m_buffer); + return *this; + } + + template + template + constexpr size_t ubo::get_offset_of_(void){ + static_assert(I < sizeof...(Types), "Index out of bounds"); + if constexpr(I == 0){ + return 0; + }else{ + using this_type = detail::get_type_t; + using prev_type = detail::get_type_t; + size_t prev_offset = get_offset_of_(); + constexpr size_t prev_alignment = detail::get_alignment_of_(); + constexpr size_t this_alignment = detail::get_alignment_of_(); + prev_offset += (prev_alignment * (detail::get_alignment_multiple_() - 1)); + bool is_on_alignment_multiple = (((prev_offset + prev_alignment) % this_alignment) == 0); + + if(is_on_alignment_multiple){ + return prev_offset + prev_alignment; + }else if(this_alignment > prev_alignment){ + return prev_offset + this_alignment; + }else{ + size_t i; + for(i = this_alignment * 2;i < prev_alignment;i += this_alignment); + return prev_offset + i; + } + } + } + template + template + constexpr size_t ubo::get_alignment_of_(void){ + return detail::get_alignment_of_>(); + } + + template + constexpr size_t ubo::get_total_size_(void){ + constexpr size_t type_num = sizeof...(Types) - 1; + using last_type = detail::get_type_t; + + return get_offset_of_() + (detail::get_alignment_of_() * detail::get_alignment_multiple_()); + } + + + template + scoped_buffer_map ubo::map(buffer::maptype m){ + return scoped_buffer_map(m_buffer, m); + } + + template + void ubo::buffer(const void* data, size_t datasize, size_t start){ + if(start > get_size()) + return; + glNamedBufferSubData(m_buffer, start, std::min(datasize, get_size() - start), data); + } + template + void ubo::initialize(unsigned char value){ + const size_t size = get_size(); + auto m = map(buffer::maptype::WRITE); + if(!m){ + return; + } + memset(m, size, value); + } + + template + template + auto ubo::get(void){ + using type = detail::get_type_t; + + constexpr size_t offset = get_offset_of_(); + constexpr size_t align = detail::get_alignment_of_(); + + auto m = map(buffer::maptype::READ); + unsigned char* data = static_cast(m.raw()); + data += offset; + + if constexpr(detail::is_valid_uniform_matrix::value){ + type t{}; + constexpr size_t rows = type::Rows; + constexpr size_t cols = type::Columns; + for(size_t i = 0;i < cols;++i){ + memcpy(t.raw() + (rows * i), data + (align * i), sizeof(typename type::value_type) * rows); + } + return t; + }else if constexpr(detail::is_bounded_array::value){ + static constexpr size_t arr_size = std::extent::value; + using this_value = std::decay_t()[0])>; + std::array t; + for(size_t i = 0;i < arr_size;++i){ + t[i] = *reinterpret_cast(data + (align * i)); + } + return t; + }else{ + type t{}; + memcpy(&t, data, sizeof(type)); + return t; + } + } + template + template + void ubo::set(T&& t){ + using type = detail::get_type_t; + + constexpr size_t offset = get_offset_of_(); + constexpr size_t align = detail::get_alignment_of_(); + + auto m = map(buffer::maptype::WRITE); + unsigned char* data = static_cast(m.raw()); + data += offset; + + if constexpr(detail::is_valid_uniform_matrix::value){ + static_assert(std::is_convertible_v,type>); + constexpr size_t rows = type::Rows; + constexpr size_t cols = type::Columns; + for(size_t i = 0;i < cols;++i){ + memcpy(data + (align * i), t.raw() + (rows * i), sizeof(typename type::value_type) * rows); + } + }else if constexpr(detail::is_bounded_array::value){ + using this_value = std::decay_t()[0])>; + using that_value = std::decay_t()[0])>; + static_assert(std::is_convertible_v); + for(size_t i = 0;i < std::extent::value;++i){ + this_value tmp = t[i]; + memcpy(data + (align * i), &tmp, sizeof(this_value)); + } + }else{ + static_assert(std::is_convertible_v,type>); + type tmp = t; + memcpy(data, &tmp, sizeof(type)); + } + } + + template + GLuint ubo::raw(void)const{ + return m_buffer; + } + template + size_t ubo::get_size(void)const{ + return get_total_size_(); + } + template + size_t ubo::get_cap(void)const{ + return get_total_size_(); + } + template + buffer::usage ubo::get_usage(void)const{ + GLint retval; + glGetNamedBufferParameteriv(m_buffer, GL_BUFFER_USAGE, &retval); + return static_cast(retval); + } + template + void ubo::bind(size_t index)const{ + glBindBufferBase(GL_UNIFORM_BUFFER, index, m_buffer); + } + + + + + namespace detail{ + template + constexpr size_t get_alignment_of_(void){ + if constexpr(detail::is_valid_uniform_scalar::value){ //scalar + if constexpr(std::is_same_v){ + return 8; + }else{ + return 4; + } + }else if constexpr(detail::is_bounded_array::value){ + return get_alignment_of_(); + }else{ + constexpr size_t element_alignment = get_alignment_of_(); + if constexpr(T::Columns > 1){ //matrix + return element_alignment * 4; + }else{ //vector + if constexpr(T::Rows == 2){ + return element_alignment * 2; + }else{ + return element_alignment * 4; + } + } + } + } + template + constexpr size_t get_alignment_multiple_(void){ + if constexpr(detail::is_valid_uniform_scalar::value){ + return 1; + }else if constexpr(detail::is_bounded_array::value){ + return std::extent::value; + }else{ + return T::Columns; + } + } + + template + constexpr size_t size_of_(void){ + if constexpr(std::is_same>::value){ + return 4; + } + return sizeof(T); + } + } +} + +#endif diff --git a/include/graphics/vbo.hpp b/include/graphics/vbo.hpp index b049fbe..a0b4b30 100644 --- a/include/graphics/vbo.hpp +++ b/include/graphics/vbo.hpp @@ -1,6 +1,6 @@ /** This file is a part of our_dick - Copyright (C) 2020 rexy712 + Copyright (C) 2020-2022 rexy712 This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by @@ -20,220 +20,54 @@ #define OUR_DICK_GRAPHICS_VBO_HPP #include "gl_include.hpp" -#include "gl_buffers.hpp" -#include //size_t, ptrdiff_t -#include //exchange, swap +#include //size_t + +#include "buffer_map.hpp" namespace gfx{ - template - class scoped_vbo_map; - //class representing a vertex buffer object class vbo { - public: - //strongly typed enum for different usages - enum class usage : GLenum{ - STATIC_DRAW = GL_STATIC_DRAW, - DYNAMIC_DRAW = GL_DYNAMIC_DRAW, - STREAM_DRAW = GL_STREAM_DRAW, - STATIC_READ = GL_STATIC_READ, - DYNAMIC_READ = GL_DYNAMIC_READ, - STREAM_READ = GL_STREAM_READ, - STATIC_COPY = GL_STATIC_COPY, - DYNAMIC_COPY = GL_DYNAMIC_COPY, - STREAM_COPY = GL_STREAM_COPY, - }; - //strongly typed enum for different mapping styles - enum class maptype : GLenum{ - READ = GL_READ_ONLY, - WRITE = GL_WRITE_ONLY, - RW = GL_READ_WRITE, - }; private: GLuint m_buffer; //handle to vbo size_t m_buffer_size; //amount of buffer currently in use public: //create a buffer with given capacity with predicted usage case 't' - vbo(size_t cap, usage t); - vbo(const void* data, size_t datasize, usage t); + vbo(size_t cap, buffer::usage t); + vbo(const void* data, size_t datasize, buffer::usage t); vbo(const vbo&); vbo(vbo&&); - ~vbo(); + ~vbo(void); vbo& operator=(const vbo&); vbo& operator=(vbo&&); - //attempt binding to given gl_buffer_manager. returns true on success - bool bind(gl_buffer_manager& b)const; //add data to the end of this buffer (starts loading at data+get_size()) bool buffer(const void* data, size_t datasize); bool buffer(const void* data, size_t datasize, size_t start); + + bool initialize(unsigned char value = 0); + //change capacity of buffer bool resize(size_t newcap); - GLuint raw()const; + GLuint raw(void)const; //glMapBuffer to given target with map style 'm' - scoped_vbo_map map(maptype m)const; + scoped_buffer_map map(buffer::maptype m)const; //emties the buffer. does not clear contents, just sets size to 0. - void clear(); - size_t get_size()const; - size_t get_cap()const; - usage get_usage()const; + void clear(void); + size_t get_size(void)const; + size_t get_cap(void)const; + buffer::usage get_usage(void)const; private: static bool copy_buffer(vbo& dest, const vbo& src); }; - //RAII wrapper for mapping vbo's - template - class scoped_vbo_map - { - public: - using value_type = T; - using size_type = size_t; - using difference_type = ptrdiff_t; - using reference = T&; - using const_reference = const T&; - using pointer = T*; - using const_pointer = const T*; - - private: - pointer m_data = nullptr; //pointer to the mapped region - GLuint m_buffer; - - public: - //create a mapping for 'v' on 'targ' with map style 'm' - explicit scoped_vbo_map(GLuint bid, vbo::maptype m); - scoped_vbo_map(const scoped_vbo_map&) = delete; - scoped_vbo_map(scoped_vbo_map&&); - ~scoped_vbo_map(); - - scoped_vbo_map& operator=(const scoped_vbo_map&) = delete; - scoped_vbo_map& operator=(scoped_vbo_map&&); - - //size of the mapped region as reported by glGetBufferParameteriv - size_type length()const; - - //release ownership of the mapping. unsafe - pointer release(); - - //direct access to the underlaying data region - operator pointer(); - operator const_pointer()const; - pointer raw(); - const_pointer raw()const; - - bool valid()const; - - //indexed access to the data region - reference operator[](size_type i); - const_reference operator[](size_type i)const; - }; - - //specialization for void pointers. The same in every way except no subscript operators - template<> - class scoped_vbo_map - { - public: - using value_type = void; - using size_type = size_t; - using difference_type = ptrdiff_t; - using reference = void; - using const_reference = void; - using pointer = void*; - using const_pointer = const void*; - - private: - pointer m_data = nullptr; - GLuint m_buffer; - - public: - explicit scoped_vbo_map(GLuint bid, vbo::maptype m); - scoped_vbo_map(const scoped_vbo_map&) = delete; - scoped_vbo_map(scoped_vbo_map&&); - ~scoped_vbo_map(); - - scoped_vbo_map& operator=(const scoped_vbo_map&) = delete; - scoped_vbo_map& operator=(scoped_vbo_map&&); - - size_type length()const; - - pointer release(); - operator pointer(); - operator const_pointer()const; - pointer raw(); - const_pointer raw()const; - - bool valid()const; - }; - - template - scoped_vbo_map::scoped_vbo_map(GLuint bid, vbo::maptype m): - m_buffer(bid) - { - m_data = reinterpret_cast(glMapNamedBuffer(m_buffer, static_cast(m))); - } - template - scoped_vbo_map::scoped_vbo_map(scoped_vbo_map&& m): - m_data(std::exchange(m.m_data, nullptr)), - m_buffer(std::exchange(m.m_buffer, 0)){} - template - scoped_vbo_map::~scoped_vbo_map(){ - if(m_data){ - glUnmapNamedBuffer(m_buffer); - } - } - template - scoped_vbo_map& scoped_vbo_map::operator=(scoped_vbo_map&& m){ - std::swap(m_data, m.m_data); - std::swap(m_buffer, m.m_buffer); - return *this; - } - template - auto scoped_vbo_map::length()const -> size_type{ - GLint64 retval; - glGetNamedBufferParameteri64v(m_buffer, GL_BUFFER_MAP_LENGTH, &retval); - return retval; - } - template - auto scoped_vbo_map::release() -> pointer{ - m_buffer = 0; - return std::exchange(m_data, nullptr); - } - template - scoped_vbo_map::operator pointer(){ - return m_data; - } - template - scoped_vbo_map::operator const_pointer()const{ - return m_data; - } - template - auto scoped_vbo_map::raw() -> pointer{ - return m_data; - } - template - auto scoped_vbo_map::raw()const -> const_pointer{ - return m_data; - } - template - bool scoped_vbo_map::valid()const{ - return m_data; - } - template - auto scoped_vbo_map::operator[](size_type i) -> reference{ - return m_data[i]; - } - template - auto scoped_vbo_map::operator[](size_type i)const -> const_reference{ - return m_data[i]; - } - } #endif diff --git a/include/math/fwd_declare.hpp b/include/math/fwd_declare.hpp index eca077c..cdaf206 100644 --- a/include/math/fwd_declare.hpp +++ b/include/math/fwd_declare.hpp @@ -51,24 +51,28 @@ namespace math{ using vec2u = vec2; using vec2d = vec2; using vec2s = vec2; + using vec2b = vec2; using vec3f = vec3; using vec3i = vec3; using vec3u = vec3; using vec3d = vec3; using vec3s = vec3; + using vec3b = vec3; using vec4f = vec4; using vec4i = vec4; using vec4u = vec4; using vec4d = vec4; using vec4s = vec4; + using vec4b = vec4; using mat2f = mat2; using mat2i = mat2; using mat2u = mat2; using mat2d = mat2; using mat2s = mat2; + using mat2b = mat2; using mat3f = mat3; using mat3i = mat3; @@ -81,6 +85,7 @@ namespace math{ using mat4u = mat4; using mat4d = mat4; using mat4s = mat4; + using mat4b = mat4; template using quat = quaternion; @@ -90,6 +95,7 @@ namespace math{ using quat_u = quat; using quat_d = quat; using quat_s = quat; + using quat_b = quat; } #endif diff --git a/include/math/projection.tpp b/include/math/projection.tpp index c365113..65faa5c 100644 --- a/include/math/projection.tpp +++ b/include/math/projection.tpp @@ -49,14 +49,14 @@ namespace math{ matrix ortho_projection(T w, T h, T n, T f){ return matrix(T{2} / w, T{0}, T{0}, T{0}, T{0}, T{2} / h, T{0}, T{0}, - T{0}, T{0}, T{2} / (n - f), T{0}, + T{0}, T{0}, T{1} / (n - f), T{0}, T{0}, T{0}, (n + f) / (n - f), T{1}); } template matrix ortho_asymetric_projection(T l, T r, T b, T t, T n, T f){ return matrix(T{2} / (r - l), T{0}, T{0}, T{0}, T{0}, T{2} / (t - b), T{0}, T{0}, - T{0}, T{0}, T{2} / (n - f), T{0}, + T{0}, T{0}, T{1} / (n - f), T{0}, (r + l) / (l - r), (t + b) / (b - t), (n + f) / (n - f), T{1}); } diff --git a/include/ttt/board_gfx.hpp b/include/ttt/board_gfx.hpp index 3705ee6..0afb92c 100644 --- a/include/ttt/board_gfx.hpp +++ b/include/ttt/board_gfx.hpp @@ -23,20 +23,22 @@ #include "graphics/vbo.hpp" #include "graphics/vao.hpp" #include "graphics/shader_program.hpp" +#include "graphics/resource_manager.hpp" #include //pair +#include //shared_ptr class tile; class board_gfx { private: - gfx::texture_array* m_textures; + std::shared_ptr m_textures; gfx::vao m_vao; gfx::vbo m_vbo; public: - board_gfx(std::pair resman_result); + board_gfx(gfx::resource_manager& resman); ~board_gfx(void) = default; void set_uniforms(gfx::shader_program& sh); diff --git a/include/ttt/font_renderer.hpp b/include/ttt/font_renderer.hpp index 83e241c..a477c25 100644 --- a/include/ttt/font_renderer.hpp +++ b/include/ttt/font_renderer.hpp @@ -30,16 +30,23 @@ #include "engine/font.hpp" #include "engine/text.hpp" +#include //shared_ptr + class font_renderer { private: - gfx::shader_program* m_font_shader; + std::shared_ptr m_font_shader; int m_screen_width, m_screen_height; public: font_renderer(gfx::resource_manager& res, int width, int height); - void render(egn::text&); + void set_position(const math::vec2f& pos); + void use_texture(const gfx::texture& tex); + void use_texture(const gfx::weak_texture& tex); + void set_aspect(float aspect); + + void begin(void); void resize_viewport(int width, int height); }; diff --git a/include/ttt/font_shader.hpp b/include/ttt/font_shader.hpp index de686a7..bc8b668 100644 --- a/include/ttt/font_shader.hpp +++ b/include/ttt/font_shader.hpp @@ -60,52 +60,66 @@ static constexpr char geometry_shader_text[] = flat vec4 color; }gs_out; - uniform vec2 out_size; + layout (std140) uniform view_proj_matrix{ + mat4 vp_mat; + }; uniform vec2 start_pos; - //map from [0, max_out] to [0, 2] - float size_to_screen_space(float x, float max_out){ - const float ratio = 2.0 / max_out; - return x * ratio; - } - //map from [0, max_out] to [-1, 1] - float pos_to_screen_space(float x, float max_out){ - return size_to_screen_space(x, max_out) - 1.0; - } - void main(){ //texture coordinates need done botleft->topleft->botright->topright //whereas the position goes topleft->botleft->topright->botright //because freetype generates bitmaps with inverse y axis to opengl + const vec4 position = vec4(gs_in[0].offset, -1, 1); + const mat2 scaler = mat2(vp_mat[0][0], vp_mat[0][1], vp_mat[1][0], vp_mat[1][1]); - vec2 start_screen_pos = vec2(pos_to_screen_space(start_pos.x, out_size.x), pos_to_screen_space(start_pos.y, out_size.y)); + const vec2 start_position = scaler * start_pos; + const vec2 screen_pos = start_position + (scaler * gs_in[0].offset); + const vec2 size = scaler * gs_in[0].size; - const vec3 position = vec3(start_screen_pos.x + size_to_screen_space(gs_in[0].offset.x, out_size.x), - start_screen_pos.y + size_to_screen_space(gs_in[0].offset.y, out_size.y), - -1); - const vec2 size = vec2(size_to_screen_space(gs_in[0].size.x, out_size.x), - size_to_screen_space(gs_in[0].size.y, out_size.y)); + const mat4 scale = vp_mat; - gl_Position = vec4(position.x, position.y + size.y, position.z, 1.0); + const vec4 p1 = scale * vec4(gs_in[0].offset.x, gs_in[0].offset.y + gs_in[0].size.y, -1, 1); + const vec4 p2 = scale * vec4(gs_in[0].offset, -1, 1); + const vec4 p3 = scale * vec4(gs_in[0].offset.x + gs_in[0].size.x, gs_in[0].offset.y + gs_in[0].size.y, -1, 1); + const vec4 p4 = scale * vec4(gs_in[0].offset.x + gs_in[0].size.x, gs_in[0].offset.y, -1, 1); + + gl_Position = p1; gs_out.tex_coords = gs_in[0].tex_coords.xy; gs_out.color = gs_in[0].color; EmitVertex(); - - gl_Position = vec4(position, 1.0); + gl_Position = p2; gs_out.tex_coords = gs_in[0].tex_coords.xw; gs_out.color = gs_in[0].color; EmitVertex(); - - gl_Position = vec4(position.x + size.x, position.y + size.y, position.z, 1.0); + gl_Position = p3; gs_out.tex_coords = gs_in[0].tex_coords.zy; gs_out.color = gs_in[0].color; EmitVertex(); - - gl_Position = vec4(position.x + size.x, position.y, position.z, 1.0); + gl_Position = p4; gs_out.tex_coords = gs_in[0].tex_coords.zw; gs_out.color = gs_in[0].color; EmitVertex(); +/* gl_Position = vec4(screen_pos.x, screen_pos.y + size.y, -1.0, 1.0); + gs_out.tex_coords = gs_in[0].tex_coords.xy; + gs_out.color = gs_in[0].color; + EmitVertex(); + + gl_Position = vec4(screen_pos, -1.0, 1.0); + gs_out.tex_coords = gs_in[0].tex_coords.xw; + gs_out.color = gs_in[0].color; + EmitVertex(); + + gl_Position = vec4(screen_pos.x + size.x, screen_pos.y + size.y, -1.0, 1.0); + gs_out.tex_coords = gs_in[0].tex_coords.zy; + gs_out.color = gs_in[0].color; + EmitVertex(); + + gl_Position = vec4(screen_pos.x + size.x, screen_pos.y, -1.0, 1.0); + gs_out.tex_coords = gs_in[0].tex_coords.zw; + gs_out.color = gs_in[0].color; + EmitVertex(); +*/ EndPrimitive(); } )glsl"; diff --git a/include/ttt/main_renderer.hpp b/include/ttt/main_renderer.hpp index f376555..557567e 100644 --- a/include/ttt/main_renderer.hpp +++ b/include/ttt/main_renderer.hpp @@ -27,6 +27,8 @@ #include "graphics/resource_manager.hpp" #include "basic_framebuffer.hpp" +#include //shared_ptr + class main_renderer { private: @@ -45,8 +47,8 @@ private: private: basic_framebuffer m_fb; gfx::fbo m_primary_fb; - gfx::shader_program* m_square_shader; - gfx::shader_program* m_screen_shader; + std::shared_ptr m_square_shader; + std::shared_ptr m_screen_shader; gfx::vbo m_vbo; gfx::vao m_vao; gfx::vao m_board_vao; diff --git a/include/ttt/pause_state.hpp b/include/ttt/pause_state.hpp index 4a46a34..6d57e64 100644 --- a/include/ttt/pause_state.hpp +++ b/include/ttt/pause_state.hpp @@ -23,17 +23,17 @@ #include "engine/input.hpp" #include "graphics/texture.hpp" #include "screen_renderer.hpp" -#include "font_renderer.hpp" -#include "engine/text.hpp" +#include "engine/menu.hpp" +#include "engine/menu_renderer.hpp" class pause_state : public egn::game_state_iface { private: egn::game_state_iface* m_under_state; screen_renderer m_screen_renderer; - font_renderer m_font_renderer; - egn::text m_text; + egn::menu_renderer m_menu_renderer; + egn::gui::menu m_menu; public: pause_state(egn::game& owner, egn::game_state_iface* under); diff --git a/include/ttt/screen_renderer.hpp b/include/ttt/screen_renderer.hpp index ed14402..82143f6 100644 --- a/include/ttt/screen_renderer.hpp +++ b/include/ttt/screen_renderer.hpp @@ -29,6 +29,8 @@ #include "engine/font.hpp" +#include //shared_ptr + class screen_renderer { private: @@ -45,14 +47,14 @@ private: {{-1.0f, 1.0f}, {0.0f, 1.0f}} }; private: - gfx::shader_program* m_screen_shader; + std::shared_ptr m_screen_shader; gfx::vbo m_vbo; gfx::vao m_vao; - gfx::texture* m_texture; + std::shared_ptr m_texture; int m_screen_width, m_screen_height; public: - screen_renderer(gfx::resource_manager& res, int width, int height, gfx::texture& base_tex); + screen_renderer(gfx::resource_manager& res, int width, int height, const std::shared_ptr& base_tex); void render(scene&); void resize_viewport(int width, int height); diff --git a/include/util/deferred.hpp b/include/util/deferred.hpp index aad7193..0b6f29a 100644 --- a/include/util/deferred.hpp +++ b/include/util/deferred.hpp @@ -30,7 +30,7 @@ namespace util{ using value_type = T; private: - std::tuple m_args; + std::tuple m_args; public: template @@ -58,7 +58,7 @@ namespace util{ private: Fn m_fn; - std::tuple m_args; + std::tuple m_args; public: template @@ -89,7 +89,7 @@ namespace util{ template template constexpr auto deferred::get_value_(std::index_sequence) -> value_type{ - return T{std::get(m_args)...}; + return T{std::forward>(std::get(m_args))...}; } template @@ -114,7 +114,7 @@ namespace util{ template template constexpr decltype(auto) deferred_function::get_value_(std::index_sequence){ - return std::forward(m_fn)(std::get(m_args)...); + return std::forward(m_fn)(std::forward>(std::get(m_args))...); } template diff --git a/src/engine/camera.cpp b/src/engine/camera.cpp index c065a90..4c123d9 100644 --- a/src/engine/camera.cpp +++ b/src/engine/camera.cpp @@ -103,4 +103,6 @@ namespace egn{ m_projection_matrix = math::ortho_projection(m_width, m_height, m_near, m_far); } + flat_camera::flat_camera(float w, float h): + ortho_camera(w, h, 0, 1){} } diff --git a/src/engine/menu.cpp b/src/engine/menu.cpp new file mode 100644 index 0000000..0b90a95 --- /dev/null +++ b/src/engine/menu.cpp @@ -0,0 +1,58 @@ +/** + This file is a part of our_dick + Copyright (C) 2022 rexy712 + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero 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 Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . +*/ + +#include "engine/menu.hpp" + +namespace egn::gui{ + + menu_item::menu_item(menu* owner): + m_owner(owner){} + + void menu_item::set_owner(menu* owner){ + m_owner = owner; + } + + + menu::menu(void): + m_camera(100, 100){} + + position_node& menu::add_item(const menu_item& m){ + return m_items.emplace_back(position_node{{math::vec2f{1, 1}}, std::unique_ptr(m.clone()), math::vec2f{0, 0}}); + } + + void menu::set_default_font(const std::shared_ptr& font){ + m_default_font = font; + } + const std::shared_ptr& menu::get_default_font(void)const{ + return m_default_font; + } + + void menu::resize(float w, float h){ + m_camera.set_projection_box(w, h); + } + + void menu::render(menu_renderer& r){ + math::mat4f tmp = m_camera.get_projection_matrix() * m_camera.get_view_matrix(); + r.set_vp_matrix(tmp); + for(auto& node : m_items){ + menu_item& item = (*node.item); + item.render(r); + } + } + +} diff --git a/src/engine/menu_renderer.cpp b/src/engine/menu_renderer.cpp new file mode 100644 index 0000000..c38ef8d --- /dev/null +++ b/src/engine/menu_renderer.cpp @@ -0,0 +1,88 @@ +/** + This file is a part of our_dick + Copyright (C) 2022 rexy712 + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero 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 Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . +*/ + +#include "engine/menu_renderer.hpp" + +#include "graphics/gl_include.hpp" + +#include "ttt/font_shader.hpp" + +namespace egn{ + + void renderer::set_viewport(float x, float y, float w, float h){ + m_saved_viewport[0] = x; + m_saved_viewport[1] = y; + m_saved_viewport[2] = w; + m_saved_viewport[3] = h; + } + void renderer::resize_viewport(float w, float h){ + m_saved_viewport[2] = w; + m_saved_viewport[3] = h; + } + void renderer::reposition_viewport(float x, float y){ + m_saved_viewport[0] = x; + m_saved_viewport[1] = y; + } + + void renderer::apply_viewport(void){ + glViewport(m_saved_viewport[0], m_saved_viewport[1], m_saved_viewport[2], m_saved_viewport[3]); + } + + menu_renderer::menu_renderer(gfx::resource_manager& resman): + m_font_shader(resman.emplace_shader("font_shader", + util::make_deferred(font_shader::vertex_shader_text, gfx::shader::type::VERTEX), + util::make_deferred(font_shader::geometry_shader_text, gfx::shader::type::GEOMETRY), + util::make_deferred(font_shader::fragment_shader_text, gfx::shader::type::FRAGMENT)).first) + { + m_font_shader->get_uniform_block("view_proj_matrix").set_binding(0, m_vp_matrix); + } + + void menu_renderer::set_font(const gfx::texture& atlas){ + m_font_shader->get_uniform("texture1").set(atlas); + } + + void menu_renderer::set_vp_matrix(const math::mat4f& matrix){ + m_vp_matrix.set<0>(matrix); + } + void menu_renderer::set_position(const math::vec2f& pos){ + m_font_shader->get_uniform("start_pos").set(pos); + } + + void menu_renderer::begin(void){ + glBindFramebuffer(GL_FRAMEBUFFER, 0); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + glDisable(GL_DEPTH_TEST); + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + apply_viewport(); + } + + void menu_renderer::draw_text(int count){ + m_font_shader->use(); + glDrawArrays(GL_POINTS, 0, count); + } + void menu_renderer::draw_text_to(gfx::fbo& target, int count){ + target.bind(); + target.apply_viewport(); + draw_text(count); + glBindFramebuffer(GL_FRAMEBUFFER, 0); + } + void menu_renderer::end(void){ + } + +} diff --git a/src/engine/text.cpp b/src/engine/text.cpp index 3103d61..95026eb 100644 --- a/src/engine/text.cpp +++ b/src/engine/text.cpp @@ -17,13 +17,14 @@ */ #include "engine/text.hpp" +#include "ttt/font_renderer.hpp" namespace egn{ text_gfx::text_gfx(const egn::font_atlas& font, int target_px, size_t initsize): m_atlas(font), m_font(font.metadata()), - m_vbo(initsize * s_size_per_char, gfx::vbo::usage::DYNAMIC_DRAW), + m_vbo(initsize * s_size_per_char, gfx::buffer::usage::DYNAMIC_DRAW), m_original_height(font.glyph_size().y()), m_scale(((float)target_px) / m_original_height){} text_gfx::text_gfx(const egn::font_atlas& font, int target_px, const char* string): @@ -80,10 +81,10 @@ namespace egn{ pos_offset.x() += (m_scale * ch.advance.x()); } } - void text_gfx::render(gfx::shader_program& sh, const math::vec2f& pos){ + void text_gfx::render(font_renderer& sh, const math::vec2f& pos){ m_vao.bind(); - sh.set_uniform("texture1", m_atlas); - sh.set_uniform("start_pos", pos); + sh.use_texture(m_atlas); + sh.set_position(pos); glDrawArrays(GL_POINTS, 0, m_vbo.get_size() / s_size_per_char); } @@ -99,8 +100,8 @@ namespace egn{ m_gfx.set_text(string); } - void text::render(gfx::shader_program& sh){ - m_gfx.render(sh, m_position); + void text::render(font_renderer& f){ + m_gfx.render(f, m_position); } } diff --git a/src/engine/text_field.cpp b/src/engine/text_field.cpp new file mode 100644 index 0000000..64d27df --- /dev/null +++ b/src/engine/text_field.cpp @@ -0,0 +1,65 @@ +/** + This file is a part of our_dick + Copyright (C) 2022 rexy712 + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero 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 Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . +*/ + +#include "engine/text_field.hpp" + +namespace egn::gui{ + + text_field::text_field(menu* m): + menu_item(m), + m_model{nullptr, m->get_default_font()}, + m_view(m_model){} + + text_field::text_field(menu* m, const char* data): + menu_item(m), + m_model{data, m->get_default_font()}, + m_view(m_model, data){} + text_field::text_field(const text_field& t): + menu_item(t), + m_model(t.m_model), + m_view(t.m_view) + { + m_view.set_model(m_model); + } + text_field::text_field(text_field&& t): + menu_item(std::move(t)), + m_model(std::move(t.m_model)), + m_view(std::move(t.m_view)) + { + m_view.set_model(m_model); + } + + const std::string& text_field::get_text(void)const{ + return m_model.text; + } + void text_field::set_font(const std::shared_ptr& f){ + m_model.font = f; + } + const std::shared_ptr& text_field::get_font(void)const{ + return m_model.font; + } + + void text_field::render(menu_renderer& r){ + m_view.render(r); + } + + text_field* text_field::clone(void)const{ + return new text_field(*this); + } + +} diff --git a/src/graphics/buffer_map.cpp b/src/graphics/buffer_map.cpp new file mode 100644 index 0000000..3e72406 --- /dev/null +++ b/src/graphics/buffer_map.cpp @@ -0,0 +1,72 @@ +/** + This file is a part of our_dick + Copyright (C) 2020-2022 rexy712 + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero 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 Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . +*/ + +#include "graphics/buffer_map.hpp" + +#include "config.hpp" + +#include //exchange, swap + +namespace gfx{ + + scoped_buffer_map::scoped_buffer_map(GLuint bid, buffer::maptype m): + m_buffer(bid) + { + debug_print_verbose("Mapping vertex buffer object %u\n", bid); + m_data = reinterpret_cast(glMapNamedBuffer(m_buffer, static_cast(m))); + } + scoped_buffer_map::scoped_buffer_map(scoped_buffer_map&& m): + m_data(std::exchange(m.m_data, nullptr)), + m_buffer(std::exchange(m.m_buffer, 0)){} + scoped_buffer_map::~scoped_buffer_map(){ + if(m_data){ + debug_print_verbose("Unmapping vertex buffer object %u\n", m_buffer); + glUnmapNamedBuffer(m_buffer); + } + } + scoped_buffer_map& scoped_buffer_map::operator=(scoped_buffer_map&& m){ + std::swap(m_data, m.m_data); + std::swap(m_buffer, m.m_buffer); + return *this; + } + auto scoped_buffer_map::length(void)const -> size_type{ + GLint64 retval; + glGetNamedBufferParameteri64v(m_buffer, GL_BUFFER_MAP_LENGTH, &retval); + return retval; + } + auto scoped_buffer_map::release(void) -> pointer{ + m_buffer = 0; + return std::exchange(m_data, nullptr); + } + scoped_buffer_map::operator pointer(void){ + return m_data; + } + scoped_buffer_map::operator const_pointer(void)const{ + return m_data; + } + auto scoped_buffer_map::raw(void) -> pointer{ + return m_data; + } + auto scoped_buffer_map::raw(void)const -> const_pointer{ + return m_data; + } + bool scoped_buffer_map::valid(void)const{ + return m_data; + } + +} diff --git a/src/graphics/fbo.cpp b/src/graphics/fbo.cpp index 55f9c74..b6f1aff 100644 --- a/src/graphics/fbo.cpp +++ b/src/graphics/fbo.cpp @@ -18,7 +18,6 @@ #include "graphics/fbo.hpp" #include //swap, exchange -#include "graphics/scoped_buffer_bind.hpp" #include "config.hpp" namespace gfx{ @@ -94,9 +93,5 @@ namespace gfx{ glBindFramebuffer(GL_FRAMEBUFFER, m_buffer); return true; } - bool fbo::bind(gl_frame_buffer_manager& b)const{ - glBindFramebuffer(b.get_buffer_id(), m_buffer); - return true; - } } diff --git a/src/graphics/gl_buffers.cpp b/src/graphics/gl_buffers.cpp deleted file mode 100644 index 1be3957..0000000 --- a/src/graphics/gl_buffers.cpp +++ /dev/null @@ -1,61 +0,0 @@ -/** - This file is a part of our_dick - Copyright (C) 2020 rexy712 - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Affero 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 Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License - along with this program. If not, see . -*/ - -#include "graphics/gl_buffers.hpp" - -namespace gfx{ - - bool buffer_accessor::bind(reference b)const{ - if(!buffer_accessor_iface::i_bind(b)) - return false; - glBindBuffer(b.get_buffer_id(), m_raw_id); - return true; - } - bool buffer_accessor::bind_lock(reference b)const{ - if(!buffer_accessor_iface::i_bind_lock(b)) - return false; - glBindBuffer(b.get_buffer_id(), m_raw_id); - return true; - } - void buffer_accessor::unbind(GLuint id)const{ - if(!m_bound) - return; - glBindBuffer(m_bound->get_buffer_id(), id); - buffer_accessor_iface::i_unbind(); - } - - bool frame_buffer_accessor::bind(reference b)const{ - if(!buffer_accessor_iface::i_bind(b)) - return false; - glBindFramebuffer(b.get_buffer_id(), m_raw_id); - return true; - } - bool frame_buffer_accessor::bind_lock(reference b)const{ - if(!buffer_accessor_iface::i_bind_lock(b)) - return false; - glBindFramebuffer(b.get_buffer_id(), m_raw_id); - return true; - } - void frame_buffer_accessor::unbind(GLuint id)const{ - if(!m_bound) - return; - glBindFramebuffer(m_bound->get_buffer_id(), id); - buffer_accessor_iface::i_unbind(); - } - -} diff --git a/src/graphics/mesh.cpp b/src/graphics/mesh.cpp index bfe17e5..4eca46c 100644 --- a/src/graphics/mesh.cpp +++ b/src/graphics/mesh.cpp @@ -26,20 +26,20 @@ namespace gfx{ vertex_mesh::vertex_mesh(const std::vector& verts): m_vertices(verts), - m_vbo(&verts[0], sizeof(vertex) * verts.size(), vbo::usage::STATIC_DRAW) + m_vbo(&verts[0], sizeof(vertex) * verts.size(), buffer::usage::STATIC_DRAW) { m_vao.bind_buffer(m_vbo, 0, 0, sizeof(vertex)); configure(); } vertex_mesh::vertex_mesh(std::vector&& verts): m_vertices(std::move(verts)), - m_vbo(&verts[0], sizeof(vertex) * verts.size(), vbo::usage::STATIC_DRAW) + m_vbo(&verts[0], sizeof(vertex) * verts.size(), buffer::usage::STATIC_DRAW) { m_vao.bind_buffer(m_vbo, 0, 0, sizeof(vertex)); configure(); } vertex_mesh::vertex_mesh(const vertex* verts, size_t nverts): - m_vbo(verts, sizeof(vertex) * nverts, vbo::usage::STATIC_DRAW) + m_vbo(verts, sizeof(vertex) * nverts, buffer::usage::STATIC_DRAW) { m_vertices.reserve(nverts); for(size_t i = 0;i < nverts;++i) @@ -99,7 +99,7 @@ namespace gfx{ size_t tex1_loc = shader.get_uniform_loc("texture1"); for(size_t i = 0;i < m_materials.size();++i){ m_materials[i]->bind_unit(i); - shader.set_uniform(tex1_loc + i, *m_materials[i]); + shader.get_uniform(tex1_loc + i).set(*m_materials[i]); } } diff --git a/src/graphics/resource_manager.cpp b/src/graphics/resource_manager.cpp index 6453664..8c09d6e 100644 --- a/src/graphics/resource_manager.cpp +++ b/src/graphics/resource_manager.cpp @@ -23,7 +23,7 @@ namespace gfx{ bool resource_manager::has_texture_array(const char* key)const{ return m_texture_arrays.has_value(key); } - gfx::texture_array* resource_manager::get_texture_array(const char* file){ + auto resource_manager::get_texture_array(const char* file) -> container_type::node{ return m_texture_arrays.get_value(file); } bool resource_manager::erase_texture_array(const char* key){ @@ -33,7 +33,7 @@ namespace gfx{ bool resource_manager::has_texture(const char* key)const{ return m_textures.has_value(key); } - gfx::texture* resource_manager::get_texture(const char* file){ + auto resource_manager::get_texture(const char* file) -> container_type::node{ return m_textures.get_value(file); } bool resource_manager::erase_texture(const char* key){ @@ -43,7 +43,7 @@ namespace gfx{ bool resource_manager::has_font(const char* key)const{ return m_fonts.has_value(key); } - egn::font_atlas* resource_manager::get_font(const char* file){ + auto resource_manager::get_font(const char* file) -> container_type::node{ return m_fonts.get_value(file); } bool resource_manager::erase_font(const char* key){ @@ -53,7 +53,7 @@ namespace gfx{ bool resource_manager::has_shader(const char* key)const{ return m_shaders.has_value(key); } - gfx::shader_program* resource_manager::get_shader(const char* key){ + auto resource_manager::get_shader(const char* key) -> container_type::node{ return m_shaders.get_value(key); } bool resource_manager::erase_shader(const char* key){ diff --git a/src/graphics/scoped_buffer_bind.cpp b/src/graphics/scoped_buffer_bind.cpp deleted file mode 100644 index 7a17f0b..0000000 --- a/src/graphics/scoped_buffer_bind.cpp +++ /dev/null @@ -1,26 +0,0 @@ -/** - This file is a part of our_dick - Copyright (C) 2020 rexy712 - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Affero 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 Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License - along with this program. If not, see . -*/ - -#include "graphics/scoped_buffer_bind.hpp" - -namespace gfx{ - - template class scoped_buffer_bind; - template class scoped_buffer_bind; - -} diff --git a/src/graphics/shader_program.cpp b/src/graphics/shader_program.cpp index 4773bf4..e2901e9 100644 --- a/src/graphics/shader_program.cpp +++ b/src/graphics/shader_program.cpp @@ -25,11 +25,11 @@ namespace gfx{ - shader_program::shader_program(): + shader_program::shader_program(void): m_shader_id(glCreateProgram()){} shader_program::shader_program(shader_program&& s): m_shader_id(std::exchange(s.m_shader_id, 0)){} - shader_program::~shader_program(){ + shader_program::~shader_program(void){ glDeleteProgram(m_shader_id); } @@ -38,36 +38,39 @@ namespace gfx{ return *this; } - GLuint shader_program::release(){ + GLuint shader_program::release(void){ return std::exchange(m_shader_id, 0); } void shader_program::attach_shader(const shader& s){ glAttachShader(m_shader_id, s.raw()); } - bool shader_program::link(){ + bool shader_program::link(void){ glLinkProgram(m_shader_id); return has_link_error(); } - void shader_program::use(){ + void shader_program::use(void){ glUseProgram(m_shader_id); } + GLuint shader_program::get_uniform_block_loc(const char* u)const{ + return glGetUniformBlockIndex(m_shader_id, u); + } GLint shader_program::get_uniform_loc(const char* u)const{ return glGetUniformLocation(m_shader_id, u); } - GLuint shader_program::raw()const{ + GLuint shader_program::raw(void)const{ return m_shader_id; } - bool shader_program::has_link_error()const{ + bool shader_program::has_link_error(void)const{ GLint status; glGetProgramiv(m_shader_id, GL_LINK_STATUS, &status); return (status == GL_FALSE); } - std::string shader_program::get_error()const{ + std::string shader_program::get_error(void)const{ GLint len; GLsizei retlen; glGetProgramiv(m_shader_id, GL_INFO_LOG_LENGTH, &len); @@ -81,7 +84,12 @@ namespace gfx{ return retval; } - + uniform shader_program::get_uniform(const char* name){ + return get_uniform(get_uniform_loc(name)); + } + uniform shader_program::get_uniform(int uloc){ + return uniform(m_shader_id, uloc); + } GLenum shader_program::get_uniform_type(const char* name)const{ return get_uniform_type(get_uniform_loc(name)); } @@ -93,447 +101,292 @@ namespace gfx{ return type; } - GLfloat shader_program::get_uniform_float(int uloc)const{ + uniform_block shader_program::get_uniform_block(const char* name){ + return get_uniform_block(get_uniform_block_loc(name)); + } + uniform_block shader_program::get_uniform_block(unsigned int uloc){ + return uniform_block(m_shader_id, uloc); + } + + + uniform::uniform(GLuint owner, const char* name): + m_shader_id(owner), + m_location(get_location_from_name_(name)){} + uniform::uniform(GLuint owner, GLuint location): + m_shader_id(owner), + m_location(location){} + + GLenum uniform::get_type(void)const{ + GLenum type; + GLint size; + GLchar name; + glGetActiveUniform(m_shader_id, m_location, 0, NULL, &size, &type, &name); + return type; + } + + GLfloat uniform::get_float(void)const{ GLfloat ret; - glGetUniformfv(m_shader_id, uloc, &ret); + glGetUniformfv(m_shader_id, m_location, &ret); return ret; } - math::vec2 shader_program::get_uniform_vec2(int uloc)const{ + math::vec2 uniform::get_vec2(void)const{ math::vec2 ret; - glGetUniformfv(m_shader_id, uloc, ret); + glGetUniformfv(m_shader_id, m_location, ret); return ret; } - math::vec3 shader_program::get_uniform_vec3(int uloc)const{ + math::vec3 uniform::get_vec3(void)const{ math::vec3 ret; - glGetUniformfv(m_shader_id, uloc, ret); + glGetUniformfv(m_shader_id, m_location, ret); return ret; } - math::vec4 shader_program::get_uniform_vec4(int uloc)const{ + math::vec4 uniform::get_vec4(void)const{ math::vec4 ret; - glGetUniformfv(m_shader_id, uloc, ret); + glGetUniformfv(m_shader_id, m_location, ret); return ret; } - GLdouble shader_program::get_uniform_double(int uloc)const{ + GLdouble uniform::get_double(void)const{ GLdouble ret; - glGetUniformdv(m_shader_id, uloc, &ret); + glGetUniformdv(m_shader_id, m_location, &ret); return ret; } - math::vec2 shader_program::get_uniform_dvec2(int uloc)const{ + math::vec2 uniform::get_dvec2(void)const{ math::vec2 ret; - glGetUniformdv(m_shader_id, uloc, ret); + glGetUniformdv(m_shader_id, m_location, ret); return ret; } - math::vec3 shader_program::get_uniform_dvec3(int uloc)const{ + math::vec3 uniform::get_dvec3(void)const{ math::vec3 ret; - glGetUniformdv(m_shader_id, uloc, ret); + glGetUniformdv(m_shader_id, m_location, ret); return ret; } - math::vec4 shader_program::get_uniform_dvec4(int uloc)const{ + math::vec4 uniform::get_dvec4(void)const{ math::vec4 ret; - glGetUniformdv(m_shader_id, uloc, ret); + glGetUniformdv(m_shader_id, m_location, ret); return ret; } - GLint shader_program::get_uniform_int(int uloc)const{ + GLint uniform::get_int(void)const{ GLint ret; - glGetUniformiv(m_shader_id, uloc, &ret); + glGetUniformiv(m_shader_id, m_location, &ret); return ret; } - math::vec2 shader_program::get_uniform_ivec2(int uloc)const{ + math::vec2 uniform::get_ivec2(void)const{ math::vec2 ret; - glGetUniformiv(m_shader_id, uloc, ret); + glGetUniformiv(m_shader_id, m_location, ret); return ret; } - math::vec3 shader_program::get_uniform_ivec3(int uloc)const{ + math::vec3 uniform::get_ivec3(void)const{ math::vec3 ret; - glGetUniformiv(m_shader_id, uloc, ret); + glGetUniformiv(m_shader_id, m_location, ret); return ret; } - math::vec4 shader_program::get_uniform_ivec4(int uloc)const{ + math::vec4 uniform::get_ivec4(void)const{ math::vec4 ret; - glGetUniformiv(m_shader_id, uloc, ret); + glGetUniformiv(m_shader_id, m_location, ret); return ret; } - GLuint shader_program::get_uniform_uint(int uloc)const{ + GLuint uniform::get_uint(void)const{ GLuint ret; - glGetUniformuiv(m_shader_id, uloc, &ret); + glGetUniformuiv(m_shader_id, m_location, &ret); return ret; } - math::vec2 shader_program::get_uniform_uvec2(int uloc)const{ + math::vec2 uniform::get_uvec2(void)const{ math::vec2 ret; - glGetUniformuiv(m_shader_id, uloc, ret); + glGetUniformuiv(m_shader_id, m_location, ret); return ret; } - math::vec3 shader_program::get_uniform_uvec3(int uloc)const{ + math::vec3 uniform::get_uvec3(void)const{ math::vec3 ret; - glGetUniformuiv(m_shader_id, uloc, ret); + glGetUniformuiv(m_shader_id, m_location, ret); return ret; } - math::vec4 shader_program::get_uniform_uvec4(int uloc)const{ + math::vec4 uniform::get_uvec4(void)const{ math::vec4 ret; - glGetUniformuiv(m_shader_id, uloc, ret); + glGetUniformuiv(m_shader_id, m_location, ret); return ret; } - GLboolean shader_program::get_uniform_bool(int uloc)const{ + GLboolean uniform::get_bool(void)const{ GLint ret; - glGetUniformiv(m_shader_id, uloc, &ret); + glGetUniformiv(m_shader_id, m_location, &ret); return static_cast(ret); } - math::vec2 shader_program::get_uniform_bvec2(int uloc)const{ + math::vec2 uniform::get_bvec2(void)const{ math::vec2 tmp; - glGetUniformiv(m_shader_id, uloc, tmp); + glGetUniformiv(m_shader_id, m_location, tmp); return math::vec2(static_cast(tmp[0]), static_cast(tmp[1])); } - math::vec3 shader_program::get_uniform_bvec3(int uloc)const{ + math::vec3 uniform::get_bvec3(void)const{ math::vec3 tmp; - glGetUniformiv(m_shader_id, uloc, tmp); + glGetUniformiv(m_shader_id, m_location, tmp); return math::vec3(static_cast(tmp[0]), static_cast(tmp[1]), static_cast(tmp[2])); } - math::vec4 shader_program::get_uniform_bvec4(int uloc)const{ + math::vec4 uniform::get_bvec4(void)const{ math::vec4 tmp; - glGetUniformiv(m_shader_id, uloc, tmp); + glGetUniformiv(m_shader_id, m_location, tmp); return math::vec4(static_cast(tmp[0]), static_cast(tmp[1]), static_cast(tmp[2]), static_cast(tmp[3])); } - math::mat2 shader_program::get_uniform_mat2(int uloc)const{ + math::mat2 uniform::get_mat2(void)const{ math::mat2 ret; - glGetUniformfv(m_shader_id, uloc, ret); + glGetUniformfv(m_shader_id, m_location, ret); return ret; } - math::mat3 shader_program::get_uniform_mat3(int uloc)const{ + math::mat3 uniform::get_mat3(void)const{ math::mat3 ret; - glGetUniformfv(m_shader_id, uloc, ret); + glGetUniformfv(m_shader_id, m_location, ret); return ret; } - math::mat4 shader_program::get_uniform_mat4(int uloc)const{ + math::mat4 uniform::get_mat4(void)const{ math::mat4 ret; - glGetUniformfv(m_shader_id, uloc, ret); + glGetUniformfv(m_shader_id, m_location, ret); return ret; } - math::matrix shader_program::get_uniform_mat2x3(int uloc)const{ + math::matrix uniform::get_mat2x3(void)const{ math::matrix ret; - glGetUniformfv(m_shader_id, uloc, ret); + glGetUniformfv(m_shader_id, m_location, ret); return ret; } - math::matrix shader_program::get_uniform_mat2x4(int uloc)const{ + math::matrix uniform::get_mat2x4(void)const{ math::matrix ret; - glGetUniformfv(m_shader_id, uloc, ret); + glGetUniformfv(m_shader_id, m_location, ret); return ret; } - math::matrix shader_program::get_uniform_mat3x2(int uloc)const{ + math::matrix uniform::get_mat3x2(void)const{ math::matrix ret; - glGetUniformfv(m_shader_id, uloc, ret); + glGetUniformfv(m_shader_id, m_location, ret); return ret; } - math::matrix shader_program::get_uniform_mat3x4(int uloc)const{ + math::matrix uniform::get_mat3x4(void)const{ math::matrix ret; - glGetUniformfv(m_shader_id, uloc, ret); + glGetUniformfv(m_shader_id, m_location, ret); return ret; } - math::matrix shader_program::get_uniform_mat4x2(int uloc)const{ + math::matrix uniform::get_mat4x2(void)const{ math::matrix ret; - glGetUniformfv(m_shader_id, uloc, ret); + glGetUniformfv(m_shader_id, m_location, ret); return ret; } - math::matrix shader_program::get_uniform_mat4x3(int uloc)const{ + math::matrix uniform::get_mat4x3(void)const{ math::matrix ret; - glGetUniformfv(m_shader_id, uloc, ret); + glGetUniformfv(m_shader_id, m_location, ret); return ret; } - math::mat2 shader_program::get_uniform_dmat2(int uloc)const{ + math::mat2 uniform::get_dmat2(void)const{ math::mat2 ret; - glGetUniformdv(m_shader_id, uloc, ret); + glGetUniformdv(m_shader_id, m_location, ret); return ret; } - math::mat3 shader_program::get_uniform_dmat3(int uloc)const{ + math::mat3 uniform::get_dmat3(void)const{ math::mat3 ret; - glGetUniformdv(m_shader_id, uloc, ret); + glGetUniformdv(m_shader_id, m_location, ret); return ret; } - math::mat4 shader_program::get_uniform_dmat4(int uloc)const{ + math::mat4 uniform::get_dmat4(void)const{ math::mat4 ret; - glGetUniformdv(m_shader_id, uloc, ret); + glGetUniformdv(m_shader_id, m_location, ret); return ret; } - math::matrix shader_program::get_uniform_dmat2x3(int uloc)const{ + math::matrix uniform::get_dmat2x3(void)const{ math::matrix ret; - glGetUniformdv(m_shader_id, uloc, ret); + glGetUniformdv(m_shader_id, m_location, ret); return ret; } - math::matrix shader_program::get_uniform_dmat2x4(int uloc)const{ + math::matrix uniform::get_dmat2x4(void)const{ math::matrix ret; - glGetUniformdv(m_shader_id, uloc, ret); + glGetUniformdv(m_shader_id, m_location, ret); return ret; } - math::matrix shader_program::get_uniform_dmat3x2(int uloc)const{ + math::matrix uniform::get_dmat3x2(void)const{ math::matrix ret; - glGetUniformdv(m_shader_id, uloc, ret); + glGetUniformdv(m_shader_id, m_location, ret); return ret; } - math::matrix shader_program::get_uniform_dmat3x4(int uloc)const{ + math::matrix uniform::get_dmat3x4(void)const{ math::matrix ret; - glGetUniformdv(m_shader_id, uloc, ret); + glGetUniformdv(m_shader_id, m_location, ret); return ret; } - math::matrix shader_program::get_uniform_dmat4x2(int uloc)const{ + math::matrix uniform::get_dmat4x2(void)const{ math::matrix ret; - glGetUniformdv(m_shader_id, uloc, ret); + glGetUniformdv(m_shader_id, m_location, ret); return ret; } - math::matrix shader_program::get_uniform_dmat4x3(int uloc)const{ + math::matrix uniform::get_dmat4x3(void)const{ math::matrix ret; - glGetUniformdv(m_shader_id, uloc, ret); + glGetUniformdv(m_shader_id, m_location, ret); return ret; } - GLfloat shader_program::get_uniform_float(const char* name)const{ - return get_uniform_float(get_uniform_loc(name)); + void uniform::set(GLfloat f){ + glProgramUniform1f(m_shader_id, m_location, f); } - math::vec2 shader_program::get_uniform_vec2(const char* name)const{ - return get_uniform_vec2(get_uniform_loc(name)); + void uniform::set(GLfloat f1, GLfloat f2){ + glProgramUniform2f(m_shader_id, m_location, f1, f2); } - math::vec3 shader_program::get_uniform_vec3(const char* name)const{ - return get_uniform_vec3(get_uniform_loc(name)); + void uniform::set(GLfloat f1, GLfloat f2, GLfloat f3){ + glProgramUniform3f(m_shader_id, m_location, f1, f2, f3); } - math::vec4 shader_program::get_uniform_vec4(const char* name)const{ - return get_uniform_vec4(get_uniform_loc(name)); + void uniform::set(GLfloat f1, GLfloat f2, GLfloat f3, GLfloat f4){ + glProgramUniform4f(m_shader_id, m_location, f1, f2, f3, f4); } - GLdouble shader_program::get_uniform_double(const char* name)const{ - return get_uniform_double(get_uniform_loc(name)); + void uniform::set(GLint i){ + glProgramUniform1i(m_shader_id, m_location, i); } - math::vec2 shader_program::get_uniform_dvec2(const char* name)const{ - return get_uniform_dvec2(get_uniform_loc(name)); + void uniform::set(GLint i1, GLint i2){ + glProgramUniform2i(m_shader_id, m_location, i1, i2); } - math::vec3 shader_program::get_uniform_dvec3(const char* name)const{ - return get_uniform_dvec3(get_uniform_loc(name)); + void uniform::set(GLint i1, GLint i2, GLint i3){ + glProgramUniform3i(m_shader_id, m_location, i1, i2, i3); } - math::vec4 shader_program::get_uniform_dvec4(const char* name)const{ - return get_uniform_dvec4(get_uniform_loc(name)); + void uniform::set(GLint i1, GLint i2, GLint i3, GLint i4){ + glProgramUniform4i(m_shader_id, m_location, i1, i2, i3, i4); } - GLint shader_program::get_uniform_int(const char* name)const{ - return get_uniform_int(get_uniform_loc(name)); + void uniform::set(GLuint i){ + glProgramUniform1ui(m_shader_id, m_location, i); } - math::vec2 shader_program::get_uniform_ivec2(const char* name)const{ - return get_uniform_ivec2(get_uniform_loc(name)); + void uniform::set(GLuint i1, GLuint i2){ + glProgramUniform2ui(m_shader_id, m_location, i1, i2); } - math::vec3 shader_program::get_uniform_ivec3(const char* name)const{ - return get_uniform_ivec3(get_uniform_loc(name)); + void uniform::set(GLuint i1, GLuint i2, GLuint i3){ + glProgramUniform3ui(m_shader_id, m_location, i1, i2, i3); } - math::vec4 shader_program::get_uniform_ivec4(const char* name)const{ - return get_uniform_ivec4(get_uniform_loc(name)); + void uniform::set(GLuint i1, GLuint i2, GLuint i3, GLuint i4){ + glProgramUniform4ui(m_shader_id, m_location, i1, i2, i3, i4); } - GLuint shader_program::get_uniform_uint(const char* name)const{ - return get_uniform_uint(get_uniform_loc(name)); - } - math::vec2 shader_program::get_uniform_uvec2(const char* name)const{ - return get_uniform_uvec2(get_uniform_loc(name)); - } - math::vec3 shader_program::get_uniform_uvec3(const char* name)const{ - return get_uniform_uvec3(get_uniform_loc(name)); - } - math::vec4 shader_program::get_uniform_uvec4(const char* name)const{ - return get_uniform_uvec4(get_uniform_loc(name)); - } - - GLboolean shader_program::get_uniform_bool(const char* name)const{ - return get_uniform_bool(get_uniform_loc(name)); - } - math::vec2 shader_program::get_uniform_bvec2(const char* name)const{ - return get_uniform_bvec2(get_uniform_loc(name)); - } - math::vec3 shader_program::get_uniform_bvec3(const char* name)const{ - return get_uniform_bvec3(get_uniform_loc(name)); - } - math::vec4 shader_program::get_uniform_bvec4(const char* name)const{ - return get_uniform_bvec4(get_uniform_loc(name)); - } - - math::mat2 shader_program::get_uniform_mat2(const char* name)const{ - return get_uniform_mat2(get_uniform_loc(name)); - } - math::mat3 shader_program::get_uniform_mat3(const char* name)const{ - return get_uniform_mat3(get_uniform_loc(name)); - } - math::mat4 shader_program::get_uniform_mat4(const char* name)const{ - return get_uniform_mat4(get_uniform_loc(name)); - } - math::matrix shader_program::get_uniform_mat2x3(const char* name)const{ - return get_uniform_mat2x3(get_uniform_loc(name)); - } - math::matrix shader_program::get_uniform_mat2x4(const char* name)const{ - return get_uniform_mat2x4(get_uniform_loc(name)); - } - math::matrix shader_program::get_uniform_mat3x2(const char* name)const{ - return get_uniform_mat3x2(get_uniform_loc(name)); - } - math::matrix shader_program::get_uniform_mat3x4(const char* name)const{ - return get_uniform_mat3x4(get_uniform_loc(name)); - } - math::matrix shader_program::get_uniform_mat4x2(const char* name)const{ - return get_uniform_mat4x2(get_uniform_loc(name)); - } - math::matrix shader_program::get_uniform_mat4x3(const char* name)const{ - return get_uniform_mat4x3(get_uniform_loc(name)); - } - - math::mat2 shader_program::get_uniform_dmat2(const char* name)const{ - return get_uniform_dmat2(get_uniform_loc(name)); - } - math::mat3 shader_program::get_uniform_dmat3(const char* name)const{ - return get_uniform_dmat3(get_uniform_loc(name)); - } - math::mat4 shader_program::get_uniform_dmat4(const char* name)const{ - return get_uniform_dmat4(get_uniform_loc(name)); - } - math::matrix shader_program::get_uniform_dmat2x3(const char* name)const{ - return get_uniform_dmat2x3(get_uniform_loc(name)); - } - math::matrix shader_program::get_uniform_dmat2x4(const char* name)const{ - return get_uniform_dmat2x4(get_uniform_loc(name)); - } - math::matrix shader_program::get_uniform_dmat3x2(const char* name)const{ - return get_uniform_dmat3x2(get_uniform_loc(name)); - } - math::matrix shader_program::get_uniform_dmat3x4(const char* name)const{ - return get_uniform_dmat3x4(get_uniform_loc(name)); - } - math::matrix shader_program::get_uniform_dmat4x2(const char* name)const{ - return get_uniform_dmat4x2(get_uniform_loc(name)); - } - math::matrix shader_program::get_uniform_dmat4x3(const char* name)const{ - return get_uniform_dmat4x3(get_uniform_loc(name)); - } - - void shader_program::set_uniform(const char* name, GLfloat f){ - set_uniform(get_uniform_loc(name), f); - } - void shader_program::set_uniform(const char* name, GLfloat f1, GLfloat f2){ - set_uniform(get_uniform_loc(name), f1, f2); - } - void shader_program::set_uniform(const char* name, GLfloat f1, GLfloat f2, GLfloat f3){ - set_uniform(get_uniform_loc(name), f1, f2, f3); - } - void shader_program::set_uniform(const char* name, GLfloat f1, GLfloat f2, GLfloat f3, GLfloat f4){ - set_uniform(get_uniform_loc(name), f1, f2, f3, f4); - } - void shader_program::set_uniform(GLuint loc, GLfloat f){ - glProgramUniform1f(m_shader_id, loc, f); - } - void shader_program::set_uniform(GLuint loc, GLfloat f1, GLfloat f2){ - glProgramUniform2f(m_shader_id, loc, f1, f2); - } - void shader_program::set_uniform(GLuint loc, GLfloat f1, GLfloat f2, GLfloat f3){ - glProgramUniform3f(m_shader_id, loc, f1, f2, f3); - } - void shader_program::set_uniform(GLuint loc, GLfloat f1, GLfloat f2, GLfloat f3, GLfloat f4){ - glProgramUniform4f(m_shader_id, loc, f1, f2, f3, f4); - } - - void shader_program::set_uniform(const char* name, GLint i){ - set_uniform(get_uniform_loc(name), i); - } - void shader_program::set_uniform(const char* name, GLint i1, GLint i2){ - set_uniform(get_uniform_loc(name), i1, i2); - } - void shader_program::set_uniform(const char* name, GLint i1, GLint i2, GLint i3){ - set_uniform(get_uniform_loc(name), i1, i2, i3); - } - void shader_program::set_uniform(const char* name, GLint i1, GLint i2, GLint i3, GLint i4){ - set_uniform(get_uniform_loc(name), i1, i2, i3, i4); - } - void shader_program::set_uniform(GLuint loc, GLint i){ - glProgramUniform1i(m_shader_id, loc, i); - } - void shader_program::set_uniform(GLuint loc, GLint i1, GLint i2){ - glProgramUniform2i(m_shader_id, loc, i1, i2); - } - void shader_program::set_uniform(GLuint loc, GLint i1, GLint i2, GLint i3){ - glProgramUniform3i(m_shader_id, loc, i1, i2, i3); - } - void shader_program::set_uniform(GLuint loc, GLint i1, GLint i2, GLint i3, GLint i4){ - glProgramUniform4i(m_shader_id, loc, i1, i2, i3, i4); - } - - void shader_program::set_uniform(const char* name, GLuint i){ - set_uniform(get_uniform_loc(name), i); - } - void shader_program::set_uniform(const char* name, GLuint i1, GLuint i2){ - set_uniform(get_uniform_loc(name), i1, i2); - } - void shader_program::set_uniform(const char* name, GLuint i1, GLuint i2, GLuint i3){ - set_uniform(get_uniform_loc(name), i1, i2, i3); - } - void shader_program::set_uniform(const char* name, GLuint i1, GLuint i2, GLuint i3, GLuint i4){ - set_uniform(get_uniform_loc(name), i1, i2, i3, i4); - } - void shader_program::set_uniform(GLuint loc, GLuint i){ - glProgramUniform1ui(m_shader_id, loc, i); - } - void shader_program::set_uniform(GLuint loc, GLuint i1, GLuint i2){ - glProgramUniform2ui(m_shader_id, loc, i1, i2); - } - void shader_program::set_uniform(GLuint loc, GLuint i1, GLuint i2, GLuint i3){ - glProgramUniform3ui(m_shader_id, loc, i1, i2, i3); - } - void shader_program::set_uniform(GLuint loc, GLuint i1, GLuint i2, GLuint i3, GLuint i4){ - glProgramUniform4ui(m_shader_id, loc, i1, i2, i3, i4); - } - - void shader_program::set_uniform(const char* name, const texture& t, GLuint tex_unit){ - set_uniform(get_uniform_loc(name), t, tex_unit); - } - void shader_program::set_uniform(GLuint loc, const texture& t, GLuint tex_unit){ + void uniform::set(const texture& t, GLuint tex_unit){ t.bind_unit(tex_unit); - glProgramUniform1i(m_shader_id, loc, tex_unit); + glProgramUniform1i(m_shader_id, m_location, tex_unit); } - void shader_program::set_uniform(const char* name, const texture_array& t, GLuint tex_unit){ - set_uniform(get_uniform_loc(name), t, tex_unit); - } - void shader_program::set_uniform(GLuint loc, const texture_array& t, GLuint tex_unit){ + void uniform::set(const texture_array& t, GLuint tex_unit){ t.bind_unit(tex_unit); - glProgramUniform1i(m_shader_id, loc, tex_unit); + glProgramUniform1i(m_shader_id, m_location, tex_unit); } - void shader_program::set_uniform(const char* name, const weak_texture& t, GLuint tex_unit){ - set_uniform(get_uniform_loc(name), t, tex_unit); - } - void shader_program::set_uniform(GLuint loc, const weak_texture& t, GLuint tex_unit){ + void uniform::set(const weak_texture& t, GLuint tex_unit){ t.bind_unit(tex_unit); - glProgramUniform1i(m_shader_id, loc, tex_unit); + glProgramUniform1i(m_shader_id, m_location, tex_unit); } - void shader_program::set_uniform(const char* name, const vec2& v){ - set_uniform(get_uniform_loc(name), v); + void uniform::set(const vec2& v){ + glProgramUniform2fv(m_shader_id, m_location, 1, v); } - void shader_program::set_uniform(const char* name, const vec3& v){ - set_uniform(get_uniform_loc(name), v); + void uniform::set(const vec3& v){ + glProgramUniform3fv(m_shader_id, m_location, 1, v); } - void shader_program::set_uniform(const char* name, const vec4& v){ - set_uniform(get_uniform_loc(name), v); - } - void shader_program::set_uniform(GLuint loc, const vec2& v){ - glProgramUniform2fv(m_shader_id, loc, 1, v); - } - void shader_program::set_uniform(GLuint loc, const vec3& v){ - glProgramUniform3fv(m_shader_id, loc, 1, v); - } - void shader_program::set_uniform(GLuint loc, const vec4& v){ - glProgramUniform4fv(m_shader_id, loc, 1, v); + void uniform::set(const vec4& v){ + glProgramUniform4fv(m_shader_id, m_location, 1, v); } - void shader_program::set_uniform(const char* name, GLsizei count, const vec2* v_arr){ - set_uniform(get_uniform_loc(name), count, v_arr); - } - void shader_program::set_uniform(GLuint loc, GLsizei count, const vec2* v_arr){ + void uniform::set(GLsizei count, const vec2* v_arr){ if constexpr(sizeof(v_arr[0]) == sizeof(GLfloat[2])){ - glProgramUniform2fv(m_shader_id, loc, count, reinterpret_cast(v_arr)); + glProgramUniform2fv(m_shader_id, m_location, count, reinterpret_cast(v_arr)); }else{ debug_print_warn("Using inefficient uniform initialization\n"); std::unique_ptr arr(new GLfloat[count * 2]); @@ -541,15 +394,12 @@ namespace gfx{ arr[i] = v_arr[i].get(0); arr[i + 1] = v_arr[i].get(1); } - glProgramUniform2fv(m_shader_id, loc, count, arr.get()); + glProgramUniform2fv(m_shader_id, m_location, count, arr.get()); } } - void shader_program::set_uniform(const char* name, GLsizei count, const vec3* v_arr){ - set_uniform(get_uniform_loc(name), count, v_arr); - } - void shader_program::set_uniform(GLuint loc, GLsizei count, const vec3* v_arr){ + void uniform::set(GLsizei count, const vec3* v_arr){ if constexpr(sizeof(v_arr[0]) == sizeof(GLfloat[3])){ - glProgramUniform3fv(m_shader_id, loc, count, reinterpret_cast(v_arr)); + glProgramUniform3fv(m_shader_id, m_location, count, reinterpret_cast(v_arr)); }else{ debug_print_warn("Using inefficient uniform initialization\n"); std::unique_ptr arr(new GLfloat[count * 3]); @@ -558,15 +408,12 @@ namespace gfx{ arr[i + 1] = v_arr[i].get(1); arr[i + 2] = v_arr[i].get(2); } - glProgramUniform3fv(m_shader_id, loc, count, arr.get()); + glProgramUniform3fv(m_shader_id, m_location, count, arr.get()); } } - void shader_program::set_uniform(const char* name, GLsizei count, const vec4* v_arr){ - set_uniform(get_uniform_loc(name), count, v_arr); - } - void shader_program::set_uniform(GLuint loc, GLsizei count, const vec4* v_arr){ + void uniform::set(GLsizei count, const vec4* v_arr){ if constexpr(sizeof(v_arr[0]) == sizeof(GLfloat[4])){ - glProgramUniform4fv(m_shader_id, loc, count, reinterpret_cast(v_arr)); + glProgramUniform4fv(m_shader_id, m_location, count, reinterpret_cast(v_arr)); }else{ debug_print_warn("Using inefficient uniform initialization\n"); std::unique_ptr arr(new GLfloat[count * 4]); @@ -576,60 +423,36 @@ namespace gfx{ arr[i + 2] = v_arr[i].get(2); arr[i + 3] = v_arr[i].get(3); } - glProgramUniform4fv(m_shader_id, loc, count, arr.get()); + glProgramUniform4fv(m_shader_id, m_location, count, arr.get()); } } - void shader_program::set_uniform_1(const char* name, GLsizei count, const GLfloat* v_arr){ - set_uniform_1(get_uniform_loc(name), count, v_arr); + void uniform::set_1(GLsizei count, const GLfloat* v_arr){ + glProgramUniform1fv(m_shader_id, m_location, count, v_arr); } - void shader_program::set_uniform_2(const char* name, GLsizei count, const GLfloat* v_arr){ - set_uniform_2(get_uniform_loc(name), count, v_arr); + void uniform::set_2(GLsizei count, const GLfloat* v_arr){ + glProgramUniform2fv(m_shader_id, m_location, count, v_arr); } - void shader_program::set_uniform_3(const char* name, GLsizei count, const GLfloat* v_arr){ - set_uniform_3(get_uniform_loc(name), count, v_arr); + void uniform::set_3(GLsizei count, const GLfloat* v_arr){ + glProgramUniform3fv(m_shader_id, m_location, count, v_arr); } - void shader_program::set_uniform_4(const char* name, GLsizei count, const GLfloat* v_arr){ - set_uniform_4(get_uniform_loc(name), count, v_arr); - } - void shader_program::set_uniform_1(GLuint loc, GLsizei count, const GLfloat* v_arr){ - glProgramUniform1fv(m_shader_id, loc, count, v_arr); - } - void shader_program::set_uniform_2(GLuint loc, GLsizei count, const GLfloat* v_arr){ - glProgramUniform2fv(m_shader_id, loc, count, v_arr); - } - void shader_program::set_uniform_3(GLuint loc, GLsizei count, const GLfloat* v_arr){ - glProgramUniform3fv(m_shader_id, loc, count, v_arr); - } - void shader_program::set_uniform_4(GLuint loc, GLsizei count, const GLfloat* v_arr){ - glProgramUniform4fv(m_shader_id, loc, count, v_arr); + void uniform::set_4(GLsizei count, const GLfloat* v_arr){ + glProgramUniform4fv(m_shader_id, m_location, count, v_arr); } - void shader_program::set_uniform(const char* name, const vec2& v){ - set_uniform(get_uniform_loc(name), v); + void uniform::set(const vec2& v){ + glProgramUniform2iv(m_shader_id, m_location, 1, v); } - void shader_program::set_uniform(const char* name, const vec3& v){ - set_uniform(get_uniform_loc(name), v); + void uniform::set(const vec3& v){ + glProgramUniform3iv(m_shader_id, m_location, 1, v); } - void shader_program::set_uniform(const char* name, const vec4& v){ - set_uniform(get_uniform_loc(name), v); - } - void shader_program::set_uniform(GLuint loc, const vec2& v){ - glProgramUniform2iv(m_shader_id, loc, 1, v); - } - void shader_program::set_uniform(GLuint loc, const vec3& v){ - glProgramUniform3iv(m_shader_id, loc, 1, v); - } - void shader_program::set_uniform(GLuint loc, const vec4& v){ - glProgramUniform4iv(m_shader_id, loc, 1, v); + void uniform::set(const vec4& v){ + glProgramUniform4iv(m_shader_id, m_location, 1, v); } - void shader_program::set_uniform(const char* name, GLsizei count, const vec2* v_arr){ - set_uniform(get_uniform_loc(name), count, v_arr); - } - void shader_program::set_uniform(GLuint loc, GLsizei count, const vec2* v_arr){ + void uniform::set(GLsizei count, const vec2* v_arr){ if constexpr(sizeof(v_arr[0]) == sizeof(GLint[2])){ - glProgramUniform2iv(m_shader_id, loc, count, reinterpret_cast(v_arr)); + glProgramUniform2iv(m_shader_id, m_location, count, reinterpret_cast(v_arr)); }else{ debug_print_warn("Using inefficient uniform initialization\n"); std::unique_ptr arr(new GLint[count * 2]); @@ -637,15 +460,12 @@ namespace gfx{ arr[i] = v_arr[i].get(0); arr[i + 1] = v_arr[i].get(1); } - glProgramUniform2iv(m_shader_id, loc, count, arr.get()); + glProgramUniform2iv(m_shader_id, m_location, count, arr.get()); } } - void shader_program::set_uniform(const char* name, GLsizei count, const vec3* v_arr){ - set_uniform(get_uniform_loc(name), count, v_arr); - } - void shader_program::set_uniform(GLuint loc, GLsizei count, const vec3* v_arr){ + void uniform::set(GLsizei count, const vec3* v_arr){ if constexpr(sizeof(v_arr[0]) == sizeof(GLint[3])){ - glProgramUniform3iv(m_shader_id, loc, count, reinterpret_cast(v_arr)); + glProgramUniform3iv(m_shader_id, m_location, count, reinterpret_cast(v_arr)); }else{ debug_print_warn("Using inefficient uniform initialization\n"); std::unique_ptr arr(new GLint[count * 3]); @@ -654,15 +474,12 @@ namespace gfx{ arr[i + 1] = v_arr[i].get(1); arr[i + 2] = v_arr[i].get(2); } - glProgramUniform3iv(m_shader_id, loc, count, arr.get()); + glProgramUniform3iv(m_shader_id, m_location, count, arr.get()); } } - void shader_program::set_uniform(const char* name, GLsizei count, const vec4* v_arr){ - set_uniform(get_uniform_loc(name), count, v_arr); - } - void shader_program::set_uniform(GLuint loc, GLsizei count, const vec4* v_arr){ + void uniform::set(GLsizei count, const vec4* v_arr){ if constexpr(sizeof(v_arr[0]) == sizeof(GLint[4])){ - glProgramUniform4iv(m_shader_id, loc, count, reinterpret_cast(v_arr)); + glProgramUniform4iv(m_shader_id, m_location, count, reinterpret_cast(v_arr)); }else{ debug_print_warn("Using inefficient uniform initialization\n"); std::unique_ptr arr(new GLint[count * 4]); @@ -672,60 +489,36 @@ namespace gfx{ arr[i + 2] = v_arr[i].get(2); arr[i + 3] = v_arr[i].get(3); } - glProgramUniform4iv(m_shader_id, loc, count, arr.get()); + glProgramUniform4iv(m_shader_id, m_location, count, arr.get()); } } - void shader_program::set_uniform_1(const char* name, GLsizei count, const GLint* v_arr){ - set_uniform_1(get_uniform_loc(name), count, v_arr); + void uniform::set_1(GLsizei count, const GLint* v_arr){ + glProgramUniform1iv(m_shader_id, m_location, count, v_arr); } - void shader_program::set_uniform_2(const char* name, GLsizei count, const GLint* v_arr){ - set_uniform_2(get_uniform_loc(name), count, v_arr); + void uniform::set_2(GLsizei count, const GLint* v_arr){ + glProgramUniform2iv(m_shader_id, m_location, count, v_arr); } - void shader_program::set_uniform_3(const char* name, GLsizei count, const GLint* v_arr){ - set_uniform_3(get_uniform_loc(name), count, v_arr); + void uniform::set_3(GLsizei count, const GLint* v_arr){ + glProgramUniform3iv(m_shader_id, m_location, count, v_arr); } - void shader_program::set_uniform_4(const char* name, GLsizei count, const GLint* v_arr){ - set_uniform_4(get_uniform_loc(name), count, v_arr); - } - void shader_program::set_uniform_1(GLuint loc, GLsizei count, const GLint* v_arr){ - glProgramUniform1iv(m_shader_id, loc, count, v_arr); - } - void shader_program::set_uniform_2(GLuint loc, GLsizei count, const GLint* v_arr){ - glProgramUniform2iv(m_shader_id, loc, count, v_arr); - } - void shader_program::set_uniform_3(GLuint loc, GLsizei count, const GLint* v_arr){ - glProgramUniform3iv(m_shader_id, loc, count, v_arr); - } - void shader_program::set_uniform_4(GLuint loc, GLsizei count, const GLint* v_arr){ - glProgramUniform4iv(m_shader_id, loc, count, v_arr); + void uniform::set_4(GLsizei count, const GLint* v_arr){ + glProgramUniform4iv(m_shader_id, m_location, count, v_arr); } - void shader_program::set_uniform(const char* name, const vec2& v){ - set_uniform(get_uniform_loc(name), v); + void uniform::set(const vec2& v){ + glProgramUniform2uiv(m_shader_id, m_location, 1, v); } - void shader_program::set_uniform(const char* name, const vec3& v){ - set_uniform(get_uniform_loc(name), v); + void uniform::set(const vec3& v){ + glProgramUniform3uiv(m_shader_id, m_location, 1, v); } - void shader_program::set_uniform(const char* name, const vec4& v){ - set_uniform(get_uniform_loc(name), v); - } - void shader_program::set_uniform(GLuint loc, const vec2& v){ - glProgramUniform2uiv(m_shader_id, loc, 1, v); - } - void shader_program::set_uniform(GLuint loc, const vec3& v){ - glProgramUniform3uiv(m_shader_id, loc, 1, v); - } - void shader_program::set_uniform(GLuint loc, const vec4& v){ - glProgramUniform4uiv(m_shader_id, loc, 1, v); + void uniform::set(const vec4& v){ + glProgramUniform4uiv(m_shader_id, m_location, 1, v); } - void shader_program::set_uniform(const char* name, GLsizei count, const vec2* v_arr){ - set_uniform(get_uniform_loc(name), count, v_arr); - } - void shader_program::set_uniform(GLuint loc, GLsizei count, const vec2* v_arr){ + void uniform::set(GLsizei count, const vec2* v_arr){ if constexpr(sizeof(v_arr[0]) == sizeof(GLuint[2])){ - glProgramUniform2uiv(m_shader_id, loc, count, reinterpret_cast(v_arr)); + glProgramUniform2uiv(m_shader_id, m_location, count, reinterpret_cast(v_arr)); }else{ debug_print_warn("Using inefficient uniform initialization\n"); std::unique_ptr arr(new GLuint[count * 2]); @@ -733,15 +526,12 @@ namespace gfx{ arr[i] = v_arr[i].get(0); arr[i + 1] = v_arr[i].get(1); } - glProgramUniform2uiv(m_shader_id, loc, count, arr.get()); + glProgramUniform2uiv(m_shader_id, m_location, count, arr.get()); } } - void shader_program::set_uniform(const char* name, GLsizei count, const vec3* v_arr){ - set_uniform(get_uniform_loc(name), count, v_arr); - } - void shader_program::set_uniform(GLuint loc, GLsizei count, const vec3* v_arr){ + void uniform::set(GLsizei count, const vec3* v_arr){ if constexpr(sizeof(v_arr[0]) == sizeof(GLuint[3])){ - glProgramUniform3uiv(m_shader_id, loc, count, reinterpret_cast(v_arr)); + glProgramUniform3uiv(m_shader_id, m_location, count, reinterpret_cast(v_arr)); }else{ debug_print_warn("Using inefficient uniform initialization\n"); std::unique_ptr arr(new GLuint[count * 3]); @@ -750,15 +540,12 @@ namespace gfx{ arr[i + 1] = v_arr[i].get(1); arr[i + 2] = v_arr[i].get(2); } - glProgramUniform3uiv(m_shader_id, loc, count, arr.get()); + glProgramUniform3uiv(m_shader_id, m_location, count, arr.get()); } } - void shader_program::set_uniform(const char* name, GLsizei count, const vec4* v_arr){ - set_uniform(get_uniform_loc(name), count, v_arr); - } - void shader_program::set_uniform(GLuint loc, GLsizei count, const vec4* v_arr){ + void uniform::set(GLsizei count, const vec4* v_arr){ if constexpr(sizeof(v_arr[0]) == sizeof(GLuint[4])){ - glProgramUniform4uiv(m_shader_id, loc, count, reinterpret_cast(v_arr)); + glProgramUniform4uiv(m_shader_id, m_location, count, reinterpret_cast(v_arr)); }else{ debug_print_warn("Using inefficient uniform initialization\n"); std::unique_ptr arr(new GLuint[count * 4]); @@ -768,60 +555,36 @@ namespace gfx{ arr[i + 2] = v_arr[i].get(2); arr[i + 3] = v_arr[i].get(3); } - glProgramUniform4uiv(m_shader_id, loc, count, arr.get()); + glProgramUniform4uiv(m_shader_id, m_location, count, arr.get()); } } - void shader_program::set_uniform_1(const char* name, GLsizei count, const GLuint* v_arr){ - set_uniform_1(get_uniform_loc(name), count, v_arr); + void uniform::set_1(GLsizei count, const GLuint* v_arr){ + glProgramUniform1uiv(m_shader_id, m_location, count, v_arr); } - void shader_program::set_uniform_2(const char* name, GLsizei count, const GLuint* v_arr){ - set_uniform_2(get_uniform_loc(name), count, v_arr); + void uniform::set_2(GLsizei count, const GLuint* v_arr){ + glProgramUniform2uiv(m_shader_id, m_location, count, v_arr); } - void shader_program::set_uniform_3(const char* name, GLsizei count, const GLuint* v_arr){ - set_uniform_3(get_uniform_loc(name), count, v_arr); + void uniform::set_3(GLsizei count, const GLuint* v_arr){ + glProgramUniform3uiv(m_shader_id, m_location, count, v_arr); } - void shader_program::set_uniform_4(const char* name, GLsizei count, const GLuint* v_arr){ - set_uniform_4(get_uniform_loc(name), count, v_arr); - } - void shader_program::set_uniform_1(GLuint loc, GLsizei count, const GLuint* v_arr){ - glProgramUniform1uiv(m_shader_id, loc, count, v_arr); - } - void shader_program::set_uniform_2(GLuint loc, GLsizei count, const GLuint* v_arr){ - glProgramUniform2uiv(m_shader_id, loc, count, v_arr); - } - void shader_program::set_uniform_3(GLuint loc, GLsizei count, const GLuint* v_arr){ - glProgramUniform3uiv(m_shader_id, loc, count, v_arr); - } - void shader_program::set_uniform_4(GLuint loc, GLsizei count, const GLuint* v_arr){ - glProgramUniform4uiv(m_shader_id, loc, count, v_arr); + void uniform::set_4(GLsizei count, const GLuint* v_arr){ + glProgramUniform4uiv(m_shader_id, m_location, count, v_arr); } - void shader_program::set_uniform(const char* name, const mat2& m){ - set_uniform(get_uniform_loc(name), m); + void uniform::set(const mat2& m){ + glProgramUniformMatrix2fv(m_shader_id, m_location, 1, GL_FALSE, m); } - void shader_program::set_uniform(const char* name, const mat3& m){ - set_uniform(get_uniform_loc(name), m); + void uniform::set(const mat3& m){ + glProgramUniformMatrix3fv(m_shader_id, m_location, 1, GL_FALSE, m); } - void shader_program::set_uniform(const char* name, const mat4& m){ - set_uniform(get_uniform_loc(name), m); - } - void shader_program::set_uniform(GLuint loc, const mat2& m){ - glProgramUniformMatrix2fv(m_shader_id, loc, 1, GL_FALSE, m); - } - void shader_program::set_uniform(GLuint loc, const mat3& m){ - glProgramUniformMatrix3fv(m_shader_id, loc, 1, GL_FALSE, m); - } - void shader_program::set_uniform(GLuint loc, const mat4& m){ - glProgramUniformMatrix4fv(m_shader_id, loc, 1, GL_FALSE, m); + void uniform::set(const mat4& m){ + glProgramUniformMatrix4fv(m_shader_id, m_location, 1, GL_FALSE, m); } - void shader_program::set_uniform(const char* name, GLsizei count, const mat2* m_arr){ - set_uniform(get_uniform_loc(name), count, m_arr); - } - void shader_program::set_uniform(GLuint loc, GLsizei count, const mat2* m_arr){ + void uniform::set(GLsizei count, const mat2* m_arr){ if constexpr(sizeof(m_arr[0]) == sizeof(GLfloat[4])){ - glProgramUniformMatrix2fv(m_shader_id, loc, count, GL_FALSE, reinterpret_cast(m_arr)); + glProgramUniformMatrix2fv(m_shader_id, m_location, count, GL_FALSE, reinterpret_cast(m_arr)); }else{ debug_print_warn("Using inefficient uniform initialization\n"); std::unique_ptr arr(new GLfloat[count * 4]); @@ -831,15 +594,12 @@ namespace gfx{ arr[i + 2] = m_arr[i].get(2); arr[i + 3] = m_arr[i].get(3); } - glProgramUniformMatrix2fv(m_shader_id, loc, count, GL_FALSE, arr.get()); + glProgramUniformMatrix2fv(m_shader_id, m_location, count, GL_FALSE, arr.get()); } } - void shader_program::set_uniform(const char* name, GLsizei count, const mat3* m_arr){ - set_uniform(get_uniform_loc(name), count, m_arr); - } - void shader_program::set_uniform(GLuint loc, GLsizei count, const mat3* m_arr){ + void uniform::set(GLsizei count, const mat3* m_arr){ if constexpr(sizeof(m_arr[0]) == sizeof(GLfloat[9])){ - glProgramUniformMatrix3fv(m_shader_id, loc, count, GL_FALSE, reinterpret_cast(m_arr)); + glProgramUniformMatrix3fv(m_shader_id, m_location, count, GL_FALSE, reinterpret_cast(m_arr)); }else{ debug_print_warn("Using inefficient uniform initialization\n"); std::unique_ptr arr(new GLfloat[count * 9]); @@ -854,15 +614,12 @@ namespace gfx{ arr[i + 7] = m_arr[i].get(7); arr[i + 8] = m_arr[i].get(8); } - glProgramUniformMatrix3fv(m_shader_id, loc, count, GL_FALSE, arr.get()); + glProgramUniformMatrix3fv(m_shader_id, m_location, count, GL_FALSE, arr.get()); } } - void shader_program::set_uniform(const char* name, GLsizei count, const mat4* m_arr){ - set_uniform(get_uniform_loc(name), count, m_arr); - } - void shader_program::set_uniform(GLuint loc, GLsizei count, const mat4* m_arr){ + void uniform::set(GLsizei count, const mat4* m_arr){ if constexpr(sizeof(m_arr[0]) == sizeof(GLfloat[16])){ - glProgramUniformMatrix4fv(m_shader_id, loc, count, GL_FALSE, reinterpret_cast(m_arr)); + glProgramUniformMatrix4fv(m_shader_id, m_location, count, GL_FALSE, reinterpret_cast(m_arr)); }else{ debug_print_warn("Using inefficient uniform initialization\n"); std::unique_ptr arr(new GLfloat[count * 16]); @@ -884,62 +641,56 @@ namespace gfx{ arr[i + 14] = m_arr[i].get(14); arr[i + 15] = m_arr[i].get(15); } - glProgramUniformMatrix4fv(m_shader_id, loc, count, GL_FALSE, arr.get()); + glProgramUniformMatrix4fv(m_shader_id, m_location, count, GL_FALSE, arr.get()); } } - void shader_program::set_uniform_mat2(const char* name, GLsizei count, const GLfloat* m_arr){ - set_uniform_mat2(get_uniform_loc(name), count, m_arr); + void uniform::set_mat2(GLsizei count, const GLfloat* m_arr){ + glProgramUniformMatrix2fv(m_shader_id, m_location, count, GL_FALSE, m_arr); } - void shader_program::set_uniform_mat3(const char* name, GLsizei count, const GLfloat* m_arr){ - set_uniform_mat3(get_uniform_loc(name), count, m_arr); + void uniform::set_mat3(GLsizei count, const GLfloat* m_arr){ + glProgramUniformMatrix3fv(m_shader_id, m_location, count, GL_FALSE, m_arr); } - void shader_program::set_uniform_mat4(const char* name, GLsizei count, const GLfloat* m_arr){ - set_uniform_mat4(get_uniform_loc(name), count, m_arr); + void uniform::set_mat4(GLsizei count, const GLfloat* m_arr){ + glProgramUniformMatrix4fv(m_shader_id, m_location, count, GL_FALSE, m_arr); } - void shader_program::set_uniform_mat2x3(const char* name, GLsizei count, const GLfloat* m_arr){ - set_uniform_mat2x3(get_uniform_loc(name), count, m_arr); + void uniform::set_mat2x3(GLsizei count, const GLfloat* m_arr){ + glProgramUniformMatrix2x3fv(m_shader_id, m_location, count, GL_FALSE, m_arr); } - void shader_program::set_uniform_mat3x2(const char* name, GLsizei count, const GLfloat* m_arr){ - set_uniform_mat3x2(get_uniform_loc(name), count, m_arr); + void uniform::set_mat3x2(GLsizei count, const GLfloat* m_arr){ + glProgramUniformMatrix3x2fv(m_shader_id, m_location, count, GL_FALSE, m_arr); } - void shader_program::set_uniform_mat2x4(const char* name, GLsizei count, const GLfloat* m_arr){ - set_uniform_mat2x4(get_uniform_loc(name), count, m_arr); + void uniform::set_mat2x4(GLsizei count, const GLfloat* m_arr){ + glProgramUniformMatrix2x4fv(m_shader_id, m_location, count, GL_FALSE, m_arr); } - void shader_program::set_uniform_mat4x2(const char* name, GLsizei count, const GLfloat* m_arr){ - set_uniform_mat4x2(get_uniform_loc(name), count, m_arr); + void uniform::set_mat4x2(GLsizei count, const GLfloat* m_arr){ + glProgramUniformMatrix4x2fv(m_shader_id, m_location, count, GL_FALSE, m_arr); } - void shader_program::set_uniform_mat3x4(const char* name, GLsizei count, const GLfloat* m_arr){ - set_uniform_mat3x4(get_uniform_loc(name), count, m_arr); + void uniform::set_mat3x4(GLsizei count, const GLfloat* m_arr){ + glProgramUniformMatrix3x4fv(m_shader_id, m_location, count, GL_FALSE, m_arr); } - void shader_program::set_uniform_mat4x3(const char* name, GLsizei count, const GLfloat* m_arr){ - set_uniform_mat4x3(get_uniform_loc(name), count, m_arr); + void uniform::set_mat4x3(GLsizei count, const GLfloat* m_arr){ + glProgramUniformMatrix4x3fv(m_shader_id, m_location, count, GL_FALSE, m_arr); } - void shader_program::set_uniform_mat2(GLuint loc, GLsizei count, const GLfloat* m_arr){ - glProgramUniformMatrix2fv(m_shader_id, loc, count, GL_FALSE, m_arr); + + GLint uniform::get_location_from_name_(const char* name){ + return glGetUniformLocation(m_shader_id, name); } - void shader_program::set_uniform_mat3(GLuint loc, GLsizei count, const GLfloat* m_arr){ - glProgramUniformMatrix3fv(m_shader_id, loc, count, GL_FALSE, m_arr); + + + uniform_block::uniform_block(GLuint owner, GLuint index): + m_shader_id(owner), + m_index(index){} + uniform_block::uniform_block(GLuint owner, const char* name): + m_shader_id(owner), + m_index(get_index_from_name_(name)){} + + void uniform_block::set_binding(size_t binding){ + glUniformBlockBinding(m_shader_id, m_index, binding); } - void shader_program::set_uniform_mat4(GLuint loc, GLsizei count, const GLfloat* m_arr){ - glProgramUniformMatrix4fv(m_shader_id, loc, count, GL_FALSE, m_arr); - } - void shader_program::set_uniform_mat2x3(GLuint loc, GLsizei count, const GLfloat* m_arr){ - glProgramUniformMatrix2x3fv(m_shader_id, loc, count, GL_FALSE, m_arr); - } - void shader_program::set_uniform_mat3x2(GLuint loc, GLsizei count, const GLfloat* m_arr){ - glProgramUniformMatrix3x2fv(m_shader_id, loc, count, GL_FALSE, m_arr); - } - void shader_program::set_uniform_mat2x4(GLuint loc, GLsizei count, const GLfloat* m_arr){ - glProgramUniformMatrix2x4fv(m_shader_id, loc, count, GL_FALSE, m_arr); - } - void shader_program::set_uniform_mat4x2(GLuint loc, GLsizei count, const GLfloat* m_arr){ - glProgramUniformMatrix4x2fv(m_shader_id, loc, count, GL_FALSE, m_arr); - } - void shader_program::set_uniform_mat3x4(GLuint loc, GLsizei count, const GLfloat* m_arr){ - glProgramUniformMatrix3x4fv(m_shader_id, loc, count, GL_FALSE, m_arr); - } - void shader_program::set_uniform_mat4x3(GLuint loc, GLsizei count, const GLfloat* m_arr){ - glProgramUniformMatrix4x3fv(m_shader_id, loc, count, GL_FALSE, m_arr); + + GLuint uniform_block::get_index_from_name_(const char* name){ + return glGetUniformBlockIndex(m_shader_id, name); } + } diff --git a/src/graphics/text_field_view.cpp b/src/graphics/text_field_view.cpp new file mode 100644 index 0000000..8d0c19c --- /dev/null +++ b/src/graphics/text_field_view.cpp @@ -0,0 +1,124 @@ +/** + This file is a part of our_dick + Copyright (C) 2022 rexy712 + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero 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 Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . +*/ + +#include "graphics/text_field_view.hpp" +#include "engine/text_field.hpp" + +#include //strlen + +namespace gfx{ + + text_field_view::text_field_view(egn::gui::text_field_model& m): + m_model(&m), + m_buffer(s_size_per_char * 10, buffer::usage::DYNAMIC_DRAW), + m_dirty_bit(0) + { + init_arrays_(); + } + text_field_view::text_field_view(egn::gui::text_field_model& m, const char* data): + m_model(&m), + m_buffer(s_size_per_char * strlen(data), buffer::usage::DYNAMIC_DRAW) + { + init_arrays_(); + set_text(data); + } + text_field_view::text_field_view(const text_field_view& f): + m_model(nullptr), + m_buffer(f.m_buffer), + m_dirty_bit(f.m_dirty_bit) + { + init_arrays_(); + } + + text_field_view& text_field_view::operator=(const text_field_view& f){ + return (*this = text_field_view(f)); + } + + bool text_field_view::set_text(const char* text){ + m_dirty_bit = 1; + const auto& font = m_model->font; + if(!font){ + m_buffer.initialize(0); + return false; + } + m_buffer.clear(); + constexpr float tmp_color[] = {1, 1, 1, 1}; + + math::vec2f pos_offset = {0, 0}; + + for(int i = 0;text[i] != 0;++i){ + if(text[i] == '\n'){ + pos_offset.x() = 0; + pos_offset.y() -= font->glyph_size().y(); + } + const auto ch = (*font)[text[i]]; + const math::vec2f char_size = ch.size; //convert from size_t to float + const math::vec2f char_offset = {ch.bearing.x() + pos_offset.x(), + ch.bearing.y() - ch.size.y() + pos_offset.y()}; + m_buffer.buffer(ch.texture_coords[1], sizeof(float) * 2); + m_buffer.buffer(ch.texture_coords[2], sizeof(float) * 2); + m_buffer.buffer(tmp_color, sizeof(float) * 4); + m_buffer.buffer(char_offset, sizeof(float) * 2); + m_buffer.buffer(char_size, sizeof(float) * 2); + + pos_offset.x() += ch.advance.x(); + } + m_dirty_bit = 0; + return true; + } + + void text_field_view::set_model(egn::gui::text_field_model& t){ + m_model = &t; + } + + + void text_field_view::render(egn::menu_renderer& r){ + if(m_dirty_bit){ + set_text(m_model->text.c_str()); + } + m_vao.bind(); + r.set_position({10, 10}); + r.set_font(*m_model->font); + r.draw_text(m_model->text.length()); + } + + + void text_field_view::init_arrays_(void){ + m_vao.bind_buffer(m_buffer, 0, 0, s_size_per_char); + + auto attrib = m_vao.get_attribute(0); + attrib.set_float_array(4, 0); + attrib.associate_with(0); + attrib.enable(); + + attrib = m_vao.get_attribute(1); + attrib.set_float_array(4, sizeof(float) * 4); + attrib.associate_with(0); + attrib.enable(); + + attrib = m_vao.get_attribute(2); + attrib.set_float_array(2, sizeof(float) * 8); + attrib.associate_with(0); + attrib.enable(); + + attrib = m_vao.get_attribute(3); + attrib.set_float_array(2, sizeof(float) * 10); + attrib.associate_with(0); + attrib.enable(); + } +} diff --git a/src/graphics/vbo.cpp b/src/graphics/vbo.cpp index 8f09b3e..ece1d2a 100644 --- a/src/graphics/vbo.cpp +++ b/src/graphics/vbo.cpp @@ -22,15 +22,17 @@ #include #include //exchange, swap, move +#include //memset + namespace gfx{ - vbo::vbo(size_t size, usage t): + vbo::vbo(size_t size, buffer::usage t): m_buffer_size(0) { glCreateBuffers(1, &m_buffer); glNamedBufferData(m_buffer, size, NULL, static_cast(t)); } - vbo::vbo(const void* data, size_t size, usage t): + vbo::vbo(const void* data, size_t size, buffer::usage t): m_buffer_size(size) { glCreateBuffers(1, &m_buffer); @@ -40,7 +42,7 @@ namespace gfx{ m_buffer_size(v.m_buffer_size) { glCreateBuffers(1, &m_buffer); - scoped_vbo_map data = v.map(maptype::READ); + scoped_buffer_map data = v.map(buffer::maptype::READ); if(!data) return; glNamedBufferData(m_buffer, v.get_size(), data, static_cast(v.get_usage())); @@ -48,7 +50,7 @@ namespace gfx{ vbo::vbo(vbo&& v): m_buffer(std::exchange(v.m_buffer, 0)), m_buffer_size(v.m_buffer_size){} - vbo::~vbo(){ + vbo::~vbo(void){ if(m_buffer) glDeleteBuffers(1, &m_buffer); } @@ -61,10 +63,6 @@ namespace gfx{ m_buffer_size = v.m_buffer_size; return *this; } - bool vbo::bind(gl_buffer_manager& buf)const{ - glBindBuffer(buf.get_buffer_id(), m_buffer); - return true; - } bool vbo::buffer(const void* data, size_t datasize){ if(datasize + m_buffer_size > get_cap()){ @@ -88,6 +86,16 @@ namespace gfx{ m_buffer_size = rexy::max(m_buffer_size, datasize + start); return true; } + bool vbo::initialize(unsigned char value){ + const size_t capacity = get_cap(); + auto m = map(buffer::maptype::WRITE); + if(!m){ + return false; + } + memset(m, capacity, value); + return true; + } + bool vbo::resize(size_t newsize){ vbo tmp(newsize, get_usage()); if(!copy_buffer(tmp, *this)) @@ -95,82 +103,36 @@ namespace gfx{ *this = std::move(tmp); return true; } - GLuint vbo::raw()const{ + GLuint vbo::raw(void)const{ return m_buffer; } - scoped_vbo_map vbo::map(maptype m)const{ - return scoped_vbo_map(m_buffer, m); + scoped_buffer_map vbo::map(buffer::maptype m)const{ + return scoped_buffer_map(m_buffer, m); } - void vbo::clear(){ + void vbo::clear(void){ m_buffer_size = 0; } - size_t vbo::get_size()const{ + size_t vbo::get_size(void)const{ return m_buffer_size; } - size_t vbo::get_cap()const{ + size_t vbo::get_cap(void)const{ GLint retval; glGetNamedBufferParameteriv(m_buffer, GL_BUFFER_SIZE, &retval); return retval; } - auto vbo::get_usage()const -> usage{ + auto vbo::get_usage(void)const -> buffer::usage{ GLint retval; glGetNamedBufferParameteriv(m_buffer, GL_BUFFER_USAGE, &retval); - return static_cast(retval); + return static_cast(retval); } bool vbo::copy_buffer(vbo& dest, const vbo& src){ size_t bytes = rexy::min(dest.get_cap(), src.get_size()); - scoped_vbo_map data(src.m_buffer, maptype::READ); + scoped_buffer_map data(src.m_buffer, buffer::maptype::READ); if(!data.valid()) return false; return dest.buffer(data, bytes); } - - scoped_vbo_map::scoped_vbo_map(GLuint bid, vbo::maptype m): - m_buffer(bid) - { - debug_print_verbose("Mapping vertex buffer object %u\n", bid); - m_data = reinterpret_cast(glMapNamedBuffer(m_buffer, static_cast(m))); - } - scoped_vbo_map::scoped_vbo_map(scoped_vbo_map&& m): - m_data(std::exchange(m.m_data, nullptr)), - m_buffer(std::exchange(m.m_buffer, 0)){} - scoped_vbo_map::~scoped_vbo_map(){ - if(m_data){ - debug_print_verbose("Unmapping vertex buffer object %u\n", m_buffer); - glUnmapNamedBuffer(m_buffer); - } - } - scoped_vbo_map& scoped_vbo_map::operator=(scoped_vbo_map&& m){ - std::swap(m_data, m.m_data); - std::swap(m_buffer, m.m_buffer); - return *this; - } - auto scoped_vbo_map::length()const -> size_type{ - GLint64 retval; - glGetNamedBufferParameteri64v(m_buffer, GL_BUFFER_MAP_LENGTH, &retval); - return retval; - } - auto scoped_vbo_map::release() -> pointer{ - m_buffer = 0; - return std::exchange(m_data, nullptr); - } - scoped_vbo_map::operator pointer(){ - return m_data; - } - scoped_vbo_map::operator const_pointer()const{ - return m_data; - } - auto scoped_vbo_map::raw() -> pointer{ - return m_data; - } - auto scoped_vbo_map::raw()const -> const_pointer{ - return m_data; - } - bool scoped_vbo_map::valid()const{ - return m_data; - } - } diff --git a/src/main.cpp b/src/main.cpp index d3d1c17..bf74780 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -27,6 +27,10 @@ #include "ttt/tic_tac_toe.hpp" #include "engine/font.hpp" +#include "graphics/ubo.hpp" + +#include "math/debug.hpp" + int main(){ srand(time(NULL)); diff --git a/src/nothrow_allocation.cpp b/src/nothrow_allocation.cpp index 8522d9c..2e2ee42 100644 --- a/src/nothrow_allocation.cpp +++ b/src/nothrow_allocation.cpp @@ -16,11 +16,12 @@ along with this program. If not, see . */ +#ifndef ENABLE_MEMCHK + #include //size_t #include //malloc, free #include -#ifndef ENABLE_MEMCHK void* operator new(size_t newsize){ return std::malloc(newsize); } @@ -38,4 +39,5 @@ void operator delete(void* ptr, std::align_val_t al)noexcept{ void operator delete(void* ptr, size_t)noexcept{ ::operator delete(ptr); } + #endif diff --git a/src/ttt/board.cpp b/src/ttt/board.cpp index ca0ed20..2fd42fe 100644 --- a/src/ttt/board.cpp +++ b/src/ttt/board.cpp @@ -36,7 +36,7 @@ const math::vec4& tile::get_color(void)const{ board::board(gfx::resource_manager& resman): m_tiles(new tile[9]), - m_gfx(new board_gfx(resman.emplace_texture_array("board_textures", 3))) + m_gfx(new board_gfx(resman)) { m_tiles[0].set_position({-2.125, 2.125, -1}); m_tiles[1].set_position({-2.125, 0, -1}); diff --git a/src/ttt/board_gfx.cpp b/src/ttt/board_gfx.cpp index 47e12d2..fb92de9 100644 --- a/src/ttt/board_gfx.cpp +++ b/src/ttt/board_gfx.cpp @@ -19,11 +19,12 @@ #include "ttt/board_gfx.hpp" #include "ttt/board.hpp" -board_gfx::board_gfx(std::pair resman_result): - m_vbo((16 * sizeof(GLfloat) + 4 * sizeof(GLfloat) + 1 * sizeof(GLint)) * 9, gfx::vbo::usage::DYNAMIC_DRAW) +board_gfx::board_gfx(gfx::resource_manager& resman): + m_vbo((16 * sizeof(GLfloat) + 4 * sizeof(GLfloat) + 1 * sizeof(GLint)) * 9, gfx::buffer::usage::DYNAMIC_DRAW) { - m_textures = resman_result.first; - if(resman_result.second){ + auto result = resman.emplace_texture_array("board_textures", 3); + m_textures = std::move(result.first); + if(result.second){ (*m_textures)[0].set_image(egn::image("assets/images/blank.jpg", true)); (*m_textures)[1].set_image(egn::image("assets/images/o.jpg", true)); (*m_textures)[2].set_image(egn::image("assets/images/x.jpg", true)); @@ -63,7 +64,7 @@ board_gfx::board_gfx(std::pair resman_result): } void board_gfx::set_uniforms(gfx::shader_program& sh){ - sh.set_uniform("textures", *m_textures, 0); + sh.get_uniform("textures").set(*m_textures, 0); } void board_gfx::bind_buffers(void){ m_vao.bind(); diff --git a/src/ttt/font_renderer.cpp b/src/ttt/font_renderer.cpp index 2b3c577..14b9041 100644 --- a/src/ttt/font_renderer.cpp +++ b/src/ttt/font_renderer.cpp @@ -31,6 +31,7 @@ font_renderer::font_renderer(gfx::resource_manager& res, int width, int height): m_font_shader = res.emplace_shader("font_shader", util::make_deferred(font_shader::vertex_shader_text, gfx::shader::type::VERTEX), util::make_deferred(font_shader::geometry_shader_text, gfx::shader::type::GEOMETRY), util::make_deferred(font_shader::fragment_shader_text, gfx::shader::type::FRAGMENT)).first; + if(m_font_shader->has_link_error()){ debug_print_error("%s\n", m_font_shader->get_error().c_str()); } @@ -38,7 +39,7 @@ font_renderer::font_renderer(gfx::resource_manager& res, int width, int height): resize_viewport(width, height); } -void font_renderer::render(egn::text& t){ +void font_renderer::begin(void){ //Clear the global state to that which this renderer prefers glBindFramebuffer(GL_FRAMEBUFFER, 0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); @@ -47,9 +48,20 @@ void font_renderer::render(egn::text& t){ glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); m_font_shader->use(); - m_font_shader->set_uniform("out_size", math::vec2f{m_screen_width, m_screen_height}); + set_aspect(((float)m_screen_width) / m_screen_height); +} - t.render(*m_font_shader); +void font_renderer::set_position(const math::vec2f& pos){ + m_font_shader->get_uniform("start_pos").set(pos); +} +void font_renderer::use_texture(const gfx::texture& tex){ + m_font_shader->get_uniform("texture1").set(tex); +} +void font_renderer::use_texture(const gfx::weak_texture& tex){ + m_font_shader->get_uniform("texture1").set(tex); +} +void font_renderer::set_aspect(float aspect){ + m_font_shader->get_uniform("aspect").set(aspect); } void font_renderer::resize_viewport(int width, int height){ diff --git a/src/ttt/main_renderer.cpp b/src/ttt/main_renderer.cpp index 533edc4..b7474f2 100644 --- a/src/ttt/main_renderer.cpp +++ b/src/ttt/main_renderer.cpp @@ -31,8 +31,8 @@ main_renderer::main_renderer(gfx::resource_manager& res, int width, int height): m_fb(MAX_WIDTH, MAX_HEIGHT), m_primary_fb(util::no_initialize), - m_vbo(s_vertices, sizeof(s_vertices), gfx::vbo::usage::DYNAMIC_DRAW), - m_board_vbo((16*sizeof(GLfloat)+sizeof(GLint)+4*sizeof(GLfloat)) * 9, gfx::vbo::usage::DYNAMIC_DRAW) + m_vbo(s_vertices, sizeof(s_vertices), gfx::buffer::usage::DYNAMIC_DRAW), + m_board_vbo((16*sizeof(GLfloat)+sizeof(GLint)+4*sizeof(GLfloat)) * 9, gfx::buffer::usage::DYNAMIC_DRAW) { m_square_shader = res.emplace_shader("square_shader", util::make_deferred(square_shader::fragment_shader_text, gfx::shader::type::FRAGMENT), util::make_deferred(square_shader::geometry_shader_text, gfx::shader::type::GEOMETRY), @@ -41,11 +41,11 @@ main_renderer::main_renderer(gfx::resource_manager& res, int width, int height): debug_print_error("%s\n", m_square_shader->get_error().c_str()); } - m_square_shader->set_uniform("vp_mat", math::mat4{}); + m_square_shader->get_uniform("vp_mat").set(math::mat4{}); m_screen_shader = res.emplace_shader("screen_shader", util::make_deferred(screen_shader::fragment_shader_text, gfx::shader::type::FRAGMENT), util::make_deferred(screen_shader::vertex_shader_text, gfx::shader::type::VERTEX)).first; - m_screen_shader->set_uniform("vp_mat", math::mat4{}); + m_screen_shader->get_uniform("vp_mat").set(math::mat4{}); m_vao.bind_buffer(m_vbo, 0, 0, sizeof(vertex)); auto attrib = m_vao.get_attribute(0); @@ -61,7 +61,7 @@ main_renderer::main_renderer(gfx::resource_manager& res, int width, int height): } void main_renderer::set_vp_matrix(const math::mat4& vp){ - m_square_shader->set_uniform("vp_mat", vp); + m_square_shader->get_uniform("vp_mat").set(vp); } void main_renderer::render(scene& s){ m_fb.bind(); @@ -79,7 +79,7 @@ void main_renderer::render(scene& s){ glDisable(GL_DEPTH_TEST); m_screen_shader->use(); - m_screen_shader->set_uniform("texture1", m_fb.colorbuffer(), 0); + m_screen_shader->get_uniform("texture1").set(m_fb.colorbuffer(), 0); m_vao.bind(); glDrawArrays(GL_TRIANGLES, 0, 6); @@ -87,7 +87,7 @@ void main_renderer::render(scene& s){ void main_renderer::resize_viewport(int width, int height){ m_fb.set_viewport(0, 0, width, height); - auto map = m_vbo.map(gfx::vbo::maptype::WRITE); + auto map = m_vbo.map(gfx::buffer::maptype::WRITE); vertex* data = reinterpret_cast(map.raw()); GLfloat x1 = 0, y1 = 0; GLfloat x2 = (GLfloat)width / m_fb.colorbuffer().get_width(); diff --git a/src/ttt/pause_state.cpp b/src/ttt/pause_state.cpp index d1be43e..e49cb8f 100644 --- a/src/ttt/pause_state.cpp +++ b/src/ttt/pause_state.cpp @@ -22,6 +22,8 @@ #include "graphics/gl_include.hpp" //TODO +#include "engine/text_field.hpp" + #include "config.hpp" static egn::font_atlas generate_font_atlas_from_file(const char* file, int target_height){ @@ -32,13 +34,21 @@ static egn::font_atlas generate_font_atlas_from_file(const char* file, int targe pause_state::pause_state(egn::game& owner, egn::game_state_iface* under): game_state_iface(&owner), m_under_state(under), - m_screen_renderer(owner.gfx_resource_manager(), owner.get_width(), owner.get_height(), *owner.gfx_resource_manager().emplace_texture("pause_screen", util::make_deferred("assets/images/pause.png", true)).first), - m_font_renderer(owner.gfx_resource_manager(), owner.get_width(), owner.get_height()), - m_text(*owner.gfx_resource_manager().emplace_font("hinted-RosaSans-Regular-atlas-50px", util::deferred_function(generate_font_atlas_from_file, "assets/hinted-RosaSans-Regular.ttf", 50)).first, "Pause Menu", 50, 100, 100){} + m_screen_renderer(owner.gfx_resource_manager(), owner.get_width(), owner.get_height(), owner.gfx_resource_manager().emplace_texture("pause_screen", util::make_deferred("assets/images/pause.png", true)).first), + m_menu_renderer(owner.gfx_resource_manager()), + m_menu() +{ + m_menu.set_default_font(owner.gfx_resource_manager().emplace_font("pause_screen_font", util::deferred_function(generate_font_atlas_from_file, "assets/hinted-RosaSans-Regular.ttf", 50)).first); + [[maybe_unused]] auto& item = m_menu.emplace_item("test text"); +} void pause_state::enter(){ - m_screen_renderer.resize_viewport(m_owner->get_width(), m_owner->get_height()); - m_font_renderer.resize_viewport(m_owner->get_width(), m_owner->get_height()); + const float w = m_owner->get_width(); + const float h = m_owner->get_height(); + + m_screen_renderer.resize_viewport(w, h); + //m_menu_renderer.resize_viewport(w, h); + } void pause_state::leave(){} void pause_state::handle_input(const egn::input_event& event){ @@ -53,12 +63,16 @@ void pause_state::handle_input(const egn::input_event& event){ }else if(event.ev_type == egn::input_event::type::RESIZE){ m_under_state->handle_input(event); m_screen_renderer.resize_viewport(event.x, event.y); - m_font_renderer.resize_viewport(event.x, event.y); + //m_menu_renderer.resize_viewport(event.x, event.y); } } void pause_state::update(double){} void pause_state::render(){ m_under_state->render(); - //m_screen_renderer.render(tmp); - m_font_renderer.render(m_text); + scene tmp; + m_screen_renderer.render(tmp); + +// m_menu_renderer.begin(); +// m_menu.render(m_menu_renderer); +// m_menu_renderer.end(); } diff --git a/src/ttt/screen_renderer.cpp b/src/ttt/screen_renderer.cpp index dd22adb..f8c1048 100644 --- a/src/ttt/screen_renderer.cpp +++ b/src/ttt/screen_renderer.cpp @@ -28,9 +28,9 @@ #include "ttt/font_shader.hpp" #include "math/debug.hpp" -screen_renderer::screen_renderer(gfx::resource_manager& res, int width, int height, gfx::texture& base_tex): - m_vbo(s_vertices, sizeof(s_vertices), gfx::vbo::usage::DYNAMIC_DRAW), - m_texture(&base_tex), +screen_renderer::screen_renderer(gfx::resource_manager& res, int width, int height, const std::shared_ptr& base_tex): + m_vbo(s_vertices, sizeof(s_vertices), gfx::buffer::usage::DYNAMIC_DRAW), + m_texture(base_tex), m_screen_width(width), m_screen_height(height) { @@ -62,7 +62,7 @@ void screen_renderer::render(scene&){ glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); //Apply our shader and vao m_screen_shader->use(); - m_screen_shader->set_uniform("texture1", *m_texture, 0); + m_screen_shader->get_uniform("texture1").set(*m_texture, 0); m_vao.bind(); //Draw