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
|
/*
* PSn00bSDK GTE library
* (C) 2019-2022 Lameguy64 - MPL licensed
*/
/**
* @file psxgte.h
* @brief GTE library header
*
* @details The Geometry Transformation Engine, often referred to as the GTE,
* is most responsible for providing 3D capabilities to the PS1. This is
* effectively an all-integer math co-processor connected directly to the CPU,
* as it is accessed using COP2 and related MIPS instructions to access
* registers and issue commands to the GTE.
*/
#pragma once
#include <stdint.h>
#define ONE (1 << 12)
/* Structure definitions */
typedef struct _MATRIX {
int16_t m[3][3];
int32_t t[3];
} MATRIX;
typedef struct _VECTOR {
int32_t vx, vy, vz;
} VECTOR;
typedef struct _SVECTOR {
int16_t vx, vy, vz, pad;
} SVECTOR;
typedef struct _CVECTOR {
uint8_t r, g, b, cd;
} CVECTOR;
typedef struct _DVECTOR {
int16_t vx, vy;
} DVECTOR;
/* Public API */
#define csin(a) isin(a)
#define ccos(a) icos(a)
#define rsin(a) isin(a)
#define rcos(a) icos(a)
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Gets sine of angle (fixed-point)
*
* @details Returns the sine of angle a.
*
* @param a Angle in fixed-point format (131072 = 360 degrees)
* @return Sine value in 20.12 fixed-point format (4096 = 1.0).
*/
int isin(int a);
/**
* @brief Gets cosine of angle (fixed-point)
*
* @details Returns the cosine of angle a.
*
* @param a Angle in fixed-point format (131072 = 360 degrees)
* @return Cosine value in 20.12 fixed-point format (4096 = 1.0).
*/
int icos(int a);
/**
* @brief Gets sine of angle (fixed-point, high precision version)
*
* @details Returns the sine of angle a.
*
* @param a Angle in fixed-point format (4194304 = 360 degrees)
* @return Sine value in 20.12 fixed-point format (4096 = 1.0).
*/
int hisin(int a);
/**
* @brief Gets cosine of angle (fixed-point, high precision version)
*
* @details Returns the cosine of angle a.
*
* @param a Angle in fixed-point format (4194304 = 360 degrees)
* @return Cosine value in 20.12 fixed-point format (4096 = 1.0).
*/
int hicos(int a);
/**
* @brief Initializes the GTE
*
* @details Resets, enables and initializes the GTE. Must be called prior to
* using any GTE function or macro.
*/
void InitGeom(void);
/**
* @brief Gets square root (fixed-point)
*
* @details Returns the square root of value v.
*
* @param v Value in 20.12 fixed-point format (4096 = 1.0)
* @return Square root in 20.12 fixed-point format (4096 = 1.0).
*/
int SquareRoot12(int v);
/**
* @brief Gets square root (integer)
*
* @details Returns the square root of value v.
*
* @param v Value in integer format
* @return Square root in integer format.
*/
int SquareRoot0(int v);
/**
* @brief Pushes the current GTE matrix to the matrix stack
*
* @details Pushes the current GTE rotation matrix and translation vector to
* the internal matrix stack. Only one matrix stack level is currently
* supported.
*/
void PushMatrix(void);
/**
* @brief Pops the last matrix pushed into the matrix stack back to the GTE
*
* @details Pops the last inserted matrix in the internal matrix stack back to
* the GTE. Only one matrix stack level is currently supported.
*/
void PopMatrix(void);
/**
* @brief Defines the rotation matrix of a MATRIX
*
* @details Defines the rotation matrix of m from rotation coordinates of r.
* The matrix is computed as follows:
*
* [ 1 0 0 ] [ cy 0 sy] [ cz -sz 0 ]
* [ 0 cx -sx] * [ 0 1 0 ] * [ sz cz 0 ]
* [ 0 sx cx] [-sy 0 cy] [ 0 0 1 ]
*
* where:
*
* sx = sin(r.x) sy = sin(r.y) sz = sin(r.z)
* cx = cos(r.x) cy = cos(r.y) cz = cos(r.z)
*
* @param r Rotation vector (input)
* @param m Matrix (output)
* @return Pointer to m.
*
* @see TransMatrix(), CompMatrixLV()
*/
MATRIX *RotMatrix(SVECTOR *r, MATRIX *m);
/**
* @brief Defines the rotation matrix of a MATRIX (high precision version)
*
* @details Defines the rotation matrix of m from rotation coordinates of r.
* This function is a variant of RotMatrix() that uses hisin()/hicos() instead
* of isin()/icos().
*
* See RotMatrix() for more details.
*
* @param r Rotation vector (input)
* @param m Matrix (output)
* @return Pointer to m.
*
* @see RotMatrix()
*/
MATRIX *HiRotMatrix(VECTOR *r, MATRIX *m);
/**
* @brief Defines the translation vector of a MATRIX
*
* @details Simply sets the translation vector of MATRIX m. To perform
* accumulative translation operations, see CompMatrixLV().
*
* @param m Matrix (output)
* @param r Translation vector (input)
* @return Pointer to m.
*
* @see RotMatrix(), CompMatrixLV()
*/
MATRIX *TransMatrix(MATRIX *m, VECTOR *r);
MATRIX *ScaleMatrix(MATRIX *m, VECTOR *s);
MATRIX *ScaleMatrixL(MATRIX *m, VECTOR *s);
MATRIX *MulMatrix(MATRIX *m0, MATRIX *m1);
MATRIX *MulMatrix0(MATRIX *m0, MATRIX *m1, MATRIX *m2);
/**
* @brief Composite coordinate matrix transform
*
* @details Performs vector multiply by matrix with vector addition from v0 to
* the translation vector of v1. Then, multiples the rotation matrix of v0 by
* the rotation matrix of v1. The result of both operations is then stored in
* v2. Replaces the current GTE rotation matrix and translation vector with v0.
*
* Often used to adjust the matrix (includes rotation and translation) of an
* object relative to a world matrix, so the object would render relative to
* the world matrix.
*
* @param v0 Input matrix A
* @param v1 Input matrix B
* @param v2 Output matrix
* @return Pointer to v2.
*/
MATRIX *CompMatrixLV(MATRIX *v0, MATRIX *v1, MATRIX *v2);
/**
* @brief Multiplies a vector by a matrix
*
* @details Multiplies vector v0 with matrix m, result is stored to v1.
* Replaces the current GTE rotation matrix and translation vector with m.
*
* Often used to calculate a translation vector in relation to the rotation
* matrix for first person or vector camera perspectives.
*
* @param m Input matrix
* @param v0 Input vector
* @param v1 Output vector
* @return Pointer to v1.
*/
VECTOR *ApplyMatrixLV(MATRIX *m, VECTOR *v0, VECTOR *v1);
/**
* @brief Normalizes a VECTOR into SVECTOR format
*
* Normalizes a 32-bit vector into a 16-bit vector in 4.12 fixed-point format
* (4096 = 1.0, 2048 = 0.5).
*
* @param v0 Input (raw) 32-bit vector
* @param v1 Output (normalized) 16-bit vector
*/
void VectorNormalS(VECTOR *v0, SVECTOR *v1);
/**
* @brief Calculates the square of a VECTOR
*
* @details Calculates the square of vector v0 and stores the result to v1.
*
* @param v0 Input vector
* @param v1 Output vector
*/
void Square0(VECTOR *v0, VECTOR *v1);
#ifdef __cplusplus
}
#endif
|