-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsurface_meta.c
1703 lines (1371 loc) · 45.6 KB
/
surface_meta.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
/* -------------------------------------------------------------------------------
Copyright (C) 1999-2006 Id Software, Inc. and contributors.
For a list of contributors, see the accompanying CONTRIBUTORS file.
This file is part of GtkRadiant.
GtkRadiant is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
GtkRadiant is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GtkRadiant; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
----------------------------------------------------------------------------------
This code has been altered significantly from its original form, to support
several games based on the Quake III Arena engine, in the form of "Q3Map2."
------------------------------------------------------------------------------- */
/* marker */
#define SURFACE_META_C
/* dependencies */
//#include "q3map2.h"
#include "kmap2.h" //add hypov8
#define LIGHTMAP_EXCEEDED -1
#define S_EXCEEDED -2
#define T_EXCEEDED -3
#define ST_EXCEEDED -4
#define UNSUITABLE_TRIANGLE -10
#define VERTS_EXCEEDED -1000
#define INDEXES_EXCEEDED -2000
#define GROW_META_VERTS 1024
#define GROW_META_TRIANGLES 1024
static int numMetaSurfaces, numPatchMetaSurfaces;
static int maxMetaVerts = 0;
static int numMetaVerts = 0;
static int firstSearchMetaVert = 0;
static bspDrawVert_t *metaVerts = NULL;
static int maxMetaTriangles = 0;
static int numMetaTriangles = 0;
static metaTriangle_t *metaTriangles = NULL;
/*
ClearMetaVertexes()
called before staring a new entity to clear out the triangle list
*/
void ClearMetaTriangles(void)
{
numMetaVerts = 0;
numMetaTriangles = 0;
}
/*
FindMetaVertex()
finds a matching metavertex in the global list, returning its index
*/
static int FindMetaVertex(bspDrawVert_t * src)
{
int i;
bspDrawVert_t *v, *temp;
/* try to find an existing drawvert */
for(i = firstSearchMetaVert, v = &metaVerts[i]; i < numMetaVerts; i++, v++)
{
if(memcmp(src, v, sizeof(bspDrawVert_t)) == 0)
return i;
}
/* enough space? */
if(numMetaVerts >= maxMetaVerts)
{
/* reallocate more room */
maxMetaVerts += GROW_META_VERTS;
temp = safe_malloc(maxMetaVerts * sizeof(bspDrawVert_t));
if(metaVerts != NULL)
{
memcpy(temp, metaVerts, numMetaVerts * sizeof(bspDrawVert_t));
free(metaVerts);
}
metaVerts = temp;
}
/* add the triangle */
memcpy(&metaVerts[numMetaVerts], src, sizeof(bspDrawVert_t));
numMetaVerts++;
/* return the count */
return (numMetaVerts - 1);
}
/*
AddMetaTriangle()
adds a new meta triangle, allocating more memory if necessary
*/
static int AddMetaTriangle(void)
{
metaTriangle_t *temp;
/* enough space? */
if(numMetaTriangles >= maxMetaTriangles)
{
/* reallocate more room */
maxMetaTriangles += GROW_META_TRIANGLES;
temp = safe_malloc(maxMetaTriangles * sizeof(metaTriangle_t));
if(metaTriangles != NULL)
{
memcpy(temp, metaTriangles, numMetaTriangles * sizeof(metaTriangle_t));
free(metaTriangles);
}
metaTriangles = temp;
}
/* increment and return */
numMetaTriangles++;
return numMetaTriangles - 1;
}
/*
FindMetaTriangle()
finds a matching metatriangle in the global list,
otherwise adds it and returns the index to the metatriangle
*/
int FindMetaTriangle(metaTriangle_t * src, bspDrawVert_t * a, bspDrawVert_t * b, bspDrawVert_t * c, int planeNum)
{
int triIndex;
vec3_t dir;
/* detect degenerate triangles fixme: do something proper here */
VectorSubtract(a->xyz, b->xyz, dir);
if(VectorLength(dir) < 0.125f)
return -1;
VectorSubtract(b->xyz, c->xyz, dir);
if(VectorLength(dir) < 0.125f)
return -1;
VectorSubtract(c->xyz, a->xyz, dir);
if(VectorLength(dir) < 0.125f)
return -1;
/* find plane */
if(planeNum >= 0)
{
/* because of precision issues with small triangles, try to use the specified plane */
src->planeNum = planeNum;
VectorCopy(mapplanes[planeNum].normal, src->plane);
src->plane[3] = mapplanes[planeNum].dist;
}
else
{
/* calculate a plane from the triangle's points (and bail if a plane can't be constructed) */
src->planeNum = -1;
if(PlaneFromPoints(src->plane, a->xyz, b->xyz, c->xyz, qtrue) == qfalse)
return -1;
}
/* ydnar 2002-10-03: repair any bogus normals (busted ase import kludge) */
if(VectorLength(a->normal) <= 0.0f)
VectorCopy(src->plane, a->normal);
if(VectorLength(b->normal) <= 0.0f)
VectorCopy(src->plane, b->normal);
if(VectorLength(c->normal) <= 0.0f)
VectorCopy(src->plane, c->normal);
/* ydnar 2002-10-04: set lightmap axis if not already set */
if(!(src->si->compileFlags & C_VERTEXLIT) &&
src->lightmapAxis[0] == 0.0f && src->lightmapAxis[1] == 0.0f && src->lightmapAxis[2] == 0.0f)
{
/* the shader can specify an explicit lightmap axis */
if(src->si->lightmapAxis[0] || src->si->lightmapAxis[1] || src->si->lightmapAxis[2])
VectorCopy(src->si->lightmapAxis, src->lightmapAxis);
/* new axis-finding code */
else
CalcLightmapAxis(src->plane, src->lightmapAxis);
}
/* fill out the src triangle */
src->indexes[0] = FindMetaVertex(a);
src->indexes[1] = FindMetaVertex(b);
src->indexes[2] = FindMetaVertex(c);
/* try to find an existing triangle */
#ifdef USE_EXHAUSTIVE_SEARCH
{
int i;
metaTriangle_t *tri;
for(i = 0, tri = metaTriangles; i < numMetaTriangles; i++, tri++)
{
if(memcmp(src, tri, sizeof(metaTriangle_t)) == 0)
return i;
}
}
#endif
/* get a new triangle */
triIndex = AddMetaTriangle();
/* add the triangle */
memcpy(&metaTriangles[triIndex], src, sizeof(metaTriangle_t));
/* return the triangle index */
return triIndex;
}
/*
SurfaceToMetaTriangles()
converts a classified surface to metatriangles
*/
static void SurfaceToMetaTriangles(mapDrawSurface_t * ds)
{
int i;
metaTriangle_t src;
bspDrawVert_t a, b, c;
/* only handle certain types of surfaces */
if(ds->type != SURFACE_FACE && ds->type != SURFACE_META && ds->type != SURFACE_FORCED_META && ds->type != SURFACE_DECAL)
return;
/* speed at the expense of memory */
firstSearchMetaVert = numMetaVerts;
/* only handle valid surfaces */
if(ds->type != SURFACE_BAD && ds->numVerts >= 3 && ds->numIndexes >= 3)
{
/* walk the indexes and create triangles */
for(i = 0; i < ds->numIndexes; i += 3)
{
/* sanity check the indexes */
if(ds->indexes[i] == ds->indexes[i + 1] ||
ds->indexes[i] == ds->indexes[i + 2] || ds->indexes[i + 1] == ds->indexes[i + 2])
{
//% Sys_Printf( "%d! ", ds->numVerts );
continue;
}
/* build a metatriangle */
src.si = ds->shaderInfo;
src.side = (ds->sideRef != NULL ? ds->sideRef->side : NULL);
src.entityNum = ds->entityNum;
src.surfaceNum = ds->surfaceNum;
src.planeNum = ds->planeNum;
src.castShadows = ds->castShadows;
src.recvShadows = ds->recvShadows;
src.fogNum = ds->fogNum;
src.sampleSize = ds->sampleSize;
src.shadeAngleDegrees = ds->shadeAngleDegrees;
VectorCopy(ds->lightmapAxis, src.lightmapAxis);
/* copy drawverts */
memcpy(&a, &ds->verts[ds->indexes[i]], sizeof(a));
memcpy(&b, &ds->verts[ds->indexes[i + 1]], sizeof(b));
memcpy(&c, &ds->verts[ds->indexes[i + 2]], sizeof(c));
FindMetaTriangle(&src, &a, &b, &c, ds->planeNum);
}
/* add to count */
numMetaSurfaces++;
}
/* clear the surface (free verts and indexes, sets it to SURFACE_BAD) */
ClearSurface(ds);
}
/*
TriangulatePatchSurface()
creates triangles from a patch
*/
void TriangulatePatchSurface(entity_t * e, mapDrawSurface_t * ds)
{
int iterations, x, y, pw[5], r;
mapDrawSurface_t *dsNew;
mesh_t src, *subdivided, *mesh;
int forcePatchMeta;
int patchQuality;
int patchSubdivision;
/* vortex: _patchMeta, _patchQuality, _patchSubdivide support */
forcePatchMeta = IntForKey(e, "_patchMeta");
if(!forcePatchMeta)
forcePatchMeta = IntForKey(e, "patchMeta");
patchQuality = IntForKey(e, "_patchQuality");
if(!patchQuality)
patchQuality = IntForKey(e, "patchQuality");
if(!patchQuality)
patchQuality = 1.0;
patchSubdivision = IntForKey(e, "_patchSubdivide");
if(!patchSubdivision)
patchSubdivision = IntForKey(e, "patchSubdivide");
/* try to early out */
if(ds->numVerts == 0 || ds->type != SURFACE_PATCH || (patchMeta == qfalse && !forcePatchMeta))
return;
/* make a mesh from the drawsurf */
src.width = ds->patchWidth;
src.height = ds->patchHeight;
src.verts = ds->verts;
//% subdivided = SubdivideMesh( src, 8, 999 );
if(patchSubdivision)
iterations = IterationsForCurve(ds->longestCurve, patchSubdivision);
else
iterations = IterationsForCurve(ds->longestCurve, patchSubdivisions / patchQuality);
subdivided = SubdivideMesh2(src, iterations); //% ds->maxIterations
/* fit it to the curve and remove colinear verts on rows/columns */
PutMeshOnCurve(*subdivided);
mesh = RemoveLinearMeshColumnsRows(subdivided);
FreeMesh(subdivided);
//% MakeMeshNormals( mesh );
/* make a copy of the drawsurface */
dsNew = AllocDrawSurface(SURFACE_META);
memcpy(dsNew, ds, sizeof(*ds));
/* if the patch is nonsolid, then discard it */
if(!(ds->shaderInfo->compileFlags & C_SOLID))
ClearSurface(ds);
/* set new pointer */
ds = dsNew;
/* basic transmogrification */
ds->type = SURFACE_META;
ds->numIndexes = 0;
ds->indexes = safe_malloc(mesh->width * mesh->height * 6 * sizeof(int));
/* copy the verts in */
ds->numVerts = (mesh->width * mesh->height);
ds->verts = mesh->verts;
/* iterate through the mesh quads */
for(y = 0; y < (mesh->height - 1); y++)
{
for(x = 0; x < (mesh->width - 1); x++)
{
/* set indexes */
pw[0] = x + (y * mesh->width);
pw[1] = x + ((y + 1) * mesh->width);
pw[2] = x + 1 + ((y + 1) * mesh->width);
pw[3] = x + 1 + (y * mesh->width);
pw[4] = x + (y * mesh->width); /* same as pw[ 0 ] */
/* set radix */
r = (x + y) & 1;
/* make first triangle */
ds->indexes[ds->numIndexes++] = pw[r + 0];
ds->indexes[ds->numIndexes++] = pw[r + 1];
ds->indexes[ds->numIndexes++] = pw[r + 2];
/* make second triangle */
ds->indexes[ds->numIndexes++] = pw[r + 0];
ds->indexes[ds->numIndexes++] = pw[r + 2];
ds->indexes[ds->numIndexes++] = pw[r + 3];
}
}
/* free the mesh, but not the verts */
free(mesh);
/* add to count */
numPatchMetaSurfaces++;
/* classify it */
ClassifySurfaces(1, ds);
}
/*
FanFaceSurface() - ydnar
creates a tri-fan from a brush face winding
loosely based on SurfaceAsTriFan()
*/
void FanFaceSurface(mapDrawSurface_t * ds)
{
int i, j, k, a, b, c, color[MAX_LIGHTMAPS][4];
bspDrawVert_t *verts, *centroid, *dv;
double iv;
/* try to early out */
if(!ds->numVerts || (ds->type != SURFACE_FACE && ds->type != SURFACE_DECAL))
return;
/* add a new vertex at the beginning of the surface */
verts = safe_malloc((ds->numVerts + 1) * sizeof(bspDrawVert_t));
memset(verts, 0, sizeof(bspDrawVert_t));
memcpy(&verts[1], ds->verts, ds->numVerts * sizeof(bspDrawVert_t));
free(ds->verts);
ds->verts = verts;
/* add up the drawverts to create a centroid */
centroid = &verts[0];
memset(color, 0, 4 * MAX_LIGHTMAPS * sizeof(int));
for(i = 1, dv = &verts[1]; i < (ds->numVerts + 1); i++, dv++)
{
VectorAdd(centroid->xyz, dv->xyz, centroid->xyz);
VectorAdd(centroid->normal, dv->normal, centroid->normal);
for(j = 0; j < 4; j++)
{
for(k = 0; k < MAX_LIGHTMAPS; k++)
color[k][j] += dv->lightColor[k][j];
if(j < 2)
{
centroid->st[j] += dv->st[j];
for(k = 0; k < MAX_LIGHTMAPS; k++)
centroid->lightmap[k][j] += dv->lightmap[k][j];
}
}
}
/* average the centroid */
iv = 1.0f / ds->numVerts;
VectorScale(centroid->xyz, iv, centroid->xyz);
if(VectorNormalize2(centroid->normal, centroid->normal) <= 0)
VectorCopy(verts[1].normal, centroid->normal);
for(j = 0; j < 4; j++)
{
for(k = 0; k < MAX_LIGHTMAPS; k++)
{
color[k][j] /= ds->numVerts;
centroid->lightColor[k][j] = (color[k][j] < 255.0f ? color[k][j] : 255.0f);
}
if(j < 2)
{
centroid->st[j] *= iv;
for(k = 0; k < MAX_LIGHTMAPS; k++)
centroid->lightmap[k][j] *= iv;
}
}
/* add to vert count */
ds->numVerts++;
/* fill indexes in triangle fan order */
ds->numIndexes = 0;
ds->indexes = safe_malloc(ds->numVerts * 3 * sizeof(int));
for(i = 1; i < ds->numVerts; i++)
{
a = 0;
b = i;
c = (i + 1) % ds->numVerts;
c = c ? c : 1;
ds->indexes[ds->numIndexes++] = a;
ds->indexes[ds->numIndexes++] = b;
ds->indexes[ds->numIndexes++] = c;
}
/* add to count */
numFanSurfaces++;
/* classify it */
ClassifySurfaces(1, ds);
}
/*
StripFaceSurface() - ydnar
attempts to create a valid tri-strip w/o degenerate triangles from a brush face winding
based on SurfaceAsTriStrip()
*/
#define MAX_INDEXES 1024
void StripFaceSurface(mapDrawSurface_t * ds)
{
int i, r, least, rotate, numIndexes, ni, a, b, c, indexes[MAX_INDEXES];
vec_t *v1, *v2;
/* try to early out */
if(!ds->numVerts || (ds->type != SURFACE_FACE && ds->type != SURFACE_DECAL))
return;
/* is this a simple triangle? */
if(ds->numVerts == 3)
{
numIndexes = 3;
VectorSet(indexes, 0, 1, 2);
}
else
{
/* ydnar: find smallest coordinate */
least = 0;
if(ds->shaderInfo != NULL && ds->shaderInfo->autosprite == qfalse)
{
for(i = 0; i < ds->numVerts; i++)
{
/* get points */
v1 = ds->verts[i].xyz;
v2 = ds->verts[least].xyz;
/* compare */
if(v1[0] < v2[0] || (v1[0] == v2[0] && v1[1] < v2[1]) || (v1[0] == v2[0] && v1[1] == v2[1] && v1[2] < v2[2]))
least = i;
}
}
/* determine the triangle strip order */
numIndexes = (ds->numVerts - 2) * 3;
if(numIndexes > MAX_INDEXES)
Error("MAX_INDEXES exceeded for surface (%d > %d) (%d verts)", numIndexes, MAX_INDEXES, ds->numVerts);
/* try all possible orderings of the points looking for a non-degenerate strip order */
for(r = 0; r < ds->numVerts; r++)
{
/* set rotation */
rotate = (r + least) % ds->numVerts;
/* walk the winding in both directions */
for(ni = 0, i = 0; i < ds->numVerts - 2 - i; i++)
{
/* make indexes */
a = (ds->numVerts - 1 - i + rotate) % ds->numVerts;
b = (i + rotate) % ds->numVerts;
c = (ds->numVerts - 2 - i + rotate) % ds->numVerts;
/* test this triangle */
if(ds->numVerts > 4 && IsTriangleDegenerate(ds->verts, a, b, c))
break;
indexes[ni++] = a;
indexes[ni++] = b;
indexes[ni++] = c;
/* handle end case */
if(i + 1 != ds->numVerts - 1 - i)
{
/* make indexes */
a = (ds->numVerts - 2 - i + rotate) % ds->numVerts;
b = (i + rotate) % ds->numVerts;
c = (i + 1 + rotate) % ds->numVerts;
/* test triangle */
if(ds->numVerts > 4 && IsTriangleDegenerate(ds->verts, a, b, c))
break;
indexes[ni++] = a;
indexes[ni++] = b;
indexes[ni++] = c;
}
}
/* valid strip? */
if(ni == numIndexes)
break;
}
/* if any triangle in the strip is degenerate, render from a centered fan point instead */
if(ni < numIndexes)
{
FanFaceSurface(ds);
return;
}
}
/* copy strip triangle indexes */
ds->numIndexes = numIndexes;
ds->indexes = safe_malloc(ds->numIndexes * sizeof(int));
memcpy(ds->indexes, indexes, ds->numIndexes * sizeof(int));
/* add to count */
numStripSurfaces++;
/* classify it */
ClassifySurfaces(1, ds);
}
/*
EmitMetaStatictics
vortex: prints meta statistics in general output
*/
void EmitMetaStats()
{
Sys_Printf("--- EmitMetaStats ---\n");
Sys_Printf("%9d total meta surfaces\n", numMetaSurfaces);
Sys_Printf("%9d stripped surfaces\n", numStripSurfaces);
Sys_Printf("%9d fanned surfaces\n", numFanSurfaces);
Sys_Printf("%9d patch meta surfaces\n", numPatchMetaSurfaces);
Sys_Printf("%9d meta verts\n", numMetaVerts);
Sys_Printf("%9d meta triangles\n", numMetaTriangles);
}
/*
MakeEntityMetaTriangles()
builds meta triangles from brush faces (tristrips and fans)
*/
void MakeEntityMetaTriangles(entity_t * e)
{
int i, f, fOld, start;
mapDrawSurface_t *ds;
/* note it */
Sys_FPrintf(SYS_VRB, "--- MakeEntityMetaTriangles ---\n");
/* init pacifier */
fOld = -1;
start = I_FloatTime();
/* walk the list of surfaces in the entity */
for(i = e->firstDrawSurf; i < numMapDrawSurfs; i++)
{
/* print pacifier */
f = 10 * (i - e->firstDrawSurf) / (numMapDrawSurfs - e->firstDrawSurf);
if(f != fOld)
{
fOld = f;
Sys_FPrintf(SYS_VRB, "%d...", f);
}
/* get surface */
ds = &mapDrawSurfs[i];
if(ds->numVerts <= 0)
continue;
/* ignore autosprite surfaces */
if(ds->shaderInfo->autosprite)
continue;
/* meta this surface? */
if(meta == qfalse && ds->shaderInfo->forceMeta == qfalse)
continue;
/* switch on type */
switch (ds->type)
{
case SURFACE_FACE:
case SURFACE_DECAL:
StripFaceSurface(ds);
SurfaceToMetaTriangles(ds);
break;
case SURFACE_PATCH:
TriangulatePatchSurface(e, ds);
break;
case SURFACE_TRIANGLES:
break;
case SURFACE_FORCED_META:
case SURFACE_META:
SurfaceToMetaTriangles(ds);
break;
default:
break;
}
}
/* print time */
if((numMapDrawSurfs - e->firstDrawSurf))
Sys_FPrintf(SYS_VRB, " (%d)\n", (int)(I_FloatTime() - start));
/* emit some stats */
Sys_FPrintf(SYS_VRB, "%9d total meta surfaces\n", numMetaSurfaces);
Sys_FPrintf(SYS_VRB, "%9d stripped surfaces\n", numStripSurfaces);
Sys_FPrintf(SYS_VRB, "%9d fanned surfaces\n", numFanSurfaces);
Sys_FPrintf(SYS_VRB, "%9d patch meta surfaces\n", numPatchMetaSurfaces);
Sys_FPrintf(SYS_VRB, "%9d meta verts\n", numMetaVerts);
Sys_FPrintf(SYS_VRB, "%9d meta triangles\n", numMetaTriangles);
/* tidy things up */
TidyEntitySurfaces(e);
}
/*
PointTriangleIntersect()
assuming that all points lie in plane, determine if pt
is inside the triangle abc
code originally (c) 2001 softSurfer (www.softsurfer.com)
*/
#define MIN_OUTSIDE_EPSILON -0.01f
#define MAX_OUTSIDE_EPSILON 1.01f
static qboolean PointTriangleIntersect(vec3_t pt, vec4_t plane, vec3_t a, vec3_t b, vec3_t c, vec3_t bary)
{
vec3_t u, v, w;
float uu, uv, vv, wu, wv, d;
/* make vectors */
VectorSubtract(b, a, u);
VectorSubtract(c, a, v);
VectorSubtract(pt, a, w);
/* more setup */
uu = DotProduct(u, u);
uv = DotProduct(u, v);
vv = DotProduct(v, v);
wu = DotProduct(w, u);
wv = DotProduct(w, v);
d = uv * uv - uu * vv;
/* calculate barycentric coordinates */
bary[1] = (uv * wv - vv * wu) / d;
if(bary[1] < MIN_OUTSIDE_EPSILON || bary[1] > MAX_OUTSIDE_EPSILON)
return qfalse;
bary[2] = (uv * wv - uu * wv) / d;
if(bary[2] < MIN_OUTSIDE_EPSILON || bary[2] > MAX_OUTSIDE_EPSILON)
return qfalse;
bary[0] = 1.0f - (bary[1] + bary[2]);
/* point is in triangle */
return qtrue;
}
/*
CreateEdge()
sets up an edge structure from a plane and 2 points that the edge ab falls lies in
*/
typedef struct edge_s
{
vec3_t origin, edge;
vec_t length, kingpinLength;
int kingpin;
vec4_t plane;
}
edge_t;
void CreateEdge(vec4_t plane, vec3_t a, vec3_t b, edge_t * edge)
{
/* copy edge origin */
VectorCopy(a, edge->origin);
/* create vector aligned with winding direction of edge */
VectorSubtract(b, a, edge->edge);
if(fabs(edge->edge[0]) > fabs(edge->edge[1]) && fabs(edge->edge[0]) > fabs(edge->edge[2]))
edge->kingpin = 0;
else if(fabs(edge->edge[1]) > fabs(edge->edge[0]) && fabs(edge->edge[1]) > fabs(edge->edge[2]))
edge->kingpin = 1;
else
edge->kingpin = 2;
edge->kingpinLength = edge->edge[edge->kingpin];
VectorNormalize(edge->edge);
edge->edge[3] = DotProduct(a, edge->edge);
edge->length = DotProduct(b, edge->edge) - edge->edge[3];
/* create perpendicular plane that edge lies in */
CrossProduct(plane, edge->edge, edge->plane);
edge->plane[3] = DotProduct(a, edge->plane);
}
/*
FixMetaTJunctions()
fixes t-junctions on meta triangles
*/
#define TJ_PLANE_EPSILON (1.0f / 8.0f)
#define TJ_EDGE_EPSILON (1.0f / 8.0f)
#define TJ_POINT_EPSILON (1.0f / 8.0f)
void FixMetaTJunctions(void)
{
int i, j, k, f, fOld, start, vertIndex, triIndex, numTJuncs;
metaTriangle_t *tri, *newTri;
shaderInfo_t *si;
bspDrawVert_t *a, *b, *c, junc;
float dist, amount;
vec3_t pt;
vec4_t plane;
edge_t edges[3];
/* this code is crap; revisit later */
return;
/* note it */
Sys_FPrintf(SYS_VRB, "--- FixMetaTJunctions ---\n");
/* init pacifier */
fOld = -1;
start = I_FloatTime();
/* walk triangle list */
numTJuncs = 0;
for(i = 0; i < numMetaTriangles; i++)
{
/* get triangle */
tri = &metaTriangles[i];
/* print pacifier */
f = 10 * i / numMetaTriangles;
if(f != fOld)
{
fOld = f;
Sys_FPrintf(SYS_VRB, "%d...", f);
}
/* attempt to early out */
si = tri->si;
if((si->compileFlags & C_NODRAW) || si->autosprite || si->notjunc)
continue;
/* calculate planes */
VectorCopy(tri->plane, plane);
plane[3] = tri->plane[3];
CreateEdge(plane, metaVerts[tri->indexes[0]].xyz, metaVerts[tri->indexes[1]].xyz, &edges[0]);
CreateEdge(plane, metaVerts[tri->indexes[1]].xyz, metaVerts[tri->indexes[2]].xyz, &edges[1]);
CreateEdge(plane, metaVerts[tri->indexes[2]].xyz, metaVerts[tri->indexes[0]].xyz, &edges[2]);
/* walk meta vert list */
for(j = 0; j < numMetaVerts; j++)
{
/* get vert */
VectorCopy(metaVerts[j].xyz, pt);
/* debug code: darken verts */
if(i == 0)
VectorSet(metaVerts[j].lightColor[0], 8, 8, 8);
/* determine if point lies in the triangle's plane */
dist = DotProduct(pt, plane) - plane[3];
if(fabs(dist) > TJ_PLANE_EPSILON)
continue;
/* skip this point if it already exists in the triangle */
for(k = 0; k < 3; k++)
{
if(fabs(pt[0] - metaVerts[tri->indexes[k]].xyz[0]) <= TJ_POINT_EPSILON &&
fabs(pt[1] - metaVerts[tri->indexes[k]].xyz[1]) <= TJ_POINT_EPSILON &&
fabs(pt[2] - metaVerts[tri->indexes[k]].xyz[2]) <= TJ_POINT_EPSILON)
break;
}
if(k < 3)
continue;
/* walk edges */
for(k = 0; k < 3; k++)
{
/* ignore bogus edges */
if(fabs(edges[k].kingpinLength) < TJ_EDGE_EPSILON)
continue;
/* determine if point lies on the edge */
dist = DotProduct(pt, edges[k].plane) - edges[k].plane[3];
if(fabs(dist) > TJ_EDGE_EPSILON)
continue;
/* determine how far along the edge the point lies */
amount = (pt[edges[k].kingpin] - edges[k].origin[edges[k].kingpin]) / edges[k].kingpinLength;
if(amount <= 0.0f || amount >= 1.0f)
continue;
#if 0
dist = DotProduct(pt, edges[k].edge) - edges[k].edge[3];
if(dist <= -0.0f || dist >= edges[k].length)
continue;
amount = dist / edges[k].length;
#endif
/* debug code: brighten this point */
//% metaVerts[ j ].color[ 0 ][ 0 ] += 5;
//% metaVerts[ j ].color[ 0 ][ 1 ] += 4;
VectorSet(metaVerts[tri->indexes[k]].lightColor[0], 255, 204, 0);
VectorSet(metaVerts[tri->indexes[(k + 1) % 3]].lightColor[0], 255, 204, 0);
/* the edge opposite the zero-weighted vertex was hit, so use that as an amount */
a = &metaVerts[tri->indexes[k % 3]];
b = &metaVerts[tri->indexes[(k + 1) % 3]];
c = &metaVerts[tri->indexes[(k + 2) % 3]];
/* make new vert */
LerpDrawVertAmount(a, b, amount, &junc);
VectorCopy(pt, junc.xyz);
/* compare against existing verts */
if(VectorCompare(junc.xyz, a->xyz) || VectorCompare(junc.xyz, b->xyz) || VectorCompare(junc.xyz, c->xyz))
continue;
/* see if we can just re-use the existing vert */
if(!memcmp(&metaVerts[j], &junc, sizeof(junc)))
vertIndex = j;
else
{
/* find new vertex (note: a and b are invalid pointers after this) */
firstSearchMetaVert = numMetaVerts;
vertIndex = FindMetaVertex(&junc);
if(vertIndex < 0)
continue;
}
/* make new triangle */
triIndex = AddMetaTriangle();
if(triIndex < 0)
continue;
/* get triangles */
tri = &metaTriangles[i];
newTri = &metaTriangles[triIndex];
/* copy the triangle */
memcpy(newTri, tri, sizeof(*tri));
/* fix verts */
tri->indexes[(k + 1) % 3] = vertIndex;
newTri->indexes[k] = vertIndex;
/* recalculate edges */
CreateEdge(plane, metaVerts[tri->indexes[0]].xyz, metaVerts[tri->indexes[1]].xyz, &edges[0]);
CreateEdge(plane, metaVerts[tri->indexes[1]].xyz, metaVerts[tri->indexes[2]].xyz, &edges[1]);
CreateEdge(plane, metaVerts[tri->indexes[2]].xyz, metaVerts[tri->indexes[0]].xyz, &edges[2]);
/* debug code */
metaVerts[vertIndex].lightColor[0][0] = 255;
metaVerts[vertIndex].lightColor[0][1] = 204;
metaVerts[vertIndex].lightColor[0][2] = 0;
/* add to counter and end processing of this vert */
numTJuncs++;
break;
}
}
}
/* print time */
Sys_FPrintf(SYS_VRB, " (%d)\n", (int)(I_FloatTime() - start));
/* emit some stats */
Sys_FPrintf(SYS_VRB, "%9d T-junctions added\n", numTJuncs);
}
/*
SmoothMetaTriangles()
averages coincident vertex normals in the meta triangles
*/
#define MAX_SAMPLES 256
#define THETA_EPSILON 0.000001
#define EQUAL_NORMAL_EPSILON 0.01
void SmoothMetaTriangles(void)
{
int i, j, k, f, fOld, start, cs, numVerts, numVotes, numSmoothed;
float shadeAngle, defaultShadeAngle, maxShadeAngle, dot, testAngle;
metaTriangle_t *tri;
float *shadeAngles;
byte *smoothed;
vec3_t average, diff;