-
Notifications
You must be signed in to change notification settings - Fork 4
/
vtkWiiMoteStyleCamera.cxx
232 lines (192 loc) · 7.24 KB
/
vtkWiiMoteStyleCamera.cxx
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
/*=========================================================================
Name: vtkWiiMoteStyleCamera.cxx
Author: David Borland, The Renaissance Computing Institute (RENCI)
Copyright: The Renaissance Computing Institute (RENCI)
License: Licensed under the RENCI Open Source Software License v. 1.0.
See included License.txt or
http://www.renci.org/resources/open-source-software-license
for details.
=========================================================================*/
#include "vtkWiiMoteStyleCamera.h"
#include "vtkCamera.h"
#include "vtkInteractorObserver.h"
#include "vtkMath.h"
#include "vtkObjectFactory.h"
#include "vtkRenderWindow.h"
#include "vtkRenderer.h"
#include "vtkVRPNAnalog.h"
#include "vtkVRPNAnalogOutput.h"
#include "vtkVRPNButton.h"
vtkStandardNewMacro(vtkWiiMoteStyleCamera);
vtkCxxRevisionMacro(vtkWiiMoteStyleCamera, "$Revision: 1.0 $");
//----------------------------------------------------------------------------
vtkWiiMoteStyleCamera::vtkWiiMoteStyleCamera()
{
this->ZoomSensitivity = 0.1;
this->PanSensitivity = 0.05;
this->RotateSensitivity = 10.0;
this->XGravity = 0.0;
this->YGravity = 0.0;
this->ZGravity = 0.0;
this->OldXGravity = 0.0;
this->OldYGravity = 0.0;
this->OldZGravity = 0.0;
this->TriggerDown = false;
this->HomeDown = false;
}
//----------------------------------------------------------------------------
vtkWiiMoteStyleCamera::~vtkWiiMoteStyleCamera()
{
}
//----------------------------------------------------------------------------
void vtkWiiMoteStyleCamera::OnEvent(vtkObject* caller, unsigned long eid, void* callData)
{
// Need to initialize both here, as they cannot be initialized in the case statement
vtkVRPNAnalog* analog = static_cast<vtkVRPNAnalog*>(caller);
vtkVRPNButton* button = static_cast<vtkVRPNButton*>(caller);
switch(eid)
{
case vtkVRPNDevice::AnalogEvent:
this->OnAnalog(analog);
break;
case vtkVRPNDevice::ButtonEvent:
this->OnButton(button);
break;
}
}
//----------------------------------------------------------------------------
void vtkWiiMoteStyleCamera::OnAnalog(vtkVRPNAnalog* analog)
{
vtkCamera* camera = this->Renderer->GetActiveCamera();
this->XGravity = analog->GetChannel(vtkWiiMoteStyle::GravityX);
this->YGravity = analog->GetChannel(vtkWiiMoteStyle::GravityY);
this->ZGravity = analog->GetChannel(vtkWiiMoteStyle::GravityZ);
}
//----------------------------------------------------------------------------
void vtkWiiMoteStyleCamera::OnButton(vtkVRPNButton* button)
{
vtkCamera* camera = this->Renderer->GetActiveCamera();
// Reset
if (button->GetButton(vtkWiiMoteStyle::ButtonHome))
{
camera->SetPosition(0.0, 0.0, 1.0);
camera->SetFocalPoint(0.0, 0.0, 0.0);
camera->SetViewUp(0.0, 1.0, 0.0);
this->Renderer->ResetCamera();
if (this->AnalogOutput) this->AnalogOutput->SetChannel(0, 1.0);
this->HomeDown = true;
}
else
{
if (this->HomeDown)
{
if (this->AnalogOutput) this->AnalogOutput->SetChannel(0, 0.0);
this->HomeDown = false;
}
}
// Zoom
if (button->GetButton(vtkWiiMoteStyle::ButtonMinus))
{
// Zoom out
camera->Dolly(1.0 - this->ZoomSensitivity);
}
else if (button->GetButton(vtkWiiMoteStyle::ButtonPlus))
{
// Zoom in
camera->Dolly(1.0 + this->ZoomSensitivity);
}
// Pan
if (button->GetButton(vtkWiiMoteStyle::ButtonLeft))
{
// Pan left
this->Pan(-1.0, 0.0);
}
else if (button->GetButton(vtkWiiMoteStyle::ButtonRight))
{
// Pan right
this->Pan(1.0, 0.0);
}
else if (button->GetButton(vtkWiiMoteStyle::ButtonDown))
{
// Pan down
this->Pan(0.0, -1.0);
}
else if (button->GetButton(vtkWiiMoteStyle::ButtonUp))
{
// Pan up
this->Pan(0.0, 1.0);
}
// Rotate
if (button->GetButton(ButtonB))
{
// Rotate based on WiiMote orientation
if (!this->TriggerDown)
{
this->OldXGravity = this->XGravity;
this->OldYGravity = this->YGravity;
this->OldZGravity = this->ZGravity;
}
camera->Azimuth((this->XGravity - this->OldXGravity) * this->RotateSensitivity);
camera->Elevation(-(this->YGravity - this->OldYGravity) * this->RotateSensitivity);
// camera->Roll(-(this->ZGravity - this->OldZGravity) * this->RotateSensitivity);
camera->OrthogonalizeViewUp();
this->TriggerDown = true;
}
else
{
this->TriggerDown = false;
}
this->Renderer->ResetCameraClippingRange();
// Render() will be called in the interactor
}
//----------------------------------------------------------------------------
void vtkWiiMoteStyleCamera::Pan(double xDelta, double yDelta)
{
vtkCamera* camera = this->Renderer->GetActiveCamera();
double viewFocus[4], focalDepth, viewPoint[3];
double newPickPoint[4], oldPickPoint[4], motionVector[3];
camera->GetFocalPoint(viewFocus);
vtkInteractorObserver::ComputeWorldToDisplay(this->Renderer,
viewFocus[0], viewFocus[1], viewFocus[2],
viewFocus);
focalDepth = viewFocus[2];
// Normalize for window size
int width = this->Renderer->GetRenderWindow()->GetSize()[0];
int height = this->Renderer->GetRenderWindow()->GetSize()[1];
xDelta *= width * this->PanSensitivity;
yDelta *= height * this->PanSensitivity;
vtkInteractorObserver::ComputeDisplayToWorld(this->Renderer,
xDelta, yDelta, focalDepth,
newPickPoint);
vtkInteractorObserver::ComputeDisplayToWorld(this->Renderer,
0, 0, focalDepth,
oldPickPoint);
// Camera motion is reversed
motionVector[0] = newPickPoint[0] - oldPickPoint[0];
motionVector[1] = newPickPoint[1] - oldPickPoint[1];
motionVector[2] = newPickPoint[2] - oldPickPoint[2];
camera->GetFocalPoint(viewFocus);
camera->GetPosition(viewPoint);
camera->SetFocalPoint(motionVector[0] + viewFocus[0],
motionVector[1] + viewFocus[1],
motionVector[2] + viewFocus[2]);
camera->SetPosition(motionVector[0] + viewPoint[0],
motionVector[1] + viewPoint[1],
motionVector[2] + viewPoint[2]);
}
//----------------------------------------------------------------------------
void vtkWiiMoteStyleCamera::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
os << indent << "ZoomSensitivity: " << this->ZoomSensitivity << "\n";
os << indent << "PanSensitivity: " << this->PanSensitivity << "\n";
os << indent << "RotateSensitivity: " << this->RotateSensitivity << "\n";
os << indent << "XGravity: " << this->XGravity << "\n";
os << indent << "YGravity: " << this->YGravity << "\n";
os << indent << "ZGravity: " << this->ZGravity << "\n";
os << indent << "OldXGravity: " << this->OldXGravity << "\n";
os << indent << "OldYGravity: " << this->OldYGravity << "\n";
os << indent << "OldZGravity: " << this->OldZGravity << "\n";
os << indent << "TriggerDown: " << this->TriggerDown << "\n";
os << indent << "HomeDown: " << this->HomeDown << "\n";
}