graph_node.h (19247B)
1 #ifndef GRAPH_NODE_H 2 #define GRAPH_NODE_H 3 4 #include <PR/ultratypes.h> 5 #include <PR/gbi.h> 6 7 #include "types.h" 8 #include "game/memory.h" 9 10 #define GRAPH_RENDER_ACTIVE (1 << 0) 11 #define GRAPH_RENDER_CHILDREN_FIRST (1 << 1) 12 #define GRAPH_RENDER_BILLBOARD (1 << 2) 13 #define GRAPH_RENDER_Z_BUFFER (1 << 3) 14 #define GRAPH_RENDER_INVISIBLE (1 << 4) 15 #define GRAPH_RENDER_HAS_ANIMATION (1 << 5) 16 17 // Whether the node type has a function pointer of type GraphNodeFunc 18 #define GRAPH_NODE_TYPE_FUNCTIONAL 0x100 19 20 // Type used for Bowser and an unused geo function in obj_behaviors.c 21 #define GRAPH_NODE_TYPE_400 0x400 22 23 // The discriminant for different types of geo nodes 24 #define GRAPH_NODE_TYPE_ROOT 0x001 25 #define GRAPH_NODE_TYPE_ORTHO_PROJECTION 0x002 26 #define GRAPH_NODE_TYPE_PERSPECTIVE (0x003 | GRAPH_NODE_TYPE_FUNCTIONAL) 27 #define GRAPH_NODE_TYPE_MASTER_LIST 0x004 28 #define GRAPH_NODE_TYPE_START 0x00A 29 #define GRAPH_NODE_TYPE_LEVEL_OF_DETAIL 0x00B 30 #define GRAPH_NODE_TYPE_SWITCH_CASE (0x00C | GRAPH_NODE_TYPE_FUNCTIONAL) 31 #define GRAPH_NODE_TYPE_CAMERA (0x014 | GRAPH_NODE_TYPE_FUNCTIONAL) 32 #define GRAPH_NODE_TYPE_TRANSLATION_ROTATION 0x015 33 #define GRAPH_NODE_TYPE_TRANSLATION 0x016 34 #define GRAPH_NODE_TYPE_ROTATION 0x017 35 #define GRAPH_NODE_TYPE_OBJECT 0x018 36 #define GRAPH_NODE_TYPE_ANIMATED_PART 0x019 37 #define GRAPH_NODE_TYPE_BILLBOARD 0x01A 38 #define GRAPH_NODE_TYPE_DISPLAY_LIST 0x01B 39 #define GRAPH_NODE_TYPE_SCALE 0x01C 40 #define GRAPH_NODE_TYPE_SHADOW 0x028 41 #define GRAPH_NODE_TYPE_OBJECT_PARENT 0x029 42 #define GRAPH_NODE_TYPE_GENERATED_LIST (0x02A | GRAPH_NODE_TYPE_FUNCTIONAL) 43 #define GRAPH_NODE_TYPE_BACKGROUND (0x02C | GRAPH_NODE_TYPE_FUNCTIONAL) 44 #define GRAPH_NODE_TYPE_HELD_OBJ (0x02E | GRAPH_NODE_TYPE_FUNCTIONAL) 45 #define GRAPH_NODE_TYPE_CULLING_RADIUS 0x02F 46 47 // The number of master lists. A master list determines the order and render 48 // mode with which display lists are drawn. 49 #define GFX_NUM_MASTER_LISTS 8 50 51 // Passed as first argument to a GraphNodeFunc to give information about in 52 // which context it was called and what it is expected to do. 53 #define GEO_CONTEXT_CREATE 0 // called when node is created from a geo command 54 #define GEO_CONTEXT_RENDER 1 // called from rendering_graph_node.c 55 #define GEO_CONTEXT_AREA_UNLOAD 2 // called when unloading an area 56 #define GEO_CONTEXT_AREA_LOAD 3 // called when loading an area 57 #define GEO_CONTEXT_AREA_INIT 4 // called when initializing the 8 areas 58 #define GEO_CONTEXT_HELD_OBJ 5 // called when processing a GraphNodeHeldObj 59 60 // The signature for a function stored in a geo node 61 // The context argument depends on the callContext: 62 // - for GEO_CONTEXT_CREATE it is the AllocOnlyPool from which the node was allocated 63 // - for GEO_CONTEXT_RENDER or GEO_CONTEXT_HELD_OBJ it is the top of the float matrix stack with type Mat4 64 // - for GEO_CONTEXT_AREA_* it is the root geo node 65 typedef Gfx *(*GraphNodeFunc)(s32 callContext, struct GraphNode *node, void *context); 66 67 /** An extension of a graph node that includes a function pointer. 68 * Many graph node types have an update function that gets called 69 * when they are processed. 70 */ 71 struct FnGraphNode { 72 /*0x00*/ struct GraphNode node; 73 /*0x14*/ GraphNodeFunc func; 74 }; 75 76 /** The very root of the geo tree. Specifies the viewport. 77 */ 78 struct GraphNodeRoot { 79 /*0x00*/ struct GraphNode node; 80 /*0x14*/ u8 areaIndex; 81 /*0x15*/ s8 unk15; // ? 82 /*0x16*/ s16 x; 83 /*0x18*/ s16 y; 84 /*0x1A*/ s16 width; // half width, 160 85 /*0x1C*/ s16 height; // half height 86 /*0x1E*/ s16 numViews; // number of entries in mystery array 87 /*0x20*/ struct GraphNode **views; 88 }; 89 90 /** A node that sets up an orthographic projection based on the global 91 * root node. Used to draw the skybox image. 92 */ 93 struct GraphNodeOrthoProjection { 94 /*0x00*/ struct GraphNode node; 95 /*0x14*/ f32 scale; 96 }; 97 98 /** A node that sets up a perspective projection. Used for drawing the 99 * game world. It does not set up the camera position, that is done by 100 * the child of this node, which has type GraphNodeCamera. 101 */ 102 struct GraphNodePerspective { 103 /*0x00*/ struct FnGraphNode fnNode; 104 /*0x18*/ s32 unused; 105 /*0x1C*/ f32 fov; // horizontal field of view in degrees 106 /*0x20*/ s16 near; // near clipping plane 107 /*0x22*/ s16 far; // far clipping plane 108 }; 109 110 /** An entry in the master list. It is a linked list of display lists 111 * carrying a transformation matrix. 112 */ 113 struct DisplayListNode { 114 Mtx *transform; 115 void *displayList; 116 struct DisplayListNode *next; 117 }; 118 119 /** GraphNode that manages the 8 top-level display lists that will be drawn 120 * Each list has its own render mode, so for example water is drawn in a 121 * different master list than opaque objects. 122 * It also sets the z-buffer on before rendering and off after. 123 */ 124 struct GraphNodeMasterList { 125 /*0x00*/ struct GraphNode node; 126 /*0x14*/ struct DisplayListNode *listHeads[GFX_NUM_MASTER_LISTS]; 127 /*0x34*/ struct DisplayListNode *listTails[GFX_NUM_MASTER_LISTS]; 128 }; 129 130 /** Simply used as a parent to group multiple children. 131 * Does not have any additional functionality. 132 */ 133 struct GraphNodeStart { 134 /*0x00*/ struct GraphNode node; 135 }; 136 137 /** GraphNode that only renders its children if the current transformation matrix 138 * has a z-translation (in camera space) greater than minDistance and less than 139 * maxDistance. 140 * Usage examples: Mario has three level's of detail: Normal, low-poly arms only, and fully low-poly 141 * The tower in Whomp's fortress has two levels of detail. 142 */ 143 struct GraphNodeLevelOfDetail { 144 /*0x00*/ struct GraphNode node; 145 /*0x14*/ s16 minDistance; 146 /*0x16*/ s16 maxDistance; 147 }; 148 149 /** GraphNode that renders exactly one of its children. 150 * Which one is rendered is determined by the field 'selectedCase' 151 * which is set in the node's function. 152 * Usage examples: room visibility, coin animation, blinking, Mario's power-up / hand pose / cap 153 */ 154 struct GraphNodeSwitchCase { 155 /*0x00*/ struct FnGraphNode fnNode; 156 /*0x18*/ s32 unused; 157 /*0x1C*/ s16 numCases; 158 /*0x1E*/ s16 selectedCase; 159 }; 160 161 /** 162 * GraphNode that specifies the location and aim of the camera. 163 * When the roll is 0, the up vector is (0, 1, 0). 164 */ 165 struct GraphNodeCamera { 166 /*0x00*/ struct FnGraphNode fnNode; 167 /*0x18*/ union { 168 // When the node is created, a mode is assigned to the node. 169 // Later in geo_camera_main a Camera is allocated, 170 // the mode is passed to the struct, and the field is overridden 171 // by a pointer to the struct. Gotta save those 4 bytes. 172 s32 mode; 173 struct Camera *camera; 174 } config; 175 /*0x1C*/ Vec3f pos; 176 /*0x28*/ Vec3f focus; 177 /*0x34*/ Mat4 *matrixPtr; // pointer to look-at matrix of this camera as a Mat4 178 /*0x38*/ s16 roll; // roll in look at matrix. Doesn't account for light direction unlike rollScreen. 179 /*0x3A*/ s16 rollScreen; // rolls screen while keeping the light direction consistent 180 }; 181 182 /** GraphNode that translates and rotates its children. 183 * Usage example: wing cap wings. 184 * There is a dprint function that sets the translation and rotation values 185 * based on the ENEMYINFO array. 186 * The display list can be null, in which case it won't draw anything itself. 187 */ 188 struct GraphNodeTranslationRotation { 189 /*0x00*/ struct GraphNode node; 190 /*0x14*/ void *displayList; 191 /*0x18*/ Vec3s translation; 192 /*0x1E*/ Vec3s rotation; 193 }; 194 195 /** GraphNode that translates itself and its children. 196 * Usage example: SUPER MARIO logo letters in debug level select. 197 * The display list can be null, in which case it won't draw anything itself. 198 */ 199 struct GraphNodeTranslation { 200 /*0x00*/ struct GraphNode node; 201 /*0x14*/ void *displayList; 202 /*0x18*/ Vec3s translation; 203 u8 filler[2]; 204 }; 205 206 /** GraphNode that rotates itself and its children. 207 * Usage example: Mario torso / head rotation. Its parameters are dynamically 208 * set by a parent script node in that case. 209 * The display list can be null, in which case it won't draw anything itself. 210 */ 211 struct GraphNodeRotation { 212 /*0x00*/ struct GraphNode node; 213 /*0x14*/ void *displayList; 214 /*0x18*/ Vec3s rotation; 215 u8 filler[2]; 216 }; 217 218 /** GraphNode part that transforms itself and its children based on animation 219 * data. This animation data is not stored in the node itself but in global 220 * variables that are set when object nodes are processed if the object has 221 * animation. 222 * Used for Mario, enemies and anything else with animation data. 223 * The display list can be null, in which case it won't draw anything itself. 224 */ 225 struct GraphNodeAnimatedPart { 226 /*0x00*/ struct GraphNode node; 227 /*0x14*/ void *displayList; 228 /*0x18*/ Vec3s translation; 229 }; 230 231 /** A GraphNode that draws a display list rotated in a way to always face the 232 * camera. Note that if the entire object is a billboard (like a coin or 1-up) 233 * then it simply sets the billboard flag for the entire object, this node is 234 * used for billboard parts (like a chuckya or goomba body). 235 */ 236 struct GraphNodeBillboard { 237 /*0x00*/ struct GraphNode node; 238 /*0x14*/ void *displayList; 239 /*0x18*/ Vec3s translation; 240 }; 241 242 /** A GraphNode that simply draws a display list without doing any 243 * transformation beforehand. It does inherit the parent's transformation. 244 */ 245 struct GraphNodeDisplayList { 246 /*0x00*/ struct GraphNode node; 247 /*0x14*/ void *displayList; 248 }; 249 250 /** GraphNode part that scales itself and its children. 251 * Usage example: Mario's fist or shoe, which grows when attacking. This can't 252 * be done with an animated part sine animation data doesn't support scaling. 253 * Note that many scaling animations (like a goomba getting stomped) happen on 254 * the entire object. This node is only used when a single part needs to be scaled. 255 * There is also a level command that scales the entire level, used for THI. 256 * The display list can be null, in which case it won't draw anything itself. 257 */ 258 struct GraphNodeScale { 259 /*0x00*/ struct GraphNode node; 260 /*0x14*/ void *displayList; 261 /*0x18*/ f32 scale; 262 }; 263 264 /** GraphNode that draws a shadow under an object. 265 * Every object starts with a shadow node. 266 * The shadow type determines the shape (round or rectangular), vertices (4 or 9) 267 * and other features. 268 */ 269 struct GraphNodeShadow { 270 /*0x00*/ struct GraphNode node; 271 /*0x14*/ s16 shadowScale; // diameter (when a circle) or side (when a square) of shadow 272 /*0x16*/ u8 shadowSolidity; // opacity of shadow, 255 = opaque 273 /*0x17*/ u8 shadowType; // see ShadowType enum in shadow.h 274 }; 275 276 /** GraphNode that contains as its sharedChild a group node containing all 277 * object nodes. 278 */ 279 struct GraphNodeObjectParent { 280 /*0x00*/ struct GraphNode node; 281 /*0x14*/ struct GraphNode *sharedChild; 282 }; 283 284 /** GraphNode that draws display lists not directly in memory but generated by 285 * a function. 286 * Used for wobbling paintings, water, environment effects. 287 * It might not draw anything, it could also just update something. 288 * For example: there is a node that stops water flow when the game is paused. 289 * The parameter field gives extra context info. For shifting sand or paintings, 290 * it can determine which texture to use. 291 */ 292 struct GraphNodeGenerated { 293 /*0x00*/ struct FnGraphNode fnNode; 294 /*0x18*/ u32 parameter; // extra context for the function 295 }; 296 297 /** GraphNode that draws a background image or a rectangle of a color. 298 * Drawn in an orthographic projection, used for skyboxes. 299 */ 300 struct GraphNodeBackground { 301 /*0x00*/ struct FnGraphNode fnNode; 302 /*0x18*/ s32 unused; 303 /*0x1C*/ s32 background; // background ID, or rgba5551 color if fnNode.func is null 304 }; 305 306 /** Renders the object that Mario is holding. 307 */ 308 struct GraphNodeHeldObject { 309 /*0x00*/ struct FnGraphNode fnNode; 310 /*0x18*/ s32 playerIndex; 311 /*0x1C*/ struct Object *objNode; 312 /*0x20*/ Vec3s translation; 313 }; 314 315 /** A node that allows an object to specify a different culling radius than the 316 * default one of 300. For this to work, it needs to be a direct child of the 317 * object node. Used for very large objects, such as shock wave rings that Bowser 318 * creates, tornadoes, the big eel. 319 */ 320 struct GraphNodeCullingRadius { 321 /*0x00*/ struct GraphNode node; 322 /*0x14*/ s16 cullingRadius; // specifies the 'sphere radius' for purposes of frustum culling 323 u8 filler[2]; 324 }; 325 326 extern struct GraphNodeMasterList *gCurGraphNodeMasterList; 327 extern struct GraphNodePerspective *gCurGraphNodeCamFrustum; 328 extern struct GraphNodeCamera *gCurGraphNodeCamera; 329 extern struct GraphNodeHeldObject *gCurGraphNodeHeldObject; 330 extern u16 gAreaUpdateCounter; 331 332 extern struct GraphNode *gCurRootGraphNode; 333 extern struct GraphNode *gCurGraphNodeList[]; 334 335 extern s16 gCurGraphNodeIndex; 336 337 extern Vec3f gVec3fZero; 338 extern Vec3s gVec3sZero; 339 extern Vec3f gVec3fOne; 340 extern Vec3s gVec3sOne; 341 342 void init_scene_graph_node_links(struct GraphNode *graphNode, s32 type); 343 344 struct GraphNodeRoot *init_graph_node_root(struct AllocOnlyPool *pool, struct GraphNodeRoot *graphNode, 345 s16 areaIndex, s16 x, s16 y, s16 width, s16 height); 346 struct GraphNodeOrthoProjection *init_graph_node_ortho_projection(struct AllocOnlyPool *pool, struct GraphNodeOrthoProjection *graphNode, f32 scale); 347 struct GraphNodePerspective *init_graph_node_perspective(struct AllocOnlyPool *pool, struct GraphNodePerspective *graphNode, 348 f32 fov, s16 near, s16 far, GraphNodeFunc nodeFunc, s32 unused); 349 struct GraphNodeStart *init_graph_node_start(struct AllocOnlyPool *pool, struct GraphNodeStart *graphNode); 350 struct GraphNodeMasterList *init_graph_node_master_list(struct AllocOnlyPool *pool, struct GraphNodeMasterList *graphNode, s16 on); 351 struct GraphNodeLevelOfDetail *init_graph_node_render_range(struct AllocOnlyPool *pool, struct GraphNodeLevelOfDetail *graphNode, 352 s16 minDistance, s16 maxDistance); 353 struct GraphNodeSwitchCase *init_graph_node_switch_case(struct AllocOnlyPool *pool, struct GraphNodeSwitchCase *graphNode, 354 s16 numCases, s16 selectedCase, GraphNodeFunc nodeFunc, s32 unused); 355 struct GraphNodeCamera *init_graph_node_camera(struct AllocOnlyPool *pool, struct GraphNodeCamera *graphNode, 356 f32 *pos, f32 *focus, GraphNodeFunc func, s32 mode); 357 struct GraphNodeTranslationRotation *init_graph_node_translation_rotation(struct AllocOnlyPool *pool, struct GraphNodeTranslationRotation *graphNode, 358 s32 drawingLayer, void *displayList, Vec3s translation, Vec3s rotation); 359 struct GraphNodeTranslation *init_graph_node_translation(struct AllocOnlyPool *pool, struct GraphNodeTranslation *graphNode, 360 s32 drawingLayer, void *displayList, Vec3s translation); 361 struct GraphNodeRotation *init_graph_node_rotation(struct AllocOnlyPool *pool, struct GraphNodeRotation *graphNode, 362 s32 drawingLayer, void *displayList, Vec3s rotation); 363 struct GraphNodeScale *init_graph_node_scale(struct AllocOnlyPool *pool, struct GraphNodeScale *graphNode, 364 s32 drawingLayer, void *displayList, f32 scale); 365 struct GraphNodeObject *init_graph_node_object(struct AllocOnlyPool *pool, struct GraphNodeObject *graphNode, 366 struct GraphNode *sharedChild, Vec3f pos, Vec3s angle, Vec3f scale); 367 struct GraphNodeCullingRadius *init_graph_node_culling_radius(struct AllocOnlyPool *pool, struct GraphNodeCullingRadius *graphNode, s16 radius); 368 struct GraphNodeAnimatedPart *init_graph_node_animated_part(struct AllocOnlyPool *pool, struct GraphNodeAnimatedPart *graphNode, 369 s32 drawingLayer, void *displayList, Vec3s translation); 370 struct GraphNodeBillboard *init_graph_node_billboard(struct AllocOnlyPool *pool, struct GraphNodeBillboard *graphNode, 371 s32 drawingLayer, void *displayList, Vec3s translation); 372 struct GraphNodeDisplayList *init_graph_node_display_list(struct AllocOnlyPool *pool, struct GraphNodeDisplayList *graphNode, 373 s32 drawingLayer, void *displayList); 374 struct GraphNodeShadow *init_graph_node_shadow(struct AllocOnlyPool *pool, struct GraphNodeShadow *graphNode, 375 s16 shadowScale, u8 shadowSolidity, u8 shadowType); 376 struct GraphNodeObjectParent *init_graph_node_object_parent(struct AllocOnlyPool *pool, struct GraphNodeObjectParent *sp1c, 377 struct GraphNode *sharedChild); 378 struct GraphNodeGenerated *init_graph_node_generated(struct AllocOnlyPool *pool, struct GraphNodeGenerated *sp1c, 379 GraphNodeFunc gfxFunc, s32 parameter); 380 struct GraphNodeBackground *init_graph_node_background(struct AllocOnlyPool *pool, struct GraphNodeBackground *sp1c, 381 u16 background, GraphNodeFunc backgroundFunc, s32 zero); 382 struct GraphNodeHeldObject *init_graph_node_held_object(struct AllocOnlyPool *pool, struct GraphNodeHeldObject *sp1c, 383 struct Object *objNode, Vec3s translation, 384 GraphNodeFunc nodeFunc, s32 playerIndex); 385 struct GraphNode *geo_add_child(struct GraphNode *parent, struct GraphNode *childNode); 386 struct GraphNode *geo_remove_child(struct GraphNode *graphNode); 387 struct GraphNode *geo_make_first_child(struct GraphNode *newFirstChild); 388 389 void geo_call_global_function_nodes_helper(struct GraphNode *graphNode, s32 callContext); 390 void geo_call_global_function_nodes(struct GraphNode *graphNode, s32 callContext); 391 392 void geo_reset_object_node(struct GraphNodeObject *graphNode); 393 void geo_obj_init(struct GraphNodeObject *graphNode, void *sharedChild, Vec3f pos, Vec3s angle); 394 void geo_obj_init_spawninfo(struct GraphNodeObject *graphNode, struct SpawnInfo *spawn); 395 void geo_obj_init_animation(struct GraphNodeObject *graphNode, struct Animation **animPtrAddr); 396 void geo_obj_init_animation_accel(struct GraphNodeObject *graphNode, struct Animation **animPtrAddr, u32 animAccel); 397 398 s32 retrieve_animation_index(s32 frame, u16 **attributes); 399 400 s16 geo_update_animation_frame(struct AnimInfo *obj, s32 *accelAssist); 401 void geo_retreive_animation_translation(struct GraphNodeObject *obj, Vec3f position); 402 403 struct GraphNodeRoot *geo_find_root(struct GraphNode *graphNode); 404 405 // graph_node_manager 406 s16 *read_vec3s_to_vec3f(Vec3f, s16 *src); 407 s16 *read_vec3s(Vec3s dst, s16 *src); 408 s16 *read_vec3s_angle(Vec3s dst, s16 *src); 409 void register_scene_graph_node(struct GraphNode *graphNode); 410 411 #endif // GRAPH_NODE_H