-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgfx.c
386 lines (343 loc) · 10.2 KB
/
gfx.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
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
/*****************************************************************************/
/*
* PiSHi LE (Lite edition) - Fundamentals of the King's Crook graphics engine.
*
* by EMMIR 2018-2022
*
* YouTube: https://www.youtube.com/c/LMP88
*
* This software is released into the public domain.
*/
/*****************************************************************************/
#include "pl.h"
/* gfx.c
*
* Scans and rasterizes polygons.
*
*/
#include <string.h>
#include <limits.h>
int PL_hres;
int PL_vres;
int PL_hres_h;
int PL_vres_h;
int PL_polygon_count;
int *PL_video_buffer = NULL;
int *PL_depth_buffer = NULL;
#define ZP 15 /* z precision */
#define TXSH PL_REQ_TEX_LOG_DIM
#define TXMSK ((1 << (TXSH + PL_TP)) - 1)
#define TXVMSK (TXMSK & (~((1<<PL_TP) - 1)))
#define ATTRIBS 8
#define ATTRIB_BITS 3
/* y to table */
#define YT(y) ((y) << ATTRIB_BITS)
#define ZL(ytb) ((ytb) + 0)
#define UL(ytb) ((ytb) + 2)
#define VL(ytb) ((ytb) + 4)
#define ZR(ytb) ((ytb) + 1)
#define UR(ytb) ((ytb) + 3)
#define VR(ytb) ((ytb) + 5)
#define SCANP 18
#define SCANP_ROUND (1 << (SCANP - 1))
static int scan_miny;
static int scan_maxy;
/* integer reserve for data locality */
static int g3dresv[PL_MAX_SCREENSIZE /* x_L */
+ PL_MAX_SCREENSIZE /* x_R */
+ PL_MAX_SCREENSIZE /* xLc */
+ PL_MAX_SCREENSIZE /* xRc */
+ (ATTRIBS * PL_MAX_SCREENSIZE) /* attrbuf */
];
#define G3R_OFFS_XL (0)
#define G3R_OFFS_XR (G3R_OFFS_XL + PL_MAX_SCREENSIZE)
#define G3R_OFFS_XLC (G3R_OFFS_XR + PL_MAX_SCREENSIZE)
#define G3R_OFFS_XLR (G3R_OFFS_XLC + PL_MAX_SCREENSIZE)
#define G3R_OFFS_ATTR (G3R_OFFS_XLR + PL_MAX_SCREENSIZE)
static int *x_L = g3dresv + G3R_OFFS_XL;
static int *x_R = g3dresv + G3R_OFFS_XR;
static int *attrbuf = g3dresv + G3R_OFFS_ATTR; /* attribute buffer */
/* cleared versions of scan conversion L/R buffers */
static int *xLc = g3dresv + G3R_OFFS_XLC;
static int *xRc = g3dresv + G3R_OFFS_XLR;
static unsigned char mul8[256][256];
extern void
PL_init(int *video, int hres, int vres)
{
int i, j;
PL_hres = hres;
PL_vres = vres;
PL_hres_h = PL_hres >> 1;
PL_vres_h = PL_vres >> 1;
PL_set_viewport(0, 0, PL_hres - 1, PL_vres - 1, 1);
if (PL_depth_buffer) {
EXT_free(PL_depth_buffer);
PL_depth_buffer = NULL;
}
PL_depth_buffer = EXT_calloc(PL_hres * PL_vres, sizeof(int));
if (PL_depth_buffer == NULL) {
EXT_error(PL_ERR_NO_MEM, "gfx", "no memory");
}
PL_video_buffer = video;
/* 8-bit * 8-bit number multiplication table */
for (i = 0; i < 256; i++) {
for (j = 0; j < 256; j++) {
mul8[i][j] = (unsigned char) ((i * j) >> 8);
}
}
/* set buffer offsets */
x_L = g3dresv;
x_R = x_L + vres;
xLc = x_R + vres;
xRc = xLc + vres;
for (i = 0; i < vres; i++) {
xLc[i] = INT_MAX;
xRc[i] = INT_MIN;
}
/* sine is mirrored over X after PI */
for (i = 0; i < (PL_TRIGMAX >> 1); i++) {
PL_sin[(PL_TRIGMAX >> 1) + i] = -PL_sin[i];
}
/* construct cosine table by copying sine table */
for (i = 0; i < ((PL_TRIGMAX >> 1) + (PL_TRIGMAX >> 2)); i++) {
PL_cos[i] = PL_sin[i + (PL_TRIGMAX >> 2)];
}
for (i = 0; i < (PL_TRIGMAX >> 2); i++) {
PL_cos[i + ((PL_TRIGMAX >> 1) + (PL_TRIGMAX >> 2))] = PL_sin[i];
}
}
static int
packrgb(int r, int g, int b)
{
if ((r | g | b) & ~0xff) {
r = r > 0xff ? 0xff : r;
g = g > 0xff ? 0xff : g;
b = b > 0xff ? 0xff : b;
}
return (r << 16) | (g << 8) | b;
}
extern void
PL_clear_vp(int r, int g, int b)
{
PL_clear_color_vp(r, g, b);
PL_clear_depth_vp();
}
extern void
PL_clear_color_vp(int r, int g, int b)
{
int hc, x, y, yoff;
hc = packrgb(r, g, b);
for (y = PL_vp_min_y; y <= PL_vp_max_y; y++) {
yoff = y * PL_hres;
for (x = PL_vp_min_x; x <= PL_vp_max_x; x++) {
PL_video_buffer[x + yoff] = hc;
}
}
}
extern void
PL_clear_depth_vp(void)
{
int x, y, yoff;
for (y = PL_vp_min_y; y <= PL_vp_max_y; y++) {
yoff = y * PL_hres;
for (x = PL_vp_min_x; x <= PL_vp_max_x; x++) {
PL_depth_buffer[x + yoff] = 0;
}
}
}
/* scan convert polygon */
static int
pscan(int *stream, int dim, int len)
{
int resv[PL_VDIM + PL_VDIM + (PL_MAX_POLY_VERTS * PL_STREAM_TEX)];
int rdim;
int *vA, *vB;
int x, y, dx, dy;
int mjr, ady;
int sx, sy, i;
int *AS; /* attribute buffer ptr */
int *AT = resv + (0 * PL_VDIM); /* vertex attributes */
int *DT = resv + (1 * PL_VDIM); /* delta vertex attributes */
int *VS = resv + (2 * PL_VDIM); /* vertex stream (x-clipped) */
int *ABL = attrbuf + 0; /* left side is +0 */
int *ABR = attrbuf + 1; /* right side is +1 */
rdim = dim - 2;
scan_miny = INT_MAX;
scan_maxy = INT_MIN;
/* clean scan tables */
memcpy(x_L, xLc, PL_vres * sizeof(int));
memcpy(x_R, xRc, PL_vres * sizeof(int));
len = PL_clip_poly_x(VS, stream, dim, len);
while (len--) {
vA = VS;
vB = VS += dim;
if (!PL_clip_line_y(&vA, &vB, dim, PL_vp_min_y, PL_vp_max_y)) {
continue;
}
x = *vA++;
y = *vA++;
dx = *vB++;
dy = *vB++;
if (y < scan_miny) { scan_miny = y; }
if (y > scan_maxy) { scan_maxy = y; }
if (dy < scan_miny) { scan_miny = dy;}
if (dy > scan_maxy) { scan_maxy = dy;}
dx -= x;
dy -= y;
mjr = dx;
ady = dy;
if (dx < 0) { mjr = -dx; }
if (dy < 0) { ady = -dy; }
if (ady > mjr) {
mjr = ady;
}
if (mjr <= 0) {
continue;
}
/* Z precision gets added here */
AT[0] = vA[0] << ZP;
DT[0] = ((vB[0] - vA[0]) << ZP) / mjr;
/* the rest get computed with whatever precision they had */
for (i = 1; i < rdim; i++) {
AT[i] = vA[i];
DT[i] = (vB[i] - vA[i]) / mjr;
}
/* make sure to round! */
x = (x << SCANP) + SCANP_ROUND;
y = (y << SCANP) + SCANP_ROUND;
dx = (dx << SCANP) / mjr;
dy = (dy << SCANP) / mjr;
do {
sx = x >> SCANP;
sy = y >> SCANP;
if (x_L[sy] > sx) {
x_L[sy] = sx;
AS = ABL + YT(sy);
for (i = 0; i < rdim; i++) {
AS[i << 1] = AT[i];
}
}
if (x_R[sy] < sx) {
x_R[sy] = sx;
AS = ABR + YT(sy);
for (i = 0; i < rdim; i++) {
AS[i << 1] = AT[i];
}
}
x += dx;
y += dy;
for (i = 0; i < rdim; i++) {
AT[i] += DT[i];
}
} while (mjr--);
}
return (scan_miny >= scan_maxy);
}
extern void
PL_flat_poly(int *stream, int len, int rgb)
{
int miny, maxy;
int pos, beg, pbg;
register int *vbuf, *zbuf;
int d, yt, dz, sz, dlen;
int r8 = rgb >> 16 & 0xff;
int g8 = rgb >> 8 & 0xff;
int b8 = rgb >> 0 & 0xff;
if (pscan(stream, PL_STREAM_FLAT, len)) {
return;
}
miny = scan_miny;
maxy = scan_maxy;
pos = miny * PL_hres;
while (miny <= maxy) {
beg = x_L[miny];
pbg = pos + beg;
vbuf = PL_video_buffer + pbg;
zbuf = PL_depth_buffer + pbg;
len = x_R[miny] - beg;
dlen = len + (len == 0);
yt = YT(miny);
sz = attrbuf[ZL(yt)];
dz = (attrbuf[ZR(yt)] - sz) / dlen;
do {
if (*zbuf < sz) {
*zbuf = sz;
d = (sz >> 20) * 3 / 2;
if (d >= 256) {
*vbuf = rgb;
} else {
*vbuf = mul8[d][r8] << 16 | mul8[d][g8] << 8 | mul8[d][b8];
}
}
sz += dz;
vbuf++;
zbuf++;
} while (len--);
/* next scanline */
miny++;
pos += PL_hres;
}
PL_polygon_count++;
}
extern void
PL_lintx_poly(int *stream, int len, int *texels)
{
int miny, maxy;
int pos, beg, pbg;
int *vbuf, *zbuf;
int yt;
int du = 0, dv = 0, dz;
int su = 0, sv = 0, sz;
int r, d, dlen;
if (pscan(stream, PL_STREAM_TEX, len)) {
return;
}
miny = scan_miny;
maxy = scan_maxy;
pos = miny * PL_hres;
while (miny <= maxy) {
beg = x_L[miny];
pbg = pos + beg;
vbuf = PL_video_buffer + pbg;
zbuf = PL_depth_buffer + pbg;
len = x_R[miny] - beg;
dlen = len + (len == 0);
yt = YT(miny);
sz = attrbuf[ZL(yt)];
dz = (attrbuf[ZR(yt)] - sz) / dlen;
su = attrbuf[UL(yt)];
du = (attrbuf[UR(yt)] - su) / dlen;
sv = attrbuf[VL(yt)];
dv = (attrbuf[VR(yt)] - sv) / dlen;
while (len >= 0) {
if (*zbuf < sz) {
*zbuf = sz;
su &= TXMSK;
sv &= TXMSK;
/* we can bitwise OR the x and y coordinates together
* because the texture is guaranteed to be square.
*/
yt = texels[(su >> PL_TP) | (sv >> PL_TP << TXSH)];
d = (sz >> 20) * 3 / 2;
if (d >= 256) {
*vbuf = yt;
} else {
r = mul8[d][(yt >> 16) & 0xff] << 16;
r |= mul8[d][(yt >> 8) & 0xff] << 8;
r |= mul8[d][(yt >> 0) & 0xff] << 0;
*vbuf = r;
}
}
su += du;
sv += dv;
sz += dz;
vbuf++;
zbuf++;
len--;
}
/* next scanline */
miny++;
pos += PL_hres;
}
PL_polygon_count++;
}