diff options
| -rw-r--r-- | CMakeLists.txt | 1 | ||||
| -rw-r--r-- | main.cpp | 56 | ||||
| -rw-r--r-- | shaders/tile-shader.vert | 3 | ||||
| -rw-r--r-- | tile-shader.cpp | 7 | ||||
| -rw-r--r-- | tile-shader.hpp | 14 |
5 files changed, 55 insertions, 26 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 0dedbd3f..db5ac019 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -56,7 +56,6 @@ add_executable(magnum-triangle WIN32 "${sources}" "${TexturedQuad_RESOURCES}") target_link_libraries(magnum-triangle PRIVATE Magnum::Application Magnum::GL - Magnum::WglContext Magnum::Magnum Magnum::Shaders Magnum::Trade) @@ -1,4 +1,5 @@ #include "atlas.hpp" +#include "tile-shader.hpp" #include <Corrade/Containers/Optional.h> #include <Corrade/Containers/ArrayViewStl.h> @@ -8,16 +9,21 @@ #include <Magnum/GL/Buffer.h> #include <Magnum/GL/DefaultFramebuffer.h> #include <Magnum/GL/Mesh.h> +#include <Magnum/GL/Renderer.h> #include <Magnum/GL/TextureFormat.h> #include <Magnum/Platform/Sdl2Application.h> #include <Magnum/Trade/AbstractImporter.h> #include <Magnum/Trade/ImageData.h> -#include "tile-shader.hpp" +#include <Magnum/GlmIntegration/Integration.h> +#include <glm/glm.hpp> +#include <glm/ext/matrix_transform.hpp> +#include <glm/ext/matrix_clip_space.hpp> namespace Magnum::Examples { -struct application : Platform::Application { +struct application final : Platform::Application +{ explicit application(const Arguments& arguments); void drawEvent() override; @@ -30,16 +36,8 @@ struct application : Platform::Application { tile_shader _shader; atlas_texture atlas = make_atlas("images/tiles.tga", {8, 4}); - atlas_texture make_atlas(const std::string& file, Vector2i dims) - { - if(!tga_importer || !tga_importer->openFile(file)) - std::exit(1); - - Containers::Optional<Trade::ImageData2D> image = tga_importer->image2D(0); - CORRADE_INTERNAL_ASSERT(image); - - return atlas_texture{*image, dims}; - } + atlas_texture make_atlas(const std::string& file, Vector2i dims); + Matrix4x4 make_projection(Vector3 offset); }; application::application(const Arguments& arguments): @@ -50,9 +48,10 @@ application::application(const Arguments& arguments): struct QuadVertex { Vector3 position; Vector2 textureCoordinates; + // todo gl_FragDepth }; QuadVertex vertices[4]; - auto positions = atlas.floor_quad({}, {2, 2}); + auto positions = atlas.floor_quad({}, {48, 48}); auto texcoords = atlas.texcoords_for_id(2); auto indices = atlas.indices(0); @@ -66,18 +65,45 @@ application::application(const Arguments& arguments): } void application::drawEvent() { - GL::defaultFramebuffer.clear(GL::FramebufferClear::Color); + GL::defaultFramebuffer.clear(GL::FramebufferClear::Color | GL::FramebufferClear::Depth); + + //GL::Renderer::enable(GL::Renderer::Feature::DepthTest); + //GL::Renderer::setDepthMask(true); using namespace Math::Literals; _shader - .setColor(0xffffff_rgbf) + .set_projection(make_projection({0, 0, 0})) + .set_color(0xffffff_rgbf) .bindTexture(atlas.texture()) .draw(_mesh); swapBuffers(); } +atlas_texture application::make_atlas(const std::string& file, Vector2i dims) +{ + if(!tga_importer || !tga_importer->openFile(file)) + std::exit(1); + + Containers::Optional<Trade::ImageData2D> image = tga_importer->image2D(0); + CORRADE_INTERNAL_ASSERT(image); + + return atlas_texture{*image, dims}; +} + +Matrix4x4 application::make_projection(Vector3 offset) +{ + auto m = glm::mat4{1}; + m = glm::ortho<float>(-256, 256, -256, 256, -512, 512); + m = glm::translate(m, { offset[0], -offset[1], offset[2] }); + m = glm::scale(m, { 1.f, 0.6f, 1.f }); + m = glm::rotate(m, glm::radians(-45.f), glm::vec3(1.0f, 0.0f, 0.0f)); + m = glm::rotate(m, glm::radians(0.0f), glm::vec3(0.0f, 1.0f, 0.0f)); + m = glm::rotate(m, glm::radians(-45.0f), glm::vec3(0.0f, 0.0f, 1.0f)); + return Matrix4x4{m}; +} + } // namespace Magnum::Examples MAGNUM_APPLICATION_MAIN(Magnum::Examples::application); diff --git a/shaders/tile-shader.vert b/shaders/tile-shader.vert index 3c5c7c68..670015b1 100644 --- a/shaders/tile-shader.vert +++ b/shaders/tile-shader.vert @@ -1,10 +1,11 @@ layout(location = 0) in vec4 position; layout(location = 1) in vec2 textureCoordinates; +uniform mat4 projection; out vec2 interpolatedTextureCoordinates; void main() { interpolatedTextureCoordinates = textureCoordinates; - gl_Position = position; + gl_Position = projection * position; } diff --git a/tile-shader.cpp b/tile-shader.cpp index 2e92cfc1..22cad2fa 100644 --- a/tile-shader.cpp +++ b/tile-shader.cpp @@ -37,7 +37,8 @@ namespace Magnum::Examples { -tile_shader::tile_shader() { +tile_shader::tile_shader() +{ MAGNUM_ASSERT_GL_VERSION_SUPPORTED(GL::Version::GL460); const Utility::Resource rs{"texturedquad-data"}; @@ -54,7 +55,9 @@ tile_shader::tile_shader() { CORRADE_INTERNAL_ASSERT_OUTPUT(link()); - _colorUniform = uniformLocation("color"); + _color_uniform = uniformLocation("color"); + _projection_uniform = uniformLocation("projection"); + setUniform(uniformLocation("textureData"), TextureUnit); } diff --git a/tile-shader.hpp b/tile-shader.hpp index 68a2c0e5..03c32cc5 100644 --- a/tile-shader.hpp +++ b/tile-shader.hpp @@ -31,20 +31,20 @@ #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> namespace Magnum::Examples { -class tile_shader : public GL::AbstractShaderProgram { -public: +struct tile_shader : GL::AbstractShaderProgram +{ typedef GL::Attribute<0, Vector3> Position; typedef GL::Attribute<1, Vector2> TextureCoordinates; explicit tile_shader(); - tile_shader& setColor(const Color3& color) { - setUniform(_colorUniform, color); - return *this; - } + auto& set_color(const Color3& color) { setUniform(_color_uniform, color); return *this; } + auto& set_projection(const Math::Matrix4<float>& mat) { setUniform(_projection_uniform, mat); return *this; } tile_shader& bindTexture(GL::Texture2D& texture) { texture.bind(TextureUnit); @@ -54,7 +54,7 @@ public: private: enum: Int { TextureUnit = 0 }; - Int _colorUniform; + Int _color_uniform, _projection_uniform; }; } // namespace Magnum::Examples |
