forked from JACoders/OpenJK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtr_ghoul2.cpp
3909 lines (3423 loc) · 115 KB
/
tr_ghoul2.cpp
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) 2000 - 2013, Raven Software, Inc.
Copyright (C) 2001 - 2013, Activision, Inc.
Copyright (C) 2013 - 2015, OpenJK contributors
This file is part of the OpenJK source code.
OpenJK is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License version 2 as
published by the Free Software Foundation.
This program 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 this program; if not, see <http://www.gnu.org/licenses/>.
===========================================================================
*/
#include "../server/exe_headers.h"
#include "../client/client.h" //FIXME!! EVIL - just include the definitions needed
#include "../client/vmachine.h"
#if !defined(TR_LOCAL_H)
#include "tr_local.h"
#endif
#include "qcommon/matcomp.h"
#if !defined(_QCOMMON_H_)
#include "../qcommon/qcommon.h"
#endif
#if !defined(G2_H_INC)
#include "../ghoul2/G2.h"
#endif
#ifdef _G2_GORE
#include "../ghoul2/ghoul2_gore.h"
#endif
#define LL(x) x=LittleLong(x)
#define LS(x) x=LittleShort(x)
#define LF(x) x=LittleFloat(x)
#ifdef G2_PERFORMANCE_ANALYSIS
#include "../qcommon/timing.h"
timing_c G2PerformanceTimer_RB_SurfaceGhoul;
int G2PerformanceCounter_G2_TransformGhoulBones = 0;
int G2Time_RB_SurfaceGhoul = 0;
void G2Time_ResetTimers(void)
{
G2Time_RB_SurfaceGhoul = 0;
G2PerformanceCounter_G2_TransformGhoulBones = 0;
}
void G2Time_ReportTimers(void)
{
Com_Printf("\n---------------------------------\nRB_SurfaceGhoul: %i\nTransformGhoulBones calls: %i\n---------------------------------\n\n",
G2Time_RB_SurfaceGhoul,
G2PerformanceCounter_G2_TransformGhoulBones
);
}
#endif
//rww - RAGDOLL_BEGIN
#include <float.h>
//rww - RAGDOLL_END
extern cvar_t *r_Ghoul2UnSqash;
extern cvar_t *r_Ghoul2AnimSmooth;
extern cvar_t *r_Ghoul2NoLerp;
extern cvar_t *r_Ghoul2NoBlend;
extern cvar_t *r_Ghoul2UnSqashAfterSmooth;
bool HackadelicOnClient=false; // means this is a render traversal
// I hate doing this, but this is the simplest way to get this into the routines it needs to be
mdxaBone_t worldMatrix;
mdxaBone_t worldMatrixInv;
#ifdef _G2_GORE
qhandle_t goreShader=-1;
#endif
const static mdxaBone_t identityMatrix =
{
{
{ 0.0f, -1.0f, 0.0f, 0.0f },
{ 1.0f, 0.0f, 0.0f, 0.0f },
{ 0.0f, 0.0f, 1.0f, 0.0f }
}
};
class CTransformBone
{
public:
//rww - RAGDOLL_BEGIN
int touchRender;
//rww - RAGDOLL_END
mdxaBone_t boneMatrix; //final matrix
int parent; // only set once
int touch; // for minimal recalculation
CTransformBone()
{
touch=0;
//rww - RAGDOLL_BEGIN
touchRender = 0;
//rww - RAGDOLL_END
}
};
struct SBoneCalc
{
int newFrame;
int currentFrame;
float backlerp;
float blendFrame;
int blendOldFrame;
bool blendMode;
float blendLerp;
};
class CBoneCache;
void G2_TransformBone(int index,CBoneCache &CB);
class CBoneCache
{
void EvalLow(int index)
{
assert(index>=0&&index<mNumBones);
if (mFinalBones[index].touch!=mCurrentTouch)
{
// need to evaluate the bone
assert((mFinalBones[index].parent>=0&&mFinalBones[index].parent<mNumBones)||(index==0&&mFinalBones[index].parent==-1));
if (mFinalBones[index].parent>=0)
{
EvalLow(mFinalBones[index].parent); // make sure parent is evaluated
SBoneCalc &par=mBones[mFinalBones[index].parent];
mBones[index].newFrame=par.newFrame;
mBones[index].currentFrame=par.currentFrame;
mBones[index].backlerp=par.backlerp;
mBones[index].blendFrame=par.blendFrame;
mBones[index].blendOldFrame=par.blendOldFrame;
mBones[index].blendMode=par.blendMode;
mBones[index].blendLerp=par.blendLerp;
}
G2_TransformBone(index,*this);
mFinalBones[index].touch=mCurrentTouch;
}
}
//rww - RAGDOLL_BEGIN
void SmoothLow(int index)
{
if (mSmoothBones[index].touch==mLastTouch)
{
int i;
float *oldM=&mSmoothBones[index].boneMatrix.matrix[0][0];
float *newM=&mFinalBones[index].boneMatrix.matrix[0][0];
for (i=0;i<12;i++,oldM++,newM++)
{
*oldM=mSmoothFactor*(*oldM-*newM)+*newM;
}
}
else
{
memcpy(&mSmoothBones[index].boneMatrix,&mFinalBones[index].boneMatrix,sizeof(mdxaBone_t));
}
mdxaSkelOffsets_t *offsets = (mdxaSkelOffsets_t *)((byte *)header + sizeof(mdxaHeader_t));
mdxaSkel_t *skel = (mdxaSkel_t *)((byte *)header + sizeof(mdxaHeader_t) + offsets->offsets[index]);
mdxaBone_t tempMatrix;
Multiply_3x4Matrix(&tempMatrix,&mSmoothBones[index].boneMatrix, &skel->BasePoseMat);
float maxl;
maxl=VectorLength(&skel->BasePoseMat.matrix[0][0]);
VectorNormalize(&tempMatrix.matrix[0][0]);
VectorNormalize(&tempMatrix.matrix[1][0]);
VectorNormalize(&tempMatrix.matrix[2][0]);
VectorScale(&tempMatrix.matrix[0][0],maxl,&tempMatrix.matrix[0][0]);
VectorScale(&tempMatrix.matrix[1][0],maxl,&tempMatrix.matrix[1][0]);
VectorScale(&tempMatrix.matrix[2][0],maxl,&tempMatrix.matrix[2][0]);
Multiply_3x4Matrix(&mSmoothBones[index].boneMatrix,&tempMatrix,&skel->BasePoseMatInv);
// Added by BTO (VV) - I hope this is right.
mSmoothBones[index].touch=mCurrentTouch;
#ifdef _DEBUG
for ( int i = 0; i < 3; i++ )
{
for ( int j = 0; j < 4; j++ )
{
assert( !Q_isnan(mSmoothBones[index].boneMatrix.matrix[i][j]));
}
}
#endif// _DEBUG
}
//rww - RAGDOLL_END
public:
int frameSize;
const mdxaHeader_t *header;
const model_t *mod;
// these are split for better cpu cache behavior
SBoneCalc *mBones;
CTransformBone *mFinalBones;
CTransformBone *mSmoothBones; // for render smoothing
mdxaSkel_t **mSkels;
int mNumBones;
boneInfo_v *rootBoneList;
mdxaBone_t rootMatrix;
int incomingTime;
int mCurrentTouch;
//rww - RAGDOLL_BEGIN
int mCurrentTouchRender;
int mLastTouch;
int mLastLastTouch;
//rww - RAGDOLL_END
// for render smoothing
bool mSmoothingActive;
bool mUnsquash;
float mSmoothFactor;
// int mWraithID; // this is just used for debug prints, can use it for any int of interest in JK2
CBoneCache(const model_t *amod,const mdxaHeader_t *aheader) :
header(aheader),
mod(amod)
{
assert(amod);
assert(aheader);
mSmoothingActive=false;
mUnsquash=false;
mSmoothFactor=0.0f;
mNumBones = header->numBones;
mBones = new SBoneCalc[mNumBones];
mFinalBones = (CTransformBone*)Z_Malloc(sizeof(CTransformBone) * mNumBones, TAG_GHOUL2, qtrue, 16);
mSmoothBones = (CTransformBone*)Z_Malloc(sizeof(CTransformBone) * mNumBones, TAG_GHOUL2, qtrue, 16);
mSkels = new mdxaSkel_t*[mNumBones];
mdxaSkelOffsets_t *offsets;
mdxaSkel_t *skel;
offsets = (mdxaSkelOffsets_t *)((byte *)header + sizeof(mdxaHeader_t));
int i;
for (i=0;i<mNumBones;i++)
{
skel = (mdxaSkel_t *)((byte *)header + sizeof(mdxaHeader_t) + offsets->offsets[i]);
mSkels[i]=skel;
mFinalBones[i].parent=skel->parent;
}
mCurrentTouch=3;
//rww - RAGDOLL_BEGIN
mLastTouch=2;
mLastLastTouch=1;
//rww - RAGDOLL_END
}
~CBoneCache ()
{
delete [] mBones;
// Alignment
Z_Free(mFinalBones);
Z_Free(mSmoothBones);
delete [] mSkels;
}
SBoneCalc &Root()
{
assert(mNumBones);
return mBones[0];
}
const mdxaBone_t &EvalUnsmooth(int index)
{
EvalLow(index);
if (mSmoothingActive&&mSmoothBones[index].touch)
{
return mSmoothBones[index].boneMatrix;
}
return mFinalBones[index].boneMatrix;
}
const mdxaBone_t &Eval(int index)
{
/*
bool wasEval=EvalLow(index);
if (mSmoothingActive)
{
if (mSmoothBones[index].touch!=incomingTime||wasEval)
{
float dif=float(incomingTime)-float(mSmoothBones[index].touch);
if (mSmoothBones[index].touch&&dif<300.0f)
{
if (dif<16.0f) // 60 fps
{
dif=16.0f;
}
if (dif>100.0f) // 10 fps
{
dif=100.0f;
}
float f=1.0f-pow(1.0f-mSmoothFactor,16.0f/dif);
int i;
float *oldM=&mSmoothBones[index].boneMatrix.matrix[0][0];
float *newM=&mFinalBones[index].boneMatrix.matrix[0][0];
for (i=0;i<12;i++,oldM++,newM++)
{
*oldM=f*(*oldM-*newM)+*newM;
}
if (mUnsquash)
{
mdxaBone_t tempMatrix;
Multiply_3x4Matrix(&tempMatrix,&mSmoothBones[index].boneMatrix, &mSkels[index]->BasePoseMat);
float maxl;
maxl=VectorLength(&mSkels[index]->BasePoseMat.matrix[0][0]);
VectorNormalizeFast(&tempMatrix.matrix[0][0]);
VectorNormalizeFast(&tempMatrix.matrix[1][0]);
VectorNormalizeFast(&tempMatrix.matrix[2][0]);
VectorScale(&tempMatrix.matrix[0][0],maxl,&tempMatrix.matrix[0][0]);
VectorScale(&tempMatrix.matrix[1][0],maxl,&tempMatrix.matrix[1][0]);
VectorScale(&tempMatrix.matrix[2][0],maxl,&tempMatrix.matrix[2][0]);
Multiply_3x4Matrix(&mSmoothBones[index].boneMatrix,&tempMatrix,&mSkels[index]->BasePoseMatInv);
}
}
else
{
memcpy(&mSmoothBones[index].boneMatrix,&mFinalBones[index].boneMatrix,sizeof(mdxaBone_t));
}
mSmoothBones[index].touch=incomingTime;
}
return mSmoothBones[index].boneMatrix;
}
return mFinalBones[index].boneMatrix;
*/
//all above is not necessary, smoothing is taken care of when we want to use smoothlow (only when evalrender)
assert(index>=0&&index<mNumBones);
if (mFinalBones[index].touch!=mCurrentTouch)
{
EvalLow(index);
}
return mFinalBones[index].boneMatrix;
}
//rww - RAGDOLL_BEGIN
const inline mdxaBone_t &EvalRender(int index)
{
assert(index>=0&&index<mNumBones);
if (mFinalBones[index].touch!=mCurrentTouch)
{
mFinalBones[index].touchRender=mCurrentTouchRender;
EvalLow(index);
}
if (mSmoothingActive)
{
if (mSmoothBones[index].touch!=mCurrentTouch)
{
SmoothLow(index);
}
return mSmoothBones[index].boneMatrix;
}
return mFinalBones[index].boneMatrix;
}
//rww - RAGDOLL_END
//rww - RAGDOLL_BEGIN
bool WasRendered(int index)
{
assert(index>=0&&index<mNumBones);
return mFinalBones[index].touchRender==mCurrentTouchRender;
}
int GetParent(int index)
{
if (index==0)
{
return -1;
}
assert(index>=0&&index<mNumBones);
return mFinalBones[index].parent;
}
//rww - RAGDOLL_END
// Added by BTO (VV) - This is probably broken
// Need to add in smoothing step?
CTransformBone *EvalFull(int index)
{
// Eval(index);
EvalRender(index);
if (mSmoothingActive)
{
return mSmoothBones + index;
}
return mFinalBones + index;
}
};
static inline float G2_GetVertBoneWeightNotSlow( const mdxmVertex_t *pVert, const int iWeightNum)
{
float fBoneWeight;
int iTemp = pVert->BoneWeightings[iWeightNum];
iTemp|= (pVert->uiNmWeightsAndBoneIndexes >> (iG2_BONEWEIGHT_TOPBITS_SHIFT+(iWeightNum*2)) ) & iG2_BONEWEIGHT_TOPBITS_AND;
fBoneWeight = fG2_BONEWEIGHT_RECIPROCAL_MULT * iTemp;
return fBoneWeight;
}
//rww - RAGDOLL_BEGIN
const mdxaHeader_t *G2_GetModA(CGhoul2Info &ghoul2)
{
if (!ghoul2.mBoneCache)
{
return 0;
}
CBoneCache &boneCache=*ghoul2.mBoneCache;
return boneCache.header;
}
int G2_GetBoneDependents(CGhoul2Info &ghoul2,int boneNum,int *tempDependents,int maxDep)
{
// fixme, these should be precomputed
if (!ghoul2.mBoneCache||!maxDep)
{
return 0;
}
CBoneCache &boneCache=*ghoul2.mBoneCache;
mdxaSkel_t *skel;
mdxaSkelOffsets_t *offsets;
offsets = (mdxaSkelOffsets_t *)((byte *)boneCache.header + sizeof(mdxaHeader_t));
skel = (mdxaSkel_t *)((byte *)boneCache.header + sizeof(mdxaHeader_t) + offsets->offsets[boneNum]);
int i;
int ret=0;
for (i=0;i<skel->numChildren;i++)
{
if (!maxDep)
{
return i; // number added
}
*tempDependents=skel->children[i];
assert(*tempDependents>0&&*tempDependents<boneCache.header->numBones);
maxDep--;
tempDependents++;
ret++;
}
for (i=0;i<skel->numChildren;i++)
{
int num=G2_GetBoneDependents(ghoul2,skel->children[i],tempDependents,maxDep);
tempDependents+=num;
ret+=num;
maxDep-=num;
assert(maxDep>=0);
if (!maxDep)
{
break;
}
}
return ret;
}
bool G2_WasBoneRendered(CGhoul2Info &ghoul2,int boneNum)
{
if (!ghoul2.mBoneCache)
{
return false;
}
CBoneCache &boneCache=*ghoul2.mBoneCache;
return boneCache.WasRendered(boneNum);
}
void G2_GetBoneBasepose(CGhoul2Info &ghoul2,int boneNum,mdxaBone_t *&retBasepose,mdxaBone_t *&retBaseposeInv)
{
if (!ghoul2.mBoneCache)
{
// yikes
retBasepose=const_cast<mdxaBone_t *>(&identityMatrix);
retBaseposeInv=const_cast<mdxaBone_t *>(&identityMatrix);
return;
}
assert(ghoul2.mBoneCache);
CBoneCache &boneCache=*ghoul2.mBoneCache;
assert(boneCache.mod);
assert(boneNum>=0&&boneNum<boneCache.header->numBones);
mdxaSkel_t *skel;
mdxaSkelOffsets_t *offsets;
offsets = (mdxaSkelOffsets_t *)((byte *)boneCache.header + sizeof(mdxaHeader_t));
skel = (mdxaSkel_t *)((byte *)boneCache.header + sizeof(mdxaHeader_t) + offsets->offsets[boneNum]);
retBasepose=&skel->BasePoseMat;
retBaseposeInv=&skel->BasePoseMatInv;
}
char *G2_GetBoneNameFromSkel(CGhoul2Info &ghoul2, int boneNum)
{
if (!ghoul2.mBoneCache)
{
return NULL;
}
CBoneCache &boneCache=*ghoul2.mBoneCache;
assert(boneCache.mod);
assert(boneNum>=0&&boneNum<boneCache.header->numBones);
mdxaSkel_t *skel;
mdxaSkelOffsets_t *offsets;
offsets = (mdxaSkelOffsets_t *)((byte *)boneCache.header + sizeof(mdxaHeader_t));
skel = (mdxaSkel_t *)((byte *)boneCache.header + sizeof(mdxaHeader_t) + offsets->offsets[boneNum]);
return skel->name;
}
void G2_RagGetBoneBasePoseMatrixLow(CGhoul2Info &ghoul2, int boneNum, mdxaBone_t &boneMatrix, mdxaBone_t &retMatrix, vec3_t scale)
{
assert(ghoul2.mBoneCache);
CBoneCache &boneCache=*ghoul2.mBoneCache;
assert(boneCache.mod);
assert(boneNum>=0&&boneNum<boneCache.header->numBones);
mdxaSkel_t *skel;
mdxaSkelOffsets_t *offsets;
offsets = (mdxaSkelOffsets_t *)((byte *)boneCache.header + sizeof(mdxaHeader_t));
skel = (mdxaSkel_t *)((byte *)boneCache.header + sizeof(mdxaHeader_t) + offsets->offsets[boneNum]);
Multiply_3x4Matrix(&retMatrix, &boneMatrix, &skel->BasePoseMat);
if (scale[0])
{
retMatrix.matrix[0][3] *= scale[0];
}
if (scale[1])
{
retMatrix.matrix[1][3] *= scale[1];
}
if (scale[2])
{
retMatrix.matrix[2][3] *= scale[2];
}
VectorNormalize((float*)&retMatrix.matrix[0]);
VectorNormalize((float*)&retMatrix.matrix[1]);
VectorNormalize((float*)&retMatrix.matrix[2]);
}
void G2_GetBoneMatrixLow(CGhoul2Info &ghoul2,int boneNum,const vec3_t scale,mdxaBone_t &retMatrix,mdxaBone_t *&retBasepose,mdxaBone_t *&retBaseposeInv)
{
if (!ghoul2.mBoneCache)
{
retMatrix=identityMatrix;
// yikes
retBasepose=const_cast<mdxaBone_t *>(&identityMatrix);
retBaseposeInv=const_cast<mdxaBone_t *>(&identityMatrix);
return;
}
mdxaBone_t bolt;
assert(ghoul2.mBoneCache);
CBoneCache &boneCache=*ghoul2.mBoneCache;
assert(boneCache.mod);
assert(boneNum>=0&&boneNum<boneCache.header->numBones);
mdxaSkel_t *skel;
mdxaSkelOffsets_t *offsets;
offsets = (mdxaSkelOffsets_t *)((byte *)boneCache.header + sizeof(mdxaHeader_t));
skel = (mdxaSkel_t *)((byte *)boneCache.header + sizeof(mdxaHeader_t) + offsets->offsets[boneNum]);
Multiply_3x4Matrix(&bolt, &boneCache.Eval(boneNum), &skel->BasePoseMat); // DEST FIRST ARG
retBasepose=&skel->BasePoseMat;
retBaseposeInv=&skel->BasePoseMatInv;
if (scale[0])
{
bolt.matrix[0][3] *= scale[0];
}
if (scale[1])
{
bolt.matrix[1][3] *= scale[1];
}
if (scale[2])
{
bolt.matrix[2][3] *= scale[2];
}
VectorNormalize((float*)&bolt.matrix[0]);
VectorNormalize((float*)&bolt.matrix[1]);
VectorNormalize((float*)&bolt.matrix[2]);
Multiply_3x4Matrix(&retMatrix,&worldMatrix, &bolt);
#ifdef _DEBUG
for ( int i = 0; i < 3; i++ )
{
for ( int j = 0; j < 4; j++ )
{
assert( !Q_isnan(retMatrix.matrix[i][j]));
}
}
#endif// _DEBUG
}
int G2_GetParentBoneMatrixLow(CGhoul2Info &ghoul2,int boneNum,const vec3_t scale,mdxaBone_t &retMatrix,mdxaBone_t *&retBasepose,mdxaBone_t *&retBaseposeInv)
{
int parent=-1;
if (ghoul2.mBoneCache)
{
CBoneCache &boneCache=*ghoul2.mBoneCache;
assert(boneCache.mod);
assert(boneNum>=0&&boneNum<boneCache.header->numBones);
parent=boneCache.GetParent(boneNum);
if (parent<0||parent>=boneCache.header->numBones)
{
parent=-1;
retMatrix=identityMatrix;
// yikes
retBasepose=const_cast<mdxaBone_t *>(&identityMatrix);
retBaseposeInv=const_cast<mdxaBone_t *>(&identityMatrix);
}
else
{
G2_GetBoneMatrixLow(ghoul2,parent,scale,retMatrix,retBasepose,retBaseposeInv);
}
}
return parent;
}
//rww - RAGDOLL_END
void RemoveBoneCache(CBoneCache *boneCache)
{
delete boneCache;
}
const mdxaBone_t &EvalBoneCache(int index,CBoneCache *boneCache)
{
assert(boneCache);
return boneCache->Eval(index);
}
class CRenderSurface
{
public:
int surfaceNum;
surfaceInfo_v &rootSList;
const shader_t *cust_shader;
int fogNum;
qboolean personalModel;
CBoneCache *boneCache;
int renderfx;
const skin_t *skin;
const model_t *currentModel;
int lod;
boltInfo_v &boltList;
#ifdef _G2_GORE
shader_t *gore_shader;
CGoreSet *gore_set;
#endif
CRenderSurface(
int initsurfaceNum,
surfaceInfo_v &initrootSList,
const shader_t *initcust_shader,
int initfogNum,
qboolean initpersonalModel,
CBoneCache *initboneCache,
int initrenderfx,
const skin_t *initskin,
const model_t *initcurrentModel,
int initlod,
#ifdef _G2_GORE
boltInfo_v &initboltList,
shader_t *initgore_shader,
CGoreSet *initgore_set):
#else
boltInfo_v &initboltList):
#endif
surfaceNum(initsurfaceNum),
rootSList(initrootSList),
cust_shader(initcust_shader),
fogNum(initfogNum),
personalModel(initpersonalModel),
boneCache(initboneCache),
renderfx(initrenderfx),
skin(initskin),
currentModel(initcurrentModel),
lod(initlod),
#ifdef _G2_GORE
boltList(initboltList),
gore_shader(initgore_shader),
gore_set(initgore_set)
#else
boltList(initboltList)
#endif
{}
};
#define MAX_RENDER_SURFACES (2048)
static CRenderableSurface RSStorage[MAX_RENDER_SURFACES];
static unsigned int NextRS=0;
CRenderableSurface *AllocRS()
{
CRenderableSurface *ret=&RSStorage[NextRS];
ret->Init();
NextRS++;
NextRS%=MAX_RENDER_SURFACES;
return ret;
}
/*
All bones should be an identity orientation to display the mesh exactly
as it is specified.
For all other frames, the bones represent the transformation from the
orientation of the bone in the base frame to the orientation in this
frame.
*/
/*
=============
R_ACullModel
=============
*/
static int R_GCullModel( trRefEntity_t *ent ) {
// scale the radius if need be
float largestScale = ent->e.modelScale[0];
if (ent->e.modelScale[1] > largestScale)
{
largestScale = ent->e.modelScale[1];
}
if (ent->e.modelScale[2] > largestScale)
{
largestScale = ent->e.modelScale[2];
}
if (!largestScale)
{
largestScale = 1;
}
// cull bounding sphere
switch ( R_CullLocalPointAndRadius( vec3_origin, ent->e.radius * largestScale) )
{
case CULL_OUT:
tr.pc.c_sphere_cull_md3_out++;
return CULL_OUT;
case CULL_IN:
tr.pc.c_sphere_cull_md3_in++;
return CULL_IN;
case CULL_CLIP:
tr.pc.c_sphere_cull_md3_clip++;
return CULL_IN;
}
return CULL_IN;
}
/*
=================
R_AComputeFogNum
=================
*/
static int R_GComputeFogNum( trRefEntity_t *ent ) {
int i;
fog_t *fog;
if ( tr.refdef.rdflags & RDF_NOWORLDMODEL ) {
return 0;
}
if ( tr.refdef.doLAGoggles )
{
return tr.world->numfogs;
}
int partialFog = 0;
for ( i = 1 ; i < tr.world->numfogs ; i++ ) {
fog = &tr.world->fogs[i];
if ( ent->e.origin[0] - ent->e.radius >= fog->bounds[0][0]
&& ent->e.origin[0] + ent->e.radius <= fog->bounds[1][0]
&& ent->e.origin[1] - ent->e.radius >= fog->bounds[0][1]
&& ent->e.origin[1] + ent->e.radius <= fog->bounds[1][1]
&& ent->e.origin[2] - ent->e.radius >= fog->bounds[0][2]
&& ent->e.origin[2] + ent->e.radius <= fog->bounds[1][2] )
{//totally inside it
return i;
break;
}
if ( ( ent->e.origin[0] - ent->e.radius >= fog->bounds[0][0] && ent->e.origin[1] - ent->e.radius >= fog->bounds[0][1] && ent->e.origin[2] - ent->e.radius >= fog->bounds[0][2] &&
ent->e.origin[0] - ent->e.radius <= fog->bounds[1][0] && ent->e.origin[1] - ent->e.radius <= fog->bounds[1][1] && ent->e.origin[2] - ent->e.radius <= fog->bounds[1][2] ) ||
( ent->e.origin[0] + ent->e.radius >= fog->bounds[0][0] && ent->e.origin[1] + ent->e.radius >= fog->bounds[0][1] && ent->e.origin[2] + ent->e.radius >= fog->bounds[0][2] &&
ent->e.origin[0] + ent->e.radius <= fog->bounds[1][0] && ent->e.origin[1] + ent->e.radius <= fog->bounds[1][1] && ent->e.origin[2] + ent->e.radius <= fog->bounds[1][2] ) )
{//partially inside it
if ( tr.refdef.fogIndex == i || R_FogParmsMatch( tr.refdef.fogIndex, i ) )
{//take new one only if it's the same one that the viewpoint is in
return i;
break;
}
else if ( !partialFog )
{//first partialFog
partialFog = i;
}
}
}
//if nothing else, use the first partial fog you found
return partialFog;
}
// work out lod for this entity.
static int G2_ComputeLOD( trRefEntity_t *ent, const model_t *currentModel, int lodBias )
{
float flod, lodscale;
float projectedRadius;
int lod;
if ( currentModel->numLods < 2 )
{ // model has only 1 LOD level, skip computations and bias
return(0);
}
if (r_lodbias->integer > lodBias)
{
lodBias = r_lodbias->integer;
}
//**early out, it's going to be max lod
if (lodBias >= currentModel->numLods )
{
return currentModel->numLods - 1;
}
// scale the radius if need be
float largestScale = ent->e.modelScale[0];
if (ent->e.modelScale[1] > largestScale)
{
largestScale = ent->e.modelScale[1];
}
if (ent->e.modelScale[2] > largestScale)
{
largestScale = ent->e.modelScale[2];
}
if (!largestScale)
{
largestScale = 1;
}
if ( ( projectedRadius = ProjectRadius( 0.75*largestScale*ent->e.radius, ent->e.origin ) ) != 0 ) //we reduce the radius to make the LOD match other model types which use the actual bound box size
{
lodscale = r_lodscale->value;
if (lodscale > 20) lodscale = 20;
flod = 1.0f - projectedRadius * lodscale;
}
else
{
// object intersects near view plane, e.g. view weapon
flod = 0;
}
flod *= currentModel->numLods;
lod = Q_ftol( flod );
if ( lod < 0 )
{
lod = 0;
}
else if ( lod >= currentModel->numLods )
{
lod = currentModel->numLods - 1;
}
lod += lodBias;
if ( lod >= currentModel->numLods )
lod = currentModel->numLods - 1;
if ( lod < 0 )
lod = 0;
return lod;
}
void Multiply_3x4Matrix(mdxaBone_t *out,const mdxaBone_t *in2,const mdxaBone_t *in)
{
// first row of out
out->matrix[0][0] = (in2->matrix[0][0] * in->matrix[0][0]) + (in2->matrix[0][1] * in->matrix[1][0]) + (in2->matrix[0][2] * in->matrix[2][0]);
out->matrix[0][1] = (in2->matrix[0][0] * in->matrix[0][1]) + (in2->matrix[0][1] * in->matrix[1][1]) + (in2->matrix[0][2] * in->matrix[2][1]);
out->matrix[0][2] = (in2->matrix[0][0] * in->matrix[0][2]) + (in2->matrix[0][1] * in->matrix[1][2]) + (in2->matrix[0][2] * in->matrix[2][2]);
out->matrix[0][3] = (in2->matrix[0][0] * in->matrix[0][3]) + (in2->matrix[0][1] * in->matrix[1][3]) + (in2->matrix[0][2] * in->matrix[2][3]) + in2->matrix[0][3];
// second row of outf out
out->matrix[1][0] = (in2->matrix[1][0] * in->matrix[0][0]) + (in2->matrix[1][1] * in->matrix[1][0]) + (in2->matrix[1][2] * in->matrix[2][0]);
out->matrix[1][1] = (in2->matrix[1][0] * in->matrix[0][1]) + (in2->matrix[1][1] * in->matrix[1][1]) + (in2->matrix[1][2] * in->matrix[2][1]);
out->matrix[1][2] = (in2->matrix[1][0] * in->matrix[0][2]) + (in2->matrix[1][1] * in->matrix[1][2]) + (in2->matrix[1][2] * in->matrix[2][2]);
out->matrix[1][3] = (in2->matrix[1][0] * in->matrix[0][3]) + (in2->matrix[1][1] * in->matrix[1][3]) + (in2->matrix[1][2] * in->matrix[2][3]) + in2->matrix[1][3];
// third row of out out
out->matrix[2][0] = (in2->matrix[2][0] * in->matrix[0][0]) + (in2->matrix[2][1] * in->matrix[1][0]) + (in2->matrix[2][2] * in->matrix[2][0]);
out->matrix[2][1] = (in2->matrix[2][0] * in->matrix[0][1]) + (in2->matrix[2][1] * in->matrix[1][1]) + (in2->matrix[2][2] * in->matrix[2][1]);
out->matrix[2][2] = (in2->matrix[2][0] * in->matrix[0][2]) + (in2->matrix[2][1] * in->matrix[1][2]) + (in2->matrix[2][2] * in->matrix[2][2]);
out->matrix[2][3] = (in2->matrix[2][0] * in->matrix[0][3]) + (in2->matrix[2][1] * in->matrix[1][3]) + (in2->matrix[2][2] * in->matrix[2][3]) + in2->matrix[2][3];
}
static int G2_GetBonePoolIndex( const mdxaHeader_t *pMDXAHeader, int iFrame, int iBone)
{
assert(iFrame>=0&&iFrame<pMDXAHeader->numFrames);
assert(iBone>=0&&iBone<pMDXAHeader->numBones);
const int iOffsetToIndex = (iFrame * pMDXAHeader->numBones * 3) + (iBone * 3);
mdxaIndex_t *pIndex = (mdxaIndex_t *) ((byte*) pMDXAHeader + pMDXAHeader->ofsFrames + iOffsetToIndex);
#ifdef Q3_BIG_ENDIAN
int tmp = pIndex->iIndex & 0xFFFFFF00;
LL(tmp);
return tmp;
#else
return pIndex->iIndex & 0x00FFFFFF;
#endif
}
/*static inline*/ void UnCompressBone(float mat[3][4], int iBoneIndex, const mdxaHeader_t *pMDXAHeader, int iFrame)
{
mdxaCompQuatBone_t *pCompBonePool = (mdxaCompQuatBone_t *) ((byte *)pMDXAHeader + pMDXAHeader->ofsCompBonePool);
MC_UnCompressQuat(mat, pCompBonePool[ G2_GetBonePoolIndex( pMDXAHeader, iFrame, iBoneIndex ) ].Comp);
}
#define DEBUG_G2_TIMING (0)
#define DEBUG_G2_TIMING_RENDER_ONLY (1)
void G2_TimingModel(boneInfo_t &bone,int currentTime,int numFramesInFile,int ¤tFrame,int &newFrame,float &lerp)
{
assert(bone.startFrame>=0);
assert(bone.startFrame<=numFramesInFile);
assert(bone.endFrame>=0);
assert(bone.endFrame<=numFramesInFile);
// yes - add in animation speed to current frame
float animSpeed = bone.animSpeed;
float time;
if (bone.pauseTime)
{
time = (bone.pauseTime - bone.startTime) / 50.0f;
}
else
{
time = (currentTime - bone.startTime) / 50.0f;
}
if (time<0.0f)
{
time=0.0f;
}
float newFrame_g = bone.startFrame + (time * animSpeed);
int animSize = bone.endFrame - bone.startFrame;
float endFrame = (float)bone.endFrame;
// we are supposed to be animating right?
if (animSize)
{
// did we run off the end?
if (((animSpeed > 0.0f) && (newFrame_g > endFrame - 1)) ||
((animSpeed < 0.0f) && (newFrame_g < endFrame+1)))
{
// yep - decide what to do
if (bone.flags & BONE_ANIM_OVERRIDE_LOOP)
{
// get our new animation frame back within the bounds of the animation set
if (animSpeed < 0.0f)
{
// we don't use this case, or so I am told
// if we do, let me know, I need to insure the mod works
// should we be creating a virtual frame?
if ((newFrame_g < endFrame+1) && (newFrame_g >= endFrame))
{
// now figure out what we are lerping between
// delta is the fraction between this frame and the next, since the new anim is always at a .0f;
lerp = float(endFrame+1)-newFrame_g;
// frames are easy to calculate
currentFrame = endFrame;
assert(currentFrame>=0&¤tFrame<numFramesInFile);
newFrame = bone.startFrame;
assert(newFrame>=0&&newFrame<numFramesInFile);
}
else
{
if (newFrame_g <= endFrame+1)
{
newFrame_g=endFrame+fmod(newFrame_g-endFrame,animSize)-animSize;