-
Notifications
You must be signed in to change notification settings - Fork 0
/
Viewers.cpp
465 lines (407 loc) · 14.9 KB
/
Viewers.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
/* Copyright (c) 2002-2012 Croteam Ltd.
This program is free software; you can redistribute it and/or modify
it under the terms of version 2 of the GNU General Public License as published by
the Free Software Foundation
This program 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 this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
#include "StdH.h"
#include "WorldEditor.h"
/////////////////////////////////////////////////////////////////////
// CSlaveViewer
#define MIN_TARGET_DISTANCE 0.1f
#define MAX_TARGET_DISTANCE 640000.0f
extern FLOAT wed_fFrontClipDistance;
/*
* Default constructor
*/
CMasterViewer::CMasterViewer(void)
{
// set initial viewer variables, viewing tovards origin from ten meters distance
mv_plViewer.pl_PositionVector = FLOAT3D(3.0f, 4.0f, 10.0f);
mv_plViewer.pl_OrientationAngle = ANGLE3D(AngleDeg( 20.0f), AngleDeg( -20.0f), 0);
mv_fTargetDistance = 10.0f; // this must never be very small!
}
// get placement of the virtual target
CPlacement3D CMasterViewer::GetTargetPlacement(void) const
{
CPlacement3D plTarget;
// copy viewer to target
plTarget = mv_plViewer;
// get the direction vector of viewer's sight
FLOAT3D vDirection;
AnglesToDirectionVector(mv_plViewer.pl_OrientationAngle, vDirection);
// offset the target placement by target distance along the vector
plTarget.pl_PositionVector += vDirection*mv_fTargetDistance;
// return the target placement
return plTarget;
}
// set placement of the virtual target
void CMasterViewer::SetTargetPlacement(FLOAT3D f3dTarget)
{
CPlacement3D plViewer;
// copy the viewer's orientation, it will stay the same
plViewer.pl_OrientationAngle = mv_plViewer.pl_OrientationAngle;
// set position vector of new target
plViewer.pl_PositionVector = f3dTarget;
// translate viewer back from target for their distance
plViewer.Translate_OwnSystem( FLOAT3D( 0.0f, 0.0f, mv_fTargetDistance) );
// set new viewer's pacement
mv_plViewer = plViewer;
}
/*
* Convert from slave.
*/
void CMasterViewer::operator=(const CSlaveViewer &svSlave)
{
// copy target distance
mv_fTargetDistance = svSlave.sv_fTargetDistance;
// if is perspective
if( svSlave.IsPerspective())
{
// copy viewer placement
mv_plViewer = svSlave.sv_plViewer;
}
// if is isometric
else
{
// copy slave viewer position to master viewer position
mv_plViewer.pl_PositionVector = svSlave.sv_plViewer.pl_PositionVector;
// get the ray of the slave viewing direction
FLOAT3D vSlaveDirection;
AnglesToDirectionVector(svSlave.sv_plViewer.pl_OrientationAngle, vSlaveDirection);
// offset the viewer forward by target distance along the slave direction vector
mv_plViewer.pl_PositionVector += vSlaveDirection*mv_fTargetDistance;
// get the ray of the master viewing direction
FLOAT3D vMasterDirection;
AnglesToDirectionVector(mv_plViewer.pl_OrientationAngle, vMasterDirection);
// offset the viewer backward by target distance along the slave direction vector
mv_plViewer.pl_PositionVector -= vMasterDirection*mv_fTargetDistance;
}
}
/////////////////////////////////////////////////////////////////////
// CSlaveViewer
// get orientation angles for type of isometric viewer
ANGLE3D CSlaveViewer::GetAngleForIsometricType(void) const
{
// depending on the projection type, return angles
switch (sv_ProjectionType) {
case PT_ISOMETRIC_FRONT:
return ANGLE3D((ANGLE)ANGLE_0 ,(ANGLE)ANGLE_0 ,(ANGLE)ANGLE_0 );
case PT_ISOMETRIC_RIGHT:
return ANGLE3D((ANGLE)ANGLE_90 ,(ANGLE)ANGLE_0 ,(ANGLE)ANGLE_0 );
case PT_ISOMETRIC_TOP:
return ANGLE3D((ANGLE)ANGLE_0 ,(ANGLE)ANGLE_270,(ANGLE)ANGLE_0 );
case PT_ISOMETRIC_BACK:
return ANGLE3D((ANGLE)ANGLE_180,(ANGLE)ANGLE_0 ,(ANGLE)ANGLE_0 );
case PT_ISOMETRIC_LEFT:
return ANGLE3D((ANGLE)ANGLE_270,(ANGLE)ANGLE_0 ,(ANGLE)ANGLE_0 );
case PT_ISOMETRIC_BOTTOM:
return ANGLE3D((ANGLE)ANGLE_0 ,(ANGLE)ANGLE_90 ,(ANGLE)ANGLE_0 );
default:
ASSERT(FALSE);
return ANGLE3D((ANGLE)ANGLE_0 ,(ANGLE)ANGLE_0 ,(ANGLE)ANGLE_0 );
}
}
/*
* Get zoom factor for the viewer.
*/
FLOAT CSlaveViewer::GetZoomFactor(void)
{
// calculate the zoom factor that as ratio of size in pixels and size in meters
// for target object
FLOAT fScreenX = (float)sv_pdpDrawPort->GetWidth();
ANGLE aHalfI = AngleDeg(90.0f)/2; // assume FOV of 90 degrees for perspective
FLOAT fPerspectiveRatio = fScreenX/(2.0f*Tan(aHalfI));
FLOAT fZoomFactor = fPerspectiveRatio/sv_fTargetDistance;
ASSERT( fZoomFactor > 0);
return fZoomFactor;
}
/*
* Get distance for wanted zoom factor for the viewer.
*/
FLOAT CSlaveViewer::GetDistanceForZoom(FLOAT fZoom)
{
// calculate the zoom factor that as ratio of size in pixels and size in meters
// for target object
FLOAT fScreenX = (float)sv_pdpDrawPort->GetWidth();
ANGLE aHalfI = AngleDeg(90.0f)/2; // assume FOV of 90 degrees for perspective
FLOAT fPerspectiveRatio = fScreenX/(2.0f*Tan(aHalfI));
FLOAT fDistance = fPerspectiveRatio / fZoom;
ASSERT( fDistance > 0);
return fDistance;
}
// get placement of the virtual target
CPlacement3D CSlaveViewer::GetTargetPlacement(void) const
{
CPlacement3D plTarget;
// copy viewer to target
plTarget = sv_plViewer;
// get the direction vector of viewer's sight
FLOAT3D vDirection;
AnglesToDirectionVector(sv_plViewer.pl_OrientationAngle, vDirection);
// offset the target placement by target distance along the vector
plTarget.pl_PositionVector += vDirection*sv_fTargetDistance;
// return the target placement
return plTarget;
}
/*
* Default constructor
*/
CSlaveViewer::CSlaveViewer(const CMasterViewer &mvMaster,
enum ProjectionType ptProjectionType,
const CPlacement3D &plGrid,
CDrawPort *pdpDrawPort)
{
// remember the drawport
sv_pdpDrawPort = pdpDrawPort;
// set projection type
sv_ProjectionType = ptProjectionType;
// set grid placement
sv_plGrid = plGrid;
// copy target distance
sv_fTargetDistance = mvMaster.mv_fTargetDistance;
// if is perspective
if( IsPerspective())
{
// copy viewer placement
sv_plViewer = mvMaster.mv_plViewer;
}
// if is isometric
else
{
// copy target position to slave viewer position
sv_plViewer.pl_PositionVector = mvMaster.GetTargetPlacement().pl_PositionVector;
// get the orientation depending on the isometry type
sv_plViewer.pl_OrientationAngle = GetAngleForIsometricType();
// transform to grid system
sv_plViewer.AbsoluteToRelative(sv_plGrid);
// get the ray of the viewing direction
FLOAT3D vDirection;
AnglesToDirectionVector(sv_plViewer.pl_OrientationAngle, vDirection);
// offset the viewer backward by target distance along the vector
sv_plViewer.pl_PositionVector -= vDirection*sv_fTargetDistance;
// transform back from grid system
sv_plViewer.RelativeToAbsolute(sv_plGrid);
}
}
/*
* create a perspective projection for this viewer
*/
void CSlaveViewer::MakePerspectiveProjection(CPerspectiveProjection3D &prPerspectiveProjection)
{
// if is perspective
ASSERT( IsPerspective());
// init projection parameters
prPerspectiveProjection.FOVL() = AngleDeg(90.0f);
prPerspectiveProjection.ScreenBBoxL() = FLOATaabbox2D(
FLOAT2D(0.0f, 0.0f),
FLOAT2D((float)sv_pdpDrawPort->GetWidth(), (float)sv_pdpDrawPort->GetHeight())
);
wed_fFrontClipDistance = Clamp( wed_fFrontClipDistance, 0.02f, 2.00f);
prPerspectiveProjection.FrontClipDistanceL() = wed_fFrontClipDistance;
prPerspectiveProjection.AspectRatioL() = 1.0f;
prPerspectiveProjection.ObjectStretchL() = FLOAT3D(1.0f, 1.0f, 1.0f);
// set up viewer position and placement
prPerspectiveProjection.ViewerPlacementL() = sv_plViewer;
prPerspectiveProjection.ObjectPlacementL().pl_PositionVector = FLOAT3D(0.0f, 0.0f, 0.0f);
prPerspectiveProjection.ObjectPlacementL().pl_OrientationAngle = ANGLE3D(0, 0, 0);
}
/*
* create a projection for this viewer
*/
void CSlaveViewer::MakeProjection(CAnyProjection3D &prProjection)
{
// if is perspective
if( IsPerspective()) {
// use perspective projection
CPerspectiveProjection3D prPerspectiveProjection;
// make perspective projection
MakePerspectiveProjection( prPerspectiveProjection);
// return the result
prProjection = prPerspectiveProjection;
// if is isometric
} else {
// use isometric projection
CIsometricProjection3D prIsometricProjection;
// init projection parameters
prIsometricProjection.ZoomFactorL() = GetZoomFactor();
prIsometricProjection.ScreenBBoxL() = FLOATaabbox2D(
FLOAT2D(0.0f, 0.0f),
FLOAT2D((float)sv_pdpDrawPort->GetWidth(), (float)sv_pdpDrawPort->GetHeight())
);
wed_fFrontClipDistance = Clamp( wed_fFrontClipDistance, 0.02f, 2.00f);
prIsometricProjection.AspectRatioL() = 1.0f;
prIsometricProjection.FrontClipDistanceL() = wed_fFrontClipDistance;
prIsometricProjection.ObjectStretchL() = FLOAT3D(1.0f, 1.0f, 1.0f);
// set up viewer position and placement
prIsometricProjection.ViewerPlacementL() = sv_plViewer;
prIsometricProjection.ObjectPlacementL().pl_PositionVector = FLOAT3D(0.0f, 0.0f, 0.0f);
prIsometricProjection.ObjectPlacementL().pl_OrientationAngle = ANGLE3D(0, 0, 0);
// return the result
prProjection = prIsometricProjection;
}
}
/*
* Translate slave viewer in his own system
*/
void CSlaveViewer::Translate_OwnSystem( PIX pixDI, PIX pixDJ, PIX pixDK)
{
FLOAT fZoom = GetZoomFactor();
sv_fTargetDistance += pixDK/fZoom;
if( sv_fTargetDistance < MIN_TARGET_DISTANCE)
{
sv_fTargetDistance = MIN_TARGET_DISTANCE;
return;
}
if( sv_fTargetDistance > MAX_TARGET_DISTANCE)
{
sv_fTargetDistance = MAX_TARGET_DISTANCE;
return;
}
ASSERT( sv_fTargetDistance > 0);
sv_plViewer.Translate_OwnSystem( FLOAT3D(-pixDI/fZoom, pixDJ/fZoom, pixDK/fZoom));
}
/*
* Translate slave viewer without stretching zoom factor
*/
void CSlaveViewer::Translate_Local_OwnSystem(FLOAT fdX, FLOAT fdY, FLOAT fdZ)
{
sv_plViewer.Translate_OwnSystem( FLOAT3D(fdX, fdY, fdZ));
}
/*
* Scales target distance using given factor
*/
void CSlaveViewer::ScaleTargetDistance( FLOAT fFactor)
{
// if very close to the target, and trying to go even closer or
// if very far away and trying to move further away
if( (sv_fTargetDistance <MIN_TARGET_DISTANCE && fFactor<1.0f) ||
(sv_fTargetDistance >MAX_TARGET_DISTANCE && fFactor>1.0f) )
{
// don't do anything
return;
}
// change along z axis
FLOAT dz = sv_fTargetDistance * fFactor;
sv_plViewer.Translate_OwnSystem( FLOAT3D( 0.0f, 0.0f, dz));
sv_fTargetDistance += dz;
}
/*
* Rotate slave viewer arround target using HPB method
*/
void CSlaveViewer::Rotate_HPB( PIX pixDI, PIX pixDJ, PIX pixDK)
{
// if is perspective
if( IsPerspective())
{
// get target placement
CPlacement3D plTarget = GetTargetPlacement();
// project viewer placement to the target placement
sv_plViewer.AbsoluteToRelative(plTarget);
// rotate the target
plTarget.Rotate_HPB( ANGLE3D( AngleDeg((FLOAT)-pixDI),
AngleDeg((FLOAT)-pixDJ),
AngleDeg((FLOAT)-pixDK)));
// project viewer placement back from the target placement
sv_plViewer.RelativeToAbsolute(plTarget);
}
// if is isometric
else
{
// can't rotate isometric projections
}
}
/*
* Rotate slave viewer in his own system using HPB method
*/
void CSlaveViewer::Rotate_Local_HPB( FLOAT fdX, FLOAT fdY, FLOAT fdZ)
{
// if is perspective
if( IsPerspective())
{
// rotate the viewer
sv_plViewer.Rotate_HPB( ANGLE3D( AngleDeg(-fdX), AngleDeg(-fdY), AngleDeg(-fdZ)));
}
// if is isometric
else
{
// can't rotate isometric projections
}
}
// translate a placement in given system
void CSlaveViewer::TranslatePlacement_OtherSystem(CPlacement3D &plToTranslate,
CPlacement3D &plOtherSystem,
PIX pixDI, PIX pixDJ, PIX pixDK)
{
FLOAT fZoom = GetZoomFactor();
// project the placement to the viewer's system
plToTranslate.AbsoluteToRelative(sv_plViewer);
// project the placement to the given system
plToTranslate.AbsoluteToRelative(plOtherSystem);
// translate it
plToTranslate.Translate_AbsoluteSystem( FLOAT3D(pixDI/fZoom, -pixDJ/fZoom, pixDK/fZoom));
// project the placement back from given system
plToTranslate.RelativeToAbsolute(plOtherSystem);
// project the placement back from viewer's system
plToTranslate.RelativeToAbsolute(sv_plViewer);
}
// translate a placement in viewer's system
void CSlaveViewer::TranslatePlacement_OwnSystem(CPlacement3D &plToTranslate,
PIX pixDI, PIX pixDJ, PIX pixDK)
{
FLOAT fZoom = GetZoomFactor();
// project the placement to the viewer's system
plToTranslate.AbsoluteToRelative(sv_plViewer);
// translate it
plToTranslate.Translate_AbsoluteSystem( FLOAT3D(pixDI/fZoom, -pixDJ/fZoom, pixDK/fZoom));
// project the placement back from viewer's system
plToTranslate.RelativeToAbsolute(sv_plViewer);
}
// convert offset from pixels to meters
FLOAT CSlaveViewer::PixelsToMeters(PIX pix)
{
FLOAT fZoom = GetZoomFactor();
return pix/fZoom;
}
// translate a placement in viewer's system
void CSlaveViewer::RotatePlacement_HPB(CPlacement3D &plToRotate,
PIX pixDI, PIX pixDJ, PIX pixDK)
{
// project the placement to the viewer's system
plToRotate.AbsoluteToRelative(sv_plViewer);
// rotate it
plToRotate.Rotate_HPB( ANGLE3D( AngleDeg((FLOAT)-pixDI),
AngleDeg((FLOAT)-pixDJ),
AngleDeg((FLOAT)-pixDK)));
// project the placement back from viewer's system
plToRotate.RelativeToAbsolute(sv_plViewer);
}
// translate a placement in viewer's system
void CSlaveViewer::RotatePlacement_TrackBall(CPlacement3D &plToRotate,
PIX pixDI, PIX pixDJ, PIX pixDK)
{
// if viewer is perspective
if( IsPerspective()) {
// project the placement to the viewer's system
plToRotate.AbsoluteToRelative(sv_plViewer);
// rotate it
plToRotate.Rotate_TrackBall( ANGLE3D( AngleDeg((FLOAT)-pixDI),
AngleDeg((FLOAT)-pixDJ),
AngleDeg((FLOAT)-pixDK)));
// project the placement back from viewer's system
plToRotate.RelativeToAbsolute(sv_plViewer);
// if viewer is isometric
} else {
// project the placement to the viewer's system
plToRotate.AbsoluteToRelative(sv_plViewer);
// rotate it only around banking axis
plToRotate.Rotate_TrackBall( ANGLE3D( 0,0, AngleDeg((FLOAT)-pixDI)));
// project the placement back from viewer's system
plToRotate.RelativeToAbsolute(sv_plViewer);
}
}