summaryrefslogtreecommitdiff
path: root/libs/JSystem/J3DGraphAnimator/J3DJoint.cpp
blob: 632adaf0c6760b6c3b64ad4f47e4196706ed0170 (plain)
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
#include "JSystem/J3DGraphAnimator/J3DJoint.h"
#include "JSystem/J3DGraphAnimator/J3DMaterialAnm.h"
#include "JSystem/J3DGraphAnimator/J3DModel.h"
#include "JSystem/J3DGraphBase/J3DDrawBuffer.h"
#include "JSystem/J3DGraphBase/J3DMaterial.h"
#include "JSystem/JMath/JMath.h"
#include "dol2asm.h"
#include "m_Do/m_Do_mtx.h"

/* 8032EC28-8032ECAC 329568 0084+00 0/0 1/1 0/0 .text
 * init__25J3DMtxCalcJ3DSysInitBasicFRC3VecRA3_A4_Cf            */
void J3DMtxCalcJ3DSysInitBasic::init(Vec const& scale, Mtx const& mtx) {
    J3DSys::mCurrentS = scale;
    J3DSys::mParentS = (Vec){1.0f, 1.0f, 1.0f};
    JMAMTXApplyScale(mtx, J3DSys::mCurrentMtx, J3DSys::mCurrentS.x, J3DSys::mCurrentS.y,
                     J3DSys::mCurrentS.z);
}

/* 8032ECAC-8032ED30 3295EC 0084+00 0/0 2/2 2/2 .text
 * init__24J3DMtxCalcJ3DSysInitMayaFRC3VecRA3_A4_Cf             */
void J3DMtxCalcJ3DSysInitMaya::init(Vec const& scale, Mtx const& mtx) {
    J3DSys::mParentS = (Vec){1.0f, 1.0f, 1.0f};
    J3DSys::mCurrentS = scale;
    JMAMTXApplyScale(mtx, J3DSys::mCurrentMtx, J3DSys::mCurrentS.x, J3DSys::mCurrentS.y,
                     J3DSys::mCurrentS.z);
}

/* ############################################################################################## */
/* 804515F0-804515F4 000AF0 0004+00 3/3 1/1 0/0 .sbss            mMtxBuffer__10J3DMtxCalc */
J3DMtxBuffer* J3DMtxCalc::mMtxBuffer;

/* 804515F4-804515F8 000AF4 0004+00 4/4 9/9 2/2 .sbss            mJoint__10J3DMtxCalc */
J3DJoint* J3DMtxCalc::mJoint;

inline s32 checkScaleOne(const Vec& param_0) {
    if (param_0.x == 1.0f && param_0.y == 1.0f && param_0.z == 1.0f) {
        return true;
    } else {
        return false;
    }
}

/* 8032ED30-8032EE50 329670 0120+00 0/0 1/1 0/0 .text
 * calcTransform__28J3DMtxCalcCalcTransformBasicFRC16J3DTransformInfo */
void J3DMtxCalcCalcTransformBasic::calcTransform(J3DTransformInfo const& transInfo) {
    J3DMtxBuffer* mtxBuf = J3DMtxCalc::getMtxBuffer();
    u16 jntNo = J3DMtxCalc::getJoint()->getJntNo();

    MtxP anmMtx = mtxBuf->getAnmMtx(jntNo);

    J3DSys::mCurrentS.x *= transInfo.mScale.x;
    J3DSys::mCurrentS.y *= transInfo.mScale.y;
    J3DSys::mCurrentS.z *= transInfo.mScale.z;
    J3DGetTranslateRotateMtx(transInfo, anmMtx);

    if (!checkScaleOne(J3DSys::mCurrentS)) {
        mtxBuf->setScaleFlag(jntNo, 0);
        JMAMTXApplyScale(anmMtx, anmMtx, transInfo.mScale.x, transInfo.mScale.y,
                         transInfo.mScale.z);
    } else {
        mtxBuf->setScaleFlag(jntNo, 1);
    }

    MTXConcat(J3DSys::mCurrentMtx, anmMtx, J3DSys::mCurrentMtx);
    MTXCopy(J3DSys::mCurrentMtx, anmMtx);
}

/* 8032EE50-8032EFBC 329790 016C+00 0/0 1/1 0/0 .text
 * calcTransform__32J3DMtxCalcCalcTransformSoftimageFRC16J3DTransformInfo */
