Skip to content

Instantly share code, notes, and snippets.

@alexesDev
Last active January 4, 2018 20:48
Show Gist options
  • Save alexesDev/dd10a4c9f6cb28783304d20ef26bb8f9 to your computer and use it in GitHub Desktop.
Save alexesDev/dd10a4c9f6cb28783304d20ef26bb8f9 to your computer and use it in GitHub Desktop.
Simple CircleRenderer feature for margum engine
#include "CircleRenderer.h"
#include "Types.h"
#include <Magnum/Mesh.h>
#include <Magnum/Buffer.h>
#include <Magnum/SceneGraph/Camera.h>
#include <Magnum/Shaders/Flat.h>
#include <Magnum/Primitives/Circle.h>
#include <Magnum/Trade/MeshData2D.h>
#include <Magnum/MeshTools/Interleave.h>
using namespace Magnum;
CircleRenderer::CircleRenderer(SceneGraph::AbstractObject2D *object, SceneGraph::DrawableGroup2D *drawables)
: SceneGraph::Drawable2D(*object, drawables) {
auto &rs = GameResourceManager::instance();
/* shader */
_shader = rs.get<AbstractShaderProgram, Shaders::Flat2D>("circle");
if(!_shader) {
rs.set<AbstractShaderProgram>(_shader.key(), new Shaders::Flat2D);
}
/* Mesh and vertex buffer */
_mesh = rs.get<Mesh>("circle");
_vertexBuffer = rs.get<Buffer>("circle-vertex");
if(_mesh) return;
/* Create the mesh */
Buffer *vertexBuffer = new Buffer{Buffer::TargetHint::Array};
Mesh* mesh = new Mesh;
auto circle = Primitives::Circle::solid(32);
vertexBuffer->setData(MeshTools::interleave(circle.positions(0)), Buffer::Usage::StaticDraw);
rs.set<Buffer>(_vertexBuffer.key(), vertexBuffer, ResourceDataState::Final, ResourcePolicy::Manual);
mesh->setPrimitive(circle.primitive())
.setCount(circle.positions(0).size())
.addVertexBuffer(*_vertexBuffer, 0, Shaders::Flat2D::Position());
rs.set<Mesh>(_mesh.key(), mesh, ResourceDataState::Final, ResourcePolicy::Manual);
}
void CircleRenderer::draw(const Matrix3 &transformationMatrix, SceneGraph::Camera2D &camera) {
_shader->setTransformationProjectionMatrix(camera.projectionMatrix() * transformationMatrix);
_mesh->draw(*_shader);
}
#ifndef GAME_CIRCLE_RENDERER
#define GAME_CIRCLE_RENDERER
#include <Magnum/SceneGraph/Drawable.h>
#include <Magnum/Shaders/Shaders.h>
#include <Magnum/Resource.h>
class CircleRenderer : public Magnum::SceneGraph::Drawable2D {
public:
explicit CircleRenderer(Magnum::SceneGraph::AbstractObject2D *object, Magnum::SceneGraph::DrawableGroup2D *drawables);
private:
void draw(const Magnum::Matrix3 &transformationMatrix, Magnum::SceneGraph::Camera2D &camera) override;
Magnum::Resource<Magnum::AbstractShaderProgram, Magnum::Shaders::Flat2D> _shader;
Magnum::Resource<Magnum::Mesh> _mesh;
Magnum::Resource<Magnum::Buffer> _vertexBuffer;
};
#endif
#ifndef H_GAME_TYPES
#define H_GAME_TYPES
#include <Magnum/Magnum.h>
#include <Magnum/SceneGraph/Scene.h>
#include <Magnum/SceneGraph/MatrixTransformation2D.h>
#include <Magnum/ResourceManager.h>
typedef Magnum::SceneGraph::Object<Magnum::SceneGraph::MatrixTransformation2D> Object2D;
typedef Magnum::SceneGraph::Scene<Magnum::SceneGraph::MatrixTransformation2D> Scene2D;
typedef Magnum::ResourceManager<Magnum::Mesh, Magnum::Buffer, Magnum::AbstractShaderProgram> GameResourceManager;
#endif
GameResourceManager manager;
// ..
Object2D* object = new Object2D(&_scene);
new CircleRenderer{object, &_drawables};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment