-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.c
2059 lines (1880 loc) · 88.5 KB
/
test.c
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
#include <math.h>
#include <SDL.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
/* Define window size */
#define W2 640 // width of screen
#define H 480 // height of screen
#define W 640 // width of _game_ screen (in SplitScreen mode, map is drawn in the remaining space)
/* Define camera height from floor and distance to ceiling */
#define EyeHeight 6
#define DuckHeight 2.5
#define HeadMargin 1
#define KneeHeight 2 /* Maximum walkable obstacle height */
/* Define factors that affect the field of vision (never >= 180 degrees though) */
#define hfov (1.0 * 0.73f*H/W)
#define vfov (1.0 * .2f)
#define TextureMapping
//#define DepthShading
#define LightMapping
#define VisibilityTracking
//#define SplitScreen
// Utility functions. Because C doesn't have templates,
// we use the slightly less safe preprocessor macros to
// implement these functions that work with multiple types.
#define min(a,b) (((a) < (b)) ? (a) : (b)) // min: Choose smaller of two values.
#define max(a,b) (((a) > (b)) ? (a) : (b)) // max: Choose bigger of two values.
#define abs(a) ((a) < 0 ? -(a) : (a))
#define clamp(a, mi,ma) min(max(a,mi),ma) // clamp: Clamp value into set range.
#define sign(v) (((v) > 0) - ((v) < 0)) // sign: Return the sign of a value (-1, 0 or 1)
#define vxs(x0,y0, x1,y1) ((x0)*(y1) - (x1)*(y0)) // vxs: Vector cross product
// Overlap: Determine whether the two number ranges overlap.
#define Overlap(a0,a1,b0,b1) (min(a0,a1) <= max(b0,b1) && min(b0,b1) <= max(a0,a1))
// IntersectBox: Determine whether two 2D-boxes intersect.
#define IntersectBox(x0,y0, x1,y1, x2,y2, x3,y3) (Overlap(x0,x1,x2,x3) && Overlap(y0,y1,y2,y3))
// PointSide: Determine which side of a line the point is on. Return value: -1, 0 or 1.
#define PointSide(px,py, x0,y0, x1,y1) sign(vxs((x1)-(x0), (y1)-(y0), (px)-(x0), (py)-(y0)))
// Intersect: Calculate the point of intersection between two lines.
#define Intersect(x1,y1, x2,y2, x3,y3, x4,y4) ((struct xy) { \
vxs(vxs(x1,y1, x2,y2), (x1)-(x2), vxs(x3,y3, x4,y4), (x3)-(x4)) / vxs((x1)-(x2), (y1)-(y2), (x3)-(x4), (y3)-(y4)), \
vxs(vxs(x1,y1, x2,y2), (y1)-(y2), vxs(x3,y3, x4,y4), (y3)-(y4)) / vxs((x1)-(x2), (y1)-(y2), (x3)-(x4), (y3)-(y4)) })
// Some hard-coded limits.
#define MaxVertices 100 // maximum number of vertices in a map
#define MaxEdges 100 // maximum number of edges in a sector
#define MaxQueue 32 // maximum number of pending portal renders
#ifdef TextureMapping
typedef int Texture[1024][1024];
struct TextureSet { Texture texture, normalmap, lightmap, lightmap_diffuseonly; };
#endif
/* Sectors: Floor and ceiling height; list of wall vertexes and neighbors */
static struct sector
{
float floor, ceil;
struct xy { float x, y; } *vertex; /* Each vertex has an x and y coordinate */
unsigned short npoints; /* How many vertexes there are */
signed char *neighbors; /* Each pair of vertexes may have a corresponding neighboring sector */
#ifdef VisibilityTracking
int visible;
#endif
#ifdef TextureMapping
struct TextureSet *floortexture, *ceiltexture, *uppertextures, *lowertextures;
#endif
} *sectors = NULL;
static unsigned NumSectors = 0;
#ifdef VisibilityTracking
#define MaxVisibleSectors 256
struct xy VisibleFloorBegins[MaxVisibleSectors][W], VisibleFloorEnds[MaxVisibleSectors][W];
char VisibleFloors[MaxVisibleSectors][W];
struct xy VisibleCeilBegins[MaxVisibleSectors][W], VisibleCeilEnds[MaxVisibleSectors][W];
char VisibleCeils[MaxVisibleSectors][W];
unsigned NumVisibleSectors=0;
#endif
/* Player: location */
static struct player
{
struct xyz { float x,y,z; } where, /* Current position */
velocity; /* Current motion vector */
float angle, anglesin, anglecos, yaw; /* Looking towards (and sin() and cos() thereof) */
unsigned char sector; /* Which sector the player is currently in */
} player;
#ifdef LightMapping
static struct light
{
struct xyz where, light;
unsigned char sector;
}* lights = NULL;
static unsigned NumLights = 0;
#endif
static SDL_Surface* surface = NULL;
static void SaveFrame1(void)
{
return;
char Buf[512];
sprintf(Buf, "ffmpeg -an -f rawvideo -pix_fmt bgr0 -s %ux%u -r 60 -i - -aspect %u/%u -c:v h264 -crf 2 -preset fast -y file1.avi", W2,H, W2,H);
static FILE* fp = NULL;
if(!fp) { fp = /*fopen("file1.bin", "wb");*/ popen(Buf, "w"); }
fwrite(surface->pixels, W2*H, 4, fp);
fflush(fp);
}
static void SaveFrame2(void)
{
return;
//static unsigned skip=0;
//if(++skip>=3) { skip=0; } else return;
char Buf[512];
sprintf(Buf, "ffmpeg -an -f rawvideo -pix_fmt bgr0 -s %ux%u -r 60 -i - -aspect %u/%u -c:v h264 -crf 2 -preset fast -y file2.avi", W2,H, W2,H);
static FILE* fp = NULL;
if(!fp) { fp = /*fopen("file2.bin", "wb");*/ popen(Buf, "w"); }
fwrite(surface->pixels, W2*H, 4, fp);
fflush(fp);
}
static void LoadData(void)
{
FILE* fp = fopen("map.txt", "rt");
if(!fp) { perror("map.txt"); exit(1); }
char Buf[256], word[256], *ptr;
struct xy vertex[MaxVertices], *vertexptr = vertex;
float x,y,angle,number, numbers[MaxEdges];
int n, m;
while(fgets(Buf, sizeof Buf, fp))
switch(sscanf(ptr = Buf, "%32s%n", word, &n) == 1 ? word[0] : '\0')
{
case 'v': // vertex
for(sscanf(ptr += n, "%f%n", &y, &n); sscanf(ptr += n, "%f%n", &x, &n) == 1; )
{
if(vertexptr >= vertex+MaxVertices) { fprintf(stderr, "ERROR: Too many vertices, limit is %u\n", MaxVertices); exit(2); }
*vertexptr++ = (struct xy) { x, y };
}
break;
case 's': // sector
sectors = realloc(sectors, ++NumSectors * sizeof(*sectors));
struct sector* sect = §ors[NumSectors-1];
sscanf(ptr += n, "%f%f%n", §->floor,§->ceil, &n);
for(m=0; sscanf(ptr += n, "%32s%n", word, &n) == 1 && word[0] != '#'; )
{
if(m >= MaxEdges)
{ fprintf(stderr, "ERROR: Too many edges in sector %u. Limit is %u\n", NumSectors-1, MaxEdges); exit(2); }
numbers[m++] = word[0]=='x' ? -1 : strtof(word,0);
}
sect->npoints = m /= 2;
sect->neighbors = malloc( (m ) * sizeof(*sect->neighbors) );
sect->vertex = malloc( (m+1) * sizeof(*sect->vertex) );
#ifdef VisibilityTracking
sect->visible = 0;
#endif
for(n=0; n<m; ++n) sect->neighbors[n] = numbers[m + n];
for(n=0; n<m; ++n)
{
int v = numbers[n];
if(v >= vertexptr-vertex)
{ fprintf(stderr, "ERROR: Invalid vertex number %d in sector %u; only have %u\n",
v, NumSectors-1, (int)(vertexptr-vertex)); exit(2); }
sect->vertex[n+1] = vertex[v]; // TODO: bounds checking
}
sect->vertex[0] = sect->vertex[m];
break;
#ifdef LightMapping
case 'l': // light
lights = realloc(lights, ++NumLights * sizeof(*lights));
struct light* light = &lights[NumLights-1];
sscanf(ptr += n, "%f %f %f %f %f %f %f", &light->where.x, &light->where.z, &light->where.y,
&number, &light->light.x, &light->light.y, &light->light.z);
light->sector = (int)number;
break;
#endif
case 'p': // player
sscanf(ptr += n, "%f %f %f %f", &x,&y, &angle,&number);
player = (struct player) { {x,y,0},{0,0,0}, angle,0,0,0, number };
player.where.z = sectors[player.sector].floor + EyeHeight;
player.anglesin = sinf(player.angle);
player.anglecos = cosf(player.angle);
}
fclose(fp);
}
static void UnloadData(void)
{
for(unsigned a=0; a<NumSectors; ++a)
free(sectors[a].vertex), free(sectors[a].neighbors);
free(sectors);
sectors = NULL;
NumSectors = 0;
}
static int IntersectLineSegments(float x0,float y0, float x1,float y1,
float x2,float y2, float x3,float y3)
{
return IntersectBox(x0,y0,x1,y1, x2,y2,x3,y3)
&& abs(PointSide(x2,y2, x0,y0,x1,y1) + PointSide(x3,y3, x0,y0,x1,y1)) != 2
&& abs(PointSide(x0,y0, x2,y2,x3,y3) + PointSide(x1,y1, x2,y2,x3,y3)) != 2;
}
struct Scaler { int result, bop, fd, ca, cache; };
#define Scaler_Init(a,b,c,d,f) \
{ d + (b-1 - a) * (f-d) / (c-a), ((f<d) ^ (c<a)) ? -1 : 1, \
abs(f-d), abs(c-a), (int)((b-1-a) * abs(f-d)) % abs(c-a) }
// Scaler_Next: Return (b++ - a) * (f-d) / (c-a) + d using the initial values passed to Scaler_Init().
static int Scaler_Next(struct Scaler* i)
{
for(i->cache += i->fd; i->cache >= i->ca; i->cache -= i->ca) i->result += i->bop;
return i->result;
}
#ifdef TextureMapping
# include <sys/mman.h>
# include <unistd.h>
# include <fcntl.h>
# include <sys/stat.h>
# include <errno.h>
static int LoadTexture(void)
{
int initialized = 0;
int fd = open("portrend_textures.bin", O_RDWR | O_CREAT, 0644);
if(lseek(fd, 0, SEEK_END) == 0)
{
InitializeTextures:;
// Initialize by loading textures
#define LoadTexture(filename, name) \
Texture* name = NULL; do { \
FILE* fp = fopen(filename, "rb"); \
if(!fp) perror(filename); else { \
name = malloc(sizeof(*name)); \
fseek(fp, 0x11, SEEK_SET); \
for(unsigned y=0; y<1024; ++y) \
for(unsigned x=0; x<1024; ++x) \
{ \
int r = fgetc(fp), g = fgetc(fp), b = fgetc(fp); \
(*name)[x][y] = r*65536 + g*256 + b; \
} \
fclose(fp); } \
} while(0)
#define UnloadTexture(name) free(name)
Texture dummylightmap;
memset(&dummylightmap, 0, sizeof(dummylightmap));
LoadTexture("wall2.ppm", WallTexture); LoadTexture("wall2_norm.ppm", WallNormal);
LoadTexture("wall3.ppm", WallTexture2); LoadTexture("wall3_norm.ppm", WallNormal2);
LoadTexture("floor2.ppm", FloorTexture); LoadTexture("floor2_norm.ppm", FloorNormal);
LoadTexture("ceil2.ppm", CeilTexture); LoadTexture("ceil2_norm.ppm", CeilNormal);
#define SafeWrite(fd, buf, amount) do { \
const char* source = (const char*)(buf); \
long remain = (amount); \
while(remain > 0) { \
long result = write(fd, source, remain); \
if(result >= 0) { remain -= result; source += result; } \
else if(errno == EAGAIN || errno == EINTR) continue; \
else break; \
} \
if(remain > 0) perror("write"); \
} while(0)
#define PutTextureSet(txtname, normname) do { \
SafeWrite(fd, txtname, sizeof(Texture)); \
SafeWrite(fd, normname, sizeof(Texture)); \
SafeWrite(fd, &dummylightmap, sizeof(Texture)); \
SafeWrite(fd, &dummylightmap, sizeof(Texture)); } while(0)
printf("Initializing textures... ");
lseek(fd, 0, SEEK_SET);
for(unsigned n=0; n<NumSectors; ++n)
{
for(int s=printf("%d/%d", n+1,NumSectors); s--; ) putchar('\b');
fflush(stdout);
PutTextureSet(FloorTexture, FloorNormal);
PutTextureSet(CeilTexture, CeilNormal);
for(unsigned w=0; w<sectors[n].npoints; ++w) PutTextureSet(WallTexture, WallNormal);
for(unsigned w=0; w<sectors[n].npoints; ++w) PutTextureSet(WallTexture2, WallNormal2);
}
ftruncate(fd, lseek(fd, 0, SEEK_CUR));
printf("\n"); fflush(stdout);
UnloadTexture(WallTexture); UnloadTexture(WallNormal);
UnloadTexture(WallTexture2); UnloadTexture(WallNormal2);
UnloadTexture(FloorTexture); UnloadTexture(FloorNormal);
UnloadTexture(CeilTexture); UnloadTexture(CeilNormal);
#undef UnloadTexture
#undef LoadTexture
initialized = 1;
}
off_t filesize = lseek(fd, 0, SEEK_END);
char* texturedata = mmap(NULL, filesize, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
if(!texturedata) perror("mmap");
printf("Loading textures\n");
off_t pos = 0;
for(unsigned n=0; n<NumSectors; ++n)
{
sectors[n].floortexture = (void*) (texturedata + pos); pos += sizeof(struct TextureSet);
sectors[n].ceiltexture = (void*) (texturedata + pos); pos += sizeof(struct TextureSet);
unsigned w = sectors[n].npoints;
sectors[n].uppertextures = (void*) (texturedata + pos); pos += sizeof(struct TextureSet) * w;
sectors[n].lowertextures = (void*) (texturedata + pos); pos += sizeof(struct TextureSet) * w;
}
printf("done, %llu bytes mmapped out of %llu\n", (unsigned long long)pos, (unsigned long long) filesize);
if(pos != filesize)
{
printf(" -- Wrong filesize! Let's try that again.\n");
munmap(texturedata, filesize);
goto InitializeTextures;
}
return initialized;
}
#ifdef LightMapping
#define vlen(x,y,z) sqrtf((x)*(x) + (y)*(y) + (z)*(z))
#define vlen2(x0,y0,z0, x1,y1,z1) vlen((x1)-(x0), (y1)-(y0), (z1)-(z0))
#define vdot3(x0,y0,z0, x1,y1,z1) ((x0)*(x1) + (y0)*(y1) + (z0)*(z1))
#define vxs3(x0,y0,z0, x1,y1,z1) (struct xyz){ vxs(y0,z0, y1,z1), vxs(z0,x0, z1,x1), vxs(x0,y0, x1,y1) }
struct Intersection
{
// Map coordinates where the hit happened. x,z = map, y = height
struct xyz where;
// Information about the surface that was hit
struct TextureSet* surface;
struct xyz normal; // Perturbed surface normal
int sample; // RGB sample from surface texture & lightmap
int sectorno;
};
static int ClampWithDesaturation(int r,int g,int b)
{
int luma = r*299 + g*587 + b*114;
if(luma > 255000) { r=g=b=255; }
else if(luma <= 0) { r=g=b=0; }
else
{
double sat = 1000;
if(r > 255) sat = min(sat, (luma-255e3) / (luma-r)); else if(r < 0) sat = min(sat, luma / (double)(luma-r));
if(g > 255) sat = min(sat, (luma-255e3) / (luma-g)); else if(g < 0) sat = min(sat, luma / (double)(luma-g));
if(b > 255) sat = min(sat, (luma-255e3) / (luma-b)); else if(b < 0) sat = min(sat, luma / (double)(luma-b));
if(sat != 1.)
{
r = (r - luma) * sat/1e3 + luma; r = clamp(r,0,255);
g = (g - luma) * sat/1e3 + luma; g = clamp(g,0,255);
b = (b - luma) * sat/1e3 + luma; b = clamp(b,0,255);
}
}
return r*65536 + g*256 + b;
}
static int ApplyLight(int texture, int light)
{
int tr = (texture >>16) & 0xFF;
int tg = (texture >> 8) & 0xFF;
int tb = (texture >> 0) & 0xFF;
int lr = ((light >>16) & 0xFF);
int lg = ((light >> 8) & 0xFF);
int lb = ((light >> 0) & 0xFF);
int r = tr*lr *2 / 255;
int g = tg*lg *2 / 255;
int b = tb*lb *2 / 255;
#if 1
return ClampWithDesaturation(r,g,b);
#else
return clamp(tr*lr / 255,0,255)*65536
+ clamp(tg*lg / 255,0,255)*256
+ clamp(tb*lb / 255,0,255);
#endif
}
static void PutColor(int* target, struct xyz color)
{
*target = ClampWithDesaturation(color.x, color.y, color.z);
}
static void AddColor(int* target, struct xyz color)
{
int r = ((*target >> 16) & 0xFF) + color.x;
int g = ((*target >> 8) & 0xFF) + color.y;
int b = ((*target >> 0) & 0xFF) + color.z;
*target = ClampWithDesaturation(r, g, b);
}
static struct xyz PerturbNormal(struct xyz normal,
struct xyz tangent,
struct xyz bitangent,
int normal_sample)
{
struct xyz perturb = { ((normal_sample >> 16) & 0xFF) / 127.5f - 1.f,
((normal_sample >> 8) & 0xFF) / 127.5f - 1.f,
((normal_sample >> 0) & 0xFF) / 127.5f - 1.f};
// TODO: Verify whether this calculation is correct
return (struct xyz) { normal.x * perturb.z + bitangent.x * perturb.y + tangent.x * perturb.x,
normal.y * perturb.z + bitangent.y * perturb.y + tangent.y * perturb.x,
normal.z * perturb.z + bitangent.z * perturb.y + tangent.z * perturb.x };
}
static void GetSectorBoundingBox(int sectorno, struct xy* bounding_min, struct xy* bounding_max)
{
const struct sector* sect = §ors[sectorno];
for(int s = 0; s < sect->npoints; ++s)
{
bounding_min->x = min(bounding_min->x, sect->vertex[s].x);
bounding_min->y = min(bounding_min->y, sect->vertex[s].y);
bounding_max->x = max(bounding_max->x, sect->vertex[s].x);
bounding_max->y = max(bounding_max->y, sect->vertex[s].y);
}
}
// Return values:
// 0 = clear path, nothing hit
// 1 = hit, *result indicates where it hit
// 2 = your princess is in another castle (a direct path doesn't lead to this sector)
static int IntersectRay(struct xyz origin, int origin_sectorno,
struct xyz target, int target_sectorno,
struct Intersection* result)
{
unsigned n_rescan=0;
int prev_sectorno=-1;
rescan:;
++n_rescan;
struct sector* sect = §ors[origin_sectorno];
/*printf("Intersect: Now in sector %d at %.3f %.3f %.3f, going towards sector %d at %.3f %.3f %.3f\n",
origin_sectorno,origin.x,origin.y,origin.z,
target_sectorno,target.x,target.y,target.z);*/
// Check if this beam hits one of the sector's edges.
unsigned u=0, v=0, lu=0, lv=0;
struct xyz tangent, bitangent;
for(int s = 0; s < sect->npoints; ++s)
{
float vx1 = sect->vertex[s+0].x, vy1 = sect->vertex[s+0].y;
float vx2 = sect->vertex[s+1].x, vy2 = sect->vertex[s+1].y;
if(!IntersectLineSegments(origin.x,origin.z, target.x,target.z, vx1,vy1, vx2,vy2)/*
|| PointSide(target.x,target.z, vx1,vy1, vx2,vy2) >= 0*/)
continue;
// Determine the X & Z coordinates of the wall hit.
struct xy hit = Intersect(origin.x,origin.z, target.x,target.z, vx1,vy1, vx2,vy2);
float x = hit.x;
float z = hit.y;
// Also determine the Y coordinate.
float y = origin.y + ((abs(target.x-origin.x) > abs(target.z-origin.z))
? ((x - origin.x) * (target.y - origin.y) / (target.x - origin.x))
: ((z - origin.z) * (target.y - origin.y) / (target.z - origin.z)) );
/*fprintf(stderr, "(%.2f, %.2f, %.2f) - (%.2f, %.2f, %.2f) versus (%.2f, %.2f) - (%.2f, %.2f) intersected at (%.2f, %.2f, %.2f)\n",
origin.x,origin.y,origin.z,
target.x,target.y,target.z,
vx1,vy1, vx2,vy2,
x,y,z);*/
/* Check where the hole is. */
float hole_low = 9e9, hole_high = -9e9;
if(sect->neighbors[s] >= 0)
{
hole_low = max( sect->floor, sectors[sect->neighbors[s]].floor );
hole_high = min( sect->ceil, sectors[sect->neighbors[s]].ceil );
}
if(y >= hole_low && y <= hole_high)
{
// The point fit in between this hole.
origin_sectorno = sect->neighbors[s];
origin.x = x + (target.x - origin.x)*1e-2;
origin.y = y + (target.y - origin.y)*1e-2;
origin.z = z + (target.z - origin.z)*1e-2;
float distance = vlen(target.x-origin.x, target.y-origin.y, target.z-origin.z);
if(origin_sectorno == prev_sectorno)
{
// Disregard this boundary.
continue;
}
if(distance < 1e-3f || origin_sectorno == prev_sectorno)
{
// Close enough.
goto close_enough;
}
prev_sectorno = origin_sectorno;
goto rescan;
}
// It hit the wall.
// Did it hit the sector's floor first?
if(y < sect->floor) goto hit_floor;
if(y > sect->ceil) goto hit_ceil;
// Nope. It hit the wall.
result->where = (struct xyz) { x,y,z };
result->surface = (y < hole_low) ? §->lowertextures[s] : §->uppertextures[s];
result->sectorno = origin_sectorno;
/*printf(" Thus hit a wall\n");*/
float nx = vy2-vy1, nz = vx1-vx2, len = sqrtf(nx*nx + nz*nz);
result->normal = (struct xyz){ nx/len, 0, nz/len };
nx = vx2-vx1; nz = vy2-vy1; len = sqrtf(nx*nx + nz*nz);
tangent = (struct xyz){ nx/len, 0, nz/len };
bitangent = (struct xyz) { 0,1,0};
// Calculate the texture coordinates.
float dx = vx2-vx1;
float dy = vy2-vy1;
v = (unsigned)((y - sect->floor) * 1024.f / (sect->ceil - sect->floor)) % 1024u;
u = (abs(dx) > abs(dy) ? (unsigned)((x - vx1) * 1024 / dx)
: (unsigned)((z - vy1) * 1024 / dy)) % 1024u;
// Lightmap coordinates are the same as texture coordinates.
lu = u;
lv = v;
perturb_normal:;
int texture_sample = result->surface->texture[v][u];
int normal_sample = result->surface->normalmap[v][u];
int light_sample = result->surface->lightmap[lv][lu];
result->sample = ApplyLight(texture_sample, light_sample);
result->normal = PerturbNormal(result->normal, tangent, bitangent, normal_sample);
return 1;
}
if(target.y > sect->ceil)
{
hit_ceil:
result->where.y = sect->ceil;
result->surface = sect->ceiltexture;
result->normal = (struct xyz){0,-1,0};
tangent = (struct xyz){1,0,0};
bitangent = vxs3(result->normal.x,result->normal.y,result->normal.z, tangent.x,tangent.y,tangent.z);
hit_ceil_or_floor:
// Either the floor or ceiling was hit. Determine the X & Z coordinates.
result->where.x = (result->where.y - origin.y) * (target.x - origin.x) / (target.y - origin.y) + origin.x;
result->where.z = (result->where.y - origin.y) * (target.z - origin.z) / (target.y - origin.y) + origin.z;
/*printf(" Hit the ceiling or floor at %.3f, %.3f, %.3f\n", result->where.x, result->where.y, result->where.z);*/
// Calculate the texture coordinates.
u = ((unsigned)(result->where.x * 256)) % 1024u;
v = ((unsigned)(result->where.z * 256)) % 1024u;
// Calculate the lightmap coordinates.
struct xy bounding_min = {1e9f, 1e9f}, bounding_max = {-1e9f, -1e9f};
GetSectorBoundingBox(origin_sectorno, &bounding_min, &bounding_max);
lu = ((unsigned)((result->where.x - bounding_min.x) * 1024 / (bounding_max.x - bounding_min.x))) % 1024;
lv = ((unsigned)((result->where.y - bounding_min.y) * 1024 / (bounding_max.y - bounding_min.y))) % 1024;
goto perturb_normal;
}
if(target.y < sect->floor)
{
hit_floor:
result->where.y = sect->floor;
result->surface = sect->floortexture;
result->normal = (struct xyz){0, 1,0};
tangent = (struct xyz){-1,0,0};
bitangent = vxs3(result->normal.x,result->normal.y,result->normal.z, tangent.x,tangent.y,tangent.z);
goto hit_ceil_or_floor;
}
close_enough:;
/*printf(" Hit nothing. Sector %s.\n", origin_sectorno == target_sectorno ? "match" : "doesn't match");*/
// Is the target in this sector?
return origin_sectorno == target_sectorno ? 0 : 2;
}
#define narealightcomponents 32 //512;//64
#define area_light_radius 0.4
#define nrandomvectors 128 // 8192
#define firstround 1
#define maxrounds 100
#define fade_distance_diffuse 10.0
#define fade_distance_radiosity 10.0
#define radiomul 1.0
static struct xyz tvec[nrandomvectors];
static struct xyz avec[narealightcomponents];
static void DiffuseLightCalculation(struct xyz normal, struct xyz tangent, struct xyz bitangent,
struct TextureSet* texture,
unsigned tx, unsigned ty,
unsigned lx, unsigned ly,
struct xyz point_in_wall, unsigned sectorno)
{
struct xyz perturbed_normal = PerturbNormal(normal,tangent,bitangent,
texture->normalmap[tx][ty]);
// For each lightsource, check if there is an obstacle
// in between this vertex and the lightsource. Calculate
// the ambient light levels from the fact.
// This simulates diffuse light.
struct xyz color = {0,0,0};
for(unsigned l=0; l<NumLights; ++l)
{
const struct light* light = &lights[l];
struct xyz source = { point_in_wall.x + normal.x * 1e-5f,
point_in_wall.y + normal.y * 1e-5f,
point_in_wall.z + normal.z * 1e-5f };
for(unsigned qa=0; qa<narealightcomponents; ++qa)
{
struct xyz target = { light->where.x + avec[qa].x, light->where.y + avec[qa].y, light->where.z + avec[qa].z };
struct xyz towards = { target.x-source.x, target.y-source.y, target.z-source.z };
float len = vlen(towards.x, towards.y, towards.z), invlen = 1.0f / len;
towards.x *= invlen;
towards.y *= invlen;
towards.z *= invlen;
float cosine = vdot3(perturbed_normal.x,perturbed_normal.y,perturbed_normal.z, towards.x,towards.y,towards.z);
//if(cosine > 1) fprintf(stderr, "cosine = %.3f\n", cosine);
float power = cosine / (1.f + powf(len / fade_distance_diffuse, 2.0f));
power /= (float) narealightcomponents;
if(power > 1e-7f)
{
struct Intersection i;
if(IntersectRay(source, sectorno, target, light->sector, &i) == 0)
{
color.x += light->light.x * power;
color.y += light->light.y * power;
color.z += light->light.z * power;
} } } }
PutColor(&texture->lightmap[lx][ly], color);
}
static void RadiosityCalculation(struct xyz normal, struct xyz tangent, struct xyz bitangent,
struct TextureSet* texture,
unsigned tx, unsigned ty,
unsigned lx, unsigned ly,
struct xyz point_in_wall, unsigned sectorno)
{
struct xyz perturbed_normal = PerturbNormal(normal,tangent,bitangent,
texture->normalmap[tx][ty]);
// Shoot rays to each random direction and see what it hits.
// Take the last round's light value from that location.
struct xyz source = { point_in_wall.x + normal.x * 1e-3f,
point_in_wall.y + normal.y * 1e-3f,
point_in_wall.z + normal.z * 1e-3f };
float basepower = radiomul / nrandomvectors;
// Apply the set of random vectors to this surface.
// This produces a set of vectors all pointing away
// from the wall to random directions.
struct xyz color = {0,0,0};
for(unsigned qq=0; qq<nrandomvectors; ++qq)
{
struct xyz rvec = tvec[qq];
// If the random vector points to the wrong side from the wall, flip it
if(vdot3(rvec.x, rvec.y, rvec.z, normal.x, normal.y, normal.z) < 0)
{
rvec.x = -rvec.x;
rvec.y = -rvec.y;
rvec.z = -rvec.z;
}
struct xyz target = { source.x + rvec.x * 512.f,
source.y + rvec.y * 512.f,
source.z + rvec.z * 512.f };
struct Intersection i;
if(IntersectRay(source, sectorno, target, -1 /* no particular sector */, &i) == 1) // hit something
{
float cosine = vdot3(perturbed_normal.x, i.normal.x,
perturbed_normal.y, i.normal.y,
perturbed_normal.z, i.normal.z) * basepower;
float len = vlen(i.where.x-source.x, i.where.y-source.y, i.where.z-source.z);
float power = abs(cosine) / (1.f + powf(len / fade_distance_radiosity, 2.0f));
color.x += ((i.sample >> 16) & 0xFF) * power;
color.y += ((i.sample >> 8) & 0xFF) * power;
color.z += ((i.sample >> 0) & 0xFF) * power;
} }
AddColor(&texture->lightmap[lx][ly], color);
}
static void Begin_Radiosity(struct TextureSet* set)
{
memcpy(&set->lightmap, &set->lightmap_diffuseonly, sizeof(Texture));
}
static double End_Radiosity(struct TextureSet* set, const char* label)
{
long differences = 0;
for(unsigned x=0; x<1024; ++x)
for(unsigned y=0; y<1024; ++y)
{
int old = set->lightmap_diffuseonly[x][y];
int r = (old >> 16) & 0xFF, g = (old >> 8) & 0xFF, b = (old) & 0xFF;
int new = set->lightmap[x][y];
r -= (new >> 16) & 0xFF; g -= (new >> 8) & 0xFF; b -= (new) & 0xFF;
differences += abs(r) + abs(g) + abs(b);
}
double result = differences / (double)(1024*1024);
fprintf(stderr, "Differences in %s: %g\33[K\n", label, result);
return result;
}
static void End_Diffuse(struct TextureSet* set)
{
memcpy(&set->lightmap_diffuseonly, &set->lightmap, sizeof(Texture));
}
#ifdef _OPENMP
# include <omp.h>
#define OMP_SCALER_LOOP_BEGIN(a,b,c,d,e,f) do { \
int this_thread = omp_get_thread_num(), num_threads = omp_get_num_threads(); \
int my_start = (this_thread ) * ((c)-(a)) / num_threads + (a); \
int my_end = (this_thread+1) * ((c)-(a)) / num_threads + (a); \
struct Scaler e##int = Scaler_Init(a, my_start, (c)-1, (d) * 32768, (f) * 32768); \
for(int b = my_start; b < my_end; ++b) \
{ \
float e = Scaler_Next(&e##int) / 32768.f;
#else
#define OMP_SCALER_LOOP_BEGIN(a,b,c,d,e,f) do { \
struct Scaler e##int = Scaler_Init(a, a, (c)-1, (d) * 32768, (f) * 32768); \
for(int b = (a); b < (c); ++b) \
{ \
float e = Scaler_Next(&e##int) / 32768.f;
#endif
#define OMP_SCALER_LOOP_END() \
} } while(0)
/* My lightmap calculation involves some raytracing.
* There are faster ways to do it, but this is the only way I know how to do it in software.
*/
static void BuildLightmaps(void)
{
for(unsigned round=firstround; round<=maxrounds; ++round)
{
fprintf(stderr, "Lighting calculation, round %u...\n", round);
#ifndef _OPENMP
fprintf(stderr, "Note: This would probably go faster if you enabled OpenMP in your compiler options. It's -fopenmp in GCC and Clang.\n");
#endif
// Create uniformly distributed random unit vectors
for(unsigned n=0; n<nrandomvectors; ++n)
{
double u = (rand() % 1000000) / 1e6; // 0..1
double v = (rand() % 1000000) / 1e6; // 0..1
double theta = 2*3.141592653*u;
double phi = acos(2*v-1);
tvec[n].x = cos(theta) * sin(phi);
tvec[n].y = sin(theta) * sin(phi);
tvec[n].z = cos(phi);
}
// A lightsource is represented by a spherical cloud
// of smaller lightsources around the actual lightsource.
// This achieves smooth edges for the shadows.
#define drand(m) ((rand()%1000-500)*5e-2*m)
for(unsigned qa=0; qa<narealightcomponents; ++qa)
{
double len;
do {
avec[qa] = (struct xyz){ drand(100.0), drand(100.0), drand(100.0) };
len = sqrt(avec[qa].x*avec[qa].x + avec[qa].y*avec[qa].y + avec[qa].z*avec[qa].z);
} while(len < 1e-3);
avec[qa].x *= area_light_radius/len;
avec[qa].y *= area_light_radius/len;
avec[qa].z *= area_light_radius/len;
}
#undef drand
fprintf(stderr, "Note: You can interrupt this program at any time you want. If you wish to resume\n"
" the lightmap calculation at a later date, use the --rebuild commandline option.\n"
" If you have already finished round 1 (diffuse light), and don't wish to do that\n"
" again, change the '#define firstround' value to your liking. Value 1 means\n"
" it starts from beginning, and any value from 2-100 (actual value is not important)\n"
" means to progressively improve the radiosity (cumulative). The current value is %d.\n",
firstround);
double total_differences = 0;
for(unsigned sectorno=0; sectorno<NumSectors; ++sectorno)
{
struct sector* const sect = §ors[sectorno];
const struct xy* const vert = sect->vertex;
double sector_differences = 0;
if(1) // Do ceiling and floor
{
struct xy bounding_min = {1e9f, 1e9f}, bounding_max = {-1e9f, -1e9f};
GetSectorBoundingBox(sectorno, &bounding_min, &bounding_max);
struct xyz floornormal = (struct xyz){0, 1, 0}; // floor
struct xyz floortangent = (struct xyz){1, 0, 0};
struct xyz floorbitangent = vxs3(floornormal.x,floornormal.y,floornormal.z, floortangent.x,floortangent.y,floortangent.z);
struct xyz ceilnormal = (struct xyz){0,-1, 0}; // ceiling
struct xyz ceiltangent = (struct xyz){1, 0, 0};
struct xyz ceilbitangent = vxs3(ceilnormal.x,ceilnormal.y,ceilnormal.z, ceiltangent.x,ceiltangent.y,ceiltangent.z);
fprintf(stderr, "Bounding box for sector %d/%d: %g,%g - %g,%g\n",
sectorno+1,NumSectors, bounding_min.x,bounding_min.y, bounding_max.x,bounding_max.y);
// Round 1: Check lightsources
if(round == 1)
{
struct Scaler txtx_int = Scaler_Init(0,0,1023, bounding_min.x*32768, bounding_max.x*32768);
for(unsigned x=0; x<1024; ++x)
{
fprintf(stderr, "- Sector %d ceils&floors, %u/%u diffuse light...\r", sectorno+1, x,1024);
float txtx = Scaler_Next(&txtx_int)/32768.f;
// For better cache locality, first do floors and then ceils
#pragma omp parallel
OMP_SCALER_LOOP_BEGIN(0,y,1024, bounding_min.y, txty, bounding_max.y);
DiffuseLightCalculation(floornormal, floortangent, floorbitangent, sect->floortexture,
((unsigned)(txtx*256)) % 1024, ((unsigned)(txty*256)) % 1024,
x,y,
(struct xyz){txtx, sect->floor, txty}, sectorno);
OMP_SCALER_LOOP_END();
#pragma omp parallel
OMP_SCALER_LOOP_BEGIN(0,y,1024, bounding_min.y, txty, bounding_max.y);
DiffuseLightCalculation(ceilnormal, ceiltangent, ceilbitangent, sect->ceiltexture,
((unsigned)(txtx*256)) % 1024, ((unsigned)(txty*256)) % 1024,
x,y,
(struct xyz){txtx, sect->ceil, txty}, sectorno);
OMP_SCALER_LOOP_END();
}
fprintf(stderr, "\n");
End_Diffuse(sect->floortexture);
End_Diffuse(sect->ceiltexture);
}
else
{
// Round 2+: Radiosity
Begin_Radiosity(sect->floortexture);
Begin_Radiosity(sect->ceiltexture);
// Calculate radiosity in decreased resolution
struct Scaler txtx_int = Scaler_Init(0,0,1023, bounding_min.x*32768, bounding_max.x*32768);
for(unsigned x=0; x<1024; ++x)
{
float txtx = Scaler_Next(&txtx_int)/32768.f;
fprintf(stderr, "- Sector %u ceils&floors, %u/%u radiosity...\r", sectorno+1, x,1024);
#pragma omp parallel
OMP_SCALER_LOOP_BEGIN(0,y,1024, bounding_min.y, txty, bounding_max.y);
RadiosityCalculation(floornormal, floortangent, floorbitangent, sect->ceiltexture,
((unsigned)(txtx*256)) % 1024, ((unsigned)(txty*256)) % 1024, x,y,
(struct xyz){txtx, sect->floor, txty}, sectorno);
OMP_SCALER_LOOP_END();
#pragma omp parallel
OMP_SCALER_LOOP_BEGIN(0,y,1024, bounding_min.y, txty, bounding_max.y);
RadiosityCalculation(ceilnormal, ceiltangent, ceilbitangent, sect->ceiltexture,
((unsigned)(txtx*256)) % 1024, ((unsigned)(txty*256)) % 1024, x,y,
(struct xyz){txtx, sect->ceil, txty}, sectorno);
OMP_SCALER_LOOP_END();
}
char Buf[128];
sprintf(Buf, "Sector %u floors", sectorno+1); sector_differences += End_Radiosity(sect->floortexture, Buf);
sprintf(Buf, "Sector %u ceils", sectorno+1); sector_differences += End_Radiosity(sect->ceiltexture, Buf);
}
}
if(1)for(unsigned s=0; s<sect->npoints; ++s)
{
float xd = vert[s+1].x - vert[s].x;
float zd = vert[s+1].y - vert[s].y;
float len = vlen(xd,zd,0);
struct xyz normal = {-zd/len, 0, xd/len};
struct xyz tangent = {xd/len, 0, zd/len};
struct xyz bitangent = {0,1,0};
float hole_low = 9e9, hole_high = -9e9;
if(sect->neighbors[s] >= 0)
{
hole_low = max( sect->floor, sectors[sect->neighbors[s]].floor );
hole_high = min( sect->ceil, sectors[sect->neighbors[s]].ceil );
}
if(round == 1)
{
// Round 1: Check lightsources
struct Scaler txtx_int = Scaler_Init(0,0,1023, vert[s].x*32768,vert[s+1].x*32768);
struct Scaler txtz_int = Scaler_Init(0,0,1023, vert[s].y*32768,vert[s+1].y*32768);
for(unsigned x=0; x<1024; ++x)
{
float txtx = Scaler_Next(&txtx_int)/32768.f;
float txtz = Scaler_Next(&txtz_int)/32768.f;
fprintf(stderr, "- Sector %u Wall %u/%u %u/%u diffuse light...\r", sectorno+1, s+1, sect->npoints, x,1024);
#pragma omp parallel
OMP_SCALER_LOOP_BEGIN(0,y,1024, sect->ceil, txty, sect->floor);
struct TextureSet* texture = §->uppertextures[s];
if(sect->neighbors[s] >= 0 && txty < hole_high)
{
if(txty > hole_low) continue;
texture = §->lowertextures[s];
}
struct xyz point_in_wall = { txtx, txty, txtz };
DiffuseLightCalculation(normal, tangent, bitangent, texture, x,y, x,y,
point_in_wall, sectorno);
OMP_SCALER_LOOP_END();
}
End_Diffuse(§->uppertextures[s]);
End_Diffuse(§->lowertextures[s]);
}
else
{
Begin_Radiosity(§->uppertextures[s]);
Begin_Radiosity(§->lowertextures[s]);
// Round 2+: Radiosity
struct Scaler txtx_int = Scaler_Init(0,0,1023, vert[s].x*32768,vert[s+1].x*32768);
struct Scaler txtz_int = Scaler_Init(0,0,1023, vert[s].y*32768,vert[s+1].y*32768);
for(unsigned x=0; x<1024; ++x)
{
float txtx = Scaler_Next(&txtx_int)/32768.f;
float txtz = Scaler_Next(&txtz_int)/32768.f;
fprintf(stderr, "- Sector %u Wall %u/%u %u/%u radiosity...\r", sectorno+1, s+1, sect->npoints, x,1024);
#pragma omp parallel
OMP_SCALER_LOOP_BEGIN(0,y,1024, sect->ceil, txty, sect->floor);
struct TextureSet* texture = §->uppertextures[s];
if(sect->neighbors[s] >= 0 && txty < hole_high)
{
if(txty > hole_low) continue;
texture = §->lowertextures[s];
}
struct xyz point_in_wall = { txtx, txty, txtz };
RadiosityCalculation(normal, tangent, bitangent, texture, x,y, x,y, point_in_wall, sectorno);
OMP_SCALER_LOOP_END();
}
char Buf[128];
sprintf(Buf, "Sector %u wall %u lower texture", sectorno+1, s+1); sector_differences += End_Radiosity(§->uppertextures[s], Buf);
sprintf(Buf, "Sector %u wall %u upper texture", sectorno+1, s+1); sector_differences += End_Radiosity(§->lowertextures[s], Buf);
}
fprintf(stderr, "\n");
}
fprintf(stderr, "Round %u differences in sector %u: %g\n", round, sectorno+1, sector_differences);
total_differences += sector_differences;
}
fprintf(stderr, "Round %u differences total: %g.\n", round, total_differences);
if(total_differences < 1e-6)
{
break;
}
}
}
#endif
#endif
// Helper function for the antialiased line algorithm.
#define fpart(x) ((x) < 0 ? 1 - ((x) - floorf(x)) : (x) - floorf(x))
#define rfpart(x) (1 - fpart(x))
static void plot(int x,int y, float opacity, int color)
{
opacity = powf(opacity, 1/2.2f);
int *pix = ((int*) surface->pixels) + y * W2 + x;
int r0 = (*pix >> 16) & 0xFF, r1 = (color >> 16) & 0xFF;
int g0 = (*pix >> 8) & 0xFF, g1 = (color >> 8) & 0xFF;
int b0 = (*pix >> 0) & 0xFF, b1 = (color >> 0) & 0xFF;
int r = max(r0, opacity*r1);
int g = max(g0, opacity*g1);
int b = max(b0, opacity*b1);
*pix = (r << 16) | (g << 8) | b;
}
static void line(float x0,float y0, float x1,float y1, int color)
{
// Xiaolin Wu's antialiased line algorithm from Wikipedia.
int steep = fabsf(y1-y0) > fabsf(x1-x0);
if(steep) { float tmp; tmp=x0; x0=y0; y0=tmp; tmp=x1; x1=y1; y1=tmp; }
if(x0 > x1) { float tmp; tmp=x0; x0=x1; x1=tmp; tmp=y0; y0=y1; y1=tmp; }
float dx = x1-x0, dy = y1-y0, gradient = dy/dx;
// handle first endpoint
int xend = (int)(x0 + 0.5f);
int yend = y0 + gradient * (xend - x0);
float xgap = rfpart(x0 + 0.5f);
int xpxl1 = xend; // this will be used in the main loop
int ypxl1 = (int)(yend);
if(steep)
{
plot(ypxl1, xpxl1, rfpart(yend) * xgap, color);
plot(ypxl1+1, xpxl1, fpart(yend) * xgap, color);
}
else
{
plot(xpxl1, ypxl1 , rfpart(yend) * xgap, color);
plot(xpxl1, ypxl1+1, fpart(yend) * xgap, color);
}
float intery = yend + gradient; // first y-intersection for the main loop
// handle second endpoint
xend = (int)(x1 + 0.5f);
yend = y1 + gradient * (xend - x1);
xgap = fpart(x1 + 0.5);