-
Notifications
You must be signed in to change notification settings - Fork 1
/
graphics.cpp
480 lines (386 loc) · 11.1 KB
/
graphics.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
/******************************************************
g-Matrix3D Neo Engine
Copyright (c)2003 Kim Seong Wan (kaswan, Â𻧱ͽÅ)
E-mail: kaswan@hitel.net
http://www.g-matrix.pe.kr
*******************************************************/
#include "stdafx.h"
#include <assert.h>
#include "fileopen.h"
#include "dibsection.h"
#include "bmp.h"
#include "transform.h"
#include "vertex.h"
#include "mesh.h"
#include "renbuffer.h"
#include "raster.h"
#include "util.h"
#include "texmanager.h"
#include "matmanager.h"
#include "asemodel.h"
#include "model.h"
#include "raytracer.h"
#include "process.h"
#include <thread>
#include <vector>
using std::thread;
using std::vector;
#ifdef _MSC_VER
#define _CW_DEFAULT ( _RC_NEAR + _PC_53 + _EM_INVALID + _EM_ZERODIVIDE + _EM_OVERFLOW + _EM_UNDERFLOW + _EM_INEXACT + _EM_DENORMAL)
#endif
//typedef Vector3 Color3;
//typedef Vector4 Color4;
static float thetaX0, thetaY0;
static float thetaX, thetaY;
/////////////////////////////
CTransform TF;
CRenBuffer RBuffer;
CDIBSection FrameBuffer;
CTexManager TEXMAN;
CMatManager MATMAN;
RenderInfo RInfo;
RayTracer RTRT;
vector<thread> workers;
Model *pModel;
/*
#define REDI 240.0f
Mesh Rect;
Vertex VertexList[16] = {
Vertex(Vector3(-REDI, REDI, -REDI), Vector4(1.0f, 0.5f, 0.5f), Vector3(0,0,-1), Vector2(0,0)),
Vertex(Vector3( REDI, REDI, -REDI), Vector4(1.0f, 1.0f, 0.5f), Vector3(0,0,-1), Vector2(1,0)),
Vertex(Vector3( REDI, -REDI, -REDI), Vector4(1.0f, 1.0f, 0.5f), Vector3(0,0,-1), Vector2(1,1)),
Vertex(Vector3(-REDI, -REDI, -REDI), Vector4(0.5f, 0.5f, 1.0f), Vector3(0,0,-1), Vector2(0,1)),
Vertex(Vector3(-REDI, REDI, REDI), Vector4(1.0f, 1.0f, 1.0f), Vector3(0,0,1), Vector2(1,0)),
Vertex(Vector3( REDI, REDI, REDI), Vector4(1.0f, 1.0f, 1.0f), Vector3(0,0,1), Vector2(0,0)),
Vertex(Vector3( REDI, -REDI, REDI), Vector4(1.0f, 1.0f, 1.0f), Vector3(0,0,1), Vector2(0,1)),
Vertex(Vector3(-REDI, -REDI, REDI), Vector4(1.0f, 1.0f, 1.0f), Vector3(0,0,1), Vector2(1,1)),
Vertex(Vector3(-REDI, REDI, -REDI), Vector4(1.0f, 0.5f, 0.5f), Vector3(0,1,0), Vector2(0,0)),
Vertex(Vector3( REDI, REDI, -REDI), Vector4(1.0f, 1.0f, 0.5f), Vector3(0,1,0), Vector2(1,0)),
Vertex(Vector3(-REDI, REDI, REDI), Vector4(1.0f, 0.5f, 0.5f), Vector3(0,1,0), Vector2(0,1)),
Vertex(Vector3( REDI, REDI, REDI), Vector4(1.0f, 1.0f, 0.5f), Vector3(0,1,0), Vector2(1,1)),
Vertex(Vector3( REDI, -REDI, -REDI), Vector4(1.0f, 1.0f, 0.5f), Vector3(0,-1,0), Vector2(1,1)),
Vertex(Vector3(-REDI, -REDI, -REDI), Vector4(0.5f, 0.5f, 1.0f), Vector3(0,-1,0), Vector2(0,1)),
Vertex(Vector3( REDI, -REDI, REDI), Vector4(1.0f, 1.0f, 0.5f), Vector3(0,-1,0), Vector2(1,0)),
Vertex(Vector3(-REDI, -REDI, REDI), Vector4(0.5f, 0.5f, 1.0f), Vector3(0,-1,0), Vector2(0,0))};
int IndexList[36] = {0,1,2, 0,2,3, // front
4,6,5, 4,7,6, // back
8,10,11, 8,11,9, // up
12,15,13, 12,14,15, // down
0,7,4, 0,3,7, // left
1,5,6, 1,6,2}; // right
*/
HDC hScreenDC, hMemoryDC;
UINT BytesPerScanline;
BYTE *pBits;
BYTE *pDepthBuffer;
bool bLoadedBMP;
bool g_bIsActive;
bool g_bIsASE;
UINT frameCounter;
LARGE_INTEGER Frequency;
LARGE_INTEGER StartCounter, EndCounter;
static float fps;
void InitGraphics(HWND hWnd, int width, int height);
void DeinitGraphics(HWND hWnd);
void UpdateFrame(void);
void DisplayFPS(void);
void PutNumber(int x, int y, int number);
void PutNumber(int x, int y, UINT number);
void PutNumber(int x, int y, float number);
void PutNumberHex(int x, int y, int number);
void PutNumber(int x, int y, char* text, int number);
void PutNumber(int x, int y, char* text, UINT number);
void PutNumber(int x, int y, char* text, float number);
void PutNumberHex(int x, int y, char* text, int number);
void ResetFrameCounter(void);
void SetNumThreads(int num){
RTRT.SetNumThreads(num);
}
int GetNumThreads(void) {
return RTRT.GetNumThreads();
}
void SetColor(BYTE r, BYTE g, BYTE b);
void OnOpen(HWND hWnd);
void OnPicking(int mx, int my);
void OnMouseMove(int mx, int my);
void InitGraphics(HWND hWnd, int width, int height)
{
// Set Frame Buffer
hScreenDC = GetDC(hWnd);
hMemoryDC = FrameBuffer.Create(hScreenDC, width, height);
BytesPerScanline = FrameBuffer.BytesPerScanline;
pBits = FrameBuffer.pBits;
// Set Depth Buffer
pDepthBuffer = new BYTE[640 * 480 * 2];
MATMAN.Init(20);
// Texture
TEXMAN.Init(20);
TEXMAN.Create(256, 256);
//TEXMAN.Load("Wizard.bmp");
TEXMAN.SetTexture(0);
//////////////////////////////////////////////
//Rect.LoadMesh(VertexList, IndexList, 16, 36);
RTRT.Setup(4);
QueryPerformanceFrequency(&Frequency);
g_bIsActive = TRUE;
}
void DeinitGraphics(HWND hWnd)
{
g_bIsActive = FALSE;
delete[] pDepthBuffer;
TEXMAN.DeInit();
FrameBuffer.Destroy();
ReleaseDC(hWnd, hScreenDC);
}
bool IsActive(void)
{
return g_bIsActive;
}
void ClearDepthBuffer(void)
{
#ifdef _MSC_VER
_asm {
cld
mov edi, pDepthBuffer
xor eax, eax
mov ecx, 640 * 480 * 2 / 4
rep stosd
}
#else
asm ("cld");
asm ("movl %0, %%edi" : : "m" (pDepthBuffer) );
asm ("xorl %eax, %eax");
asm ("movl $(640 * 480 * 2 / 4), %ecx");
asm ("rep stosl");
#endif
}
void ClearDepthBufferMMX(void)
{
/* _asm {
mov edi, pDepthBuffer
pxor mm0, mm0
mov ecx, 640 * 480 * 2 / 8
L0: movq [edi], mm0
add edi, 8
dec ecx
jnz L0
emms
}
*/
}
void RenderCall(int index)
{
RTRT.Render(index);
}
void UpdateFrame(void)
{
static float frametime;
static Vector3 camera;
static float FOV = 90;
static int max_threads;
frameCounter++;
max_threads = RTRT.GetNumThreads();
// Clear fame buffer and depth buffer
FrameBuffer.Clear(10, 10, 10);
//memset(pDepthBuffer, 0, 640 * 480 * 2);
ClearDepthBuffer();
//SetColor(255, 50, 50);
//DrawBox(20, 20, 639, 479);
//FillBox(20, 20, 639, 479);
// Keyboard Inputs
if (GetAsyncKeyState(VK_LEFT)) camera.x -= 10;
if (GetAsyncKeyState(VK_RIGHT)) camera.x += 10;
if (GetAsyncKeyState(VK_UP)) camera.z += 10;
if (GetAsyncKeyState(VK_DOWN)) camera.z -= 10;
if (GetAsyncKeyState(VK_PRIOR)) camera.y += 10;
if (GetAsyncKeyState(VK_NEXT)) camera.y -= 10;
if (GetAsyncKeyState(VK_ADD)) FOV -= 5;
if (GetAsyncKeyState(VK_SUBTRACT)) FOV += 5;
//PutNumber(0, 0, "camera.x", camera.x);
//PutNumber(0, 20, "camera.y", camera.y);
//PutNumber(0, 40, "camera.z", camera.z);
//PutNumber(0, 60, "FOV", FOV);
if (GetAsyncKeyState(VK_F1)) TEXMAN.SetTexture(0);
if (GetAsyncKeyState(VK_F2)) TEXMAN.SetTexture(1);
if (GetAsyncKeyState(VK_F3)) TEXMAN.SetTexture(2);
if (GetAsyncKeyState(VK_F4)) TEXMAN.SetTexture(3);
/////////////////////////////////////////////////////////////
TF.SetMatrixMode(MM_VIEW);
TF.LoadIndentity();
//TF.Translate(0, 0, -100);
TF.Translate(-camera.x, -camera.y, -camera.z);
TF.SetMatrixMode(MM_PROJECT);
TF.LoadIndentity();
//TF.SimplePerspective();
//TF.Perspective(640, 480, 640, 2000);
TF.PerspectiveFOVW(FOV, 0.75, 128, 10000);
//TF.PerspectiveFOVH(75, 1.3333f, 320, 2000);
TF.SetMatrixMode(MM_VIEWPORT);
TF.LoadIndentity();
TF.Scale(320, -240, 1);
TF.Translate(320, 240, 0);
////////////////////////////////////////////////////////////
TF.SetMatrixMode(MM_WORLD);
TF.LoadIndentity();
//TF.Scale(5, 5, 5);
TF.RotateY(thetaY);
TF.RotateX(thetaX + 0.5f);
TF.Translate(100, 1, 300);
if (g_bIsASE)
{
//pModel->Render();
}
TF.SetMatrixMode(MM_WORLD);
TF.LoadIndentity();
TF.RotateY(thetaY);
TF.RotateX(thetaX);
TF.Translate(0, 0, 300);
if (g_bIsASE)
{
//pModel->Render();
}
RTRT.Move();
for (int i = 0; i < max_threads; i++){
workers.push_back(thread(RenderCall, i));
}
for (int i = 0; i < max_threads; i++) {
workers[i].join();
}
workers.clear();
/*
thread t0(RenderCall, 0);
thread t1(RenderCall, 1);
thread t2(RenderCall, 2);
thread t3(RenderCall, 3);
thread t4(RenderCall, 4);
thread t5(RenderCall, 5);
thread t6(RenderCall, 6);
thread t7(RenderCall, 7);
t0.join();
t1.join();
t2.join();
t3.join();
t4.join();
t5.join();
t6.join();
t7.join();
*/
DisplayFPS();
//RInfo.Display(0, 340);
//RInfo.Clear();
FrameBuffer.Swap();
// Calculate FPS
QueryPerformanceCounter(&EndCounter);
frametime = (float)(EndCounter.QuadPart - StartCounter.QuadPart) / (float)Frequency.QuadPart;
StartCounter = EndCounter;
fps = 1 / frametime;
}
//------------------------------------------------------------------------
void ResetFrameCounter(void)
{
frameCounter = 0;
}
void DisplayFPS(void)
{
char string[20];
sprintf(string, "FPS: %f", fps);
TextOut(hMemoryDC, 0, 0, string, strlen(string));
//TextOut(hScreenDC, 250, 0, string, strlen(string));
}
void PutNumber(int x, int y, int number)
{
char string[20];
sprintf(string, "%d", number);
TextOut(hMemoryDC, x, y, string, strlen(string));
//TextOut(hScreenDC, x, y, string, strlen(string));
}
void PutNumber(int x, int y, UINT number)
{
char string[20];
sprintf(string, "%u", number);
TextOut(hMemoryDC, x, y, string, strlen(string));
}
void PutNumber(int x, int y, float number)
{
char string[20];
sprintf(string, "%f", number);
TextOut(hMemoryDC, x, y, string, strlen(string));
}
void PutNumber(int x, int y, char* text, int number)
{
char string[128];
sprintf(string, "%s: %i", text, number);
TextOut(hMemoryDC, x, y, string, strlen(string));
//TextOut(hScreenDC, x, y, string, strlen(string));
}
void PutNumber(int x, int y, char* text, UINT number)
{
char string[128];
sprintf(string, "%s: %u", text, number);
TextOut(hMemoryDC, x, y, string, strlen(string));
}
void PutNumber(int x, int y, char* text, float number)
{
char string[128];
sprintf(string, "%s: %f", text, number);
TextOut(hMemoryDC, x, y, string, strlen(string));
}
void PutNumberHex(int x, int y, int number)
{
char string[20];
sprintf(string, "0x%X ", number);
TextOut(hMemoryDC, x, y, string, strlen(string));
}
void PutNumberHex(int x, int y, char* text, int number)
{
char string[128];
sprintf(string, "%s: 0x%X ", text, number);
TextOut(hMemoryDC, x, y, string, strlen(string));
}
//================================================================//
int Picked;
void OnPicking(int mx, int my)
{
/* for (int i = 0; i < 4; i++)
{
int dx = abs(ftoi(VertexList[i].pos.x) - mx);
int dy = abs(ftoi(VertexList[i].pos.y) - my);
if (dx <= 5 && dy <= 5)
{
Picked = i;
return;
}
}
Picked = -1;
*/
}
static int mx0, my0;
void OnMouseLButtonDown(int mx, int my)
{
mx0 = mx;
my0 = my;
thetaX0 = thetaX;
thetaY0 = thetaY;
}
void OnMouseMove(int mx, int my)
{
thetaX = thetaX0 - DEG2RAD(my-my0);
thetaY = thetaY0 - DEG2RAD(mx-mx0);
}
void OnOpen(HWND hWnd)
{
char *fname = NULL;
fname = GetOpenFileNameDlg(hWnd);
if (NULL == fname)
{
return;
}
if (pModel) delete pModel;
ASEMODEL ase;
ase.Init();
pModel = ase.LoadASE(fname);
assert(pModel);
//BYTE *pBMP = LoadBMP(fname, );
//if (pBMP == NULL) return;
//DrawBMP(0, 0, pBMP);
}