-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrender.cpp
704 lines (568 loc) · 19.7 KB
/
render.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
692
693
694
695
696
697
698
699
700
701
702
703
704
/* START OF 'render.cpp' FILE */
#include "render.h"
#include "shadermodel.h"
#include "nodevisitor.h"
class RENDER_VISITOR : public NODE_VISITOR {
public:
RENDER_VISITOR (const glm::mat4 worldMatrix, int winWidth, int winHeight, COLORREF * bitPointer,
RENDER::DISPLAY_MODE displayMode) :
worldMatrix(worldMatrix), winHeight(winHeight), winWidth(winWidth), bitPointer(bitPointer),
displayMode(displayMode)
{
zBuffer = new int[winWidth * winHeight];
for (int i = 0; i < winWidth * winHeight; i++) {
zBuffer[i] = (int)-1e9;
}
transformStack.push(glm::mat4());
}
~RENDER_VISITOR (void)
{
delete [] zBuffer;
}
void Apply (OBJECT_NODE & node)
{
currentTransform = transformStack.top();
transformStack.pop();
RENDER::DrawNode((OBJECT_NODE *)&node, worldMatrix * currentTransform, bitPointer,
winWidth, winHeight, zBuffer, displayMode);
}
void Apply (TRANSFORM_NODE & node)
{
currentTransform = transformStack.top();
transformStack.push(node.GetTransform() * currentTransform);
node.Descend(*this);
}
private:
glm::mat4 worldMatrix;
glm::mat4 currentTransform;
std::stack<glm::mat4> transformStack;
COLORREF * bitPointer;
RENDER::DISPLAY_MODE displayMode;
int winHeight;
int winWidth;
int * zBuffer;
};
RENDER::RENDER (void) :
displayMode(DISPLAY_MODE::POINTS), backgroundColor(RGB(0, 0, 0))
{
root = new TRANSFORM_NODE;
}
RENDER::RENDER (SCENE_NODE * newRoot) :
displayMode(DISPLAY_MODE::POINTS), backgroundColor(RGB(0, 0, 0))
{
root->AddChild(newRoot);
}
RENDER::RENDER (SCENE_NODE * newRoot, const CAMERA & newCamera) :
camera(newCamera), displayMode(DISPLAY_MODE::POINTS), backgroundColor(RGB(0, 0, 0))
{
root->AddChild(newRoot);
}
RENDER::~RENDER (void)
{
delete root;
}
RENDER::COLOURED_POINT::COLOURED_POINT (void) :
pos(0, 0), color(RGB(0, 0, 0)), depth(0)
{
}
RENDER::COLOURED_POINT::COLOURED_POINT (int x, int y, COLORREF newColor) :
pos(x, y), color(newColor), depth(0)
{
}
RENDER::COLOURED_POINT::COLOURED_POINT (int x, int y, double depth, COLORREF newColor) :
pos(x, y), color(newColor), depth(depth)
{
}
RENDER::COLOURED_POINT::COLOURED_POINT (const glm::ivec2 & newPos, COLORREF newColor) :
pos(newPos), color(newColor), depth(0)
{
}
RENDER::COLOURED_POINT::COLOURED_POINT (const glm::ivec2 & newPos, double depth, COLORREF newColor) :
pos(newPos), color(newColor), depth(depth)
{
}
RENDER::COLOURED_POINT::COLOURED_POINT (const COLOURED_POINT & copyPoint) :
pos(copyPoint.pos), color(copyPoint.color), depth(copyPoint.depth)
{
}
RENDER::COLOURED_POINT::~COLOURED_POINT (void)
{
}
RENDER::COLOURED_POINT & RENDER::COLOURED_POINT::operator= (const COLOURED_POINT & copyPoint)
{
pos = copyPoint.pos;
color = copyPoint.color;
depth = copyPoint.depth;
return *this;
}
void RENDER::CreateDIB (HDC hDC, HBITMAP * hBitmap, COLORREF ** bitPointer, BITMAPINFO * bitmap)
{
(*hBitmap) = (HBITMAP)GetCurrentObject(hDC, OBJ_BITMAP);
GetObject((*hBitmap), sizeof(BITMAP), bitmap);
bitmap->bmiHeader.biSize = sizeof(bitmap->bmiHeader);
bitmap->bmiHeader.biPlanes = 1;
bitmap->bmiHeader.biBitCount = 32;
bitmap->bmiHeader.biCompression = BI_RGB;
bitmap->bmiHeader.biSizeImage = bitmap->bmiHeader.biWidth * 4 * bitmap->bmiHeader.biHeight;
bitmap->bmiHeader.biClrUsed = 0;
bitmap->bmiHeader.biClrImportant = 0;
(*hBitmap) = CreateDIBSection(NULL, bitmap, DIB_RGB_COLORS, (void**)bitPointer, NULL, NULL);
}
COLORREF RENDER::InterpolateColors (COLORREF color0, COLORREF color1, double coeff)
{
int r[2];
int g[2];
int b[2];
r[0] = GetRValue(color0);
r[1] = GetRValue(color1);
g[0] = GetGValue(color0);
g[1] = GetGValue(color1);
b[0] = GetBValue(color0);
b[1] = GetBValue(color1);
int newR;
int newG;
int newB;
newR = (int)((1 - coeff) * r[0] + coeff * r[1]);
newG = (int)((1 - coeff) * g[0] + coeff * g[1]); newB = (int)((1 - coeff) * b[0] + coeff * b[1]);
return RGB(newR, newG, newB);
}
void RENDER::PutPixel (COLORREF * bitPointer, COLORREF color, int x, int y, int winWidth, int winHeight)
{
if ((winHeight - y) <= 0 || x >= winWidth || x <= 0 || (winHeight - y) >= winHeight) {
return;
}
color = FlipColor(color);
bitPointer[(winHeight - y) * winWidth + x] = color;
}
void RENDER::PutPixel (COLORREF * bitPointer, COLOURED_POINT point, int winWidth, int winHeight)
{
PutPixel(bitPointer, point.color, point.pos.x, point.pos.y, winWidth, winHeight);
}
bool RENDER::CheckIfPointInFrame (int width, int height, int px, int py)
{
if (py <= 0 || px >= width || px <= 0 || py >= height) {
return false;
}
return true;
}
void RENDER::DrawPoint (COLORREF * bitPointer, const glm::ivec3 & point,
int winWidth, int winHeight, int * zBuffer)
{
if (!CheckIfPointInFrame(winWidth, winHeight, point.x, point.y) || point.z < 0) {
return;
}
int x = point.x;
int y = point.y;
int idx = x + y * winWidth;
if (zBuffer[idx] < point.z) {
zBuffer[x] = point.z;
PutPixelNoCheck(bitPointer, WireFrameColor, x, y, winWidth, winHeight);
}
}
void RENDER::DrawLine (COLORREF * bitPointer, const glm::ivec3 * pts, int width, int height,
int * zBuffer)
{
if (pts[0].z < 0 || pts[1].z < 0 ||
(!CheckIfPointInFrame(width, height, (int)pts[0].x, (int)pts[0].y) &&
!CheckIfPointInFrame(width, height, (int)pts[1].x, (int)pts[1].y))) {
return;
}
int dx = (int)abs(pts[1].x - pts[0].x);
int dy = (int)abs(pts[1].y - pts[0].y);
int sx = (int)(pts[1].x >= pts[0].x) ? 1 : -1;
int sy = (int)(pts[1].y >= pts[0].y) ? 1 : -1;
double segmentLen = sqrt(dx * dx + dy * dy);
if (dy < dx) {
int d = (dy << 1) - dx;
int d1 = dy << 1;
int d2 = (dy - dx) << 1;
PutPixel(bitPointer, WireFrameColor, (int)pts[0].x, (int)pts[0].y, width, height);
int x = (int)pts[0].x + sx;
int y = (int)pts[0].y;
for (int i = 1; i <= dx; i++) {
if (d > 0) {
d += d2;
y += sy;
} else {
d += d1;
}
if (x <= 0) {
if (sx < 0) {
break;
} else {
x += sx;
continue;
}
} else if (x >= width) {
if (sx > 0) {
break;
} else {
x += sx;
continue;
}
}
if (y <= 0) {
if (sy < 0) {
break;
} else {
x += sx;
continue;
}
} else if (y >= height) {
if (sy > 0) {
break;
} else {
x += sx;
continue;
}
}
int dz = pts[0].z + (pts[1].z - pts[0].z) * (i / dx);
int idx = x + y * width;
if (zBuffer[idx] < dz) {
zBuffer[idx] = dz;
PutPixelNoCheck(bitPointer, WireFrameColor, x, y, width, height);
}
x += sx;
}
} else {
int d = (dx << 1) - dy;
int d1 = dx << 1;
int d2 = (dx - dy) << 1;
PutPixel(bitPointer, WireFrameColor, (int)pts[0].x, (int)pts[0].y, width, height);
int x = (int)pts[0].x;
int y = (int)pts[0].y + sy;
for (int i = 1; i <= dy; i++) {
if (d > 0) {
d += d2;
x += sx;
} else {
d += d1;
}
if (x <= 0) {
if (sx < 0) {
break;
} else {
y += sy;
continue;
}
} else if (x >= width) {
if (sx > 0) {
break;
} else {
y += sy;
continue;
}
}
if (y <= 0) {
if (sy < 0) {
break;
}
else {
y += sy;
continue;
}
} else if (y >= height) {
if (sy > 0) {
break;
} else {
y += sy;
continue;
}
}
int dz = pts[0].z + (pts[1].z - pts[0].z) * (i / dy);
int idx = x + y * width;
if (zBuffer[idx] < dz) {
zBuffer[idx] = dz;
PutPixelNoCheck(bitPointer, WireFrameColor, x, y, width, height);
}
y += sy;
}
}
}
void RENDER::PutPixelNoCheck (COLORREF * bitPointer, COLORREF color, int x, int y, int winWidth, int winHeight)
{
bitPointer[(winHeight - y) * winWidth + x] = color;
}
void RENDER::DrawLine (COLORREF * bitPointer, const glm::ivec3 & pt1, const glm::vec3 & pt2,
int width, int height, int * zBuffer)
{
glm::ivec3 pts[2] = { pt1, pt2 };
DrawLine(bitPointer, pts, width, height, zBuffer);
}
void RENDER::DrawTriangle (COLORREF * bitPointer, COLOURED_POINT point1,
COLOURED_POINT point2, COLOURED_POINT point3, int winWidth, int winHeight, double * zBuffer)
{
// Sorting points by y
if (point1.pos.y > point2.pos.y) {
std::swap(point1, point2);
}
if (point1.pos.y > point3.pos.y) {
std::swap(point1, point3);
}
if (point2.pos.y > point3.pos.y) {
std::swap(point2, point3);
}
int totalHeight = point3.pos.y - point1.pos.y;
if (totalHeight == 0 || point1.pos.y + totalHeight < 0 || point1.pos.y > winHeight) {
return;
}
int firstHalfHeight = point2.pos.y - point1.pos.y;
int secondHalfHeight = point3.pos.y - point2.pos.y;
if (firstHalfHeight == 0) {
firstHalfHeight++;
}
if (secondHalfHeight == 0) {
secondHalfHeight++;
}
glm::ivec2 segment13 = point3.pos - point1.pos;
glm::ivec2 segment12 = point2.pos - point1.pos;
glm::ivec2 segment32 = point3.pos - point2.pos;
double length32 = sqrt(segment32.x * segment32.x + segment32.y * segment32.y);
double length13 = sqrt(segment13.x * segment13.x + segment13.y * segment13.y);
double length12 = sqrt(segment12.x * segment12.x + segment12.y * segment12.y);
for (int y = 0; y <= totalHeight; y++) {
if (y + point1.pos.y >= winHeight) {
break;
}
if (point1.pos.y + y <= 0) {
y -= point1.pos.y;
y++;
}
// Counting coordinates
float alpha = (float)y / totalHeight;
int x1 = point1.pos.x + (int)((point3.pos.x - point1.pos.x) * alpha);
float beta;
int x2;
if (y < firstHalfHeight) { // Drawing first half
beta = (float)y / firstHalfHeight;
x2 = point1.pos.x + (int)((point2.pos.x - point1.pos.x) * beta);
}
else { // Drawing second half
beta = (float)(y - firstHalfHeight) / secondHalfHeight;
x2 = point2.pos.x + (int)((point3.pos.x - point2.pos.x) * beta);
}
// Counting interpolated color value
double currLen1 = sqrt(y * y + (x1 - point1.pos.x) * (x1 - point1.pos.x));
double lenCoeff1 = currLen1 / length13;
COLORREF color1 = InterpolateColors(point1.color, point3.color, lenCoeff1);
double currLen2;
double lenCoeff2;
COLORREF color2;
if (y < firstHalfHeight) {
currLen2 = sqrt(y * y + (x2 - point1.pos.x) * (x2 - point1.pos.x));
lenCoeff2 = currLen2 / length12;
color2 = InterpolateColors(point1.color, point2.color, lenCoeff2);
}
else {
currLen2 = sqrt((y - firstHalfHeight) * (y - firstHalfHeight) +
(x2 - point2.pos.x) * (x2 - point2.pos.x));
lenCoeff2 = currLen2 / length32;
color2 = InterpolateColors(point2.color, point3.color, lenCoeff2);
}
// Filling in between lines
double z1 = point1.depth + ((point3.depth - point1.depth) * alpha);
double z2;
if (y < firstHalfHeight) {
z2 = point1.depth + ((point2.depth - point1.depth) * beta);
} else {
z2 = point2.depth + ((point3.depth - point2.depth) * beta);
}
if (x1 > x2) {
std::swap(x1, x2);
std::swap(color1, color2);
std::swap(z1, z2);
}
double segmentLen = sqrt((x2 - x1) * (x2 - x1));
if (segmentLen == 0) {
segmentLen++;
}
for (int j = x1 > 0 ? x1 : 1; j <= x2 && j < winWidth; j++) {
float phi = x2 == x1 ? 1.0f : (float)(j - x1) / (float)(x2 - x1);
double pz = z1 + (z2 - z1) * phi;
int idx = j + (y + point1.pos.y) * winWidth;
if (zBuffer[idx] < pz) {
zBuffer[idx] = pz;
double currLen = sqrt((j - x1) * (j - x1));
double lenCoeff = currLen / segmentLen;
COLORREF resColor = InterpolateColors(color1, color2, lenCoeff);
resColor = RGB(GetBValue(resColor), GetGValue(resColor), GetRValue(resColor));
bitPointer[(winHeight - (point1.pos.y + y)) * winWidth + j] = resColor;
}
}
}
}
glm::vec3 RENDER::Barycentric (const glm::vec2 & a, const glm::vec2 & b,
const glm::vec2 & c, const glm::vec2 & p)
{
glm::vec3 s[2];
for (int i = 2; i--; ) {
s[i][0] = c[i] - a[i];
s[i][1] = b[i] - a[i];
s[i][2] = a[i] - p[i];
}
glm::vec3 u = glm::cross(s[0], s[1]);
if (std::abs(u[2]) > 0) {
return glm::vec3(1.f - (u.x + u.y) / u.z, u.y / u.z, u.x / u.z);
}
return glm::vec3(-1, 1, 1);
}
void RENDER::DrawTriangleBarycentric (COLORREF * bitPointer, glm::vec4 * pts,
glm::vec2 * uvs, const TEXTURE & tex, int winWidth, int winHeight, int * zBuffer)
{
glm::vec2 bboxmin(std::numeric_limits<float>::max(), std::numeric_limits<float>::max());
glm::vec2 bboxmax(-std::numeric_limits<float>::max(), -std::numeric_limits<float>::max());
glm::vec2 clampMax(winWidth - 1, winHeight - 1);
glm::vec2 clampMin(1, 1);
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 2; j++) {
bboxmin[j] = std::max(clampMin[j], std::min(bboxmin[j], pts[i][j] / pts[i][3]));
bboxmax[j] = std::min(clampMax[j], std::max(bboxmax[j], pts[i][j] / pts[i][3]));
}
}
glm::ivec2 p;
COLORREF color;
for (p.x = (int)bboxmin.x; p.x <= bboxmax.x; p.x++) {
for (p.y = (int)bboxmin.y; p.y <= bboxmax.y; p.y++) {
glm::vec3 screen = Barycentric(pts[0] / pts[0][3], pts[1] / pts[1][3], pts[2] / pts[2][3], p);
float z = pts[0][2] * screen.x + pts[1][2] * screen.y + pts[2][2] * screen.z;
float w = pts[0][3] * screen.x + pts[1][3] * screen.y + pts[2][3] * screen.z;
int fragDepth = (int)(z / w);
// This is used for texture perspective correction
glm::vec3 clip = glm::vec3(screen.x / pts[0][3], screen.y / pts[1][3], screen.z / pts[2][3]);
clip /= clip.x + clip.y + clip.z;
//
if (screen.x < 0 || screen.y < 0 || screen.z < 0 || zBuffer[p.x + p.y * winWidth] > fragDepth) {
continue;
}
if (tex.width > 0) {
glm::vec2 uv;
glm::mat3x2 m(uvs[0], uvs[1], uvs[2]);
uv = m * clip;
int y = (int)(uv.y * (tex.height - 1));
int x = (int)(uv.x * (tex.width - 1));
color = tex.pixels[y * tex.width + x];
} else {
color = WireFrameColor;
}
color = FlipColor(color);
zBuffer[p.x + p.y * winWidth] = fragDepth;
PutPixelNoCheck(bitPointer, color, p.x, p.y, winWidth, winHeight);
}
}
}
void RENDER::DrawTriangleBarycentric (COLORREF * bitPointer, glm::vec4 * pts,
iSHADER & shader, int winWidth, int winHeight, int * zBuffer)
{
glm::vec2 bboxmin(std::numeric_limits<float>::max(), std::numeric_limits<float>::max());
glm::vec2 bboxmax(-std::numeric_limits<float>::max(), -std::numeric_limits<float>::max());
glm::vec2 clampMax(winWidth - 1, winHeight - 1);
glm::vec2 clampMin(1, 1);
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 2; j++) {
bboxmin[j] = std::max(clampMin[j], std::min(bboxmin[j], pts[i][j] / pts[i][3]));
bboxmax[j] = std::min(clampMax[j], std::max(bboxmax[j], pts[i][j] / pts[i][3]));
}
}
glm::ivec2 p;
COLORREF color;
for (p.x = (int)bboxmin.x; p.x <= bboxmax.x; p.x++) {
for (p.y = (int)bboxmin.y; p.y <= bboxmax.y; p.y++) {
glm::vec3 screen = Barycentric(pts[0] / pts[0][3], pts[1] / pts[1][3], pts[2] / pts[2][3], p);
float z = pts[0][2] * screen.x + pts[1][2] * screen.y + pts[2][2] * screen.z;
float w = pts[0][3] * screen.x + pts[1][3] * screen.y + pts[2][3] * screen.z;
int fragDepth = (int)(z / w);
// This is used for texture perspective correction
glm::vec3 clip = glm::vec3(screen.x / pts[0][3], screen.y / pts[1][3], screen.z / pts[2][3]);
clip /= clip.x + clip.y + clip.z;
//
if (screen.x < 0 || screen.y < 0 || screen.z < 0 || zBuffer[p.x + p.y * winWidth] > fragDepth) {
continue;
}
bool discard = shader.Fragment(clip, color);
if (!discard) {
color = FlipColor(color);
zBuffer[p.x + p.y * winWidth] = fragDepth;
PutPixelNoCheck(bitPointer, color, p.x, p.y, winWidth, winHeight);
}
}
}
}
void RENDER::DrawNode (const OBJECT_NODE * node, const glm::mat4 & matrixTransform, COLORREF * bitPointer,
int winWidth, int winHeight, int * zBuffer, DISPLAY_MODE displayMode)
{
const objMODEL * prim = &node->object;
size_t indSize = prim->vertexIndices.size();
SHADER_MODEL shader;
shader.SetTransformMatrix(matrixTransform);
shader.SetModel(prim);
if (displayMode == DISPLAY_MODE::POINTS) {
for (size_t i = 0; i < prim->vertices.size(); i++) {
glm::vec4 vec = shader.Vertex(i, 0);
vec /= vec.w;
DrawPoint(bitPointer, vec, winWidth, winHeight, zBuffer);
}
} else {
for (size_t i = 0; i < indSize; i += 3) {
glm::vec4 pts[3];
for (int c = 0; c < 3; c++) {
pts[c] = shader.Vertex(i + c, c);
}
if (displayMode == DISPLAY_MODE::WIREFRAME) {
DrawLine(bitPointer, pts[0] / pts[0].w, pts[1] / pts[1].w, winWidth, winHeight, zBuffer);
DrawLine(bitPointer, pts[1] / pts[1].w, pts[2] / pts[2].w, winWidth, winHeight, zBuffer);
DrawLine(bitPointer, pts[2] / pts[2].w, pts[0] / pts[0].w, winWidth, winHeight, zBuffer);
} else { // displayMode == DISPLAY_MODE::TRIANGLES
DrawTriangleBarycentric(bitPointer, pts, shader, winWidth, winHeight, zBuffer);
}
}
}
}
void RENDER::DrawScene (HWND hWnd, int winWidth, int winHeight) const
{
BITMAPINFO bitmap;
HBITMAP hBitmap;
RECT rect;
COLORREF * bitPointer;
PAINTSTRUCT ps;
// Get current DC
HDC hDC = GetDC(hWnd);
BeginPaint(hWnd, &ps);
// Create memory DC
HDC hMemDC = CreateCompatibleDC(hDC);
// Create DIB section
CreateDIB(hDC, &hBitmap, &bitPointer, &bitmap);
SelectObject(hMemDC, hBitmap);
// Copy window to memory
BitBlt(hMemDC, 0, 0, winWidth, winHeight, hDC, 0, 0, SRCCOPY);
// Fill the memory window with black color
HBRUSH brush = CreateSolidBrush(backgroundColor);
GetClientRect(hWnd, &rect);
FillRect(hMemDC, &rect, brush);
// Drawing scene
glm::mat4 worldMatrix = GetViewportMatrix(0, 0, winWidth, winHeight) *
GetProjectionMatrix() * camera.GetWorldToViewMatrix();
RENDER_VISITOR rv(worldMatrix, winWidth, winHeight, bitPointer, displayMode);
rv.Apply(*root);
// Copy memory to window
BitBlt(hDC, 0, 0, winWidth, winHeight, hMemDC, 0, 0, SRCCOPY);
// Clear up
DeleteObject(hBitmap);
DeleteObject(hMemDC);
EndPaint(hWnd, &ps);
}
glm::mat4 RENDER::GetProjectionMatrix (void) const
{
return glm::mat4({ 1, 0, 0, 0 }, { 0, 1, 0, 0 }, { 0, 0, 1, 1 }, { 0, 0, 1, 0 });
}
glm::mat4 RENDER::GetViewportMatrix (int x0, int y0, int width, int height)
{
glm::mat4 m = glm::mat4();
m[3][0] = x0 + width / 2.f;
m[3][1] = y0 + height / 2.f;
m[3][2] = 255 / 2.f;
m[0][0] = width / 2.f;
m[1][1] = height / 2.f;
m[2][2] = 255 / 2.f;
return m;
}
/* END OF 'render.cpp' FILE */