-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGlmfcnView.cpp
More file actions
369 lines (292 loc) · 9.08 KB
/
GlmfcnView.cpp
File metadata and controls
369 lines (292 loc) · 9.08 KB
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
// GlmfcnView.cpp : implementation of the CGlmfcnView class
//
#include "stdafx.h"
#include "Glmfcn.h"
#include "GlmfcnDoc.h"
#include "GlmfcnView.h"
#include <gl/gl.h>
#include <gl/glu.h>
#include "glaux.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CGlmfcnView
IMPLEMENT_DYNCREATE(CGlmfcnView, CView)
BEGIN_MESSAGE_MAP(CGlmfcnView, CView)
ON_WM_CREATE()
ON_WM_SIZE()
ON_WM_ERASEBKGND()
ON_WM_TIMER()
ON_WM_TIMER()
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_RBUTTONDOWN()
ON_WM_MOUSEMOVE()
ON_WM_MBUTTONDBLCLK()
ON_WM_LBUTTONDBLCLK()
// ON_COMMAND(ID_VIEW_DIFFUSECOLOR, &CGlmfcnView::OnViewDiffusecolor)
END_MESSAGE_MAP()
float spin=0.0;
/////////////////////////////////////////////////////////////////////////////
// CGlmfcnView construction/destruction
CGlmfcnView::CGlmfcnView()
{
// TODO: add construction code here
}
CGlmfcnView::~CGlmfcnView()
{
}
BOOL CGlmfcnView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
cs.style |= WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
return CView::PreCreateWindow(cs);
}
BOOL CGlmfcnView::InitializeOpenGL()
{
CGlmfcnDoc* pDoc = GetDocument();
pDoc->m_pDC = new CClientDC(this);
m_hDC = pDoc->m_pDC->GetSafeHdc();
if ( NULL == pDoc->m_pDC ) // failure to get DC
{
::AfxMessageBox("Couldn't get a valid DC.");
return FALSE;
}
if ( !SetupPixelFormat() )
{
::AfxMessageBox("SetupPixelFormat failed.\n");
return FALSE;
}
m_hRC=wglGetCurrentContext();
if (m_hRC==NULL)
if ( 0 == (m_hRC = ::wglCreateContext( m_hDC ) ) )
{
::AfxMessageBox("wglCreateContext failed.");
return FALSE;
}
return makeCurrent();
}
void CGlmfcnView::RenderScene()
{
// Empty - no OpenGL rendering needed
}
BOOL CGlmfcnView::SetupPixelFormat()
{
// default pixel format for a single-buffered,
// OpenGL-supporting, hardware-accelerated,
// RGBA-mode format. Pass in a pointer to a different
// pixel format if you want something else
CGlmfcnDoc* pDoc = GetDocument();
PIXELFORMATDESCRIPTOR pfd =
{
sizeof(PIXELFORMATDESCRIPTOR),// size of this pfd
1, // version number
PFD_DRAW_TO_WINDOW | // support window
PFD_DOUBLEBUFFER | // doubble buffer
PFD_SUPPORT_OPENGL, // support OpenGL
PFD_TYPE_RGBA, // RGBA type
24, // 24-bit color depth
0, 0, 0, 0, 0, 0, // color bits ignored
0, // no alpha buffer
0, // shift bit ignored
0, // no accumulation buffer
0, 0, 0, 0, // accum bits ignored
16, // 16-bit z-buffer
0, // no stencil buffer
0, // no auxiliary buffer
PFD_MAIN_PLANE, // main layer
0, // reserved
0, 0, 0 // layer masks ignored
};
int pixelformat;
PIXELFORMATDESCRIPTOR* pPFDtoUse;
// let the user override the default pixel format
// pPFDtoUse = (0 == pPFD)? &pfd : pPFD;
pPFDtoUse = &pfd ;
if ( 0 == (pixelformat =
::ChoosePixelFormat( m_hDC, pPFDtoUse )) )
{
::AfxMessageBox("ChoosePixelFormat failed.");
return FALSE;
}
if ( FALSE == ::SetPixelFormat( m_hDC,
pixelformat, pPFDtoUse ) )
{
::AfxMessageBox("SetPixelFormat failed.");
return FALSE;
}
return TRUE;
}
void CGlmfcnView::myReshape()
{
CGlmfcnDoc* pDoc = GetDocument();
// compute the aspect ratio
// this will keep all dimension scales equal
pDoc->m_AspectRatio = (GLfloat)pDoc->m_WindowWidth/(GLfloat)pDoc->m_WindowHeight;
// Now, set up the viewing area-select the full client area
::glViewport(0, 0, pDoc->m_WindowWidth, pDoc->m_WindowHeight);
if ( GL_NO_ERROR != ::glGetError() )
{
::AfxMessageBox("Error while trying to set viewport.");
}
// select the projection matrix as the recipient of
// matrix operations (there's three to choose from)
::glMatrixMode(GL_PROJECTION);
// initialize the projection matrix to a pristine state
::glLoadIdentity();
// select the viewing volume. You do it after you
// get the aspect ratio and set the viewport
// gluPerspective( 45.0f, pDoc->m_AspectRatio, 0.1f, 20.0f );
float r=1.0f/pDoc->m_AspectRatio;
if (pDoc->m_WindowWidth <= pDoc->m_WindowHeight)
glOrtho (0, 2.0, 0, 2.0*r, -10.0, 10.0);
else
glOrtho (0, 2.0/r, 0, 2.0, -10.0, 10.0);
glMatrixMode(GL_MODELVIEW);
// NOTE: Other commands you could have here are
// glFrustum, which gives you much more control over
// the perspective view, or glOrtho which is used for
// parallel projections. No matter what you use, check
// the error status when you set the viewing frustum!
if ( GL_NO_ERROR != ::glGetError() )
{
::AfxMessageBox("Error while trying to set viewing frustum.");
return ;
}
// now select the modelview matrix and clear it
// this is the mode we do most of our calculations in
// so we leave it as the default mode.
::glMatrixMode(GL_MODELVIEW);
::glLoadIdentity();
// now perform a default viewing transformations
// ::glTranslatef( 0.0f, 0.0f, -5.0f );
// ::glRotatef( 20.0f, 1.0f, 0.0f, 0.0f );
}
BOOL CGlmfcnView::makeCurrent()
{
CGlmfcnDoc* pDoc = GetDocument();
// HGLRC hglrc = wglGetCurrentContext();
// if (hglrc!=pDoc->m_hRC )
if ( FALSE == ::wglMakeCurrent(m_hDC, m_hRC ) )
{
::AfxMessageBox("wglMakeCurrent failed.");
return FALSE;
}
// specify black as clear color
::glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
// specify the back of the buffer as clear depth
::glClearDepth( 1.0f );
// enable depth testing
::glEnable( GL_DEPTH_TEST );
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CGlmfcnView drawing
void CGlmfcnView::OnDraw(CDC* pDC)
{
CGlmfcnDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
CBrush brush;
brush.CreateSysColorBrush(COLOR_BTNFACE);
for (int i = 0; i < pDoc->rectanglesCount; i++) {
if (pDoc->rectangles[i] != NULL) {
pDC->Rectangle(pDoc->rectangles[i]);
pDC->SelectObject(brush);
}
}
}
void CGlmfcnView::OnRButtonDown(UINT nFlags, CPoint point)
{
CGlmfcnDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
CRect newRectangles[30];
CRect rectRemove = NULL;
for (int i = 0; i < pDoc->rectanglesCount; i++) {
CRect currentRect = pDoc->rectangles[i];
CPoint rectBottomRight = currentRect.BottomRight();
CPoint rectTopLeft = currentRect.TopLeft();
if ((point.x >= rectTopLeft.x && point.x <= rectBottomRight.x)
&& (point.y >= rectTopLeft.y && point.y <= rectBottomRight.y)) {
rectRemove = currentRect;
break;
}
}
for (int i = 0; i < pDoc->rectanglesCount; i++) {
CRect currentRect = pDoc->rectangles[i];
if (currentRect != rectRemove) {
newRectangles[i] = currentRect;
}
}
pDoc->rectanglesCount = sizeof(newRectangles) / sizeof(newRectangles[0]);
*pDoc->rectangles = newRectangles;
Invalidate();
CView::OnRButtonDown(nFlags, point);
}
void CGlmfcnView::OnLButtonDown(UINT nFlags, CPoint point)
{
CGlmfcnDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (pDoc->hasFirstPoint == false) {
pDoc->firstPoint = point;
pDoc->hasFirstPoint = true;
} else {
CRect newRect = new CRect(pDoc->firstPoint, point);
pDoc->rectangles[pDoc->rectanglesCount] = newRect;
pDoc->rectanglesCount++;
pDoc->hasFirstPoint = false;
}
Invalidate();
CView::OnLButtonDown(nFlags, point);
}
/////////////////////////////////////////////////////////////////////////////
// CGlmfcnView diagnostics
#ifdef _DEBUG
void CGlmfcnView::AssertValid() const
{
CView::AssertValid();
}
void CGlmfcnView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CGlmfcnDoc* CGlmfcnView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CGlmfcnDoc)));
return (CGlmfcnDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CGlmfcnView message handlers
int CGlmfcnView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
InitializeOpenGL();
return 0;
}
void CGlmfcnView::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);
// TODO: Add your message handler code here
CGlmfcnDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if ( 0 >= cx || 0 >= cy )
{
return;
}
// save the width and height of the current window
pDoc->m_WindowWidth = cx;
pDoc->m_WindowHeight = cy;
myReshape();
}
BOOL CGlmfcnView::OnEraseBkgnd(CDC* pDC)
{
// TODO: Add your message handler code here and/or call default
return TRUE;//CView::OnEraseBkgnd(pDC);
}