-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleEngine.cpp
More file actions
executable file
·373 lines (338 loc) · 10 KB
/
Copy pathSimpleEngine.cpp
File metadata and controls
executable file
·373 lines (338 loc) · 10 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
369
370
371
372
373
// SimpleEngine.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "SimpleEngine.h"
#define MAX_LOADSTRING 100
#define VK_A 0x41
#define VK_D 0x44
#define VK_S 0x53
#define VK_W 0x57
#define VK_R 0x52
#define VK_1 0x31
#define VK_2 0x32
#define VK_3 0x33
#define VK_4 0x34
#define VK_5 0x35
#define VK_6 0x36
// Global Variables:
HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name
//
Scene *g_pScene=new Scene;
Camera *g_pCamera=new Camera;
Light *g_pLights=new Light[2];
Object3D *g_pObject3D = NULL;
Texture *g_pTexture=NULL;
int g_x=0;
int g_y=0;
double g_Angle=0.0;
// Forward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
int APIENTRY _tWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPTSTR lpCmdLine,
_In_ int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// TODO: Place code here.
MSG msg;
HWND hWnd;
HACCEL hAccelTable;
// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_SIMPLEENGINE, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
hInst = hInstance; // Store instance handle in our global variable
//hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
// CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW| WS_CLIPCHILDREN | WS_CLIPSIBLINGS,
100, 100, 800, 800, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
//Initialize
g_pCamera->setWCToVCTransform(Vec3(0,0,20),Vec3(0,0,0),Vec3(0,1,0));
g_pCamera->setVCToNCTransform(90.0,1.0,50.0,1000.0);
g_pCamera->setNCToVPTransform(0,0,600,600);
g_pScene->SetWnd(hWnd);
g_pScene->SetWinWidthAndHeight(0,0,600,600);
g_pScene->SetShadeMode(SHADE_MODE_NONE);
g_pObject3D=g_pScene->LoadObject("cube.3d");
g_pTexture=g_pScene->LoadTexture("checker.bmp");
g_pObject3D->InitTexture(g_pTexture);
//Initialize lights info
g_pLights[0].setLightPosition(Vec4(-10,10.0,10.0,1.0));
g_pLights[0].bActive = true;
g_pLights[1].setLightPosition(Vec4(-10,10.0,10.0,1.0));
g_pLights[1].bActive = true;
//show window
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_SIMPLEENGINE));
// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
//destroy the scene
g_pScene->Destroy_Camera(g_pCamera);
g_pScene->Destroy_Texture(g_pTexture);
g_pScene->Destroy_Object3D(g_pObject3D);
if(g_pScene!=NULL)
{
delete g_pScene;
g_pScene=NULL;
}
return (int) msg.wParam;
}
//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_SIMPLEENGINE));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+3);
wcex.lpszMenuName = NULL/*MAKEINTRESOURCE(IDC_SIMPLEENGINE)*/;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
return RegisterClassEx(&wcex);
}
void SetDCPixelFormat(HDC hDC)
{
int nPixelFormat;
static PIXELFORMATDESCRIPTOR pfd = {
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_DRAW_TO_WINDOW |
PFD_SUPPORT_OPENGL |
PFD_DOUBLEBUFFER,
PFD_TYPE_RGBA,
32,
0, 0, 0, 0, 0, 0,
0,
0,
0,
0, 0, 0, 0,
24,
0,
0,
PFD_MAIN_PLANE,
0,
0, 0, 0 };
nPixelFormat = ChoosePixelFormat(hDC, &pfd);
SetPixelFormat(hDC, nPixelFormat, &pfd);
}
int InitGL(GLvoid)
{
glShadeModel(GL_SMOOTH);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
return TRUE;
}
void ReShape(int width, int height)
{
glViewport(0, 0, width, height);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluOrtho2D ((GLdouble) (-width)/2, (GLdouble) width/2,(GLdouble) (-height)/2, (GLdouble) height/2);
}
//
// FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
static HDC hdc;
static HGLRC hrc;
switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_ERASEBKGND:
return true;// not redraw the background
break;
case WM_CREATE:
hdc = GetDC(hWnd);
SetDCPixelFormat(hdc);
hrc = wglCreateContext(hdc);
wglMakeCurrent(hdc, hrc);
InitGL();
break;
case WM_SIZE:
ReShape(LOWORD(lParam),HIWORD(lParam));
break;
case WM_PAINT:
//RECT rectClient;
//hdc = BeginPaint(hWnd, &ps);
//GetClientRect(hWnd,&rectClient);
//SetMapMode (hdc, MM_ISOTROPIC);
//SetWindowExtEx (hdc, rectClient.right, rectClient.bottom, NULL);
//SetViewportExtEx (hdc, rectClient.right, -rectClient.bottom, NULL);
//SetViewportOrgEx (hdc, rectClient.right/2,rectClient.bottom/2, NULL);
//X axis,red
//DDA_DrawLine(hdc,Vec2f(-rectClient.right/3,0),Vec2f(rectClient.right/3,0),RGB(255,0,0),RGB(0,0,255));
//Y axis,blue
//DDA_DrawLine(hdc,Vec2f(0,-rectClient.bottom/3),Vec2f(0,rectClient.bottom/3),RGB(0,0,255),RGB(255,0,0));
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
//glBegin(GL_POINTS);
g_pScene->DrawObject3D(hdc,g_pObject3D,g_pCamera,g_pLights,2,g_Angle);
//glEnd();
//glFlush();
SwapBuffers(hdc);
ValidateRect(hWnd, NULL);
//EndPaint(hWnd, &ps);
break;
case WM_KEYDOWN:
switch (wParam)
{
case VK_RETURN:
case VK_A: //left
g_pCamera->eye_pos[0]-=1;
//g_pCamera->setWCToVCTransform(g_pCamera->eye_pos,Vec3(g_x-=1,g_y,0),Vec3(0,1,0));
g_pCamera->setWCToVCTransform(g_pCamera->eye_pos,Vec3(0,0,0),Vec3(0,1,0));
InvalidateRect(hWnd,NULL,false);
UpdateWindow(hWnd);
break;
case VK_S: //down
g_pCamera->eye_pos[1]-=1;
//g_pCamera->setWCToVCTransform(g_pCamera->eye_pos,Vec3(g_x,g_y-=1,0),Vec3(0,1,0));
g_pCamera->setWCToVCTransform(g_pCamera->eye_pos,Vec3(0,0,0),Vec3(0,1,0));
InvalidateRect(hWnd,NULL,false);
UpdateWindow(hWnd);
break;
case VK_D: //right
g_pCamera->eye_pos[0]+=1;
//g_pCamera->setWCToVCTransform(g_pCamera->eye_pos,Vec3(g_x+=1,g_y,0),Vec3(0,1,0));
g_pCamera->setWCToVCTransform(g_pCamera->eye_pos,Vec3(0,0,0),Vec3(0,1,0));
InvalidateRect(hWnd,NULL,false);
UpdateWindow(hWnd);
break;
case VK_W: //up
g_pCamera->eye_pos[1]+=1;
//g_pCamera->setWCToVCTransform(g_pCamera->eye_pos,Vec3(g_x,g_y+=1,0),Vec3(0,1,0));
g_pCamera->setWCToVCTransform(g_pCamera->eye_pos,Vec3(0,0,0),Vec3(0,1,0));
InvalidateRect(hWnd,NULL,false);
UpdateWindow(hWnd);
break;
case VK_1://shade mode: none(wire frame)
g_pScene->SetShadeMode(SHADE_MODE_NONE);
InvalidateRect(hWnd,NULL,false);
UpdateWindow(hWnd);
break;
case VK_2://shade mode: constant(the original triangle color)
g_pScene->SetShadeMode(SHADE_MODE_CONSTANT);
InvalidateRect(hWnd,NULL,false);
UpdateWindow(hWnd);
break;
case VK_3://shade mode: flat with lights mode
g_pScene->SetShadeMode(SHADE_MODE_FLAT);
InvalidateRect(hWnd,NULL,false);
UpdateWindow(hWnd);
break;
case VK_4://shade mode: gouraud
g_pScene->SetShadeMode(SHADE_MODE_GOURAUD);
InvalidateRect(hWnd,NULL,false);
UpdateWindow(hWnd);
break;
case VK_5://shade mode: texture
g_pScene->SetShadeMode(SHADE_MODE_TEXTURE);
InvalidateRect(hWnd,NULL,false);
UpdateWindow(hWnd);
break;
case VK_6:
g_pScene->SetShadeMode(SHADE_MODE_NORMAL);
InvalidateRect(hWnd,NULL,false);
UpdateWindow(hWnd);
break;
case VK_R:
{
g_Angle+=5;
if (g_Angle>=360.0)
{
g_Angle=0.0;
}
//g_pScene->Rotate3D(g_pObject3D,g_Angle,0.0,1.0,0.0);
InvalidateRect(hWnd,NULL,false);
UpdateWindow(hWnd);
}
break;
case VK_ESCAPE:
SendMessage(hWnd, WM_CLOSE, 0, 0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_DESTROY:
{
wglMakeCurrent(hdc, NULL);
wglDeleteContext(hrc);
DeleteObject(hdc);
PostQuitMessage(0);
}
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// Message handler for about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
return (INT_PTR)TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
}