-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGLView.cpp
589 lines (470 loc) · 14.4 KB
/
GLView.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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
// GLView.cpp : implementation of the CGLView class
//
#include "stdafx.h"
#include "mainfrm.h"
#include "GLView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////
// CGLView
IMPLEMENT_DYNCREATE(CGLView, CView)
BEGIN_MESSAGE_MAP(CGLView, CView)
//{{AFX_MSG_MAP(CGLView)
ON_WM_CREATE()
ON_WM_ERASEBKGND()
ON_WM_SIZE()
ON_WM_RBUTTONDOWN()
ON_WM_RBUTTONUP()
ON_WM_MOUSEMOVE()
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
// Palette handlers
ON_WM_PALETTECHANGED()
ON_WM_QUERYNEWPALETTE()
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////
// CGLView construction/destruction
CGLView::CGLView() :
m_hPalette(NULL)
{
ASSERT(FALSE); //HeikoH: do we still need this code?
}
CGLView::~CGLView()
{
}
BOOL CGLView::PreCreateWindow(CREATESTRUCT& cs)
{
// Set up style to suit OpenGL
cs.style |= WS_CLIPCHILDREN | WS_CLIPSIBLINGS;
cs.lpszClass = AfxRegisterWndClass(CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS |
CS_OWNDC,
::LoadCursor(NULL, IDC_ARROW), HBRUSH(COLOR_WINDOW + 1), NULL);
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////
// CGLView printing
BOOL CGLView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CGLView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CGLView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////
// CGLView drawing
void CGLView::OnDraw(CDC* pDC)
{
// Draw the OpenGL scene
}
/////////////////////////////////////////////////////////////////////
// CGLView diagnostics
#ifdef _DEBUG
void CGLView::AssertValid() const
{
CView::AssertValid();
}
void CGLView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////
// CGLView helpers
void CGLView::SetupPalette()
{
LOGICALPALETTE pal = { 0x300, 256 };
BYTE baRed[] = {0, 36, 72, 109, 145, 182, 218, 255};
BYTE baGreen[] = {0, 36, 72, 109, 145, 182, 218, 255};
BYTE baBlue[] = {0, 85, 170, 255};
// Fill our logical palette structure with color data
for ( int iColor = 0; iColor < 256; ++iColor ) {
pal.peaEntries[iColor].peRed =
baRed[iColor & 0x07];
pal.peaEntries[iColor].peGreen =
baGreen[(iColor >> 0x03) & 0x07];
pal.peaEntries[iColor].peBlue =
baBlue[(iColor >> 0x06) & 0x03];
pal.peaEntries[iColor].peFlags = 0;
} // for
// Build our palette
m_hPalette = CreatePalette((LOGPALETTE*)&pal);
}
/////////////////////////////////////////////////////////////////////
// CGLView message handlers required by OpenGL
int CGLView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
// Initialize OpenGL parameters
PIXELFORMATDESCRIPTOR pfd = {
sizeof(PIXELFORMATDESCRIPTOR), // Structure size
1, // Version number
PFD_DRAW_TO_WINDOW | // Property flags
PFD_SUPPORT_OPENGL |
PFD_DOUBLEBUFFER, // (remove if no double buf)
PFD_TYPE_RGBA, // PixelType
24, // 24-bit color
0, 0, 0, 0, 0, 0, // Color bits and shift
0, 0, 0, 0, 0, 0, 0, // Alpha and accum buffer bits
32, // 32-bit depth buffer
0, 0, // No stencil or aux buffer
PFD_MAIN_PLANE, // Layer type
0, // Reserved
0, 0, 0 // Unsupported
};
// Tell GDI to convert device context from Win32 to
// OpenGL.
CClientDC dcClient(this);
int iPixelFormat = ChoosePixelFormat(dcClient.m_hDC,&pfd);
if ( !iPixelFormat ) {
// This system cannot run OpenGL
TRACE("Error retrieving pixel format index...\n");
ASSERT(FALSE);
AfxMessageBox("Cannot initialize OpenGL...quitting.",
MB_OK | MB_ICONERROR);
return -1; // will fail new document creation...
} // if
if ( !SetPixelFormat(dcClient.m_hDC,iPixelFormat,&pfd) ) {
// This system cannot run OpenGL
TRACE("Error setting new pixel format...\n");
ASSERT(FALSE);
AfxMessageBox("Cannot initialize OpenGL...quitting.",
MB_OK | MB_ICONERROR);
return -1; // will fail new document creation...
} // if
// Update the PIXELFORMATDESCRIPTOR structure once
// the device context has been modified.
DescribePixelFormat(dcClient.m_hDC,iPixelFormat,
sizeof(pfd),&pfd);
// The PIXELFORMATDESCRIPTOR has been updated, so we now
// determine whether to create and manage a custom
// palette.
if ( pfd.dwFlags & PFD_NEED_PALETTE ) {
// We do, so build a new palette...
SetupPalette();
} // if
// Create the OpenGL rendering context
m_hRC = wglCreateContext(dcClient.m_hDC);
if ( m_hRC == NULL ) {
// This system cannot run OpenGL
TRACE("Error creating OpenGL rendering context...\n");
ASSERT(FALSE);
AfxMessageBox("Cannot initialize OpenGL...quitting.",
MB_OK | MB_ICONERROR);
return -1; // will fail new document creation...
} // if
// We now make it the current rendering context so
// we might set our clear color
wglMakeCurrent(dcClient.m_hDC,m_hRC);
glClearColor(0.0f,0.0f,0.0f,0.0f); // black
wglMakeCurrent(dcClient.m_hDC,NULL);
return 0;
}
BOOL CGLView::DestroyWindow()
{
// Shut down OpenGL by deleting the rendering context
wglDeleteContext(m_hRC);
// Delete our custom palette, if any was created
if ( m_hPalette != NULL ) {
DeleteObject(m_hPalette);
} // if
return CView::DestroyWindow();
}
BOOL CGLView::OnEraseBkgnd(CDC* pDC)
{
// Don't erase view...let OpenGL handle
// that for us...
return TRUE;
}
void CGLView::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);
m_ClientRect.right = cx;
m_ClientRect.bottom = cy;
// Change OpenGL's rendering context size and
// viewing frustrum.
CClientDC dcClient(this);
//double aspect;
CSize size(cx,cy);
VERIFY(wglMakeCurrent(dcClient.m_hDC,m_hRC));
/* aspect = (cy == 0) ? (double)size.cx : (double)size.cx/(double)size.cy;
glViewport(0,0,size.cx,size.cy);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//glFrustum(-10.0,10.0,-10.0,10.0,18.0,70.0);
gluPerspective(30,aspect,1,500);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0f,0.0f,-65.0f);
wglMakeCurrent(NULL,NULL);*/
nRange = 50.0f;
if(cy == 0)
cy = 1;
//lastWidth = cx;
//lastHeight = cy;
// Set Viewport to window dimensions
glViewport(0, 0, cx, cy);
glMatrixMode(GL_PROJECTION);
// Reset coordinate system
glLoadIdentity();
// Establish clipping volume (left, right, bottom, top, near, far)
if (cx <= cy)
glOrtho (-nRange, nRange, -nRange*cy/cx, nRange*cy/cx, -nRange*5, nRange*5);
else
glOrtho (-nRange*cx/cy, nRange*cx/cy, -nRange, nRange, -nRange*5, nRange*5);
//glScalef(60.0f, 60.0f, 60.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void CGLView::OnPaletteChanged(CWnd* pFocusWnd)
{
CView::OnPaletteChanged(pFocusWnd);
// Manage our palette
if ( m_hPalette ) {
CDC* pDC = GetDC();
SelectPalette(pDC->m_hDC,m_hPalette,FALSE);
RealizePalette(pDC->m_hDC);
ReleaseDC(pDC);
// Redraw
Invalidate();
} // if
}
BOOL CGLView::OnQueryNewPalette()
{
// Manage our palette
if ( m_hPalette ) {
CDC* pDC = GetDC();
SelectPalette(pDC->m_hDC,m_hPalette,FALSE);
RealizePalette(pDC->m_hDC);
ReleaseDC(pDC);
// Redraw
Invalidate();
} // if
return CView::OnQueryNewPalette();
}
/////////////////////////////////////////////////////////////////////
// Non-OpenGL CGLView message handlers
void CGLView::DrawCartAxis()
{
glPushMatrix();
glNewList(Axis = glGenLists(1), GL_COMPILE);
glBegin(GL_LINES);
// light red x axis arrow
glColor3f(1.f,0.0f,0.0f);
glVertex3f(0.0f,0.0f,0.0f);
glVertex3f(1.0f,0.0f,0.0f);
// light green y axis arrow
glColor3f(0.0f,1.f,0.0f);
glVertex3f(0.0f,0.0f,0.0f);
glVertex3f(0.0f,1.0f,0.0f);
// light blue z axis arrow
glColor3f(0.0f,0.0f,1.f);
glVertex3f(0.0f,0.0f,0.0f);
glVertex3f(0.0f,0.0f,1.0f);
/*glEnd();
glBegin(GL_LINES);*/
// x letter & arrowhead
glColor3f(1.f,0.0f,0.0f);
glVertex3f(1.1f,0.1f,0.0f);
glVertex3f(1.3f,-0.1f,0.0f);
glVertex3f(1.3f,0.1f,0.0f);
glVertex3f(1.1f,-0.1f,0.0f);
glVertex3f(1.0f,0.0f,0.0f);
glVertex3f(0.9f,0.1f,0.0f);
glVertex3f(1.0f,0.0f,0.0f);
glVertex3f(0.9f,-0.1f,0.0f);
// y letter & arrowhead
glColor3f(.0f,1.f,0.0f);
glVertex3f(-0.1f,1.3f,0.0f);
glVertex3f(0.f,1.2f,0.0f);
glVertex3f(0.1f,1.3f,0.0f);
glVertex3f(0.f,1.2f,0.0f);
glVertex3f(0.f,1.2f,0.0f);
glVertex3f(0.f,1.1f,0.0f);
glVertex3f(0.0f,1.0f,0.0f);
glVertex3f(0.1f,0.9f,0.0f);
glVertex3f(0.0f,1.0f,0.0f);
glVertex3f(-0.1f,0.9f,0.0f);
// z letter & arrowhead
glColor3f(.0f,.0f,1.f);
glVertex3f(0.0f,-0.1f,1.3f);
glVertex3f(0.0f,-0.1f,1.1f);
glVertex3f(0.0f,-0.1f,1.1f);
glVertex3f(0.0f,0.1f,1.3f);
glVertex3f(0.0f,0.1f,1.3f);
glVertex3f(0.0f,0.1f,1.1f);
glVertex3f(0.0f,0.0f,1.0f);
glVertex3f(0.0f,0.1f,0.9f);
glVertex3f(0.0f,0.0f,1.0f);
glVertex3f(0.0f,-0.1f,0.9f);
glEnd();
glEndList();
//glPushMatrix();
}
void CGLView::DrawGrid()
{
// Draw the 3 center grid axis lines
glColor3d(0.3,0.3,0.3);
glLineWidth(2.0);
glBegin(GL_LINES);
glVertex3f(0.0,21.0,0.0);
glVertex3f(0.0,-21.0,0.0);
glVertex3f(21.0,0.0,0.0);
glVertex3f(-21.0,0.0,0.0);
glVertex3f(0.0,0.0,21.0);
glVertex3f(0.0,0.0,-21.0);
glEnd();
// Draw the grid lines
glColor3d(0.2,0.2,0.2);
glLineWidth(1.0);
for(float f = 1.0;f<=20.0;f++)
{
glBegin(GL_LINES);
glVertex3f(20.0,f,0.0); //XY Grid
glVertex3f(-20.0,f,0.0);
glVertex3f(20.0,-f,0.0);
glVertex3f(-20.0,-f,0.0);
glVertex3f(f,20.0,0.0);
glVertex3f(f,-20.0,0.0);
glVertex3f(-f,20.0,0.0);
glVertex3f(-f,-20.0,0.0);
glVertex3f(f,0.0,20.0); //XZ Grid
glVertex3f(f,0.0,-20.0);
glVertex3f(-f,0.0,20.0);
glVertex3f(-f,0.0,-20.0);
glVertex3f(20.0,0.0,f);
glVertex3f(-20.0,0.0,f);
glVertex3f(20.0,0.0,-f);
glVertex3f(-20.0,0.0,-f);
glVertex3f(0.0,f,20.0); //yz grid
glVertex3f(0.0,f,-20.0);
glVertex3f(0.0,-f,20.0);
glVertex3f(0.0,-f,-20.0);
glVertex3f(0.0,20.0,f);
glVertex3f(0.0,-20.0,f);
glVertex3f(0.0,20.0,-f);
glVertex3f(0.0,-20.0,-f);
glEnd();
}
glLineWidth(1.0); //resets the line width
///////////////
}
void CGLView::RenderIndicator()
{
glRotatef(m_xRotation, 1.0, 0.0, 0.0);
glRotatef(m_yRotation, 0.0, 1.0, 0.0);
glPushMatrix();
//"superimpose" the RGB-carthesian-axes in a small area near the bottom-left corner of the view
glViewport(m_ClientRect.right - (m_ClientRect.right/6),0,m_ClientRect.right/6,m_ClientRect.bottom/6);
//
glPopMatrix();
glScalef(30.0,30.0,30.0);
glPushAttrib(GL_ENABLE_BIT);
glDisable(GL_DEPTH_TEST);
glDisable(GL_LIGHTING);
glCallList(Axis);
glPopAttrib();
glViewport(0,0,m_ClientRect.right,m_ClientRect.bottom);
glPopMatrix();
}
void CGLView::OnRButtonDown(UINT nFlags, CPoint point)
{
m_RightButtonDown = TRUE;
m_RightDownPos = point;
if(nFlags == 10)
{
m_RBControl = TRUE;
}
CView::OnRButtonDown(nFlags, point);
}
void CGLView::OnRButtonUp(UINT nFlags, CPoint point)
{
m_RightButtonDown = FALSE;
m_RBControl = FALSE;
CView::OnRButtonUp(nFlags, point);
}
void CGLView::OnMouseMove(UINT nFlags, CPoint point)
{
static int last_x = 0;
static int last_y = 0;
float ZoomFactor = 0.0;
//int delta_x = point.x-last_x;
//int delta_y = point.y-last_y;
CMainFrame *viewFrame=static_cast<CMainFrame*>(GetParentFrame());
if(m_LeftButtonDown) // Zoom
{
ZoomFactor = (float)(m_LeftDownPos.y - point.y)/100.0f;
m_xScaling+=ZoomFactor;
m_yScaling+=ZoomFactor;
m_zScaling+=ZoomFactor;
if(m_xScaling < (float)0.01) m_xScaling = (float)0.01;
if(m_yScaling < (float)0.01) m_yScaling = (float)0.01;
if(m_zScaling < (float)0.01) m_zScaling = (float)0.01;
m_LeftDownPos = point;
InvalidateRect(NULL,FALSE);
viewFrame->m_ZoomLevel = m_xScaling; //for the status of the zoom level
}
if(m_RightButtonDown && !m_RBControl) //Rotation of the scene
{
m_yRotation -= (float)(m_RightDownPos.x - point.x)/2.0f;
m_xRotation -= (float)(m_RightDownPos.y - point.y)/2.0f;
m_RightDownPos = point;
if(m_yRotation < 0) m_yRotation = m_yRotation + 360;
if(m_yRotation >= 360) m_yRotation = m_yRotation - 360;
if(m_xRotation < 0) m_xRotation = m_xRotation + 360;
if(m_xRotation >= 360) m_xRotation = m_xRotation - 360;
InvalidateRect(NULL,FALSE);
viewFrame->m_YRotation = m_yRotation; // for the status of the rotation.
viewFrame->m_XRotation = m_xRotation;
}
if(m_RBControl) //panning
{
if(last_x > point.x)
{
m_xTranslation += (float)(point.x - last_x)/(float)7.5;
}
else
{
m_xTranslation -= (float)(last_x - point.x)/(float)7.5;
}
if(last_y > point.y)
{
m_yTranslation -= (float)(point.y - last_y)/(float)7.5;
}
else
{
m_yTranslation += (float)(last_y - point.y)/(float)7.5;
}
InvalidateRect(NULL,FALSE);
viewFrame->m_YTranslation = m_yTranslation; // for the status of the panning.
viewFrame->m_XTranslation = m_xTranslation;
}
last_x = point.x;
last_y = point.y;
CView::OnMouseMove(nFlags, point);
}
void CGLView::OnLButtonDown(UINT nFlags, CPoint point)
{
m_LeftButtonDown = TRUE;
m_LeftDownPos = point;
CView::OnLButtonDown(nFlags, point);
}
void CGLView::OnLButtonUp(UINT nFlags, CPoint point)
{
m_LeftButtonDown = FALSE;
CView::OnLButtonUp(nFlags, point);
}