-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWindowManager.h
582 lines (455 loc) · 14.9 KB
/
WindowManager.h
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
#pragma once
#include <SDL.h>
#include "StructAndVecOp.h"
class WindowManager {
private :
vector<Mesh> meshPool;
vector<gameObject> objPool;
vec3d vCamera = { 0,0,0 };
SDL_Renderer* renderer;
SDL_Window* window;
//window size
int width = 1000;
int height = 1000;
mat4x4 matRotX, matRotZ, matRotY;
bool is_top_left(vec2d a, vec2d b) {
vec2d edge = { b.x - a.x,b.y - a.y };
bool is_top_edge = edge.y == 0 && edge.x > 0;
bool is_left_edge = edge.y < 0;
return is_top_edge || is_left_edge;
}
bool is_top_left(vec3d v1, vec3d v2) {
vec2d a = { v1.x,v1.y };
vec2d b = { v2.x,v2.y };
vec2d edge = { b.x - a.x,b.y - a.y };
bool is_top_edge = edge.y == 0 && edge.x > 0;
bool is_left_edge = edge.y < 0;
return is_top_edge || is_left_edge;
}
Mesh getMesh(int i) {
if(i<=meshPool.size())return meshPool[i];
Mesh a;
return a;
}
public:
//Public Variable definition
//Projection Matrix
float fNear = 0.1f;
float fFar = 1000.0f;
float fov = 90.f;
float fAspectRatio = (float)height / width;
float radFov = fov / 180.f * 3.14159f; // conversione da gradi a radianti
float fFovRad = 1.f / tanf(radFov * 0.5f);
mat4x4 projMatrix;
//Mesh Transform Operation
vec3d getMeshPosition(int i) {
return meshPool[i].position;
}
vec3d getMeshRotation(int i) {
return meshPool[i].rotation;
}
void updateMeshPosition(int i, vec3d newPos) {
if (i <= meshPool.size()) {
meshPool[i].position = newPos;
}
}
void updateMeshRotationDegrees(int i, vec3d newRot) {
if (i <= meshPool.size()) {
meshPool[i].rotation = newRot;
}
}
//Object Transform Operation
void adjustObjtrisOrder(int i) {
if (i <= objPool.size()) {
//adjustMeshTrisOrderNew(objPool[i].mesh);
}
}
vec3d getObjPosition(int i) {
return objPool[i].position;
}
vec3d getObjRotation(int i) {
return objPool[i].rotation;
}
void updateObjPosition(int i, vec3d newPos) {
if (i <= objPool.size()) {
objPool[i].position = newPos;
}
}
void updateObjRotationDegrees(int i, vec3d newRot) {
if (i <= objPool.size()) {
objPool[i].rotation = newRot;
}
}
//ObjectPool operation
int getObjPoolSize() {
return objPool.size();
}
void clearObjPool() {
objPool.clear();
}
//MeshPool operation
int getMeshPoolSize() {
return meshPool.size();
}
void clearMeshPool() {
meshPool.clear();
}
//Other Mesh Operation
void makeWater(int i) {
if (objPool[i].mesh.water)return;
meshGenerator genMesh;
Mesh a = genMesh.creatRect(4.f, 0.05f);
a.position = objPool[i].mesh.position;
a.rotation = objPool[i].mesh.rotation;
a.color = {0,0,255,255};
objPool[i].mesh = a;
for (int j = 0; j <8; j++) {
subsetMesh(i);
}
objPool[i].mesh.water = true;
}
void updateMeshColor(int i, Color c) {
if (i <= meshPool.size()) {
meshPool[i].color = c;
}
}
void subsetMesh(int i) {
if (objPool[i].mesh.water)return;
meshGenerator meshGen;
meshGen.subsectMesh(objPool[i].mesh);
}
void subsetAllMesh() {
meshGenerator meshGen;
for (unsigned int i = 0; i < objPool.size(); i++)
{
meshGen.subsectMesh(objPool[i].mesh);
}
}
void addMeshToPool(Mesh m) {
meshPool.insert(meshPool.end(), m);
}
void addObjToPool(gameObject o) {
objPool.insert(objPool.end(), o);
}
//Some Window Operation
int getScreenWidth() {
return width;
}
int getScreenHeight() {
return height;
}
//Constructor & Initializer
void initializeMatrix() {
projMatrix.m[0][0] = fAspectRatio * fFovRad;
projMatrix.m[1][1] = fFovRad;
projMatrix.m[2][2] = fFar / (fFar - fNear);
projMatrix.m[3][2] = (-fFar * fNear) / (fFar - fNear);
projMatrix.m[2][3] = 1.f;
projMatrix.m[3][3] = 0.f;
matRotX.m[0][0] = matRotX.m[3][3] = 1;
matRotZ.m[2][2] = matRotZ.m[3][3] = 1;
matRotY.m[1][1] = matRotY.m[3][3] = 1;
}
WindowManager(const char *title, int x,int y,int h, int w,Uint32 flags) {
width = w;
height = h;
window= SDL_CreateWindow(title, width, height, h, w, flags);
renderer = SDL_CreateRenderer(window, -1, 0);
initializeMatrix();
}
WindowManager() {
window = SDL_CreateWindow("default", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, NULL);
renderer = SDL_CreateRenderer(window, -1, 0);
initializeMatrix();
}
//Drawing on Screen
void render() {
SDL_SetRenderDrawColor(renderer,0, 0, 0, 0);
SDL_RenderPresent(renderer);
}
void clear() {
SDL_RenderClear(renderer);
}
void drawLine(int x1, int y1, int x2, int y2,Color c) {
SDL_SetRenderDrawColor(renderer, c.r, c.g, c.b, c.a);
SDL_RenderDrawLine(renderer, x1, y1, x2, y2);
}
void drawTriangle(vec2d p1, vec2d p2, vec2d p3, Color c) {
drawLine(p1.x, p1.y, p2.x, p2.y, c);
drawLine(p1.x, p1.y, p3.x, p3.y, c);
drawLine(p2.x, p2.y, p3.x, p3.y, c);
}
void drawTriangle(triangle3d t , Color c) {
drawLine(t.p[0].x, t.p[0].y, t.p[1].x, t.p[1].y, c);
drawLine(t.p[0].x, t.p[0].y, t.p[2].x, t.p[2].y, c);
drawLine(t.p[1].x, t.p[1].y, t.p[2].x, t.p[2].y, c);
}
void drawVertsTriangle(triangle3d t, Color c) {
drawLine(t.p[0].x, t.p[0].y, t.p[1].x, t.p[1].y, c);
drawLine(t.p[0].x, t.p[0].y, t.p[2].x, t.p[2].y, c);
drawLine(t.p[1].x, t.p[1].y, t.p[2].x, t.p[2].y, c);
for (int i = 0; i < 3; i++) {
vec2d p;
p.x = t.p[i].x;
p.y = t.p[i].y;
drawBigPoint(p, { 255,0,0,255 });
}
}
void drawRasterizedTriangle(triangle3d t, Color c) {
vector<float> xCords;
vector<float> yCords;
xCords.insert(xCords.end(),t.p[0].x ); xCords.insert(xCords.end(), t.p[1].x); xCords.insert(xCords.end(), t.p[2].x);
yCords.insert(yCords.end(), t.p[0].y); yCords.insert(yCords.end(), t.p[1].y); yCords.insert(yCords.end(), t.p[2].y);
int xMin = fgetMin(xCords);
int xMax = fgetMax(xCords);
int yMin = fgetMin(yCords);
int yMax = fgetMax(yCords);
int bias0 = is_top_left(t.p[1],t.p[2]) ? 0 : -1;
int bias1 = is_top_left(t.p[2], t.p[0]) ? 0 : -1;
int bias2 = is_top_left(t.p[0], t.p[1]) ? 0 : -1;
for (int y = yMin; y <= yMax; y++) {
for (int x = xMin; x <= xMax; x++) {
vec2d point = { x,y };
if (pointIsInTriangle(t,point, bias0, bias1, bias2)) {
SDL_SetRenderDrawColor(renderer, c.r, c.g, c.b, c.a);
SDL_RenderDrawPoint(renderer, point.x, point.y);
}
}
}
}
void drawRasterizedTriangleSdl(triangle3d t, Color c) {
SDL_Color c1 = { c.r, c.g, c.b, c.a };
SDL_Color c2 = { c.r , c.g , c.b , c.a };
SDL_Color c3 = { c.r , c.g , c.b , c.a };
vector< SDL_Vertex > verts =
{
{ SDL_FPoint{ t.p[0].x, t.p[0].y}, c1, SDL_FPoint{0},},
{ SDL_FPoint{ t.p[1].x, t.p[1].y }, c1, SDL_FPoint{ 0 }, },
{ SDL_FPoint{ t.p[2].x, t.p[2].y }, c1, SDL_FPoint{ 0 }, },
};
SDL_RenderGeometry(renderer, nullptr, verts.data(), verts.size(), nullptr, 0);
}
void drawPoint(vec2d p, Color c) {
SDL_SetRenderDrawColor(renderer, c.r, c.g, c.b, c.a);
SDL_RenderDrawPoint(renderer, p.x, p.y);
}
void drawBigPoint(vec2d point,Color c) {
SDL_SetRenderDrawColor(renderer, c.r, c.g, c.b, c.a);
//SDL_RenderDrawPoint(renderer, point.x, point.y);
int size = 3;
for (int i = -(size-1); i < size; i++) {
SDL_RenderDrawPoint(renderer, point.x + i, point.y);
SDL_RenderDrawPoint(renderer, point.x, point.y+i);
}
for (int i = -(size-2); i < size-1; i++) {
SDL_RenderDrawPoint(renderer, point.x + i, point.y + i);
}
for (int i = -(size - 2); i < size - 1; i++) {
SDL_RenderDrawPoint(renderer, point.x - i, point.y - i);
}
}
void drawSquare(vec2d pos, int size, Color c) {
SDL_SetRenderDrawColor(renderer, c.r, c.g, c.b, c.a);
drawLine(pos.x - (int)size / 2, pos.y - (int)size / 2, pos.x + (int)size / 2, pos.y - (int)size / 2, c);
drawLine(pos.x - (int)size / 2, pos.y + (int)size / 2, pos.x + (int)size / 2, pos.y + (int)size / 2, c);
drawLine(pos.x - (int)size / 2, pos.y - (int)size / 2, pos.x - (int)size / 2, pos.y + (int)size / 2, c);
drawLine(pos.x + (int)size / 2, pos.y - (int)size / 2, pos.x + (int)size / 2, pos.y + (int)size / 2, c);
}
void endLoop() {
render();
clear();
}
void endLoop(int delay) {
render();
SDL_Delay(delay);
clear();
}
//Rotation Advanced Operation
void rotateMesh(int i) {
if (i <= meshPool.size()) {
triangle3d triProjected, triTranslated, triRotatedZ, triRotatedX;
for (auto tri : meshPool[i].tris) {
for (int i = 0; i < 3; i++) {
MultiplyMatVec(tri.p[i], triRotatedZ.p[i], matRotZ);
MultiplyMatVec(triRotatedZ.p[i], triRotatedX.p[i], matRotX);
triRotatedX.p[i].x += meshPool[i].position.x;
triRotatedX.p[i].y += meshPool[i].position.y;
triRotatedX.p[i].z += meshPool[i].position.z;
}
}
}
}
void updateRotationMatrices(float time) {
matRotZ.m[0][0] = cosf(time);
matRotZ.m[0][1] = sinf(time);
matRotZ.m[1][0] = -sinf(time);
matRotZ.m[1][1] = cosf(time);
matRotX.m[1][1] = cosf(time);
matRotX.m[1][2] = sinf(time);
matRotX.m[2][1] = -sinf(time);
matRotX.m[2][2] = cosf(time);
}
//update the triangle rotation, according to the mesh rotation
void updateTriFixedRotation(Mesh &mesh, triangle3d &tri) {
mat4x4 FixedMatRotX, FixedMatRotZ, FixedMatRotY;
FixedMatRotX.m[1][1] = cosf(mesh.rotation.x);
FixedMatRotX.m[1][2] = sinf(mesh.rotation.x);
FixedMatRotX.m[2][1] = -sinf(mesh.rotation.x);
FixedMatRotX.m[2][2] = cosf(mesh.rotation.x);
FixedMatRotZ.m[0][0] = cosf(mesh.rotation.z);
FixedMatRotZ.m[0][1] = sinf(mesh.rotation.z);
FixedMatRotZ.m[1][0] = -sinf(mesh.rotation.z);
FixedMatRotZ.m[1][1] = cosf(mesh.rotation.z);
FixedMatRotY.m[0][0] = cosf(mesh.rotation.y);
FixedMatRotY.m[2][0] = sinf(mesh.rotation.y);
FixedMatRotY.m[0][2] = -sinf(mesh.rotation.y);
FixedMatRotY.m[2][2] = cosf(mesh.rotation.y);
FixedMatRotY.m[1][1] = FixedMatRotY.m[3][3] = FixedMatRotX.m[0][0] = FixedMatRotX.m[3][3]= FixedMatRotZ.m[2][2] = FixedMatRotZ.m[3][3] = 1;
tri=rotateTri(tri, FixedMatRotX, FixedMatRotZ, FixedMatRotY);
}
Color changeColorDarknes(Color c, float lum) {
int r = c.r * lum / 150.f;
int g = c.g * lum / 150.f;
int b = c.b * lum / 150.f;
return {r,g,b,c.a};
}
vec3d light_direction = { 0.0f,0.f,-1.f };
void projectTri(triangle3d& in, triangle3d& out) {
for (int i = 0; i < 3; i++) {
MultiplyMatVec(in.p[0], out.p[0], projMatrix);//Proietta un singolo triangolo
MultiplyMatVec(in.p[1], out.p[1], projMatrix);
MultiplyMatVec(in.p[2], out.p[2], projMatrix);
}
}
void updateTriPosition(Mesh& mesh, triangle3d& tri) {
for (int i = 0; i < 3; i++) {
tri.p[i].x += mesh.position.x;
tri.p[i].y += mesh.position.y;
tri.p[i].z += mesh.position.z;
}
} //update the triangle position, according to the mesh position
void drawRenderableTriangles(vector<ColoredTriangle> renderable,int wireFrame) {
sort(renderable.begin(), renderable.end(), [](ColoredTriangle& a, ColoredTriangle& b) {
triangle3d t1 = a.tri;
triangle3d t2 = b.tri;
float z1 = (t1.p[0].z + t1.p[1].z + t1.p[2].z) / 3.f;
float z2 = (t2.p[0].z + t2.p[1].z + t2.p[2].z) / 3.f;
return z1 > z2;
});
for (auto tri : renderable) {
switch (wireFrame % 3) {
case 0:
drawRasterizedTriangleSdl(tri.tri, tri.c);
break;
case 1:
drawTriangle(tri.tri, tri.c);
break;
case 2:
drawRasterizedTriangleSdl(tri.tri, tri.c);
drawVertsTriangle(tri.tri, { 255,255,255,255 });
break;
}
}
}
void normalizeTriangleInScreenSpace(triangle3d &tri) {
tri.p[0].x += 1.f; tri.p[0].y += 1.f;
tri.p[1].x += 1.f; tri.p[1].y += 1.f;
tri.p[2].x += 1.f; tri.p[2].y += 1.f;
tri.p[0].x *= 0.5 * (float)getScreenWidth(); tri.p[0].y *= 0.5 * (float)getScreenWidth();
tri.p[1].x *= 0.5 * (float)getScreenWidth(); tri.p[1].y *= 0.5 * (float)getScreenWidth();
tri.p[2].x *= 0.5 * (float)getScreenWidth(); tri.p[2].y *= 0.5 * (float)getScreenWidth();
}
void renderMesh(int wireFrame,float time) {
vector<ColoredTriangle> renderable;
for (auto mesh : meshPool) {
updateRotationMatrices(time);
for (auto tri : mesh.tris) {
triangle3d triProjected, triTranslated ;
//Update Rotation
updateTriFixedRotation(mesh, tri);
triTranslated = tri;
updateTriPosition(mesh, triTranslated);
//Wave
if (mesh.water) {
float f = 1;
if (true) {
for (int i = 0; i < 3; i++) {
triTranslated.p[i].x += 0.2 * sinf((time + triTranslated.p[i].z) * f);
triTranslated.p[i].y += 0.3 * sinf((time + triTranslated.p[i].x) * f * 2);
triTranslated.p[i].z += 0.2 * sinf((time + triTranslated.p[i].y) * f);
}
}
else {
}
}
//normals
vec3d normal = getNormalVector(triTranslated);
if ((normal.x * (triTranslated.p[0].x - vCamera.x) +
normal.y * (triTranslated.p[0].y - vCamera.y) +
normal.z * (triTranslated.p[0].z - vCamera.z) < 0.f)) {
normalizeVec(light_direction);
float dp = dotProdutct(normal, light_direction);
//calc lum
int lum = (int)(150.f * dp);
Color litghedColor = changeColorDarknes(mesh.color, lum);
if (wireFrame%3 != 0) {
litghedColor = mesh.color;
}
projectTri(triTranslated, triProjected);//Proietta un singolo triangolo
//La matrice di proiezione restituisce un risultato in uno schermo normalizzato da -1 ad +1. Va scalato !
normalizeTriangleInScreenSpace(triProjected);
renderable.insert(renderable.end(), { triProjected,litghedColor });
}
}
}
drawRenderableTriangles(renderable, wireFrame);
}
void renderObject(int wireFrame,float time) {
vector<ColoredTriangle> renderable;
for (auto obj : objPool) {
Mesh mesh = obj.mesh;
mesh.position = obj.position;
mesh.rotation = obj.rotation;
updateRotationMatrices(time);
for (auto tri : mesh.tris) {
triangle3d triProjected, triTranslated ;
//Update Rotation
updateTriFixedRotation(mesh, tri);
triTranslated = tri;
updateTriPosition(mesh, triTranslated);
//Wave
if (mesh.water) {
float f = 1;
if (true) {
for (int i = 0; i < 3; i++) {
triTranslated.p[i].x += 0.2 * sinf((time + triTranslated.p[i].z) * f);
triTranslated.p[i].y += 0.3 * sinf((time + triTranslated.p[i].x) * f * 2);
triTranslated.p[i].z += 0.2 * sinf((time + triTranslated.p[i].y) * f);
}
}
else {
}
}
//normals
vec3d normal = getNormalVector(triTranslated);
if ((normal.x * (triTranslated.p[0].x - vCamera.x) +
normal.y * (triTranslated.p[0].y - vCamera.y) +
normal.z * (triTranslated.p[0].z - vCamera.z) < 0.f)) {
normalizeVec(light_direction);
float dp = dotProdutct(normal, light_direction);
//calc lum
int lum = (int)(150.f * dp);
Color litghedColor = changeColorDarknes(mesh.color, lum);
if (wireFrame%3 != 0) {
litghedColor = mesh.color;
}
projectTri(triTranslated, triProjected);//Proietta un singolo triangolo
//La matrice di proiezione restituisce un risultato in uno schermo normalizzato da -1 ad +1. Va scalato !
normalizeTriangleInScreenSpace(triProjected);
renderable.insert(renderable.end(), { triProjected,litghedColor });
}
}
}
drawRenderableTriangles(renderable, wireFrame);
}
};