-
Notifications
You must be signed in to change notification settings - Fork 0
/
OBJReader.cpp
403 lines (306 loc) · 8.72 KB
/
OBJReader.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
#include "stdafx.h"
#include "OBJReader.h"
#include "OBJLoader.h"
#include "GL/GL.h"
#include "GL/GLU.h"
#include <MMSystem.h>
//*********************************************************************************
#define WDCLASS_NAME "OBJReaderWdClass"
#define REFERENCE_TIME 0.016f
//*********************************************************************************
//***********************************MENU UTILS************************************
//*********************************************************************************
// Add new popup menu
#define ADDPOPUPMENU(hmenu, string) \
HMENU hSubMenu = CreatePopupMenu(); \
AppendMenu(hmenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, string);
// Add a menu item
#define ADDMENUITEM(hmenu, ID, string) \
AppendMenu(hSubMenu, MF_STRING, ID, string);
enum
{
ID_FILE_EXIT,
ID_FILE_REGENERATE,
ID_FILE_TOGGLE_TEXTURE
};
/**********************************************************************************/
using namespace std;
//*********************************************************************************
Engine MyEngine;
Camera MyCamera;
HINSTANCE g_hAppInstance;
HWND g_hAppWnd;
HDC g_hDC;
HGLRC g_hGLContext;
BOOL g_bRun = TRUE;
C3DEngine *C3DEngine::g_pRendererInstance = NULL;
Camera *Camera::g_CameraInstance = NULL;
Castle *castle;
BOOL texON = TRUE;
BOOL readyForRedraw = TRUE;
//*********************************************************************************
ATOM RegisterWindowClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE hInstance, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
void CreateMainMenu(HWND hWnd);
void SetupOpenGL(void);
void ShutdownOpenGL(void);
//*********************************************************************************
// Application entry point
//
int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
MSG Msg;
DWORD u32Time, u32PrevTime;
RECT R;
float fDT;
MyEngine = Engine();
//MyCamera = Camera(Vector3D(-250.0, 800.0, -300.0));
if (!C3DEngine::GetInstance())
{
MessageBox(NULL, "No 3D renderer defined", "3D error", MB_OK | MB_ICONSTOP);
return(0);
}
/*
if (!Camera::GetInstance())
{
MessageBox(NULL, "No camera defined", "camera error", MB_OK | MB_ICONSTOP);
return(0);
}*/
//--- Register custom window class
RegisterWindowClass(hInstance);
//--- Initialize application
if (!InitInstance(hInstance, nCmdShow))
return(FALSE);
//--- Load scene files
C3DEngine::GetInstance()->Setup(g_hAppWnd);
//--- Main message loop
fDT = 0;
u32PrevTime = timeGetTime();
while (g_bRun)
{
//--- Messages handling
while (PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
//--- Scene refresh
GetClientRect(g_hAppWnd, &R);
C3DEngine::GetInstance()->Render(R.right, R.bottom, MyCamera, castle, texON, readyForRedraw);
//--- Scene update
u32Time = timeGetTime();
fDT += (u32Time - u32PrevTime) / 1000.f;
while (fDT >= REFERENCE_TIME)
{
C3DEngine::GetInstance()->Update(REFERENCE_TIME, MyCamera);
fDT -= REFERENCE_TIME;
}
u32PrevTime = u32Time;
//--- Buffers swapping
SwapBuffers(g_hDC);
}
//--- Shutdown renderer
C3DEngine::GetInstance()->Shutdown();
//--- Terminate application
//FreeConsole();
return((int)Msg.wParam);
}
//*********************************************************************************
// Custom window registration
//
ATOM RegisterWindowClass(HINSTANCE hInstance)
{
WNDCLASSEX Class;
Class.cbSize = sizeof(WNDCLASSEX);
Class.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
Class.lpfnWndProc = WndProc;
Class.cbClsExtra = 0;
Class.cbWndExtra = 0;
Class.hInstance = hInstance;
Class.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_OBJREADER));
Class.hCursor = LoadCursor(NULL, IDC_ARROW);
Class.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
Class.lpszMenuName = NULL;
Class.lpszClassName = WDCLASS_NAME;
Class.hIconSm = LoadIcon(Class.hInstance, MAKEINTRESOURCE(IDI_SMALL));
return(RegisterClassEx(&Class));
}
//*********************************************************************************
// Application initialization
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
g_hAppInstance = hInstance;
//--- Create window
g_hAppWnd = CreateWindow(WDCLASS_NAME, "3D scene renderer", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, g_hAppInstance, NULL);
if (!g_hAppWnd)
return(FALSE);
CreateMainMenu(g_hAppWnd);
SetupOpenGL();
SETTINGS settings;
settings.matrix_width = 20;
settings.matrix_height = 20;
settings.rect_number = 4;
castle = new Castle(settings);
//Génération du chateau
castle->generateWall();
//--- Display window
ShowWindow(g_hAppWnd, nCmdShow);
UpdateWindow(g_hAppWnd);
return(TRUE);
}
//*********************************************************************************
// Setup OpenGL renderer
//
void SetupOpenGL(void)
{
PIXELFORMATDESCRIPTOR PFD;
int s32PixelIndex;
//--- Retrieve window DC
g_hDC = ::GetDC(g_hAppWnd);
if (!g_hDC)
return;
//--- Init desired pixel format
memset(&PFD, 0, sizeof(PFD));
PFD.nSize = sizeof(PFD);
PFD.nVersion = 1;
PFD.dwFlags = PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW | PFD_GENERIC_ACCELERATED | PFD_DOUBLEBUFFER;
PFD.iPixelType = PFD_TYPE_RGBA;
PFD.cColorBits = 24;
PFD.cAlphaBits = 8;
PFD.cDepthBits = 24;
PFD.iLayerType = PFD_MAIN_PLANE;
//--- Try to match the desired pixel format
s32PixelIndex = ChoosePixelFormat(g_hDC, &PFD);
if (!s32PixelIndex)
s32PixelIndex = 1;
//--- Get pixel format description
memset(&PFD, 0, sizeof(PFD));
if (!DescribePixelFormat(g_hDC, s32PixelIndex, sizeof(PFD), &PFD))
return;
//--- Set pixel format
if (s32PixelIndex != GetPixelFormat(g_hDC))
if (!SetPixelFormat(g_hDC, s32PixelIndex, &PFD))
return;
//--- Create GL render context
g_hGLContext = (HGLRC)wglCreateContext(g_hDC);
//--- Activate render context
wglMakeCurrent(g_hDC, g_hGLContext);
}
//*********************************************************************************
// Shutdown OpenGL renderer
//
void ShutdownOpenGL(void)
{
//--- Release GL context
if (g_hGLContext)
{
wglMakeCurrent(NULL, NULL);
wglDeleteContext(g_hGLContext);
}
//--- Free up DC
if (g_hDC)
ReleaseDC(g_hAppWnd, g_hDC);
}
//*********************************************************************************
// Application message handler
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT PS;
HDC hDC;
POINT Pt;
switch (message)
{
case WM_PAINT:
//--- Window refresh notification
hDC = BeginPaint(hWnd, &PS);
EndPaint(hWnd, &PS);
break;
case WM_CLOSE:
//--- Close window notification
ShutdownOpenGL();
DestroyWindow(g_hAppWnd);
break;
case WM_DESTROY:
//--- Window destruction notification
PostQuitMessage(0);
g_bRun = FALSE;
break;
case WM_MOUSEWHEEL:
//--- Mouse wheel notification
//C3DEngine::GetInstance()->MouseWheel(((float)(short)HIWORD(wParam)) / WHEEL_DELTA);
break;
case WM_MOUSEMOVE:
//--- Mouse move notification
Pt.x = (short)LOWORD(lParam);
Pt.y = (short)HIWORD(lParam);
C3DEngine::GetInstance()->MouseMove(Pt);
//MyCamera.OnMouseMotion(Pt);
break;
case WM_LBUTTONDOWN:
//--- Left mouse button down notification
Pt.x = (short)LOWORD(lParam);
Pt.y = (short)HIWORD(lParam);
//C3DEngine::GetInstance()->LButtonDown(Pt);
break;
case WM_LBUTTONUP:
//--- Left mouse button up notification
Pt.x = (short)LOWORD(lParam);
Pt.y = (short)HIWORD(lParam);
//C3DEngine::GetInstance()->LButtonUp(Pt);
break;
case WM_RBUTTONDOWN:
//--- Right mouse button down notification
Pt.x = (short)LOWORD(lParam);
Pt.y = (short)HIWORD(lParam);
//C3DEngine::GetInstance()->RButtonDown(Pt);
break;
case WM_RBUTTONUP:
//--- Right mouse button up notification
Pt.x = (short)LOWORD(lParam);
Pt.y = (short)HIWORD(lParam);
//C3DEngine::GetInstance()->RButtonUp(Pt);
break;
case WM_KEYDOWN:
C3DEngine::GetInstance()->KeyDown((int)wParam);
//MyCamera.OnKeyDown((int)wParam);
break;
case WM_KEYUP:
//MyCamera.OnKeyUp((int)wParam);
break;
//MENU
case WM_COMMAND:
switch (LOWORD(wParam))
{
case ID_FILE_EXIT:
ShutdownOpenGL();
DestroyWindow(g_hAppWnd);
break;
case ID_FILE_REGENERATE:
readyForRedraw = FALSE;
castle->regenerateWall();
readyForRedraw = TRUE;
break;
case ID_FILE_TOGGLE_TEXTURE:
texON = !texON;
break;
default:
break;
}
default:
//--- Default message handling
return(DefWindowProc(hWnd, message, wParam, lParam));
}
return(0);
}
void CreateMainMenu(HWND hWnd)
{
HMENU hMenu = CreateMenu();
ADDPOPUPMENU(hMenu, "&File");
ADDMENUITEM(hMenu, ID_FILE_EXIT, "&Exit");
ADDMENUITEM(hMenu, ID_FILE_REGENERATE, "&Regenerate");
ADDMENUITEM(hMenu, ID_FILE_TOGGLE_TEXTURE, "&Toggle Texture");
SetMenu(hWnd, hMenu);
}