not a good idea yet

This commit is contained in:
Hartmut Seichter 2023-07-01 15:21:44 +02:00
parent 1a5287bbc2
commit 296d3add7b
4 changed files with 59 additions and 22 deletions

View file

@ -24,13 +24,15 @@ layout (location = 0) in vec3 vertices;
layout (location = 1) in vec3 normals;
layout (location = 2) in vec2 texture_coords;
uniform vec4 pivot = vec4( 0.5, 0.5, 0.0, 1.0 );
uniform vec2 pivot = vec2( 0.0, 0.0 );
vec4 pivot_3d = vec4( pivot , 0.0, 1.0 );
out vec2 tex_c;
void main() {
tex_c = texture_coords;
gl_Position = vec4(vertices, 1.0) + pivot;
gl_Position = vec4(vertices, 1.0) + pivot_3d;
}
)";
@ -94,13 +96,21 @@ auto main() -> int {
// eine sehr rudimentäre Eingabebehandlung. Bei vorhandenen
// Eingaben landen diese hier. Wer sich am Design beteiligen
// möchte, kann hier einen eleganteren Vorschlag machen
// möchte: hier gibt es viel Potential zur Verbesserung ;)
window.set_keyboardcallback(
[&](auto& w, int key, int scancode, int action, int mods) {
if (key == 'Q' || key == 256)
if (key == 'Q' || key == 256) // Q oder ESC beenden das Programm
want_close = true;
else if (key == 'A') {
else if (key == 'B') { // kleine Spielerei
slider_value += 10;
} else if (key == 'W') {
sprite.pivot.y() += 0.1f;
} else if (key == 'S') {
sprite.pivot.y() -= 0.1f;
} else if (key == 'A') {
sprite.pivot.x() -= 0.1f;
} else if (key == 'D') {
sprite.pivot.x() += 0.1f;
}
});
@ -119,13 +129,16 @@ auto main() -> int {
// werden
ctx.set_viewport(paradiso::Rectangle{
.size =
w.client_size().minimal_extent() // wir wollen das
w.client_size().maximal_extent() // wir wollen das
// Seitenverhältnis beibehalten
});
// hier wird das eigentliche löschen des vorherigen Inhalts ausgelöst
ctx.clear();
// wir setzen den pivot der sprite über den shader
shader.set_uniform("pivot", sprite.pivot);
// Ein `renderer` kann nur mit einer Sprite verwendet werden!
// Aber ein Shader kann man für mehrere Sprite-Renderer Kombis verwenden
renderer.draw(sprite, shader);

View file

@ -27,11 +27,18 @@
namespace paradiso {
/**
* @brief position with integral type
*/
struct Point final {
using value_type = int32_t;
value_type x{0}, y{0};
};
/**
* @brief size with integral type
*/
struct Size final {
using value_type = uint32_t;
value_type width{0}, height{0};
@ -42,11 +49,20 @@ struct Size final {
return {.width = std::min(width, height),
.height = std::min(width, height)};
}
constexpr Size maximal_extent() const noexcept {
return {.width = std::max(width, height),
.height = std::max(width, height)};
}
};
struct Rectangle {
Point position;
Size size;
/**
* @brief position and size with integral type
*/
struct Rectangle final {
Point position{.x = 0, .y = 0};
Size size{};
constexpr bool contains(const Point& p) const noexcept {
return p.x >= position.x && p.x <= position.x + size.width &&

View file

@ -24,6 +24,8 @@
#define PARADISO_SHADER_HPP
#include <paradiso/globals.hpp>
#include <paradiso/matrix.hpp>
#include <paradiso/vector.hpp>
#include <memory>
#include <unordered_map>
@ -60,6 +62,9 @@ struct Shader final {
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<float>& v) const; //!< sets a 2D float vector
/**
* @brief retrieves the position of a uniform
* @param name of the uniform
@ -77,7 +82,7 @@ struct Shader final {
}
/**
* sets data of the
* sets data of the uniform inside a shader program
*/
template <typename T>
const Shader& set_uniform(std::string const& name, T&& value) const {
@ -87,7 +92,7 @@ struct Shader final {
using uniform_t =
std::variant<bool, int, float,
double /*,vector2f,vector3f,vector4f,matrix4x4f*/>;
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

@ -172,16 +172,19 @@ struct Shader::impl {
// glUniformMatrix4fv(location,1,GL_FALSE,m.ptr());
// }
// void bind(int location,const vector4f& v)
// {
// glUniform4fv(location,1,v.ptr());
// }
void bind(int location, const Vector2<float>& v) const {
glUniform2fv(location, 1, v.ptr());
}
void bind(int location, const float& v) const { glUniform1f(location, v); }
void bind(int location, const uint32_t& i) const { glUniform1ui(location, i); }
void bind(int location, const uint32_t& i) const {
glUniform1ui(location, i);
}
void bind(int location, const int32_t& i) const { glUniform1i(location, i); }
void bind(int location, const int32_t& i) const {
glUniform1i(location, i);
}
// void bind(int location,const texture& v)
// {
@ -211,11 +214,11 @@ const Shader& Shader::set_uniform_at_location(int location, int32_t v) const {
return *this;
}
// Shader &Shader::set_uniform_at_location(int location, vector4f const &v)
// {
// impl_->bind(location, v);
// return *this;
// }
const Shader &Shader::set_uniform_at_location(int location, Vector2<float> const &v) const
{
impl_->bind(location, v);
return *this;
}
// Shader &Shader::set_uniform_at_location(int location, matrix4x4f const &v)
// {