-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathTranslate1DDragger.cpp
More file actions
196 lines (161 loc) · 7.04 KB
/
Translate1DDragger.cpp
File metadata and controls
196 lines (161 loc) · 7.04 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
*
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* OpenSceneGraph Public License for more details.
*/
//osgManipulator - Copyright (C) 2007 Fugro-Jason B.V.
#include <osgManipulator/Translate1DDragger>
#include <osgManipulator/Command>
#include <osg/ShapeDrawable>
#include <osg/Geode>
#include <osg/Geometry>
#include <osg/LineWidth>
#include <osg/Material>
using namespace osgManipulator;
Translate1DDragger::Translate1DDragger() : Dragger(), _checkForNodeInNodePath(true)
{
_projector = new LineProjector;
setColor(osg::Vec4(0.0f, 1.0f, 0.0f, 1.0f));
setPickColor(osg::Vec4(1.0f, 1.0f, 0.0f, 1.0f));
}
Translate1DDragger::Translate1DDragger(const osg::Vec3d& s, const osg::Vec3d& e) : Dragger(), _checkForNodeInNodePath(true)
{
_projector = new LineProjector(s,e);
setColor(osg::Vec4(0.0f, 1.0f, 0.0f, 1.0f));
setPickColor(osg::Vec4(1.0f, 1.0f, 0.0f, 1.0f));
}
Translate1DDragger::~Translate1DDragger()
{
}
bool Translate1DDragger::handle(const PointerInfo& pointer, const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa)
{
// Check if the dragger node is in the nodepath.
if (_checkForNodeInNodePath)
{
if (!pointer.contains(this)) return false;
}
switch (ea.getEventType())
{
// Pick start.
case (osgGA::GUIEventAdapter::PUSH):
{
// Get the LocalToWorld matrix for this node and set it for the projector.
osg::NodePath nodePathToRoot;
computeNodePathToRoot(*this,nodePathToRoot);
osg::Matrix localToWorld = osg::computeLocalToWorld(nodePathToRoot);
_projector->setLocalToWorld(localToWorld);
if (_projector->project(pointer, _startProjectedPoint))
{
// Generate the motion command.
osg::ref_ptr<TranslateInLineCommand> cmd = new TranslateInLineCommand(_projector->getLineStart(),
_projector->getLineEnd());
cmd->setStage(MotionCommand::START);
cmd->setLocalToWorldAndWorldToLocal(_projector->getLocalToWorld(),_projector->getWorldToLocal());
// Dispatch command.
dispatch(*cmd);
// Set color to pick color.
setMaterialColor(_pickColor,*this);
aa.requestRedraw();
}
return true;
}
// Pick move.
case (osgGA::GUIEventAdapter::DRAG):
{
osg::Vec3d projectedPoint;
if (_projector->project(pointer, projectedPoint))
{
// Generate the motion command.
osg::ref_ptr<TranslateInLineCommand> cmd = new TranslateInLineCommand(_projector->getLineStart(),
_projector->getLineEnd());
cmd->setStage(MotionCommand::MOVE);
cmd->setLocalToWorldAndWorldToLocal(_projector->getLocalToWorld(),_projector->getWorldToLocal());
cmd->setTranslation(projectedPoint - _startProjectedPoint);
// Dispatch command.
dispatch(*cmd);
aa.requestRedraw();
}
return true;
}
// Pick finish.
case (osgGA::GUIEventAdapter::RELEASE):
{
osg::Vec3d projectedPoint;
if (_projector->project(pointer, projectedPoint))
{
osg::ref_ptr<TranslateInLineCommand> cmd = new TranslateInLineCommand(_projector->getLineStart(),
_projector->getLineEnd());
cmd->setStage(MotionCommand::FINISH);
cmd->setLocalToWorldAndWorldToLocal(_projector->getLocalToWorld(),_projector->getWorldToLocal());
// Dispatch command.
dispatch(*cmd);
// Reset color.
setMaterialColor(_color,*this);
aa.requestRedraw();
}
return true;
}
default:
return false;
}
}
void Translate1DDragger::setupDefaultGeometry()
{
// Get the line length and direction.
osg::Vec3 lineDir = _projector->getLineEnd()-_projector->getLineStart();
float lineLength = lineDir.length();
lineDir.normalize();
osg::Geode* geode = new osg::Geode;
// Create a left cone.
{
osg::Cone* cone = new osg::Cone (_projector->getLineStart(), 0.025f * lineLength, 0.10f * lineLength);
osg::Quat rotation;
rotation.makeRotate(osg::Vec3(0.0f, 0.0f, 1.0f),-lineDir);
cone->setRotation(rotation);
geode->addDrawable(new osg::ShapeDrawable(cone));
}
// Create a right cone.
{
osg::Cone* cone = new osg::Cone (_projector->getLineEnd(), 0.025f * lineLength, 0.10f * lineLength);
osg::Quat rotation;
rotation.makeRotate(osg::Vec3(0.0f, 0.0f, 1.0f), lineDir);
cone->setRotation(rotation);
geode->addDrawable(new osg::ShapeDrawable(cone));
}
// Create an invisible cylinder for picking the line.
{
osg::Cylinder* cylinder = new osg::Cylinder ((_projector->getLineStart()+_projector->getLineEnd())/2, 0.015f * lineLength, lineLength);
osg::Quat rotation;
rotation.makeRotate(osg::Vec3(0.0f, 0.0f, 1.0f), lineDir);
cylinder->setRotation(rotation);
osg::Drawable* cylinderGeom = new osg::ShapeDrawable(cylinder);
setDrawableToAlwaysCull(*cylinderGeom);
geode->addDrawable(cylinderGeom);
}
osg::Geode* lineGeode = new osg::Geode;
// Create a line.
{
osg::Geometry* geometry = new osg::Geometry();
osg::Vec3Array* vertices = new osg::Vec3Array(2);
(*vertices)[0] = _projector->getLineStart();
(*vertices)[1] = _projector->getLineEnd();
geometry->setVertexArray(vertices);
geometry->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINES,0,2));
lineGeode->addDrawable(geometry);
}
// Turn of lighting for line and set line width.
lineGeode->getOrCreateStateSet()->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
osg::LineWidth* linewidth = new osg::LineWidth();
linewidth->setWidth(2.0f);
lineGeode->getOrCreateStateSet()->setAttributeAndModes(linewidth, osg::StateAttribute::ON);
// Add line and cones to the scene.
addChild(lineGeode);
addChild(geode);
}