-
Notifications
You must be signed in to change notification settings - Fork 5
/
modelnw.h
307 lines (251 loc) · 13 KB
/
modelnw.h
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
#pragma once
#include "game/resource/resarchive.h"
#include "game/graphics/layer/renderobjlayer.h"
#include "game/graphics/model/animation.h"
#include "game/graphics/drawmgr.h"
#include "game/graphics/model/model.h"
#include "game/graphics/model/materialnw.h"
#include "game/graphics/lightmapmgr.h"
#include "common/aglShaderProgram.h"
#include "common/aglShaderProgramArchive.h"
#include "common/aglShaderLocation.h"
#include "common/aglUniformBlock.h"
#include "g3d/aglModelShaderAssign.h"
#include "g3d/aglModelEx.h"
#include "nw/g3d/g3d_ShapeObj.h"
#include "nw/g3d/g3d_MaterialObj.h"
#include "nw/g3d/g3d_SkeletalAnimObj.h"
#include "nw/g3d/res/g3d_ResModel.h"
#include "container/seadBuffer.h"
#include "sdk/os/functions.h"
class CullViewFrustum;
class ModelNW : public Model {
SEAD_RTTI_OVERRIDE_DECL(ModelNW, Model);
public:
struct ShaderAssign {
void initialize(const agl::ShaderProgram* shaderProgram) {
this->shaderProgram = shaderProgram;
this->envLocation.setName("MdlEnvView");
this->mtxLocation.setName("MdlMtx");
this->shpLocation.setName("Shp");
this->matLocation.setName("Mat");
this->sdwLocation.setName("cShadowMap");
this->rflLocation.setName("cReflectionMap");
this->updateLocation();
}
void updateLocation() {
if (this->shaderProgram) {
this->envLocation.search(*this->shaderProgram);
this->mtxLocation.search(*this->shaderProgram);
this->shpLocation.search(*this->shaderProgram);
this->matLocation.search(*this->shaderProgram);
this->sdwLocation.search(*this->shaderProgram);
this->rflLocation.search(*this->shaderProgram);
}
}
const agl::ShaderProgram* shaderProgram;
agl::UniformBlockLocation envLocation, mtxLocation, shpLocation, matLocation;
agl::SamplerLocation sdwLocation, rflLocation;
};
static_assert(sizeof(ShaderAssign) == 0x64, "ModelNW::ShaderAssign size mismatch");
struct ShapeRenderInfo {
s32 idxShape;
s32 priority;
sead::BitFlag32 flag;
s32 polygonOffset;
mutable agl::DisplayList attribDl, matDl;
};
static_assert(sizeof(ShapeRenderInfo) == 0x30, "ModelNW::ShapeRenderInfo size mismatch");
struct LightMap {
void clear() {
OSBlockSet(this->idxLightMap, u8(-1), sizeof(this->idxLightMap));
OSBlockSet(this->idxSampler, u8(-1), sizeof(this->idxSampler));
}
s32 idxLightMap[LightMapMgr::cLightMapNum];
s32 idxSampler[LightMapMgr::cLightMapNum];
};
static_assert(sizeof(LightMap) == 0x10, "ModelNW::LightMap size mismatch");
struct Shape {
agl::UniformBlock uniformBlock;
LightMap lightMap;
sead::Buffer<nw::g3d::fnd::GfxBuffer> vtxBuffer;
};
struct DrawInfo {
s32 viewIndex;
const sead::Matrix34f* viewMtx;
const sead::Matrix44f* projMtx;
const agl::ShaderProgram* shaderProgram;
ShaderAssign* shaderAssign;
s32 materialIndex;
bool drawShape;
bool drawReflection;
agl::ShaderMode shaderMode;
s32 polygonOffset;
const CullViewFrustum* cull;
};
static_assert(sizeof(DrawInfo) == 0x28, "ModelNW::DrawInfo size mismatch");
public:
ModelNW();
virtual ~ModelNW();
void calc() override;
void calcGPU(s32 viewIndex, const sead::Matrix34f& viewMtx, const sead::Matrix44f& projMtx, RenderMgr* renderMgr) override;
virtual void updateView(s32 viewIndex, const sead::Matrix34f& viewMtx, const sead::Matrix44f& projMtx, RenderMgr* renderMgr) override;
virtual void drawOpa(s32 viewIndex, const sead::Matrix34f& viewMtx, const sead::Matrix44f& projMtx, RenderMgr* renderMgr) override;
virtual void drawXlu(s32 viewIndex, const sead::Matrix34f& viewMtx, const sead::Matrix44f& projMtx, RenderMgr* renderMgr) override;
virtual void drawShadowOpa(s32 viewIndex, const sead::Matrix34f& viewMtx, const sead::Matrix44f& projMtx, RenderMgr* renderMgr) override;
virtual void drawReflectionOpa(s32 viewIndex, const sead::Matrix34f& viewMtx, const sead::Matrix44f& projMtx, RenderMgr* renderMgr) override;
virtual void drawReflectionXlu(s32 viewIndex, const sead::Matrix34f& viewMtx, const sead::Matrix44f& projMtx, RenderMgr* renderMgr) override;
void updateAnimations() override;
void updateModel() override;
bool hasOpa() const override;
bool hasXlu() const override;
s32 searchBoneIndex(const sead::SafeString& name) const override;
const char* getBoneName(s32 idx) const override;
u32 getBoneNum() const override;
void setBoneLocalMatrix(s32 idx, const sead::Matrix34f& mtx, const sead::Vector3f& scale) override;
void getBoneLocalMatrix(s32 idx, sead::Matrix34f* mtx = nullptr, sead::Vector3f* scale = nullptr) const override;
void setBoneWorldMatrix(s32 idx, const sead::Matrix34f& mtx) override;
void getBoneWorldMatrix(s32 idx, sead::Matrix34f* mtx) const override;
void setBoneVisible(s32 idx, bool visible) override;
bool isBoneVisible(s32 idx) const override; // deleted
u32 getMaterialNum() const override;
s32 searchMaterialIndex(const sead::SafeString& name) const override;
const char* getMaterialName(s32 idx) const override;
Material* getMaterial(s32 idx) const override {
return this->materials[idx];
}
void setMaterialVisible(s32 index, bool visible) override;
bool isMaterialVisible(s32 index) const override; // deleted
void setBoundingEnable(bool enable) override; // TODO
bool isBoundingEnable() const override; // TODO
const sead::Sphere3f& getBounding() const override {
return this->bounding;
}
void calcViewShapeShadowFlags(agl::sdw::DepthShadow* depthShadow, ObjLayer* shadowLayer, RenderMgr* renderMgr) override;
sead::SafeString getName() const override; // TODO
void setSklAnimBlendWeight(s32 index, f32 weight) override {
this->sklAnimBlendWeights[index] = weight;
}
f32 getSklAnimBlendWeight(s32 index) const override {
return this->sklAnimBlendWeights[index];
}
void setSklAnim(s32 index, Animation* anim) override;
void setTexAnim(s32 index, Animation* anim) override;
void setShuAnim(s32 index, Animation* anim) override;
void setVisAnim(s32 index, Animation* anim) override;
void setShaAnim(s32 index, Animation* anim) override;
Animation* const* getSklAnims() const override { return reinterpret_cast<Animation* const*>(this->sklAnims.getBufferPtr()); }
Animation* const* getTexAnims() const override { return reinterpret_cast<Animation* const*>(this->texAnims.getBufferPtr()); }
Animation* const* getShuAnims() const override { return reinterpret_cast<Animation* const*>(this->shuAnims.getBufferPtr()); }
Animation* const* getVisAnims() const override { return reinterpret_cast<Animation* const*>(this->visAnims.getBufferPtr()); }
Animation* const* getShaAnims() const override { return reinterpret_cast<Animation* const*>(this->shaAnims.getBufferPtr()); }
void initialize(nw::g3d::res::ResModel* resModel, const agl::ShaderProgramArchive* shaderArchive, s32 numView, s32 numSklAnim, s32 numTexAnim, s32 numShuAnim, s32 numVisAnim, s32 numShaAnim, u32 boundingMode, sead::Heap* heap);
void activateMaterial(const agl::g3d::ModelShaderAssign& shaderAssign, const nw::g3d::MaterialObj* material, const LightMap& lightMap) const;
void setDisplayListDirty();
void createViewShapeShadowFlagBuffer_(s32 numView, sead::Heap* heap);
void initializeShapeRenderInfo_(ShapeRenderInfo& renderInfo, const nw::g3d::MaterialObj* material, const nw::g3d::ShapeObj* shape);
static s32 sortShapeRenderInfoCmp(const ShapeRenderInfo* a, const ShapeRenderInfo* b);
void calcBounding_();
void applyBlendWeight_(s32 shapeIndex);
static void setBoundingFlagArray_(u32 flagArray[], const SkeletalAnimation& anim);
void drawOpa_(DrawInfo& drawInfo, const RenderMgr* renderMgr) const;
void drawXlu_(DrawInfo& drawInfo, const RenderMgr* renderMgr) const;
void drawShape_(DrawInfo& drawInfo, const ShapeRenderInfo& renderInfo, const RenderMgr* renderMgr) const;
agl::g3d::ModelEx modelEx;
nw::g3d::SkeletalAnimBlender sklAnimBlender;
sead::Buffer<SkeletalAnimation*> sklAnims;
sead::Buffer<TextureAnimation*> texAnims;
sead::Buffer<ShaderAnimation*> shuAnims;
sead::Buffer<VisibilityAnimation*> visAnims;
sead::Buffer<ShapeAnimation*> shaAnims;
void* buffer;
void* blockBuffer;
size_t blockBufferSize;
void* sklAnimBlenderBuffer;
sead::Buffer<f32> sklAnimBlendWeights;
sead::PtrArray<ShapeRenderInfo> opaShapeInfos;
sead::PtrArray<ShapeRenderInfo> xluShapeInfos;
sead::Buffer<ShaderAssign> shaderAssigns;
sead::Buffer<MaterialNW*> materials;
sead::Buffer<Shape> shapes;
sead::Matrix34f mtxRT;
sead::Vector3f scale;
u8 _128;
sead::BitFlag32 renderFlag;
sead::BitFlag32 boundingEnableFlag;
sead::Buffer<sead::Buffer<sead::BitFlag32> > viewShapeShadowFlagBuffer;
sead::Sphere3f bounding;
sead::BoundBox3f* subBounding;
sead::BitFlag32 shapeFlag;
u32 boundingFlagArray[10];
u32 subBoundingFlagArray[10];
sead::BitFlag32 viewDepthShadowEnableFlag;
bool displayListDirty;
};
static_assert(sizeof(ModelNW) == 0x1AC, "ModelNW size mismatch");
class ModelWrapper {
public:
ModelWrapper(Model* model, u32 numSklAnims, u32 numTexPatternAnims, u32 numTexSrtAnims, u32 numVisAnims, u32 numShaAnims);
void setup(ResArchive* archive, void* unk = nullptr, sead::Heap* heap = nullptr);
void updateModel();
void updateAnimations();
void setMtx(const sead::Matrix34f& mtxRT) { this->model->setMtxRT(mtxRT); }
const sead::Matrix34f& getMtx() const { return this->model->getMtxRT(); }
void setScale(const sead::Vector3f& scale) { this->model->setScale(scale); }
const sead::Vector3f& getScale() const { return this->model->getScale(); }
void playSklAnim(const sead::SafeString& identifier, u32 idx = 0) {
if (idx < this->sklAnims.size())
this->sklAnims[idx]->play(this->archive, identifier);
}
void playTexPatternAnim(const sead::SafeString& identifier, u32 idx = 0) {
if (idx < this->texPatternAnims.size())
this->texPatternAnims[idx]->play(this->archive, identifier);
}
void playColorAnim(const sead::SafeString& identifier, u32 idx = 0) {
if (idx < this->texSrtAnims.size())
this->texSrtAnims[idx]->playColorAnim(this->archive, identifier);
}
void playTexSrtAnim(const sead::SafeString& identifier, u32 idx = 0) {
if (idx < this->texSrtAnims.size())
this->texSrtAnims[idx]->playTexSrtAnim(this->archive, identifier);
}
void playBoneVisAnim(const sead::SafeString& identifier, u32 idx = 0) {
if (idx < this->visAnims.size())
this->visAnims[idx]->play(this->archive, identifier);
}
void playShapeAnim(const sead::SafeString& identifier, u32 idx = 0) {
if (idx < this->shaAnims.size())
this->shaAnims[idx]->play(this->archive, identifier);
}
void loopSklAnims(bool loop) {
for (u32 i = 0; i < sklAnims.size(); i++) {
SkeletalAnimation* anim = sklAnims[i];
if (!anim)
return;
anim->frameCtrl.shouldLoop(loop);
}
}
void draw() {
DrawMgr::instance()->drawModel(this);
}
static ModelWrapper* create(const sead::SafeString& archiveIdentifier, const sead::SafeString& modelIdentifier, u32 numSklAnims = 0, u32 numTexPatternAnims = 0, u32 numTexSrtAnims = 0, u32 numVisAnims = 0, u32 numShaAnims = 0, bool unk2 = false) {
ResArchive* archive = ResArchiveMgr::instance()->get(archiveIdentifier);
Model* model = archive->getModel(modelIdentifier, numSklAnims, numTexPatternAnims, numTexSrtAnims, numVisAnims, numShaAnims, unk2, nullptr);
ModelWrapper* wrapper = new ModelWrapper(model, numSklAnims, numTexPatternAnims, numTexSrtAnims, numVisAnims, numShaAnims);
wrapper->setup(archive);
return wrapper;
}
static ModelWrapper* create(ResArchive* archive, const sead::SafeString& modelIdentifier, u32 numSklAnims = 0, u32 numTexPatternAnims = 0, u32 numTexSrtAnims = 0, u32 numVisAnims = 0, u32 numShaAnims = 0, bool unk2 = false) {
Model* model = archive->getModel(modelIdentifier, numSklAnims, numTexPatternAnims, numTexSrtAnims, numVisAnims, numShaAnims, unk2, nullptr);
ModelWrapper* wrapper = new ModelWrapper(model, numSklAnims, numTexPatternAnims, numTexSrtAnims, numVisAnims, numShaAnims);
wrapper->setup(archive);
return wrapper;
}
Model* model; // 0
ResArchive* archive; // 4
sead::Buffer<SkeletalAnimation*> sklAnims; // 8
sead::Buffer<TextureAnimation*> texPatternAnims; // 10
sead::Buffer<ShaderAnimation*> texSrtAnims; // 18
sead::Buffer<VisibilityAnimation*> visAnims; // 20
sead::Buffer<ShapeAnimation*> shaAnims; // 28
};