cube.c
3.92 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
/*---------------------------------------------------------------------*
Copyright (C) 1997, Nintendo.
File cube.c
Coded by Yoshitaka Yasumoto. Sep 12, 1997.
Modified by
$Id: cube.c,v 1.1.1.1 2002/05/02 03:27:17 blythe Exp $
*---------------------------------------------------------------------*/
#include <ultra64.h>
#include "gzpoly.h"
#include "system.h"
#include "action.h"
#include "cube.h"
s16 rot = 0;
s16 zpos = 800;
Dynamic dynamic;
u16 perspNorm;
u64 outbuffer[SIZEOF_CUBEDEST*CUBE_NUM/sizeof(u64)+256];
/* Max size of cube object is 3 clipped face(each face consist of 3 tri) and
3 Quadrangle. (=64*3*3+80*3) */
u64 zobjbuf[(64*3*3+80*3)*CUBE_NUM/sizeof(u64)];
void cubeInit(void)
{
s32 i, n;
n = 0;
for (i = CUBE_TYPE; i < CUBE_NUM; i ++){
cube_poly[i] = cube_poly[n];
n ++;
if (n == CUBE_TYPE) n = 0;
}
#ifdef FOG
for (i = 0; i < CUBE_NUM; i ++){
cube_poly[i].renderMode |= ZRM_FOG;
}
#endif
}
#define Move(mtx,x,y,z) { (mtx).m[1][2] = ((x)<<16)|((u32)(y)&0xffff); \
(mtx).m[1][3] = ((z)<<16)| 1 ; \
(mtx).m[3][2] = (mtx).m[3][3] = 0; }
void cubePass1(Gfx **gp)
{
u32 i, m, n, outp;
s32 z;
Mtx rotate[4];
float deg;
static s32 dx[] =
{0,-200,-100, 0, 100, 200, 200, 200, 100, 0, -100,-200,-200,-100, 100 };
static s32 dy[] =
{0,-120,-120,-120,-120, -120, 0, 120, 120, 120, 120, 120, 0, 0, 0 };
static s32 flag = 0;
/*------ コントローラ操作 ------*/
if (Ac.pad[0].push & START_BUTTON) flag ^= 1;
if (flag) rot ++;
if (Ac.pad[0].button & L_TRIG) rot ++;
if (Ac.pad[0].button & R_TRIG) rot --;
rot %= 360;
if (Ac.pad[0].button & U_JPAD) zpos -= 2;
if (Ac.pad[0].button & D_JPAD) zpos += 2;
/*------ 透視行列の設定 ------*/
guPerspective(&dynamic.projection, &perspNorm, 33,
(float)SCREEN_WD/(float)SCREEN_HT,
(float)CLIP_NEAR, (float)CLIP_FAR, 1.0);
#if 0
guLookAt(&dynamic.viewing, 0, 0, zpos, 0, 0, zpos-10, 0, 1, 0);
#else
guLookAtReflect(&dynamic.viewing, &dynamic.lookat,
0, 0, zpos, 0, 0, zpos-10, 0, 1, 0);
guZFixLookAt(&dynamic.lookat);
#endif
/*------ モデル行列の設定 ------*/
deg = (float)rot;
for (i = 0; i < 4; i ++){
guRotate(rotate+i, deg, 0.2, 1.0, 0.7);
deg += 22.5f;
}
m = n = 0;
z = 50;
for (i = 0; i < CUBE_NUM; i ++){
dynamic.modeling[i] = rotate[m];
Move(dynamic.modeling[i], dx[n], dy[n], z);
z -= 20;
if (m ++ == 3) m = 0;
if (n ++ == (sizeof(dx)/sizeof(dx[0])-1)) n = 0;
}
/*--- WriteBack Dcache ---*/
osWritebackDCache(&dynamic, sizeof(Dynamic));
/*--- Set ViewPort Projection Viewing ---*/
gSPZViewport((*gp) ++, &viewport);
gSPZPerspNormalize((*gp) ++, perspNorm);
gSPZSetMtx((*gp) ++, GZM_MMTX, &dynamic.projection);
gSPZSetMtx((*gp) ++, GZM_PMTX, &dynamic.viewing);
gSPZMtxCat((*gp) ++, GZM_PMTX, GZM_MMTX, GZM_PMTX);
/*--- Load light parameter ---*/
gSPZSetAmbient((*gp)++, UMEM_LIGHT, &scene_light.a);
gSPZSetDefuse ((*gp)++, UMEM_LIGHT, 0, &scene_light.l[0]);
/*--- Load reflection parameter ---*/
gSPZSetLookAt ((*gp)++, UMEM_LIGHT, 1, &dynamic.lookat);
/*--- Generate Display List for vertices of each cube ---*/
outp = (u32)outbuffer;
for (i = 0; i < CUBE_NUM; i ++){
/*--- Set Modeling Matrix ---*/
zSetMMtx(gp, &dynamic.modeling[i], 1, cube_poly[i].renderMode);
/*--- zPoly calculation ---*/
zCalcPoly(gp, cube_poly+i, (u8 **)&outp);
}
/*--- Invalid cache ---*/
osInvalDCache(outbuffer, outp - (u32)outbuffer);
}
void cubePass2(Gfx **gp)
{
zHeader *zobj = (zHeader *)zobjbuf;
u32 i;
/*--- Initialize Drawing Controller ---*/
/*--- + Set viewport for Clipping ---*/
/*--- + Setup bucket array for ZSort ---*/
zDrawSetup(gp, &viewport, CLIP_NEAR, CLIP_FAR);
/*--- ZObject の登録 ----*/
for (i = 0; i < CUBE_NUM; i ++){
zobj = zDrawPoly(zobj, cube_poly+i);
}
/* Z ソート用バケットのクローズ */
zArray_close();
}
/*======== End of cube.c ========*/