all important components are rewired and can be used

This commit is contained in:
Hartmut Seichter 2023-07-01 16:07:28 +02:00
parent 296d3add7b
commit 14343e7fd0
5 changed files with 70 additions and 37 deletions

View file

@ -18,36 +18,56 @@
void setup_shaders(paradiso::Shader& shader) { void setup_shaders(paradiso::Shader& shader) {
const auto unlit_v = R"( const auto unlit_v = R"(
#version 330 core #version 400 core
layout (location = 0) in vec3 vertices; layout (location = 0) in vec3 vertices;
layout (location = 1) in vec3 normals; layout (location = 1) in vec3 normals;
layout (location = 2) in vec2 texture_coords; layout (location = 2) in vec2 texture_coords;
// pivot der sprite
uniform vec2 pivot = vec2( 0.0, 0.0 ); 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() { void main() {
tex_c = texture_coords; tex_c = texture_coords; // umstaendlich aber notwendig
gl_Position = vec4(vertices, 1.0) + pivot_3d; gl_Position = mm * mr * vec4(vertices, 1.0); // unsere eigentliche shader position
} }
)"; )";
const auto unlit_f = R"( 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; // hier ist unsere sprite textur (bitmap)
uniform sampler2D tex_color;
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() { 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); shader.set_source(paradiso::Shader::Type::Vertex, unlit_v);
@ -111,6 +131,10 @@ auto main() -> int {
sprite.pivot.x() -= 0.1f; sprite.pivot.x() -= 0.1f;
} else if (key == 'D') { } else if (key == 'D') {
sprite.pivot.x() += 0.1f; 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 // hier wird das eigentliche löschen des vorherigen Inhalts ausgelöst
ctx.clear(); 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("pivot", sprite.pivot);
shader.set_uniform("scale", sprite.scale);
shader.set_uniform("rotation", sprite.rotation);
// Ein `renderer` kann nur mit einer Sprite verwendet werden! // Ein `renderer` kann nur mit einer Sprite verwendet werden!
// Aber ein Shader kann man für mehrere Sprite-Renderer Kombis verwenden // Aber ein Shader kann man für mehrere Sprite-Renderer Kombis verwenden

View file

@ -67,12 +67,15 @@ template <typename Scalar, typename Derived> struct MatrixBase {
return derived(); return derived();
} }
static constexpr Derived zero() noexcept { static constexpr Derived
all(const std::convertible_to<Scalar> auto& v) noexcept {
Derived d; Derived d;
d.fill(0); d.fill(v);
return d; return d;
} }
static constexpr Derived zero() noexcept { return Derived::all(Scalar{0}); }
constexpr Scalar squared_norm() const { return dot(*this, *this); } constexpr Scalar squared_norm() const { return dot(*this, *this); }
constexpr Scalar norm() const { return std::sqrt(squared_norm()); } constexpr Scalar norm() const { return std::sqrt(squared_norm()); }

View file

@ -51,19 +51,18 @@ struct Shader final {
void use() const; 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& const Shader&
set_uniform_at_location(int location, set_uniform_at_location(int location,
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 int32_t v) const; //!< sets a 32bit signed in a shader
const Shader& const Shader& set_uniform_at_location(
set_uniform_at_location(int location, int location,
const Vector2<float>& v) const; //!< sets a 2D float vector const Vector2<float>& v) const; //!< sets a 2D float vector
/** /**
* @brief retrieves the position of a uniform * @brief retrieves the position of a uniform
@ -91,8 +90,8 @@ struct Shader final {
} }
using uniform_t = using uniform_t =
std::variant<bool, int, float, std::variant<bool, int, float, double,
double ,Vector2<float>/*vector3f,vector4f,matrix4x4f*/>; Vector2<float> /*vector3f,vector4f,matrix4x4f*/>;
using uniform_entry_t = std::tuple<std::string, uniform_t, int>; using uniform_entry_t = std::tuple<std::string, uniform_t, int>;
using uniform_cache_t = std::vector<uniform_entry_t>; using uniform_cache_t = std::vector<uniform_entry_t>;

View file

@ -23,8 +23,8 @@
#ifndef PARADISO_SPRITE_HPP #ifndef PARADISO_SPRITE_HPP
#define PARADISO_SPRITE_HPP #define PARADISO_SPRITE_HPP
#include <paradiso/globals.hpp>
#include <paradiso/bitmap.hpp> #include <paradiso/bitmap.hpp>
#include <paradiso/globals.hpp>
#include <paradiso/matrix.hpp> #include <paradiso/matrix.hpp>
#include <paradiso/vector.hpp> #include <paradiso/vector.hpp>
@ -32,13 +32,15 @@ namespace paradiso {
/** /**
* @brief simple sprite handler * @brief simple sprite handler
*/ */
struct Sprite final { struct Sprite final {
using ChangeCountType = std::uint64_t; using ChangeCountType = std::uint64_t;
Bitmap bitmap{}; //!< associated bitmap Bitmap bitmap{}; //!< associated bitmap
Vector2<float> pivot{Vector2<float>::zero()}; //!< center point Vector2<float> pivot{Vector2<float>::zero()}; //!< center point
Vector2<float> scale{Vector2<float>::all(1)}; //!< scale
float rotation{0.0f}; //!< rotation
// 0 3 | y // 0 3 | y
// +------+ | ^ // +------+ | ^
@ -49,17 +51,20 @@ struct Sprite final {
std::array<std::uint32_t, 6> indices{0, 1, 2, 0, 2, 3}; //!< topology std::array<std::uint32_t, 6> indices{0, 1, 2, 0, 2, 3}; //!< topology
std::array<Vector3<float>, 4> vertices{ //!< geometry std::array<Vector3<float>, 4> vertices{
//!< geometry
Vector3<float>::make(-1.0f, +1.0f, 0.0f), Vector3<float>::make(-1.0f, +1.0f, 0.0f),
Vector3<float>::make(-1.0f, -1.0f, 0.0f), Vector3<float>::make(-1.0f, -1.0f, 0.0f),
Vector3<float>::make(+1.0f, -1.0f, 0.0f), Vector3<float>::make(+1.0f, -1.0f, 0.0f),
Vector3<float>::make(+1.0f, +1.0f, 0.0f)}; Vector3<float>::make(+1.0f, +1.0f, 0.0f)};
std::array<Vector3<float>, 4> normals{ //!< normals std::array<Vector3<float>, 4> normals{
//!< normals
Vector3<float>::z_axis(), Vector3<float>::z_axis(), Vector3<float>::z_axis(), Vector3<float>::z_axis(),
Vector3<float>::z_axis(), Vector3<float>::z_axis()}; Vector3<float>::z_axis(), Vector3<float>::z_axis()};
std::array<Vector2<float>, 4> texture_coordinates{ //!< UV coordinates std::array<Vector2<float>, 4> texture_coordinates{
//!< UV coordinates
Vector2<float>::make(0.0f, 0.0f), Vector2<float>::make(1.0f, 0.0f), Vector2<float>::make(0.0f, 0.0f), Vector2<float>::make(1.0f, 0.0f),
Vector2<float>::make(1.0f, 1.0f), Vector2<float>::make(0.0f, 1.0f)}; Vector2<float>::make(1.0f, 1.0f), Vector2<float>::make(0.0f, 1.0f)};

View file

@ -199,12 +199,12 @@ Shader::~Shader() {}
bool Shader::ready() const { return impl_->is_valid(); } 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); impl_->bind(location, v);
return *this; 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); impl_->bind(location, v);
return *this; return *this;
} }
@ -214,8 +214,8 @@ const Shader& Shader::set_uniform_at_location(int location, int32_t v) const {
return *this; return *this;
} }
const Shader &Shader::set_uniform_at_location(int location, Vector2<float> const &v) const const Shader& Shader::set_uniform_at_location(int location,
{ Vector2<float> const& v) const {
impl_->bind(location, v); impl_->bind(location, v);
return *this; return *this;
} }