forked from TTimo/bspc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
map_q1.c
1174 lines (1122 loc) · 32.5 KB
/
map_q1.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-2005 Id Software, Inc.
This file is part of Quake III Arena source code.
Quake III Arena source code 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.
Quake III Arena source code 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 Foobar; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
===========================================================================
*/
#include "qbsp.h"
#include "l_bsp_q1.h"
#include "aas_map.h" //AAS_CreateMapBrushes
int q1_numbrushes;
int q1_numclipbrushes;
//#define Q1_PRINT
//===========================================================================
// water, slime and lava brush textures names always start with a *
// followed by the type: "slime", "lava" or otherwise water
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
int Q1_TextureContents(char *name)
{
if (!Q_strcasecmp(name, "clip")) return CONTENTS_SOLID;
if (name[0] == '*')
{
if (!Q_strncasecmp(name+1,"lava",4)) return CONTENTS_LAVA;
else if (!Q_strncasecmp(name+1,"slime",5)) return CONTENTS_SLIME;
else return CONTENTS_WATER;
} //end if
else if (!Q_strncasecmp(name, "sky", 3)) return CONTENTS_SOLID;
else return CONTENTS_SOLID;
} //end of the function Q1_TextureContents
//===========================================================================
// Generates two new brushes, leaving the original
// unchanged
//
// modified for Half-Life because there are quite a lot of tiny node leaves
// in the Half-Life bsps
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
void Q1_SplitBrush(bspbrush_t *brush, int planenum, int nodenum,
bspbrush_t **front, bspbrush_t **back)
{
bspbrush_t *b[2];
int i, j;
winding_t *w, *cw[2], *midwinding;
plane_t *plane, *plane2;
side_t *s, *cs;
float d, d_front, d_back;
*front = *back = NULL;
plane = &mapplanes[planenum];
// check all points
d_front = d_back = 0;
for (i=0 ; i<brush->numsides ; i++)
{
w = brush->sides[i].winding;
if (!w)
continue;
for (j=0 ; j<w->numpoints ; j++)
{
d = DotProduct (w->p[j], plane->normal) - plane->dist;
if (d > 0 && d > d_front)
d_front = d;
if (d < 0 && d < d_back)
d_back = d;
} //end for
} //end for
if (d_front < 0.1) // PLANESIDE_EPSILON)
{ // only on back
*back = CopyBrush (brush);
Log_Print("Q1_SplitBrush: only on back\n");
return;
} //end if
if (d_back > -0.1) // PLANESIDE_EPSILON)
{ // only on front
*front = CopyBrush (brush);
Log_Print("Q1_SplitBrush: only on front\n");
return;
} //end if
// create a new winding from the split plane
w = BaseWindingForPlane (plane->normal, plane->dist);
for (i = 0; i < brush->numsides && w; i++)
{
plane2 = &mapplanes[brush->sides[i].planenum ^ 1];
ChopWindingInPlace(&w, plane2->normal, plane2->dist, 0); // PLANESIDE_EPSILON);
} //end for
if (!w || WindingIsTiny(w))
{ // the brush isn't really split
int side;
Log_Print("Q1_SplitBrush: no split winding\n");
side = BrushMostlyOnSide (brush, plane);
if (side == PSIDE_FRONT)
*front = CopyBrush (brush);
if (side == PSIDE_BACK)
*back = CopyBrush (brush);
return;
}
if (WindingIsHuge(w))
{
Log_Print("Q1_SplitBrush: WARNING huge split winding\n");
} //end of
midwinding = w;
// split it for real
for (i = 0; i < 2; i++)
{
b[i] = AllocBrush (brush->numsides+1);
b[i]->original = brush->original;
} //end for
// split all the current windings
for (i=0 ; i<brush->numsides ; i++)
{
s = &brush->sides[i];
w = s->winding;
if (!w)
continue;
ClipWindingEpsilon (w, plane->normal, plane->dist,
0 /*PLANESIDE_EPSILON*/, &cw[0], &cw[1]);
for (j=0 ; j<2 ; j++)
{
if (!cw[j])
continue;
#if 0
if (WindingIsTiny (cw[j]))
{
FreeWinding (cw[j]);
continue;
}
#endif
cs = &b[j]->sides[b[j]->numsides];
b[j]->numsides++;
*cs = *s;
// cs->planenum = s->planenum;
// cs->texinfo = s->texinfo;
// cs->visible = s->visible;
// cs->original = s->original;
cs->winding = cw[j];
cs->flags &= ~SFL_TESTED;
} //end for
} //end for
// see if we have valid polygons on both sides
for (i=0 ; i<2 ; i++)
{
BoundBrush (b[i]);
for (j=0 ; j<3 ; j++)
{
if (b[i]->mins[j] < -4096 || b[i]->maxs[j] > 4096)
{
Log_Print("Q1_SplitBrush: bogus brush after clip\n");
break;
} //end if
} //end for
if (b[i]->numsides < 3 || j < 3)
{
FreeBrush (b[i]);
b[i] = NULL;
Log_Print("Q1_SplitBrush: numsides < 3\n");
} //end if
} //end for
if ( !(b[0] && b[1]) )
{
if (!b[0] && !b[1])
Log_Print("Q1_SplitBrush: split removed brush\n");
else
Log_Print("Q1_SplitBrush: split not on both sides\n");
if (b[0])
{
FreeBrush (b[0]);
*front = CopyBrush (brush);
} //end if
if (b[1])
{
FreeBrush (b[1]);
*back = CopyBrush (brush);
} //end if
return;
} //end if
// add the midwinding to both sides
for (i = 0; i < 2; i++)
{
cs = &b[i]->sides[b[i]->numsides];
b[i]->numsides++;
cs->planenum = planenum^i^1;
cs->texinfo = 0;
//store the node number in the surf to find the texinfo later on
cs->surf = nodenum;
//
cs->flags &= ~SFL_VISIBLE;
cs->flags &= ~SFL_TESTED;
cs->flags &= ~SFL_TEXTURED;
if (i==0)
cs->winding = CopyWinding (midwinding);
else
cs->winding = midwinding;
} //end for
{
vec_t v1;
int i;
for (i=0 ; i<2 ; i++)
{
v1 = BrushVolume (b[i]);
if (v1 < 1)
{
FreeBrush (b[i]);
b[i] = NULL;
Log_Print("Q1_SplitBrush: tiny volume after clip\n");
} //end if
} //end for
} //*/
*front = b[0];
*back = b[1];
} //end of the function Q1_SplitBrush
//===========================================================================
// returns true if the tree starting at nodenum has only solid leaves
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
int Q1_SolidTree_r(int nodenum)
{
if (nodenum < 0)
{
switch(q1_dleafs[(-nodenum) - 1].contents)
{
case Q1_CONTENTS_EMPTY:
{
return false;
} //end case
case Q1_CONTENTS_SOLID:
#ifdef HLCONTENTS
case Q1_CONTENTS_CLIP:
#endif
case Q1_CONTENTS_SKY:
#ifdef HLCONTENTS
case Q1_CONTENTS_TRANSLUCENT:
#endif
{
return true;
} //end case
case Q1_CONTENTS_WATER:
case Q1_CONTENTS_SLIME:
case Q1_CONTENTS_LAVA:
#ifdef HLCONTENTS
//these contents should not be found in the BSP
case Q1_CONTENTS_ORIGIN:
case Q1_CONTENTS_CURRENT_0:
case Q1_CONTENTS_CURRENT_90:
case Q1_CONTENTS_CURRENT_180:
case Q1_CONTENTS_CURRENT_270:
case Q1_CONTENTS_CURRENT_UP:
case Q1_CONTENTS_CURRENT_DOWN:
#endif
default:
{
return false;
} //end default
} //end switch
return false;
} //end if
if (!Q1_SolidTree_r(q1_dnodes[nodenum].children[0])) return false;
if (!Q1_SolidTree_r(q1_dnodes[nodenum].children[1])) return false;
return true;
} //end of the function Q1_SolidTree_r
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
bspbrush_t *Q1_CreateBrushes_r(bspbrush_t *brush, int nodenum)
{
int planenum;
bspbrush_t *front, *back;
q1_dleaf_t *leaf;
//if it is a leaf
if (nodenum < 0)
{
leaf = &q1_dleafs[(-nodenum) - 1];
if (leaf->contents != Q1_CONTENTS_EMPTY)
{
#ifdef Q1_PRINT
qprintf("\r%5i", ++q1_numbrushes);
#endif //Q1_PRINT
} //end if
switch(leaf->contents)
{
case Q1_CONTENTS_EMPTY:
{
FreeBrush(brush);
return NULL;
} //end case
case Q1_CONTENTS_SOLID:
#ifdef HLCONTENTS
case Q1_CONTENTS_CLIP:
#endif
case Q1_CONTENTS_SKY:
#ifdef HLCONTENTS
case Q1_CONTENTS_TRANSLUCENT:
#endif
{
brush->side = CONTENTS_SOLID;
return brush;
} //end case
case Q1_CONTENTS_WATER:
{
brush->side = CONTENTS_WATER;
return brush;
} //end case
case Q1_CONTENTS_SLIME:
{
brush->side = CONTENTS_SLIME;
return brush;
} //end case
case Q1_CONTENTS_LAVA:
{
brush->side = CONTENTS_LAVA;
return brush;
} //end case
#ifdef HLCONTENTS
//these contents should not be found in the BSP
case Q1_CONTENTS_ORIGIN:
case Q1_CONTENTS_CURRENT_0:
case Q1_CONTENTS_CURRENT_90:
case Q1_CONTENTS_CURRENT_180:
case Q1_CONTENTS_CURRENT_270:
case Q1_CONTENTS_CURRENT_UP:
case Q1_CONTENTS_CURRENT_DOWN:
{
Error("Q1_CreateBrushes_r: found contents %d in Half-Life BSP", leaf->contents);
return NULL;
} //end case
#endif
default:
{
Error("Q1_CreateBrushes_r: unknown contents %d in Half-Life BSP", leaf->contents);
return NULL;
} //end default
} //end switch
return NULL;
} //end if
//if the rest of the tree is solid
/*if (Q1_SolidTree_r(nodenum))
{
brush->side = CONTENTS_SOLID;
return brush;
} //end if*/
//
planenum = q1_dnodes[nodenum].planenum;
planenum = FindFloatPlane(q1_dplanes[planenum].normal, q1_dplanes[planenum].dist);
//split the brush with the node plane
Q1_SplitBrush(brush, planenum, nodenum, &front, &back);
//free the original brush
FreeBrush(brush);
//every node must split the brush in two
if (!front || !back)
{
Log_Print("Q1_CreateBrushes_r: WARNING node not splitting brush\n");
//return NULL;
} //end if
//create brushes recursively
if (front) front = Q1_CreateBrushes_r(front, q1_dnodes[nodenum].children[0]);
if (back) back = Q1_CreateBrushes_r(back, q1_dnodes[nodenum].children[1]);
//link the brushes if possible and return them
if (front)
{
for (brush = front; brush->next; brush = brush->next);
brush->next = back;
return front;
} //end if
else
{
return back;
} //end else
} //end of the function Q1_CreateBrushes_r
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
bspbrush_t *Q1_CreateBrushesFromBSP(int modelnum)
{
bspbrush_t *brushlist;
bspbrush_t *brush;
q1_dnode_t *headnode;
vec3_t mins, maxs;
int i;
//
headnode = &q1_dnodes[q1_dmodels[modelnum].headnode[0]];
//get the mins and maxs of the world
VectorCopy(headnode->mins, mins);
VectorCopy(headnode->maxs, maxs);
//enlarge these mins and maxs
for (i = 0; i < 3; i++)
{
mins[i] -= 8;
maxs[i] += 8;
} //end for
//NOTE: have to add the BSP tree mins and maxs to the MAP mins and maxs
AddPointToBounds(mins, map_mins, map_maxs);
AddPointToBounds(maxs, map_mins, map_maxs);
//
if (!modelnum)
{
Log_Print("brush size: %5.0f,%5.0f,%5.0f to %5.0f,%5.0f,%5.0f\n",
map_mins[0], map_mins[1], map_mins[2],
map_maxs[0], map_maxs[1], map_maxs[2]);
} //end if
//create one huge brush containing the whole world
brush = BrushFromBounds(mins, maxs);
VectorCopy(mins, brush->mins);
VectorCopy(maxs, brush->maxs);
//
#ifdef Q1_PRINT
qprintf("creating Quake brushes\n");
qprintf("%5d brushes", q1_numbrushes = 0);
#endif //Q1_PRINT
//create the brushes
brushlist = Q1_CreateBrushes_r(brush, q1_dmodels[modelnum].headnode[0]);
//
#ifdef Q1_PRINT
qprintf("\n");
#endif //Q1_PRINT
//now we've got a list with brushes!
return brushlist;
} //end of the function Q1_CreateBrushesFromBSP
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
q1_dleaf_t *Q1_PointInLeaf(int startnode, vec3_t point)
{
int nodenum;
vec_t dist;
q1_dnode_t *node;
q1_dplane_t *plane;
nodenum = startnode;
while (nodenum >= 0)
{
node = &q1_dnodes[nodenum];
plane = &q1_dplanes[node->planenum];
dist = DotProduct(point, plane->normal) - plane->dist;
if (dist > 0)
nodenum = node->children[0];
else
nodenum = node->children[1];
} //end while
return &q1_dleafs[-nodenum - 1];
} //end of the function Q1_PointInLeaf
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
float Q1_FaceArea(q1_dface_t *face)
{
int i;
float total;
vec_t *v;
vec3_t d1, d2, cross;
q1_dedge_t *edge;
edge = &q1_dedges[face->firstedge];
v = q1_dvertexes[edge->v[0]].point;
total = 0;
for (i = 1; i < face->numedges - 1; i++)
{
edge = &q1_dedges[face->firstedge + i];
VectorSubtract(q1_dvertexes[edge->v[0]].point, v, d1);
VectorSubtract(q1_dvertexes[edge->v[1]].point, v, d2);
CrossProduct(d1, d2, cross);
total += 0.5 * VectorLength(cross);
} //end for
return total;
} //end of the function AAS_FaceArea
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
void Q1_FacePlane(q1_dface_t *face, vec3_t normal, float *dist)
{
vec_t *v1, *v2, *v3;
vec3_t vec1, vec2;
int side, edgenum;
edgenum = q1_dsurfedges[face->firstedge];
side = edgenum < 0;
v1 = q1_dvertexes[q1_dedges[abs(edgenum)].v[side]].point;
v2 = q1_dvertexes[q1_dedges[abs(edgenum)].v[!side]].point;
edgenum = q1_dsurfedges[face->firstedge+1];
side = edgenum < 0;
v3 = q1_dvertexes[q1_dedges[abs(edgenum)].v[!side]].point;
//
VectorSubtract(v2, v1, vec1);
VectorSubtract(v3, v1, vec2);
CrossProduct(vec1, vec2, normal);
VectorNormalize(normal);
*dist = DotProduct(v1, normal);
} //end of the function Q1_FacePlane
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
bspbrush_t *Q1_MergeBrushes(bspbrush_t *brushlist, int modelnum)
{
int nummerges, merged;
bspbrush_t *b1, *b2, *tail, *newbrush, *newbrushlist;
bspbrush_t *lastb2;
if (!brushlist) return NULL;
if (!modelnum) qprintf("%5d brushes merged", nummerges = 0);
do
{
for (tail = brushlist; tail; tail = tail->next)
{
if (!tail->next) break;
} //end for
merged = 0;
newbrushlist = NULL;
for (b1 = brushlist; b1; b1 = brushlist)
{
lastb2 = b1;
for (b2 = b1->next; b2; b2 = b2->next)
{
//can't merge brushes with different contents
if (b1->side != b2->side) newbrush = NULL;
else newbrush = TryMergeBrushes(b1, b2);
//if a merged brush is created
if (newbrush)
{
//copy the brush contents
newbrush->side = b1->side;
//add the new brush to the end of the list
tail->next = newbrush;
//remove the second brush from the list
lastb2->next = b2->next;
//remove the first brush from the list
brushlist = brushlist->next;
//free the merged brushes
FreeBrush(b1);
FreeBrush(b2);
//get a new tail brush
for (tail = brushlist; tail; tail = tail->next)
{
if (!tail->next) break;
} //end for
merged++;
if (!modelnum) qprintf("\r%5d", nummerges++);
break;
} //end if
lastb2 = b2;
} //end for
//if b1 can't be merged with any of the other brushes
if (!b2)
{
brushlist = brushlist->next;
//keep b1
b1->next = newbrushlist;
newbrushlist = b1;
} //end else
} //end for
brushlist = newbrushlist;
} while(merged);
if (!modelnum) qprintf("\n");
return newbrushlist;
} //end of the function Q1_MergeBrushes
//===========================================================================
// returns the amount the face and the winding overlap
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
float Q1_FaceOnWinding(q1_dface_t *face, winding_t *winding)
{
int i, edgenum, side;
float dist, area;
q1_dplane_t plane;
vec_t *v1, *v2;
vec3_t normal, edgevec;
winding_t *w;
//
w = CopyWinding(winding);
memcpy(&plane, &q1_dplanes[face->planenum], sizeof(q1_dplane_t));
//check on which side of the plane the face is
if (face->side)
{
VectorNegate(plane.normal, plane.normal);
plane.dist = -plane.dist;
} //end if
for (i = 0; i < face->numedges && w; i++)
{
//get the first and second vertex of the edge
edgenum = q1_dsurfedges[face->firstedge + i];
side = edgenum > 0;
//if the face plane is flipped
v1 = q1_dvertexes[q1_dedges[abs(edgenum)].v[side]].point;
v2 = q1_dvertexes[q1_dedges[abs(edgenum)].v[!side]].point;
//create a plane through the edge vector, orthogonal to the face plane
//and with the normal vector pointing out of the face
VectorSubtract(v1, v2, edgevec);
CrossProduct(edgevec, plane.normal, normal);
VectorNormalize(normal);
dist = DotProduct(normal, v1);
//
ChopWindingInPlace(&w, normal, dist, 0.9); //CLIP_EPSILON
} //end for
if (w)
{
area = WindingArea(w);
FreeWinding(w);
return area;
} //end if
return 0;
} //end of the function Q1_FaceOnWinding
//===========================================================================
// returns a list with brushes created by splitting the given brush with
// planes that go through the face edges and are orthogonal to the face plane
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
bspbrush_t *Q1_SplitBrushWithFace(bspbrush_t *brush, q1_dface_t *face)
{
int i, edgenum, side, planenum, splits;
float dist;
q1_dplane_t plane;
vec_t *v1, *v2;
vec3_t normal, edgevec;
bspbrush_t *front, *back, *brushlist;
memcpy(&plane, &q1_dplanes[face->planenum], sizeof(q1_dplane_t));
//check on which side of the plane the face is
if (face->side)
{
VectorNegate(plane.normal, plane.normal);
plane.dist = -plane.dist;
} //end if
splits = 0;
brushlist = NULL;
for (i = 0; i < face->numedges; i++)
{
//get the first and second vertex of the edge
edgenum = q1_dsurfedges[face->firstedge + i];
side = edgenum > 0;
//if the face plane is flipped
v1 = q1_dvertexes[q1_dedges[abs(edgenum)].v[side]].point;
v2 = q1_dvertexes[q1_dedges[abs(edgenum)].v[!side]].point;
//create a plane through the edge vector, orthogonal to the face plane
//and with the normal vector pointing out of the face
VectorSubtract(v1, v2, edgevec);
CrossProduct(edgevec, plane.normal, normal);
VectorNormalize(normal);
dist = DotProduct(normal, v1);
//
planenum = FindFloatPlane(normal, dist);
//split the current brush
SplitBrush(brush, planenum, &front, &back);
//if there is a back brush just put it in the list
if (back)
{
//copy the brush contents
back->side = brush->side;
//
back->next = brushlist;
brushlist = back;
splits++;
} //end if
if (!front)
{
Log_Print("Q1_SplitBrushWithFace: no new brush\n");
FreeBrushList(brushlist);
return NULL;
} //end if
//copy the brush contents
front->side = brush->side;
//continue splitting the front brush
brush = front;
} //end for
if (!splits)
{
FreeBrush(front);
return NULL;
} //end if
front->next = brushlist;
brushlist = front;
return brushlist;
} //end of the function Q1_SplitBrushWithFace
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
bspbrush_t *Q1_TextureBrushes(bspbrush_t *brushlist, int modelnum)
{
float area, largestarea;
int i, n, texinfonum, sn, numbrushes, ofs;
int bestfacenum, sidenodenum;
side_t *side;
q1_dmiptexlump_t *miptexlump;
q1_miptex_t *miptex;
bspbrush_t *brush, *nextbrush, *prevbrush, *newbrushes, *brushlistend;
vec_t defaultvec[4] = {1, 0, 0, 0};
if (!modelnum) qprintf("texturing brushes\n");
if (!modelnum) qprintf("%5d brushes", numbrushes = 0);
//get a pointer to the last brush in the list
for (brushlistend = brushlist; brushlistend; brushlistend = brushlistend->next)
{
if (!brushlistend->next) break;
} //end for
//there's no previous brush when at the start of the list
prevbrush = NULL;
//go over the brush list
for (brush = brushlist; brush; brush = nextbrush)
{
nextbrush = brush->next;
//find a texinfo for every brush side
for (sn = 0; sn < brush->numsides; sn++)
{
side = &brush->sides[sn];
//
if (side->flags & SFL_TEXTURED) continue;
//number of the node that created this brush side
sidenodenum = side->surf; //see midwinding in Q1_SplitBrush
//no face found yet
bestfacenum = -1;
//minimum face size
largestarea = 1;
//if optimizing the texture placement and not going for the
//least number of brushes
if (!lessbrushes)
{
for (i = 0; i < q1_numfaces; i++)
{
//the face must be in the same plane as the node plane that created
//this brush side
if (q1_dfaces[i].planenum == q1_dnodes[sidenodenum].planenum)
{
//get the area the face and the brush side overlap
area = Q1_FaceOnWinding(&q1_dfaces[i], side->winding);
//if this face overlaps the brush side winding more than previous faces
if (area > largestarea)
{
//if there already was a face for texturing this brush side with
//a different texture
if (bestfacenum >= 0 &&
(q1_dfaces[bestfacenum].texinfo != q1_dfaces[i].texinfo))
{
//split the brush to fit the texture
newbrushes = Q1_SplitBrushWithFace(brush, &q1_dfaces[i]);
//if new brushes where created
if (newbrushes)
{
//remove the current brush from the list
if (prevbrush) prevbrush->next = brush->next;
else brushlist = brush->next;
if (brushlistend == brush)
{
brushlistend = prevbrush;
nextbrush = newbrushes;
} //end if
//add the new brushes to the end of the list
if (brushlistend) brushlistend->next = newbrushes;
else brushlist = newbrushes;
//free the current brush
FreeBrush(brush);
//don't forget about the prevbrush pointer at the bottom of
//the outer loop
brush = prevbrush;
//find the end of the list
for (brushlistend = brushlist; brushlistend; brushlistend = brushlistend->next)
{
if (!brushlistend->next) break;
} //end for
break;
} //end if
else
{
Log_Write("brush %d: no real texture split", numbrushes);
} //end else
} //end if
else
{
//best face for texturing this brush side
bestfacenum = i;
} //end else
} //end if
} //end if
} //end for
//if the brush was split the original brush is removed
//and we just continue with the next one in the list
if (i < q1_numfaces) break;
} //end if
else
{
//find the face with the largest overlap with this brush side
//for texturing the brush side
for (i = 0; i < q1_numfaces; i++)
{
//the face must be in the same plane as the node plane that created
//this brush side
if (q1_dfaces[i].planenum == q1_dnodes[sidenodenum].planenum)
{
//get the area the face and the brush side overlap
area = Q1_FaceOnWinding(&q1_dfaces[i], side->winding);
//if this face overlaps the brush side winding more than previous faces
if (area > largestarea)
{
largestarea = area;
bestfacenum = i;
} //end if
} //end if
} //end for
} //end else
//if a face was found for texturing this brush side
if (bestfacenum >= 0)
{
//set the MAP texinfo values
texinfonum = q1_dfaces[bestfacenum].texinfo;
for (n = 0; n < 4; n++)
{
map_texinfo[texinfonum].vecs[0][n] = q1_texinfo[texinfonum].vecs[0][n];
map_texinfo[texinfonum].vecs[1][n] = q1_texinfo[texinfonum].vecs[1][n];
} //end for
//make sure the two vectors aren't of zero length otherwise use the default
//vector to prevent a divide by zero in the map writing
if (VectorLength(map_texinfo[texinfonum].vecs[0]) < 0.01)
memcpy(map_texinfo[texinfonum].vecs[0], defaultvec, sizeof(defaultvec));
if (VectorLength(map_texinfo[texinfonum].vecs[1]) < 0.01)
memcpy(map_texinfo[texinfonum].vecs[1], defaultvec, sizeof(defaultvec));
//
map_texinfo[texinfonum].flags = q1_texinfo[texinfonum].flags;
map_texinfo[texinfonum].value = 0; //Q1 and HL texinfos don't have a value
//the mip texture
miptexlump = (q1_dmiptexlump_t *) q1_dtexdata;
ofs = miptexlump->dataofs[q1_texinfo[texinfonum].miptex];
if ( ofs > q1_texdatasize ) {
ofs = miptexlump->dataofs[0];
}
miptex = (q1_miptex_t *)((byte *)miptexlump + ofs);
//get the mip texture name
strcpy(map_texinfo[texinfonum].texture, miptex->name);
//no animations in Quake1 and Half-Life mip textures
map_texinfo[texinfonum].nexttexinfo = -1;
//store the texinfo number
side->texinfo = texinfonum;
//
if (texinfonum > map_numtexinfo) map_numtexinfo = texinfonum;
//this side is textured
side->flags |= SFL_TEXTURED;
} //end if
else
{
//no texture for this side
side->texinfo = TEXINFO_NODE;
//this side is textured
side->flags |= SFL_TEXTURED;
} //end if
} //end for
//
if (!modelnum && prevbrush != brush) qprintf("\r%5d", ++numbrushes);
//previous brush in the list
prevbrush = brush;
} //end for
if (!modelnum) qprintf("\n");
//return the new list with brushes
return brushlist;
} //end of the function Q1_TextureBrushes
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
void Q1_FixContentsTextures(bspbrush_t *brushlist)
{
int i, texinfonum;
bspbrush_t *brush;
for (brush = brushlist; brush; brush = brush->next)
{
//only fix the textures of water, slime and lava brushes
if (brush->side != CONTENTS_WATER &&
brush->side != CONTENTS_SLIME &&
brush->side != CONTENTS_LAVA) continue;
//
for (i = 0; i < brush->numsides; i++)
{
texinfonum = brush->sides[i].texinfo;
if (Q1_TextureContents(map_texinfo[texinfonum].texture) == brush->side) break;
} //end for
//if no specific contents texture was found
if (i >= brush->numsides)
{
texinfonum = -1;
for (i = 0; i < map_numtexinfo; i++)
{
if (Q1_TextureContents(map_texinfo[i].texture) == brush->side)
{
texinfonum = i;
break;
} //end if
} //end for
} //end if
//
if (texinfonum >= 0)
{
//give all the brush sides this contents texture
for (i = 0; i < brush->numsides; i++)
{
brush->sides[i].texinfo = texinfonum;
} //end for
} //end if
else Log_Print("brush contents %d with wrong textures\n", brush->side);
//
} //end for
/*
for (brush = brushlist; brush; brush = brush->next)
{
//give all the brush sides this contents texture
for (i = 0; i < brush->numsides; i++)
{
if (Q1_TextureContents(map_texinfo[texinfonum].texture) != brush->side)
{
Error("brush contents %d with wrong contents textures %s\n", brush->side,
Q1_TextureContents(map_texinfo[texinfonum].texture));
} //end if
} //end for
} //end for*/
} //end of the function Q1_FixContentsTextures
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================