-
Notifications
You must be signed in to change notification settings - Fork 0
/
brush.c
2312 lines (1970 loc) · 49.9 KB
/
brush.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
// brush.c
#include "qe3.h"
#include <assert.h>
brush_t *Brush_Alloc()
{
brush_t *b = (brush_t*)qmalloc(sizeof(brush_t));
return b;
}
void PrintWinding (winding_t *w)
{
int i;
printf ("-------------\n");
for (i=0 ; i<w->numpoints ; i++)
printf ("(%5.2f, %5.2f, %5.2f)\n", w->points[i][0]
, w->points[i][1], w->points[i][2]);
}
void PrintPlane (plane_t *p)
{
printf ("(%5.2f, %5.2f, %5.2f) : %5.2f\n", p->normal[0], p->normal[1],
p->normal[2], p->dist);
}
void PrintVector (vec3_t v)
{
printf ("(%5.2f, %5.2f, %5.2f)\n", v[0], v[1], v[2]);
}
/*
=============================================================================
TEXTURE COORDINATES
=============================================================================
*/
/*
==================
textureAxisFromPlane
==================
*/
vec3_t baseaxis[18] =
{
{0,0,1}, {1,0,0}, {0,-1,0}, // floor
{0,0,-1}, {1,0,0}, {0,-1,0}, // ceiling
{1,0,0}, {0,1,0}, {0,0,-1}, // west wall
{-1,0,0}, {0,1,0}, {0,0,-1}, // east wall
{0,1,0}, {1,0,0}, {0,0,-1}, // south wall
{0,-1,0}, {1,0,0}, {0,0,-1} // north wall
};
void TextureAxisFromPlane(plane_t *pln, vec3_t xv, vec3_t yv)
{
int bestaxis;
float dot,best;
int i;
best = 0;
bestaxis = 0;
for (i=0 ; i<6 ; i++)
{
dot = DotProduct (pln->normal, baseaxis[i*3]);
if (dot > best)
{
best = dot;
bestaxis = i;
}
}
VectorCopy (baseaxis[bestaxis*3+1], xv);
VectorCopy (baseaxis[bestaxis*3+2], yv);
}
//float lightaxis[3] = {0.6, 0.8, 1.0};
vec_t lightaxis[3] = {(vec_t)0.6, (vec_t)0.8, (vec_t)1.0};
/*
================
SetShadeForPlane
Light different planes differently to
improve recognition
================
*/
float SetShadeForPlane (plane_t *p)
{
int i;
float f;
// axial plane
for (i=0 ; i<3 ; i++)
if (fabs(p->normal[i]) > 0.9)
{
f = lightaxis[i];
return f;
}
// between two axial planes
for (i=0 ; i<3 ; i++)
if (fabs(p->normal[i]) < 0.1)
{
f = (lightaxis[(i+1)%3] + lightaxis[(i+2)%3])/2;
return f;
}
// other
f= (lightaxis[0] + lightaxis[1] + lightaxis[2]) / 3;
return f;
}
vec3_t vecs[2];
float shift[2];
/*
================
Face_Alloc
================
*/
face_t *Face_Alloc( void )
{
face_t *f = (face_t*)qmalloc( sizeof( *f ) );
return f;
}
/*
================
Face_Free
================
*/
void Face_Free( face_t *f )
{
assert( f != 0 );
if ( f->face_winding )
{
free( f->face_winding );
f->face_winding = 0;
}
free( f );
}
/*
================
Face_Clone
================
*/
face_t *Face_Clone (face_t *f)
{
face_t *n;
n = Face_Alloc();
n->texdef = f->texdef;
memcpy (n->planepts, f->planepts, sizeof(n->planepts));
// all other fields are derived, and will be set by Brush_Build
return n;
}
/*
================
Face_SetColor
================
*/
void Face_SetColor (brush_t *b, face_t *f)
{
float shade;
qtexture_t *q;
q = f->d_texture;
// set shading for face
shade = SetShadeForPlane (&f->plane);
if (g_qeglobals.d_camera.draw_mode == cd_texture && !b->owner->eclass->fixedsize)
{
f->d_color[0] =
f->d_color[1] =
f->d_color[2] = shade;
}
else
{
f->d_color[0] = shade*q->color[0];
f->d_color[1] = shade*q->color[1];
f->d_color[2] = shade*q->color[2];
}
}
/*
=================
Face_SetTexture
=================
*/
void Face_SetTexture (face_t *f, texdef_t *texdef)
{
f->texdef = *texdef;
Brush_Build(f->owner);
}
/*
================
BeginTexturingFace
================
*/
void BeginTexturingFace (brush_t *b, face_t *f, qtexture_t *q)
{
vec3_t pvecs[2];
int sv, tv;
float ang, sinv, cosv;
float ns, nt;
int i,j;
float shade;
// get natural texture axis
TextureAxisFromPlane(&f->plane, pvecs[0], pvecs[1]);
// set shading for face
shade = SetShadeForPlane (&f->plane);
if (g_qeglobals.d_camera.draw_mode == cd_texture && !b->owner->eclass->fixedsize)
{
f->d_color[0] =
f->d_color[1] =
f->d_color[2] = shade;
}
else
{
f->d_color[0] = shade*q->color[0];
f->d_color[1] = shade*q->color[1];
f->d_color[2] = shade*q->color[2];
}
if (g_qeglobals.d_camera.draw_mode != cd_texture)
return;
if (!f->texdef.scale[0])
f->texdef.scale[0] = 1;
if (!f->texdef.scale[1])
f->texdef.scale[1] = 1;
// rotate axis
if (f->texdef.rotate == 0)
{ sinv = 0 ; cosv = 1; }
else if (f->texdef.rotate == 90)
{ sinv = 1 ; cosv = 0; }
else if (f->texdef.rotate == 180)
{ sinv = 0 ; cosv = -1; }
else if (f->texdef.rotate == 270)
{ sinv = -1 ; cosv = 0; }
else
{
ang = f->texdef.rotate / 180 * Q_PI;
sinv = sin(ang);
cosv = cos(ang);
}
if (pvecs[0][0])
sv = 0;
else if (pvecs[0][1])
sv = 1;
else
sv = 2;
if (pvecs[1][0])
tv = 0;
else if (pvecs[1][1])
tv = 1;
else
tv = 2;
for (i=0 ; i<2 ; i++)
{
ns = cosv * pvecs[i][sv] - sinv * pvecs[i][tv];
nt = sinv * pvecs[i][sv] + cosv * pvecs[i][tv];
vecs[i][sv] = ns;
vecs[i][tv] = nt;
}
for (i=0 ; i<2 ; i++)
for (j=0 ; j<3 ; j++)
vecs[i][j] = vecs[i][j] / f->texdef.scale[i];
}
/*
================
Face_MakePlane
================
*/
void Face_MakePlane (face_t *f)
{
int j;
vec3_t t1, t2, t3;
// convert to a vector / dist plane
for (j=0 ; j<3 ; j++)
{
t1[j] = f->planepts[0][j] - f->planepts[1][j];
t2[j] = f->planepts[2][j] - f->planepts[1][j];
t3[j] = f->planepts[1][j];
}
CrossProduct(t1,t2, f->plane.normal);
if (VectorCompare (f->plane.normal, vec3_origin))
Sys_Printf ("WARNING: brush plane with no normal\n");
VectorNormalize (f->plane.normal);
f->plane.dist = DotProduct (t3, f->plane.normal);
}
/*
void _EmitTextureCoordinates (vec3_t v, qtexture_t *q)
{
float s, t;
s = DotProduct (v, vecs[0]);
t = DotProduct (v, vecs[1]);
s += shift[0];
t += shift[1];
s /= q->width;
t /= q->height;
glTexCoord2f (s, t);
}
*/
/*
================
EmitTextureCoordinates
================
*/
void EmitTextureCoordinates ( float *xyzst, qtexture_t *q, face_t *f)
{
float s, t, ns, nt;
float ang, sinv, cosv;
vec3_t vecs[2];
texdef_t *td;
// get natural texture axis
TextureAxisFromPlane(&f->plane, vecs[0], vecs[1]);
td = &f->texdef;
ang = td->rotate / 180 * Q_PI;
sinv = sin(ang);
cosv = cos(ang);
if (!td->scale[0])
td->scale[0] = 1;
if (!td->scale[1])
td->scale[1] = 1;
s = DotProduct(xyzst, vecs[0]);
t = DotProduct(xyzst, vecs[1]);
ns = cosv * s - sinv * t;
nt = sinv * s + cosv * t;
s = ns/td->scale[0] + td->shift[0];
t = nt/td->scale[1] + td->shift[1];
// gl scales everything from 0 to 1
s /= q->width;
t /= q->height;
xyzst[3] = s;
xyzst[4] = t;
}
//==========================================================================
/*
================
Face_MoveTexture
================
*/
void Face_MoveTexture(face_t *f, vec3_t move)
{
vec3_t pvecs[2];
vec_t s, t, ns, nt;
vec_t ang, sinv, cosv;
texdef_t *td;
TextureAxisFromPlane(&f->plane, pvecs[0], pvecs[1]);
td = &f->texdef;
ang = td->rotate / 180 * Q_PI;
sinv = sin(ang);
cosv = cos(ang);
if (!td->scale[0])
td->scale[0] = 1;
if (!td->scale[1])
td->scale[1] = 1;
s = DotProduct(move, pvecs[0]);
t = DotProduct(move, pvecs[1]);
ns = cosv * s - sinv * t;
nt = sinv * s + cosv * t;
s = ns/td->scale[0];
t = nt/td->scale[1];
f->texdef.shift[0] -= s;
f->texdef.shift[1] -= t;
while(f->texdef.shift[0] > f->d_texture->width)
f->texdef.shift[0] -= f->d_texture->width;
while(f->texdef.shift[1] > f->d_texture->height)
f->texdef.shift[1] -= f->d_texture->height;
while(f->texdef.shift[0] < 0)
f->texdef.shift[0] += f->d_texture->width;
while(f->texdef.shift[1] < 0)
f->texdef.shift[1] += f->d_texture->height;
}
void Brush_MakeFacePlanes (brush_t *b)
{
face_t *f;
int j;
vec3_t t1, t2, t3;
for (f=b->brush_faces ; f ; f=f->next)
{
// convert to a vector / dist plane
for (j=0 ; j<3 ; j++)
{
t1[j] = f->planepts[0][j] - f->planepts[1][j];
t2[j] = f->planepts[2][j] - f->planepts[1][j];
t3[j] = f->planepts[1][j];
}
CrossProduct(t1,t2, f->plane.normal);
if (VectorCompare (f->plane.normal, vec3_origin))
printf ("WARNING: brush plane with no normal\n");
VectorNormalize (f->plane.normal);
f->plane.dist = DotProduct (t3, f->plane.normal);
}
}
void DrawBrushEntityName (brush_t *b)
{
char *name;
float a, s, c;
vec3_t mid;
int i;
if (!b->owner)
return; // during contruction
if (b->owner == world_entity)
return;
if (b != b->owner->brushes.onext)
return; // not key brush
// draw the angle pointer
a = FloatForKey (b->owner, "angle");
if (a)
{
s = sin (a/180*Q_PI);
c = cos (a/180*Q_PI);
for (i=0 ; i<3 ; i++)
mid[i] = (b->mins[i] + b->maxs[i])*0.5;
glBegin (GL_LINE_STRIP);
glVertex3fv (mid);
mid[0] += c*8;
mid[1] += s*8;
glVertex3fv (mid);
mid[0] -= c*4;
mid[1] -= s*4;
mid[0] -= s*4;
mid[1] += c*4;
glVertex3fv (mid);
mid[0] += c*4;
mid[1] += s*4;
mid[0] += s*4;
mid[1] -= c*4;
glVertex3fv (mid);
mid[0] -= c*4;
mid[1] -= s*4;
mid[0] += s*4;
mid[1] -= c*4;
glVertex3fv (mid);
glEnd ();
}
/*
if (!g_qeglobals.d_savedinfo.show_names)
return;
name = ValueForKey (b->owner, "classname");
glRasterPos2f (b->mins[0]+4, b->mins[1]+4);
glCallLists (strlen(name), GL_UNSIGNED_BYTE, name);
*/
if (g_qeglobals.d_savedinfo.show_names)
{
name = ValueForKey (b->owner, "classname");
glRasterPos3f (b->mins[0]+4, b->mins[1]+4, b->mins[2]+4);
glCallLists (strlen(name), GL_UNSIGNED_BYTE, name);
}
}
/*
=================
Brush_MakeFaceWinding
returns the visible polygon on a face
=================
*/
winding_t *Brush_MakeFaceWinding (brush_t *b, face_t *face)
{
winding_t *w;
face_t *clip;
plane_t plane;
qboolean past;
// get a poly that covers an effectively infinite area
w = Winding_BaseForPlane (&face->plane);
// chop the poly by all of the other faces
past = false;
for (clip = b->brush_faces ; clip && w ; clip=clip->next)
{
if (clip == face)
{
past = true;
continue;
}
if (DotProduct (face->plane.normal, clip->plane.normal) > 0.999
&& fabs(face->plane.dist - clip->plane.dist) < 0.01 )
{ // identical plane, use the later one
if (past)
{
free (w);
return NULL;
}
continue;
}
// flip the plane, because we want to keep the back side
VectorSubtract (vec3_origin,clip->plane.normal, plane.normal);
plane.dist = -clip->plane.dist;
w = Winding_Clip (w, &plane, false);
if (!w)
return w;
}
if (w->numpoints < 3)
{
free(w);
w = NULL;
}
if (!w)
printf ("unused plane\n");
return w;
}
void Brush_SnapPlanepts (brush_t *b)
{
int i, j;
face_t *f;
if (g_qeglobals.d_savedinfo.noclamp)
return;
for (f=b->brush_faces ; f; f=f->next)
for (i=0 ; i<3 ; i++)
for (j=0 ; j<3 ; j++)
f->planepts[i][j] = floor (f->planepts[i][j] + 0.5);
}
/*
** Brush_Build
**
** Builds a brush rendering data and also sets the min/max bounds
*/
#define ZERO_EPSILON 0.001
void Brush_Build( brush_t *b )
{
// int order;
// face_t *face;
// winding_t *w;
char title[1024];
if (modified != 1)
{
modified = true; // mark the map as changed
sprintf (title, "%s *", currentmap);
QE_ConvertDOSToUnixName( title, title );
Sys_SetTitle (title);
}
/*
** build the windings and generate the bounding box
*/
Brush_BuildWindings( b );
/*
** move the points and edges if in select mode
*/
if (g_qeglobals.d_select_mode == sel_vertex || g_qeglobals.d_select_mode == sel_edge)
SetupVertexSelection ();
}
/*
=================
Brush_Convex
=================
*/
qboolean Brush_Convex(brush_t *b)
{
face_t *face1, *face2;
for (face1 = b->brush_faces; face1; face1 = face1->next)
{
if (!face1->face_winding)
continue;
for (face2 = b->brush_faces; face2; face2 = face2->next)
{
if (face1 == face2)
continue;
if (!face2->face_winding)
continue;
if (Winding_PlanesConcave(face1->face_winding, face2->face_winding,
face1->plane.normal, face2->plane.normal,
face1->plane.dist, face2->plane.dist))
{
return false;
}
}
}
return true;
}
/*
=================
Brush_MoveVertexes
- The input brush must be convex
- The input brush must have face windings.
- The output brush will be convex.
- Returns true if the WHOLE vertex movement is performed.
=================
*/
#define MAX_MOVE_FACES 64
qboolean Brush_MoveVertex(brush_t *b, vec3_t vertex, vec3_t delta, vec3_t end)
{
face_t *f, *face, *newface, *lastface, *nextface;
face_t *movefaces[MAX_MOVE_FACES];
int movefacepoints[MAX_MOVE_FACES];
winding_t *w, tmpw;
vec3_t start, mid;
plane_t plane;
int i, j, k, nummovefaces, done;
qboolean result;
float dot, front, back, frac, smallestfrac;
result = true;
//
tmpw.numpoints = 3;
tmpw.maxpoints = 3;
VectorCopy(vertex, start);
VectorAdd(vertex, delta, end);
//snap or not?
if (!g_qeglobals.d_savedinfo.noclamp)
for (i = 0; i < 3; i++)
end[i] = floor(end[i] / g_qeglobals.d_gridsize + 0.5) * g_qeglobals.d_gridsize;
//
VectorCopy(end, mid);
//if the start and end are the same
if (Point_Equal(start, end, 0.3))
return false;
//the end point may not be the same as another vertex
for (face = b->brush_faces; face; face = face->next)
{
w = face->face_winding;
if (!w)
continue;
for (i = 0; i < w->numpoints; i++)
{
if (Point_Equal(w->points[i], end, 0.3))
{
VectorCopy(vertex, end);
return false;
}
}
}
//
done = false;
while(!done)
{
//chop off triangles from all brush faces that use the to be moved vertex
//store pointers to these chopped off triangles in movefaces[]
nummovefaces = 0;
for (face = b->brush_faces; face; face = face->next)
{
w = face->face_winding;
if (!w)
continue;
for (i = 0; i < w->numpoints; i++)
{
if (Point_Equal(w->points[i], start, 0.2))
{
if (face->face_winding->numpoints <= 3)
{
movefacepoints[nummovefaces] = i;
movefaces[nummovefaces++] = face;
break;
}
dot = DotProduct(end, face->plane.normal) - face->plane.dist;
//if the end point is in front of the face plane
if (dot > 0.1)
{
//fanout triangle subdivision
for (k = i; k < i + w->numpoints-3; k++)
{
VectorCopy(w->points[i], tmpw.points[0]);
VectorCopy(w->points[(k+1) % w->numpoints], tmpw.points[1]);
VectorCopy(w->points[(k+2) % w->numpoints], tmpw.points[2]);
//
newface = Face_Clone(face);
//get the original
for (f = face; f->original; f = f->original)
;
newface->original = f;
//store the new winding
if (newface->face_winding)
Winding_Free(newface->face_winding);
newface->face_winding = Winding_Clone(&tmpw);
//get the texture
newface->d_texture = Texture_ForName( newface->texdef.name );
//add the face to the brush
newface->next = b->brush_faces;
b->brush_faces = newface;
//add this new triangle to the move faces
movefacepoints[nummovefaces] = 0;
movefaces[nummovefaces++] = newface;
}
//give the original face a new winding
VectorCopy(w->points[(i-2+w->numpoints) % w->numpoints], tmpw.points[0]);
VectorCopy(w->points[(i-1+w->numpoints) % w->numpoints], tmpw.points[1]);
VectorCopy(w->points[i], tmpw.points[2]);
Winding_Free(face->face_winding);
face->face_winding = Winding_Clone(&tmpw);
//add the original face to the move faces
movefacepoints[nummovefaces] = 2;
movefaces[nummovefaces++] = face;
}
else
{
//chop a triangle off the face
VectorCopy(w->points[(i-1+w->numpoints) % w->numpoints], tmpw.points[0]);
VectorCopy(w->points[i], tmpw.points[1]);
VectorCopy(w->points[(i+1) % w->numpoints], tmpw.points[2]);
//remove the point from the face winding
Winding_RemovePoint(w, i);
//get texture crap right
Face_SetColor(b, face);
for (j = 0; j < w->numpoints; j++)
EmitTextureCoordinates(w->points[j], face->d_texture, face);
//make a triangle face
newface = Face_Clone(face);
//get the original
for (f = face; f->original; f = f->original)
;
newface->original = f;
//store the new winding
if (newface->face_winding)
Winding_Free(newface->face_winding);
newface->face_winding = Winding_Clone(&tmpw);
//get the texture
newface->d_texture = Texture_ForName( newface->texdef.name );
//add the face to the brush
newface->next = b->brush_faces;
b->brush_faces = newface;
//
movefacepoints[nummovefaces] = 1;
movefaces[nummovefaces++] = newface;
}
break;
}
}
}
//now movefaces contains pointers to triangle faces that
//contain the to be moved vertex
//
done = true;
VectorCopy(end, mid);
smallestfrac = 1;
for (face = b->brush_faces; face; face = face->next)
{
//check if there is a move face that has this face as the original
for (i = 0; i < nummovefaces; i++)
{
if (movefaces[i]->original == face)
break;
}
if (i >= nummovefaces)
continue;
//check if the original is not a move face itself
for (j = 0; j < nummovefaces; j++)
{
if (face == movefaces[j])
break;
}
//if the original is not a move face itself
if (j >= nummovefaces)
{
memcpy(&plane, &movefaces[i]->original->plane, sizeof(plane_t));
}
else
{
k = movefacepoints[j];
w = movefaces[j]->face_winding;
VectorCopy(w->points[(k+1)%w->numpoints], tmpw.points[0]);
VectorCopy(w->points[(k+2)%w->numpoints], tmpw.points[1]);
//
k = movefacepoints[i];
w = movefaces[i]->face_winding;
VectorCopy(w->points[(k+1)%w->numpoints], tmpw.points[2]);
if (!Plane_FromPoints(tmpw.points[0], tmpw.points[1], tmpw.points[2], &plane))
{
VectorCopy(w->points[(k+2)%w->numpoints], tmpw.points[2]);
if (!Plane_FromPoints(tmpw.points[0], tmpw.points[1], tmpw.points[2], &plane))
//this should never happen otherwise the face merge did a crappy job a previous pass
continue;
}
}
//now we've got the plane to check agains
front = DotProduct(start, plane.normal) - plane.dist;
back = DotProduct(end, plane.normal) - plane.dist;
//if the whole move is at one side of the plane
if (front < 0.01 && back < 0.01)
continue;
if (front > -0.01 && back > -0.01)
continue;
//if there's no movement orthogonal to this plane at all
if (fabs(front-back) < 0.001)
continue;
//ok first only move till the plane is hit
frac = front/(front-back);
if (frac < smallestfrac)
{
mid[0] = start[0] + (end[0] - start[0]) * frac;
mid[1] = start[1] + (end[1] - start[1]) * frac;
mid[2] = start[2] + (end[2] - start[2]) * frac;
smallestfrac = frac;
}
//
done = false;
}
//move the vertex
for (i = 0; i < nummovefaces; i++)
{
//move vertex to end position
VectorCopy(mid, movefaces[i]->face_winding->points[movefacepoints[i]]);
//create new face plane
for (j = 0; j < 3; j++)
{
VectorCopy(movefaces[i]->face_winding->points[j], movefaces[i]->planepts[j]);
}
Face_MakePlane(movefaces[i]);
if (VectorLength(movefaces[i]->plane.normal) < 0.1)
result = false;
}
//if the brush is no longer convex
if (!result || !Brush_Convex(b))
{
for (i = 0; i < nummovefaces; i++)
{
//move the vertex back to the initial position
VectorCopy(start, movefaces[i]->face_winding->points[movefacepoints[i]]);
//create new face plane
for (j = 0; j < 3; j++)
{
VectorCopy(movefaces[i]->face_winding->points[j], movefaces[i]->planepts[j]);
}
Face_MakePlane(movefaces[i]);
}
result = false;
VectorCopy(start, end);
done = true;
}
else
{
VectorCopy(mid, start);
}
//get texture crap right
for (i = 0; i < nummovefaces; i++)
{
Face_SetColor(b, movefaces[i]);
for (j = 0; j < movefaces[i]->face_winding->numpoints; j++)
EmitTextureCoordinates(movefaces[i]->face_winding->points[j], movefaces[i]->d_texture, movefaces[i]);
}
//now try to merge faces with their original faces
lastface = NULL;
for (face = b->brush_faces; face; face = nextface)
{
nextface = face->next;
if (!face->original)
{
lastface = face;
continue;
}
if (!Plane_Equal(&face->plane, &face->original->plane, false))
{
lastface = face;
continue;
}
w = Winding_TryMerge(face->face_winding, face->original->face_winding, face->plane.normal, true);
if (!w)
{
lastface = face;
continue;
}
Winding_Free(face->original->face_winding);
face->original->face_winding = w;
//get texture crap right
Face_SetColor(b, face->original);
for (j = 0; j < face->original->face_winding->numpoints; j++)
EmitTextureCoordinates(face->original->face_winding->points[j], face->original->d_texture, face->original);
//remove the face that was merged with the original
if (lastface)
lastface->next = face->next;
else
b->brush_faces = face->next;
Face_Free(face);
}
}
return result;
}
/*
=================
Brush_ResetFaceOriginals
=================
*/
void Brush_ResetFaceOriginals(brush_t *b)
{
face_t *face;
for (face = b->brush_faces; face; face = face->next)
{
face->original = NULL;
}
}
/*
=================
Brush_Parse
The brush is NOT linked to any list
=================
*/
brush_t *Brush_Parse (void)
{
brush_t *b;
face_t *f;
int i,j;
g_qeglobals.d_parsed_brushes++;
b = Brush_Alloc();
do
{
if (!GetToken (true))
break;
if (!strcmp (token, "}") )
break;
f = Face_Alloc();
// add the brush to the end of the chain, so
// loading and saving a map doesn't reverse the order
f->next = NULL;
if (!b->brush_faces)
{
b->brush_faces = f;