-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathraytrace.c
More file actions
1840 lines (1544 loc) · 44.1 KB
/
raytrace.c
File metadata and controls
1840 lines (1544 loc) · 44.1 KB
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
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Majuma's Raytracer MRT 1.1
Coded by Mikolaj 'Majuma' Feliks
Mail : majuma@wp.pl
WWW : http://www.majuma.xt.pl/
Modified : Tue Sep 13 13:00:50 2005
Tab spacing : 4
Compilation
-----------
BCC 5.5 : bcc32 -Lpath_to_libraries -Ipath_to_includes mrt.c
GCC 3.4.3 : gcc -lm -o mrt mrt.c
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include <time.h>
#include "low.h"
#include "clock.h"
#include "math3d.h"
#include "pcx.h"
#include "fontnew.h"
#define BYTE unsigned char
#define WORD unsigned short int
#define DWORD unsigned long
#define VERSION "1.1"
#define C_EMPTY 0
#define C_WHITE 0
#define C_RED 32
#define C_GREEN 64
#define C_BLUE 96
#define C_YELLOW 128
#define C_CYAN 160
#define C_MAGENTA 192
#define C_BLUE2 224
#define MAX_LINES 30
#define MAX_LINE_LEN 256
#define MAX_NAME_LEN 16
#define MAX_NUM_LEN 16
#define MAX_FILENAME_LEN 200
#define MAX_LABEL_LEN 10
#define MAX_VAL_LEN 64
#define MAX_DIST 60.0
#define ANGLE_OF_VIEW 70.0
#define ERR_OK 0
#define ERR_NOFILE -1
#define ERR_COMMENT -2
#define ERR_CHARS -4
#define ERR_COMMENT2 -5
#define ERR_EMPTY -6
#define ERR_NAMECHAR -7
#define ERR_NAMELEN -8
#define ERR_NONAME -9
#define ERR_BADNAME -10
#define ERR_NUMLEN -11
#define ERR_NUM -12
#define ERR_LINEEND -13
#define ERR_NUMARGS -14
#define ERR_CAMERA -15
#define ERR_LIGHT -16
#define ERR_SCRMEM -17
#define ERR_SPHMEM -18
#define ERR_PLNMEM -19
#define ERR_RES -20
#define ERR_SYNTAX -21
#define ERR_WRITE -22
#define ERR_RADIUS -23
#define ERR_COLOR -24
#define ERR_NOCAMERA -25
#define ERR_NOLIGHT -26
#define ERR_NOOBJ -27
#define ERR_CYLMEM -28
#define ERR_NEGSCALE -29
#define ERR_LABELLEN -30
#define ERR_VALLEN -31
#define ERR_LABEL -32
#define ERR_LABELDBL -33
#define ERR_NOLABEL -34
#define PAL_OK 0
#define PAL_FILE -1
#define PAL_NOTNUM -2
#define PAL_NUMLEN -3
#define PAL_LINELEN -4
#define PAL_NUMNUM -5
#define PAL_RANGE -6
#define PAL_LINES -7
#define ID_NOTHING 0
#define ID_CAMERA 1
#define ID_LIGHT 2
#define ID_SPHERE 3
#define ID_PLANE 4
#define ID_CYLINDER 5
#define CYL_XY 1
#define CYL_XZ 2
#define CYL_YZ 3
#define RES_320X200 0
#define RES_320X240 1
#define RES_400X300 2
#define RES_640X480 3
#define RES_800X600 4
#define RES_1024X768 5
#define RES_1600X1200 6
typedef struct sSCREEN
{
BYTE* Buffer;
BYTE* Palette;
WORD Width;
WORD Height;
} SCREEN;
typedef struct tSPHERE
{
float x0, y0, z0;
float R;
BYTE color;
struct tSPHERE *prev, *next;
} SPHERE;
typedef struct tPLANE
{
float x0, y0, z0;
float A, B, C;
BYTE color1, color2;
VECTOR tex_x, tex_y;
float tex_scale;
struct tPLANE *prev, *next;
} PLANE;
typedef struct tCYLINDER
{
float m, n;
float R;
BYTE color;
BYTE type;
struct tCYLINDER *prev, *next;
} CYLINDER;
typedef struct tCAMERA
{
VECTOR pos;
VECTOR look_at;
SCREEN scr;
float w_div_h;
float persp;
float fov;
float rot;
} CAMERA;
typedef struct tLIGHT
{
VECTOR pos;
} LIGHT;
typedef struct tWORLD
{
CAMERA cam;
LIGHT light;
SPHERE* sphere_tab;
PLANE* plane_tab;
CYLINDER* cylinder_tab;
int n_spheres;
int n_planes;
int n_cylinders;
char file[MAX_FILENAME_LEN];
} WORLD;
int solve_quadratic_equation(float a, float b, float c, float mind,
float maxd, float* dist)
{
float t, dsqr, da, db;
float delta = b * b - 4 * a * c;
if (delta < 0) {
return 0;
}
if (delta == 0) {
t = -b / 2 * a;
}
else {
dsqr = sqrt(delta);
t = 0.5 / a;
da = (-b - dsqr) * t;
db = (-b + dsqr) * t;
if (da < db) {
t = da;
}
else {
t = db;
}
}
if (t < mind || t > maxd) {
return 0;
}
*dist = t;
return 1;
}
int LoadPalette(SCREEN* s, char* PalFile)
{
FILE* p;
char line[32], c;
int eof = 0;
int curr_char = 0, curr_line = 0;
int i, j, n_num, num_char = 0;
char num[4], cc;
int ln;
int color;
BYTE* pal_ptr = s->Palette;
p = fopen(PalFile, "r");
if (!p) {
return PAL_FILE;
}
while (!eof) {
c = fgetc(p);
eof = feof(p);
if (c == 0x0a || eof) {
if (!curr_char)
continue;
line[curr_char] = 0;
curr_char = 0;
ln = strlen(line);
num_char = 0;
n_num = 0;
for (i = 0; i < ln; i++) {
cc = line[i];
if (cc == ',' || i == (ln - 1)) {
if (i == (ln - 1)) {
num[num_char++] = cc;
if (num_char == 3) {
fclose(p);
return PAL_NUMLEN;
}
}
num[num_char] = 0;
num_char = 0;
for (j = 0; j < strlen(num); j++) {
if (num[j] < '0' || num[j] > '9') {
fclose(p);
return PAL_NOTNUM;
}
}
color = atoi(num);
if (color < 0 || color > 63) {
fclose(p);
return PAL_RANGE;
}
*pal_ptr = (BYTE)color;
pal_ptr++;
n_num++;
if (n_num > 3) {
fclose(p);
return PAL_NUMNUM;
}
} else {
num[num_char++] = cc;
if (num_char == 3) {
fclose(p);
return PAL_NUMLEN;
}
}
}
if ((++curr_line) == 256)
break;
} else if (c >= '!') {
line[curr_char++] = c;
if (curr_char == 32) {
fclose(p);
return PAL_LINELEN;
}
}
}
fclose(p);
if (curr_line != 256) {
return PAL_LINES;
}
return PAL_OK;
}
//------------------------------------------------------------
DWORD SetScreen(SCREEN* s, int w, int h)
{
DWORD size = w * h;
s->Width = w;
s->Height = h;
s->Buffer = (BYTE*)malloc(size);
if (!s->Buffer)
return 0;
s->Palette = (BYTE*)malloc(768);
if (!s->Palette) {
free(s->Buffer);
return 0;
}
return (size + 768);
}
//------------------------------------------------------------
void FreeScreen(SCREEN* s)
{
free(s->Buffer);
free(s->Palette);
}
//------------------------------------------------------------
/*
void ClearScreen(SCREEN *s, BYTE c) {
memset(s->Buffer, c, s->Width * s->Height);
}
*/
//------------------------------------------------------------
SPHERE* add_sphere(WORLD* w)
{
SPHERE *s, *ns;
if (!w->n_spheres) {
w->sphere_tab = (SPHERE*)malloc(sizeof(SPHERE));
if (!w->sphere_tab)
return (SPHERE*)0;
w->sphere_tab->prev = w->sphere_tab->next = (SPHERE*)0;
ns = w->sphere_tab;
} else {
s = w->sphere_tab;
while (s->next)
s = s->next;
s->next = (SPHERE*)malloc(sizeof(SPHERE));
if (!s->next)
return (SPHERE*)0;
ns = s->next;
ns->prev = s;
ns->next = (SPHERE*)0;
}
w->n_spheres++;
return ns;
}
PLANE* add_plane(WORLD* w)
{
PLANE *p, *np;
if (!w->n_planes) {
w->plane_tab = (PLANE*)malloc(sizeof(PLANE));
if (!w->plane_tab)
return (PLANE*)0;
w->plane_tab->prev = w->plane_tab->next = (PLANE*)0;
np = w->plane_tab;
} else {
p = w->plane_tab;
while (p->next)
p = p->next;
p->next = (PLANE*)malloc(sizeof(PLANE));
if (!p->next)
return (PLANE*)0;
np = p->next;
np->prev = p;
np->next = (PLANE*)0;
}
w->n_planes++;
return np;
}
CYLINDER* add_cylinder(WORLD* w)
{
CYLINDER *c, *nc;
if (!w->n_cylinders) {
w->cylinder_tab = (CYLINDER*)malloc(sizeof(CYLINDER));
if (!w->cylinder_tab)
return (CYLINDER*)0;
w->cylinder_tab->prev = w->cylinder_tab->next = (CYLINDER*)0;
nc = w->cylinder_tab;
} else {
c = w->cylinder_tab;
while (c->next)
c = c->next;
c->next = (CYLINDER*)malloc(sizeof(CYLINDER));
if (!c->next)
return (CYLINDER*)0;
nc = c->next;
nc->prev = c;
nc->next = (CYLINDER*)0;
}
w->n_cylinders++;
return nc;
}
//------------------------------------------------------------
int set_world(WORLD* w, int scr_w, int scr_h, float fov,
DWORD* allocated_mem)
{
int j;
BYTE* p;
if (!(*allocated_mem = SetScreen(&w->cam.scr, scr_w, scr_h))) {
return ERR_SCRMEM;
}
w->sphere_tab = (SPHERE*)0;
w->n_spheres = 0;
w->plane_tab = (PLANE*)0;
w->n_planes = 0;
w->cylinder_tab = (CYLINDER*)0;
w->n_cylinders = 0;
p = w->cam.scr.Palette;
// COLOR 1 (WHITE)
for (j = 0; j < 32; j++, p += 3) {
*(p + 0) = j << 1;
*(p + 1) = j << 1;
*(p + 2) = j << 1;
}
// COLOR 2 (RED)
for (j = 0; j < 32; j++, p += 3) {
*(p + 0) = j << 1;
*(p + 1) = 0;
*(p + 2) = 0;
}
// COLOR 3 (GREEN)
for (j = 0; j < 32; j++, p += 3) {
*(p + 0) = 0;
*(p + 1) = j << 1;
*(p + 2) = 0;
}
// COLOR 4 (BLUE)
for (j = 0; j < 32; j++, p += 3) {
*(p + 0) = 0;
*(p + 1) = 0;
*(p + 2) = j << 1;
}
// COLOR 5 (YELLOW)
for (j = 0; j < 32; j++, p += 3) {
*(p + 0) = j << 1;
*(p + 1) = j << 1;
*(p + 2) = 0;
}
// COLOR 6 (CYAN)
for (j = 0; j < 32; j++, p += 3) {
*(p + 0) = 0;
*(p + 1) = j << 1;
*(p + 2) = j << 1;
}
// COLOR 7 (MAGENTA)
for (j = 0; j < 32; j++, p += 3) {
*(p + 0) = j << 1;
*(p + 1) = 0;
*(p + 2) = j << 1;
}
// COLOR 8 (BLUE 2)
for (j = 0; j < 32; j++, p += 3) {
*(p + 0) = 0;
*(p + 1) = j;
*(p + 2) = j << 1;
}
w->cam.fov = fov;
w->cam.w_div_h = (float)scr_w / (float)scr_h;
w->cam.persp = w->cam.w_div_h / (2.0 * tan(w->cam.fov / 2.0 * M_PI / 180.0));
return ERR_OK;
}
//------------------------------------------------------------
void free_world(WORLD* w)
{
SPHERE* s;
PLANE* p;
CYLINDER* c;
s = w->sphere_tab;
if (s) {
while (s->next)
s = s->next;
s = s->prev;
while (s) {
free(s->next);
s = s->prev;
}
free(w->sphere_tab);
}
p = w->plane_tab;
if (p) {
while (p->next)
p = p->next;
p = p->prev;
while (p) {
free(p->next);
p = p->prev;
}
free(w->plane_tab);
}
c = w->cylinder_tab;
if (c) {
while (c->next)
c = c->next;
c = c->prev;
while (c) {
free(c->next);
c = c->prev;
}
free(w->cylinder_tab);
}
FreeScreen(&w->cam.scr);
}
//------------------------------------------------------------
int solve_plane(PLANE* p, VECTOR* o, VECTOR* d,
float min_dist, float max_dist, float* dist)
{
float alfa = o->x - p->x0;
float beta = o->y - p->y0;
float gamma = o->z - p->z0;
float t;
t = -((p->A * alfa + p->B * beta + p->C * gamma) / (p->A * d->x + p->B * d->y + p->C * d->z));
if ((t < min_dist) || (t > max_dist))
return 0;
*dist = t;
return 1;
}
int solve_sphere(SPHERE* s, VECTOR* o, VECTOR* d, float min_dist,
float max_dist, float* dist)
{
float i = o->x - s->x0;
float j = o->y - s->y0;
float k = o->z - s->z0;
float a = d->x * d->x + d->y * d->y + d->z * d->z;
float b = 2.0 * (i * d->x + j * d->y + k * d->z);
float c = i * i + j * j + k * k - s->R * s->R;
return solve_quadratic_equation(a, b, c, min_dist, max_dist, dist);
}
int solve_cylinder(CYLINDER* cyl, VECTOR* o, VECTOR* d, float min_dist,
float max_dist, float* dist)
{
float a, b, c;
float alfa = -cyl->m;
float beta = -cyl->n;
switch (cyl->type) {
case CYL_XY:
alfa += o->x;
beta += o->y;
a = d->x * d->x + d->y * d->y;
b = 2.0 * (alfa * d->x + beta * d->y);
break;
case CYL_XZ:
alfa += o->x;
beta += o->z;
a = d->x * d->x + d->z * d->z;
b = 2.0 * (alfa * d->x + beta * d->z);
break;
case CYL_YZ:
alfa += o->y;
beta += o->z;
a = d->y * d->y + d->z * d->z;
b = 2.0 * (alfa * d->y + beta * d->z);
}
c = alfa * alfa + beta * beta - cyl->R * cyl->R;
return solve_quadratic_equation(a, b, c, min_dist, max_dist, dist);
}
//------------------------------------------------------------
BYTE shoot_ray(WORLD* w, VECTOR* ro, VECTOR* rd)
{
float curr_dist = MAX_DIST, dist;
SPHERE* s;
PLANE* p;
CYLINDER* c;
VECTOR hit;
float t;
SPHERE* hit_s;
PLANE* hit_p;
CYLINDER* hit_c;
int found = ID_NOTHING, found_shadow = 0;
BYTE color;
VECTOR normal, to_light;
int tx, ty;
int r;
float shadow_mul, v_len;
VECTOR h, rx, ry;
float dot_val;
float fade_color;
int icolor;
// search for spheres
s = w->sphere_tab;
while (s) {
r = solve_sphere(s, ro, rd, 0.0, MAX_DIST, &dist);
if (r && (dist < curr_dist)) {
curr_dist = dist;
hit_s = s;
found = ID_SPHERE;
}
s = s->next;
}
// search for planes
p = w->plane_tab;
while (p) {
r = solve_plane(p, ro, rd, 0.0, MAX_DIST, &dist);
if (r && (dist < curr_dist)) {
curr_dist = dist;
hit_p = p;
found = ID_PLANE;
}
p = p->next;
}
// search for cylinders
c = w->cylinder_tab;
while (c) {
r = solve_cylinder(c, ro, rd, 0.0, MAX_DIST, &dist);
if (r && (dist < curr_dist)) {
curr_dist = dist;
hit_c = c;
found = ID_CYLINDER;
}
c = c->next;
}
if (found != ID_NOTHING) {
hit.x = ro->x + rd->x * curr_dist;
hit.y = ro->y + rd->y * curr_dist;
hit.z = ro->z + rd->z * curr_dist;
if (found == ID_SPHERE) {
normal.x = hit.x - hit_s->x0;
normal.y = hit.y - hit_s->y0;
normal.z = hit.z - hit_s->z0;
} else if (found == ID_PLANE) {
normal.x = hit_p->A;
normal.y = hit_p->B;
normal.z = hit_p->C;
} else /*if (found == ID_CYLINDER)*/ {
switch (hit_c->type) {
case CYL_XY:
normal.x = hit.x - hit_c->m;
normal.y = hit.y - hit_c->n;
normal.z = 0.0;
break;
case CYL_XZ:
normal.x = hit.x - hit_c->m;
normal.y = 0.0;
normal.z = hit.z - hit_c->n;
break;
default: //case CYL_YZ:
normal.x = 0.0;
normal.y = hit.y - hit_c->m;
normal.z = hit.z - hit_c->n;
}
}
if (found != ID_PLANE) {
t = 1.0 / vector_len(&normal);
normal.x *= t;
normal.y *= t;
normal.z *= t;
}
to_light.x = w->light.pos.x - hit.x;
to_light.y = w->light.pos.y - hit.y;
to_light.z = w->light.pos.z - hit.z;
t = 1.0 / (v_len = vector_len(&to_light));
to_light.x *= t;
to_light.y *= t;
to_light.z *= t;
t = dot_product(&normal, &to_light);
if (t < 0.0)
color = C_EMPTY;
else {
//check for shadow
s = w->sphere_tab;
while (s) {
if (found == ID_SPHERE && s == hit_s) {
s = s->next;
continue;
}
if (solve_sphere(s, &hit, &to_light, 0.0, v_len, &dist)) {
found_shadow = 1;
break;
}
s = s->next;
}
if (!found_shadow) {
p = w->plane_tab;
while (p) {
if (found == ID_PLANE && p == hit_p) {
p = p->next;
continue;
}
if (solve_plane(p, &hit, &to_light, 0.0, v_len, &dist)) {
found_shadow = 1;
break;
}
p = p->next;
}
}
if (!found_shadow) {
c = w->cylinder_tab;
while (c) {
if (found == ID_CYLINDER && c == hit_c) {
c = c->next;
continue;
}
if (solve_cylinder(c, &hit, &to_light, 0.0, v_len, &dist)) {
found_shadow = 1;
break;
}
c = c->next;
}
}
if (!found_shadow)
shadow_mul = 1.0;
else
shadow_mul = 0.5;
fade_color = 31.0 / (MAX_DIST * MAX_DIST) * curr_dist * curr_dist;
icolor = (int)(t * shadow_mul * 31.0 - fade_color);
if (icolor < 0)
icolor = 0;
color = (BYTE)icolor;
}
if (found == ID_SPHERE) {
color += hit_s->color;
} else if (found == ID_PLANE) {
h.x = hit.x - hit_p->x0;
h.y = hit.y - hit_p->y0;
h.z = hit.z - hit_p->z0;
dot_val = dot_product(&h, &hit_p->tex_x);
rx.x = hit_p->tex_x.x * dot_val;
rx.y = hit_p->tex_x.y * dot_val;
rx.z = hit_p->tex_x.z * dot_val;
tx = (int)(vector_len(&rx) * hit_p->tex_scale);
dot_val = dot_product(&h, &hit_p->tex_y);
ry.x = hit_p->tex_y.x * dot_val;
ry.y = hit_p->tex_y.y * dot_val;
ry.z = hit_p->tex_y.z * dot_val;
ty = (int)(vector_len(&ry) * hit_p->tex_scale);
if (h.x < hit_p->A)
tx--;
if (h.y < hit_p->B)
ty--;
if (!(tx & 1)) {
if (!(ty & 1))
color += hit_p->color1;
else
color += hit_p->color2;
} else {
if (!(ty & 1))
color += hit_p->color2;
else
color += hit_p->color1;
}
} else /*if (found == ID_CYLINDER)*/ {
color += hit_c->color;
}
} else
color = C_EMPTY;
return color;
}
//------------------------------------------------------------
void render(WORLD* w, int v)
{
VECTOR a, b, c, new_c;
VECTOR ro, rd;
VECTOR so;
VECTOR ver1, ver2, ver3;
VECTOR vw, vh;
int wi, hi;
BYTE* scr_ptr;
BYTE color;
float t;
float sina, cosa;
long n_pixels = w->cam.scr.Width * w->cam.scr.Height;
long pix_counter = 0, pix_put_dot = n_pixels >> 6;
char out_str[MAX_FILENAME_LEN + 16];
if (v) {
printf("%s", "rendering");
}
a.x = w->cam.look_at.x - w->cam.pos.x;
a.y = w->cam.look_at.y - w->cam.pos.y;
a.z = w->cam.look_at.z - w->cam.pos.z;
t = 1.0 / vector_len(&a);
a.x *= t;
a.y *= t;
a.z *= t;
if (a.x == 0.0 && a.y == 0.0)
return;
c.x = 0.0;
c.y = 0.0;
c.z = 1.0;
cross_product(&b, &a, &c);
cross_product(&c, &a, &b);
t = 1.0 / vector_len(&b);
b.x *= t;
b.y *= t;
b.z *= t;
t = -1.0 / vector_len(&c);
c.x *= t;
c.y *= t;
c.z *= t;
sina = -sin(w->cam.rot);
cosa = cos(w->cam.rot);
new_c.x = sina * b.x + cosa * c.x;
new_c.y = sina * b.y + cosa * c.y;
new_c.z = sina * b.z + cosa * c.z;
cross_product(&b, &a, &new_c);
cross_product(&c, &a, &b);
t = 1.0 / vector_len(&b);
b.x *= t;
b.y *= t;
b.z *= t;
t = -1.0 / vector_len(&c);
c.x *= t;
c.y *= t;
c.z *= t;
so.x = w->cam.pos.x + a.x * w->cam.persp;
so.y = w->cam.pos.y + a.y * w->cam.persp;
so.z = w->cam.pos.z + a.z * w->cam.persp;
t = -w->cam.w_div_h * 0.5;
b.x *= t;
b.y *= t;
b.z *= t;
c.x *= 0.5;
c.y *= 0.5;
c.z *= 0.5;
ver1.x = b.x + c.x + so.x;
ver1.y = b.y + c.y + so.y;
ver1.z = b.z + c.z + so.z;
ver2.x = -b.x + c.x + so.x;
ver2.y = -b.y + c.y + so.y;
ver2.z = -b.z + c.z + so.z;
ver3.x = b.x - c.x + so.x;
ver3.y = b.y - c.y + so.y;
ver3.z = b.z - c.z + so.z;
vw.x = ver2.x - ver1.x;
vw.y = ver2.y - ver1.y;
vw.z = ver2.z - ver1.z;
vw.x /= w->cam.scr.Width;
vw.y /= w->cam.scr.Width;
vw.z /= w->cam.scr.Width;
vh.x = ver3.x - ver1.x;
vh.y = ver3.y - ver1.y;
vh.z = ver3.z - ver1.z;
vh.x /= w->cam.scr.Height;
vh.y /= w->cam.scr.Height;
vh.z /= w->cam.scr.Height;
scr_ptr = w->cam.scr.Buffer;
for (hi = 0; hi < w->cam.scr.Height; hi++) {
for (wi = 0; wi < w->cam.scr.Width; wi++, scr_ptr++) {
ro.x = ver1.x + vw.x * wi + vh.x * hi;
ro.y = ver1.y + vw.y * wi + vh.y * hi;
ro.z = ver1.z + vw.z * wi + vh.z * hi;
rd.x = ro.x - w->cam.pos.x;
rd.y = ro.y - w->cam.pos.y;
rd.z = ro.z - w->cam.pos.z;
t = 1.0 / vector_len(&rd);
rd.x *= t;
rd.y *= t;
rd.z *= t;
color = shoot_ray(w, &ro, &rd);
*scr_ptr = color;
if (v) {
if ((++pix_counter) == pix_put_dot) {
pix_counter = 0;
printf("%s", ".");