diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2022-10-25 16:41:21 +0200 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2022-10-25 16:41:21 +0200 |
commit | 0d97856bf6974450a8e72816be7bf271af04a458 (patch) | |
tree | a503cf7d7330970b468310a9c05860b8fbc7f3e4 /shaders/tile.hpp | |
parent | f1df7c20129a1f1cc47c47e5731efd10f8e49aeb (diff) |
rename shader
Diffstat (limited to 'shaders/tile.hpp')
-rw-r--r-- | shaders/tile.hpp | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/shaders/tile.hpp b/shaders/tile.hpp new file mode 100644 index 00000000..bbe57e52 --- /dev/null +++ b/shaders/tile.hpp @@ -0,0 +1,66 @@ +#pragma once +#include "compat/defs.hpp" +#include <Magnum/GL/AbstractShaderProgram.h> +#include <Magnum/Math/Vector2.h> +#include <Magnum/Math/Vector3.h> +#include <Magnum/Math/Vector4.h> + +namespace floormat { + +struct tile_shader : GL::AbstractShaderProgram +{ + typedef GL::Attribute<0, Vector3> Position; + typedef GL::Attribute<1, Vector2> TextureCoordinates; + + fm_DECLARE_DEFAULT_MOVE_ASSIGNMENT_(tile_shader); + fm_DECLARE_DELETED_COPY_ASSIGNMENT(tile_shader); + + explicit tile_shader(); + ~tile_shader() override; + + Vector2 scale() const { return _scale; } + tile_shader& set_scale(const Vector2& scale); + Vector2d camera_offset() const { return _camera_offset; } + tile_shader& set_camera_offset(Vector2d camera_offset); + Vector4 tint() const { return _tint; } + tile_shader& set_tint(const Vector4& tint); + + static constexpr Vector2d project(Vector3d pt); + static constexpr Vector2d unproject(Vector2d px); + + template<typename T, typename... Xs> + auto draw(T&& mesh, Xs&&... xs) -> + decltype(GL::AbstractShaderProgram::draw(std::forward<T>(mesh), std::forward<Xs>(xs)...)); + +private: + void _draw(); + + Vector2d _camera_offset; + Vector4 _tint; + Vector2 _scale; + Vector2 _real_camera_offset; + + enum { ScaleUniform = 0, OffsetUniform = 1, TintUniform = 2, }; +}; + +template<typename T, typename... Xs> +auto tile_shader::draw(T&& mesh, Xs&&... xs) -> + decltype(GL::AbstractShaderProgram::draw(std::forward<T>(mesh), std::forward<Xs>(xs)...)) +{ + _draw(); + return GL::AbstractShaderProgram::draw(std::forward<T>(mesh), std::forward<Xs>(xs)...); +} + +constexpr Vector2d tile_shader::project(const Vector3d pt) +{ + const auto x = pt[0], y = pt[1], z = pt[2]; + return { (x-y), (x+y+z*2)*.59 }; +} + +constexpr Vector2d tile_shader::unproject(const Vector2d px) +{ + const auto X = px[0], Y = px[1]; + return { X + 100 * Y / 59, 100 * Y / 59 - X }; +} + +} // namespace floormat |