-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcube.c
130 lines (97 loc) · 3.37 KB
/
cube.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
// screw you code formatter, this one needs to stay on top
#include <stdlib.h>
// now we need to have libgte.h
#include <libgte.h>
// then, these can work; the psyq library is badly written and won't include what it uses
#include <libetc.h>
#include <libgpu.h>
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(*(a)))
#define OTSIZE 4096
#define SCREEN_Z 512
#define CUBESIZE 196
typedef struct DB {
DRAWENV draw;
DISPENV disp;
u_long ot[OTSIZE];
POLY_F4 s[6];
} DB;
static SVECTOR cube_vertices[] = {
{-CUBESIZE / 2, -CUBESIZE / 2, -CUBESIZE / 2, 0}, {CUBESIZE / 2, -CUBESIZE / 2, -CUBESIZE / 2, 0},
{CUBESIZE / 2, CUBESIZE / 2, -CUBESIZE / 2, 0}, {-CUBESIZE / 2, CUBESIZE / 2, -CUBESIZE / 2, 0},
{-CUBESIZE / 2, -CUBESIZE / 2, CUBESIZE / 2, 0}, {CUBESIZE / 2, -CUBESIZE / 2, CUBESIZE / 2, 0},
{CUBESIZE / 2, CUBESIZE / 2, CUBESIZE / 2, 0}, {-CUBESIZE / 2, CUBESIZE / 2, CUBESIZE / 2, 0},
};
static int cube_indices[] = {
0, 1, 2, 3, 1, 5, 6, 2, 5, 4, 7, 6, 4, 0, 3, 7, 4, 5, 1, 0, 6, 7, 3, 2,
};
static void init_cube(DB *db, CVECTOR *col) {
size_t i;
for (i = 0; i < ARRAY_SIZE(db->s); ++i) {
SetPolyF4(&db->s[i]);
setRGB0(&db->s[i], col[i].r, col[i].g, col[i].b);
}
}
static void add_cube(u_long *ot, POLY_F4 *s, MATRIX *transform) {
long p, otz, flg;
int nclip;
size_t i;
SetRotMatrix(transform);
SetTransMatrix(transform);
for (i = 0; i < ARRAY_SIZE(cube_indices); i += 4, ++s) {
nclip = RotAverageNclip4(&cube_vertices[cube_indices[i + 0]], &cube_vertices[cube_indices[i + 1]],
&cube_vertices[cube_indices[i + 2]], &cube_vertices[cube_indices[i + 3]],
(long *)&s->x0, (long *)&s->x1, (long *)&s->x3, (long *)&s->x2, &p, &otz, &flg);
if (nclip <= 0) continue;
if ((otz > 0) && (otz < OTSIZE)) AddPrim(&ot[otz], s);
}
}
int main(void) {
DB db[2];
DB *cdb;
SVECTOR rotation = {0};
VECTOR translation = {0, 0, (SCREEN_Z * 3) / 2, 0};
MATRIX transform;
CVECTOR col[6];
size_t i;
ResetGraph(0);
InitGeom();
SetGraphDebug(0);
FntLoad(960, 256);
SetDumpFnt(FntOpen(32, 32, 320, 64, 0, 512));
SetGeomOffset(320, 240);
SetGeomScreen(SCREEN_Z);
SetDefDrawEnv(&db[0].draw, 0, 0, 640, 480);
SetDefDrawEnv(&db[1].draw, 0, 0, 640, 480);
SetDefDispEnv(&db[0].disp, 0, 0, 640, 480);
SetDefDispEnv(&db[1].disp, 0, 0, 640, 480);
srand(0);
for (i = 0; i < ARRAY_SIZE(col); ++i) {
col[i].r = rand();
col[i].g = rand();
col[i].b = rand();
}
init_cube(&db[0], col);
init_cube(&db[1], col);
SetDispMask(1);
PutDrawEnv(&db[0].draw);
PutDispEnv(&db[0].disp);
while (1) {
cdb = (cdb == &db[0]) ? &db[1] : &db[0];
rotation.vy += 16;
rotation.vz += 16;
RotMatrix(&rotation, &transform);
TransMatrix(&transform, &translation);
ClearOTagR(cdb->ot, OTSIZE);
FntPrint("Code compiled using psyq libraries\n\n");
FntPrint("converted by psyq-obj-parser\n\n");
FntPrint("PCSX-Redux project\n\n");
FntPrint("https://bit.ly/pcsx-redux");
add_cube(cdb->ot, cdb->s, &transform);
DrawSync(0);
VSync(0);
ClearImage(&cdb->draw.clip, 60, 120, 120);
DrawOTag(&cdb->ot[OTSIZE - 1]);
FntFlush(-1);
}
return 0;
}