drawskel.c
9.86 KB
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
/********************************************************************************
NINTENDO64 Disk Drive IPL4
mario drawing module
February 27, 1997
********************************************************************************/
#include <ultra64.h>
#include "ipl4.h"
#include "graphics.h"
#include "static.h"
#define ANIM_STAT_INACTIVE 0
#define ANIM_STAT_TRANS 1
#define ANIM_STAT_ANGLE 2
static AnimePlay animplay;
//////////////////////////////////////////////////////////////////////////////////
//
//
//
//
// Animation control.
//
//
//
//
//////////////////////////////////////////////////////////////////////////////////
/********************************************************************************/
/* */
/* Set skeleton animation. */
/* */
/********************************************************************************/
extern int
SetAnimation(CtrlShape *shape, short id, AnimeData *anime, long speed)
{
AnimeCtrl *animctrl = &shape->animation;
if ((animctrl->anime != anime) || (animctrl->code != id)) {
animctrl->code = id;
animctrl->anime = anime;
animctrl->frame = (anime->start<<16) - speed;
}
animctrl->speed = speed;
return(animctrl->frame >> 16);
}
/********************************************************************************/
/* */
/* Check skeleton animation frame. */
/* */
/********************************************************************************/
static int
CheckAnimeFrame(CtrlShape *shape, int frame)
{
int result;
AnimeCtrl *animctrl = &shape->animation;
long nextframe = animctrl->frame + animctrl->speed;
long testframe = frame << 16;
result = ((animctrl->frame < testframe) && (testframe <= nextframe));
return(result);
}
/********************************************************************************/
/* */
/* get skeleton animation index. */
/* */
/********************************************************************************/
static int
AnimeIndex(int frame, short **table)
{
int index;
if ((*table)[0] > frame) index = (*table)[1] + frame;
else index = (*table)[1] + (*table)[0] - 1;
*table += 2;
return(index);
}
/********************************************************************************/
/* */
/* Calculate next animation frame. */
/* */
/********************************************************************************/
static long
NextAnimeFrame(AnimeCtrl *animctrl)
{
SPacked frame;
AnimeData *anime = animctrl->anime;
frame.word = animctrl->frame + animctrl->speed;
if (frame.half[0] >= anime->nframes) {
if (anime->attr & HMS_ANIM_ONETIME) frame.half[0] = anime->nframes - 1;
else frame.half[0] = anime->loop;
}
return(frame.word);
}
/********************************************************************************/
/* */
/* Begin animation. */
/* */
/********************************************************************************/
static void
BeginAnimation(AnimeCtrl *animctrl, int animesw)
{
if (animesw) animctrl->frame = NextAnimeFrame(animctrl);
animplay.status = ANIM_STAT_TRANS;
animplay.frame = animctrl->frame >> 16;
animplay.scale = 1.0f;
animplay.table = animctrl->anime->table;
animplay.param = animctrl->anime->param;
}
//////////////////////////////////////////////////////////////////////////////////
//
//
//
//
// Skeleton control.
//
//
//
//
//////////////////////////////////////////////////////////////////////////////////
/********************************************************************************/
/* */
/* Select head. */
/* */
/********************************************************************************/
static void
DoSelectHead(Gfx *gfxlist1, Gfx *gfxlist2, Gfx *gfxlist3, int offset)
{
static char eyeBlink[7] = { 1,2,1,0,1,2,1 };
int select = 0;
int count = ((frameCounter+offset) >> 1) & 0x1f;
if (count < 7) select = eyeBlink[count];
switch (select) {
case 0: gSPDisplayList(graphicp++, gfxlist1); break;
case 1: gSPDisplayList(graphicp++, gfxlist2); break;
case 2: gSPDisplayList(graphicp++, gfxlist3); break;
}
}
/********************************************************************************/
/* */
/* Select head. */
/* */
/********************************************************************************/
static void
DoSelectHand(short flags, Gfx *gfxlist1, Gfx *gfxlist2)
{
if (flags & SHAPE_FLAG_OPENHAND) {
gSPDisplayList(graphicp++, gfxlist2);
} else {
gSPDisplayList(graphicp++, gfxlist1);
}
}
/********************************************************************************/
/* */
/* Do Joint. */
/* */
/********************************************************************************/
static void
DoJoint(Gfx *gfxlist, short offsetx, short offsety, short offsetz)
{
FVector angle;
FVector trans;
Mtx * matrix = (Mtx *)AllocDynamic(sizeof(Mtx));
SetFVector(&angle, 0.0f , 0.0f , 0.0f );
SetFVector(&trans, (float)offsetx, (float)offsety, (float)offsetz);
if (animplay.status == ANIM_STAT_TRANS) {
trans.x += (float)animplay.param[ AnimeIndex(animplay.frame, &animplay.table)] * animplay.scale;
trans.y += (float)animplay.param[ AnimeIndex(animplay.frame, &animplay.table)] * animplay.scale;
trans.z += (float)animplay.param[ AnimeIndex(animplay.frame, &animplay.table)] * animplay.scale;
animplay.status = ANIM_STAT_ANGLE;
}
angle.x = AngleToRadian( animplay.param[ AnimeIndex(animplay.frame, &animplay.table)]);
angle.y = AngleToRadian( animplay.param[ AnimeIndex(animplay.frame, &animplay.table)]);
angle.z = AngleToRadian( animplay.param[ AnimeIndex(animplay.frame, &animplay.table)]);
CreateJointMatrix(matrix, &trans, &angle);
gSPMatrix(graphicp++, K0_TO_PHYS(matrix), G_MTX_MODELVIEW|G_MTX_MUL|G_MTX_PUSH)
if (gfxlist != NULL) gSPDisplayList(graphicp++, gfxlist);
}
/********************************************************************************/
/* */
/* Pop matrix. */
/* */
/********************************************************************************/
static void
PopMatrix(void)
{
gSPPopMatrix(graphicp++, G_MTX_MODELVIEW);
}
/********************************************************************************/
/* */
/* Draw skeleton shape. */
/* */
/********************************************************************************/
extern void
DrawSkeleton(CtrlShape *shape, Skeleton skeleton, int drawmode)
{
if (shape->flags & SHAPE_FLAG_ENABLE) {
FVector radian;
FVector scaling;
FVector position = shape->position;
Mtx * modeling = (Mtx *)AllocDynamic(sizeof(Mtx));
SetFVector(&radian , 0.0f, AngleToRadian(shape->angle), 0.0f);
SetFVector(&scaling, 1.0f, shape->scale , 1.0f);
position.y += 10.0f; /* for 3D calculate error correction */
if (drawmode == SHAPE_DRAW_MIRROR) {
position.y *= -1.0f;
scaling.y *= -1.0f;
gSPClearGeometryMode(graphicp++, G_CULL_BACK );
gSPSetGeometryMode (graphicp++, G_CULL_FRONT);
}
CreateModelMatrix(modeling, &position, &radian, &scaling);
gSPMatrix(graphicp++, K0_TO_PHYS(modeling), G_MTX_MODELVIEW |G_MTX_LOAD|G_MTX_NOPUSH);
BeginAnimation(&shape->animation, (drawmode == SHAPE_DRAW_NORMAL));
skeleton(shape->flags);
if (drawmode == SHAPE_DRAW_MIRROR) {
gSPClearGeometryMode(graphicp++, G_CULL_FRONT);
gSPSetGeometryMode (graphicp++, G_CULL_BACK );
}
}
}
//////////////////////////////////////////////////////////////////////////////////
//
//
//
//
// Skeleton shape data.
//
//
//
//
//////////////////////////////////////////////////////////////////////////////////
/********************************************************************************/
/* */
/* Mario skeleton data. */
/* */
/********************************************************************************/
extern void
MarioSkeleton(short flags)
{
DoJoint(NULL, 0, 0, 0); /* chn14 */
DoJoint(RCP_mario_near14, 0, 0, 0); /* m_waist1_3 */
DoJoint(RCP_mario_near_body, 68, 0, 0); /* m_body1 */
DoJoint(NULL, 87, 0, 0); /* m_head2 */
DoSelectHead(RCP_mario_head1, RCP_mario_head2, RCP_mario_head3, 0);
PopMatrix();
DoJoint(NULL, 67, -10, 79); /* chn6 */
DoJoint(RCP_mario_near2, 0, 0, 0); /* m_larmA1 */
DoJoint(RCP_mario_near1, 65, 0, 0); /* m_larmB1 */
DoJoint(NULL, 60, 0, 0); /* m_lhand1 */
DoSelectHand(flags, RCP_mario_near0, RCP_mario_swim_l);
PopMatrix();
PopMatrix();
PopMatrix();
PopMatrix();
DoJoint(NULL, 68, -10, -79); /* chn10 */
DoJoint(RCP_mario_near5, 0, 0, 0); /* m_rarmA1 */
DoJoint(RCP_mario_near4, 65, 0, 0); /* m_rarmB1 */
DoJoint(NULL, 60, 0, 0); /* m_rhand1 */
DoSelectHand(flags, RCP_mario_near3, RCP_mario_swim_r);
PopMatrix();
PopMatrix();
PopMatrix();
PopMatrix();
PopMatrix();
DoJoint(NULL, 13, -8, 42); /* chn15 */
DoJoint(RCP_mario_near11, 0, 0, 0); /* m_llegA1 */
DoJoint(RCP_mario_near10, 89, 0, 0); /* m_llegB1 */
DoJoint(RCP_mario_near9, 67, 0, 0); /* m_lfoot1 */
PopMatrix();
PopMatrix();
PopMatrix();
PopMatrix();
DoJoint(NULL, 13, -8, -42); /* chn17 */
DoJoint(RCP_mario_near8, 0, 0, 0); /* m_rlegA1 */
DoJoint(RCP_mario_near7, 89, 0, 0); /* m_rlegB1 */
DoJoint(RCP_mario_near6, 67, 0, 0);
PopMatrix();
PopMatrix();
PopMatrix();
PopMatrix();
PopMatrix();
PopMatrix();
}