-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathLineRenderer.cpp
56 lines (40 loc) · 1.86 KB
/
LineRenderer.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
#include "LineRenderer.h"
#include <Magnum/SceneGraph/Camera.h>
#include <Magnum/DebugTools/ResourceManager.h>
using namespace Magnum;
constexpr std::array<Vector3, 2> positions{{
{ 0, 0, 0 },
{ 0, 0, 1.f }
}};
LineRenderer::LineRenderer(Object3D& object, Magnum::SceneGraph::DrawableGroup3D* drawables) : SceneGraph::Drawable3D{object, drawables} {
_shader = DebugTools::ResourceManager::instance().get<GL::AbstractShaderProgram, Shaders::Flat3D>("FlatShader3D");
if(!_shader) DebugTools::ResourceManager::instance().set<GL::AbstractShaderProgram>(_shader.key(), new Shaders::Flat3D);
_mesh = DebugTools::ResourceManager::instance().get<GL::Mesh>("my-line");
if(_mesh) return;
GL::Mesh* mesh = new GL::Mesh;
GL::Buffer vertexBuffer{GL::Buffer::TargetHint::Array};
vertexBuffer.setData(positions, GL::BufferUsage::StaticDraw);
mesh->setPrimitive(GL::MeshPrimitive::Lines)
.setCount(2)
.addVertexBuffer(std::move(vertexBuffer), 0, Shaders::Flat3D::Position{});
DebugTools::ResourceManager::instance().set<GL::Mesh>(_mesh.key(), mesh, ResourceDataState::Final, ResourcePolicy::Manual);
}
void LineRenderer::draw(const Matrix4& transformationMatrix, SceneGraph::Camera3D& camera) {
for (auto &line : _lines) {
_shader->setTransformationProjectionMatrix(camera.projectionMatrix() * transformationMatrix * line->transformation())
.setColor(line->color());
_mesh->draw(*_shader);
}
}
Matrix4 LineRendererOptions::transformation() {
if (_dirty) {
Float len = (_to - _from).length();
Vector3 start = Vector3::zAxis(1);
Vector3 dir = (_to - _from).normalized();
Vector3 right = Math::cross(start, dir).normalized();
Rad angle = Math::angle(start, dir);
_transformation = Matrix4::translation(_from) * Matrix4::rotation(angle, right) * Matrix4::scaling(Vector3(len));
_dirty = false;
}
return _transformation;
}