-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple_gl.cpp
511 lines (426 loc) · 10.6 KB
/
simple_gl.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
#include "simple_gl.h"
#include <cmath>
#include <cstdio>
#include <vector>
#include <string>
#include <cassert>
#ifdef _WIN32
#include <windows.h>
#endif
using namespace std;
#include "Matrix44.h"
#include "resource.h"
#include "ui.h"
#include "drawtext.h"
#include "parameters.h"
#include "simple_gl_common.h"
#include "camera.h"
extern bool init();
extern void draw();
extern void exit();
//in main.cpp (TODO: put that in some header?)
void loadFile(const string& name, bool merge = false);
static bool g_done = false, g_isInited = false, g_initCalled = false;
#ifdef _WIN32
static HINSTANCE g_hInst = NULL;
static HWND g_hWnd = NULL;
static HDC g_hDc = NULL;
static HGLRC g_hContext = NULL;
static GLYPHMETRICS g_glyphMetrics[256];
static TEXTMETRIC g_fontMetrics;
#endif
static int g_width = 0, g_height = 0;
GLuint g_fontBase;
static bool g_isInputOn = false;
static bool g_isInputComplete = true;
static string g_input;
static string g_startupText;
int getWindowWidth()
{
return g_width;
}
int getWindowHeight()
{
return g_height;
}
bool isInputInProgress()
{
return g_isInputOn;
}
string getCommand()
{
if(g_isInputComplete)
{
string ret = g_input;
g_input = "";
return ret;
}
else
return "";
}
void flush()
{
//draw strings
if(!isKeyPressed(VK_TAB))
{
//use g_glyphMetrics[currChar].gmCellIncX for char width?
renderTexts(g_width, g_height,
g_fontMetrics.tmAveCharWidth, g_fontMetrics.tmHeight,
g_fontBase, 1.f/getAverageSecondsPerFrame());
}
glFlush();
SwapBuffers(g_hDc);
}
HWND getHWnd()
{
return g_hWnd;
}
void setStartupText(const std::string& text)
{
if(g_isInited)
return; //otherwise, once the second model is loaded,
//the message pump in this function would
//call draw on a half loaded model, causing a crash
g_startupText = text;
InvalidateRect(getHWnd(), NULL, TRUE);
UpdateWindow(getHWnd());
//pump some messages
MSG msg;
while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
bool isKeyPressed(int key)
{
return (GetAsyncKeyState(key) & 0x8000) != 0;
}
bool init3D()
{
//get window dc
g_hDc = GetDC(g_hWnd);
if(g_hDc == NULL)
return false;
int bpp = GetDeviceCaps(g_hDc, BITSPIXEL);
//set pixel format
PIXELFORMATDESCRIPTOR pfd;
memset(&pfd, 0, sizeof(pfd));
pfd.nSize = sizeof(pfd);
pfd.nVersion = 1;
pfd.cColorBits = (bpp >= 24)?24:16;
pfd.cAlphaBits = (bpp == 32)?8:0;
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cDepthBits = (bpp >= 24)?24:16;
pfd.cStencilBits = (bpp == 32)?8:0;
int formatIndex = ChoosePixelFormat(g_hDc, &pfd);
//debug:
DescribePixelFormat(g_hDc, formatIndex, sizeof(pfd), &pfd);
SetPixelFormat(g_hDc, formatIndex, &pfd);
//create rendering context
g_hContext = wglCreateContext(g_hDc);
if(g_hContext == NULL)
return false;
//activate context
if(!wglMakeCurrent(g_hDc, g_hContext))
return false;;
//load extensions
glewInit();
//load font
g_fontBase = glGenLists(256);
//HFONT old = (HFONT)SelectObject(g_hDc, GetStockObject(DEFAULT_GUI_FONT));
HFONT old = (HFONT)SelectObject(g_hDc, GetStockObject(OEM_FIXED_FONT));
wglUseFontBitmaps(g_hDc, 0, 256, g_fontBase);
for(int i = 0; i < 256; ++i)
GetGlyphOutline(g_hDc, i, GGO_METRICS, &g_glyphMetrics[i], 0, NULL, NULL);
GetTextMetrics(g_hDc, &g_fontMetrics);
SelectObject(g_hDc, old);
return true;
}
void exit3d()
{
glDeleteLists(g_fontBase, 256);
wglMakeCurrent(NULL, NULL);
if(g_hContext != NULL)
{
wglDeleteContext(g_hContext);
g_hContext = NULL;
}
if(g_hDc != NULL)
{
ReleaseDC(g_hWnd, g_hDc);
g_hDc = NULL;
}
}
void centerWindowInWindow(HWND hDlg, HWND hParent)
{
RECT dlg, parent;
GetWindowRect(hDlg, &dlg);
dlg.right -= dlg.left;
dlg.bottom -= dlg.top;
GetWindowRect(hParent, &parent);
parent.right -= parent.left;
parent.bottom -= parent.top;
MoveWindow(hDlg,
parent.left + (parent.right - dlg.right)/2,
parent.top + (parent.bottom - dlg.bottom)/2,
dlg.right, dlg.bottom, FALSE);
}
BOOL CALLBACK dialogProcAbout(HWND hDlg, UINT msg, WPARAM wP, LPARAM lP)
{
switch(msg)
{
case WM_INITDIALOG:
{
centerWindowInWindow(hDlg, g_hWnd);
SetWindowText(GetDlgItem(hDlg, IDC_ABOUT_DATE),
(string("Build:\n") + string(__DATE__)).c_str());
return TRUE;
}
case WM_COMMAND:
switch(LOWORD(wP))
{
case IDOK:
case IDCANCEL:
EndDialog(hDlg, 0);
return TRUE;
}
break;
}
return FALSE;
}
LRESULT CALLBACK eventListener(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static int lastMouseX = 0, lastMouseY = 0;
static bool isLDown = false;
switch(message)
{
case WM_SIZE:
{
g_width = (int)LOWORD(lParam);
g_height = (int)HIWORD(lParam);
updateProjectionMatrix(g_width, g_height);
return 0;
}break;
case WM_PAINT:
{
if(!g_isInited)
{
RECT r;
PAINTSTRUCT ps;
GetClientRect(hWnd, &r);
HDC hDc = BeginPaint(hWnd, &ps);
DrawText(hDc, g_startupText.data(), g_startupText.length(),
&r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
EndPaint(hWnd, &ps);
//this allows init() to redraw the window without
//calling itself
if(!g_initCalled)
{
g_initCalled = true;
if(!init())
{
MessageBox(hWnd, "Something went wrong!",
"ARGH!!!", MB_OK);
g_done = true;
}
else
{
g_isInited = true;
draw();
}
}
}
else
{
ValidateRect(hWnd, NULL);
draw();
}
return 0;
}break;
case WM_LBUTTONDOWN:
{
isLDown = true;
POINTS p = MAKEPOINTS(lParam);
lastMouseX = p.x;
lastMouseY = p.y;
return 0;
}break;
case WM_MOUSEMOVE:
{
if(!isLDown)
return 0;
RECT r;
GetClientRect(hWnd, &r);
POINTS pos = MAKEPOINTS(lParam);
int dx = pos.x - lastMouseX;
int dy = pos.y - lastMouseY;
float radX = 2*PI*float(dx)/float(r.right);
float radY = 2*PI*float(dy)/float(r.bottom);
turnRight(radX);
turnDown(radY);
lastMouseX = pos.x;
lastMouseY = pos.y;
return 0;
}break;
case WM_LBUTTONUP:
{
isLDown = false;
return 0;
}break;
case WM_CHAR:
{
switch(LOWORD(wParam))
{
case '\r':
{
if(g_isInputOn)
{
g_isInputOn = false;
g_isInputComplete = true;
}
else
{
g_isInputOn = true;
g_isInputComplete = false;
g_input = "";
}
}break;
case '\b':
{
if(g_isInputOn)
g_input = g_input.substr(0, g_input.length() - 1);
}break;
case 0x1B: //escape
{
if(g_isInputOn)
{
g_isInputOn = false;
g_isInputComplete = true;
g_input = "";
}
}break;
default:
{
if(g_isInputOn)
g_input += (char)LOWORD(wParam);
}break;
}
return 0;
}break;
case WM_DROPFILES:
{
HDROP hDrop = (HDROP)wParam;
char buff[1024];
DragQueryFile(hDrop, 0, buff, 1024);
loadFile(buff);
DragFinish(hDrop);
return 0;
}break;
case WM_COMMAND:
{
UpdateWindow(hWnd);
switch(LOWORD(wParam))
{
case MENU_FILE_OPEN_MODEL:
menuFileOpenModel();
break;
case MENU_FILE_MERGE_MODEL:
menuFileMergeModel();
break;
case MENU_FILE_OPEN_ANIMATION:
menuFileOpenAnimation();
break;
case MENU_FILE_EXPORT_MODEL:
menuFileExportModel();
break;
case MENU_FILE_EXPORT_TEXTURES:
menuFileExportTextures();
break;
case MENU_FILE_EXPORT_SHADERS:
menuFileExportShaders();
break;
case MENU_FILE_REIMPORT_SHADERS:
menuFileReimportShaders();
break;
case MENU_FILE_REGENERATE_SHADERS:
menuFileRegenerateShaders();
break;
case MENU_FILE_EXIT:
PostQuitMessage(0);
break;
case MENU_HELP_ABOUT:
DialogBox(g_hInst, MAKEINTRESOURCE(IDD_ABOUT),
g_hWnd, dialogProcAbout);
break;
case MENU_DEBUG_SECTIONINFO:
menuDebugSectioninfo();
break;
}
return 0;
}break;
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
}break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
}
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE old, LPSTR params, int showCom)
{
parseParameters(params);
WNDCLASSEX wc = { sizeof(wc), CS_HREDRAW | CS_VREDRAW, eventListener, 0, 0, hInst,
LoadIcon(hInst, MAKEINTRESOURCE(IDI_ICON)), LoadCursor(NULL, IDC_ARROW),
(HBRUSH)GetStockObject(WHITE_BRUSH),
MAKEINTRESOURCE(IDM_MENU), "glclass",
LoadIcon(hInst, MAKEINTRESOURCE(IDI_ICON))
};
if(!RegisterClassEx(&wc))
return -1;
//RECT client = { 0, 0, 720, 576 }; //PAL resolution
RECT client = { 0, 0, 1200, 650 };
AdjustWindowRect(&client, WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, TRUE);
client.right -= client.left;
client.bottom -= client.top;
HWND hWnd = CreateWindowEx(0, "glclass", "bmdview2", WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
0, 0, client.right, client.bottom,
NULL, NULL, hInst, NULL);
if(hWnd == NULL)
return -1;
g_hInst = hInst;
g_hWnd = hWnd;
g_done = !init3D();
ShowWindow(hWnd, showCom); //SW_MAXIMIZE);
if(g_done)
MessageBox(hWnd, "OpenGL couldn't be set up", "ARGH!!!", MB_OK);
setStartupText("Loading...");
DragAcceptFiles(hWnd, TRUE);
MSG msg;
DWORD startTime = GetTickCount();
while(!g_done)
{
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
if(msg.message == WM_QUIT)
g_done = true;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
startTime = GetTickCount();
if(g_isInputComplete)
handleCamera();
else
drawText(g_input.c_str());
draw();
setLastFrameSeconds((GetTickCount() - startTime)/1000.0f);
}
}
exit();
exit3d();
return msg.wParam;
}