-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsurface.c
3628 lines (2890 loc) · 87.1 KB
/
surface.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_C
/* dependencies */
//#include "q3map2.h"
#include "kmap2.h" //add hypov8
/*
AllocDrawSurface()
ydnar: gs mods: changed to force an explicit type when allocating
*/
mapDrawSurface_t *AllocDrawSurface(surfaceType_t type)
{
mapDrawSurface_t *ds;
/* ydnar: gs mods: only allocate valid types */
if(type <= SURFACE_BAD || type >= NUM_SURFACE_TYPES)
Error("AllocDrawSurface: Invalid surface type %d specified", type);
/* bounds check */
if(numMapDrawSurfs >= MAX_MAP_DRAW_SURFS)
Error("MAX_MAP_DRAW_SURFS (%d) exceeded", MAX_MAP_DRAW_SURFS);
ds = &mapDrawSurfs[numMapDrawSurfs];
numMapDrawSurfs++;
/* ydnar: do initial surface setup */
memset(ds, 0, sizeof(mapDrawSurface_t));
ds->type = type;
ds->planeNum = -1;
ds->fogNum = defaultFogNum; /* ydnar 2003-02-12 */
ds->outputNum = -1; /* ydnar 2002-08-13 */
ds->surfaceNum = numMapDrawSurfs - 1; /* ydnar 2003-02-16 */
return ds;
}
/*
FinishSurface()
ydnar: general surface finish pass
*/
void FinishSurface(mapDrawSurface_t * ds)
{
mapDrawSurface_t *ds2;
/* dummy check */
if(ds->type <= SURFACE_BAD || ds->type >= NUM_SURFACE_TYPES || ds == NULL || ds->shaderInfo == NULL)
return;
/* ydnar: rocking tek-fu celshading */
if(ds->celShader != NULL)
MakeCelSurface(ds, ds->celShader);
/* backsides stop here */
if(ds->backSide)
return;
/* ydnar: rocking surface cloning (fur baby yeah!) */
if(ds->shaderInfo->cloneShader != NULL && ds->shaderInfo->cloneShader[0] != '\0')
CloneSurface(ds, ShaderInfoForShader(ds->shaderInfo->cloneShader));
/* ydnar: kmap_backShader support */
if(ds->shaderInfo->backShader != NULL && ds->shaderInfo->backShader[0] != '\0')
{
ds2 = CloneSurface(ds, ShaderInfoForShader(ds->shaderInfo->backShader));
ds2->backSide = qtrue;
}
}
/*
CloneSurface()
clones a map drawsurface, using the specified shader
*/
mapDrawSurface_t *CloneSurface(mapDrawSurface_t * src, shaderInfo_t * si)
{
mapDrawSurface_t *ds;
/* dummy check */
if(src == NULL || si == NULL)
return NULL;
/* allocate a new surface */
ds = AllocDrawSurface(src->type);
if(ds == NULL)
return NULL;
/* copy it */
memcpy(ds, src, sizeof(*ds));
/* destroy side reference */
ds->sideRef = NULL;
/* set shader */
ds->shaderInfo = si;
/* copy verts */
if(ds->numVerts > 0)
{
ds->verts = safe_malloc(ds->numVerts * sizeof(*ds->verts));
memcpy(ds->verts, src->verts, ds->numVerts * sizeof(*ds->verts));
}
/* copy indexes */
if(ds->numIndexes <= 0)
return ds;
ds->indexes = safe_malloc(ds->numIndexes * sizeof(*ds->indexes));
memcpy(ds->indexes, src->indexes, ds->numIndexes * sizeof(*ds->indexes));
/* return the surface */
return ds;
}
/*
MakeCelSurface() - ydnar
makes a copy of a surface, but specific to cel shading
*/
mapDrawSurface_t *MakeCelSurface(mapDrawSurface_t * src, shaderInfo_t * si)
{
mapDrawSurface_t *ds;
/* dummy check */
if(src == NULL || si == NULL)
return NULL;
/* don't create cel surfaces for certain types of shaders */
if((src->shaderInfo->compileFlags & C_TRANSLUCENT) || (src->shaderInfo->compileFlags & C_SKY))
return NULL;
/* make a copy */
ds = CloneSurface(src, si);
if(ds == NULL)
return NULL;
/* do some fixups for celshading */
ds->planar = qfalse;
ds->planeNum = -1;
ds->celShader = NULL; /* don't cel shade cels :P */
/* return the surface */
return ds;
}
/*
MakeSkyboxSurface() - ydnar
generates a skybox surface, viewable from everywhere there is sky
*/
mapDrawSurface_t *MakeSkyboxSurface(mapDrawSurface_t * src)
{
int i;
mapDrawSurface_t *ds;
/* dummy check */
if(src == NULL)
return NULL;
/* make a copy */
ds = CloneSurface(src, src->shaderInfo);
if(ds == NULL)
return NULL;
/* set parent */
ds->parent = src;
/* scale the surface vertexes */
for(i = 0; i < ds->numVerts; i++)
{
MatrixTransformPoint2(skyboxTransform, ds->verts[i].xyz);
/* debug code */
//% bspDrawVerts[ bspDrawSurfaces[ ds->outputNum ].firstVert + i ].color[ 0 ][ 1 ] = 0;
//% bspDrawVerts[ bspDrawSurfaces[ ds->outputNum ].firstVert + i ].color[ 0 ][ 2 ] = 0;
}
/* so backface culling creep doesn't bork the surface */
VectorClear(ds->lightmapVecs[2]);
/* return the surface */
return ds;
}
/*
IsTriangleDegenerate
returns qtrue if all three points are colinear, backwards, or the triangle is just plain bogus
*/
#define TINY_AREA 1.0f
qboolean IsTriangleDegenerate(bspDrawVert_t * points, int a, int b, int c)
{
vec3_t v1, v2, v3;
float d;
/* calcuate the area of the triangle */
VectorSubtract(points[b].xyz, points[a].xyz, v1);
VectorSubtract(points[c].xyz, points[a].xyz, v2);
CrossProduct(v1, v2, v3);
d = VectorLength(v3);
/* assume all very small or backwards triangles will cause problems */
if(d < TINY_AREA)
return qtrue;
/* must be a good triangle */
return qfalse;
}
/*
ClearSurface() - ydnar
clears a surface and frees any allocated memory
*/
void ClearSurface(mapDrawSurface_t * ds)
{
ds->type = SURFACE_BAD;
ds->planar = qfalse;
ds->planeNum = -1;
ds->numVerts = 0;
if(ds->verts != NULL)
free(ds->verts);
ds->verts = NULL;
ds->numIndexes = 0;
if(ds->indexes != NULL)
free(ds->indexes);
ds->indexes = NULL;
numClearedSurfaces++;
}
/*
TidyEntitySurfaces() - ydnar
deletes all empty or bad surfaces from the surface list
*/
void TidyEntitySurfaces(entity_t * e)
{
int i, j, deleted;
mapDrawSurface_t *out, *in;
/* note it */
Sys_FPrintf(SYS_VRB, "--- TidyEntitySurfaces ---\n");
/* walk the surface list */
deleted = 0;
for(i = e->firstDrawSurf, j = e->firstDrawSurf; j < numMapDrawSurfs; i++, j++)
{
/* get out surface */
out = &mapDrawSurfs[i];
/* walk the surface list again until a proper surface is found */
for(; j < numMapDrawSurfs; j++)
{
/* get in surface */
in = &mapDrawSurfs[j];
/* this surface ok? */
if(in->type == SURFACE_FLARE || in->type == SURFACE_SHADER || (in->type != SURFACE_BAD && in->numVerts > 0))
break;
/* nuke it */
ClearSurface(in);
deleted++;
}
/* copy if necessary */
if(i != j)
memcpy(out, in, sizeof(mapDrawSurface_t));
}
/* set the new number of drawsurfs */
numMapDrawSurfs = i;
/* emit some stats */
Sys_FPrintf(SYS_VRB, "%9d empty or malformed surfaces deleted\n", deleted);
}
/*
CalcSurfaceTextureRange() - ydnar
calculates the clamped texture range for a given surface, returns qtrue if it's within [-texRange,texRange]
*/
qboolean CalcSurfaceTextureRange(mapDrawSurface_t * ds)
{
int i, j, v, size[2];
float mins[2], maxs[2];
/* try to early out */
if(ds->numVerts <= 0)
return qtrue;
/* walk the verts and determine min/max st values */
mins[0] = 999999;
mins[1] = 999999;
maxs[0] = -999999;
maxs[1] = -999999;
for(i = 0; i < ds->numVerts; i++)
{
for(j = 0; j < 2; j++)
{
if(ds->verts[i].st[j] < mins[j])
mins[j] = ds->verts[i].st[j];
if(ds->verts[i].st[j] > maxs[j])
maxs[j] = ds->verts[i].st[j];
}
}
/* clamp to integer range and calculate surface bias values */
for(j = 0; j < 2; j++)
ds->bias[j] = -floor(0.5f * (mins[j] + maxs[j]));
/* find biased texture coordinate mins/maxs */
size[0] = ds->shaderInfo->shaderWidth;
size[1] = ds->shaderInfo->shaderHeight;
ds->texMins[0] = 999999;
ds->texMins[1] = 999999;
ds->texMaxs[0] = -999999;
ds->texMaxs[1] = -999999;
for(i = 0; i < ds->numVerts; i++)
{
for(j = 0; j < 2; j++)
{
v = ((float)ds->verts[i].st[j] + ds->bias[j]) * size[j];
if(v < ds->texMins[j])
ds->texMins[j] = v;
if(v > ds->texMaxs[j])
ds->texMaxs[j] = v;
}
}
/* calc ranges */
for(j = 0; j < 2; j++)
ds->texRange[j] = (ds->texMaxs[j] - ds->texMins[j]);
/* if range is zero, then assume unlimited precision */
if(texRange == 0)
return qtrue;
/* within range? */
for(j = 0; j < 2; j++)
{
if(ds->texMins[j] < -texRange || ds->texMaxs[j] > texRange)
return qfalse;
}
/* within range */
return qtrue;
}
/*
CalcLightmapAxis() - ydnar
gives closed lightmap axis for a plane normal
*/
qboolean CalcLightmapAxis(vec3_t normal, vec3_t axis)
{
vec3_t absolute;
/* test */
if(normal[0] == 0.0f && normal[1] == 0.0f && normal[2] == 0.0f)
{
VectorClear(axis);
return qfalse;
}
/* get absolute normal */
absolute[0] = fabs(normal[0]);
absolute[1] = fabs(normal[1]);
absolute[2] = fabs(normal[2]);
/* test and set */
if(absolute[2] > absolute[0] - 0.0001f && absolute[2] > absolute[1] - 0.0001f)
{
if(normal[2] > 0.0f)
VectorSet(axis, 0.0f, 0.0f, 1.0f);
else
VectorSet(axis, 0.0f, 0.0f, -1.0f);
}
else if(absolute[0] > absolute[1] - 0.0001f && absolute[0] > absolute[2] - 0.0001f)
{
if(normal[0] > 0.0f)
VectorSet(axis, 1.0f, 0.0f, 0.0f);
else
VectorSet(axis, -1.0f, 0.0f, 0.0f);
}
else
{
if(normal[1] > 0.0f)
VectorSet(axis, 0.0f, 1.0f, 0.0f);
else
VectorSet(axis, 0.0f, -1.0f, 0.0f);
}
/* return ok */
return qtrue;
}
/*
ClassifySurfaces() - ydnar
fills out a bunch of info in the surfaces, including planar status, lightmap projection, and bounding box
*/
#define PLANAR_EPSILON 0.5f //% 0.126f 0.25f
void ClassifySurfaces(int numSurfs, mapDrawSurface_t * ds)
{
int i, bestAxis;
float dist;
vec4_t plane;
shaderInfo_t *si;
static vec3_t axii[6] = {
{0, 0, -1},
{0, 0, 1},
{-1, 0, 0},
{1, 0, 0},
{0, -1, 0},
{0, 1, 0}
};
/* walk the list of surfaces */
for(; numSurfs > 0; numSurfs--, ds++)
{
/* ignore bogus (or flare) surfaces */
if(ds->type == SURFACE_BAD || ds->numVerts <= 0)
continue;
/* get shader */
si = ds->shaderInfo;
/* -----------------------------------------------------------------
force meta if vertex count is too high or shader requires it
----------------------------------------------------------------- */
if(ds->type != SURFACE_PATCH && ds->type != SURFACE_FACE)
{
if(ds->numVerts > SHADER_MAX_VERTEXES)
ds->type = SURFACE_FORCED_META;
}
/* -----------------------------------------------------------------
plane and bounding box classification
----------------------------------------------------------------- */
/* set surface bounding box */
ClearBounds(ds->mins, ds->maxs);
for(i = 0; i < ds->numVerts; i++)
AddPointToBounds(ds->verts[i].xyz, ds->mins, ds->maxs);
/* try to get an existing plane */
if(ds->planeNum >= 0)
{
VectorCopy(mapplanes[ds->planeNum].normal, plane);
plane[3] = mapplanes[ds->planeNum].dist;
}
/* construct one from the first vert with a valid normal */
else
{
VectorClear(plane);
plane[3] = 0.0f;
for(i = 0; i < ds->numVerts; i++)
{
if(ds->verts[i].normal[0] != 0.0f && ds->verts[i].normal[1] != 0.0f && ds->verts[i].normal[2] != 0.0f)
{
VectorCopy(ds->verts[i].normal, plane);
plane[3] = DotProduct(ds->verts[i].xyz, plane);
break;
}
}
}
/* test for bogus plane */
if(VectorLength(plane) <= 0.0f)
{
ds->planar = qfalse;
ds->planeNum = -1;
}
else
{
/* determine if surface is planar */
ds->planar = qtrue;
/* test each vert */
for(i = 0; i < ds->numVerts; i++)
{
/* point-plane test */
dist = DotProduct(ds->verts[i].xyz, plane) - plane[3];
if(fabs(dist) > PLANAR_EPSILON)
{
//% if( ds->planeNum >= 0 )
//% {
//% Sys_Printf( "WARNING: Planar surface marked unplanar (%f > %f)\n", fabs( dist ), PLANAR_EPSILON );
//% ds->verts[ i ].color[ 0 ][ 0 ] = ds->verts[ i ].color[ 0 ][ 2 ] = 0;
//% }
ds->planar = qfalse;
break;
}
}
}
/* find map plane if necessary */
if(ds->planar)
{
if(ds->planeNum < 0)
ds->planeNum = FindFloatPlane(plane, plane[3], 1, &ds->verts[0].xyz);
VectorCopy(plane, ds->lightmapVecs[2]);
}
else
{
ds->planeNum = -1;
VectorClear(ds->lightmapVecs[2]);
//% if( ds->type == SURF_META || ds->type == SURF_FACE )
//% Sys_Printf( "WARNING: Non-planar face (%d): %s\n", ds->planeNum, ds->shaderInfo->shader );
}
/* -----------------------------------------------------------------
lightmap bounds and axis projection
----------------------------------------------------------------- */
/* vertex lit surfaces don't need this information */
if(si->compileFlags & C_VERTEXLIT || ds->type == SURFACE_TRIANGLES)
{
VectorClear(ds->lightmapAxis);
//% VectorClear( ds->lightmapVecs[ 2 ] );
ds->sampleSize = 0;
continue;
}
/* the shader can specify an explicit lightmap axis */
if(si->lightmapAxis[0] || si->lightmapAxis[1] || si->lightmapAxis[2])
VectorCopy(si->lightmapAxis, ds->lightmapAxis);
else if(ds->type == SURFACE_FORCED_META)
VectorClear(ds->lightmapAxis);
else if(ds->planar)
CalcLightmapAxis(plane, ds->lightmapAxis);
else
{
/* find best lightmap axis */
for(bestAxis = 0; bestAxis < 6; bestAxis++)
{
for(i = 0; i < ds->numVerts && bestAxis < 6; i++)
{
//% Sys_Printf( "Comparing %1.3f %1.3f %1.3f to %1.3f %1.3f %1.3f\n",
//% ds->verts[ i ].normal[ 0 ], ds->verts[ i ].normal[ 1 ], ds->verts[ i ].normal[ 2 ],
//% axii[ bestAxis ][ 0 ], axii[ bestAxis ][ 1 ], axii[ bestAxis ][ 2 ] );
if(DotProduct(ds->verts[i].normal, axii[bestAxis]) < 0.25f) /* fixme: adjust this tolerance to taste */
break;
}
if(i == ds->numVerts)
break;
}
/* set axis if possible */
if(bestAxis < 6)
{
//% if( ds->type == SURFACE_PATCH )
//% Sys_Printf( "Mapped axis %d onto patch\n", bestAxis );
VectorCopy(axii[bestAxis], ds->lightmapAxis);
}
/* debug code */
//% if( ds->type == SURFACE_PATCH )
//% Sys_Printf( "Failed to map axis %d onto patch\n", bestAxis );
}
/* calculate lightmap sample size */
if(ds->shaderInfo->lightmapSampleSize > 0) /* shader value overrides every other */
ds->sampleSize = ds->shaderInfo->lightmapSampleSize;
else if(ds->sampleSize <= 0) /* may contain the entity asigned value */
ds->sampleSize = sampleSize; /* otherwise use global default */
if(ds->lightmapScale > 0.0f) /* apply surface lightmap scaling factor */
{
ds->sampleSize = ds->lightmapScale * (float)ds->sampleSize;
ds->lightmapScale = 0; /* applied */
}
if(ds->sampleSize < minSampleSize)
ds->sampleSize = minSampleSize;
if(ds->sampleSize < 1)
ds->sampleSize = 1;
if(ds->sampleSize > 16384) /* powers of 2 are preferred */
ds->sampleSize = 16384;
}
}
/*
ClassifyEntitySurfaces() - ydnar
classifies all surfaces in an entity
*/
void ClassifyEntitySurfaces(entity_t * e)
{
int i;
/* note it */
Sys_FPrintf(SYS_VRB, "--- ClassifyEntitySurfaces ---\n");
/* walk the surface list */
for(i = e->firstDrawSurf; i < numMapDrawSurfs; i++)
{
FinishSurface(&mapDrawSurfs[i]);
ClassifySurfaces(1, &mapDrawSurfs[i]);
}
/* tidy things up */
TidyEntitySurfaces(e);
}
/*
GetShaderIndexForPoint() - ydnar
for shader-indexed surfaces (terrain), find a matching index from the indexmap
*/
byte GetShaderIndexForPoint(indexMap_t * im, vec3_t eMins, vec3_t eMaxs, vec3_t point)
{
int i, x, y;
float s, t;
vec3_t mins, maxs, size;
/* early out if no indexmap */
if(im == NULL)
return 0;
/* this code is really broken */
#if 0
/* legacy precision fudges for terrain */
for(i = 0; i < 3; i++)
{
mins[i] = floor(eMins[i] + 0.1);
maxs[i] = floor(eMaxs[i] + 0.1);
size[i] = maxs[i] - mins[i];
}
/* find st (fixme: support more than just z-axis projection) */
s = floor(point[0] + 0.1f - mins[0]) / size[0];
t = floor(maxs[1] - point[1] + 0.1f) / size[1];
if(s < 0.0f)
s = 0.0f;
else if(s > 1.0f)
s = 1.0f;
if(t < 0.0f)
t = 0.0f;
else if(t > 1.0f)
t = 1.0f;
/* make xy */
x = (im->w - 1) * s;
y = (im->h - 1) * t;
#else
/* get size */
for(i = 0; i < 3; i++)
{
mins[i] = eMins[i];
maxs[i] = eMaxs[i];
size[i] = maxs[i] - mins[i];
}
/* calc st */
s = (point[0] - mins[0]) / size[0];
t = (maxs[1] - point[1]) / size[1];
/* calc xy */
x = s * im->w;
y = t * im->h;
if(x < 0)
x = 0;
else if(x > (im->w - 1))
x = (im->w - 1);
if(y < 0)
y = 0;
else if(y > (im->h - 1))
y = (im->h - 1);
#endif
/* return index */
return im->pixels[y * im->w + x];
}
/*
GetIndexedShader() - ydnar
for a given set of indexes and an indexmap, get a shader and set the vertex alpha in-place
this combines a couple different functions from terrain.c
*/
shaderInfo_t *GetIndexedShader(shaderInfo_t * parent, indexMap_t * im, int numPoints, byte * shaderIndexes)
{
int i;
byte minShaderIndex, maxShaderIndex;
char shader[MAX_QPATH];
shaderInfo_t *si;
/* early out if bad data */
if(im == NULL || numPoints <= 0 || shaderIndexes == NULL)
return ShaderInfoForShader("default");
/* determine min/max index */
minShaderIndex = 255;
maxShaderIndex = 0;
for(i = 0; i < numPoints; i++)
{
if(shaderIndexes[i] < minShaderIndex)
minShaderIndex = shaderIndexes[i];
if(shaderIndexes[i] > maxShaderIndex)
maxShaderIndex = shaderIndexes[i];
}
/* set alpha inline */
for(i = 0; i < numPoints; i++)
{
/* straight rip from terrain.c */
if(shaderIndexes[i] < maxShaderIndex)
shaderIndexes[i] = 0;
else
shaderIndexes[i] = 255;
}
/* make a shader name */
if(minShaderIndex == maxShaderIndex)
sprintf(shader, "textures/%s_%d", im->shader, maxShaderIndex);
else
sprintf(shader, "textures/%s_%dto%d", im->shader, minShaderIndex, maxShaderIndex);
/* get the shader */
si = ShaderInfoForShader(shader);
/* inherit a few things from parent shader */
if(parent->globalTexture)
si->globalTexture = qtrue;
if(parent->forceMeta)
si->forceMeta = qtrue;
if(parent->nonplanar)
si->nonplanar = qtrue;
if(si->shadeAngleDegrees == 0.0)
si->shadeAngleDegrees = parent->shadeAngleDegrees;
if(parent->tcGen && si->tcGen == qfalse)
{
/* set xy texture projection */
si->tcGen = qtrue;
VectorCopy(parent->vecs[0], si->vecs[0]);
VectorCopy(parent->vecs[1], si->vecs[1]);
}
if(VectorLength(parent->lightmapAxis) > 0.0f && VectorLength(si->lightmapAxis) <= 0.0f)
{
/* set lightmap projection axis */
VectorCopy(parent->lightmapAxis, si->lightmapAxis);
}
/* return the shader */
return si;
}
/*
DrawSurfaceForSide()
creates a SURF_FACE drawsurface from a given brush side and winding
*/
#define SNAP_FLOAT_TO_INT 8
#define SNAP_INT_TO_FLOAT (1.0 / SNAP_FLOAT_TO_INT)
mapDrawSurface_t *DrawSurfaceForSide(entity_t * e, brush_t * b, side_t * s, winding_t * w)
{
int i, j, k;
mapDrawSurface_t *ds;
shaderInfo_t *si, *parent;
bspDrawVert_t *dv;
vec3_t texX, texY;
vec_t x, y;
vec3_t vTranslated;
qboolean indexed;
byte shaderIndexes[256];
float offsets[256];
char tempShader[MAX_QPATH];
/* ydnar: don't make a drawsurf for culled sides */
if(s->culled)
return NULL;
/* range check */
if(w->numpoints > MAX_POINTS_ON_WINDING)
Error("DrawSurfaceForSide: w->numpoints = %d (> %d)", w->numpoints, MAX_POINTS_ON_WINDING);
/* get shader */
si = s->shaderInfo;
/* ydnar: gs mods: check for indexed shader */
if(si->indexed && b->im != NULL)
{
/* indexed */
indexed = qtrue;
/* get shader indexes for each point */
for(i = 0; i < w->numpoints; i++)
{
shaderIndexes[i] = GetShaderIndexForPoint(b->im, b->eMins, b->eMaxs, w->p[i]);
offsets[i] = b->im->offsets[shaderIndexes[i]];
//% Sys_Printf( "%f ", offsets[ i ] );
}
/* get matching shader and set alpha */
parent = si;
si = GetIndexedShader(parent, b->im, w->numpoints, shaderIndexes);
}
else
indexed = qfalse;
/* ydnar: sky hack/fix for GL_CLAMP borders on ati cards */
if(skyFixHack && si->skyParmsImageBase[0] != '\0')
{
//% Sys_FPrintf( SYS_VRB, "Enabling sky hack for shader %s using env %s\n", si->shader, si->skyParmsImageBase );
sprintf(tempShader, "%s_lf", si->skyParmsImageBase);
DrawSurfaceForShader(tempShader);
sprintf(tempShader, "%s_rt", si->skyParmsImageBase);
DrawSurfaceForShader(tempShader);
sprintf(tempShader, "%s_ft", si->skyParmsImageBase);
DrawSurfaceForShader(tempShader);
sprintf(tempShader, "%s_bk", si->skyParmsImageBase);
DrawSurfaceForShader(tempShader);
sprintf(tempShader, "%s_up", si->skyParmsImageBase);
DrawSurfaceForShader(tempShader);
sprintf(tempShader, "%s_dn", si->skyParmsImageBase);
DrawSurfaceForShader(tempShader);
}
/* ydnar: gs mods */
ds = AllocDrawSurface(SURFACE_FACE);
ds->entityNum = b->entityNum;
ds->castShadows = b->castShadows;
ds->recvShadows = b->recvShadows;
ds->planar = qtrue;
ds->planeNum = s->planenum;
VectorCopy(mapplanes[s->planenum].normal, ds->lightmapVecs[2]);
ds->shaderInfo = si;
ds->mapBrush = b;
ds->sideRef = AllocSideRef(s, NULL);
ds->fogNum = -1;
ds->sampleSize = b->lightmapSampleSize;
ds->lightmapScale = b->lightmapScale;
ds->numVerts = w->numpoints;
ds->verts = safe_malloc(ds->numVerts * sizeof(*ds->verts));
memset(ds->verts, 0, ds->numVerts * sizeof(*ds->verts));
/* compute s/t coordinates from brush primitive texture matrix (compute axis base) */
ComputeAxisBase(mapplanes[s->planenum].normal, texX, texY);
/* create the vertexes */
for(j = 0; j < w->numpoints; j++)
{
/* get the drawvert */
dv = ds->verts + j;
/* copy xyz and do potential z offset */
VectorCopy(w->p[j], dv->xyz);
if(indexed)
dv->xyz[2] += offsets[j];
/* round the xyz to a given precision and translate by origin */
for(i = 0; i < 3; i++)
dv->xyz[i] = SNAP_INT_TO_FLOAT * floor(dv->xyz[i] * SNAP_FLOAT_TO_INT + 0.5f);
VectorAdd(dv->xyz, e->origin, vTranslated);
/* ydnar: tek-fu celshading support for flat shaded shit */
if(flat)
{
dv->st[0] = si->stFlat[0];
dv->st[1] = si->stFlat[1];
}
/* ydnar: gs mods: added support for explicit shader texcoord generation */
else if(si->tcGen)
{
dv->st[0] = DotProduct(si->vecs[0], vTranslated);
dv->st[1] = DotProduct(si->vecs[1], vTranslated);
}
/* old quake-style texturing */
else if(g_bBrushPrimit == BPRIMIT_OLDBRUSHES)
{
/* nearest-axial projection */
dv->st[0] = s->vecs[0][3] + DotProduct(s->vecs[0], vTranslated);
dv->st[1] = s->vecs[1][3] + DotProduct(s->vecs[1], vTranslated);
dv->st[0] /= si->shaderWidth;
dv->st[1] /= si->shaderHeight;
}
/* brush primitive texturing */
else
{
/* calculate texture s/t from brush primitive texture matrix */
x = DotProduct(vTranslated, texX);
y = DotProduct(vTranslated, texY);
dv->st[0] = s->texMat[0][0] * x + s->texMat[0][1] * y + s->texMat[0][2];
dv->st[1] = s->texMat[1][0] * x + s->texMat[1][1] * y + s->texMat[1][2];
}
/* copy normal */
VectorCopy(mapplanes[s->planenum].normal, dv->normal);
/* ydnar: set color */
dv->paintColor[0] = 1.0f;
dv->paintColor[1] = 1.0f;
dv->paintColor[2] = 1.0f;
dv->paintColor[3] = (indexed ? shaderIndexes[i] : 1);
for(k = 0; k < MAX_LIGHTMAPS; k++)
{
dv->lightColor[k][0] = 255;
dv->lightColor[k][1] = 255;
dv->lightColor[k][2] = 255;
/* ydnar: gs mods: handle indexed shader blending */
dv->lightColor[k][3] = (indexed ? shaderIndexes[j] : 255);
}
}
/* set cel shader */
ds->celShader = b->celShader;
/* set shade angle */
if(b->shadeAngleDegrees > 0.0f)
ds->shadeAngleDegrees = b->shadeAngleDegrees;