summaryrefslogtreecommitdiff
path: root/mm/src/code/z_debug_display.c
blob: 7f1eeebb46ed15ff15f3d5a08046a46d75605b45 (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
#include "z64debug_display.h"
#include "global.h"

DebugDispObject* sDebugObjectListHead;

typedef struct {
    /* 0x0 */ s16 drawType;  // indicates which draw function to use when displaying the object
    /* 0x4 */ void* drawArg; // segment address (display list or texture) passed to the draw funciton when called
} DebugDispObjectInfo;       // size = 0x8

typedef void (*DebugDispObjectDrawFunc)(DebugDispObject*, void*, PlayState*);

void DebugDisplay_DrawSpriteI8(DebugDispObject* dispObj, void* texture, PlayState* play);
void DebugDisplay_DrawPolygon(DebugDispObject* dispObj, void* dList, PlayState* play);
Gfx* DebugDisplay_PathDisplayList(GraphicsContext* gfxCtx, Path* path);

DebugDispObject* DebugDisplay_Init(void) {
    sDebugObjectListHead = NULL;
    return sDebugObjectListHead;
}

DebugDispObject* DebugDisplay_AddObject(f32 posX, f32 posY, f32 posZ, s16 rotX, s16 rotY, s16 rotZ, f32 scaleX,
                                        f32 scaleY, f32 scaleZ, u8 red, u8 green, u8 blue, u8 alpha, s16 type,
                                        GraphicsContext* gfxCtx) {
    DebugDispObject* oldHead = sDebugObjectListHead;

    sDebugObjectListHead = GRAPH_ALLOC(gfxCtx, sizeof(DebugDispObject));
    sDebugObjectListHead->pos.x = posX;
    sDebugObjectListHead->pos.y = posY;
    sDebugObjectListHead->pos.z = posZ;
    sDebugObjectListHead->rot.x = rotX;
    sDebugObjectListHead->rot.y = rotY;
    sDebugObjectListHead->rot.z = rotZ;
    sDebugObjectListHead->scale.x = scaleX;
    sDebugObjectListHead->scale.y = scaleY;
    sDebugObjectListHead->scale.z = scaleZ;
    sDebugObjectListHead->color.r = red;
    sDebugObjectListHead->color.g = green;
    sDebugObjectListHead->color.b = blue;
    sDebugObjectListHead->color.a = alpha;
    sDebugObjectListHead->type = type;
    sDebugObjectListHead->next = oldHead;
    return sDebugObjectListHead;
}

#include "code/debug_display/debug_display.h"

DebugDispObjectDrawFunc sDebugObjectDrawFuncTable[] = { DebugDisplay_DrawSpriteI8, DebugDisplay_DrawPolygon };

DebugDispObjectInfo sDebugObjectInfoTable[] = {
    { 0, sDebugDisplayCircleTex }, { 0, sDebugDisplayCrossTex }, { 0, sDebugDisplayBallTex },
    { 0, sDebugDisplayCursorTex }, { 1, sDebugDisplay1DL },      { 1, sDebugDisplay3DL },
    { 1, sDebugDisplay2DL },
};

void DebugDisplay_DrawObjects(PlayState* play) {
    DebugDispObject* dispObj = sDebugObjectListHead;
    DebugDispObjectInfo* objInfo;

    while (dispObj != NULL) {
        objInfo = &sDebugObjectInfoTable[dispObj->type];
        sDebugObjectDrawFuncTable[objInfo->drawType](dispObj, objInfo->drawArg, play);
        dispObj = dispObj->next;
    }
}

void DebugDisplay_DrawSpriteI8(DebugDispObject* dispObj, void* texture, PlayState* play) {
    OPEN_DISPS(play->state.gfxCtx);

    Gfx_SetupDL47_Xlu(play->state.gfxCtx);

    gDPSetPrimColor(POLY_XLU_DISP++, 0, 0, dispObj->color.r, dispObj->color.g, dispObj->color.b, dispObj->color.a);
    Matrix_Translate(dispObj->pos.x, dispObj->pos.y, dispObj->pos.z, MTXMODE_NEW);
    Matrix_Scale(dispObj->scale.x, dispObj->scale.y, dispObj->scale.z, MTXMODE_APPLY);
    Matrix_Mult(&play->billboardMtxF, MTXMODE_APPLY);
    Matrix_RotateZYX(dispObj->rot.x, dispObj->rot.y, dispObj->rot.z, MTXMODE_APPLY);

    gDPLoadTextureBlock(POLY_XLU_DISP++, texture, G_IM_FMT_I, G_IM_SIZ_8b, 16, 16, 0, G_TX_NOMIRROR | G_TX_WRAP,
                        G_TX_NOMIRROR | G_TX_WRAP, G_TX_NOMASK, G_TX_NOMASK, G_TX_NOLOD, G_TX_NOLOD);

    MATRIX_FINALIZE_AND_LOAD(POLY_XLU_DISP++, play->state.gfxCtx);

    gSPDisplayList(POLY_XLU_DISP++, sDebugDisplaySpriteDL);

    CLOSE_DISPS(play->state.gfxCtx);
}

Lights1 sDebugDisplayLight1 = gdSPDefLights1(128, 128, 128, 255, 255, 255, 73, 73, 73);

void DebugDisplay_DrawPolygon(DebugDispObject* dispObj, void* dList, PlayState* play) {
    OPEN_DISPS(play->state.gfxCtx);

    Gfx_SetupDL4_Xlu(play->state.gfxCtx);

    gDPSetPrimColor(POLY_XLU_DISP++, 0, 0, dispObj->color.r, dispObj->color.g, dispObj->color.b, dispObj->color.a);

    gSPSetLights1(POLY_XLU_DISP++, sDebugDisplayLight1);

    Matrix_SetTranslateRotateYXZ(dispObj->pos.x, dispObj->pos.y, dispObj->pos.z, &dispObj->rot);
    Matrix_Scale(dispObj->scale.x, dispObj->scale.y, dispObj->scale.z, MTXMODE_APPLY);
    MATRIX_FINALIZE_AND_LOAD(POLY_XLU_DISP++, play->state.gfxCtx);

    gSPDisplayList(POLY_XLU_DISP++, dList);
    CLOSE_DISPS(play->state.gfxCtx);
}

/**
 * Visualise a path, see DebugDisplay_PathDisplayList for details.
 */

void DebugDisplay_DrawPath(PlayState* play, Path* path) {
    s32 pad;

    OPEN_DISPS(play->state.gfxCtx);

    Gfx_SetupDL38_Xlu(play->state.gfxCtx);
    gSPMatrix(POLY_XLU_DISP++, &gIdentityMtx, G_MTX_NOPUSH | G_MTX_LOAD | G_MTX_MODELVIEW);
    gSPDisplayList(POLY_XLU_DISP++, DebugDisplay_PathDisplayList(play->state.gfxCtx, path));

    CLOSE_DISPS(play->state.gfxCtx);
}

#define R_DRAW_PATH_SEGMENT 0  // bREG(82)
#define R_DRAW_PATH_SCALE 1.0f // (1.0f + 0.1f * bREG(83))
#define R_DRAW_PATH_RED 0      // bREG(84)
#define R_DRAW_PATH_GREEN 0    // bREG(85)
#define R_DRAW_PATH_BLUE 0     // bREG(86)
#define R_DRAW_PATH_ALPHA 0    // bREG(87)

/**
 * Offsets of the points of the triaxial shape used for visualising paths
 */
Vec3s sDebugPathVtxOffsets[][2] = {
    { { 20, 0, 0 }, { -20, 0, 0 } },
    { { 0, 20, 0 }, { 0, -20, 0 } },
    { { 0, 0, 20 }, { 0, 0, -20 } },
};

/**
 * Constructs a displaylist to show a Path, by extruding the three-axis shape defined by `sPathDebugVtx` along it.
 */
Gfx* DebugDisplay_PathDisplayList(GraphicsContext* gfxCtx, Path* path) {
    Gfx* gfxHead;
    Gfx* gfx;
    Vtx* curVtx;
    Vtx* curBaseVtx;  // First Vtx for the current point
    Vtx* prevBaseVtx; // First Vtx for the previous point
    Vec3s* curPoint;
    s32 segment; // of path ending at the current point, 1-indexed

    if (path != NULL) {
        // (count - 1) segments, 1 gSPVertex and 3 gSP2Triangles for each, plus a gSPEndDisplayList
        gfx = GRAPH_ALLOC(gfxCtx, ((path->count - 1) * 4 + 1) * sizeof(Gfx));
        curVtx = GRAPH_ALLOC(gfxCtx, path->count * 6 * sizeof(Vtx));

        gfxHead = gfx;
        curBaseVtx = curVtx;
        curPoint = Lib_SegmentedToVirtual(path->points);

        for (segment = 0; segment < path->count; segment++, curPoint++) {
            s32 i;
            s32 j;

            prevBaseVtx = curBaseVtx;
            curBaseVtx = curVtx;

            // Add a vertex for each point in the triaxial shape
            for (i = 0; i < ARRAY_COUNT(sDebugPathVtxOffsets); i++) {
                for (j = 0; j < ARRAY_COUNT(sDebugPathVtxOffsets[0]); j++) {
                    curVtx->v.ob[0] = curPoint->x + (s32)(sDebugPathVtxOffsets[i][j].x * R_DRAW_PATH_SCALE);
                    curVtx->v.ob[1] = curPoint->y + (s32)(sDebugPathVtxOffsets[i][j].y * R_DRAW_PATH_SCALE);
                    curVtx->v.ob[2] = curPoint->z + (s32)(sDebugPathVtxOffsets[i][j].z * R_DRAW_PATH_SCALE);
                    curVtx->v.flag = 0;
                    curVtx->v.tc[0] = 0;
                    curVtx->v.tc[1] = 0;
                    curVtx->v.cn[0] = 128 + R_DRAW_PATH_RED;
                    curVtx->v.cn[1] = 128 + R_DRAW_PATH_GREEN;
                    curVtx->v.cn[2] = 128 + R_DRAW_PATH_BLUE;
                    curVtx->v.cn[3] = 128 + R_DRAW_PATH_ALPHA;
                    curVtx++;
                }
            }

            // Draw the path segment ending at point `R_DRAW_PATH_SEGMENT`, or the whole path if `R_DRAW_PATH_SEGMENT`
            // is 0
            if (R_DRAW_PATH_SEGMENT == 0) {
                if (segment > 0) {
                    gSPVertex(gfx++, prevBaseVtx, 12, 0);
                    gSP2Triangles(gfx++, 0, 7, 1, 0, 0, 6, 7, 0);
                    gSP2Triangles(gfx++, 2, 3, 8, 0, 3, 9, 8, 0);
                    gSP2Triangles(gfx++, 4, 11, 10, 0, 4, 5, 11, 0);
                }
            } else {
                if ((segment > 0) && (segment == R_DRAW_PATH_SEGMENT)) {
                    gSPVertex(gfx++, prevBaseVtx, 12, 0);
                    gSP2Triangles(gfx++, 0, 7, 1, 0, 0, 6, 7, 0);
                    gSP2Triangles(gfx++, 2, 3, 8, 0, 3, 9, 8, 0);
                    gSP2Triangles(gfx++, 4, 11, 10, 0, 4, 5, 11, 0);
                }
            }
        }

        gSPEndDisplayList(gfx++);
    } else {
        // No path, trivial displaylist
        gfx = gfxHead = GRAPH_ALLOC(gfxCtx, sizeof(Gfx));
        gSPEndDisplayList(gfx++);
    }
    return gfxHead;
}