void J3DMtxCalcCalcTransformSoftimage::calcTransform(J3DTransformInfo const& transInfo) {
    J3DMtxBuffer* mtxBuf = J3DMtxCalc::getMtxBuffer();
    u16 jntNo = J3DMtxCalc::getJoint()->getJntNo();

    MtxP anmMtx = mtxBuf->getAnmMtx(jntNo);

    J3DGetTranslateRotateMtx(transInfo.mRotation.x, transInfo.mRotation.y, transInfo.mRotation.z,
                             transInfo.mTranslate.x * J3DSys::mCurrentS.x,
                             transInfo.mTranslate.y * J3DSys::mCurrentS.y,
                             transInfo.mTranslate.z * J3DSys::mCurrentS.z, anmMtx);
    MTXConcat(J3DSys::mCurrentMtx, anmMtx, J3DSys::mCurrentMtx);

    J3DSys::mCurrentS.x *= transInfo.mScale.x;
    J3DSys::mCurrentS.y *= transInfo.mScale.y;
    J3DSys::mCurrentS.z *= transInfo.mScale.z;

    if (!checkScaleOne(J3DSys::mCurrentS)) {
        mtxBuf->setScaleFlag(jntNo, 0);
        JMAMTXApplyScale(J3DSys::mCurrentMtx, anmMtx, J3DSys::mCurrentS.x, J3DSys::mCurrentS.y,
                         J3DSys::mCurrentS.z);
        anmMtx[0][3] = J3DSys::mCurrentMtx[0][3];
        anmMtx[1][3] = J3DSys::mCurrentMtx[1][3];
        anmMtx[2][3] = J3DSys::mCurrentMtx[2][3];
    } else {
        mtxBuf->setScaleFlag(jntNo, 1);
        MTXCopy(J3DSys::mCurrentMtx, anmMtx);
    }
}

/* 8032EFBC-8032F13C 3298FC 0180+00 0/0 6/6 2/2 .text
 * calcTransform__27J3DMtxCalcCalcTransformMayaFRC16J3DTransformInfo */
void J3DMtxCalcCalcTransformMaya::calcTransform(J3DTransformInfo const& transInfo) {
    J3DJoint* joint = J3DMtxCalc::getJoint();
    J3DMtxBuffer* mtxBuf = J3DMtxCalc::getMtxBuffer();

    u16 jntNo = joint->getJntNo();

    MtxP anmMtx = mtxBuf->getAnmMtx(jntNo);

    J3DGetTranslateRotateMtx(transInfo, anmMtx);

    if (transInfo.mScale.x == 1.0f && transInfo.mScale.y == 1.0f && transInfo.mScale.z == 1.0f) {
        mtxBuf->setScaleFlag(jntNo, 1);
    } else {
        mtxBuf->setScaleFlag(jntNo, 0);
        JMAMTXApplyScale(anmMtx, anmMtx, transInfo.mScale.x, transInfo.mScale.y,
                         transInfo.mScale.z);
    }

    if (joint->getScaleCompensate() == 1) {
        f32 invX = JMath::fastReciprocal(J3DSys::mParentS.x);
        f32 invY = JMath::fastReciprocal(J3DSys::mParentS.y);
        f32 invZ = JMath::fastReciprocal(J3DSys::mParentS.z);

        anmMtx[0][0] *= invX;
        anmMtx[0][1] *= invX;
        anmMtx[0][2] *= invX;
        anmMtx[1][0] *= invY;
        anmMtx[1][1] *= invY;
        anmMtx[1][2] *= invY;
        anmMtx[2][0] *= invZ;
        anmMtx[2][1] *= invZ;
        anmMtx[2][2] *= invZ;
    }

    MTXConcat(J3DSys::mCurrentMtx, anmMtx, J3DSys::mCurrentMtx);
    MTXCopy(J3DSys::mCurrentMtx, anmMtx);

    J3DSys::mParentS.x = transInfo.mScale.x;
    J3DSys::mParentS.y = transInfo.mScale.y;
    J3DSys::mParentS.z = transInfo.mScale.z;
}

/* 8032F13C-8032F170 329A7C 0034+00 0/0 1/1 0/0 .text            appendChild__8J3DJointFP8J3DJoint
 */
void J3DJoint::appendChild(J3DJoint* pChild) {
    if (mChild == NULL) {
        mChild = pChild;
    } else {
        J3DJoint* curChild = mChild;
        while (curChild->getYounger() != NULL) {
            curChild = curChild->getYounger();
        }
        curChild->setYounger(pChild);
    }
}

/* ############################################################################################## */

#pragma push
#pragma force_active on
/* dead data */
SECTION_DEAD static u32 const pad_803A2080[2] = {0, 0};
#pragma pop

