diff --git a/assets/shaders/image_f_shader.glsl b/assets/shaders/image_f_shader.glsl new file mode 100644 index 0000000..376eded --- /dev/null +++ b/assets/shaders/image_f_shader.glsl @@ -0,0 +1,11 @@ +#version 460 + +in vec2 tc; + +out vec4 color; + +layout (binding = 0) uniform sampler2D samp; + +void main(void) { + color = texture(samp, tc); +} \ No newline at end of file diff --git a/assets/shaders/image_v_shader.glsl b/assets/shaders/image_v_shader.glsl new file mode 100644 index 0000000..dbd4b7f --- /dev/null +++ b/assets/shaders/image_v_shader.glsl @@ -0,0 +1,14 @@ +#version 460 + +layout (location = 0) in vec2 pos; +layout (location = 1) in vec2 texCoord; + +out vec2 tc; + +uniform mat4 model_matrix; +uniform mat4 proj_matrix; + +void main(void) { + gl_Position = proj_matrix * model_matrix * vec4(pos, 0.0, 1.0); + tc = texCoord; +} diff --git a/assets/shaders/ui_f_shader.glsl b/assets/shaders/ui_f_shader.glsl deleted file mode 100644 index eee4617..0000000 --- a/assets/shaders/ui_f_shader.glsl +++ /dev/null @@ -1,12 +0,0 @@ -#version 460 - -in vec2 tc; -flat in int tex_layer; - -out vec4 color; - -layout (binding = 0) uniform sampler2DArray samp; - -void main(void) { - color = texture(samp, vec3(tc, tex_layer)); -} \ No newline at end of file diff --git a/assets/shaders/ui_v_shader.glsl b/assets/shaders/ui_v_shader.glsl deleted file mode 100644 index ae600c3..0000000 --- a/assets/shaders/ui_v_shader.glsl +++ /dev/null @@ -1,18 +0,0 @@ -#version 460 - -layout (location = 0) in vec2 pos; -layout (location = 1) in vec2 texCoord; -layout (location = 2) in float layer; - -out vec2 tc; - -flat out int tex_layer; - -uniform mat4 m_matrix; -uniform mat4 proj_matrix; - -void main(void) { - gl_Position = proj_matrix * m_matrix * vec4(pos, 0.0, 1.0); - tc = texCoord; - tex_layer = int(layer); -} diff --git a/include/Cubed/render/renderer.hpp b/include/Cubed/render/renderer.hpp index 42471c1..afaea89 100644 --- a/include/Cubed/render/renderer.hpp +++ b/include/Cubed/render/renderer.hpp @@ -9,6 +9,7 @@ #include "Cubed/render/vertex_buffer.hpp" #include "Cubed/render/world_renderer.hpp" #include "Cubed/shader.hpp" +#include "Cubed/ui/image.hpp" #include "Cubed/ui/label.hpp" #include @@ -33,6 +34,7 @@ public: void render(); void render_lable(const Label& label); + void render_image(const Image& image); void update(float delta_time); void update_fov(float fov); @@ -99,8 +101,9 @@ private: std::unique_ptr m_player_vbo; std::unique_ptr m_quad_vbo; + std::unique_ptr m_crosshair_image; + glm::mat4 m_ui_proj_matrix; - glm::mat4 m_ui_model_matrix; ShaderManager m_shaders; /* diff --git a/include/Cubed/render/texture.hpp b/include/Cubed/render/texture.hpp index df975e8..25b9180 100644 --- a/include/Cubed/render/texture.hpp +++ b/include/Cubed/render/texture.hpp @@ -88,7 +88,6 @@ public: private: GLuint m_id = 0; const TextureType M_TYPE; - GLenum get_gl_texture_type() const; }; } // namespace Cubed \ No newline at end of file diff --git a/include/Cubed/tools/shader_tools.hpp b/include/Cubed/tools/shader_tools.hpp index aedb4e3..3162e36 100644 --- a/include/Cubed/tools/shader_tools.hpp +++ b/include/Cubed/tools/shader_tools.hpp @@ -3,9 +3,42 @@ #include #include #include - +#include namespace Cubed { +namespace Tools { +void delete_image_data(unsigned char* data); +} + +struct ImageData { + unsigned char* data = nullptr; + int width = 0; + int height = 0; + int channels = 0; + ImageData(const ImageData&) = delete; + ImageData(ImageData&& o) noexcept + : data(std::exchange(o.data, nullptr)), width(o.width), + height(o.height), channels(o.channels) {} + ImageData& operator=(const ImageData&) = delete; + ImageData& operator=(ImageData&& o) noexcept { + if (this == &o) { + return *this; + } + if (data) { + Tools::delete_image_data(data); + } + data = std::exchange(o.data, nullptr); + width = o.width; + height = o.height; + channels = o.channels; + return *this; + } + ImageData(unsigned char* d, int w, int h, int c) + : data(d), width(w), height(h), channels(c) {} + ImageData() = default; + ~ImageData() { Tools::delete_image_data(data); } +}; + namespace Tools { GLuint create_shader_program(const std::string& v_shader_path, const std::string& f_shader_path); @@ -13,9 +46,9 @@ void print_shader_log(GLuint shader); void print_program_info(int prog); bool check_opengl_error(); std::string read_shader_source(const std::string& file_path); -void delete_image_data(unsigned char* data); -unsigned char* load_image_data(const std::string& tex_image_path, - bool check_exist = true); + +ImageData load_image_data(const std::string& tex_image_path, + bool check_exist = true); } // namespace Tools diff --git a/include/Cubed/ui/image.hpp b/include/Cubed/ui/image.hpp new file mode 100644 index 0000000..44e122e --- /dev/null +++ b/include/Cubed/ui/image.hpp @@ -0,0 +1,28 @@ +#pragma once + +#include "Cubed/render/texture.hpp" +#include "Cubed/ui/widget.hpp" +#include "glm/ext/vector_float2.hpp" + +#include + +namespace Cubed { +class Image : public Widget { +public: + Image(); + void update(float dt) override; + void render(Renderer& renderer) override; + + Image& set_image(std::filesystem::path path); + float width() const; + float height() const; + const Texture& texture() const; + +private: + Texture m_texture; + int m_width = 0; + int m_height = 0; + glm::vec2 m_pos{0.0f, 0.0f}; + float m_scale = 1.0f; +}; +} // namespace Cubed \ No newline at end of file diff --git a/include/Cubed/ui/label.hpp b/include/Cubed/ui/label.hpp index f4034cc..788820a 100644 --- a/include/Cubed/ui/label.hpp +++ b/include/Cubed/ui/label.hpp @@ -14,18 +14,15 @@ public: Label(const std::string& id); virtual ~Label() = default; - Label& set_position(const glm::vec2& pos); - Label& set_position(float x, float y); Label& set_text(std::string_view text); - Label& set_scale(float scale); + Label& set_color(Color color); virtual void update(float dt) override; virtual void render(Renderer& renderer) override; const UIVertexData& data() const; - const glm::vec2& pos() const; + const TextStyle& text_style() const; - float scale() const; protected: virtual void on_update(float dt) override; diff --git a/include/Cubed/ui/widget.hpp b/include/Cubed/ui/widget.hpp index 5268257..608839a 100644 --- a/include/Cubed/ui/widget.hpp +++ b/include/Cubed/ui/widget.hpp @@ -1,4 +1,6 @@ #pragma once +#include "glm/ext/vector_float2.hpp" + #include #include namespace Cubed { @@ -12,6 +14,11 @@ public: virtual void update(float dt); virtual void render(Renderer& renderer); virtual const std::string& id() const; + virtual Widget& set_position(const glm::vec2& pos); + virtual Widget& set_position(float x, float y); + virtual Widget& set_scale(float scale); + virtual const glm::vec2& pos() const; + virtual float scale() const; template T& add_child(Args&&... args) { auto widget = std::make_unique(std::forward(args)...); T& ref = *widget; @@ -23,6 +30,8 @@ protected: virtual void on_update(float dt); virtual void on_render(Renderer& renderer); std::string m_id; + float m_scale = 1.0f; + glm::vec2 m_pos{0.0f, 0.0f}; private: Widget* m_parent = nullptr; diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index a99015c..5d78f70 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -61,4 +61,5 @@ target_sources(${PROJECT_NAME} ui/widget.cpp ui/label.cpp ui/ui_vertex_data.cpp + ui/image.cpp ) \ No newline at end of file diff --git a/src/debug_collector.cpp b/src/debug_collector.cpp index a2808e7..ca183d6 100644 --- a/src/debug_collector.cpp +++ b/src/debug_collector.cpp @@ -21,22 +21,22 @@ void DebugCollector::init_text() { #else version.append("-release"); #endif - version_text.set_position(0.0f, 100.0f) - .set_scale(0.8f) - .set_color(Color::WHITE) - .set_text(version); + version_text.set_color(Color::WHITE) + .set_text(version) + .set_position(0.0f, 100.0f) + .set_scale(0.8f); m_component.try_emplace(version_text.id(), &version_text); // fps auto& fps_text = m_widget.add_child