-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathraytrace.asm
604 lines (533 loc) · 15.1 KB
/
raytrace.asm
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
;==============================================================================
; Configurations and predefinitions
; Using nasm -d option to change the default settings
;==============================================================================
%ifndef Image_Width
%define Image_Width 640
%endif
%ifndef Image_Height
%define Image_Height 480
%endif
%ifndef RowStart
%define RowStart 0
%endif
%ifndef RowCount
%define RowCount Image_Width
%endif
%ifndef LineStart
%define LineStart 0
%endif
%ifndef LineCount
%define LineCount Image_Height
%endif
%ifndef FixedBits
%define FixedBits 16
%endif
%ifndef RayStepCount
%define RayStepCount 160
%endif
%ifndef SampleDepth
%define SampleDepth 12
%endif
%ifndef GroundOnly
%define GroundOnly 0
%endif
%if Image_Width % 4
%assign Image_Width Image_Width + 4 - Image_Width % 4
%endif
;==============================================================================
; Bitmap file header and info header
; Can be disabled for parcial rendering
;==============================================================================
%ifndef No_Header
BITMAPFILEHEADER:
.bfType db "BM"
.bfSize dd EOF - BITMAPFILEHEADER
.bfReserved1 dw 0
.bfReserved2 dw 0
.bfOffBits dd BitmapStart
BITMAPINFOHEADER:
.biSize dd 40
%ifdef No_Bitmap
.biWidth dd Image_Width
.biHeight dd Image_Height
%else
.biWidth dd RowCount
.biHeight dd LineCount
%endif
.biPlanes dw 1
.biBitCount dw 24
.biCompression dd 0
.biSizeImage dd 0
.biXPelsPerMeter dd 0
.biYPelsPerMeter dd 0
.biClrUsed dd 0
.biClrImportant dd 0
%endif ;No_Header
;==============================================================================
; Fixed number arithmetic
;==============================================================================
%assign FixedBase 1 << FixedBits
%assign SqrtBase (1 << (FixedBits / 2))
%define MakeFixed(i) ((i) * FixedBase)
%define FixedToInt(i) ((i) // FixedBase)
%define FixedMul(a,b) (((a) * (b)) // FixedBase)
%define FixedDiv(a,b) (((a) * FixedBase) // (b))
%assign FixedMax ((-1) / 2)
%assign CastEpsilon FixedDiv(1, 100)
%assign CastEpsilon2 CastEpsilon + CastEpsilon
;==============================================================================
; Variables used to store the return value of the macros
;==============================================================================
%assign result 0
%assign result_index 0
%assign result_r 0
%assign result_g 0
%assign result_b 0
%assign result_x 0
%assign result_y 0
%assign result_z 0
%assign result_w 0
;==============================================================================
; Macro functions for vector arithmetic
;==============================================================================
%macro IntSqrt 1
%assign %%sqrt_v_bit 15
%assign %%sqrt_n 0
%assign %%sqrt_b 0x8000
%assign %%sqrt_x %1
%if %%sqrt_x <= 1
%assign result %1
%else
%rep 16
%assign %%sqrt_temp ((%%sqrt_n << 1) + %%sqrt_b) << (%%sqrt_v_bit)
%assign %%sqrt_v_bit %%sqrt_v_bit - 1
%if %%sqrt_x >= %%sqrt_temp
%assign %%sqrt_n %%sqrt_n + %%sqrt_b
%assign %%sqrt_x %%sqrt_x - %%sqrt_temp
%endif
%assign %%sqrt_b %%sqrt_b >> 1
%endrep
%assign result %%sqrt_n
%endif
%endmacro
%macro FixedSqrt 1
IntSqrt %1
%assign result result * SqrtBase
%endmacro
%define Mix(x,y,a) FixedMul((x), FixedBase - (a)) + FixedMul((y), (a))
%macro Min 2
%if %1 < %2
%assign result %1
%else
%assign result %2
%endif
%endmacro
%macro Max 2
%if %1 > %2
%assign result %1
%else
%assign result %2
%endif
%endmacro
%macro Abs 1
%if %1 >= 0
%assign result %1
%else
%assign result -%1
%endif
%endmacro
%macro Saturate 3
%if %1 < 0
%assign result_r 0
%elif %1 > FixedBase
%assign result_r FixedBase
%else
%assign result_r %1
%endif
%if %2 < 0
%assign result_g 0
%elif %2 > FixedBase
%assign result_g FixedBase
%else
%assign result_g %2
%endif
%if %3 < 0
%assign result_b 0
%elif %3 > FixedBase
%assign result_b FixedBase
%else
%assign result_b %3
%endif
%endmacro
%macro Dot 4-8
%if %0 == 4
%assign result FixedMul(%1, %3) + FixedMul(%2, %4)
%elif %0 == 6
%assign result FixedMul(%1, %4) + FixedMul(%2, %5) + FixedMul(%3, %6)
%elif %0 == 8
%assign result FixedMul(%1, %5) + FixedMul(%2, %6) + FixedMul(%3, %7) + FixedMul(%4, %8)
%endif
%endmacro
%macro FixedIPow 2
%if %2 < 0
%assign result 0
%else
%assign result FixedBase
%rep %2
%assign result FixedMul(result, %1)
%endrep
%endif
%endmacro
%macro Length 2-4
%if %0 == 2
Dot %1, %2, %1, %2
%elif %0 == 3
Dot %1, %2, %3, %1, %2, %3
%elif %0 == 4
Dot %1, %2, %3, %4, %1, %2, %3, %4
%endif
FixedSqrt result
%endmacro
%macro Distance 4-8
%if %0 == 4
Length %3 - %1, %4 - %2
%elif %0 == 6
Length %4 - %1, %5 - %2, %6 - %3
%elif %0 == 8
Length %5 - %1, %6 - %2, %7 - %3, %8 - %4
%endif
%endmacro
%macro Normalize 2-4
%if %0 == 2
Length %1, %2
Max result, 1
%assign result_x FixedDiv(%1, result)
%assign result_y FixedDiv(%2, result)
%elif %0 == 3
Length %1, %2, %3
Max result, 1
%assign result_x FixedDiv(%1, result)
%assign result_y FixedDiv(%2, result)
%assign result_z FixedDiv(%3, result)
%elif %0 == 4
Length %1, %2, %3, %4
Max result, 1
%assign result_x FixedDiv(%1, result)
%assign result_y FixedDiv(%2, result)
%assign result_z FixedDiv(%3, result)
%assign result_w FixedDiv(%4, result)
%endif
%endmacro
%macro Reflect 6
Dot %1,%2,%3,%4,%5,%6
%assign %%n_2 FixedMul(result, MakeFixed(2))
%assign result_x %1 - FixedMul(%4, %%n_2)
%assign result_y %2 - FixedMul(%5, %%n_2)
%assign result_z %3 - FixedMul(%6, %%n_2)
%endmacro
;==============================================================================
; Scene setup
;==============================================================================
%assign CamPos_x MakeFixed(0)
%assign CamPos_y MakeFixed(2)
%assign CamPos_z MakeFixed(7)
%assign Sphere1_x MakeFixed(0)
%assign Sphere1_y MakeFixed(2)
%assign Sphere1_z MakeFixed(0)
%assign Sphere1_r MakeFixed(2)
%assign Sphere1_Visible 1
%assign Sphere2_x MakeFixed(1)
%assign Sphere2_y MakeFixed(1)
%assign Sphere2_z MakeFixed(2)
%assign Sphere2_r MakeFixed(1)
%assign Sphere2_Visible 1
%assign Sphere3_x MakeFixed(-3)
%assign Sphere3_y MakeFixed(1)
%assign Sphere3_z MakeFixed(0)
%assign Sphere3_r MakeFixed(1)
%assign Sphere3_Visible 1
%assign LightDir_x MakeFixed(1)
%assign LightDir_y MakeFixed(-1)
%assign LightDir_z MakeFixed(1)
%assign LightPow 20
%assign LightColor_r FixedDiv(10, 10)
%assign LightColor_g FixedDiv( 8, 10)
%assign LightColor_b FixedDiv( 6, 10)
%assign FogColor_r FixedDiv( 8, 10)
%assign FogColor_g FixedDiv( 9, 10)
%assign FogColor_b FixedDiv(10, 10)
%assign SkyColor_r FixedDiv(2, 10)
%assign SkyColor_g FixedDiv(5, 10)
%assign SkyColor_b FixedDiv(8, 10)
%assign FogDistance MakeFixed(100)
%if GroundOnly
%assign Sphere1_Visible 0
%assign Sphere2_Visible 0
%assign Sphere3_Visible 0
%elif Sphere1_Visible == 0 && Sphere2_Visible == 0 && Sphere3_Visible == 0
%assign GroundOnly 1
%endif
Normalize LightDir_x, LightDir_y, LightDir_z
%assign LightDir_x result_x
%assign LightDir_y result_y
%assign LightDir_z result_z
;==============================================================================
; Bounding box
;==============================================================================
%assign boundbox_xn MakeFixed(-5)
%assign boundbox_yn MakeFixed(-1)
%assign boundbox_zn MakeFixed(-3)
%assign boundbox_xp MakeFixed(3)
%assign boundbox_yp MakeFixed(5)
%assign boundbox_zp MakeFixed(4)
%macro testawayfrom_bb 6
%if GroundOnly
%assign result 1
%else
%if %1 < boundbox_xn && %4 <= 0
%assign result 1
%elif %1 > boundbox_xp && %4 >= 0
%assign result 1
%elif %2 < boundbox_yn && %5 <= 0
%assign result 1
%elif %2 > boundbox_yp && %5 >= 0
%assign result 1
%elif %3 < boundbox_zn && %6 <= 0
%assign result 1
%elif %3 > boundbox_zp && %6 >= 0
%assign result 1
%else
%assign result 0
%endif
%endif
%endmacro
;==============================================================================
; Scene functions
;==============================================================================
%macro SkyColor 3
Dot LightDir_x, LightDir_y, LightDir_z, -%1, -%2, -%3
FixedIPow result, LightPow
%assign %%SunLum result
Abs %2
%assign %%FogDensity FixedBase - result
%assign result_r Mix(SkyColor_r, FogColor_r, %%FogDensity) + FixedMul(%%SunLum, LightColor_r)
%assign result_g Mix(SkyColor_g, FogColor_g, %%FogDensity) + FixedMul(%%SunLum, LightColor_g)
%assign result_b Mix(SkyColor_b, FogColor_b, %%FogDensity) + FixedMul(%%SunLum, LightColor_b)
%endmacro
%macro Map_Dist 3
%assign %%Dist_1 %2
%assign result_index 0
%if Sphere1_Visible
Distance %1, %2, %3, Sphere1_x, Sphere1_y, Sphere1_z
%assign %%Dist_2 result - Sphere1_r
%else
%assign %%Dist_2 FixedMax
%endif
%if Sphere2_Visible
Distance %1, %2, %3, Sphere2_x, Sphere2_y, Sphere2_z
%assign %%Dist_3 result - Sphere2_r
%else
%assign %%Dist_3 FixedMax
%endif
%if Sphere3_Visible
Distance %1, %2, %3, Sphere3_x, Sphere3_y, Sphere3_z
%assign %%Dist_4 result - Sphere3_r
%else
%assign %%Dist_4 FixedMax
%endif
%assign result FixedMax
%if %%Dist_1 < result
%assign result %%Dist_1
%if (((%1) * 2 / FixedBase) & 1) ^ (((%3) * 2 / FixedBase) & 1)
%assign result_index 1
%else
%assign result_index 5
%endif
%endif
%if %%Dist_2 < result
%assign result %%Dist_2
%assign result_index 2
%endif
%if %%Dist_3 < result
%assign result %%Dist_3
%assign result_index 3
%endif
%if %%Dist_4 < result
%assign result %%Dist_4
%assign result_index 4
%endif
%endmacro
%macro Map_Normal 3
Map_Dist %1, %2, %3
%if result_index == 1 || result_index == 5
%assign result_x 0
%assign result_y FixedBase
%assign result_z 0
%elif result_index == 2
%assign %%normal_x %1 - Sphere1_x
%assign %%normal_y %2 - Sphere1_y
%assign %%normal_z %3 - Sphere1_z
Normalize %%normal_x, %%normal_y, %%normal_z
%elif result_index == 3
%assign %%normal_x %1 - Sphere2_x
%assign %%normal_y %2 - Sphere2_y
%assign %%normal_z %3 - Sphere2_z
Normalize %%normal_x, %%normal_y, %%normal_z
%elif result_index == 4
%assign %%normal_x %1 - Sphere3_x
%assign %%normal_y %2 - Sphere3_y
%assign %%normal_z %3 - Sphere3_z
Normalize %%normal_x, %%normal_y, %%normal_z
%endif
%endmacro
%macro Map_Color 3
Map_Dist %1, %2, %3
%if result_index == 1
%assign result_r FixedBase
%assign result_g FixedBase
%assign result_b FixedBase
%elif result_index == 2
%assign result_r FixedDiv(192,256)
%assign result_g FixedDiv(256,256)
%assign result_b FixedDiv(32,256)
%elif result_index == 3
%assign result_r FixedDiv(32,256)
%assign result_g FixedDiv(192,256)
%assign result_b FixedDiv(256,256)
%elif result_index == 4
%assign result_r FixedDiv(256,256)
%assign result_g FixedDiv(32,256)
%assign result_b FixedDiv(192,256)
%elif result_index == 5
%assign result_r FixedDiv(5, 10)
%assign result_g FixedDiv(5, 10)
%assign result_b FixedDiv(5, 10)
%endif
%endmacro
%macro Ground_Cast 6
%if %5 == 0
%assign result -1
%else
%assign result FixedDiv(%2, -%5)
%endif
%endmacro
%macro Map_Cast 6
%assign %%dist 0
%assign %%hit 0
%rep RayStepCount
testawayfrom_bb %1, %2, %3, %4, %5, %6
%if result
%if %5 < 0
Ground_Cast %1, %2, %3, %4, %5, %6
%if result >= 0
%assign %%dist %%dist + result
%assign %%hit 1
%endif
%endif
%exitrep
%else
%assign %%cur_x %1 + FixedMul(%4, %%dist)
%assign %%cur_y %2 + FixedMul(%5, %%dist)
%assign %%cur_z %3 + FixedMul(%6, %%dist)
Map_Dist %%cur_x, %%cur_y, %%cur_z
%assign %%dist %%dist + result
%if result <= CastEpsilon
%assign %%hit 1
%exitrep
%endif
%endif
%endrep
%if %%hit != 0
%assign result %%dist
%else
%assign result -1
%endif
%endmacro
;==============================================================================
; The main macro for render the scene
;==============================================================================
%macro RenderScene 6
%assign %%cro_x %1
%assign %%cro_y %2
%assign %%cro_z %3
%assign %%crd_x %4
%assign %%crd_y %5
%assign %%crd_z %6
%assign %%mask_r FixedBase
%assign %%mask_g FixedBase
%assign %%mask_b FixedBase
%rep SampleDepth
Map_Cast %%cro_x, %%cro_y, %%cro_z, %%crd_x, %%crd_y, %%crd_z
%if result < 0
%exitrep
%else
%assign %%cast_x %%cro_x + FixedMul(%%crd_x, result)
%assign %%cast_y %%cro_y + FixedMul(%%crd_y, result)
%assign %%cast_z %%cro_z + FixedMul(%%crd_z, result)
%assign %%foggy FixedDiv(result, FogDistance)
Map_Normal %%cast_x, %%cast_y, %%cast_z
%assign %%cast_nx result_x
%assign %%cast_ny result_y
%assign %%cast_nz result_z
Map_Color %%cast_x, %%cast_y, %%cast_z
%if %%foggy > FixedBase
%assign %%foggy FixedBase
%endif
%assign result_r Mix(result_r, FogColor_r, %%foggy)
%assign result_g Mix(result_g, FogColor_g, %%foggy)
%assign result_b Mix(result_b, FogColor_b, %%foggy)
%assign %%mask_r FixedMul(%%mask_r, result_r)
%assign %%mask_g FixedMul(%%mask_g, result_g)
%assign %%mask_b FixedMul(%%mask_b, result_b)
%assign %%cro_x %%cast_x
%assign %%cro_y %%cast_y
%assign %%cro_z %%cast_z
Reflect %%crd_x, %%crd_y, %%crd_z, %%cast_nx, %%cast_ny, %%cast_nz
Normalize result_x, result_y, result_z
%assign %%crd_x result_x
%assign %%crd_y result_y
%assign %%crd_z result_z
%assign %%cro_x %%cro_x + FixedMul(%%crd_x, CastEpsilon2)
%assign %%cro_y %%cro_y + FixedMul(%%crd_y, CastEpsilon2)
%assign %%cro_z %%cro_z + FixedMul(%%crd_z, CastEpsilon2)
%endif
%endrep
SkyColor %%crd_x, %%crd_y, %%crd_z
%assign result_r FixedMul(result_r, %%mask_r)
%assign result_g FixedMul(result_g, %%mask_g)
%assign result_b FixedMul(result_b, %%mask_b)
Saturate result_r, result_g, result_b
%endmacro
;==============================================================================
; The pixels of the scene
;==============================================================================
BitmapStart:
%ifndef No_Bitmap
%assign y LineStart
%rep LineCount
%assign x RowStart
%rep RowCount
%assign ro_x CamPos_x
%assign ro_y CamPos_y
%assign ro_z CamPos_z
%assign iu (x * 2 - Image_Width)
%assign iv (y * 2 - Image_Height)
%assign rd_x FixedDiv(iu, Image_Height)
%assign rd_y FixedDiv(iv, Image_Height)
%assign rd_z FixedDiv(-175, 100)
Normalize rd_x, rd_y, rd_z
%assign rd_x result_x
%assign rd_y result_y
%assign rd_z result_z
RenderScene ro_x, ro_y, ro_z, rd_x, rd_y, rd_z
db FixedToInt(result_b * 255)
db FixedToInt(result_g * 255)
db FixedToInt(result_r * 255)
%assign x x+1
%endrep
times 3 - ($ - BitmapStart - 1) % 4 db 0
%assign y y+1
%endrep
%endif ; No_Bitmap
EOF: