-
Notifications
You must be signed in to change notification settings - Fork 4
/
GridRenderer.cpp
67 lines (48 loc) · 2.33 KB
/
GridRenderer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#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);
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");
if(_mesh) return;
GL::Buffer vertexBuffer{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({ { -size, 0, i }, getLineColor(i, baseColor, xColor, subColor) });
data.push_back({ { size, 0, i }, getLineColor(i, baseColor, xColor, subColor) });
data.push_back({ { i, 0, -size }, getLineColor(i, baseColor, zColor, subColor) });
data.push_back({ { i, 0, size }, getLineColor(i, baseColor, zColor, subColor) });
}
vertexBuffer.setData(data, GL::BufferUsage::StaticDraw);
mesh->setPrimitive(GL::MeshPrimitive::Lines)
.setCount(data.size())
.addVertexBuffer(std::move(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);
}