-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathTranslateController.cpp
135 lines (105 loc) · 4.43 KB
/
TranslateController.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include "TranslateController.h"
#include <Magnum/Math/Intersection.h>
#include <Magnum/SceneGraph/Camera.h>
#include <Magnum/SceneGraph/Drawable.h>
#include <Magnum/Shaders/VertexColor.h>
#include <Magnum/Primitives/Axis.h>
#include <Magnum/DebugTools/ResourceManager.h>
#include <Magnum/GL/Mesh.h>
#include <Magnum/Trade/MeshData3D.h>
#include <Magnum/MeshTools/Compile.h>
#include <Magnum/MeshTools/Interleave.h>
#include <Magnum/MeshTools/CompressIndices.h>
using namespace Magnum;
class ObjectRenderer : SceneGraph::Drawable3D {
public:
ObjectRenderer(Object3D &parent, SceneGraph::DrawableGroup3D *group, std::function<bool(void)> drawCheck) : SceneGraph::Drawable3D{parent, group}, _drawCheck(drawCheck) {
/* Shader */
_shader = DebugTools::ResourceManager::instance().get<GL::AbstractShaderProgram, Shaders::VertexColor3D>("TranslateController-shader");
if(!_shader) DebugTools::ResourceManager::instance().set<GL::AbstractShaderProgram>(_shader.key(), new Shaders::VertexColor3D);
/* Mesh and vertex buffer */
_mesh = DebugTools::ResourceManager::instance().get<GL::Mesh>("TranslateController-mesh");
if(_mesh) return;
/* Create the mesh */
GL::Buffer vertexBuffer{GL::Buffer::TargetHint::Array};
GL::Buffer indexBuffer{GL::Buffer::TargetHint::ElementArray};
GL::Mesh* mesh = new GL::Mesh;
auto data = Primitives::axis3D();
vertexBuffer.setData(MeshTools::interleave(data.positions(0), data.colors(0)), GL::BufferUsage::StaticDraw);
indexBuffer.setData(MeshTools::compressIndicesAs<UnsignedByte>(data.indices()), GL::BufferUsage::StaticDraw);
mesh->setPrimitive(GL::MeshPrimitive::Lines)
.setCount(data.indices().size())
.addVertexBuffer(std::move(vertexBuffer), 0, Shaders::VertexColor3D::Position(), Shaders::VertexColor3D::Color4{})
.setIndexBuffer(std::move(indexBuffer), 0, GL::MeshIndexType::UnsignedByte, 0, data.positions(0).size());
DebugTools::ResourceManager::instance().set<GL::Mesh>(_mesh.key(), mesh, ResourceDataState::Final, ResourcePolicy::Manual);
}
private:
void draw(const Matrix4& transformationMatrix, SceneGraph::Camera3D& camera) override {
if (_drawCheck()) {
_shader->setTransformationProjectionMatrix(camera.projectionMatrix() * transformationMatrix);
_mesh->draw(*_shader);
}
}
Resource<GL::AbstractShaderProgram, Shaders::VertexColor3D> _shader;
Resource<GL::Mesh> _mesh;
std::function<bool(void)> _drawCheck;
};
TranslateController::TranslateController(Object3D *parent, SceneGraph::DrawableGroup3D *group) : Object3D(parent) {
new ObjectRenderer{*this, group, [this](){
return children().first();
}};
}
void TranslateController::move(const Ray &cameraRay) {
if (_missed) {
return;
}
Float t = Math::Intersection::planeLine(_plane, cameraRay.origin, cameraRay.direction);
if (Magnum::Math::isInf(t) || Magnum::Math::isNan(t)) {
return;
}
Vector3 currentPoint = cameraRay.origin + cameraRay.direction * t;
Matrix4 tr = transformation();
tr.translation() = _startPosition + _dir * (currentPoint - _startPoint);
setTransformation(tr);
}
void TranslateController::grab(const Ray &cameraRay) {
if (!children().first()) {
return;
}
_startPosition = transformation().translation();
_missed = true;
// check x-z
_plane = Math::planeEquation(Vector3::yAxis(1), _startPosition);
Float t = Math::Intersection::planeLine(_plane, cameraRay.origin, cameraRay.direction);
if (Magnum::Math::isInf(t) || Magnum::Math::isNan(t)) {
return;
}
_startPoint = cameraRay.origin + cameraRay.direction * t;
Vector3 p = _startPoint - _startPosition;
if (Math::abs(p.z()) < _axisThreshold && p.x() > 0 && p.x() <= 1) {
_dir = Vector3::xAxis(1);
_missed = false;
return;
}
if (Math::abs(p.x()) < _axisThreshold && p.z() > 0 && p.z() <= 1) {
_dir = Vector3::zAxis(1);
_missed = false;
return;
}
// check y
_plane = Math::planeEquation(Vector3::xAxis(1), _startPosition);
t = Math::Intersection::planeLine(_plane, cameraRay.origin, cameraRay.direction);
if (Magnum::Math::isInf(t) || Magnum::Math::isNan(t)) {
return;
}
_startPoint = cameraRay.origin + cameraRay.direction * t;
p = _startPoint - _startPosition;
if (Math::abs(p.z()) < _axisThreshold && p.y() > 0 && p.y() <= 1) {
_dir = Vector3::yAxis(1);
_missed = false;
return;
}
}
void TranslateController::release() {
_missed = true;
}