-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshader.cpp
511 lines (411 loc) · 20.1 KB
/
shader.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
//
// Copyright 2018-present, Pouya Kary. All Rights Reserved. <pouya@kary.us>
//
//
// ─── IMPORTS ────────────────────────────────────────────────────────────────────
//
#ifdef __APPLE__
#include <GLUT/glut.h>
#include <stdlib.h>
#else
#include <GL/glut.h>
#endif
#include <math.h>
#include <vector>
#include <thread>
// ────────────────────────────────────────────────────────────────────────────────
//
// ┌─────────────────────────────────────────────────────────────────────────┐
// │**************************** View Buffer ***************************** │
// ├─────────────────────────────────────────────────────────────────────────┤
// │ │
// │ ┌───┐ │
// │ │ A │ │
// │ └───┘ │
// │ ╱╲ │
// │ ╱ ╲ │
// │ ╱ ╲ │
// │ ╱ ╲ │
// │ ╱ ╲ │
// │ ╱ ╲ │
// │ ╱ ╲ │
// │ ╱ ╲ │
// │ ╱ ╲ │
// │ ┌───┐ ╱ ╲ ┌───┐ │
// │ │ L │ ╱ ╲ │ R │ │
// │ └───┘ ╱ ╲ └───┘ │
// ├───────────────────────╳────────────*───────────╳────────────────────────┤
// │ ╱ ┌───┐ ╲ │
// │ ╱ │ S │ ╲ │
// │ ╱ └───┘ ╲ │
// │ ╱ ╲ │
// │ ╱ ╲ │
// │ ╱ ╲ │
// │ ╱ ╲ │
// │ ╱ ╲ │
// │ ╱ ╲ │
// │ ╱ ╲ ┌───┐ │
// │ ┌───┐ ╱──────────────────────────────────────────────╲ │ B │ │
// │ │ C │ └───┘ │
// │ └───┘ │
// │ │
// └─────────────────────────────────────────────────────────────────────────┘
//
//
// ─── TYPES ──────────────────────────────────────────────────────────────────────
//
struct Point {
float x; float y;
};
struct Line {
Point start; Point end;
};
struct RGBA {
float R; float G; float B;
};
struct Triangle_Info {
float starting_x; float starting_y;
float ending_x; float ending_y;
};
struct Scan_Line_Info {
Point L; // Left point of intersection in scanning line with triangle
Point R; // Right point of intersection in scanning line with triangle
};
//
// ─── SETTING CONSTANTS ──────────────────────────────────────────────────────────
//
// window
const int screen_width =
800;
const int screen_height =
500;
// triangle
const struct Point A =
{ 400, 100 };
const struct Point C =
{ 150, 400 };
const struct Point B =
{ 650, 400 };
// triangle edges
const struct Line AC =
{ A, C };
const struct Line CB =
{ C, B };
const struct Line BA =
{ B, A };
const std::vector<Line> edges =
{ AC, CB, BA };
// origin
const struct Point origin =
{ 0, 0 };
// Lighting
const struct RGBA A_Lighting =
{ 0.5, 0.0, 1.0 };
const struct RGBA C_Lighting =
{ 0.0, 0.0, 0.0 };
const struct RGBA B_Lighting =
{ 0.0, 0.0, 0.0 };
const struct RGBA BLACK =
{ 0.f, 0.f, 0.f };
const float stronging_ratio =
4.f;
// threading
const auto cores =
std::thread::hardware_concurrency( );
//
// ─── GLOBALS ────────────────────────────────────────────────────────────────────
//
RGBA ** screen_matrix;
float display_number;
//
// ─── INIT SCREEN MATRIX ─────────────────────────────────────────────────────────
//
void init_screen_matrix ( ) {
screen_matrix =
new RGBA * [ screen_height ];
for( int i = 0; i < screen_height; i++ )
screen_matrix[ i ] = new RGBA[ screen_width ];
for ( int y = 0; y < screen_height; y++ )
for ( int x = 0; x < screen_width; x++ )
screen_matrix[ y ][ x ] = BLACK;
}
//
// ─── VECTOR LENGTH ──────────────────────────────────────────────────────────────
//
inline float vertex_length ( Point a, Point b ) {
return abs( sqrt( pow( b.x - a.x, 2 ) + pow( b.y - a.y, 2 ) ) );
}
//
// ─── DO LINES INTERSECT ─────────────────────────────────────────────────────────
//
bool are_lines_intersecting( Line a, Line b ) {
const auto first_condition =
( ( ( b.start.x - a.start.x ) * ( a.end.y - a.start.y )
- ( b.start.y - a.start.y ) * ( a.end.x - a.start.x ) )
* ( ( b.end.x - a.start.x ) * ( a.end.y - a.start.y )
- ( b.end.y - a.start.y ) * ( a.end.x - a.start.x )
) < 0 );
const auto second_condition =
( ( ( a.start.x - b.start.x ) * ( b.end.y - b.start.y )
- ( a.start.y - b.start.y ) * ( b.end.x - b.start.x ) )
* ( ( a.end.x - b.start.x ) * ( b.end.y - b.start.y )
- ( a.end.y - b.start.y ) * ( b.end.x - b.start.x )
) < 0 );
return first_condition && second_condition;
}
//
// ─── SCREEN MINIMUM ─────────────────────────────────────────────────────────────
//
Triangle_Info get_triangle_info ( ) {
float minimum_x = screen_width;
float minimum_y = screen_height;
float maximum_x = 0.0;
float maximum_y = 0.0;
for ( auto edge : edges ) {
const auto x =
edge.start.x;
const auto y =
edge.start.y;
if ( minimum_x > x ) minimum_x = x;
if ( minimum_y > y ) minimum_y = y;
if ( maximum_x < x ) maximum_x = x;
if ( maximum_y < y ) maximum_y = y;
}
const Triangle_Info result =
{ minimum_x, minimum_y, maximum_x, maximum_y };
return result;
}
//
// ─── COMBINE COLORS ─────────────────────────────────────────────────────────────
//
RGBA combine_colors ( RGBA color_a, float volume_a, RGBA color_b, float volume_b ) {
const auto new_R =
( ( color_a.R * volume_b ) + ( color_b.R * volume_a ) ) / 2;
const auto new_G =
( ( color_a.G * volume_b ) + ( color_b.G * volume_a ) ) / 2;
const auto new_B =
( ( color_a.B * volume_b ) + ( color_b.B * volume_a ) ) / 2;
const struct RGBA result =
{ new_R, new_G, new_B };
return result;
}
//
// ─── GET PERCENTAGE BASED RATIO ─────────────────────────────────────────────────
//
float get_percentage_based_ration ( float input ) {
return abs( cos( sin( input ) * display_number ) );
}
//
// ─── COMPUTE SCAN LINE LIGHTS ───────────────────────────────────────────────────
//
RGBA get_RGBA_on_linear_interpolation ( Point position,
Point up,
Point down,
RGBA up_color,
RGBA down_color ) {
const auto up_distance =
vertex_length( up, position );
const auto down_distance =
vertex_length( down, position );
const auto up_down_size =
vertex_length( up, down );
const auto up_volume =
get_percentage_based_ration( up_distance / up_down_size );
const auto down_volume =
get_percentage_based_ration( down_distance / up_down_size );
const auto new_RGBA =
combine_colors( up_color, up_volume, down_color, down_volume );
return new_RGBA;
}
//
// ─── GET IR ─────────────────────────────────────────────────────────────────────
//
inline RGBA get_IR ( Point position ) {
return get_RGBA_on_linear_interpolation(
position, A, B, A_Lighting, B_Lighting
);
}
//
// ─── COMPUTE IR ─────────────────────────────────────────────────────────────────
//
inline RGBA get_IL ( Point position ) {
return get_RGBA_on_linear_interpolation(
position, A, C, A_Lighting, C_Lighting
);
}
//
// ─── COMPUTE IS ─────────────────────────────────────────────────────────────────
//
inline RGBA get_IS ( Point position, RGBA IL, RGBA IR, Scan_Line_Info scan_line ) {
return get_RGBA_on_linear_interpolation(
position, scan_line.L, scan_line.R, IL, IR
);
}
//
// ─── APPLY AMBIENT LIGHT ────────────────────────────────────────────────────────
//
inline RGBA sharpen_color ( RGBA color ) {
return {
color.R * stronging_ratio,
color.G * stronging_ratio,
color.B * stronging_ratio
};
}
//
// ─── COMPUTE COLOR ON POSITION ──────────────────────────────────────────────────
//
RGBA compute_color_on_position ( float x, float y, Scan_Line_Info scan_line ) {
const struct Point position =
{ x, y };
const auto IL =
get_IL( position );
const auto IR =
get_IR( position );
const auto IS =
get_IS( position, IL, IR, scan_line );
const auto resulting_color =
sharpen_color( IS );
return resulting_color;
}
//
// ─── SET COLOR BASED ON POSITION ────────────────────────────────────────────────
//
void set_color_based_on_position ( float x, float y, Scan_Line_Info line ) {
if ( line.L.x <= x && line.R.x >= x && line.L.y > 100 && line.L.y < 400 )
screen_matrix[ (int) y ][ (int) x ] =
compute_color_on_position( x, y, line );
else
screen_matrix[ (int) y ][ (int) x ] = BLACK;
}
//
// ─── GET SCANNING LINE INFORMATION ──────────────────────────────────────────────
//
Scan_Line_Info get_scan_line_info ( float line_number, Triangle_Info info ) {
struct Point L =
{ 0.f, line_number };
struct Point R =
{ screen_width - 1, line_number };
bool found_L =
false;
// getting the L and R
for ( float x = info.starting_x - 1; x <= info.ending_x; x++ ) {
const struct Point origin =
{ 0.f, line_number };
const struct Point current_position =
{ x, line_number };
const struct Line checking_line =
{ origin, current_position };
if ( !found_L && are_lines_intersecting( checking_line, AC ) ) {
L.x = x;
found_L = true;
}
if ( are_lines_intersecting( checking_line, BA ) ) {
R.x = x;
goto done_with_scanning;
}
}
done_with_scanning:
const struct Scan_Line_Info result =
{ L, R };
// done
return result;
}
//
// ─── DRAWING THREAD TASK ────────────────────────────────────────────────────────
//
void drawing_thread_task ( float test_start_line,
float task_end_line,
Triangle_Info info ) {
for ( auto y = test_start_line; y <= task_end_line; y++ ) {
const struct Scan_Line_Info line_info =
get_scan_line_info( y, info );
for ( auto x = info.starting_x; x <= info.ending_x; x++ ) {
set_color_based_on_position( x, y, line_info );
}
}
}
//
// ─── OPTIMAL FOR ────────────────────────────────────────────────────────────────
//
void optimal_triangle_drawing_loop ( ) {
const auto info =
get_triangle_info( );
const auto range =
info.ending_y - info.starting_y;
const auto thread_grouping_size =
range / cores;
for ( int thread_no = 0; thread_no < cores; thread_no++ ) {
const auto starting_line =
info.starting_y + thread_no * thread_grouping_size;
const auto ending_line =
info.starting_y + ( thread_no + 1 ) * thread_grouping_size;
std::thread process (
drawing_thread_task, starting_line, ending_line, info
);
process.join( );
}
}
//
// ─── DRAW MATRIX ────────────────────────────────────────────────────────────────
//
void draw_screen_matrix ( ) {
glClear( GL_COLOR_BUFFER_BIT );
glBegin( GL_POINTS );
for ( int y = 0; y < screen_height; y++ ) {
for ( int x = 0; x < screen_width; x++ ) {
const auto color =
screen_matrix[ y ][ x ];
glColor3f( color.R, color.G, color.B );
glVertex2f( x, y );
}
}
glEnd( );
glFlush( );
}
//
// ─── UPDATE DISPLAY NUMBER ──────────────────────────────────────────────────────
//
void update_display_number ( ) {
if ( display_number < 100 ) {
display_number += 1;
} else if ( display_number < 1000 ) {
display_number += 20;
} else {
display_number += 50;
}
}
//
// ─── DISPLAY VIEW ───────────────────────────────────────────────────────────────
//
void display ( ) {
update_display_number( );
optimal_triangle_drawing_loop( );
draw_screen_matrix( );
}
//
// ─── INIT ───────────────────────────────────────────────────────────────────────
//
inline void init ( ) {
display_number = 1.;
glClearColor( 0.f, 0.f, 0.f, 0.f );
glClear( GL_COLOR_BUFFER_BIT );
// glColor4f( 1.0, 1.0, 1.0, 1.0 );
glOrtho( 0.f, screen_width, screen_height, 0.f, 0.f, 1.f );
init_screen_matrix( );
}
//
// ─── MAIN ───────────────────────────────────────────────────────────────────────
//
int main ( int argc, char ** argv ) {
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_SINGLE | GLUT_RGB );
glutInitWindowSize( screen_width, screen_height );
glutInitWindowPosition( 100, 100);
glutCreateWindow( "✣ Pouya's Strange Fractal ✣" );
init( );
glutDisplayFunc( display );
glutIdleFunc( display );
glutMainLoop( );
return 0;
}
// ────────────────────────────────────────────────────────────────────────────────