summaryrefslogtreecommitdiffhomepage
path: root/shaders
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2022-10-02 15:29:41 +0200
committerStanislaw Halik <sthalik@misaki.pl>2022-10-02 15:29:41 +0200
commit445caf6184cafbce361670ea6028ff76317976bc (patch)
tree0ff8c2c33211f7a738de8f6f5d6cec79aa776f36 /shaders
parentf339fa747348e742859701331b529f86d7bd8453 (diff)
a
Diffstat (limited to 'shaders')
-rw-r--r--shaders/tile-shader.cpp54
-rw-r--r--shaders/tile-shader.hpp41
2 files changed, 95 insertions, 0 deletions
diff --git a/shaders/tile-shader.cpp b/shaders/tile-shader.cpp
new file mode 100644
index 00000000..835f94a0
--- /dev/null
+++ b/shaders/tile-shader.cpp
@@ -0,0 +1,54 @@
+#include "shaders/tile-shader.hpp"
+#include "loader.hpp"
+#include <algorithm>
+#include <Corrade/Containers/Reference.h>
+#include <Corrade/Utility/Resource.h>
+#include <Magnum/GL/Context.h>
+#include <Magnum/GL/Shader.h>
+#include <Magnum/GL/Version.h>
+
+namespace Magnum::Examples {
+
+tile_shader::tile_shader()
+{
+ MAGNUM_ASSERT_GL_VERSION_SUPPORTED(GL::Version::GL460);
+
+ GL::Shader vert{GL::Version::GL460, GL::Shader::Type::Vertex};
+ GL::Shader frag{GL::Version::GL460, GL::Shader::Type::Fragment};
+
+ vert.addSource(loader.shader("shaders/tile-shader.vert"));
+ frag.addSource(loader.shader("shaders/tile-shader.frag"));
+
+#ifdef __clang__
+# pragma clang diagnostic push
+# pragma clang diagnostic ignored "-Wdeprecated-declarations"
+#endif
+ CORRADE_INTERNAL_ASSERT_OUTPUT(GL::Shader::compile({vert, frag}));
+#ifdef __clang__
+# pragma clang diagnostic pop
+#endif
+ attachShaders({vert, frag});
+
+ CORRADE_INTERNAL_ASSERT_OUTPUT(link());
+
+ set_scale({640, 480});
+ set_camera_offset({0, 0});
+}
+
+tile_shader& tile_shader::set_scale(const Vector2& scale)
+{
+ scale_ = scale;
+ setUniform(ScaleUniform, scale);
+ return *this;
+}
+
+tile_shader& tile_shader::set_camera_offset(Vector2 camera_offset)
+{
+ CORRADE_INTERNAL_ASSERT(std::fabs(camera_offset[0]) <= std::scalbn(1.f, std::numeric_limits<float>::digits));
+ CORRADE_INTERNAL_ASSERT(std::fabs(camera_offset[1]) <= std::scalbn(1.f, std::numeric_limits<float>::digits));
+ camera_offset_ = camera_offset;
+ setUniform(OffsetUniform, camera_offset);
+ return *this;
+}
+
+} // namespace Magnum::Examples
diff --git a/shaders/tile-shader.hpp b/shaders/tile-shader.hpp
new file mode 100644
index 00000000..a1975daf
--- /dev/null
+++ b/shaders/tile-shader.hpp
@@ -0,0 +1,41 @@
+#pragma once
+#include "tile-atlas.hpp"
+#include <vector>
+#include <utility>
+#include <Magnum/GL/AbstractShaderProgram.h>
+#include <Magnum/GL/Texture.h>
+#include <Magnum/Math/Color.h>
+#include <Magnum/Math/Vector2.h>
+#include <Magnum/Math/Matrix4.h>
+
+struct tile_atlas;
+
+namespace Magnum::Examples {
+
+struct tile_shader : GL::AbstractShaderProgram
+{
+ typedef GL::Attribute<0, Vector3> Position;
+ typedef GL::Attribute<1, Vector2> TextureCoordinates;
+
+ explicit tile_shader();
+
+ Vector2 scale() const { return scale_; }
+ tile_shader& set_scale(const Vector2& scale);
+ Vector2 camera_offset() const { return camera_offset_; }
+ tile_shader& set_camera_offset(Vector2 camera_offset);
+
+ static inline Vector2 project(Vector3 pt);
+
+private:
+ Vector2 scale_, camera_offset_;
+
+ enum { ScaleUniform = 0, OffsetUniform = 1, };
+};
+
+Vector2 tile_shader::project(Vector3 pt)
+{
+ float x = pt[1], y = pt[0], z = pt[2];
+ return { x-y, (x+y+z*2)*.75f };
+}
+
+} // namespace Magnum::Examples