diff --git a/examples/simple/main.cpp b/examples/simple/main.cpp index cee9607..eb4c1c8 100644 --- a/examples/simple/main.cpp +++ b/examples/simple/main.cpp @@ -18,36 +18,56 @@ void setup_shaders(paradiso::Shader& shader) { const auto unlit_v = R"( -#version 330 core +#version 400 core layout (location = 0) in vec3 vertices; layout (location = 1) in vec3 normals; layout (location = 2) in vec2 texture_coords; +// pivot der sprite uniform vec2 pivot = vec2( 0.0, 0.0 ); +// scale +uniform vec2 scale = vec2( 1.0, 1.0 ); +// rotation +uniform float rotation = 0.2; -vec4 pivot_3d = vec4( pivot , 0.0, 1.0 ); +// wir sind natuerlich in homogenenen 3D Koordinaten unterwegs +mat4 mm = mat4( + vec4( scale.x, 0.0, 0.0, 0.0), + vec4( 0.0, scale.y, 0.0, 0.0), + vec4( 0.0, 0.0, 1.0, 0.0), + vec4( pivot, 0.0, 1.0) +); -out vec2 tex_c; +float sir = sin(rotation); +float cor = cos(rotation); + +mat4 mr = mat4( + vec4( cor, sir, 0.0, 0.0), + vec4(-sir, cor, 0.0, 0.0), + vec4( 0.0, 0.0, 1.0, 0.0), + vec4( 0.0, 0.0, 0.0, 1.0) + ); + +out vec2 tex_c; // das hier reicht die texturkoordinaten durch void main() { - tex_c = texture_coords; - gl_Position = vec4(vertices, 1.0) + pivot_3d; + tex_c = texture_coords; // umstaendlich aber notwendig + gl_Position = mm * mr * vec4(vertices, 1.0); // unsere eigentliche shader position } )"; const auto unlit_f = R"( -#version 330 core +#version 400 core -uniform vec4 color = vec4(1.0, 0.0, 0.0, 1.0); -uniform sampler2D tex_color; +uniform sampler2D tex_color; // hier ist unsere sprite textur (bitmap) -in vec2 tex_c; +in vec2 tex_c; // da sind die texturkoordinaten wieder -out vec4 frag_color; +out vec4 frag_color; // das hier wird der output (pixelwert/fragment) void main() { - frag_color = texture(tex_color,tex_c); // * color; + frag_color = texture(tex_color,tex_c); })"; shader.set_source(paradiso::Shader::Type::Vertex, unlit_v); @@ -111,6 +131,10 @@ auto main() -> int { sprite.pivot.x() -= 0.1f; } else if (key == 'D') { sprite.pivot.x() += 0.1f; + } else if (key == 'P') { + sprite.scale *= 0.9; + } else if (key == 'R') { + sprite.rotation += 0.1; } }); @@ -136,8 +160,10 @@ auto main() -> int { // hier wird das eigentliche löschen des vorherigen Inhalts ausgelöst ctx.clear(); - // wir setzen den pivot der sprite über den shader + // wir setzen die daten der sprite über den shader shader.set_uniform("pivot", sprite.pivot); + shader.set_uniform("scale", sprite.scale); + shader.set_uniform("rotation", sprite.rotation); // Ein `renderer` kann nur mit einer Sprite verwendet werden! // Aber ein Shader kann man für mehrere Sprite-Renderer Kombis verwenden diff --git a/src/lib/include/paradiso/matrixbase.hpp b/src/lib/include/paradiso/matrixbase.hpp index 5aa73e2..56d012e 100644 --- a/src/lib/include/paradiso/matrixbase.hpp +++ b/src/lib/include/paradiso/matrixbase.hpp @@ -67,12 +67,15 @@ template struct MatrixBase { return derived(); } - static constexpr Derived zero() noexcept { + static constexpr Derived + all(const std::convertible_to auto& v) noexcept { Derived d; - d.fill(0); + d.fill(v); return d; } + static constexpr Derived zero() noexcept { return Derived::all(Scalar{0}); } + constexpr Scalar squared_norm() const { return dot(*this, *this); } constexpr Scalar norm() const { return std::sqrt(squared_norm()); } diff --git a/src/lib/include/paradiso/shader.hpp b/src/lib/include/paradiso/shader.hpp index 53833c6..9382fc9 100644 --- a/src/lib/include/paradiso/shader.hpp +++ b/src/lib/include/paradiso/shader.hpp @@ -51,20 +51,19 @@ struct Shader final { void use() const; - - - Shader& set_uniform_at_location(int location, - float v); //!< sets a float in a shader - Shader& - set_uniform_at_location(int location, - uint32_t v); //!< sets a 32bit unsigned in a shader const Shader& set_uniform_at_location(int location, - int32_t v) const; //!< sets a 32bit signed in a shader + float v) const; //!< sets a float in a shader + const Shader& set_uniform_at_location( + int location, + uint32_t v) const; //!< sets a 32bit unsigned in a shader + const Shader& set_uniform_at_location( + int location, + int32_t v) const; //!< sets a 32bit signed in a shader - const Shader& - set_uniform_at_location(int location, - const Vector2& v) const; //!< sets a 2D float vector + const Shader& set_uniform_at_location( + int location, + const Vector2& v) const; //!< sets a 2D float vector /** * @brief retrieves the position of a uniform * @param name of the uniform @@ -91,8 +90,8 @@ struct Shader final { } using uniform_t = - std::variant/*vector3f,vector4f,matrix4x4f*/>; + std::variant /*vector3f,vector4f,matrix4x4f*/>; using uniform_entry_t = std::tuple; using uniform_cache_t = std::vector; diff --git a/src/lib/include/paradiso/sprite.hpp b/src/lib/include/paradiso/sprite.hpp index a06c6a3..34a024b 100644 --- a/src/lib/include/paradiso/sprite.hpp +++ b/src/lib/include/paradiso/sprite.hpp @@ -23,8 +23,8 @@ #ifndef PARADISO_SPRITE_HPP #define PARADISO_SPRITE_HPP -#include #include +#include #include #include @@ -32,13 +32,15 @@ namespace paradiso { /** * @brief simple sprite handler -*/ + */ struct Sprite final { using ChangeCountType = std::uint64_t; - Bitmap bitmap{}; //!< associated bitmap + Bitmap bitmap{}; //!< associated bitmap Vector2 pivot{Vector2::zero()}; //!< center point + Vector2 scale{Vector2::all(1)}; //!< scale + float rotation{0.0f}; //!< rotation // 0 3 | y // +------+ | ^ @@ -49,17 +51,20 @@ struct Sprite final { std::array indices{0, 1, 2, 0, 2, 3}; //!< topology - std::array, 4> vertices{ //!< geometry + std::array, 4> vertices{ + //!< geometry Vector3::make(-1.0f, +1.0f, 0.0f), Vector3::make(-1.0f, -1.0f, 0.0f), Vector3::make(+1.0f, -1.0f, 0.0f), Vector3::make(+1.0f, +1.0f, 0.0f)}; - std::array, 4> normals{ //!< normals + std::array, 4> normals{ + //!< normals Vector3::z_axis(), Vector3::z_axis(), Vector3::z_axis(), Vector3::z_axis()}; - std::array, 4> texture_coordinates{ //!< UV coordinates + std::array, 4> texture_coordinates{ + //!< UV coordinates Vector2::make(0.0f, 0.0f), Vector2::make(1.0f, 0.0f), Vector2::make(1.0f, 1.0f), Vector2::make(0.0f, 1.0f)}; diff --git a/src/lib/src/shader.cpp b/src/lib/src/shader.cpp index 14a613b..47d5070 100644 --- a/src/lib/src/shader.cpp +++ b/src/lib/src/shader.cpp @@ -199,12 +199,12 @@ Shader::~Shader() {} bool Shader::ready() const { return impl_->is_valid(); } -Shader& Shader::set_uniform_at_location(int location, float v) { +const Shader& Shader::set_uniform_at_location(int location, float v) const { impl_->bind(location, v); return *this; } -Shader& Shader::set_uniform_at_location(int location, uint32_t v) { +const Shader& Shader::set_uniform_at_location(int location, uint32_t v) const { impl_->bind(location, v); return *this; } @@ -214,8 +214,8 @@ const Shader& Shader::set_uniform_at_location(int location, int32_t v) const { return *this; } -const Shader &Shader::set_uniform_at_location(int location, Vector2 const &v) const -{ +const Shader& Shader::set_uniform_at_location(int location, + Vector2 const& v) const { impl_->bind(location, v); return *this; }