-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodel.c
1108 lines (926 loc) · 31.9 KB
/
model.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 MODEL_C
/* dependencies */
//#include "q3map2.h"
#include "kmap2.h" //add hypov8
/*
PicoPrintFunc()
callback for picomodel.lib
*/
void PicoPrintFunc(int level, const char *str)
{
if(str == NULL)
return;
switch (level)
{
case PICO_NORMAL:
Sys_Printf("%s\n", str);
break;
case PICO_VERBOSE:
Sys_FPrintf(SYS_VRB, "%s\n", str);
break;
case PICO_WARNING:
Sys_Printf("WARNING: %s\n", str);
break;
case PICO_ERROR:
Sys_Printf("ERROR: %s\n", str);
break;
case PICO_FATAL:
Error("ERROR: %s\n", str);
break;
}
}
/*
PicoLoadFileFunc()
callback for picomodel.lib
*/
void PicoLoadFileFunc(char *name, byte ** buffer, int *bufSize)
{
*bufSize = vfsLoadFile((const char *)name, (void **)buffer, 0);
}
/*
FindModel() - ydnar
finds an existing picoModel and returns a pointer to the picoModel_t struct or NULL if not found
*/
picoModel_t *FindModel(char *name, int frame)
{
int i;
/* init */
if(numPicoModels <= 0)
memset(picoModels, 0, sizeof(picoModels));
/* dummy check */
if(name == NULL || name[0] == '\0')
return NULL;
/* search list */
for(i = 0; i < MAX_MODELS; i++)
{
if(picoModels[i] != NULL &&
!strcmp(PicoGetModelName(picoModels[i]), name) && PicoGetModelFrameNum(picoModels[i]) == frame)
return picoModels[i];
}
/* no matching picoModel found */
return NULL;
}
/*
LoadModel() - ydnar
loads a picoModel and returns a pointer to the picoModel_t struct or NULL if not found
*/
picoModel_t *LoadModel(char *name, int frame)
{
int i;
picoModel_t *model, **pm;
/* init */
if(numPicoModels <= 0)
memset(picoModels, 0, sizeof(picoModels));
/* dummy check */
if(name == NULL || name[0] == '\0')
return NULL;
/* try to find existing picoModel */
model = FindModel(name, frame);
if(model != NULL)
return model;
/* none found, so find first non-null picoModel */
pm = NULL;
for(i = 0; i < MAX_MODELS; i++)
{
if(picoModels[i] == NULL)
{
pm = &picoModels[i];
break;
}
}
/* too many picoModels? */
if(pm == NULL)
Error("MAX_MODELS (%d) exceeded, there are too many model files referenced by the map.", MAX_MODELS);
/* attempt to parse model */
*pm = PicoLoadModel((char *)name, frame);
/* if loading failed, make a bogus model to silence the rest of the warnings */
if(*pm == NULL)
{
/* allocate a new model */
*pm = PicoNewModel();
if(*pm == NULL)
return NULL;
/* set data */
PicoSetModelName(*pm, name);
PicoSetModelFrameNum(*pm, frame);
}
/* debug code */
#if 0
{
int numSurfaces, numVertexes;
picoSurface_t *ps;
Sys_Printf("Model %s\n", name);
numSurfaces = PicoGetModelNumSurfaces(*pm);
for(i = 0; i < numSurfaces; i++)
{
ps = PicoGetModelSurface(*pm, i);
numVertexes = PicoGetSurfaceNumVertexes(ps);
Sys_Printf("Surface %d has %d vertexes\n", i, numVertexes);
}
}
#endif
/* set count */
if(*pm != NULL)
numPicoModels++;
/* return the picoModel */
return *pm;
}
/*
InsertModel() - ydnar
adds a picomodel into the bsp
*/
void InsertModel(char *name, int frame, matrix_t transform, matrix_t nTransform, remap_t * remap, shaderInfo_t * celShader, int eNum, int castShadows,
int recvShadows, int spawnFlags, float lightmapScale, int lightmapSampleSize, float shadeAngle, int forceSmoothGroups)
{
int i, j, k, s, numSurfaces;
matrix_t identity;
picoModel_t *model;
picoShader_t *shader;
picoSurface_t *surface;
shaderInfo_t *si;
mapDrawSurface_t *ds;
bspDrawVert_t *dv;
char *picoShaderName;
char shaderName[MAX_QPATH];
picoVec_t *xyz, *normal, *st;
byte *color;
picoIndex_t *indexes;
remap_t *rm, *glob;
double normalEpsilon_save;
double distanceEpsilon_save;
/* get model */
model = LoadModel(name, frame);
if(model == NULL)
return;
/* handle null matrix */
if(transform == NULL)
{
MatrixIdentity(identity);
transform = identity;
}
/* create transform matrix for normals */
#if 0
MatrixCopy(transform, nTransform);
if(MatrixInverse(nTransform))
{
Sys_FPrintf(SYS_VRB, "WARNING: Can't invert model transform matrix, using transpose instead\n");
MatrixTranspose(transform, nTransform);
}
#endif
/* fix bogus lightmap scale */
if(lightmapScale <= 0.0f)
lightmapScale = 1.0f;
/* fix bogus shade angle */
if(shadeAngle <= 0.0f)
shadeAngle = 0.0f;
/* each surface on the model will become a new map drawsurface */
numSurfaces = PicoGetModelNumSurfaces(model);
//% Sys_FPrintf( SYS_VRB, "Model %s has %d surfaces\n", name, numSurfaces );
for(s = 0; s < numSurfaces; s++)
{
/* get surface */
surface = PicoGetModelSurface(model, s);
if(surface == NULL)
continue;
/* only handle triangle surfaces initially (fixme: support patches) */
if(PicoGetSurfaceType(surface) != PICO_TRIANGLES)
continue;
/*hypov8 duplicate...............*/
/* fix the surface's normals */ /*note hypov8 only if normals r wrong???*/
// if (!model->vertNormExist == 1) /*hypov8 only if no vertex normals defined */
//PicoFixSurfaceNormals(surface);
/* allocate a surface (ydnar: gs mods) */
ds = AllocDrawSurface(SURFACE_TRIANGLES);
ds->entityNum = eNum;
ds->castShadows = castShadows;
ds->recvShadows = recvShadows;
/* get shader name */
shader = PicoGetSurfaceShader(surface);
if(shader == NULL)
picoShaderName = "";
else
picoShaderName = PicoGetShaderName(shader);
/* handle shader remapping */
glob = NULL;
for(rm = remap; rm != NULL; rm = rm->next)
{
if(rm->from[0] == '*' && rm->from[1] == '\0')
glob = rm;
else if(!Q_stricmp(picoShaderName, rm->from))
{
Sys_FPrintf(SYS_VRB, "Remapping %s to %s\n", picoShaderName, rm->to);
picoShaderName = rm->to;
glob = NULL;
break;
}
}
if(glob != NULL)
{
Sys_FPrintf(SYS_VRB, "Globbing %s to %s\n", picoShaderName, glob->to);
picoShaderName = glob->to;
}
/* shader renaming for sof2 */
if(renameModelShaders)
{
strcpy(shaderName, picoShaderName);
StripExtension(shaderName);
if(spawnFlags & 1)
strcat(shaderName, "_RMG_BSP");
else
strcat(shaderName, "_BSP");
si = ShaderInfoForShader(shaderName);
}
else
{
si = ShaderInfoForShader(picoShaderName);
// Tr3B: HACK to support the messy Doom 3 materials provided by .ASE files
if(!si->explicitDef)
{
picoShaderName = PicoGetShaderMapName(shader);
Q_strncpyz(shaderName, picoShaderName, sizeof(shaderName));
StripExtension(shaderName);
i = 0;
while(shaderName[i])
{
if(shaderName[i] == '\\')
shaderName[i] = '/';
i++;
}
if(strstr(shaderName, "base/"))
{
si = ShaderInfoForShader(strstr(shaderName, "base/") + strlen("base/"));
Sys_FPrintf(SYS_WRN, "WARNING: Applied .ASE material loader HACK to '%s' -> '%s'\n", picoShaderName, si->shader);
}
}
}
/* set shader */
ds->shaderInfo = si;
/* hypov8 delet collision surface if model is not forced to clip */
if ((si->compileFlags & C_COLLISION) && !(spawnFlags & 2) && !(spawnFlags & 128))
continue;
/* force to meta? */
if((si != NULL && si->forceMeta)
|| (spawnFlags & 4)
|| (meta && !(si->compileFlags & C_VERTEXLIT)) /* hypov8 add meta to every model when using -meta */
) /* 3rd bit */ /* but not if shader specifies pointlight/nolightmap */
/* or models will not show vertex light info (rgbgen vertex) */
ds->type = SURFACE_FORCED_META;
/* fix the surface's normals (jal: conditioned by shader info) */
if(!(spawnFlags & 64) && (shadeAngle == 0.0f || ds->type != SURFACE_FORCED_META))
{ /*add hypov8 conditional fix the surface's normals*/
if (forceSmoothGroups == 1)
{
PicoFixSurfaceNormals(surface);
}
else if (!(model->vertNormExist== 1))
{
PicoFixSurfaceNormals(surface);
}
}
/* set sample size */
if(lightmapSampleSize > 0.0f)
ds->sampleSize = lightmapSampleSize;
/* set lightmap scale */
if(lightmapScale > 0.0f)
ds->lightmapScale = lightmapScale;
/* set shading angle */
if(shadeAngle > 0.0f)
ds->shadeAngleDegrees = shadeAngle;
/* set particulars */
ds->numVerts = PicoGetSurfaceNumVertexes(surface);
ds->verts = safe_malloc(ds->numVerts * sizeof(ds->verts[0]));
memset(ds->verts, 0, ds->numVerts * sizeof(ds->verts[0]));
ds->numIndexes = PicoGetSurfaceNumIndexes(surface);
ds->indexes = safe_malloc(ds->numIndexes * sizeof(ds->indexes[0]));
memset(ds->indexes, 0, ds->numIndexes * sizeof(ds->indexes[0]));
/* copy vertexes */
for(i = 0; i < ds->numVerts; i++)
{
/* get vertex */
dv = &ds->verts[i];
/* xyz and normal */
xyz = PicoGetSurfaceXYZ(surface, i);
VectorCopy(xyz, dv->xyz);
MatrixTransformPoint2(transform, dv->xyz);
normal = PicoGetSurfaceNormal(surface, i);
VectorCopy(normal, dv->normal);
MatrixTransformNormal2(nTransform, dv->normal);
VectorNormalize2(dv->normal, dv->normal);
/* 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)
{
/* project the texture */
dv->st[0] = DotProduct(si->vecs[0], dv->xyz);
dv->st[1] = DotProduct(si->vecs[1], dv->xyz);
}
/* normal texture coordinates */
else
{
st = PicoGetSurfaceST(surface, 0, i);
dv->st[0] = st[0];
dv->st[1] = st[1];
}
/* set lightmap/color bits */
color = PicoGetSurfaceColor(surface, 0, i);
dv->paintColor[0] = color[0] / 255.0f;
dv->paintColor[1] = color[1] / 255.0f;
dv->paintColor[2] = color[2] / 255.0f;
dv->paintColor[3] = color[3] / 255.0f;
for(j = 0; j < MAX_LIGHTMAPS; j++)
{
dv->lightmap[j][0] = 0.0f;
dv->lightmap[j][1] = 0.0f;
dv->lightColor[j][0] = 1; // 255; hypov8 0-1
dv->lightColor[j][1] = 1; //255;
dv->lightColor[j][2] = 1; //255;
dv->lightColor[j][3] = 255;
}
}
/* copy indexes */
indexes = PicoGetSurfaceIndexes(surface, 0);
for(i = 0; i < ds->numIndexes; i++)
ds->indexes[i] = indexes[i];
/* set cel shader */
ds->celShader = celShader;
/* ydnar: giant hack land: generate clipping brushes for model triangles */
if(si->clipModel || (spawnFlags & 2) || (spawnFlags & 128)) /* 2nd bit */
{
vec3_t points[4], backs[3];
vec4_t plane, reverse, pa, pb, pc;
/* temp hack */
if(!si->clipModel && /* not hypov8: C_TRANSLUCENT? this will catch some valid solid textures and remove them for clipping */
(((si->compileFlags & C_TRANSLUCENT) && !(si->compileFlags & C_COLLISION)) || !(si->compileFlags & C_SOLID))) //hypov8
continue;
//add hypov8 spawnflag 128 = 'forceCollsionTexOnlyclip'
//clip parts of model. only via surface collision, skip rest
if((spawnFlags & 128) && !(si->compileFlags & C_COLLISION))
continue; //not a collision forced surface. skip this surface
/* walk triangle list */
for(i = 0; i < ds->numIndexes; i += 3)
{
/* overflow hack */
AUTOEXPAND_BY_REALLOC(mapplanes, (nummapplanes + 64) << 1, allocatedmapplanes, 1024);
/* make points and back points */
for(j = 0; j < 3; j++)
{
/* get vertex */
dv = &ds->verts[ds->indexes[i + j]];
/* copy xyz */
VectorCopy(dv->xyz, points[j]);
VectorCopy(dv->xyz, backs[j]);
/* find nearest axial to normal and push back points opposite */
/* note: this doesn't work as well as simply using the plane of the triangle, below */
for(k = 0; k < 3; k++)
{
if(fabs(dv->normal[k]) >= fabs(dv->normal[(k + 1) % 3]) &&
fabs(dv->normal[k]) >= fabs(dv->normal[(k + 2) % 3]))
{
backs[j][k] += dv->normal[k] < 0.0f ? 64.0f : -64.0f;
break;
}
}
}
VectorCopy(points[0], points[3]); // for cyclic usage
/* make plane for triangle */
// div0: add some extra spawnflags:
// 0: snap normals to axial planes for extrusion
// 8: extrude with the original normals
// 16: extrude only with up/down normals (ideal for terrain)
// 24: extrude by distance zero (may need engine changes)
if(PlaneFromPoints(plane, points[0], points[1], points[2], qtrue))
{
vec3_t bestNormal;
float backPlaneDistance = 2;
if(spawnFlags & 8) // use a DOWN normal
{
if(spawnFlags & 16)
{
// 24: normal as is, and zero width (broken)
VectorCopy(plane, bestNormal);
}
else
{
// 8: normal as is
VectorCopy(plane, bestNormal);
}
}
else
{
if(spawnFlags & 16)
{
// 16: UP/DOWN normal
VectorSet(bestNormal, 0, 0, (plane[2] >= 0 ? 1 : -1));
}
else
{
// 0: axial normal
if(fabs(plane[0]) > fabs(plane[1])) // x>y
if(fabs(plane[1]) > fabs(plane[2])) // x>y, y>z
VectorSet(bestNormal, (plane[0] >= 0 ? 1 : -1), 0, 0);
else // x>y, z>=y
if(fabs(plane[0]) > fabs(plane[2])) // x>z, z>=y
VectorSet(bestNormal, (plane[0] >= 0 ? 1 : -1), 0, 0);
else // z>=x, x>y
VectorSet(bestNormal, 0, 0, (plane[2] >= 0 ? 1 : -1));
else // y>=x
if(fabs(plane[1]) > fabs(plane[2])) // y>z, y>=x
VectorSet(bestNormal, 0, (plane[1] >= 0 ? 1 : -1), 0);
else // z>=y, y>=x
VectorSet(bestNormal, 0, 0, (plane[2] >= 0 ? 1 : -1));
}
}
/* build a brush */
buildBrush = AllocBrush(48);
buildBrush->entityNum = mapEntityNum;
buildBrush->original = buildBrush;
buildBrush->contentShader = si;
buildBrush->compileFlags = si->compileFlags;
buildBrush->contentFlags = si->contentFlags;
buildBrush->generatedClipBrush = qtrue;
normalEpsilon_save = normalEpsilon;
distanceEpsilon_save = distanceEpsilon;
if(si->compileFlags & C_STRUCTURAL) // allow forced structural brushes here
{
buildBrush->detail = qfalse;
// only allow EXACT matches when snapping for these (this is mostly for caulk brushes inside a model)
if(normalEpsilon > 0)
normalEpsilon = 0;
if(distanceEpsilon > 0)
distanceEpsilon = 0;
}
else
buildBrush->detail = qtrue;
/* regenerate back points */
for(j = 0; j < 3; j++)
{
/* get vertex */
dv = &ds->verts[ds->indexes[i + j]];
// shift by some units
VectorMA(dv->xyz, -64.0f, bestNormal, backs[j]); // 64 prevents roundoff errors a bit
}
/* make back plane */
VectorScale(plane, -1.0f, reverse);
reverse[3] = -plane[3];
if((spawnFlags & 24) != 24)
reverse[3] += DotProduct(bestNormal, plane) * backPlaneDistance;
// that's at least sqrt(1/3) backPlaneDistance, unless in DOWN mode; in DOWN mode, we are screwed anyway if we encounter a plane that's perpendicular to the xy plane)
if(PlaneFromPoints(pa, points[2], points[1], backs[1], qtrue) &&
PlaneFromPoints(pb, points[1], points[0], backs[0], qtrue) && PlaneFromPoints(pc, points[0], points[2], backs[2], qtrue))
{
/* set up brush sides */
buildBrush->numsides = 5;
buildBrush->sides[0].shaderInfo = si;
for(j = 1; j < buildBrush->numsides; j++)
buildBrush->sides[j].shaderInfo = NULL; // don't emit these faces as draw surfaces, should make smaller BSPs; hope this works
buildBrush->sides[0].planenum = FindFloatPlane(plane, plane[3], 3, points);
buildBrush->sides[1].planenum = FindFloatPlane(pa, pa[3], 2, &points[1]); // pa contains points[1] and points[2]
buildBrush->sides[2].planenum = FindFloatPlane(pb, pb[3], 2, &points[0]); // pb contains points[0] and points[1]
buildBrush->sides[3].planenum = FindFloatPlane(pc, pc[3], 2, &points[2]); // pc contains points[2] and points[0] (copied to points[3]
buildBrush->sides[4].planenum = FindFloatPlane(reverse, reverse[3], 3, backs);
}
else
{
free(buildBrush);
continue;
}
normalEpsilon = normalEpsilon_save;
distanceEpsilon = distanceEpsilon_save;
/* add to entity */
if(CreateBrushWindings(buildBrush))
{
AddBrushBevels();
//% EmitBrushes( buildBrush, NULL, NULL );
buildBrush->next = entities[mapEntityNum].brushes;
entities[mapEntityNum].brushes = buildBrush;
entities[mapEntityNum].numBrushes++;
}
else
free(buildBrush);
}
}
}
}
}
/*
AddTriangleModel()
*/
void AddTriangleModel(entity_t * e)
{
int frame, castShadows, recvShadows, spawnFlags;
const char *name, *model, *value;
char shader[MAX_QPATH];
shaderInfo_t *celShader;
float temp/*, baseLightmapScale*/, lightmapScale;
float shadeAngle;
int lightmapSampleSize;
vec3_t scale;
matrix_t rotation, transform;
epair_t *ep;
remap_t *remap, *remap2;
char *split;
//add hypov8
int useSmoothGroup;
int forceSmoothGroups = 0;
/* note it */
Sys_FPrintf(SYS_VRB, "--- AddTriangleModel ---\n");
/* get current brush entity name */
name = ValueForKey(e, "name");
/* misc_model entities target non-worldspawn brush model entities */
if(name[0] == '\0')
return;
/* get model name */
model = ValueForKey(e, "model");
if(model[0] == '\0')
return;
/* : skip triggers and other entities */
if(!Q_stricmp(name, model))
return;
/* get model frame */
frame = IntForKey(e, "_frame");
/* worldspawn (and func_groups) default to cast/recv shadows in worldspawn group */
if(e == entities)
{
castShadows = WORLDSPAWN_CAST_SHADOWS;
recvShadows = WORLDSPAWN_RECV_SHADOWS;
}
/* other entities don't cast any shadows, but recv worldspawn shadows */
else
{
castShadows = ENTITY_CAST_SHADOWS;
recvShadows = ENTITY_RECV_SHADOWS;
}
/* get explicit shadow flags */
GetEntityShadowFlags(NULL, e, &castShadows, &recvShadows);
/* get spawnflags */
spawnFlags = IntForKey(e, "spawnflags");
/* : added clipModel option */
spawnFlags |= (IntForKey(e, "clipModel") > 0) ? 2 : 0;
/* : added forceMeta option */
spawnFlags |= (IntForKey(e, "forceMeta") > 0) ? 4 : 0;
/* : added forced colision texture option */ /* hypov8 */
spawnFlags |= (IntForKey(e, "forceCollsionTexOnly") > 0) ? 128 : 0;
/* get scale */
scale[0] = scale[1] = scale[2] = 1.0f;
temp = FloatForKey(e, "modelscale");
if(temp != 0.0f)
scale[0] = scale[1] = scale[2] = temp;
value = ValueForKey(e, "modelscale_vec");
if(value[0] != '\0')
sscanf(value, "%f %f %f", &scale[0], &scale[1], &scale[2]);
/* set transform matrix */
MatrixIdentity(transform);
MatrixMultiplyScale(transform, scale[0], scale[1], scale[2]);
MatrixIdentity(rotation);
/* get shader remappings */
remap = NULL;
for(ep = e->epairs; ep != NULL; ep = ep->next)
{
/* look for keys prefixed with "_remap" */
if(ep->key != NULL && ep->value != NULL &&
ep->key[0] != '\0' && ep->value[0] != '\0' && !Q_strncasecmp(ep->key, "_remap", 6))
{
/* create new remapping */
remap2 = remap;
remap = safe_malloc(sizeof(*remap));
remap->next = remap2;
strcpy(remap->from, ep->value);
/* split the string */
split = strchr(remap->from, ';');
if(split == NULL)
{
Sys_Printf("WARNING: Shader _remap key found in misc_model without a ; character\n");
free(remap);
remap = remap2;
continue;
}
/* store the split */
*split = '\0';
strcpy(remap->to, (split + 1));
/* note it */
//% Sys_FPrintf( SYS_VRB, "Remapping %s to %s\n", remap->from, remap->to );
}
}
/* ydnar: cel shader support */
value = ValueForKey(e, "_celshader");
if(value[0] == '\0')
value = ValueForKey(&entities[0], "_celshader");
if(value[0] != '\0')
{
sprintf(shader, "textures/%s", value);
celShader = ShaderInfoForShader(shader);
}
else
celShader = NULL;
/* get lightmap scale */
/* jal : entity based _samplesize */
lightmapSampleSize = 0;
if(strcmp("", ValueForKey(e, "_lightmapsamplesize")))
lightmapSampleSize = IntForKey(e, "_lightmapsamplesize");
else if(strcmp("", ValueForKey(e, "_samplesize")))
lightmapSampleSize = IntForKey(e, "_samplesize");
if(lightmapSampleSize < 0)
lightmapSampleSize = 0;
if(lightmapSampleSize > 0.0f)
Sys_Printf(" has lightmap sample size of %.d\n", lightmapSampleSize);
/* get lightmap scale */
/* vortex: added _ls key (short name of lightmapscale) */
lightmapScale = 0.0f;
if(strcmp("", ValueForKey(e, "lightmapscale")) ||
strcmp("", ValueForKey(e, "_lightmapscale")) || strcmp("", ValueForKey(e, "_ls")))
{
lightmapScale = FloatForKey(e, "lightmapscale");
if(lightmapScale <= 0.0f)
lightmapScale = FloatForKey(e, "_lightmapscale");
if(lightmapScale <= 0.0f)
lightmapScale = FloatForKey(e, "_ls");
if(lightmapScale < 0.0f)
lightmapScale = 0.0f;
if(lightmapScale > 0.0f)
Sys_Printf("misc_model has lightmap scale of %.4f\n", lightmapScale);
}
/* jal : entity based _shadeangle */
shadeAngle = 0.0f;
if(strcmp("", ValueForKey(e, "_shadeangle")))
shadeAngle = FloatForKey(e, "_shadeangle");
/* vortex' aliases */
else if(strcmp("", ValueForKey(e, "_smoothnormals")))
shadeAngle = FloatForKey(e, "_smoothnormals");
else if(strcmp("", ValueForKey(e, "_sn")))
shadeAngle = FloatForKey(e, "_sn");
else if(strcmp("", ValueForKey(e, "_smooth")))
shadeAngle = FloatForKey(e, "_smooth");
if(shadeAngle < 0.0f)
shadeAngle = 0.0f;
if(shadeAngle > 0.0f)
Sys_Printf("misc_model has shading angle of %.4f\n", shadeAngle);
/* add hypov8 use smothgroups or use vertexnormals*/
/*md3 can only have 1 sg, but normals are set*/
useSmoothGroup = IntForKey(e, "usesmoothgroup");
if (useSmoothGroup == 1)
forceSmoothGroups = 1;
/* insert the model */
InsertModel((char *)model, frame, transform, rotation, remap, celShader, mapEntityNum, castShadows, recvShadows, spawnFlags,
lightmapScale, lightmapSampleSize, shadeAngle, forceSmoothGroups); /*add hypov8 manual smooth*/
/* free shader remappings */
while(remap != NULL)
{
remap2 = remap->next;
free(remap);
remap = remap2;
}
}
/*
AddTriangleModels()
adds misc_model surfaces to the bsp
*/
void AddTriangleModels(entity_t * e)
{
int num, frame, castShadows, recvShadows, spawnFlags;
entity_t *e2;
const char *targetName;
const char *target, *model, *value;
char shader[MAX_QPATH];
shaderInfo_t *celShader;
float temp, baseLightmapScale, lightmapScale;
float shadeAngle;
int lightmapSampleSize;
vec3_t origin, scale, angles;
matrix_t rotation, rotationScaled, transform;
epair_t *ep;
remap_t *remap, *remap2;
char *split;
//add hypov8
int useSmoothGroup;
int forceSmoothGroups = 0; //Default: use vertex normals if they exist. unless SG forged in map
/* note it */
Sys_FPrintf(SYS_VRB, "--- AddTriangleModels ---\n");
/* get current brush entity targetname */
if(e == entities)
targetName = "";
else
{
targetName = ValueForKey(e, "name");
/* misc_model entities target non-worldspawn brush model entities */
if(targetName[0] == '\0')
return;
}
/* get lightmap scale */
/* vortex: added _ls key (short name of lightmapscale) */
baseLightmapScale = 0.0f;
if(strcmp("", ValueForKey(e, "lightmapscale")) ||
strcmp("", ValueForKey(e, "_lightmapscale")) || strcmp("", ValueForKey(e, "_ls")))
{
baseLightmapScale = FloatForKey(e, "lightmapscale");
if(baseLightmapScale <= 0.0f)
baseLightmapScale = FloatForKey(e, "_lightmapscale");
if(baseLightmapScale <= 0.0f)
baseLightmapScale = FloatForKey(e, "_ls");
if(baseLightmapScale < 0.0f)
baseLightmapScale = 0.0f;
if(baseLightmapScale > 0.0f)
Sys_Printf("World Entity has lightmap scale of %.4f\n", baseLightmapScale);
}
/* walk the entity list */
for(num = 1; num < numEntities; num++)
{
/* get e2 */
e2 = &entities[num];
/* convert misc_models into raw geometry */
if(Q_stricmp("misc_model", ValueForKey(e2, "classname")))
continue;
/* ydnar: added support for md3 models on non-worldspawn models */
target = ValueForKey(e2, "target");
if(strcmp(target, targetName))
continue;
/* get model name */
model = ValueForKey(e2, "model");
if(model[0] == '\0')
{
Sys_Printf("WARNING: misc_model at %i %i %i without a model key\n", (int)origin[0], (int)origin[1], (int)origin[2]);
continue;
}
/* get model frame */
frame = IntForKey(e2, "_frame");
/* worldspawn (and func_groups) default to cast/recv shadows in worldspawn group */
if(e == entities)
{
castShadows = WORLDSPAWN_CAST_SHADOWS;
recvShadows = WORLDSPAWN_RECV_SHADOWS;
}
/* other entities don't cast any shadows, but recv worldspawn shadows */
else
{
castShadows = ENTITY_CAST_SHADOWS;
recvShadows = ENTITY_RECV_SHADOWS;
}
/* get explicit shadow flags */
GetEntityShadowFlags(e2, e, &castShadows, &recvShadows);
/* get spawnflags */
spawnFlags = IntForKey(e2, "spawnflags");
/* : added clipModel option */
spawnFlags |= (IntForKey(e2, "clipModel") > 0) ? 2 : 0;
/* : added forceMeta option */
spawnFlags |= (IntForKey(e2, "forceMeta") > 0) ? 4 : 0;
/* : added forced colision texture option */ /* hypov8 */
spawnFlags |= (IntForKey(e2, "forceCollsionTexOnly") > 0) ? 128 : 0;
/* get origin */
GetVectorForKey(e2, "origin", origin);
VectorSubtract(origin, e->origin, origin); /* offset by parent */
/* get "angle" (yaw) or "angles" (pitch yaw roll) */
MatrixIdentity(rotation);
angles[0] = angles[1] = angles[2] = 0.0f;
value = ValueForKey(e2, "angle");
if(value[0] != '\0')
{
angles[1] = atof(value);
MatrixFromAngles(rotation, angles[PITCH], angles[YAW], angles[ROLL]);
}
value = ValueForKey(e2, "angles");
if(value[0] != '\0')
{
sscanf(value, "%f %f %f", &angles[0], &angles[1], &angles[2]);
MatrixFromAngles(rotation, angles[PITCH], angles[YAW], angles[ROLL]);
}
value = ValueForKey(e2, "rotation");
if(value[0] != '\0')
{
sscanf(value, "%f %f %f %f %f %f %f %f %f", &rotation[0], &rotation[1], &rotation[2],
&rotation[4], &rotation[5], &rotation[6], &rotation[8], &rotation[9], &rotation[10]);
}
/* get scale */
scale[0] = scale[1] = scale[2] = 1.0f;
temp = FloatForKey(e2, "modelscale");
if(temp != 0.0f)
scale[0] = scale[1] = scale[2] = temp;
value = ValueForKey(e2, "modelscale_vec");
if(value[0] != '\0')
sscanf(value, "%f %f %f", &scale[0], &scale[1], &scale[2]);
MatrixCopy(rotation, rotationScaled);
MatrixMultiplyScale(rotationScaled, scale[0], scale[1], scale[2]);
/* set transform matrix */
MatrixIdentity(transform);
MatrixSetupTransformFromRotation(transform, rotationScaled, origin);