-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMoebius.cpp
692 lines (598 loc) · 15.6 KB
/
Moebius.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
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
//==========================================================================
//
// File: Mobius.cpp
//
//==========================================================================
#include <windows.h>
#include <stdio.h>
#include "afxres.h"
#include "resource.h"
#include "OpenGLenv.h"
#define TIMERID 1000
#define UPDATE_TIME 10
#define SIGN_EXTEND(w) ((((int)(w)) << 16) >> 16)
/****************************************************************************/
#ifndef _SIM_DLL
int style = WS_OVERLAPPEDWINDOW;
#else
int style = WS_CHILD | WS_CAPTION; // style
char* str_DllName = "SimOpenGLDLL";
#endif
/****************************************************************************/
// Forward function declarations
static BOOL FirstInstance(HINSTANCE);
static BOOL AnyInstance(HWND&, HINSTANCE, int);
long FAR PASCAL WindowProc(HWND, UINT, WPARAM, LPARAM);
/****************************************************************************/
// Frame rate stuff
DWORD g_dwBegTime = 0;
DWORD g_dwLastTime = 0;
DWORD g_dwCurTime = 0;
DWORD g_dwFpsTime = 0;
DWORD g_dwDeltaTime = 0;
DWORD g_dwFramesRendered = 0;
DWORD g_dwFps = 0;
DWORD g_faces = 0;
/****************************************************************************/
HINSTANCE _prev_inst = NULL;
LPSTR _cmdline = NULL;
int _cmdshow = SW_SHOW;
int hChildLeft = 0;
int hChildTop = 0;
int hChildWidth = 512;
int hChildHeight= 512;
HWND hParwin = NULL;
HWND hWin = NULL; // Child application
HDC hdc = NULL;
HGLRC hglrc = NULL;
static char ClassName[32] = "OpenGLClass";
static char CaptionName[32] = "OpenGL Moebius Clock";
BOOL done = FALSE; // If simulation should be finished
BOOL bQuit = FALSE; // Set if we are quitting
BOOL bMovement = FALSE;
extern GraphicEnvState state;
/****************************************************************************/
//
// Register the window class for the application. Also perform other
// desired application initialization
//
static BOOL
FirstInstance(HINSTANCE this_inst)
{
// Contains the window class attributes to be registered
WNDCLASS wc;
// Status of the RegisteredClass call
BOOL rc;
//
// Fill the slots of and register the window class
//
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WindowProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = sizeof(DWORD);
wc.hInstance = this_inst;
wc.hIcon = LoadIcon(this_inst, MAKEINTRESOURCE(IDI_ICON1));
//wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = "OpenGLMenu";
wc.lpszClassName = ClassName;
rc = RegisterClass(&wc);
// Return the initialized object
return rc;
}
/****************************************************************************/
//
// Perform the setup required for every instance of the application -
// Creation of the window and initialization of the data.
//
static BOOL
AnyInstance(HWND& win, HINSTANCE this_inst, int cmdshow)
{
HMENU hmenu = LoadMenu(this_inst, MAKEINTRESOURCE(IDR_MENU1));
//
// Create the main window
//
win = CreateWindow(
ClassName, // class
CaptionName, // caption
style, // style
hChildLeft, // init. x pos
hChildTop, // init. y pos
hChildWidth, // init. x size
hChildHeight, // init. y size
hParwin, // parent window
hmenu, // menu handle
this_inst, // program handle
NULL // create params
);
if (!win) return FALSE;
if (!InitGraphicEnv(hWin, &hdc, &hglrc)) return FALSE;
InitState();
InitTexture();
InitDisplayLists();
SetTimer(win, TIMERID, UPDATE_TIME, NULL);
//
// Show and update the window
//
ShowWindow(win, cmdshow);
UpdateWindow(win);
return TRUE;
}
/****************************************************************************/
BOOL
InitWindow(HINSTANCE this_inst, HWND hPar, int xleft, int xright, int width, int height)
{
hParwin = hPar;
hChildLeft = xleft;
hChildTop = xright;
hChildWidth = width;
hChildHeight = height;
g_dwBegTime = timeGetTime();
// If there is no previous instance set, register the window class
// for the application
if (!_prev_inst)
if (!FirstInstance(this_inst))
return FALSE;
// If we cannot do the remaining window setup, return an error
if (!AnyInstance(hWin, this_inst, _cmdshow))
return FALSE;
return TRUE;
}
/****************************************************************************/
BOOL
ReInitWindow()
{
KillTimer(hWin, TIMERID);
DestroyGraphicEnv(hWin, hdc, hglrc);
CloseWindow(hWin);
return TRUE;
}
/****************************************************************************/
int
WinOpen(HINSTANCE this_inst, HWND hPar, int xleft, int xright, int width, int height)
{
MSG msg;
if (InitWindow(this_inst, hPar, xleft, xright, width, height) == FALSE)
return FALSE;
// Loop until we are done - either the user quits or an error occurs
while (!done)
{
// Check the thread message queue for a message and place it in
// message if there is one.
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
if (msg.message == WM_QUIT)
{
done = TRUE;
break;
}
// Translate the virtual key messages into character messages
TranslateMessage(&msg);
// Dispatch the message to the window proceedure
DispatchMessage(&msg);
}
}
ReInitWindow();
return msg.wParam;
}
/****************************************************************************/
//
// This function is the main routine containing the message loop.
// Here we call the functions to create, initialize, and activate
// the program's main window and handle the various messages.
//
int PASCAL
WinMain(HINSTANCE this_inst, HINSTANCE prev_inst, LPSTR cmdline, int cmdshow)
{
_prev_inst = prev_inst;
_cmdline = cmdline;
_cmdshow = cmdshow;
return WinOpen(this_inst, NULL, 0, 0, 512, 512);
}
/****************************************************************************/
BOOL
TimerProc(HWND hwnd)
{
if (!bQuit)
{
// Try to render the screen
g_dwCurTime = timeGetTime();
// Perform some timing stuff
g_dwDeltaTime = g_dwCurTime - g_dwLastTime;
g_dwLastTime = g_dwCurTime;
g_dwFpsTime += g_dwDeltaTime;
g_dwFramesRendered++;
// Update every second
if (g_dwFpsTime > 1000)
{
char str[256];
g_dwFps = g_dwFramesRendered;
g_dwFramesRendered = 0;
sprintf ( (LPSTR)str, "OpenGLDemo - FPS: %.3f",
1000.0*(float)g_dwFps/(float)g_dwFpsTime );
SetWindowText(hWin, (LPSTR)str);
g_dwFpsTime = 0;
}
UpdateTime();
InvalidateRect(hwnd, NULL, FALSE);
}
return TRUE;
}
/****************************************************************************/
//
// Process the messages for the 'about' dialog.
//
BOOL FAR PASCAL
AboutDlgProc(HWND win, unsigned msg, WORD wparam, LONG lparam)
{
// Check the message
switch (msg)
{
// If we received an InitDialog message, just return true
case WM_INITDIALOG:
return TRUE;
// If we received an OK button selection,
// close the dialog and return true
case WM_COMMAND:
if (wparam == IDOK)
{
EndDialog(win, TRUE);
return TRUE;
}
break;
}
// Otherwise, return false
return FALSE;
}
/****************************************************************************/
void
OnCreate(HWND win)
{
CheckMenuItem(
GetMenu(win), ID_VIEW_ANTIALIAS,
state.bSmooth ? MF_CHECKED : MF_UNCHECKED
);
CheckMenuItem(
GetMenu(win), ID_VIEW_BLEND,
state.bBlend ? MF_CHECKED : MF_UNCHECKED
);
CheckMenuItem(
GetMenu(win), ID_VIEW_CULL,
state.bCulling ? MF_CHECKED : MF_UNCHECKED
);
CheckMenuRadioItem(
GetMenu(win), ID_VIEW_METHOD_NICEST, ID_VIEW_METHOD_FASTEST,
state.nLineSmoothHint == GL_NICEST ? ID_VIEW_METHOD_NICEST :
ID_VIEW_METHOD_FASTEST, MF_BYCOMMAND
);
CheckMenuRadioItem(
GetMenu(win),
ID_VIEW_SHADE_FLAT, ID_VIEW_SHADE_GOURARD,
state.nShadeModel == GL_SMOOTH ? ID_VIEW_SHADE_GOURARD :
ID_VIEW_SHADE_FLAT, MF_BYCOMMAND
);
CheckMenuItem(
GetMenu(win), ID_VIEW_LINEAR,
state.nLinear == 1 ? MF_CHECKED : MF_UNCHECKED
);
CheckMenuItem(GetMenu(win), ID_VIEW_MIDMAPS, MF_CHECKED);
CheckMenuRadioItem(
GetMenu(win),
ID_VIEW_PERSP_NICEST, ID_VIEW_PERSP_FASTEST,
state.nPerspCorrectionHint == GL_NICEST ? ID_VIEW_PERSP_NICEST :
ID_VIEW_PERSP_FASTEST, MF_BYCOMMAND
);
CheckMenuItem(
GetMenu(win), ID_VIEW_NORMALS,
state.bNormals ? MF_CHECKED : MF_UNCHECKED
);
CheckMenuItem(
GetMenu(win), ID_VIEW_QUICK,
state.bQuick ? MF_CHECKED : MF_UNCHECKED
);
CheckMenuItem(
GetMenu(win), ID_VIEW_SWING,
state.bSwing ? MF_CHECKED : MF_UNCHECKED
);
CheckMenuItem(
GetMenu(win), ID_VIEW_ZBUFFER,
state.bDepthTest ? MF_CHECKED : MF_UNCHECKED
);
CheckMenuItem(
GetMenu(win), ID_VIEW_WIREFRAME,
state.nPolygonMode == GL_LINE ? MF_CHECKED : MF_UNCHECKED
);
}
/****************************************************************************/
void
OnCommand(HWND win, WPARAM wparam, LPARAM lparam)
{
switch( wparam & 0xffff )
{
case ID_FILE_EXIT:
PostQuitMessage(0);
bQuit = TRUE;
break;
case ID_VIEW_ANTIALIAS:
state.bSmooth = !state.bSmooth;
CheckMenuItem(
GetMenu(win), ID_VIEW_ANTIALIAS,
state.bSmooth ? MF_CHECKED : MF_UNCHECKED
);
EnableGraphicOption(GL_LINE_SMOOTH, state.bSmooth);
EnableGraphicOption(GL_POLYGON_SMOOTH, state.bSmooth);
break;
case ID_VIEW_BLEND:
state.da = -state.da;
state.bBlend = !state.bBlend;
CheckMenuItem(
GetMenu(win), ID_VIEW_BLEND,
state.bBlend ? MF_CHECKED : MF_UNCHECKED
);
break;
case ID_VIEW_CULL:
state.bCulling = !state.bCulling;
CheckMenuItem(
GetMenu(win), ID_VIEW_CULL,
state.bCulling ? MF_CHECKED : MF_UNCHECKED
);
EnableGraphicOption(GL_CULL_FACE, state.bCulling);
break;
case ID_VIEW_METHOD_NICEST:
if (state.nLineSmoothHint != GL_NICEST)
{
state.nLineSmoothHint = GL_NICEST;
CheckMenuRadioItem(
GetMenu(win), ID_VIEW_METHOD_NICEST, ID_VIEW_METHOD_FASTEST,
ID_VIEW_METHOD_NICEST, MF_BYCOMMAND
);
glHint(GL_LINE_SMOOTH_HINT, state.nLineSmoothHint);
}
break;
case ID_VIEW_METHOD_FASTEST:
if (state.nLineSmoothHint != GL_FASTEST)
{
state.nLineSmoothHint = GL_FASTEST;
CheckMenuRadioItem(
GetMenu(win), ID_VIEW_METHOD_NICEST, ID_VIEW_METHOD_FASTEST,
ID_VIEW_METHOD_FASTEST, MF_BYCOMMAND
);
glHint(GL_LINE_SMOOTH_HINT, state.nLineSmoothHint);
}
break;
case ID_VIEW_SHADE_FLAT:
if (state.nShadeModel != GL_FLAT)
{
state.nShadeModel = GL_FLAT;
CheckMenuRadioItem(
GetMenu(win),
ID_VIEW_SHADE_FLAT, ID_VIEW_SHADE_GOURARD,
ID_VIEW_SHADE_FLAT, MF_BYCOMMAND
);
glShadeModel(state.nShadeModel);
}
break;
case ID_VIEW_SHADE_GOURARD:
if (state.nShadeModel != GL_SMOOTH)
{
state.nShadeModel = GL_SMOOTH;
CheckMenuRadioItem(
GetMenu(win),
ID_VIEW_SHADE_FLAT, ID_VIEW_SHADE_GOURARD,
ID_VIEW_SHADE_GOURARD, MF_BYCOMMAND
);
glShadeModel(state.nShadeModel);
}
break;
case ID_VIEW_LINEAR:
state.nLinear = (state.nLinear == 1 ? 0 : 1);
CheckMenuItem(
GetMenu(win), ID_VIEW_LINEAR,
state.nLinear == 1 ? MF_CHECKED : MF_UNCHECKED
);
break;
case ID_VIEW_PERSP_NICEST:
if (state.nPerspCorrectionHint != GL_NICEST)
{
state.nPerspCorrectionHint = GL_NICEST;
CheckMenuRadioItem(
GetMenu(win),
ID_VIEW_PERSP_NICEST, ID_VIEW_PERSP_FASTEST,
ID_VIEW_PERSP_NICEST, MF_BYCOMMAND
);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, state.nPerspCorrectionHint);
}
break;
case ID_VIEW_PERSP_FASTEST:
if (state.nPerspCorrectionHint != GL_FASTEST)
{
state.nPerspCorrectionHint = GL_FASTEST;
CheckMenuRadioItem(
GetMenu(win),
ID_VIEW_PERSP_NICEST, ID_VIEW_PERSP_FASTEST,
ID_VIEW_PERSP_FASTEST, MF_BYCOMMAND
);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, state.nPerspCorrectionHint);
}
break;
case ID_VIEW_NORMALS:
state.bNormals = !state.bNormals;
CheckMenuItem(
GetMenu(win), ID_VIEW_NORMALS,
state.bNormals ? MF_CHECKED : MF_UNCHECKED
);
break;
case ID_VIEW_QUICK:
state.bQuick = !state.bQuick;
CheckMenuItem(
GetMenu(win), ID_VIEW_QUICK,
state.bQuick ? MF_CHECKED : MF_UNCHECKED
);
break;
case ID_VIEW_SWING:
state.bSwing = !state.bSwing;
CheckMenuItem(
GetMenu(win), ID_VIEW_SWING,
state.bSwing ? MF_CHECKED : MF_UNCHECKED
);
break;
case ID_VIEW_ZBUFFER:
state.bDepthTest = !state.bDepthTest;
CheckMenuItem(
GetMenu(win), ID_VIEW_ZBUFFER,
state.bDepthTest ? MF_CHECKED : MF_UNCHECKED
);
EnableGraphicOption(GL_DEPTH_TEST, state.bDepthTest);
break;
case ID_VIEW_WIREFRAME:
state.nPolygonMode = (state.nPolygonMode == GL_LINE ? GL_FILL : GL_LINE);
CheckMenuItem(
GetMenu(win), ID_VIEW_WIREFRAME,
state.nPolygonMode == GL_LINE ? MF_CHECKED : MF_UNCHECKED
);
glPolygonMode(GL_FRONT_AND_BACK, state.nPolygonMode);
break;
case ID_HELP_ABOUT:
DialogBox(
(HINSTANCE)GetWindowLong(win, GWL_HINSTANCE),
MAKEINTRESOURCE(IDD_ABOUT), win,
(DLGPROC)AboutDlgProc
);
break;
}
InvalidateRect(win, NULL, FALSE);
}
/****************************************************************************/
//
// Process the messages for the main application window
//
LONG FAR PASCAL
WindowProc(HWND win, UINT msg, WPARAM wparam, LPARAM lparam)
{
static BOOL bRoll = FALSE;
static POINT anchor;
// Handle the various message that we received
switch (msg)
{
case WM_CREATE:
OnCreate(win);
break;
case WM_KEYDOWN:
{
// Check for any keyboard key depression
switch (wparam)
{
case VK_SPACE:
ResetOrientationGraphicEnv();
CheckMenuItem(
GetMenu(win), ID_VIEW_SWING,
state.bSwing ? MF_CHECKED : MF_UNCHECKED
);
break;
case 'T':
break;
case VK_UP:
break;
case VK_DOWN:
break;
case VK_RIGHT:
break;
case VK_LEFT:
break;
case VK_HOME:
break;
case VK_END:
break;
case VK_INSERT:
break;
case VK_DELETE:
break;
case VK_PRIOR: // PageUp
break;
case VK_NEXT: // PageDown
break;
case VK_NUMPAD4:
break;
}
}
break;
// If the user releases a key, see which one it was and do the appropriate action
case WM_KEYUP:
switch (wparam)
{
case 'T':
break;
}
break;
case WM_COMMAND:
OnCommand(win, wparam, lparam);
break;
case WM_LBUTTONDOWN:
RECT rc;
bMovement = TRUE;
GetClientRect(win, &rc);
GetCursorPos(&anchor);
ScreenToClient(hWin, &anchor);
bRoll = (GetKeyState(VK_SHIFT) & 0x8000) >> 15;
SetCapture(win);
break;
case WM_LBUTTONUP:
bMovement = FALSE;
bRoll = FALSE;
ReleaseCapture();
break;
case WM_RBUTTONDOWN:
break;
case WM_RBUTTONUP:
break;
case WM_MOUSEMOVE:
if (bMovement & wparam & MK_LBUTTON)
{
POINT mouse;
mouse.x = SIGN_EXTEND(LOWORD(lparam));
mouse.y = SIGN_EXTEND(HIWORD(lparam));
MotionOfGraphicEnv(&anchor, mouse, bRoll);
}
break;
case WM_TIMER:
if (wparam == TIMERID)
TimerProc(win);
break;
// We are quitting and destroying the window
case WM_DESTROY:
PostQuitMessage(0);
bQuit = TRUE;
break;
case WM_SIZE:
{
int width = LOWORD(lparam);
int height = HIWORD(lparam);
if (width && height)
{
Reshape(hdc, width, height);
}
}
break;
case WM_ACTIVATE:
break;
// Handle the window paint message and update the window
case WM_PAINT:
{
RECT r;
PAINTSTRUCT ps;
// Retrieve the coordinates of the smallest rectangle that completely encloses the
// update region of the specified window (win)
if (GetUpdateRect(win, &r, FALSE))
{
BeginPaint(win, &ps);
Display(hdc);
EndPaint(win, &ps);
}
}
default:
// Default to just handling any messages not explicitly handled above
return DefWindowProc(win, msg, wparam, lparam);
}
return 0L;
}