summaryrefslogtreecommitdiff
path: root/src/JSystem/J2DGraph/J2DMaterial.cpp
blob: af85981c017c02a87a6498d687cca0949bbd2048 (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
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
//
// Generated By: dol2asm
// Translation Unit: J2DMaterial
//

#include "JSystem/JSystem.h" // IWYU pragma: keep

#include "JSystem/J2DGraph/J2DMaterial.h"
#include "JSystem/J3DGraphBase/J3DStruct.h"
#include "JSystem/JKernel/JKRHeap.h"
#include "JSystem/JUtility/JUTPalette.h"
#include "JSystem/JUtility/JUTTexture.h"

/* 802EA1AC-802EA2CC 2E4AEC 0120+00 0/0 1/1 0/0 .text            __ct__11J2DMaterialFv */
J2DMaterial::J2DMaterial() {
    field_0x4 = 0;
    mIndex = -1;
    mTevBlock = NULL;
    mIndBlock = NULL;
    mAnmPointer = NULL;
    field_0x8 = 1;
    mVisible = true;
}

J2DMaterial::~J2DMaterial() {
    delete mTevBlock;
    delete mIndBlock;
    delete mAnmPointer;
}

/* 802EA38C-802EA410 2E4CCC 0084+00 0/0 8/8 0/0 .text            setGX__11J2DMaterialFv */
void J2DMaterial::setGX() {
    if (mVisible) {
        getColorBlock()->setGX();
        getTexGenBlock()->setGX();

        if (getTevBlock() != NULL) {
            getTevBlock()->setGX();
        }

        if (getIndBlock() != NULL) {
            getIndBlock()->setGX();
        }

        getPEBlock()->setGX();
    }
}

/* 802EA410-802EA5C4 2E4D50 01B4+00 0/0 1/1 0/0 .text            createTevBlock__11J2DMaterialFib */
J2DTevBlock* J2DMaterial::createTevBlock(int block_type, bool noAlign) {
    J2DTevBlock* block;

    if (noAlign) {
        if (block_type <= 1) {
            block = new J2DTevBlock1();
        } else if (block_type == 2) {
            block = new J2DTevBlock2();
        } else if (block_type <= 4) {
            block = new J2DTevBlock4();
        } else if (block_type <= 8) {
            block = new J2DTevBlock8();
        } else {
            block = new J2DTevBlock16();
        }
    } else {
        if (block_type <= 1) {
            block = new (-4) J2DTevBlock1();
        } else if (block_type == 2) {
            block = new (-4) J2DTevBlock2();
        } else if (block_type <= 4) {
            block = new (-4) J2DTevBlock4();
        } else if (block_type <= 8) {
            block = new (-4) J2DTevBlock8();
        } else {
            block = new (-4) J2DTevBlock16();
        }
    }

    return block;
}

/* 802EA5C4-802EA79C 2E4F04 01D8+00 0/0 1/1 0/0 .text            createIndBlock__11J2DMaterialFib */
J2DIndBlock* J2DMaterial::createIndBlock(int block_type, bool noAlign) {
    J2DIndBlock* block;

    if (noAlign) {
        if (block_type != 0) {
            block = new J2DIndBlockFull();
        } else {
            block = new J2DIndBlockNull();
        }
    } else {
        if (block_type != 0) {
            block = new (-4) J2DIndBlockFull();
        } else {
            block = new (-4) J2DIndBlockNull();
        }
    }

    return block;
}

/* 802EA79C-802EA84C 2E50DC 00B0+00 1/1 0/0 0/0 .text
 * __ct__Q211J2DMaterial21J2DMaterialAnmPointerFv               */
J2DMaterial::J2DMaterialAnmPointer::J2DMaterialAnmPointer() {
    mColorAnm = NULL;
    mColorIds = -1;
    mSRTAnm = NULL;

    for (int i = 0; i < 8; i++) {
        mSRTIds[i] = -1;
    }
    mPatternAnm = NULL;

    for (int i = 0; i < 8; i++) {
        mPatternIds[i] = -1;
    }
    mTevAnm = NULL;

    for (int i = 0; i < 4; i++) {
        mTevCRegIds[i] = -1;
    }

    for (int i = 0; i < 4; i++) {
        mTevKRegIds[i] = -1;
    }
}

/* 802EA84C-802EA89C 2E518C 0050+00 4/4 0/0 0/0 .text            makeAnmPointer__11J2DMaterialFv */
void J2DMaterial::makeAnmPointer() {
    if (mAnmPointer == NULL) {
        mAnmPointer = new J2DMaterialAnmPointer();
    }
}

/* 802EA89C-802EA94C 2E51DC 00B0+00 0/0 4/4 0/0 .text setAnimation__11J2DMaterialFP11J2DAnmColor
 */
void J2DMaterial::setAnimation(J2DAnmColor* anm) {
    if (anm != NULL || mAnmPointer != NULL) {
        makeAnmPointer();
        mAnmPointer->mColorAnm = anm;
        mAnmPointer->mColorIds = -1;

        if (anm != NULL) {
            u16 matNum = anm->getUpdateMaterialNum();
            u16 index = getIndex();

            for (u16 i = 0; i < matNum; i++) {
                u16 matID = anm->getUpdateMaterialID(i);

                if (index == matID) {
                    mAnmPointer->mColorIds = i;
                    return;
                }
            }
        }
    }
}

/* 802EA94C-802EAA2C 2E528C 00E0+00 0/0 4/4 0/0 .text
 * setAnimation__11J2DMaterialFP19J2DAnmTextureSRTKey           */
void J2DMaterial::setAnimation(J2DAnmTextureSRTKey* anm) {
    if (anm != NULL || mAnmPointer != NULL) {
        makeAnmPointer();
        mAnmPointer->mSRTAnm = anm;

        for (int i = 0; i < 8; i++) {
            mAnmPointer->mSRTIds[i] = -1;
        }

        if (anm != NULL) {
            u16 matNum = anm->getUpdateMaterialNum();
            u16 index = getIndex();

            for (u16 i = 0; i < matNum; i++) {
                u16 matID = anm->getUpdateMaterialID(i);

                if (index == matID) {
                    u8 mtxID = anm->getUpdateTexMtxID(i);
                    mAnmPointer->mSRTIds[mtxID] = i;
                }
            }
        }
    }
}

/* 802EAA2C-802EAB0C 2E536C 00E0+00 0/0 4/4 0/0 .text
 * setAnimation__11J2DMaterialFP16J2DAnmTexPattern              */
void J2DMaterial::setAnimation(J2DAnmTexPattern* anm) {
    if (anm != NULL || mAnmPointer != NULL) {
        makeAnmPointer();
        mAnmPointer->mPatternAnm = anm;

        for (int i = 0; i < 8; i++) {
            mAnmPointer->mPatternIds[i] = -1;
        }

        if (anm != NULL) {
            u16 matNum = anm->getUpdateMaterialNum();
            u16 index = getIndex();
            J3DAnmTexPatternFullTable* anmTbl = anm->getAnmTable();

            for (u16 i = 0; i < matNum; i++) {
                u16 matID = anm->getUpdateMaterialID(i);

                if (index == matID) {
                    mAnmPointer->mPatternIds[anmTbl[i].mTexNo] = i;
                }
            }
        }
    }
}

/* 802EAB0C-802EAC78 2E544C 016C+00 0/0 4/4 0/0 .text
 * setAnimation__11J2DMaterialFP15J2DAnmTevRegKey               */
void J2DMaterial::setAnimation(J2DAnmTevRegKey* anm) {
    if (anm != NULL || mAnmPointer != NULL) {
        makeAnmPointer();
        mAnmPointer->mTevAnm = anm;

        for (int i = 0; i < 4; i++) {
            mAnmPointer->mTevCRegIds[i] = -1;
        }

        if (anm != NULL) {
            u16 matNum = anm->getCRegUpdateMaterialNum();
            u16 index = getIndex();

            for (u16 i = 0; i < matNum; i++) {
                u16 matID = anm->getCRegUpdateMaterialID(i);

                if (index == matID) {
                    J3DAnmCRegKeyTable* anmTbl = anm->getAnmCRegKeyTable();
                    mAnmPointer->mTevCRegIds[anmTbl[i].mColorId] = i;
                }
            }
        }

        for (int i = 0; i < 4; i++) {
            mAnmPointer->mTevKRegIds[i] = -1;
        }

        if (anm != NULL) {
            u16 matNum = anm->getKRegUpdateMaterialNum();
            u16 index = getIndex();

            for (u16 i = 0; i < matNum; i++) {
                u16 matID = anm->getKRegUpdateMaterialID(i);

                if (index == matID) {
                    J3DAnmKRegKeyTable* anmTbl = anm->getAnmKRegKeyTable();
                    mAnmPointer->mTevKRegIds[anmTbl[i].mColorId] = i;
                }
            }
        }
    }
}

/* 802EAC78-802EB0F4 2E55B8 047C+00 0/0 1/1 0/0 .text            animation__11J2DMaterialFv */
void J2DMaterial::animation() {
    if (!mAnmPointer) {
		return;
	}

	if (mAnmPointer->mColorAnm && mAnmPointer->mColorIds != 0xFFFF) {
		GXColor color;
		mAnmPointer->mColorAnm->getColor(mAnmPointer->mColorIds, &color);
		getColorBlock()->setMatColor(0, color);
	}
    
	if (mAnmPointer->mSRTAnm) {
		for (u8 i = 0; i < 8; i++) {
			if (mAnmPointer->mSRTIds[i] != 0xFFFF) {
				J3DTextureSRTInfo info3D;
				mAnmPointer->mSRTAnm->getTransform(mAnmPointer->mSRTIds[i], &info3D);
				J2DTextureSRTInfo info2D;
				info2D.mScaleX       = info3D.mScaleX;
				info2D.mScaleY       = info3D.mScaleY;
				info2D.mRotationDeg  = (360.0f * f32((u16)info3D.mRotation)) / 65535.0f;
				info2D.mTranslationX = info3D.mTranslationX;
				info2D.mTranslationY = info3D.mTranslationY;

				J2DTexMtx texMtx;
				getTexGenBlock()->getTexMtx(i, texMtx);

				texMtx.getTexMtxInfo().mTexSRTInfo = info2D;
				getTexGenBlock()->setTexMtx(i, texMtx);
				getTexGenBlock()->getTexCoord(i).setTexGenMtx(i * 3 + 30);
			}
		}
	}

	if (getTevBlock() && mAnmPointer->mPatternAnm) {
		for (u8 i = 0; i < 8; i++) {
			if (mAnmPointer->mPatternIds[i] != 0xFFFF) {
				JUTTexture* texture = getTevBlock()->getTexture(i);
				if (!texture) {
					continue;
				}

				u16 idx = mAnmPointer->mPatternIds[i];
				u16 texNo;
				mAnmPointer->mPatternAnm->getTexNo(idx, &texNo);
				getTevBlock()->setTexNo(i, texNo);

                ResTIMG* img = mAnmPointer->mPatternAnm->getResTIMG(idx);
                if (texture->getTexInfo() != img) {
                    JUTPalette* palette = NULL;
                    GXTlut tlut = GX_TLUT0;
                    if (img->indexTexture != 0) {
                        palette = mAnmPointer->mPatternAnm->getPalette(idx);
						if (palette->getNumColors() > 256) {
							tlut = (GXTlut)(i % 4 + 16);
						} else {
							tlut = (GXTlut)i;
						}
                    }

                    texture->storeTIMG(img, palette, tlut);
                }
            }
		}
	}

	if (getTevBlock() && mAnmPointer->mTevAnm) {
		for (u8 i = 0; i < 4; i++) {
			if (mAnmPointer->mTevCRegIds[i] != 0xFFFF) {
				J2DGXColorS10 color;
				mAnmPointer->mTevAnm->getTevColorReg(mAnmPointer->mTevCRegIds[i], &color);
				getTevBlock()->setTevColor(i, color);
			}
		}

		for (u8 i = 0; i < 4; i++) {
			u16 idx = mAnmPointer->mTevKRegIds[i];
			if (idx != 0xFFFF) {
				JUtility::TColor konstColor;
				mAnmPointer->mTevAnm->getTevKonstReg(idx, &konstColor);
				getTevBlock()->setTevKColor(i, konstColor);
			}
		}
	}
}