Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Corrade/Containers/GrowableArray.h>
- #include <Corrade/Utility/Arguments.h>
- #include <Magnum/GL/Context.h>
- #include <Magnum/GL/DefaultFramebuffer.h>
- #include <Magnum/GL/Buffer.h>
- #include <Magnum/GL/Mesh.h>
- #include <Magnum/Math/ConfigurationValue.h>
- #include <Magnum/Math/DualComplex.h>
- #include <Magnum/MeshTools/Compile.h>
- #include <Magnum/Platform/Sdl2Application.h>
- #include <Magnum/Primitives/Square.h>
- #include <Magnum/Primitives/Plane.h>
- #include <Magnum/Primitives/Cube.h>
- #include <Magnum/SceneGraph/Camera.h>
- #include <Magnum/SceneGraph/Drawable.h>
- #include <Magnum/SceneGraph/TranslationRotationScalingTransformation2D.h>
- #include <Magnum/SceneGraph/TranslationRotationScalingTransformation3D.h>
- #include <Magnum/SceneGraph/Scene.h>
- #include <Magnum/Shaders/FlatGL.h>
- #include <Magnum/Trade/MeshData.h>
- #include <Magnum/GL/Texture.h>
- #include <Magnum/GL/TextureFormat.h>
- #include <Magnum/ResourceManager.h>
- #include <Magnum/Trade/AbstractImporter.h>
- #include <Magnum/Trade/ImageData.h>
- #include <Magnum/ImageView.h>
- #include <Magnum/GL/Buffer.h>
- #include <Magnum/GL/DefaultFramebuffer.h>
- #include <Magnum/GL/BufferImage.h>
- #include <Magnum/DebugTools/ResourceManager.h>
- #include <Magnum/DebugTools/ObjectRenderer.h>
- #include <Magnum/GL/Renderer.h>
- #include <Magnum/Shaders/PhongGL.h>
- #include <utility>
- #include <iostream>
- #include <map>
- std::string imagepath = R"(C:\IMAGEPATHHERE\)";
- using namespace Magnum;
- DebugTools::ResourceManager manager;
- SceneGraph::DrawableGroup3D debugDrawables;
- using Scene3D = SceneGraph::Scene<SceneGraph::TranslationRotationScalingTransformation3D>;
- using Object3D = SceneGraph::Object<SceneGraph::TranslationRotationScalingTransformation3D>;
- using namespace Math::Literals;
- std::map<std::string, Vector3i> pngpos = {{"001.png", {376, 218, 0}},{"002.png", {385, 418, 0}},{"003.png", {332, 375, 0}},{"004.png", {430, 383, 0}},{"005.png", {341, 566, 0}},{"006.png", {438, 563, 0}},};
- static constexpr unsigned int DimensionsCount = 3;
- using TileCoord = int;
- using TilePosition = Vector3i;
- static constexpr TilePosition RegionDimensions = {10, 10, 5};
- class Sprite;
- class SpriteGroup;
- class Rock;
- struct TestGameObject
- {
- std::string png;
- Vector3i pos;
- Sprite* sprite = nullptr; //two way
- explicit TestGameObject(const std::string& png) : png(png), pos(pngpos[png]) {}
- };
- struct TestRootObject
- {
- TilePosition pos;
- std::vector<TestGameObject*> objects;
- SpriteGroup* spritegroup = nullptr; //two way
- explicit TestRootObject(TilePosition pos) : pos(pos) {}
- };
- struct TestTile
- {
- std::vector<TestRootObject*> roots;
- bool hasrock = false;
- TilePosition pos;
- Rock* rock = nullptr; //two way
- };
- class TestRegion
- {
- std::array<TestTile, RegionDimensions[0] * RegionDimensions[1] * RegionDimensions[2]> tiles;
- inline bool InBounds(TilePosition position)
- {
- return !(position[0] > RegionDimensions[0] || position[1] > RegionDimensions[1] || position[2] > RegionDimensions[2]);
- }
- void Populate()
- {
- static constexpr TileCoord FloorHeight = 1;
- for (TileCoord z = FloorHeight; z < RegionDimensions[2]; ++z)
- {
- for (TileCoord x = 0; x < RegionDimensions[0]; ++x)
- {
- (*this)[{x, 0, z}].hasrock = true;
- (*this)[{x, RegionDimensions[1] - 1, z}].hasrock = true;
- }
- for (TileCoord y = 0; y < RegionDimensions[1]; ++y)
- {
- (*this)[{0, y, z}].hasrock = true;
- (*this)[{RegionDimensions[0] - 1, y, z}].hasrock = true;
- }
- }
- for (TileCoord z = 0; z < FloorHeight; ++z)
- {
- for (TileCoord x = 0; x < RegionDimensions[0]; ++x)
- {
- for (TileCoord y = 0; y < RegionDimensions[1]; ++y)
- {
- (*this)[{x, y, z}].hasrock = true;
- }
- }
- }
- for (TileCoord z = 0; z < RegionDimensions[2]; ++z)
- {
- for (TileCoord x = 0; x < RegionDimensions[0]; ++x)
- {
- for (TileCoord y = 0; y < RegionDimensions[1]; ++y)
- {
- (*this)[{x, y, z}].pos = {x, y, z};
- }
- }
- }
- Vector3i minpos{9999999, 9999999, 0};
- Vector3i maxpos{-9999999, -9999999, 0};
- for (auto& pair : pngpos)
- {
- pair.second *= Vector3i{1, -1, 1};
- if (pair.second[0] < minpos[0]) minpos[0] = pair.second[0];
- if (pair.second[1] < minpos[1]) minpos[1] = pair.second[1];
- if (pair.second[0] > maxpos[0]) maxpos[0] = pair.second[0];
- if (pair.second[1] > maxpos[1]) maxpos[1] = pair.second[1];
- }
- Vector3i midpoint = (maxpos + minpos) / 2;
- for (auto& pair : pngpos) pair.second -= midpoint;
- // for (auto& pair : pngpos)
- // {
- // Debug() << pair.first << ": " << pair.second << '\n';
- // }
- TilePosition start = {RegionDimensions[0] / 2, RegionDimensions[1] / 2, FloorHeight};
- auto& tile = (*this)[start];
- player = new TestRootObject(start);
- for (auto& pair : pngpos)
- {
- player->objects.push_back(new TestGameObject(pair.first));
- }
- tile.roots.push_back(player);
- }
- public:
- TestRegion()
- {
- Populate();
- }
- TestRootObject* player = nullptr;
- TestTile& operator[](TilePosition position)
- {
- if (!InBounds(position))
- {
- //overflow
- std::cout << "overflow" << std::endl;
- throw;
- }
- return tiles[position[0] + (position[1] * RegionDimensions[0]) + (position[2] * RegionDimensions[0] * RegionDimensions[1])];
- }
- };
- /*
- * https://doc.magnum.graphics/magnum/classMagnum_1_1Trade_1_1AbstractImporter.html
- * https://doc.magnum.graphics/magnum/file-formats.html
- *
- * https://doc.magnum.graphics/magnum/classMagnum_1_1Trade_1_1AbstractImporter.html#a88703d241d03b605112b2163778529b2
- * https://doc.magnum.graphics/corrade/classCorrade_1_1Containers_1_1ArrayView.html
- */
- static constexpr int TilePixelScale = 800; //400 pixels cube for now?
- static constexpr Vector3i TilePixelDimensions = {TilePixelScale, TilePixelScale, TilePixelScale};
- static constexpr Vector3i TilePixelCenter = {TilePixelDimensions[0] / 2, TilePixelDimensions[1] / 2, TilePixelDimensions[2] / 2};
- static constexpr float PixelScale = 1.0f / 1000.0f; //1 might be the entire screen width, so I have to scale down considerably
- ResourceManager<GL::Texture2D> texturemanager;
- PluginManager::Manager<Trade::AbstractImporter> importermanager;
- void LoadImage(std::string imagename)
- {
- Containers::Pointer<Trade::AbstractImporter> importer =
- importermanager.loadAndInstantiate("AnyImageImporter");
- if(!importer || !importer->openFile(imagepath + imagename))
- Fatal{} << "Can't open image.png with AnyImageImporter";
- Containers::Optional<Trade::ImageData2D> image = importer->image2D(0);
- if(!image) Fatal{} << "Importing the image failed";
- GL::Texture2D _texture;
- _texture.setWrapping(GL::SamplerWrapping::ClampToEdge)
- .setMagnificationFilter(GL::SamplerFilter::Linear)
- .setMinificationFilter(GL::SamplerFilter::Linear)
- .setStorage(1, GL::textureFormat(image->format()), image->size())
- .setSubImage(0, {}, *image);
- texturemanager.set(imagename, std::move(_texture), ResourceDataState::Final, ResourcePolicy::Resident);
- }
- auto GetImage(std::string imagename)
- {
- auto texture = Resource<GL::Texture2D>{texturemanager.get<GL::Texture2D>(imagename)};
- if (!texture) LoadImage(imagename);
- return texture;
- }
- /*
- * https://doc.magnum.graphics/magnum/classMagnum_1_1GL_1_1CubeMapTexture.html
- */
- class Rock;
- struct TileBlockVertex {
- Vector3 position;
- Vector2 textureCoordinates;
- } TileBlockVertices[]{
- {{-1.0f, -1.0f, 1.0f}, { 0.0f, 0.0f}},
- {{ 1.0f, -1.0f, 1.0f}, { 1.0f, 0.0f}},
- {{ 1.0f, 1.0f, 1.0f}, { 1.0f, 1.0f}}, /* +Z */
- {{-1.0f, 1.0f, 1.0f}, { 0.0f, 1.0f}},
- {{ 1.0f, -1.0f, 1.0f}, { 0.0f, 1.0f}},
- {{ 1.0f, -1.0f, -1.0f}, { 0.0f, 0.0f}},
- {{ 1.0f, 1.0f, -1.0f}, { 1.0f, 0.0f}}, /* +X */
- {{ 1.0f, 1.0f, 1.0f}, { 1.0f, 1.0f}},
- {{-1.0f, 1.0f, 1.0f}, { 0.0f, 1.0f}},
- {{ 1.0f, 1.0f, 1.0f}, { 1.0f, 1.0f}},
- {{ 1.0f, 1.0f, -1.0f}, { 1.0f, 0.0f}}, /* +Y */
- {{-1.0f, 1.0f, -1.0f}, { 0.0f, 0.0f}},
- {{ 1.0f, -1.0f, -1.0f}, { 1.0f, 0.0f}},
- {{-1.0f, -1.0f, -1.0f}, { 0.0f, 0.0f}},
- {{-1.0f, 1.0f, -1.0f}, { 0.0f, 1.0f}}, /* -Z */
- {{ 1.0f, 1.0f, -1.0f}, { 1.0f, 1.0f}},
- {{-1.0f, -1.0f, -1.0f}, { 0.0f, 0.0f}},
- {{ 1.0f, -1.0f, -1.0f}, { 1.0f, 0.0f}},
- {{ 1.0f, -1.0f, 1.0f}, { 1.0f, 1.0f}}, /* -Y */
- {{-1.0f, -1.0f, 1.0f}, { 0.0f, 1.0f}},
- {{-1.0f, -1.0f, -1.0f}, {0.0f, 0.0f}},
- {{-1.0f, -1.0f, 1.0f}, {0.0f, 1.0f}},
- {{-1.0f, 1.0f, 1.0f}, {1.0f, 1.0f}}, /* -X */
- {{-1.0f, 1.0f, -1.0f}, {1.0f, 0.0f}}
- };
- constexpr struct TileBlockVertex2 {
- Vector3 position;
- Vector3 normal;
- Vector2 textureCoordinates;
- } TileBlockVertices2[]{
- {{-1.0f, -1.0f, 1.0f}, { 0.0f, 0.0f, 1.0f}, { 0.0f, 0.0f}},
- {{ 1.0f, -1.0f, 1.0f}, { 0.0f, 0.0f, 1.0f}, { 1.0f, 0.0f}},
- {{ 1.0f, 1.0f, 1.0f}, { 0.0f, 0.0f, 1.0f}, { 1.0f, 1.0f}}, /* +Z */
- {{-1.0f, 1.0f, 1.0f}, { 0.0f, 0.0f, 1.0f}, { 0.0f, 1.0f}},
- {{ 1.0f, -1.0f, 1.0f}, { 1.0f, 0.0f, 0.0f}, { 0.0f, 1.0f}},
- {{ 1.0f, -1.0f, -1.0f}, { 1.0f, 0.0f, 0.0f}, { 0.0f, 0.0f}},
- {{ 1.0f, 1.0f, -1.0f}, { 1.0f, 0.0f, 0.0f}, { 1.0f, 0.0f}}, /* +X */
- {{ 1.0f, 1.0f, 1.0f}, { 1.0f, 0.0f, 0.0f}, { 1.0f, 1.0f}},
- {{-1.0f, 1.0f, 1.0f}, { 0.0f, 1.0f, 0.0f}, { 0.0f, 1.0f}},
- {{ 1.0f, 1.0f, 1.0f}, { 0.0f, 1.0f, 0.0f}, { 1.0f, 1.0f}},
- {{ 1.0f, 1.0f, -1.0f}, { 0.0f, 1.0f, 0.0f}, { 1.0f, 0.0f}}, /* +Y */
- {{-1.0f, 1.0f, -1.0f}, { 0.0f, 1.0f, 0.0f}, { 0.0f, 0.0f}},
- {{ 1.0f, -1.0f, -1.0f}, { 0.0f, 0.0f, -1.0f}, { 1.0f, 0.0f}},
- {{-1.0f, -1.0f, -1.0f}, { 0.0f, 0.0f, -1.0f}, { 0.0f, 0.0f}},
- {{-1.0f, 1.0f, -1.0f}, { 0.0f, 0.0f, -1.0f}, { 0.0f, 1.0f}}, /* -Z */
- {{ 1.0f, 1.0f, -1.0f}, { 0.0f, 0.0f, -1.0f}, { 1.0f, 1.0f}},
- {{-1.0f, -1.0f, -1.0f}, { 0.0f, -1.0f, 0.0f}, { 0.0f, 0.0f}},
- {{ 1.0f, -1.0f, -1.0f}, { 0.0f, -1.0f, 0.0f}, { 1.0f, 0.0f}},
- {{ 1.0f, -1.0f, 1.0f}, { 0.0f, -1.0f, 0.0f}, { 1.0f, 1.0f}}, /* -Y */
- {{-1.0f, -1.0f, 1.0f}, { 0.0f, -1.0f, 0.0f}, { 0.0f, 1.0f}},
- {{-1.0f, -1.0f, -1.0f}, {-1.0f, 0.0f, 0.0f}, {0.0f, 0.0f}},
- {{-1.0f, -1.0f, 1.0f}, {-1.0f, 0.0f, 0.0f}, {0.0f, 1.0f}},
- {{-1.0f, 1.0f, 1.0f}, {-1.0f, 0.0f, 0.0f}, {1.0f, 1.0f}}, /* -X */
- {{-1.0f, 1.0f, -1.0f}, {-1.0f, 0.0f, 0.0f}, {1.0f, 0.0f}}
- };
- constexpr UnsignedShort TileBlockIndices[]{
- 0, 1, 2, 0, 2, 3, /* +Z */
- 4, 5, 6, 4, 6, 7, /* +X */
- 8, 9, 10, 8, 10, 11, /* +Y */
- 12, 13, 14, 12, 14, 15, /* -Z */
- 16, 17, 18, 16, 18, 19, /* -Y */
- 20, 21, 22, 20, 22, 23 /* -X */
- };
- Trade::MeshAttributeData AttributesSolid[]{
- Trade::MeshAttributeData{Trade::MeshAttribute::Position,
- Containers::stridedArrayView(TileBlockVertices, &TileBlockVertices[0].position,
- Containers::arraySize(TileBlockVertices), sizeof(TileBlockVertex))},
- Trade::MeshAttributeData{Trade::MeshAttribute::TextureCoordinates,
- Containers::stridedArrayView(TileBlockVertices, &TileBlockVertices[0].textureCoordinates,
- Containers::arraySize(TileBlockVertices), sizeof(TileBlockVertex))}
- };
- Trade::MeshAttributeData AttributesSolid2[]{
- Trade::MeshAttributeData{Trade::MeshAttribute::Position,
- Containers::stridedArrayView(TileBlockVertices2, &TileBlockVertices2[0].position,
- Containers::arraySize(TileBlockVertices2), sizeof(TileBlockVertex2))},
- Trade::MeshAttributeData{Trade::MeshAttribute::Normal,
- Containers::stridedArrayView(TileBlockVertices2, &TileBlockVertices2[0].normal,
- Containers::arraySize(TileBlockVertices2), sizeof(TileBlockVertex2))},
- Trade::MeshAttributeData{Trade::MeshAttribute::TextureCoordinates,
- Containers::stridedArrayView(TileBlockVertices2, &TileBlockVertices2[0].textureCoordinates,
- Containers::arraySize(TileBlockVertices2), sizeof(TileBlockVertex2))}
- };
- Trade::MeshData cubeSolid() {
- return Trade::MeshData{MeshPrimitive::Triangles,
- {}, TileBlockIndices, Trade::MeshIndexData{TileBlockIndices},
- {}, TileBlockVertices, Trade::meshAttributeDataNonOwningArray(AttributesSolid)};
- }
- class TerrainCategory : public Object3D
- {
- SceneGraph::DrawableGroup3D drawables;
- Shaders::FlatGL3D shader{
- Shaders::FlatGL3D::Flag::Textured|
- Shaders::FlatGL3D::Flag::TextureTransformation};
- // Shaders::PhongGL shader{Shaders::PhongGL::Flag::DiffuseTexture};
- GL::Mesh mesh = MeshTools::compile(cubeSolid());
- public:
- explicit TerrainCategory(Scene3D& parent) : Object3D(&parent)
- {
- // mesh.addVertexBuffer(vertices, 0,
- // Shaders::PhongGL::Position{},
- // Shaders::PhongGL::Normal{},
- // Shaders::PhongGL::TextureCoordinates{})
- // .setCount(24)
- // .setIndexBuffer(indices, 0, GL::MeshIndexType::UnsignedShort, 0, 24);
- // mesh.addVertexBuffer(vertices, 0,
- // Shaders::FlatGL3D::Position{},
- // Shaders::FlatGL3D::TextureCoordinates{})
- // .setCount(24)
- // .setIndexBuffer(indices, 0, GL::MeshIndexType::UnsignedShort, 0, 24);
- }
- void draw(SceneGraph::Camera3D& camera)
- {
- camera.draw(drawables);
- }
- Rock& MakeRock(TestTile& tile);
- friend Rock;
- };
- inline Vector3i GetTileOffset(TestTile& tile)
- {
- auto tilecoordpixels = tile.pos * TilePixelScale;
- return tilecoordpixels * Vector3i{1, -1, 1}; //need to invert y axis
- }
- /* https://doc.magnum.graphics/magnum/classMagnum_1_1Shaders_1_1FlatGL.html#Shaders-FlatGL-textured
- * This is how I'll make this work, I need to change the cube mesh I use to having a 3d position and 2d texture position on each vertex
- * So I'll probably make my own custom mesh
- *
- * I'll copy and paste the array from the cube thing, I'll do texture coordinates by basically looking at the 2 dimensions in play
- * -1 turns to 0, and 1 turns to 1 for texture coordinates
- * I'll see how I want orientation stuff later
- */
- class Rock : public Object3D, public SceneGraph::Drawable3D
- {
- Resource<GL::Texture2D> texture;
- TerrainCategory& category;
- TestTile* tile = nullptr; //two way
- void draw(const Matrix4& transformation, SceneGraph::Camera3D& camera) override
- {
- auto size = Vector3(TilePixelDimensions) / 4.0f;
- // category.shader
- // .setTransformationMatrix(transformation*Matrix4::scaling(size))
- // .setProjectionMatrix(camera.projectionMatrix())
- // .bindDiffuseTexture(*texture)
- // .draw(category.mesh);
- category.shader
- .setTransformationProjectionMatrix(camera.projectionMatrix()*transformation*Matrix4::scaling(size))
- .bindTexture(*texture)
- .draw(category.mesh);
- }
- public:
- Rock(TestTile& tile, TerrainCategory& category) :
- Object3D(&category), Drawable(*this, &category.drawables), texture(GetImage("dirt.png")), category(category), tile(&tile)
- {
- this->setTranslation(Vector3(GetTileOffset(tile)));
- new DebugTools::ObjectRenderer3D{manager, *this, "my", &debugDrawables};
- }
- friend TerrainCategory;
- };
- Rock& TerrainCategory::MakeRock(TestTile& tile)
- {
- return *(new Rock(tile, *this));
- }
- class SpriteCategory : public Object3D //this will be owned by a scene, but not a real object
- {
- SceneGraph::DrawableGroup3D drawables;
- Shaders::FlatGL3D shader{
- Shaders::FlatGL3D::Flag::Textured|
- Shaders::FlatGL3D::Flag::TextureTransformation};
- GL::Mesh mesh = MeshTools::compile(Primitives::planeSolid(Primitives::PlaneFlag::TextureCoordinates));
- public:
- explicit SpriteCategory(Scene3D& parent) : Object3D(&parent)
- {
- }
- void draw(SceneGraph::Camera3D& camera)
- {
- // GL::Renderer::enable(GL::Renderer::Feature::Blending);
- camera.draw(drawables);
- // GL::Renderer::disable(GL::Renderer::Feature::Blending);
- }
- SpriteGroup& MakeGroup(TestRootObject& root);
- friend Sprite;
- friend SpriteGroup;
- };
- inline Vector3i GetOffset(TestRootObject& root)
- {
- auto tilecoordpixels = root.pos * TilePixelScale;
- return tilecoordpixels * Vector3i{1, -1, 1}; //need to invert y axis
- }
- class SpriteGroup : public Object3D
- {
- SpriteCategory& category;
- TestRootObject* root; //two way
- SpriteGroup(TestRootObject& root, SpriteCategory& category) : Object3D(&category), category(category), root(&root)
- {
- this->setTranslation(Vector3(GetOffset()));
- }
- public:
- inline Vector3i GetOffset()
- {
- return ::GetOffset(*root);
- }
- Sprite& MakeSprite(TestGameObject& object);
- friend Sprite;
- friend SpriteCategory;
- };
- SpriteGroup& SpriteCategory::MakeGroup(TestRootObject& root)
- {
- return *(new SpriteGroup(root, *this));
- }
- class Sprite : public Object3D, public SceneGraph::Drawable3D
- {
- Resource<GL::Texture2D> texture;
- SpriteGroup& group;
- TestGameObject* object; //two way
- void draw(const Matrix4& transformation, SceneGraph::Camera3D& camera) override
- {
- auto size = Vector3(Vector2(texture->imageSize(0)), 0.0f) / 2.0f;
- group.category.shader
- .setTransformationProjectionMatrix(camera.projectionMatrix()*transformation*Matrix4::scaling(size))
- .bindTexture(*texture)
- .draw(group.category.mesh);
- }
- Sprite(TestGameObject& object, SpriteGroup& group) :
- Object3D(&group), Drawable(*this, &group.category.drawables), texture(GetImage(object.png)), group(group), object(&object)
- {
- this->setTranslation(Vector3(pngpos[object.png]));
- // Debug() << object.png << " size: " << texture->imageSize(0);
- new DebugTools::ObjectRenderer3D{manager, *this, "my", &debugDrawables};
- }
- public:
- friend SpriteGroup;
- };
- Sprite& SpriteGroup::MakeSprite(TestGameObject& object)
- {
- return *(new Sprite(object, *this));
- }
- class MyApplication: public Platform::Application {
- public:
- explicit MyApplication(const Arguments& arguments);
- private:
- void drawEvent() override;
- void mousePressEvent(MouseEvent& event) override;
- void mouseMoveEvent(MouseMoveEvent& event) override;
- Scene3D _scene;
- Object3D* _cameraObject;
- SceneGraph::Camera3D* _camera;
- SpriteCategory sprites{_scene};
- TerrainCategory terrain{_scene};
- TestRegion gameregion;
- Vector2i _previousMousePosition;
- void ViewRegion()
- {
- auto IncrementPos = [](TilePosition& pos)
- {
- ++pos[0];
- for (unsigned int i = 0; i < DimensionsCount - 1; ++i)
- {
- if (pos[i] >= RegionDimensions[i])
- {
- pos[i] = 0;
- ++pos[i + 1];
- }
- }
- };
- auto GetSpriteGroup = [this](TestRootObject& root) -> SpriteGroup&
- {
- if (!root.spritegroup)
- {
- root.spritegroup = &sprites.MakeGroup(root);
- }
- return *root.spritegroup;
- };
- auto GetSprite = [](TestGameObject& object, SpriteGroup& group) -> Sprite&
- {
- if (!object.sprite)
- {
- object.sprite = &group.MakeSprite(object);
- }
- return *object.sprite;
- };
- auto GetRock = [this](TestTile& tile) -> Rock&
- {
- if (!tile.rock)
- {
- tile.rock = &terrain.MakeRock(tile);
- }
- return *tile.rock;
- };
- for (TilePosition pos = {0, 0, 0}; pos[DimensionsCount - 1] < RegionDimensions[DimensionsCount - 1]; IncrementPos(pos))
- {
- auto& tile = gameregion[pos];
- if (tile.hasrock)
- {
- GetRock(tile);
- }
- for (auto* root : tile.roots)
- {
- if (!root->objects.empty())
- {
- auto& group = GetSpriteGroup(*root);
- for (auto* object : root->objects)
- {
- // if (object->png != "001.png" && object->png != "002.png" && object->png != "003.png") continue;
- GetSprite(*object, group);
- }
- }
- }
- /* Need to set camera position based on player position, player is stored in the region
- * I'll eventually need a 3d camera I think, maybe even sprites will have to be 3d objects, I'll have to see
- *
- * Maybe I should attach the camera object to the player?? Like make the player object its parent, so it inherits the player's translation?
- */
- Vector3i playerpos = GetOffset(*gameregion.player);
- _cameraObject->setTranslation(Vector3(playerpos) + Vector3{0, 0, 4000});
- }
- }
- };
- MyApplication::MyApplication(const Arguments& arguments): Platform::Application{arguments} {
- /* Try 8x MSAA, fall back to zero samples if not possible. Enable only 2x
- MSAA if we have enough DPI. */
- // {
- // const Vector2 dpiScaling = this->dpiScaling({});
- // Configuration conf;
- // conf.setTitle("Magnum Box2D Example")
- // .setSize(conf.size(), dpiScaling);
- // GLConfiguration glConf;
- // glConf.setSampleCount(dpiScaling.max() < 2.0f ? 8 : 2);
- // if(!tryCreate(conf, glConf))
- // create(conf, glConf.setSampleCount(0));
- // }
- GL::Renderer::enable(GL::Renderer::Feature::DepthTest);
- manager.set("my", DebugTools::ObjectRendererOptions{}.setSize(100.0f));
- /* Configure camera */
- _cameraObject = new Object3D{&_scene};
- _camera = new SceneGraph::Camera3D{*_cameraObject};
- _camera->setAspectRatioPolicy(SceneGraph::AspectRatioPolicy::Extend)
- .setProjectionMatrix(Matrix4::perspectiveProjection(35.0_degf, 4.0f/3.0f, 0.001f, 1000000.0f))
- .setViewport(GL::defaultFramebuffer.viewport().size());
- setSwapInterval(1);
- #if !defined(CORRADE_TARGET_EMSCRIPTEN) && !defined(CORRADE_TARGET_ANDROID)
- setMinimalLoopPeriod(16);
- #endif
- }
- /*
- * https://doc.magnum.graphics/magnum/classMagnum_1_1GL_1_1TextureArray.html
- * https://doc.magnum.graphics/magnum/classMagnum_1_1Shaders_1_1FlatGL.html#a0137f4b617fef20381737e6201745969afab48552de43c3349089eef161154d07
- * https://doc.magnum.graphics/magnum/classMagnum_1_1Shaders_1_1FlatGL.html#ae0819ba94b8b4867fa887cacad2fe134
- *
- * So I think what I'll do is have a texture array of some kind, and I still have the shader.bindTexture like in the textured triangle example?
- * I'll just have an offset/position thing that's also in my instance data, that determines what part of the passed in texture is used for each instance
- * I can test this without changing the cubes much (only their class and maybe passing in offsets), if I have some kind of texture array to work with loaded in
- */
- /*
- * https://doc.magnum.graphics/magnum/classMagnum_1_1SceneGraph_1_1Drawable.html
- * https://doc.magnum.graphics/magnum/scenegraph.html
- * https://doc.magnum.graphics/magnum/features.html
- * https://doc.magnum.graphics/magnum/classMagnum_1_1ResourceManager.html
- */
- /*
- * How to figure out alpha stuff:
- * https://doc.magnum.graphics/magnum/classMagnum_1_1Shaders_1_1FlatGL.html#Shaders-FlatGL-alpha
- *
- * Maybe just subtract the tile center off the camera's position, rather than adding it to everything else's position?
- */
- void MyApplication::drawEvent() {
- GL::defaultFramebuffer.clear(GL::FramebufferClear::Color|GL::FramebufferClear::Depth);
- ViewRegion();
- GL::Renderer::enable(GL::Renderer::Feature::DepthTest);
- sprites.draw(*_camera);
- terrain.draw(*_camera);
- GL::Renderer::disable(GL::Renderer::Feature::DepthTest);
- _camera->draw(debugDrawables);
- swapBuffers();
- redraw(); //have some way to call when this is necessary?
- }
- void MyApplication::mousePressEvent(MouseEvent& event) {
- if(event.button() != MouseEvent::Button::Left) return;
- _previousMousePosition = event.position();
- event.setAccepted();
- }
- void MyApplication::mouseMoveEvent(MouseMoveEvent& event) {
- if(!(event.buttons() & MouseMoveEvent::Button::Left)) return;
- /* We have to take window size, not framebuffer size, since the position is
- in window coordinates and the two can be different on HiDPI systems */
- const Vector2 delta = 3.0f*
- Vector2{event.position() - _previousMousePosition}/
- Vector2{windowSize()};
- (*_cameraObject)
- .rotate(Rad{-delta.y()}, _cameraObject->transformation().right().normalized())
- .rotateY(Rad{-delta.x()});
- _previousMousePosition = event.position();
- event.setAccepted();
- redraw();
- }
- MAGNUM_APPLICATION_MAIN(MyApplication)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement