-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstudiomodel.cpp
More file actions
201 lines (155 loc) · 5.73 KB
/
studiomodel.cpp
File metadata and controls
201 lines (155 loc) · 5.73 KB
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
#include "studiomodel.h"
#include <istudiorender.h>
#include <bone_setup.h>
#include <bone_accessor.h>
extern IMDLCache* g_pMDLCache;
static float s_flexdescweight[MAXSTUDIOFLEXDESC];
static float s_flexdescweight2[MAXSTUDIOFLEXDESC];
static float s_flexweightsrc[MAXSTUDIOFLEXCTRL * 4];
CStudioModel::CStudioModel(const char* path)
{
MDLHandle_t mdlHandle = g_pMDLCache->FindMDL(path);
CStudioHdr* studiohdr = new CStudioHdr(g_pMDLCache->GetStudioHdr(mdlHandle), g_pMDLCache);
studiohwdata_t* studiohwdata = g_pMDLCache->GetHardwareData(mdlHandle);
if (studiohdr->GetRenderHdr()->version != STUDIO_VERSION)
{
Error("Bad model version on %s! Expected %d, got %d!\n", path, STUDIO_VERSION, studiohdr->GetRenderHdr()->version);
return;
}
m_studiohdr = studiohdr;
m_studiohwdata = studiohwdata;
m_sequence = 0;
m_posepos = new Vector[studiohdr->numbones()];
m_poseang = new Quaternion[studiohdr->numbones()];
m_poseparameter = new float[studiohdr->GetNumPoseParameters()];
m_poseparameterProcessed = new float[studiohdr->GetNumPoseParameters()];
for (int i = 0; i < studiohdr->numbones(); i++)
{
m_posepos[i].Init();
m_poseang[i].Init();
}
for (int i = 0; i < studiohdr->GetNumPoseParameters(); i++)
{
m_poseparameter[i] = 0;
m_poseparameterProcessed[i] = 0;
}
}
CStudioModel::~CStudioModel()
{
if (m_posepos)
delete[] m_posepos;
if (m_poseang)
delete[] m_poseang;
if(m_poseparameter)
delete[] m_poseparameter;
if(m_poseparameterProcessed)
delete[] m_poseparameterProcessed;
}
void CStudioModel::Draw(Vector& pos, QAngle& ang)
{
// Set the transform
matrix3x4_t rootmatrix;
AngleMatrix(ang, rootmatrix);
MatrixSetColumn(pos, 3, rootmatrix);
Draw(rootmatrix);
}
void CStudioModel::Draw(matrix3x4_t& rootmatrix)
{
if (!m_studiohdr || !m_studiohwdata)
return;
// Set the info
DrawModelInfo_t info;
memset(&info, 0, sizeof(info));
info.m_pStudioHdr = const_cast<studiohdr_t*>(m_studiohdr->GetRenderHdr());
info.m_pHardwareData = m_studiohwdata;
info.m_Lod = -1;
// Draw it
if (m_studiohdr->flags() & STUDIOHDR_FLAGS_STATIC_PROP)
g_pStudioRender->DrawModelStaticProp(info, rootmatrix);
else
{
memset(s_flexdescweight, 0, sizeof(s_flexdescweight));
memset(s_flexdescweight2, 0, sizeof(s_flexdescweight2));
int flexcount = m_studiohdr->numflexdesc();
for (int i = 0; i < m_studiohdr->GetNumPoseParameters(); i++)
{
m_poseparameter[i] = Studio_SetPoseParameter(m_studiohdr, i, m_poseparameter[i], m_poseparameterProcessed[i]);
}
IBoneSetup boneSetup(m_studiohdr, BONE_USED_BY_ANYTHING, m_poseparameterProcessed);
boneSetup.InitPose(m_posepos, m_poseang);
float cycleRate = Studio_CPS(m_studiohdr, m_studiohdr->pSeqdesc(m_sequence), m_sequence, m_poseparameter);
boneSetup.AccumulatePose(m_posepos, m_poseang, m_sequence, fmod(m_time * cycleRate, 1.0), 1.0, m_time, 0);
matrix3x4_t* g_pBoneToWorld = g_pStudioRender->LockBoneMatrices(MAXSTUDIOBONES);
for (int i = 0; i < m_studiohdr->numbones(); i++)
{
if (CalcProceduralBone(m_studiohdr, i, CBoneAccessor(g_pBoneToWorld)))
continue;
// Set the transform
matrix3x4_t matrix;
QuaternionMatrix(m_poseang[i], matrix);
MatrixSetColumn(m_posepos[i], 3, matrix);
mstudiobone_t* pBone = m_studiohdr->pBone(i);
if (pBone->parent == -1)
ConcatTransforms(rootmatrix, matrix, g_pBoneToWorld[i]);
else
ConcatTransforms(g_pBoneToWorld[pBone->parent], matrix, g_pBoneToWorld[i]);
}
for (int i = 0; i < flexcount; i++)
{
s_flexdescweight[i] = 0.0;
}
for (LocalFlexController_t i = (LocalFlexController_t)0; i < m_studiohdr->numflexcontrollers(); i++)
{
m_studiohdr->pFlexcontroller(i)->localToGlobal = i;
}
for (LocalFlexController_t i = (LocalFlexController_t)0; i < m_studiohdr->numflexcontrollers(); i++)
{
mstudioflexcontroller_t* pflex = m_studiohdr->pFlexcontroller(i);
int j = m_studiohdr->pFlexcontroller(i)->localToGlobal;
// remap m_flexweights to full dynamic range, global flexcontroller indexes
s_flexweightsrc[j] = 0.5f * (pflex->max - pflex->min) + pflex->min;
}
m_studiohdr->RunFlexRules(s_flexweightsrc, s_flexdescweight);
// Apparently, we have to hand these over to be allocated and then copy into them now.
float* pFlexdescweight;
float* pFlexdescweight2;
g_pStudioRender->LockFlexWeights(flexcount, &pFlexdescweight, &pFlexdescweight2);
for (int i = 0; i < m_studiohdr->numflexdesc(); i++)
{
s_flexdescweight2[i] = s_flexdescweight2[i] * 0/*weight ratio*/ + s_flexdescweight[i] * (1 - 0/*weight ratio*/);
pFlexdescweight[i] = s_flexdescweight[i];
pFlexdescweight2[i] = s_flexdescweight2[i];
}
g_pStudioRender->UnlockFlexWeights();
g_pStudioRender->UnlockBoneMatrices();
g_pStudioRender->DrawModel(0, info, g_pBoneToWorld, 0, 0, { rootmatrix.m_flMatVal[0][3], rootmatrix.m_flMatVal[1][3], rootmatrix.m_flMatVal[2][3]});
}
}
Vector CStudioModel::Center()
{
return (m_studiohdr->hull_max() + m_studiohdr->hull_min()) * 0.5f;
}
// Would just include studio_generic_io, but I'm not sure what nonsense is happening in there...
// This function would be useful, if it was static...
const studiohdr_t* studiohdr_t::FindModel(void** cache, char const* modelname) const
{
MDLHandle_t handle = g_pMDLCache->FindMDL(modelname);
*cache = (void*)handle;
return g_pMDLCache->GetStudioHdr(handle);
}
virtualmodel_t* studiohdr_t::GetVirtualModel() const
{
return g_pMDLCache->GetVirtualModel((MDLHandle_t)virtualModel);
}
byte* studiohdr_t::GetAnimBlock(int i) const
{
return g_pMDLCache->GetAnimBlock((MDLHandle_t)virtualModel, i);
}
int studiohdr_t::GetAutoplayList(unsigned short** pOut) const
{
return g_pMDLCache->GetAutoplayList((MDLHandle_t)virtualModel, pOut);
}
const studiohdr_t* virtualgroup_t::GetStudioHdr() const
{
return g_pMDLCache->GetStudioHdr((MDLHandle_t)cache);
}