summaryrefslogtreecommitdiff
path: root/src/nw4r/g3d/g3d_scnroot.cpp
blob: 391d90fa9759ac7148a85b9794b654e3a45f60c6 (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
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
#include "nw4r/g3d/g3d_scnroot.h"

#include "nw4r/g3d.h" // IWYU pragma: export
#include "nw4r/g3d/g3d_scnobj.h"

#include "rvl/GX.h" // IWYU pragma: export

#include <algorithm>
#include <new>

namespace nw4r {
namespace g3d {

NW4R_G3D_RTTI_DEF(ScnRoot);

/******************************************************************************
 *
 * ScnRoot
 *
 ******************************************************************************/
ScnRoot *ScnRoot::Construct(
    MEMAllocator *pAllocator, u32 *pSize, u32 maxChildren, u32 maxScnObj, u32 numLightObj, u32 numLightSet
) {
    ScnRoot *pScnRoot = NULL;
    u32 scnRootSize = sizeof(ScnRoot);
    u32 scnObjGatherSize = sizeof(ScnObjGather);

    u32 childrenSize = maxChildren * sizeof(ScnObj *);
    u32 scnObjBufSize = maxScnObj * sizeof(ScnObj *) + maxScnObj * sizeof(ScnObj *);

    u32 lightObjSize = numLightObj * sizeof(LightObj);
    u32 ambLightObjSize = numLightObj * sizeof(AmbLightObj);
    u32 lightSetDataSize = numLightSet * sizeof(LightSetData);

    u32 scnObjGatherOfs = align4(scnRootSize);
    u32 childrenOfs = align4(scnObjGatherOfs + scnObjGatherSize);
    u32 scnObjBufOfs = align4(childrenOfs + childrenSize);
    u32 lightObjOfs = align4(scnObjBufOfs + scnObjBufSize);
    u32 ambLightOfs = align4(lightObjOfs + lightObjSize);
    u32 lightSetDataOfs = align4(ambLightOfs + ambLightObjSize);

    u32 size = align4(lightSetDataOfs + lightSetDataSize);
    if (pSize != NULL) {
        *pSize = size;
    }

    if (pAllocator != NULL) {
        u8 *pBuffer = reinterpret_cast<u8 *>(Alloc(pAllocator, size));
        if (pBuffer == NULL) {
            return NULL;
        }

        ScnObj **ppScnObjBuf = reinterpret_cast<ScnObj **>(pBuffer + scnObjBufOfs);

        IScnObjGather *pGather =
            new (pBuffer + scnObjGatherOfs) ScnObjGather(ppScnObjBuf, ppScnObjBuf + maxScnObj, maxScnObj);

        LightObj *pLightObjBuf = reinterpret_cast<LightObj *>(pBuffer + lightObjOfs);

        AmbLightObj *pAmbObjBuf = reinterpret_cast<AmbLightObj *>(pBuffer + ambLightOfs);

        LightSetData *pLightSetBuf = reinterpret_cast<LightSetData *>(pBuffer + lightSetDataOfs);

        // clang-format off
        pScnRoot = new (pBuffer) ScnRoot(
            pAllocator,
            pGather,
            reinterpret_cast<ScnObj**>(pBuffer + childrenOfs),
            maxChildren,
            numLightObj,
            numLightSet,
            pLightObjBuf,
            pAmbObjBuf,
            pLightSetBuf);
        // clang-format on
    }

    return pScnRoot;
}

void ScnRoot::G3dProc(u32 task, u32 param, void *pInfo) {
    if (IsG3dProcDisabled(task)) {
        return;
    }

    switch (task) {
        case G3DPROC_CHILD_DETACHED: {
            if (mpAnmScn == pInfo) {
                mpAnmScn->G3dProc(G3DPROC_DETACH_PARENT, 0, this);
                mpAnmScn = NULL;
                break;
            }

            // FALLTHROUGH, detach may be for ScnGroup
        }

        default: {
            DefG3dProcScnGroup(task, param, pInfo);
            break;
        }
    }
}

Camera ScnRoot::GetCamera(int idx) {
    if (0 <= idx && idx < G3DState::NUM_CAMERA) {
        return Camera(&mCamera[idx]);
    }

    return Camera(NULL);
}

Camera ScnRoot::GetCurrentCamera() {
    return Camera(&mCamera[mCurrentCameraID]);
}

void ScnRoot::SetCurrentCamera(int idx) {
    mCurrentCameraID = static_cast<u8>(idx);
}

Fog ScnRoot::GetFog(int idx) {
    if (0 <= idx && idx < G3DState::NUM_FOG) {
        return Fog(&mFog[idx]);
    }

    return Fog(NULL);
}

LightSet ScnRoot::GetLightSet(int idx) {
    return mLightSetting.GetLightSet(idx);
}

void ScnRoot::UpdateFrame() {
    if (mpAnmScn != NULL) {
        mpAnmScn->UpdateFrame();
    }

    G3dProc(G3DPROC_UPDATEFRAME, 0, NULL);
}

void ScnRoot::SetGlbSettings() {
    int i;

    Camera cam = GetCurrentCamera();
    cam.GXSetViewport();
    cam.GXSetProjection();
    cam.GXSetScissor();
    cam.GXSetScissorBoxOffset();

    for (i = 0; i < G3DState::NUM_CAMERA; i++) {
        G3DState::SetCameraProjMtx(Camera(&mCamera[i]), i, i == mCurrentCameraID);
    }

    bool persp = cam.GetProjectionType() != GX_ORTHOGRAPHIC;
    u32 projOrthoBit = persp ? 0 : GX_ORTHOGRAPHIC << 3;

    f32 near = cam.ref().projNear;
    f32 far = cam.ref().projFar;

    u16 width = 0;
    u16 center = 0;

    math::MTX44 proj;
    bool fogRangeAdj = false;

    for (i = 0; i < G3DState::NUM_FOG; i++) {
        Fog fog(&mFog[i]);
        fog.SetNearFar(near, far);

        // Enum Comparison mismatch on purpose
        if (fog.ref().type != GX_PERSPECTIVE) {
            fog.ref().type = static_cast<GXFogType>((fog.ref().type & ~(GX_ORTHOGRAPHIC << 3)) | projOrthoBit);
        }

        if (fog.IsFogRangeAdjEnable()) {
            if (!fogRangeAdj) {
                cam.GetProjectionMtx(&proj);

                f32 f_width, x;
                cam.GetViewport(&x, NULL, &f_width, NULL, NULL, NULL);

                width = math::F32ToU16(f_width);
                center = math::F32ToU16(x + f_width * 0.5f);
                fogRangeAdj = true;
            }

            fog.SetFogRangeAdjParam(width, center, proj);
        }

        G3DState::SetFog(fog, i);
    }

    G3DState::SetLightSetting(mLightSetting);
}

void ScnRoot::CalcAnmScn() {
    if (mpAnmScn == NULL) {
        return;
    }

    const u32 numLightSet = mpAnmScn->GetLightSetMaxRefNumber();
    const u32 numFog = mpAnmScn->GetFogMaxRefNumber();
    const u32 numCamera = mpAnmScn->GetCameraMaxRefNumber();

    if (numLightSet > 0) {
        mpAnmScn->GetLightSetting(&mLightSetting);
    }

    if (numFog > 0) {
        const u32 numLoadableFog = std::min<u32>(G3DState::NUM_FOG, numFog);

        for (u32 i = 0; i < numLoadableFog; i++) {
            Fog fog(&mFog[i]);
            mpAnmScn->GetFog(fog, i);
        }
    }

    if (numCamera > 0) {
        const u32 numLoadableCamera = std::min<u32>(G3DState::NUM_CAMERA, numCamera);

        for (u32 i = 0; i < numLoadableCamera; i++) {
            Camera cam(&mCamera[i]);
            mpAnmScn->GetCamera(cam, i);
        }
    }
}

void ScnRoot::CalcWorld() {
    CalcAnmScn();
    G3dProc(G3DPROC_CALC_WORLD, 0, NULL);
}

void ScnRoot::CalcMaterial() {
    G3dProc(G3DPROC_CALC_MAT, 0, NULL);
}

void ScnRoot::CalcVtx() {
    G3dProc(G3DPROC_CALC_VTX, 0, NULL);
}

void ScnRoot::CalcView() {
    math::MTX34 cam;
    GXInvalidateVtxCache();
    GetCurrentCamera().GetCameraMtx(&cam);
    G3dProc(G3DPROC_CALC_VIEW, 0, &cam);
}

void ScnRoot::GatherDrawScnObj() {
    mpCollection->Clear();

    math::FRUSTUM frustum;
    Camera cam = GetCurrentCamera();

    math::MTX34 mtx;
    cam.GetCameraMtx(&mtx);

    if (cam.ref().flags & CameraData::FLAG_PROJ_PERSP) {
        frustum.Set(cam.ref().projFovy, cam.ref().projAspect, cam.ref().projNear, cam.ref().projFar, mtx);
    } else {
        frustum.Set(
            cam.ref().projTop, cam.ref().projBottom, cam.ref().projLeft, cam.ref().projRight, cam.ref().projNear,
            cam.ref().projFar, mtx
        );
    }

    gpCullingFrustum = &frustum;
    G3dProc(G3DPROC_GATHER_SCNOBJ, 0, mpCollection);
    gpCullingFrustum = NULL;
}

void ScnRoot::ZSort() {
    mpCollection->ZSort();
    G3dProc(G3DPROC_ZSORT, 0, mpCollection);
}

void ScnRoot::DrawOpa() {
    SetGlbSettings();

    if (TestScnRootFlag(SCNROOTFLAG_FORCE_RESMDLDRAWMODE)) {
        mpCollection->DrawOpa(&mDrawMode);
    } else {
        mpCollection->DrawOpa(NULL);
    }

    G3DState::Invalidate(G3DState::INVALIDATE_TEV);
}

void ScnRoot::DrawXlu() {
    SetGlbSettings();

    if (TestScnRootFlag(SCNROOTFLAG_FORCE_RESMDLDRAWMODE)) {
        mpCollection->DrawXlu(&mDrawMode);
    } else {
        mpCollection->DrawXlu(NULL);
    }

    G3DState::Invalidate(G3DState::INVALIDATE_TEV);
}

ScnRoot::ScnRoot(
    MEMAllocator *pAllocator, IScnObjGather *pGather, ScnObj **ppChildrenBuf, u32 maxChildren, u32 numLight,
    u32 numLightSet, LightObj *pLightObjBuf, AmbLightObj *pAmbObjBuf, LightSetData *pLightSetBuf
)
    : ScnGroup(pAllocator, ppChildrenBuf, maxChildren),
      mpCollection(pGather),
      mDrawMode(RESMDL_DRAWMODE_DEFAULT),
      mScnRootFlags(0),
      mCurrentCameraID(0),
      mLightSetting(pLightObjBuf, pAmbObjBuf, numLight, pLightSetBuf, numLightSet),
      mpAnmScn(NULL) {
    for (u32 i = 0; i < G3DState::NUM_CAMERA; i++) {
        Camera res(&mCamera[i]);
        res.Init();
    }

    for (u32 i = 0; i < G3DState::NUM_FOG; i++) {
        Fog res(&mFog[i]);
        res.Init();
    }
}

ScnRoot::~ScnRoot() {
    if (mpAnmScn != NULL) {
        mpAnmScn->G3dProc(G3DPROC_DETACH_PARENT, 0, this);
    }
}

/******************************************************************************
 *
 * Sorting functions
 *
 ******************************************************************************/
namespace {

inline bool LessZSortOpa(const ScnObj *pLhs, const ScnObj *pRhs) {
    int lhsPrio = pLhs->GetPriorityDrawOpa();
    int rhsPrio = pRhs->GetPriorityDrawOpa();

    if (lhsPrio == rhsPrio) {
        return pLhs->GetMtxPtr(ScnObj::MTX_VIEW)->_23 > pRhs->GetMtxPtr(ScnObj::MTX_VIEW)->_23;
    }

    return lhsPrio < rhsPrio;
}

inline bool LessZSortXlu(const ScnObj *pLhs, const ScnObj *pRhs) {
    int lhsPrio = pLhs->GetPriorityDrawXlu();
    int rhsPrio = pRhs->GetPriorityDrawXlu();

    if (lhsPrio == rhsPrio) {
        return pLhs->GetMtxPtr(ScnObj::MTX_VIEW)->_23 < pRhs->GetMtxPtr(ScnObj::MTX_VIEW)->_23;
    }

    return lhsPrio < rhsPrio;
}

inline bool LessByGetValueForSortOpa(const ScnObj *pLhs, const ScnObj *pRhs) {
    int lhsPrio = pLhs->GetPriorityDrawOpa();
    int rhsPrio = pRhs->GetPriorityDrawOpa();

    if (lhsPrio == rhsPrio) {
        return pLhs->GetValueForSortOpa() < pRhs->GetValueForSortOpa();
    }

    return lhsPrio < rhsPrio;
}

inline bool LessByGetValueForSortXlu(const ScnObj *pLhs, const ScnObj *pRhs) {
    int lhsPrio = pLhs->GetPriorityDrawXlu();
    int rhsPrio = pRhs->GetPriorityDrawXlu();

    if (lhsPrio == rhsPrio) {
        return pLhs->GetValueForSortXlu() < pRhs->GetValueForSortXlu();
    }

    return lhsPrio < rhsPrio;
}

} // namespace

/******************************************************************************
 *
 * ScnObjGather
 *
 ******************************************************************************/
IScnObjGather::CullingStatus ScnObjGather::Add(ScnObj *pObj, bool opa, bool xlu) {
    IScnObjGather::CullingStatus status = IScnObjGather::CULLINGSTATUS_INTERSECT;

    math::IntersectionResult ixResult = math::INTERSECTION_INTERSECT;

    if (gpCullingFrustum != NULL) {
        u32 value;
        pObj->GetScnObjOption(ScnObj::OPTION_ENABLE_CULLING, &value);

        if (value) {
            math::AABB aabb;
            pObj->GetBoundingVolume(ScnObj::BOUNDINGVOLUME_AABB_WORLD, &aabb);
            ixResult = gpCullingFrustum->IntersectAABB_Ex(&aabb);

            if (ixResult == math::INTERSECTION_NONE) {
                return IScnObjGather::CULLINGSTATUS_OUTSIDE;
            } else if (ixResult == math::INTERSECTION_INSIDE) {
                status = IScnObjGather::CULLINGSTATUS_INSIDE;
            }
        }
    }

    if (opa) {
        if (mNumScnObjOpa < mSizeScnObj) {
            mpArrayOpa[mNumScnObjOpa++] = pObj;
        } else {
            return status;
        }
    }

    if (xlu) {
        if (mNumScnObjXlu < mSizeScnObj) {
            mpArrayXlu[mNumScnObjXlu++] = pObj;
        } else {
            return status;
        }
    }

    return status;
}

void ScnObjGather::ZSort() {
    std::sort(mpArrayOpa, mpArrayOpa + mNumScnObjOpa, LessZSortOpa);
    std::sort(mpArrayXlu, mpArrayXlu + mNumScnObjXlu, LessZSortXlu);
}

void ScnObjGather::Sort() {
    std::sort(mpArrayOpa, mpArrayOpa + mNumScnObjOpa, LessByGetValueForSortOpa);
    std::sort(mpArrayXlu, mpArrayXlu + mNumScnObjXlu, LessByGetValueForSortXlu);
}

void ScnObjGather::Sort(LessThanFunc pOpaFunc, LessThanFunc pXluFunc) {
    std::sort(mpArrayOpa, mpArrayOpa + mNumScnObjOpa, pOpaFunc);
    std::sort(mpArrayXlu, mpArrayXlu + mNumScnObjXlu, pXluFunc);
}

void ScnObjGather::DrawOpa(ResMdlDrawMode *pForceMode) {
    for (u32 i = 0; i != mNumScnObjOpa;) {
        mpArrayOpa[i++]->G3dProc(G3dObj::G3DPROC_DRAW_OPA, 0, pForceMode);
    }
}

void ScnObjGather::DrawXlu(ResMdlDrawMode *pForceMode) {
    for (u32 i = 0; i != mNumScnObjXlu;) {
        mpArrayXlu[i++]->G3dProc(G3dObj::G3DPROC_DRAW_XLU, 0, pForceMode);
    }
}

ScnObjGather::CheckStatus ScnObjGather::CheckScnObj(ScnObj *) {
    return ScnObjGather::CHECKSTATUS_GATHER_SCNOBJ;
}

ScnObjGather::ScnObjGather(ScnObj **ppBufOpa, ScnObj **ppBufXlu, u32 capacity)
    : mpArrayOpa(ppBufOpa), mpArrayXlu(ppBufXlu), mSizeScnObj(capacity), mNumScnObjOpa(0), mNumScnObjXlu(0) {}

} // namespace g3d
} // namespace nw4r