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
|
/*
* GTE Graphics Example (Spinning Cube)
* 2019 Meido-Tek Productions
* 2019 modified by nextvolume for PSXSDK
*
* licensed under Mozilla Public License v2.0
*/
#include <stdio.h>
#include <string.h>
#include <psx.h>
#include <psxgpu.h>
#include <meidogte.h>
/* Screen resolution */
#define SCREEN_XRES 320
#define SCREEN_YRES 240
/* Screen center position */
#define CENTERX SCREEN_XRES>>1
#define CENTERY SCREEN_YRES>>1
#define OT_LEN 0x8000
/* For easier handling of vertex indices */
typedef struct {
short v0,v1,v2,v3;
} INDEX;
/* Cube vertices */
SVECTOR cube_verts[] = {
{ -100, -100, -100, 0 },
{ 100, -100, -100, 0 },
{ -100, 100, -100, 0 },
{ 100, 100, -100, 0 },
{ 100, -100, 100, 0 },
{ -100, -100, 100, 0 },
{ 100, 100, 100, 0 },
{ -100, 100, 100, 0 }
};
/* Cube face normals */
SVECTOR cube_norms[] = {
{ 0, 0, -ONE, 0 },
{ 0, 0, ONE, 0 },
{ 0, -ONE, 0, 0 },
{ 0, ONE, 0, 0 },
{ -ONE, 0, 0, 0 },
{ ONE, 0, 0, 0 }
};
/* Cube vertex indices */
INDEX cube_indices[] = {
{ 0, 1, 2, 3 },
{ 4, 5, 6, 7 },
{ 5, 4, 0, 1 },
{ 6, 7, 3, 2 },
{ 0, 2, 5, 7 },
{ 3, 1, 6, 4 }
};
/* Number of faces of cube */
#define CUBE_FACES 6
/* Light color matrix */
/* Each column represents the color matrix of each light source and is */
/* used as material color when using gte_ncs() or multiplied by a */
/* source color when using gte_nccs(). 4096 is 1.0 in this matrix */
/* A column of zeroes disables the light source. */
MATRIX color_mtx = { .m = {
{ONE, 0, 0}, /* Red */
{ONE, 0, 0}, /* Green */
{ONE, 0, 0} /* Blue */ }
};
/* Light matrix */
/* Each row represents a vector direction of each light source. */
/* An entire row of zeroes disables the light source. */
MATRIX light_mtx = { .m = {
/* X, Y, Z */
{ -2048 , -2048 , -2048},
{0 , 0 , 0},
{0 , 0 , 0} }
};
/* Function declarations */
void init(void);
void display(void);
unsigned int prim_list[OT_LEN];
int current_buf = 0;
volatile int vblank_happened = 0;
GsPoly4 poly[CUBE_FACES]; /* Flat shaded quad primitive */
static void program_vblank_handler(void) {
vblank_happened = 1;
}
enum {
OP_SPIN,
OP_COLORR1, OP_COLORG1, OP_COLORB1,
OP_COLORR2, OP_COLORG2, OP_COLORB2,
OP_COLORR3, OP_COLORG3, OP_COLORB3,
OP_LIGHTX1, OP_LIGHTY1, OP_LIGHTZ1,
OP_LIGHTX2, OP_LIGHTY2, OP_LIGHTZ2,
OP_LIGHTX3, OP_LIGHTY3, OP_LIGHTZ3,
OP_SCRXOFF, OP_SCRYOFF,
OP_SCRDEPTH,
OP_TRANSVX, OP_TRANSVY, OP_TRANSVZ,
OP_END
};
const char *operationNames[] = {"Change spinning speed",
"Change light color R1", "Change light color G1", "Change light color B1",
"Change light color R2", "Change light color G2", "Change light color B2",
"Change light color R3", "Change light color G3", "Change light color B3",
"Change light X1", "Change light Y1", "Change light Z1",
"Change light X2", "Change light Y2", "Change light Z2",
"Change light X3", "Change light Y3", "Change light Z3",
"Change screen X offset", "Change screen Y offset",
"Change screen depth",
"Change translation vector X", "Change translation vector Y",
"Change translation vector Z",
};
int stopped = 0;
int currentOperation = 0;
int spinningSpeed = 16;
int screenXOffset = CENTERX;
int screenYOffset = CENTERY;
int screenDepth = CENTERX;
VECTOR pos = { .vx = 0, .vy = 0, .vz = 400 }; /* Translation vector for TransMatrix */
#define COLOR_STEP 32
#define LIGHT_STEP 128
/* Main function */
int main() {
int i,p,wasStart=0,wasLeft=0,wasRight=0;
int a,b;
short coord[2];
unsigned char rgb[3] = {255, 255, 0};
unsigned short padbuf;
SVECTOR rot; /* Rotation vector for Rotmatrix */
MATRIX mtx,lmtx; /* Rotation matrices for geometry and lighting */
bzero(&rot, sizeof(rot));
/* Init graphics and GTE */
init();
/* Main loop */
while( 1 ) {
/* Set GTE offset (recommended method of centering) */
gte_SetGeomOffset( screenXOffset, screenYOffset );
/* Set screen depth (basically FOV control, W/2 works best) */
gte_SetGeomScreen(screenDepth);
/* Set light ambient color and light color matrix */
gte_SetBackColor( 63, 63, 63 );
gte_SetColorMatrix( &color_mtx );
/* Set rotation and translation to the matrix */
RotMatrix( &rot, &mtx );
TransMatrix( &mtx, &pos );
/* Multiply light matrix by rotation matrix so light source */
/* won't appear relative to the model's rotation */
MulMatrix0( &light_mtx, &mtx, &lmtx );
/* Set rotation and translation matrix */
gte_SetRotMatrix( &mtx );
gte_SetTransMatrix( &mtx );
/* Set light matrix */
gte_SetLightMatrix( &lmtx );
if (!stopped) {
/* Make the cube SPEEN */
rot.vx += spinningSpeed;
rot.vz += spinningSpeed;
}
for( i=0; i<CUBE_FACES; i++ ) {
/* Load the first 3 vertices of a quad to the GTE */
gte_ldv3(
&cube_verts[cube_indices[i].v0],
&cube_verts[cube_indices[i].v1],
&cube_verts[cube_indices[i].v2] );
/* Rotation, Translation and Perspective Triple */
gte_rtpt();
/* Compute normal clip for backface culling */
gte_nclip();
/* Get result*/
gte_stopz( &p );
/* Skip this face if backfaced */
if( p < 0 ) {
poly[i].attribute = -1;
continue;
}
/* Calculate average Z for depth sorting */
gte_avsz4();
gte_stotz( &p );
/* Skip if clipping off */
/* (the shift right operator is to scale the depth precision) */
if( (p>>2) > OT_LEN ) {
poly[i].attribute = -1;
continue;
}
/* Set the projected vertices to the primitive */
gte_stsxy0( &coord );
poly[i].x[0] = coord[0];
poly[i].y[0] = coord[1];
gte_stsxy1( &coord );
poly[i].x[1] = coord[0];
poly[i].y[1] = coord[1];
gte_stsxy2( &coord );
poly[i].x[2] = coord[0];
poly[i].y[2] = coord[1];
/* Compute the last vertex and set the result */
gte_ldv0( &cube_verts[cube_indices[i].v3] );
gte_rtps();
gte_stsxy( &coord );
poly[i].x[3] = coord[0];
poly[i].y[3] = coord[1];
rgb[0] = 255;
rgb[1] = 255;
rgb[2] = 255;
gte_ldrgb( &rgb );
/* Load the face normal */
gte_ldv0( &cube_norms[i] );
/* Normal Color Single */
gte_nccs();
/* Store result to the primitive */
gte_strgb( &rgb );
poly[i].r = rgb[0];
poly[i].g = rgb[1];
poly[i].b = rgb[2];
poly[i].attribute = 0;
}
/* Swap buffers and draw the primitives */
display();
/* Wait for VBlank to happen */
vblank_happened = 0;
while(!vblank_happened);
/* Read joypad status */
PSX_ReadPad(&padbuf, NULL);
/* Logic for real-time 'configuration' of the cube */
if ((padbuf & PAD_START) && !wasStart) {
stopped = !stopped;
wasStart = 1;
}
if ((padbuf & PAD_LEFT) && !wasLeft) {
currentOperation--;
if (currentOperation < 0)
currentOperation = OP_END - 1;
wasLeft = 1;
}
if ((padbuf & PAD_RIGHT) && !wasRight) {
currentOperation++;
if (currentOperation >= OP_END)
currentOperation = 0;
wasRight = 1;
}
if (padbuf & PAD_UP) {
switch(currentOperation) {
case OP_SPIN:
spinningSpeed++;
break;
case OP_COLORR1 ... OP_COLORB3:
a = (currentOperation - OP_COLORR1);
b = a % 3;
a /= 3;
if(color_mtx.m[b][a] < ONE)
color_mtx.m[b][a]+=COLOR_STEP;
break;
case OP_LIGHTX1 ... OP_LIGHTZ3:
a = (currentOperation - OP_LIGHTX1);
b = a % 3;
a /= 3;
if(light_mtx.m[a][b] < ONE)
light_mtx.m[a][b]+=LIGHT_STEP;
break;
case OP_SCRXOFF:
screenXOffset++;
break;
case OP_SCRYOFF:
screenYOffset++;
break;
case OP_SCRDEPTH:
screenDepth++;
break;
case OP_TRANSVX:
pos.vx++;
break;
case OP_TRANSVY:
pos.vy++;
break;
case OP_TRANSVZ:
pos.vz++;
break;
}
}
if (padbuf & PAD_DOWN) {
switch(currentOperation) {
case OP_SPIN:
spinningSpeed--;
break;
case OP_COLORR1 ... OP_COLORB3:
a = (currentOperation - OP_COLORR1);
b = a % 3;
a /= 3;
if(color_mtx.m[b][a] > 0)
color_mtx.m[b][a]-=COLOR_STEP;
break;
case OP_LIGHTX1 ... OP_LIGHTZ3:
a = (currentOperation - OP_LIGHTX1);
b = a % 3;
a /= 3;
if(light_mtx.m[a][b] > 0)
light_mtx.m[a][b]-=LIGHT_STEP;
break;
case OP_SCRXOFF:
screenXOffset--;
break;
case OP_SCRYOFF:
screenYOffset--;
break;
case OP_SCRDEPTH:
screenDepth--;
break;
case OP_TRANSVX:
pos.vx--;
break;
case OP_TRANSVY:
pos.vy--;
break;
case OP_TRANSVZ:
pos.vz--;
break;
}
}
if (!(padbuf & PAD_START))
wasStart = 0;
if (!(padbuf & PAD_LEFT))
wasLeft = 0;
if (!(padbuf & PAD_RIGHT))
wasRight = 0;
}
return 0;
}
void init(void) {
/* Initialize PSXSDK */
PSX_InitEx( 0 );
/* Initialize the Graphic Synthesizer */
GsInit();
/* Clear video memory */
GsClearMem();
/* Set primitive list pointer */
GsSetList( prim_list );
/* Set automatic GPU waiting */
GsSetAutoWait();
/* Set 320x240 video mode */
GsSetVideoMode(320, 240, EXAMPLES_VMODE);
/* Set VBlank Handler */
SetVBlankHandler( program_vblank_handler );
/* Load text font in video memory */
GsLoadFont(768, 0, 768, 256);
/* Initialize the MeidoGTE library */
InitGeom();
}
void display(void) {
int i;
/* Change display and drawing environment settings */
/* for double buffering */
GsSetDispEnvSimple(0, current_buf ? 0 : 256);
GsSetDrawEnvSimple(0, current_buf ? 256 : 0, SCREEN_XRES, SCREEN_YRES);
current_buf = !current_buf;
/* Clear the screen with the specified color */
GsSortCls(63, 0, 127);
/* Add cube faces to drawing list */
for(i=0;i<CUBE_FACES;i++) {
if(poly[i].attribute != -1)
GsSortPoly4(&poly[i]);
}
/* If stopped, show so on the screen */
if (stopped)
GsPrintFont(0, 0, "Stopped");
/* Print current operation name on screen */
GsPrintFont(0, GsScreenH-16, "%s", operationNames[currentOperation]);
/* Draw list */
GsDrawList();
}
|