forked from n64decomp/sm64
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph_node.c
894 lines (752 loc) · 30.6 KB
/
graph_node.c
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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
#include <ultra64.h>
#include "sm64.h"
#include "game/level_update.h"
#include "math_util.h"
#include "game/memory.h"
#include "graph_node.h"
#include "game/rendering_graph_node.h"
#include "game/area.h"
#include "geo_layout.h"
// unused Mtx(s)
s16 identityMtx[4][4] = { { 1, 0, 0, 0 }, { 0, 1, 0, 0 }, { 0, 0, 1, 0 }, { 0, 0, 0, 1 } };
s16 zeroMtx[4][4] = { { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 } };
Vec3f gVec3fZero = { 0.0f, 0.0f, 0.0f };
Vec3s gVec3sZero = { 0, 0, 0 };
Vec3f gVec3fOne = { 1.0f, 1.0f, 1.0f };
UNUSED Vec3s gVec3sOne = { 1, 1, 1 };
/**
* Initialize a geo node with a given type. Sets all links such that there
* are no siblings, parent or children for this node.
*/
void init_scene_graph_node_links(struct GraphNode *graphNode, s32 type) {
graphNode->type = type;
graphNode->flags = GRAPH_RENDER_ACTIVE;
graphNode->prev = graphNode;
graphNode->next = graphNode;
graphNode->parent = NULL;
graphNode->children = NULL;
}
/**
* Allocated and returns a newly created root node
*/
struct GraphNodeRoot *init_graph_node_root(struct AllocOnlyPool *pool, struct GraphNodeRoot *graphNode,
s16 areaIndex, s16 x, s16 y, s16 width, s16 height) {
if (pool != NULL) {
graphNode = alloc_only_pool_alloc(pool, sizeof(struct GraphNodeRoot));
}
if (graphNode != NULL) {
init_scene_graph_node_links(&graphNode->node, GRAPH_NODE_TYPE_ROOT);
graphNode->areaIndex = areaIndex;
graphNode->unk15 = 0;
graphNode->x = x;
graphNode->y = y;
graphNode->width = width;
graphNode->height = height;
graphNode->views = NULL;
graphNode->numViews = 0;
}
return graphNode;
}
/**
* Allocates and returns a newly created otrhographic projection node
*/
struct GraphNodeOrthoProjection *
init_graph_node_ortho_projection(struct AllocOnlyPool *pool, struct GraphNodeOrthoProjection *graphNode,
f32 scale) {
if (pool != NULL) {
graphNode = alloc_only_pool_alloc(pool, sizeof(struct GraphNodeOrthoProjection));
}
if (graphNode != NULL) {
init_scene_graph_node_links(&graphNode->node, GRAPH_NODE_TYPE_ORTHO_PROJECTION);
graphNode->scale = scale;
}
return graphNode;
}
/**
* Allocates and returns a newly created perspective node
*/
struct GraphNodePerspective *init_graph_node_perspective(struct AllocOnlyPool *pool,
struct GraphNodePerspective *graphNode,
f32 fov, s16 near, s16 far,
GraphNodeFunc nodeFunc, s32 unused) {
if (pool != NULL) {
graphNode = alloc_only_pool_alloc(pool, sizeof(struct GraphNodePerspective));
}
if (graphNode != NULL) {
init_scene_graph_node_links(&graphNode->fnNode.node, GRAPH_NODE_TYPE_PERSPECTIVE);
graphNode->fov = fov;
graphNode->near = near;
graphNode->far = far;
graphNode->fnNode.func = nodeFunc;
graphNode->unused = unused;
if (nodeFunc != NULL) {
nodeFunc(GEO_CONTEXT_CREATE, &graphNode->fnNode.node, pool);
}
}
return graphNode;
}
/**
* Allocates and returns a newly created start node
*/
struct GraphNodeStart *init_graph_node_start(struct AllocOnlyPool *pool,
struct GraphNodeStart *graphNode) {
if (pool != NULL) {
graphNode = alloc_only_pool_alloc(pool, sizeof(struct GraphNodeStart));
}
if (graphNode != NULL) {
init_scene_graph_node_links(&graphNode->node, GRAPH_NODE_TYPE_START);
}
return graphNode;
}
/**
* Allocates and returns a newly created master list node
*/
struct GraphNodeMasterList *init_graph_node_master_list(struct AllocOnlyPool *pool,
struct GraphNodeMasterList *graphNode, s16 on) {
if (pool != NULL) {
graphNode = alloc_only_pool_alloc(pool, sizeof(struct GraphNodeMasterList));
}
if (graphNode != NULL) {
init_scene_graph_node_links(&graphNode->node, GRAPH_NODE_TYPE_MASTER_LIST);
if (on) {
graphNode->node.flags |= GRAPH_RENDER_Z_BUFFER;
}
}
return graphNode;
}
/**
* Allocates and returns a newly created render range node
*/
struct GraphNodeLevelOfDetail *init_graph_node_render_range(struct AllocOnlyPool *pool,
struct GraphNodeLevelOfDetail *graphNode,
s16 minDistance, s16 maxDistance) {
if (pool != NULL) {
graphNode = alloc_only_pool_alloc(pool, sizeof(struct GraphNodeLevelOfDetail));
}
if (graphNode != NULL) {
init_scene_graph_node_links(&graphNode->node, GRAPH_NODE_TYPE_LEVEL_OF_DETAIL);
graphNode->minDistance = minDistance;
graphNode->maxDistance = maxDistance;
}
return graphNode;
}
/**
* Allocates and returns a newly created switch case node
*/
struct GraphNodeSwitchCase *init_graph_node_switch_case(struct AllocOnlyPool *pool,
struct GraphNodeSwitchCase *graphNode,
s16 numCases, s16 selectedCase,
GraphNodeFunc nodeFunc, s32 unused) {
if (pool != NULL) {
graphNode = alloc_only_pool_alloc(pool, sizeof(struct GraphNodeSwitchCase));
}
if (graphNode != NULL) {
init_scene_graph_node_links(&graphNode->fnNode.node, GRAPH_NODE_TYPE_SWITCH_CASE);
graphNode->numCases = numCases;
graphNode->selectedCase = selectedCase;
graphNode->fnNode.func = nodeFunc;
graphNode->unused = unused;
if (nodeFunc != NULL) {
nodeFunc(GEO_CONTEXT_CREATE, &graphNode->fnNode.node, pool);
}
}
return graphNode;
}
/**
* Allocates and returns a newly created camera node
*/
struct GraphNodeCamera *init_graph_node_camera(struct AllocOnlyPool *pool,
struct GraphNodeCamera *graphNode, f32 *pos,
f32 *focus, GraphNodeFunc func, s32 mode) {
if (pool != NULL) {
graphNode = alloc_only_pool_alloc(pool, sizeof(struct GraphNodeCamera));
}
if (graphNode != NULL) {
init_scene_graph_node_links(&graphNode->fnNode.node, GRAPH_NODE_TYPE_CAMERA);
vec3f_copy(graphNode->pos, pos);
vec3f_copy(graphNode->focus, focus);
graphNode->fnNode.func = func;
graphNode->config.mode = mode;
graphNode->roll = 0;
graphNode->rollScreen = 0;
if (func != NULL) {
func(GEO_CONTEXT_CREATE, &graphNode->fnNode.node, pool);
}
}
return graphNode;
}
/**
* Allocates and returns a newly created translation rotation node
*/
struct GraphNodeTranslationRotation *
init_graph_node_translation_rotation(struct AllocOnlyPool *pool,
struct GraphNodeTranslationRotation *graphNode, s32 drawingLayer,
void *displayList, Vec3s translation, Vec3s rotation) {
if (pool != NULL) {
graphNode = alloc_only_pool_alloc(pool, sizeof(struct GraphNodeTranslationRotation));
}
if (graphNode != NULL) {
init_scene_graph_node_links(&graphNode->node, GRAPH_NODE_TYPE_TRANSLATION_ROTATION);
vec3s_copy(graphNode->translation, translation);
vec3s_copy(graphNode->rotation, rotation);
graphNode->node.flags = (drawingLayer << 8) | (graphNode->node.flags & 0xFF);
graphNode->displayList = displayList;
}
return graphNode;
}
/**
* Allocates and returns a newly created translation node
*/
struct GraphNodeTranslation *init_graph_node_translation(struct AllocOnlyPool *pool,
struct GraphNodeTranslation *graphNode,
s32 drawingLayer, void *displayList,
Vec3s translation) {
if (pool != NULL) {
graphNode = alloc_only_pool_alloc(pool, sizeof(struct GraphNodeTranslation));
}
if (graphNode != NULL) {
init_scene_graph_node_links(&graphNode->node, GRAPH_NODE_TYPE_TRANSLATION);
vec3s_copy(graphNode->translation, translation);
graphNode->node.flags = (drawingLayer << 8) | (graphNode->node.flags & 0xFF);
graphNode->displayList = displayList;
}
return graphNode;
}
/**
* Allocates and returns a newly created rotation node
*/
struct GraphNodeRotation *init_graph_node_rotation(struct AllocOnlyPool *pool,
struct GraphNodeRotation *graphNode,
s32 drawingLayer, void *displayList,
Vec3s rotation) {
if (pool != NULL) {
graphNode = alloc_only_pool_alloc(pool, sizeof(struct GraphNodeRotation));
}
if (graphNode != NULL) {
init_scene_graph_node_links(&graphNode->node, GRAPH_NODE_TYPE_ROTATION);
vec3s_copy(graphNode->rotation, rotation);
graphNode->node.flags = (drawingLayer << 8) | (graphNode->node.flags & 0xFF);
graphNode->displayList = displayList;
}
return graphNode;
}
/**
* Allocates and returns a newly created scaling node
*/
struct GraphNodeScale *init_graph_node_scale(struct AllocOnlyPool *pool,
struct GraphNodeScale *graphNode, s32 drawingLayer,
void *displayList, f32 scale) {
if (pool != NULL) {
graphNode = alloc_only_pool_alloc(pool, sizeof(struct GraphNodeScale));
}
if (graphNode != NULL) {
init_scene_graph_node_links(&graphNode->node, GRAPH_NODE_TYPE_SCALE);
graphNode->node.flags = (drawingLayer << 8) | (graphNode->node.flags & 0xFF);
graphNode->scale = scale;
graphNode->displayList = displayList;
}
return graphNode;
}
/**
* Allocates and returns a newly created object node
*/
struct GraphNodeObject *init_graph_node_object(struct AllocOnlyPool *pool,
struct GraphNodeObject *graphNode,
struct GraphNode *sharedChild, Vec3f pos, Vec3s angle,
Vec3f scale) {
if (pool != NULL) {
graphNode = alloc_only_pool_alloc(pool, sizeof(struct GraphNodeObject));
}
if (graphNode != NULL) {
init_scene_graph_node_links(&graphNode->node, GRAPH_NODE_TYPE_OBJECT);
vec3f_copy(graphNode->pos, pos);
vec3f_copy(graphNode->scale, scale);
vec3s_copy(graphNode->angle, angle);
graphNode->sharedChild = sharedChild;
graphNode->throwMatrix = NULL;
graphNode->unk38.animID = 0;
graphNode->unk38.curAnim = NULL;
graphNode->unk38.animFrame = 0;
graphNode->unk38.animFrameAccelAssist = 0;
graphNode->unk38.animAccel = 0x10000;
graphNode->unk38.animTimer = 0;
graphNode->node.flags |= GRAPH_RENDER_HAS_ANIMATION;
}
return graphNode;
}
/**
* Allocates and returns a newly created frustum culling radius node
*/
struct GraphNodeCullingRadius *init_graph_node_culling_radius(struct AllocOnlyPool *pool,
struct GraphNodeCullingRadius *graphNode,
s16 radius) {
if (pool != NULL) {
graphNode = alloc_only_pool_alloc(pool, sizeof(struct GraphNodeCullingRadius));
}
if (graphNode != NULL) {
init_scene_graph_node_links(&graphNode->node, GRAPH_NODE_TYPE_CULLING_RADIUS);
graphNode->cullingRadius = radius;
}
return graphNode;
}
/**
* Allocates and returns a newly created animated part node
*/
struct GraphNodeAnimatedPart *init_graph_node_animated_part(struct AllocOnlyPool *pool,
struct GraphNodeAnimatedPart *graphNode,
s32 drawingLayer, void *displayList,
Vec3s translation) {
if (pool != NULL) {
graphNode = alloc_only_pool_alloc(pool, sizeof(struct GraphNodeAnimatedPart));
}
if (graphNode != NULL) {
init_scene_graph_node_links(&graphNode->node, GRAPH_NODE_TYPE_ANIMATED_PART);
vec3s_copy(graphNode->translation, translation);
graphNode->node.flags = (drawingLayer << 8) | (graphNode->node.flags & 0xFF);
graphNode->displayList = displayList;
}
return graphNode;
}
/**
* Allocates and returns a newly created billboard node
*/
struct GraphNodeBillboard *init_graph_node_billboard(struct AllocOnlyPool *pool,
struct GraphNodeBillboard *graphNode,
s32 drawingLayer, void *displayList,
Vec3s translation) {
if (pool != NULL) {
graphNode = alloc_only_pool_alloc(pool, sizeof(struct GraphNodeBillboard));
}
if (graphNode != NULL) {
init_scene_graph_node_links(&graphNode->node, GRAPH_NODE_TYPE_BILLBOARD);
vec3s_copy(graphNode->translation, translation);
graphNode->node.flags = (drawingLayer << 8) | (graphNode->node.flags & 0xFF);
graphNode->displayList = displayList;
}
return graphNode;
}
/**
* Allocates and returns a newly created displaylist node
*/
struct GraphNodeDisplayList *init_graph_node_display_list(struct AllocOnlyPool *pool,
struct GraphNodeDisplayList *graphNode,
s32 drawingLayer, void *displayList) {
if (pool != NULL) {
graphNode = alloc_only_pool_alloc(pool, sizeof(struct GraphNodeDisplayList));
}
if (graphNode != NULL) {
init_scene_graph_node_links(&graphNode->node, GRAPH_NODE_TYPE_DISPLAY_LIST);
graphNode->node.flags = (drawingLayer << 8) | (graphNode->node.flags & 0xFF);
graphNode->displayList = displayList;
}
return graphNode;
}
/**
* Allocates and returns a newly created shadow node
*/
struct GraphNodeShadow *init_graph_node_shadow(struct AllocOnlyPool *pool,
struct GraphNodeShadow *graphNode, s16 shadowScale,
u8 shadowSolidity, u8 shadowType) {
if (pool != NULL) {
graphNode = alloc_only_pool_alloc(pool, sizeof(struct GraphNodeShadow));
}
if (graphNode != NULL) {
init_scene_graph_node_links(&graphNode->node, GRAPH_NODE_TYPE_SHADOW);
graphNode->shadowScale = shadowScale;
graphNode->shadowSolidity = shadowSolidity;
graphNode->shadowType = shadowType;
}
return graphNode;
}
/**
* Allocates and returns a newly created object parent node
*/
struct GraphNodeObjectParent *init_graph_node_object_parent(struct AllocOnlyPool *pool,
struct GraphNodeObjectParent *graphNode,
struct GraphNode *sharedChild) {
if (pool != NULL) {
graphNode = alloc_only_pool_alloc(pool, sizeof(struct GraphNodeObjectParent));
}
if (graphNode != NULL) {
init_scene_graph_node_links(&graphNode->node, GRAPH_NODE_TYPE_OBJECT_PARENT);
graphNode->sharedChild = sharedChild;
}
return graphNode;
}
/**
* Allocates and returns a newly created generated node
*/
struct GraphNodeGenerated *init_graph_node_generated(struct AllocOnlyPool *pool,
struct GraphNodeGenerated *graphNode,
GraphNodeFunc gfxFunc, s32 parameter) {
if (pool != NULL) {
graphNode = alloc_only_pool_alloc(pool, sizeof(struct GraphNodeGenerated));
}
if (graphNode != NULL) {
init_scene_graph_node_links(&graphNode->fnNode.node, GRAPH_NODE_TYPE_GENERATED_LIST);
graphNode->fnNode.func = gfxFunc;
graphNode->parameter = parameter;
if (gfxFunc != NULL) {
gfxFunc(GEO_CONTEXT_CREATE, &graphNode->fnNode.node, pool);
}
}
return graphNode;
}
/**
* Allocates and returns a newly created background node
*/
struct GraphNodeBackground *init_graph_node_background(struct AllocOnlyPool *pool,
struct GraphNodeBackground *graphNode,
u16 background, GraphNodeFunc backgroundFunc,
s32 zero) {
if (pool != NULL) {
graphNode = alloc_only_pool_alloc(pool, sizeof(struct GraphNodeBackground));
}
if (graphNode != NULL) {
init_scene_graph_node_links(&graphNode->fnNode.node, GRAPH_NODE_TYPE_BACKGROUND);
graphNode->background = (background << 16) | background;
graphNode->fnNode.func = backgroundFunc;
graphNode->unused = zero; // always 0, unused
if (backgroundFunc != NULL) {
backgroundFunc(GEO_CONTEXT_CREATE, &graphNode->fnNode.node, pool);
}
}
return graphNode;
}
/**
* Allocates and returns a newly created held object node
*/
struct GraphNodeHeldObject *init_graph_node_held_object(struct AllocOnlyPool *pool,
struct GraphNodeHeldObject *graphNode,
struct Object *objNode,
Vec3s translation,
GraphNodeFunc nodeFunc, s32 playerIndex) {
if (pool != NULL) {
graphNode = alloc_only_pool_alloc(pool, sizeof(struct GraphNodeHeldObject));
}
if (graphNode != NULL) {
init_scene_graph_node_links(&graphNode->fnNode.node, GRAPH_NODE_TYPE_HELD_OBJ);
vec3s_copy(graphNode->translation, translation);
graphNode->objNode = objNode;
graphNode->fnNode.func = nodeFunc;
graphNode->playerIndex = playerIndex;
if (nodeFunc != NULL) {
nodeFunc(GEO_CONTEXT_CREATE, &graphNode->fnNode.node, pool);
}
}
return graphNode;
}
/**
* Adds 'childNode' to the end of the list children from 'parent'
*/
struct GraphNode *geo_add_child(struct GraphNode *parent, struct GraphNode *childNode) {
struct GraphNode *parentFirstChild;
struct GraphNode *parentLastChild;
if (childNode != NULL) {
childNode->parent = parent;
parentFirstChild = parent->children;
if (parentFirstChild == NULL) {
parent->children = childNode;
childNode->prev = childNode;
childNode->next = childNode;
} else {
parentLastChild = parentFirstChild->prev;
childNode->prev = parentLastChild;
childNode->next = parentFirstChild;
parentFirstChild->prev = childNode;
parentLastChild->next = childNode;
}
}
return childNode;
}
/**
* Remove a node from the scene graph. It changes the links with its
* siblings and with its parent, it doesn't deallocate the memory
* since geo nodes are allocated in a pointer-bumping pool that
* gets thrown out when changing areas.
*/
struct GraphNode *geo_remove_child(struct GraphNode *graphNode) {
struct GraphNode *parent;
struct GraphNode **firstChild;
parent = graphNode->parent;
firstChild = &parent->children;
// Remove link with siblings
graphNode->prev->next = graphNode->next;
graphNode->next->prev = graphNode->prev;
// If this node was the first child, a new first child must be chosen
if (*firstChild == graphNode) {
// The list is circular, so this checks whether it was the only child
if (graphNode->next == graphNode) {
*firstChild = NULL; // Parent has no children anymore
} else {
*firstChild = graphNode->next; // Choose a new first child
}
}
return parent;
}
/**
* Reorders the given node so it's the first child of its parent.
* This is called on the Mario object when he is spawned. That's why Mario's
* object is always drawn before any other objects. (Note that the geo order
* is independent from processing group order, where Mario is not first.)
*/
struct GraphNode *geo_make_first_child(struct GraphNode *newFirstChild) {
struct GraphNode *lastSibling;
struct GraphNode *parent;
struct GraphNode **firstChild;
parent = newFirstChild->parent;
firstChild = &parent->children;
if (*firstChild != newFirstChild) {
if ((*firstChild)->prev != newFirstChild) {
newFirstChild->prev->next = newFirstChild->next;
newFirstChild->next->prev = newFirstChild->prev;
lastSibling = (*firstChild)->prev;
newFirstChild->prev = lastSibling;
newFirstChild->next = *firstChild;
(*firstChild)->prev = newFirstChild;
lastSibling->next = newFirstChild;
}
*firstChild = newFirstChild;
}
return parent;
}
/**
* Helper function for geo_call_global_function_nodes that recursively
* traverses the scene graph and calls the functions of global nodes.
*/
void geo_call_global_function_nodes_helper(struct GraphNode *graphNode, s32 callContext) {
struct GraphNode **globalPtr;
struct GraphNode *curNode;
struct FnGraphNode *asFnNode;
curNode = graphNode;
do {
asFnNode = (struct FnGraphNode *) curNode;
if (curNode->type & GRAPH_NODE_TYPE_FUNCTIONAL) {
if (asFnNode->func != NULL) {
asFnNode->func(callContext, curNode, NULL);
}
}
if (curNode->children != NULL) {
switch (curNode->type) {
case GRAPH_NODE_TYPE_MASTER_LIST:
globalPtr = (struct GraphNode **) &gCurGraphNodeMasterList;
break;
case GRAPH_NODE_TYPE_PERSPECTIVE:
globalPtr = (struct GraphNode **) &gCurGraphNodeCamFrustum;
break;
case GRAPH_NODE_TYPE_CAMERA:
globalPtr = (struct GraphNode **) &gCurGraphNodeCamera;
break;
case GRAPH_NODE_TYPE_OBJECT:
globalPtr = (struct GraphNode **) &gCurGraphNodeObject;
break;
default:
globalPtr = NULL;
break;
}
if (globalPtr != NULL) {
*globalPtr = curNode;
}
geo_call_global_function_nodes_helper(curNode->children, callContext);
if (globalPtr != NULL) {
*globalPtr = NULL;
}
}
} while ((curNode = curNode->next) != graphNode);
}
/**
* Call the update functions of geo nodes that are stored in global variables.
* These variables include gCurGraphNodeMasterList, gCurGraphNodeCamFrustum,
* gCurGraphNodeCamera and gCurGraphNodeObject.
* callContext is one of the GEO_CONTEXT_ defines.
* The graphNode argument should be of type GraphNodeRoot.
*/
void geo_call_global_function_nodes(struct GraphNode *graphNode, s32 callContext) {
if (graphNode->flags & GRAPH_RENDER_ACTIVE) {
gCurGraphNodeRoot = (struct GraphNodeRoot *) graphNode;
if (graphNode->children != NULL) {
geo_call_global_function_nodes_helper(graphNode->children, callContext);
}
gCurGraphNodeRoot = 0;
}
}
/**
* When objects are cleared, this is called on all object nodes (loaded or unloaded).
*/
void geo_reset_object_node(struct GraphNodeObject *graphNode) {
init_graph_node_object(NULL, graphNode, 0, gVec3fZero, gVec3sZero, gVec3fOne);
geo_add_child(&gObjParentGraphNode, &graphNode->node);
graphNode->node.flags &= ~GRAPH_RENDER_ACTIVE;
}
/**
* Initialize an object node using the given parameters
*/
void geo_obj_init(struct GraphNodeObject *graphNode, void *sharedChild, Vec3f pos, Vec3s angle) {
vec3f_set(graphNode->scale, 1.0f, 1.0f, 1.0f);
vec3f_copy(graphNode->pos, pos);
vec3s_copy(graphNode->angle, angle);
graphNode->sharedChild = sharedChild;
graphNode->unk4C = 0;
graphNode->throwMatrix = NULL;
graphNode->unk38.curAnim = NULL;
graphNode->node.flags |= GRAPH_RENDER_ACTIVE;
graphNode->node.flags &= ~GRAPH_RENDER_INVISIBLE;
graphNode->node.flags |= GRAPH_RENDER_HAS_ANIMATION;
graphNode->node.flags &= ~GRAPH_RENDER_BILLBOARD;
}
/**
* Initialize and object node using the given SpawnInfo struct
*/
void geo_obj_init_spawninfo(struct GraphNodeObject *graphNode, struct SpawnInfo *spawn) {
vec3f_set(graphNode->scale, 1.0f, 1.0f, 1.0f);
vec3s_copy(graphNode->angle, spawn->startAngle);
graphNode->pos[0] = (f32) spawn->startPos[0];
graphNode->pos[1] = (f32) spawn->startPos[1];
graphNode->pos[2] = (f32) spawn->startPos[2];
graphNode->unk18 = spawn->areaIndex;
graphNode->unk19 = spawn->activeAreaIndex;
graphNode->sharedChild = spawn->unk18;
graphNode->unk4C = spawn;
graphNode->throwMatrix = NULL;
graphNode->unk38.curAnim = 0;
graphNode->node.flags |= GRAPH_RENDER_ACTIVE;
graphNode->node.flags &= ~GRAPH_RENDER_INVISIBLE;
graphNode->node.flags |= GRAPH_RENDER_HAS_ANIMATION;
graphNode->node.flags &= ~GRAPH_RENDER_BILLBOARD;
}
/**
* Initialize the animation of an object node
*/
void geo_obj_init_animation(struct GraphNodeObject *graphNode, struct Animation **animPtrAddr) {
struct Animation **animSegmented = segmented_to_virtual(animPtrAddr);
struct Animation *anim = segmented_to_virtual(*animSegmented);
if (graphNode->unk38.curAnim != anim) {
graphNode->unk38.curAnim = anim;
graphNode->unk38.animFrame = anim->unk04 + ((anim->flags & ANIM_FLAG_FORWARD) ? 1 : -1);
graphNode->unk38.animAccel = 0;
graphNode->unk38.animYTrans = 0;
}
}
/**
* Initialize the animation of an object node
*/
void geo_obj_init_animation_accel(struct GraphNodeObject *graphNode, struct Animation **animPtrAddr, u32 animAccel) {
struct Animation **animSegmented = segmented_to_virtual(animPtrAddr);
struct Animation *anim = segmented_to_virtual(*animSegmented);
if (graphNode->unk38.curAnim != anim) {
graphNode->unk38.curAnim = anim;
graphNode->unk38.animYTrans = 0;
graphNode->unk38.animFrameAccelAssist =
(anim->unk04 << 16) + ((anim->flags & ANIM_FLAG_FORWARD) ? animAccel : -animAccel);
graphNode->unk38.animFrame = graphNode->unk38.animFrameAccelAssist >> 16;
}
graphNode->unk38.animAccel = animAccel;
}
/**
* Retrieves an index into animation data based on the attribute pointer
* An attribute is an x-, y- or z-component of the translation / rotation for a part
* Each attribute is a pair of s16's, where the first s16 represents the maximum frame
* and the second s16 the actual index. This index can be used to index in the array
* with actual animation values.
*/
s32 retrieve_animation_index(s32 frame, u16 **attributes) {
s32 result;
if (frame < (*attributes)[0]) {
result = (*attributes)[1] + frame;
} else {
result = (*attributes)[1] + (*attributes)[0] - 1;
}
*attributes += 2;
return result;
}
/**
* Update the animation frame of an object. The animation flags determine
* whether it plays forwards or backwards, and whether it stops or loops at
* the end etc.
*/
s16 geo_update_animation_frame(struct GraphNodeObject_sub *obj, s32 *accelAssist) {
s32 result;
struct Animation *anim;
anim = obj->curAnim;
if (obj->animTimer == gAreaUpdateCounter || anim->flags & ANIM_FLAG_2) {
if (accelAssist != NULL) {
accelAssist[0] = obj->animFrameAccelAssist;
}
return obj->animFrame;
}
if (anim->flags & ANIM_FLAG_FORWARD) {
if (obj->animAccel) {
result = obj->animFrameAccelAssist - obj->animAccel;
} else {
result = (obj->animFrame - 1) << 16;
}
if (GET_HIGH_S16_OF_32(result) < anim->unk06) {
if (anim->flags & ANIM_FLAG_NOLOOP) {
SET_HIGH_S16_OF_32(result, anim->unk06);
} else {
SET_HIGH_S16_OF_32(result, anim->unk08 - 1);
}
}
} else {
if (obj->animAccel != 0) {
result = obj->animFrameAccelAssist + obj->animAccel;
} else {
result = (obj->animFrame + 1) << 16;
}
if (GET_HIGH_S16_OF_32(result) >= anim->unk08) {
if (anim->flags & ANIM_FLAG_NOLOOP) {
SET_HIGH_S16_OF_32(result, anim->unk08 - 1);
} else {
SET_HIGH_S16_OF_32(result, anim->unk06);
}
}
}
if (accelAssist != 0) {
accelAssist[0] = result;
}
return GET_HIGH_S16_OF_32(result);
}
/**
* Unused function to retrieve an object's current animation translation
* Assumes that it has x, y and z data in animations, which isn't always the
* case since some animation types only have vertical or lateral translation.
* This might have been used for positioning the shadow under an object, which
* currently happens in-line in geo_process_shadow where it also accounts for
* animations without lateral translation.
*/
void geo_retreive_animation_translation(struct GraphNodeObject *obj, Vec3f position) {
struct Animation *animation = obj->unk38.curAnim;
u16 *attribute;
s16 *values;
s16 frame;
if (animation != NULL) {
attribute = segmented_to_virtual((void *) animation->index);
values = segmented_to_virtual((void *) animation->values);
frame = obj->unk38.animFrame;
if (frame < 0) {
frame = 0;
}
if (1) // ? necessary to match
{
position[0] = (f32) values[retrieve_animation_index(frame, &attribute)];
position[1] = (f32) values[retrieve_animation_index(frame, &attribute)];
position[2] = (f32) values[retrieve_animation_index(frame, &attribute)];
}
} else {
vec3f_set(position, 0, 0, 0);
}
}
/**
* Unused function to find the root of the geo node tree, which should be a
* GraphNodeRoot. If it is not for some reason, null is returned.
*/
struct GraphNodeRoot *geo_find_root(struct GraphNode *graphNode) {
struct GraphNodeRoot *resGraphNode = NULL;
while (graphNode->parent != NULL) {
graphNode = graphNode->parent;
}
if (graphNode->type == GRAPH_NODE_TYPE_ROOT) {
resGraphNode = (struct GraphNodeRoot *) graphNode;
}
return resGraphNode;
}