/* 8032F170-8032F254 329AB0 00E4+00 0/0 1/1 0/0 .text            __ct__8J3DJointFv */
J3DJoint::J3DJoint() {
    mCallBackUserData = NULL;
    mCallBack = NULL;
    field_0x8 = NULL;
    mChild = NULL;
    mYounger = NULL;
    mJntNo = 0;
    mKind = 1;
    mScaleCompensate = false;
    __memcpy(&mTransformInfo, &j3dDefaultTransformInfo, sizeof(J3DTransformInfo));
    mBoundingSphereRadius = 0.0f;
    mMtxCalc = NULL;
    mMesh = NULL;

    Vec init = {0.0f, 0.0f, 0.0f};
    mMin = init;
    Vec init2 = {0.0f, 0.0f, 0.0f};
    mMax = init2;
}

/* 8032F254-8032F3F8 329B94 01A4+00 0/0 1/1 0/0 .text            entryIn__8J3DJointFv */
void J3DJoint::entryIn() {
    MtxP anmMtx = j3dSys.getModel()->getAnmMtx(mJntNo);
    j3dSys.getDrawBuffer(0)->setZMtx(anmMtx);
    j3dSys.getDrawBuffer(1)->setZMtx(anmMtx);
    for (J3DMaterial* mesh = mMesh; mesh != NULL;) {
        if (mesh->getShape()->checkFlag(1)) {
            mesh = mesh->getNext();
        } else {
            J3DMatPacket* matPacket = j3dSys.getModel()->getMatPacket(mesh->getIndex());
            J3DShapePacket* shapePacket =
                j3dSys.getModel()->getShapePacket(mesh->getShape()->getIndex());
            if (!matPacket->isLocked()) {
                if (mesh->getMaterialAnm()) {
                    J3DMaterialAnm* piVar8 = mesh->getMaterialAnm();
                    piVar8->calc(mesh);
                }
                mesh->calc(anmMtx);
            }
            mesh->setCurrentMtx();
            matPacket->setMaterialAnmID(mesh->getMaterialAnm());
            matPacket->setShapePacket(shapePacket);
            J3DDrawBuffer* drawBuffer = j3dSys.getDrawBuffer(mesh->isDrawModeOpaTexEdge());
            if ((u8)matPacket->entry(drawBuffer)) {
                j3dSys.setMatPacket(matPacket);
                J3DDrawBuffer::entryNum++;
                mesh->makeDisplayList();
            }
            mesh = mesh->getNext();
        }
    }
}

/* ############################################################################################## */
/* 804515F8-80451600 000AF8 0004+04 1/1 1/1 0/0 .sbss            mCurrentMtxCalc__8J3DJoint */
J3DMtxCalc* J3DJoint::mCurrentMtxCalc;

/* 8032F3F8-8032F5A8 329D38 01B0+00 0/0 1/1 0/0 .text            recursiveCalc__8J3DJointFv */
void J3DJoint::recursiveCalc() {
    J3DMtxCalc* prevMtxCalc = NULL;
    Mtx prevCurrentMtx;
    mDoMtx_copy(J3DSys::mCurrentMtx, prevCurrentMtx);
    f32 currentX = J3DSys::mCurrentS.x;
    f32 currentY = J3DSys::mCurrentS.y;
    f32 currentZ = J3DSys::mCurrentS.z;
    f32 parentX = J3DSys::mParentS.x;
    f32 parentY = J3DSys::mParentS.y;
    f32 parentZ = J3DSys::mParentS.z;
    if (getMtxCalc() != NULL) {
        prevMtxCalc = getCurrentMtxCalc();
        J3DMtxCalc* piVar2 = this->getMtxCalc();
        setCurrentMtxCalc(piVar2);
        J3DMtxCalc::setJoint(this);
        piVar2->calc();
    } else {
        if (getCurrentMtxCalc() != NULL) {
            J3DMtxCalc* uVar6 = getCurrentMtxCalc();
            J3DMtxCalc::setJoint(this);
            uVar6->calc();
        }
    }

    J3DJointCallBack jointCallback = getCallBack();
    if (jointCallback != NULL) {
        (*jointCallback)(this, 0);
    }

    J3DJoint* child = getChild();
    if (child != NULL) {
        child->recursiveCalc();
    }
    mDoMtx_copy(prevCurrentMtx, J3DSys::mCurrentMtx);

    J3DSys::mCurrentS.x = currentX;
    J3DSys::mCurrentS.y = currentY;
    J3DSys::mCurrentS.z = currentZ;
    J3DSys::mParentS.x = parentX;
    J3DSys::mParentS.y = parentY;
    J3DSys::mParentS.z = parentZ;

    if (prevMtxCalc != NULL) {
        setCurrentMtxCalc(prevMtxCalc);
    }
    if (jointCallback != NULL) {
        (*jointCallback)(this, 1);
    }

    J3DJoint* younger = getYounger();
    if (younger != NULL) {
        younger->recursiveCalc();
    }
}