-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathPerspective.cpp
491 lines (397 loc) · 10.4 KB
/
Perspective.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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
#include "stdafx.h"
#include "SparkEdit.h"
#include "Perspective.h"
#include "HaloMapFile.h"
#include "SelectionManager.h"
#include "ToolView.h"
#include "Render.h"
#include "BspManager.h"
#include "Frustum1.h"
#include "ProfileSettings.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
extern CPerspective *g_pRenderWnd;
extern CHaloMapFile HaloMap;
extern CSelectionManager gSelection;
extern CToolView *g_pTools;
extern CRender gRender;
extern CBspManager gBspManager;
extern CFrustum gFrustum;
extern CProfileSettings gProfile;
#define FRAME_UPDATE_TIMER 1
IMPLEMENT_DYNCREATE(CPerspective, COpenGLWnd)
CPerspective::CPerspective()
{
m_xpos = 0.0f;
m_ypos = 0.0f;
m_zoom = 100.0f;
m_xrot = 0.0f;
m_yrot = 90.0f;
m_fCameraSpeed = 1.5f;
g_pRenderWnd = this;
m_lastMouseX = -1;
m_lastMouseY = -1;
m_Camera.PositionCamera(75, 75, 0, 0, 0, 0, 0, 0, 1);
m_CameraAccCount = 0;
m_fCameraAcc = 0.3f;
m_CameraAccMax = 1.0f;
m_CameraAccTimer.ResetTimer();
m_bInvertMouse = FALSE;
m_bLookMode = FALSE;
m_bEnableRotationMode = FALSE;
m_LastSelectionPos[0] = 0;
m_LastSelectionPos[1] = 0;
m_LastSelectionPos[2] = 0;
}
CPerspective::~CPerspective()
{
}
void CPerspective::OnDraw(CDC* pDC)
{
gRender.TimerEnd();
gRender.TimerStart();
glLoadIdentity();
CheckKeyboard();
m_Camera.Update();
m_Camera.Look();
gFrustum.CalculateFrustum();
SetContext();
RenderScene();
SwapGLBuffers();
}
BEGIN_MESSAGE_MAP(CPerspective, COpenGLWnd)
//{{AFX_MSG_MAP(CPerspective)
ON_WM_SIZE()
ON_WM_MOUSEMOVE()
ON_WM_CHAR()
ON_WM_TIMER()
ON_WM_LBUTTONUP()
ON_WM_RBUTTONDBLCLK()
ON_WM_MOUSEWHEEL()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
#ifdef _DEBUG
void CPerspective::AssertValid() const
{
CView::AssertValid();
}
void CPerspective::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
#endif //_DEBUG
void CPerspective::OnSize(UINT nType, int cx, int cy)
{
float CullDist;
COpenGLWnd::OnSize(nType, cx, cy);
if ( 0 >= cx || 0 >= cy || nType == SIZE_MINIMIZED )
return;
CullDist = gProfile.GetCullDistance();
SetRenderWindowOptions(CullDist, m_bInvertMouse);
}
void CPerspective::OnMouseMove(UINT nFlags, CPoint point)
{
int DeltaMouseX = point.x - m_lastMouseX;
int DeltaMouseY = point.y - m_lastMouseY;
m_lastMouseX = point.x;
m_lastMouseY = point.y;
if(nFlags & MK_RBUTTON)
{
if(m_bInvertMouse)
m_Camera.UpdateMouseMove(DeltaMouseX, -DeltaMouseY);
else
m_Camera.UpdateMouseMove(DeltaMouseX, DeltaMouseY);
}
//Check for mouse translation editing
if(gSelection.IsObjectSelected())
{
EDITOR_MODE mode;
mode = gSelection.GetCurrentEditorMode();
if(mode == EDITOR_MODE_ROTATE)
{
PerformRotationEdit(nFlags, DeltaMouseX, DeltaMouseY);
}
else if(mode == EDITOR_MODE_TRANSLATE)
{
PerformTranslationEdit(nFlags, point);
}
}
//If we are in look mode, reset mouse cursor position to center of client
if((m_bLookMode)&&((abs(DeltaMouseX) > 1)||(abs(DeltaMouseY) > 1)))
{
RECT rc;
POINT pt;
GetWindowRect(&rc);
pt.x = (rc.right-rc.left)/2;
pt.y = (rc.bottom-rc.top)/2;
m_lastMouseX = pt.x;
m_lastMouseY = pt.y;
ClientToScreen(&pt);
SetCursorPos(pt.x, pt.y);
if(m_bInvertMouse)
m_Camera.UpdateMouseMove(DeltaMouseX, -DeltaMouseY);
else
m_Camera.UpdateMouseMove(DeltaMouseX, DeltaMouseY);
}
COpenGLWnd::OnMouseMove(nFlags, point);
}
void CPerspective::SetCameraSpeed(float scale)
{
m_fCameraSpeed = scale;
}
void CPerspective::RefreshWindow()
{
float x, y, z;
gBspManager.GetActiveBspCentroid(&x, &y, &z);
TRACE("map center = %f %f %f\n", x, y, z);
m_Camera.PositionCamera(x+20, y+20, z+20,
x,y,z,
0, 0, 1);
OnDraw(NULL);
}
void CPerspective::CheckKeyboard()
{
BOOL bUpdateCam = FALSE;
BOOL bMovedCam = FALSE;
float move[3] = {0,0,0};
float pitch = 0;
float roll = 0;
float yaw = 0;
CWnd *pCurrentWnd = GetFocus();
// if user has held key down, accelerate the camera (long distance movement)
float rendertime = gRender.GetFrameTime();
// TRACE("render_time = %f\n", rendertime);
if((m_CameraAccTimer.GetDeltaTime() - rendertime) < 0.1)
{
m_CameraAccCount++;
}
else
{
m_CameraAccCount = 0;
m_fCameraAcc = 0.3f;
}
if(m_CameraAccCount > 5)
{
m_fCameraAcc += 0.1f*m_fCameraSpeed;
}
if(m_fCameraAcc > m_CameraAccMax)
m_fCameraAcc = m_CameraAccMax;
//check keyboard
if(pCurrentWnd == this)
{
if(GetAsyncKeyState(VK_ESCAPE) & 0x8000)
m_bLookMode = FALSE;
if(GetAsyncKeyState('W') & 0x8000)
{
m_Camera.MoveCamera(m_fCameraSpeed*m_fCameraAcc);
bMovedCam = TRUE;
}
if(GetAsyncKeyState('S') & 0x8000)
{
m_Camera.MoveCamera(-m_fCameraSpeed*m_fCameraAcc);
bMovedCam = TRUE;
}
if(GetAsyncKeyState('D') & 0x8000)
{
m_Camera.StrafeCamera(m_fCameraSpeed*m_fCameraAcc);
bMovedCam = TRUE;
}
if(GetAsyncKeyState('A') & 0x8000)
{
m_Camera.StrafeCamera(-m_fCameraSpeed*m_fCameraAcc);
bMovedCam = TRUE;
}
if(GetAsyncKeyState(' ') & 0x8000)
{
m_Camera.LevitateCamera(m_fCameraSpeed*m_fCameraAcc);
bMovedCam = TRUE;
}
if(GetAsyncKeyState(VK_SHIFT) & 0x8000)
{
m_Camera.LevitateCamera(-m_fCameraSpeed*m_fCameraAcc);
bMovedCam = TRUE;
}
if(GetAsyncKeyState(VK_LEFT) & 0x8000)
move[0] = 1.0f;
if(GetAsyncKeyState(VK_RIGHT) & 0x8000)
move[0] = -1.0;
if(GetAsyncKeyState(VK_UP) & 0x8000)
move[1] = 1.0f;
if(GetAsyncKeyState(VK_DOWN) & 0x8000)
move[1] = -1.0f;
if(GetAsyncKeyState(VK_PRIOR) & 0x8000)
move[2] = 1.0f;
if(GetAsyncKeyState(VK_NEXT) & 0x8000)
move[2] = -1.0f;
if((move[0]!=0)||(move[1]!=0)||(move[2]!=0)||(pitch!=0)||(roll!=0)||(yaw!=0))
gSelection.MoveSelection(move[0], move[1], move[2], 0, 0, 0, TRUE);
gSelection.SetEditCursorMode(move);
if(bMovedCam)
m_CameraAccTimer.ResetTimer();
}
}
void CPerspective::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
//call this when a key is pressed to update the camera
//OnDraw(NULL);
if(nChar == 9)
{
gSelection.ToggleEditorMode();
}
COpenGLWnd::OnChar(nChar, nRepCnt, nFlags);
}
void CPerspective::OnTimer(UINT nIDEvent)
{
if(nIDEvent == FRAME_UPDATE_TIMER)
{
OnDraw(NULL);
}
COpenGLWnd::OnTimer(nIDEvent);
}
void CPerspective::OnInitialUpdate()
{
SetTimer(FRAME_UPDATE_TIMER, 50, 0);
}
void CPerspective::OnLButtonUp(UINT nFlags, CPoint point)
{
gSelection.PerformSelectionTest(point.x, point.y);
COpenGLWnd::OnLButtonUp(nFlags, point);
}
void CPerspective::GetCameraPosition(float *pPosition)
{
m_Camera.GetCameraPosition(pPosition);
}
void CPerspective::SetRenderWindowOptions(float cull_dist, BOOL bInvertMouse)
{
RECT rc;
m_bInvertMouse = bInvertMouse;
GetClientRect(&rc);
float x = rc.right - rc.left;
float y = rc.bottom - rc.top;
SetContext();
glViewport( 0, 0, x, y);
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluPerspective(45.0f, x/y, 0.1f, cull_dist);
glMatrixMode( GL_MODELVIEW );
}
void CPerspective::OnRButtonDblClk(UINT nFlags, CPoint point)
{
m_bLookMode ^= 1;
COpenGLWnd::OnRButtonDblClk(nFlags, point);
}
void CPerspective::SetCamForModel(BOOL bModelMode, float dim)
{
if(bModelMode)
{
m_Camera.PositionCamera(1.5f*dim, 1.5f*dim, 1.5f*dim, 0, 0, 0, 0, 0, 1);
}
else //use stored position
{
m_Camera.RestoreSavedPosition();
}
}
float CPerspective::GetCameraSpeed(void)
{
float temp;
temp = m_fCameraSpeed;
return(temp);
}
void CPerspective::EnableRotationEdit(BOOL bEnable)
{
m_bEnableRotationMode = bEnable;
}
void CPerspective::PerformTranslationEdit(UINT nFlags, CPoint point)
{
//calculate mouse-movement of selected object
float move_x = 0;
float move_y = 0;
float move_z = 0;
float ox,oy,oz;
float cam_pos[3];
float sel_pos[3];
float obj_pos[6];
ox = 0;
oy = 0;
oz = 0;
ZeroMemory(cam_pos, 12);
ZeroMemory(sel_pos, 12);
ZeroMemory(obj_pos, 12);
//use last selected object's reference planes to figure out where to move the object
gSelection.GetReferenceCoordinate(obj_pos);
m_Camera.DoUnProject(point.x, point.y, &ox, &oy, &oz, 0.5, FALSE);
m_Camera.GetCameraPosition(cam_pos);
if(GetAsyncKeyState(VK_CONTROL) & 0x8000) //Up-Down Object Movement
{
//do calculation to determine selection plane intersect point for XZ plane
if((cam_pos[1] - oy) != 0)
{
float u = (cam_pos[1] - obj_pos[1])/(cam_pos[1] - oy);
sel_pos[0] = cam_pos[0] + u*(ox - cam_pos[0]);
sel_pos[1] = cam_pos[1] + u*(oy - cam_pos[1]);
sel_pos[2] = cam_pos[2] + u*(oz - cam_pos[2]);
move_x = 0;
move_y = 0;
move_z = sel_pos[2] - m_LastSelectionPos[2];
}
}
else //X-Y Object Movement
{
//do calculation to determine selection plane intersect point for XY plane
if((cam_pos[2] - oz) != 0)
{
float u = (cam_pos[2] - obj_pos[2])/(cam_pos[2] - oz);
sel_pos[0] = cam_pos[0] + u*(ox - cam_pos[0]);
sel_pos[1] = cam_pos[1] + u*(oy - cam_pos[1]);
sel_pos[2] = cam_pos[2] + u*(oz - cam_pos[2]);
move_x = sel_pos[0] - m_LastSelectionPos[0];
move_y = sel_pos[1] - m_LastSelectionPos[1];
move_z = 0;
}
}
m_LastSelectionPos[0] = sel_pos[0];
m_LastSelectionPos[1] = sel_pos[1];
m_LastSelectionPos[2] = sel_pos[2];
//TRACE("z-plane = %f\n", zplane);
//TRACE("%.3f %.3f %0.3f\n",move_x,move_y, move_z);
//TRACE("%.3f %.3f %0.3f %.3f %.3f %0.3f\n",sel_pos[0], sel_pos[1], sel_pos[2],move_x,move_y, move_z);
if(nFlags & MK_LBUTTON)
{
gSelection.MoveSelection(move_x, move_y, move_z,0,0,0, FALSE);
}
}
void CPerspective::PerformRotationEdit(UINT nFlags, int dx, int dy)
{
float roll = 0;
float pitch = 0;
float yaw = 0;
float adx = abs(dx);
float ady = abs(dy);
if(GetAsyncKeyState(VK_CONTROL) & 0x8000)
{
if(dx > 5)dx = 5;
if(dx < -5)dx = -5;
pitch = dx*DEG_TO_RAD*0.05;
if(dy > 5)dy = 5;
if(dy < -5)dy = -5;
roll = dy*DEG_TO_RAD*0.05;
}
else
{
if(dx > 5)dx = 5;
if(dx < -5)dx = -5;
yaw = dx*DEG_TO_RAD*0.05;
}
if(nFlags & MK_LBUTTON)
{
gSelection.MoveSelection(0, 0, 0, yaw, pitch, roll, FALSE);
}
}
BOOL CPerspective::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)
{
gSelection.ToggleEditorMode();
return COpenGLWnd::OnMouseWheel(nFlags, zDelta, pt);
}