-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathUnitDragger.cpp
302 lines (264 loc) · 9.9 KB
/
UnitDragger.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
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/***********************************************************************
UnitDragger - Class to drag structural units with a dragging tool.
Copyright (c) 2005-2011 Oliver Kreylos
The Nanotech Construction Kit is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The Nanotech Construction Kit 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 GNU
General Public License for more details.
You should have received a copy of the GNU General Public License along
with the Nanotech Construction Kit; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
***********************************************************************/
#include <GL/gl.h>
#include <GL/GLContextData.h>
#include <GL/GLModels.h>
#include <GL/GLTransformationWrappers.h>
#include "StructuralUnit.h"
#include "UnitManager.h"
#include "SpaceGrid.h"
#include "Polyhedron.h"
#include "NanotechConstructionKit.h"
#include "UnitDragger.h"
/**************************************
Methods of class UnitDragger::DataItem:
**************************************/
UnitDragger::DataItem::DataItem(void)
:displayListId(glGenLists(1))
{
}
UnitDragger::DataItem::~DataItem(void)
{
glDeleteLists(displayListId,1);
}
/****************************
Methods of class UnitDragger:
****************************/
void UnitDragger::updateDragger(const Vrui::NavTrackerState& currentTransformation)
{
/* Calculate new transformation and increment transformation: */
DragTransform newTransform=DragTransform(currentTransformation.getTranslation(),currentTransformation.getRotation());
NCK::Rotation dr=newTransform.getRotation()*Geometry::invert(influenceSphereTransform.getRotation());
NCK::Vector dt=newTransform.getTranslation()-influenceSphereTransform.getTranslation();
/* Update dragger's current position, orientation and radius: */
influenceSphereTransform=newTransform;
influenceSphereRadius=application->influenceSphereRadius*currentTransformation.getScaling();
/* Calculate dragger's linear and angular velocities: */
linearVelocity=dt;
angularVelocity=dr.getScaledAxis();
}
UnitDragger::UnitDragger(Vrui::DraggingTool* sTool,NanotechConstructionKit* sApplication)
:DraggingToolAdapter(sTool),
application(sApplication),
createType(application->createType),
draggingMode(application->draggingMode)
{
}
void UnitDragger::initContext(GLContextData& contextData) const
{
/* Create a context data item and add it to the context data: */
DataItem* dataItem=new DataItem;
contextData.addDataItem(this,dataItem);
/* Render the influence model: */
glNewList(dataItem->displayListId,GL_COMPILE);
glPushAttrib(GL_COLOR_BUFFER_BIT|GL_ENABLE_BIT|GL_LINE_BIT|GL_POLYGON_BIT);
glDisable(GL_LIGHTING);
glDisable(GL_CULL_FACE);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
glDepthMask(GL_FALSE);
glLineWidth(1.0f);
glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
glColor4f(0.0f,1.0f,0.0f,0.33f);
glDrawSphereIcosahedron(1.0f,5);
glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
glColor4f(0.1f,0.5f,0.1f,0.33f);
glDrawSphereIcosahedron(1.0f,5);
glDepthMask(GL_TRUE);
glPopAttrib();
glEndList();
}
void UnitDragger::idleMotionCallback(Vrui::DraggingTool::IdleMotionCallbackData* cbData)
{
/* Update dragger's current position, orientation and radius: */
updateDragger(cbData->currentTransformation);
}
void UnitDragger::dragStartCallback(Vrui::DraggingTool::DragStartCallbackData* cbData)
{
/* Update dragger's current position, orientation and radius: */
updateDragger(cbData->startTransformation);
NCK::Point p=influenceSphereTransform.getOrigin();
const NCK::Rotation& o=influenceSphereTransform.getRotation();
if(draggingMode==INTERSTITIAL_VOID)
{
/* Extract the interstitial void polyhedron surrounding the query position: */
delete application->currentVoid;
// application->currentVoid=new NCK::Polyhedron(*application->grid,application->screenCenter);
application->currentVoid=new NCK::Polyhedron(*application->grid,p);
}
else if(draggingMode!=INFLUENCE_SPHERE)
{
NCK::SpaceGrid::StructuralUnitList selectedUnits;
switch(draggingMode)
{
case SINGLE_UNIT:
{
NCK::StructuralUnit* unit=0;
if(cbData->rayBased)
unit=application->grid->findUnit(cbData->ray);
else
unit=application->grid->findUnit(p);
if(unit!=0)
selectedUnits.push_back(unit);
break;
}
case LINKED_ASSEMBLY:
if(cbData->rayBased)
selectedUnits=application->grid->findLinkedUnits(cbData->ray);
else
selectedUnits=application->grid->findLinkedUnits(p);
break;
}
if(createType==MARK||createType==UNMARK)
{
/* Toggle marked state for all selected units: */
application->grid->toggleUnitsMark(selectedUnits);
/* Clear the selection list: */
selectedUnits.clear();
}
else if(createType==LOCK||createType==UNLOCK)
{
/* Toggle locking state for all selected units: */
application->grid->toggleUnitsLock(selectedUnits);
/* Clear the selection list: */
selectedUnits.clear();
}
else if(createType==DELETE)
{
/* Delete all selected units: */
for(NCK::SpaceGrid::StructuralUnitList::iterator uIt=selectedUnits.begin();uIt!=selectedUnits.end();++uIt)
{
/* Remove the object from the space grid: */
application->grid->removeUnit(*uIt);
/* Delete the object: */
delete *uIt;
}
/* Clear the selection list: */
selectedUnits.clear();
}
else if(createType>=FIRST_UNITTYPE&&selectedUnits.empty())
{
/* Add new structural unit at glove position and orientation: */
NCK::StructuralUnit* newUnit=NCK::UnitManager::createUnit(createType-FIRST_UNITTYPE,p,o);
if(newUnit!=0)
{
application->grid->addUnit(newUnit);
selectedUnits.push_back(newUnit);
}
}
/* Start dragging found structural unit(s): */
DragTransform start=influenceSphereTransform;
start.doInvert();
for(NCK::SpaceGrid::StructuralUnitList::iterator uIt=selectedUnits.begin();uIt!=selectedUnits.end();++uIt)
unitStates.push_back(UnitState(*uIt,start));
}
}
void UnitDragger::dragCallback(Vrui::DraggingTool::DragCallbackData* cbData)
{
/* Update dragger's current position, orientation and radius: */
updateDragger(cbData->currentTransformation);
/* Drag selected structural unit(s): */
if(draggingMode==INFLUENCE_SPHERE)
{
/* Find all structural units inside the influence sphere: */
NCK::Point p=influenceSphereTransform.getOrigin();
NCK::SpaceGrid::StructuralUnitList selectedUnits=application->grid->findUnits(p,influenceSphereRadius);
switch(createType)
{
case MARK:
/* Mark all units inside the sphere: */
for(NCK::SpaceGrid::StructuralUnitList::iterator uIt=selectedUnits.begin();uIt!=selectedUnits.end();++uIt)
application->grid->markUnit(*uIt);
break;
case UNMARK:
/* Unmark all units inside the sphere: */
for(NCK::SpaceGrid::StructuralUnitList::iterator uIt=selectedUnits.begin();uIt!=selectedUnits.end();++uIt)
application->grid->unmarkUnit(*uIt);
break;
case LOCK:
/* Lock all units inside the sphere: */
for(NCK::SpaceGrid::StructuralUnitList::iterator uIt=selectedUnits.begin();uIt!=selectedUnits.end();++uIt)
application->grid->lockUnit(*uIt);
break;
case UNLOCK:
/* Unlock all units inside the sphere: */
for(NCK::SpaceGrid::StructuralUnitList::iterator uIt=selectedUnits.begin();uIt!=selectedUnits.end();++uIt)
application->grid->unlockUnit(*uIt);
break;
case DELETE:
break;
default:
{
/* Drag all units currently inside the influence sphere: */
NCK::Rotation r=NCK::Rotation::rotateScaledAxis(angularVelocity);
for(NCK::SpaceGrid::StructuralUnitList::iterator uIt=selectedUnits.begin();uIt!=selectedUnits.end();++uIt)
{
/* Move and rotate this unit: */
NCK::Point up=(*uIt)->getPosition();
NCK::Rotation uo=(*uIt)->getOrientation();
NCK::Vector d=up-p;
NCK::Scalar w=NCK::Scalar(1)-Geometry::mag(d)/influenceSphereRadius;
if(w>NCK::Scalar(0))
{
NCK::Vector disp=linearVelocity;
disp+=Geometry::cross(angularVelocity,d);
disp*=w;
up+=disp;
uo.leftMultiply(NCK::Rotation::rotateScaledAxis(angularVelocity*w));
application->grid->setUnitPositionOrientation(*uIt,up,uo);
//(*uIt)->setVelocities(NCK::Vector::zero,NCK::Vector::zero);
}
}
}
}
}
else
{
for(UnitStateList::iterator uIt=unitStates.begin();uIt!=unitStates.end();++uIt)
{
/* Calculate goal transformation for object and set object's position and orientation: */
DragTransform goalTransformation=influenceSphereTransform*uIt->dragTransformation;
application->grid->setUnitPositionOrientation(uIt->unit,goalTransformation.getOrigin(),goalTransformation.getRotation());
uIt->unit->setVelocities(NCK::Vector::zero,NCK::Vector::zero);
}
}
}
void UnitDragger::dragEndCallback(Vrui::DraggingTool::DragEndCallbackData* cbData)
{
/* Update dragger's current position, orientation and radius: */
updateDragger(cbData->finalTransformation);
/* Release dragged units: */
unitStates.clear();
}
void UnitDragger::setModes(int newCreateType,UnitDragger::DraggingMode newDraggingMode)
{
createType=newCreateType;
draggingMode=newDraggingMode;
}
void UnitDragger::glRenderAction(GLContextData& contextData) const
{
/* Get a pointer to the context data item: */
DataItem* dataItem=contextData.retrieveDataItem<DataItem>(this);
if(draggingMode==INFLUENCE_SPHERE)
{
/* Translate coordinate system to dragger's position and orientation: */
glPushMatrix();
glMultMatrix(influenceSphereTransform);
glScaled(influenceSphereRadius,influenceSphereRadius,influenceSphereRadius);
glCallList(dataItem->displayListId);
glPopMatrix();
}
}