summaryrefslogtreecommitdiff
path: root/include/m/m3d/m_shadow.h
blob: 963d9f2c951889819d7092760a2c139242878528 (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
#ifndef M3D_M_SHADOW_H
#define M3D_M_SHADOW_H

#include "egg/core/eggFrmHeap.h"
#include "egg/core/eggHeap.h"
#include "m/m3d/m_proc.h"
#include "m/m_color.h"
#include "m/m_frustum.h"
#include "m/m_heap.h"
#include "m/m_mtx.h"
#include "m/m_quat.h"
#include "m/m_vec.h"

namespace m3d {

/**
 * A configuration struct for our global shadow manager.
 * The game uses one for most rooms and one for the Koloktos boss fight.
 */
struct mShadowCircleConfig {
    u32 heapSize;
    u8 count;
    u8 unk1;
    u8 unk2;
    u8 texBufferSize;
    u8 drawOpaPriority;
};

/**
 * A callback that allows the stage manager to customize parts of
 * the shadow pipeline.
 */
class mShadowCallback_c {
public:
    virtual ~mShadowCallback_c() {}
    virtual void beforeDraw() = 0;
    virtual void draw(const mMtx_c &, u32) = 0;
    virtual void afterDraw() = 0;
};

class mShadowCircle_c;

/**
 * Represents an internal shadow circle.
 */
class mShadowChild_c {
    friend class mShadow_c;

public:
    nw4r::ut::Node mNode;

    mShadowChild_c() : mpCircle(nullptr), mpLeaves(nullptr), field_0x154(0), mNumLeaves(0) {}
    /* vt at 0x08 */
    virtual ~mShadowChild_c();

    bool create(u8, EGG::Heap *);
    void set(const mVec3_c &pos, f32 dist, mColor color);
    bool setGeom(const GXTexObj *texObj, const mMtx_c &mtx, const mQuat_c &quat);
    void draw();
    void updateMtx();
    void drawMdl();

    bool addMdl(scnLeaf_c &mdl, const mQuat_c &quat);

    void set0x154(UNKWORD arg) {
        field_0x154 = arg;
    }

    const mQuat_c &GetQuat() const {
        return mQuat;
    }
    const mVec3_c &GetPosition() const {
        return mPositionMaybe;
    }
    const f32 GetOffset() const {
        return mOffsetMaybe;
    }
    const f32 Get0x13C() const {
        return field_0x13C;
    }
    void Set0x13C(const f32 f) {
        field_0x13C = f;
    }

    const mFrustum_c &GetFrustum() const {
        return mFrustum;
    }

private:
    /* 0x00C */ EGG::Heap *mpHeap;
    /* 0x010 */ mShadowCircle_c *mpCircle;
    /* 0x014 */ u32 mPriorityMaybe;
    /* 0x018 */ GXTexObj mTexObj;
    /* 0x038 */ mColor mShadowColor;
    /* 0x03C */ mQuat_c mQuat;
    /* 0x04C */ mFrustum_c mFrustum;
    /* 0x13C */ f32 field_0x13C;
    /* 0x140 */ scnLeaf_c **mpLeaves;
    /* 0x144 */ mVec3_c mPositionMaybe;
    /* 0x150 */ f32 mOffsetMaybe;
    /* 0x154 */ UNKWORD field_0x154;
    /* 0x158 */ u8 mMaxNumLeaves;
    /* 0x159 */ u8 mNumLeaves;
    /* 0x15A */ u8 mColorChanIdx;
};

/**
 * An RAII handle to a shadow circle.
 */
class mShadowCircle_c {
    friend class mShadow_c;
    friend class mShadowChild_c;

public:
    mShadowCircle_c() : mpChild(nullptr) {}
    virtual ~mShadowCircle_c();

private:
    /* 0x04 */ mShadowChild_c *mpChild;
};

/**
 * The global shadow circle manager.
 */
class mShadow_c : public proc_c {
    friend class mShadowCircle_c;
    friend class mShadowChild_c;

public:
    mShadow_c(EGG::Heap *heap)
        : mpHeap(heap),
          mpCurrentHeap(nullptr),
          mpChilds(nullptr),
          mpTexBuf(nullptr),
          mpCallback(nullptr),
          mCurrentHeapIdx(0),
          mFreeChildIdx(0),
          field_0x66(true) {
        nw4r::ut::List_Init(&mList, 0);
    }
    virtual ~mShadow_c();
    virtual void remove() override;
    virtual void drawOpa() override;

    bool drawMdl(
        mShadowCircle_c *circle, u32 priority, scnLeaf_c &mdl, const mQuat_c &quat, mVec3_c &pos, mColor color,
        u32 param9, f32 dist
    );
    bool drawTexObj(
        mShadowCircle_c *circle, u32 priority, const GXTexObj *texObj, const mMtx_c &mtx, const mQuat_c &quat,
        mVec3_c &pos, mColor color, u32 param9, f32 dist
    );

    bool addMdlToCircle(mShadowCircle_c *circle, scnLeaf_c &mdl, const mQuat_c &quat);

    void beforeDraw();
    void draw(const mMtx_c &, u32);
    void afterDraw();

    void changeHeap(int index) {
        mCurrentHeapIdx = index % 2;
        mpCurrentHeap = mpFrmHeaps[mCurrentHeapIdx];
    }
    void swapHeaps();
    void
    create(int count, u8 unk1, int unk2, u16 texBufferSize, u32 drawOpaPriority, nw4r::g3d::ResMdl mdl, u32 heapSize);
    void reset();

    bool addCircle(mShadowCircle_c *circle, u32 priority, u32 unk);
    void removeCircle(mShadowCircle_c *circle);

    void drawAllShadows();

    static void create(const mShadowCircleConfig *, nw4r::g3d::ResMdl mdl, EGG::Heap *heap);
    static void destroy();

    static mShadow_c *GetInstance() {
        return sInstance;
    }

private:
    static mShadow_c *sInstance;

    /* 0x18 */ EGG::Heap *mpHeap;
    /* 0x1C */ mAllocator_c mAllocator;
    /* 0x38 */ EGG::FrmHeap *mpFrmHeaps[2];
    /* 0x40 */ EGG::FrmHeap *mpCurrentHeap;
    /* 0x44 */ nw4r::ut::List mList;
    /* 0x50 */ mShadowChild_c *mpChilds;
    /* 0x54 */ void *mpTexBuf;
    /* 0x58 */ u32 mTexBufferSize;
    /* 0x5C */ mShadowCallback_c *mpCallback;
    /* 0x60 */ u8 mCurrentHeapIdx;
    /* 0x61 */ u8 mChildCount;
    /* 0x62 */ u8 mFreeChildIdx;
    /* 0x63 */ u8 mNumTexBuffers;
    /* 0x64 */ u8 mFreeTexIdx;
    /* 0x65 */ u8 mTexSize;
    /* 0x66 */ bool field_0x66;

public:
    static GXTexObj *sTexObj;
    static nw4r::g3d::ResShp sResShp;
};

class mCustomShadow_c : public scnLeaf_c {
public:
    virtual ~mCustomShadow_c();
    virtual int getType() const override;
    /* 0x24 */ virtual void draw(const mMtx_c &);

    void calc(mMtx_c, mMtx_c &) const;

    /* 0x18 */ mMtx_c mMtx;
    /* 0x48 */ f32 field_0x48;
    /* 0x4C */ f32 field_0x4C;
};

} // namespace m3d

#endif