From 3550a1170b354b3f7dd92df038f6e6d7c8e0dd7f Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Thu, 17 Feb 2022 22:26:16 +0100 Subject: initial import --- TexturedQuadExample.cpp | 95 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 TexturedQuadExample.cpp (limited to 'TexturedQuadExample.cpp') diff --git a/TexturedQuadExample.cpp b/TexturedQuadExample.cpp new file mode 100644 index 00000000..ec166d6e --- /dev/null +++ b/TexturedQuadExample.cpp @@ -0,0 +1,95 @@ +#include "chunk.hpp" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "TexturedQuadShader.h" + +namespace Magnum::Examples { + +struct TexturedQuadExample: Platform::Application { + explicit TexturedQuadExample(const Arguments& arguments); + void drawEvent() override; + + const Utility::Resource rs{"texturedquad-data"}; + PluginManager::Manager plugins; + Containers::Pointer tga_importer = + plugins.loadAndInstantiate("TgaImporter"); + + GL::Mesh _mesh; + TexturedQuadShader _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 image = tga_importer->image2D(0); + CORRADE_INTERNAL_ASSERT(image); + + return atlas_texture{*image, dims}; + } +}; + +TexturedQuadExample::TexturedQuadExample(const Arguments& arguments): + Platform::Application{arguments, Configuration{} + .setTitle("Magnum Textured Quad Example") + .setSize({512, 512})} +{ + struct QuadVertex { + Vector3 position; + Vector2 textureCoordinates; + }; + QuadVertex vertices[4]; + auto positions = atlas.floor_quad({}, {2, 2}); + auto texcoords = atlas.texcoords_for_id(2); + auto indices = atlas.indices(0); + + for (unsigned i = 0; i < std::size(vertices); i++) + vertices[i] = { positions[i], texcoords[i] }; + + _mesh.setCount(std::size(indices)) + .addVertexBuffer(GL::Buffer{vertices}, 0, + TexturedQuadShader::Position{}, + TexturedQuadShader::TextureCoordinates{}) + .setIndexBuffer(GL::Buffer{indices}, 0, + GL::MeshIndexType::UnsignedShort); +} + +void TexturedQuadExample::drawEvent() { + GL::defaultFramebuffer.clear(GL::FramebufferClear::Color); + + using namespace Math::Literals; + + _shader + .setColor(0xffffff_rgbf) + .bindTexture(atlas.texture()) + .draw(_mesh); + + swapBuffers(); +} + +} // namespace Magnum::Examples + +MAGNUM_APPLICATION_MAIN(Magnum::Examples::TexturedQuadExample); + +#include + +#if defined _MSC_VER +int __stdcall WinMain(HINSTANCE, HINSTANCE, LPSTR, int /* nCmdShow */) +{ + return main(__argc, __argv); +} +#endif -- cgit v1.2.3