Last active
February 16, 2019 10:28
-
-
Save alexesDev/27771b10e880e82d393b6085396d2252 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DebugTools::ResourceManager _debugManager; | |
SceneGraph::DrawableGroup3D _debugDrawables; | |
Scene3D _scene; | |
new GridRenderer{_scene, &_debugDrawables}; | |
_camera->draw(_debugDrawables); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "GridRenderer.h" | |
#include <Magnum/SceneGraph/Camera.h> | |
#include <Magnum/DebugTools/ResourceManager.h> | |
using namespace Magnum; | |
Color3 getLineColor(Float i, const Color3 &baseColor, const Color3 &axesColor, const Color3 &subColor) { | |
if (i == 0) { | |
return axesColor; | |
} | |
if (std::fmod(i, 5) == 0) { | |
return subColor; | |
} | |
return baseColor; | |
} | |
GridRenderer::GridRenderer(Object3D& object, Magnum::SceneGraph::DrawableGroup3D* drawables) : SceneGraph::Drawable3D{object, drawables} { | |
_shader = DebugTools::ResourceManager::instance().get<GL::AbstractShaderProgram, Shaders::VertexColor3D>("VertexColorShader3D"); | |
if(!_shader) DebugTools::ResourceManager::instance().set<GL::AbstractShaderProgram>(_shader.key(), new Shaders::VertexColor3D); | |
_mesh = DebugTools::ResourceManager::instance().get<GL::Mesh>("grid"); | |
_vertexBuffer = DebugTools::ResourceManager::instance().get<GL::Buffer>("grid-vertices"); | |
if(_mesh) return; | |
GL::Buffer* vertexBuffer = new GL::Buffer{GL::Buffer::TargetHint::Array}; | |
GL::Mesh* mesh = new GL::Mesh; | |
struct Vertex { | |
Vector3 position; | |
Color3 color; | |
}; | |
std::vector<Vertex> data; | |
Float size = 10; | |
Float brightness = 0.3f; | |
Color3 baseColor(brightness); | |
Color3 subColor(brightness * 1.5f); | |
Color3 xColor{ brightness * 2.f, brightness, brightness }; | |
Color3 zColor{ brightness, brightness, brightness * 2.f }; | |
for (Float i = -size; i <= size; ++i) { | |
data.push_back({ { i, 0, -size }, getLineColor(i, baseColor, xColor, subColor) }); | |
data.push_back({ { i, 0, size }, getLineColor(i, baseColor, xColor, subColor) }); | |
data.push_back({ { -size, 0, i }, getLineColor(i, baseColor, zColor, subColor) }); | |
data.push_back({ { size, 0, i }, getLineColor(i, baseColor, zColor, subColor) }); | |
} | |
vertexBuffer->setData(data, GL::BufferUsage::StaticDraw); | |
DebugTools::ResourceManager::instance().set(_vertexBuffer.key(), vertexBuffer, ResourceDataState::Final, ResourcePolicy::Manual); | |
mesh->setPrimitive(GL::MeshPrimitive::Lines) | |
.setCount(data.size()) | |
.addVertexBuffer(*vertexBuffer, 0, Shaders::VertexColor3D::Position{}, Shaders::VertexColor3D::Color3{}); | |
DebugTools::ResourceManager::instance().set<GL::Mesh>(_mesh.key(), mesh, ResourceDataState::Final, ResourcePolicy::Manual); | |
} | |
void GridRenderer::draw(const Matrix4& transformationMatrix, SceneGraph::Camera3D& camera) { | |
_shader->setTransformationProjectionMatrix(camera.projectionMatrix() * transformationMatrix); | |
_mesh->draw(*_shader); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef GridRenderer_h | |
#define GridRenderer_h | |
#include <Magnum/Resource.h> | |
#include <Magnum/GL/Mesh.h> | |
#include <Magnum/GL/Buffer.h> | |
#include <Magnum/SceneGraph/Drawable.h> | |
#include <Magnum/Shaders/VertexColor.h> | |
// #include "Types.h" | |
typedef Magnum::SceneGraph::Object<Magnum::SceneGraph::MatrixTransformation3D> Object3D; | |
class GridRenderer: public Magnum::SceneGraph::Drawable3D { | |
public: | |
explicit GridRenderer(Object3D& object, Magnum::SceneGraph::DrawableGroup3D* drawables = nullptr); | |
private: | |
void draw(const Magnum::Matrix4& transformationMatrix, Magnum::SceneGraph::Camera3D& camera) override; | |
Magnum::Resource<Magnum::GL::AbstractShaderProgram, Magnum::Shaders::VertexColor3D> _shader; | |
Magnum::Resource<Magnum::GL::Mesh> _mesh; | |
Magnum::Resource<Magnum::GL::Buffer> _vertexBuffer; | |
}; | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment