From 0eebe25ea9f42a057ecb54af99133676e25f4b0c Mon Sep 17 00:00:00 2001 From: Hartmut Seichter Date: Fri, 30 Jun 2023 23:20:30 +0200 Subject: [PATCH] minor update poking around why the texturing code is broken --- README.md | 11 +++++---- src/lib/include/paradiso/shader.hpp | 6 ++--- src/lib/include/paradiso/sprite.hpp | 3 +++ src/lib/src/renderer.cpp | 35 +++++++++++++++++++++-------- src/lib/src/shader.cpp | 8 +++---- src/main.cpp | 8 +++---- 6 files changed, 47 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 80e092c..6060d9a 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,13 @@ -# ParadisSO - a minimal 2D rendering engine for +# ParadisSO - a minimal 2D graphics engine **ParadiSO** was conceived as a heavily stripped down version of my pixwerx engine. It is a minimalistic 2D graphics engine for educational purposes. It uses modern C++ and a datadriven design, but no ECS. ## Educational -* plenty of mix and match of concepts and paradigms to write expressive but concise code -* math code is eager evaluation but constexpr to compensate overheads -* it hides old-style APIs behind a renovated facade +Some arguments for its educational side: + +* mix and match of various concepts and paradigms to write expressive but concise code +* heavily inspired by Rust code +* math code is eager evaluation but `constexpr` to compensate overheads +* hides old-style `C` APIs behind a renovated facade * it leans heavily on the STL and its algorithms \ No newline at end of file diff --git a/src/lib/include/paradiso/shader.hpp b/src/lib/include/paradiso/shader.hpp index b616ac9..3655a0b 100644 --- a/src/lib/include/paradiso/shader.hpp +++ b/src/lib/include/paradiso/shader.hpp @@ -56,9 +56,9 @@ struct Shader final { Shader& set_uniform_at_location(int location, uint32_t v); //!< sets a 32bit unsigned in a shader - Shader& + const Shader& set_uniform_at_location(int location, - int32_t v); //!< sets a 32bit signed in a shader + int32_t v) const; //!< sets a 32bit signed in a shader /** * @brief retrieves the position of a uniform @@ -80,7 +80,7 @@ struct Shader final { * sets data of the */ template - Shader& set_uniform(std::string const& name, T&& value) { + const Shader& set_uniform(std::string const& name, T&& value) const { return set_uniform_at_location(uniform_location(name), std::forward(value)); } diff --git a/src/lib/include/paradiso/sprite.hpp b/src/lib/include/paradiso/sprite.hpp index d766bbb..90ca7a8 100644 --- a/src/lib/include/paradiso/sprite.hpp +++ b/src/lib/include/paradiso/sprite.hpp @@ -35,6 +35,8 @@ struct Sprite final { static constexpr Sprite create() noexcept { return {}; } + Bitmap bitmap{}; + Vector2 pivot{Vector2::zero()}; std::array indices{0, 3, 2, 2, 1, 0}; @@ -57,6 +59,7 @@ struct Sprite final { }; ChangeCountType change_count{}; + }; } // namespace paradiso diff --git a/src/lib/src/renderer.cpp b/src/lib/src/renderer.cpp index a68d877..5cf4df0 100644 --- a/src/lib/src/renderer.cpp +++ b/src/lib/src/renderer.cpp @@ -53,12 +53,6 @@ struct Renderer::impl { GL_TRUE == glIsVertexArray(vertex_array_obj); } - void bind_texture() { - glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, texture_id); - } - - void unbind_texture() { glBindTexture(GL_TEXTURE_2D, 0); } bool build(const Sprite& sprite) { // reset if the Renderer already in use @@ -120,7 +114,15 @@ struct Renderer::impl { return ready(); } - void update_texture(const Bitmap& image) { + + void texture_bind() { + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, texture_id); + } + + void texture_unbind() { glBindTexture(GL_TEXTURE_2D, 0); } + + void texture_update(const Bitmap& image) { if (GL_FALSE == glIsTexture(texture_id)) { glGenTextures(1, &texture_id); @@ -170,7 +172,12 @@ struct Renderer::impl { image.data.data()); // pointer } - glBindTexture(GL_TEXTURE_2D, 0); + // glBindTexture(GL_TEXTURE_2D, 0); + } + + void texture_release() + { + glDeleteTextures(1, &texture_id); } void release() { @@ -181,7 +188,8 @@ struct Renderer::impl { glDeleteVertexArrays(1, &vertex_array_obj); - // glDeleteTextures(1, &texture_id); + texture_release(); + } void just_draw(const Sprite& sprite) { @@ -215,6 +223,7 @@ bool Renderer::draw(const Sprite& sprite, const Shader& shader) { // if interna are not ready or sprite has been altered - rebuild if (!impl_->ready() || sprite.change_count != impl_->change_count) { impl_->build(sprite); + impl_->texture_update(sprite.bitmap); } // render the sprite with the shader @@ -222,8 +231,16 @@ bool Renderer::draw(const Sprite& sprite, const Shader& shader) { shader.use(); + shader.set_uniform("tex_color", 0u); // texture unit 0 + + impl_->texture_bind(); + impl_->just_draw(sprite); + //Renderer::impl::fetch_errors(__PRETTY_FUNCTION__); + + // impl_->texture_unbind(); + return true; } diff --git a/src/lib/src/shader.cpp b/src/lib/src/shader.cpp index 220318b..92232c4 100644 --- a/src/lib/src/shader.cpp +++ b/src/lib/src/shader.cpp @@ -177,11 +177,11 @@ struct Shader::impl { // glUniform4fv(location,1,v.ptr()); // } - void bind(int location, const float& v) { glUniform1f(location, v); } + void bind(int location, const float& v) const { glUniform1f(location, v); } - void bind(int location, const uint32_t& i) { glUniform1ui(location, i); } + void bind(int location, const uint32_t& i) const { glUniform1ui(location, i); } - void bind(int location, const int32_t& i) { glUniform1i(location, i); } + void bind(int location, const int32_t& i) const { glUniform1i(location, i); } // void bind(int location,const texture& v) // { @@ -206,7 +206,7 @@ Shader& Shader::set_uniform_at_location(int location, uint32_t v) { return *this; } -Shader& Shader::set_uniform_at_location(int location, int32_t v) { +const Shader& Shader::set_uniform_at_location(int location, int32_t v) const { impl_->bind(location, v); return *this; } diff --git a/src/main.cpp b/src/main.cpp index a93995c..0e02a5e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -77,7 +77,7 @@ auto main() -> int { auto ctx = paradiso::Context{}; auto sprite = paradiso::Sprite{ - // + .bitmap = checker_board_2 }; auto renderer = paradiso::Renderer{}; @@ -102,9 +102,9 @@ auto main() -> int { while (window.update([&](auto& w) -> bool { ctx.set_clearcolor(paradiso::RGBA::from_rgb(0xFF, green_slider, 0x00)); - ctx.set_viewport(paradiso::Rectangle{ - .size = w.client_size() - }); + // ctx.set_viewport(paradiso::Rectangle{ + // .size = w.client_size() + // }); ctx.clear(); renderer.draw(sprite, shader);