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) {
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

View file

@ -67,12 +67,15 @@ template <typename Scalar, typename Derived> struct MatrixBase {
return derived();
}
static constexpr Derived zero() noexcept {
static constexpr Derived
all(const std::convertible_to<Scalar> 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()); }

View file

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

View file

@ -23,8 +23,8 @@
#ifndef PARADISO_SPRITE_HPP
#define PARADISO_SPRITE_HPP
#include <paradiso/globals.hpp>
#include <paradiso/bitmap.hpp>
#include <paradiso/globals.hpp>
#include <paradiso/matrix.hpp>
#include <paradiso/vector.hpp>
@ -32,13 +32,15 @@ namespace paradiso {
/**
* @brief simple sprite handler
*/
*/
struct Sprite final {
using ChangeCountType = std::uint64_t;
Bitmap bitmap{}; //!< associated bitmap
Vector2<float> pivot{Vector2<float>::zero()}; //!< center point
Vector2<float> scale{Vector2<float>::all(1)}; //!< scale
float rotation{0.0f}; //!< rotation
// 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<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)};
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()};
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(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(); }
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<float> const &v) const
{
const Shader& Shader::set_uniform_at_location(int location,
Vector2<float> const& v) const {
impl_->bind(location, v);
return *this;
}