7 /*#define DEBUG_OVERDRAW PACK_RGB(10, 10, 10)*/
9 #define FILL_POLY_BITS 0x03
12 /* mode bits: 00-wire 01-flat 10-gouraud 11-reserved
14 * bit 3-4: blend mode: 00-none 01-alpha 10-additive 11-reserved
17 void (*fillfunc[])(struct pvertex*, int) = {
28 polyfill_alpha_gouraud,
30 polyfill_alpha_tex_wire,
31 polyfill_alpha_tex_flat,
32 polyfill_alpha_tex_gouraud,
38 polyfill_add_tex_wire,
39 polyfill_add_tex_flat,
40 polyfill_add_tex_gouraud,
41 0, 0, 0, 0, 0, 0, 0, 0, 0,
44 polyfill_gouraud_zbuf,
47 polyfill_tex_flat_zbuf,
48 polyfill_tex_gouraud_zbuf,
51 polyfill_alpha_flat_zbuf,
52 polyfill_alpha_gouraud_zbuf,
54 polyfill_alpha_tex_wire,
55 polyfill_alpha_tex_flat_zbuf,
56 polyfill_alpha_tex_gouraud_zbuf,
59 polyfill_add_flat_zbuf,
60 polyfill_add_gouraud_zbuf,
62 polyfill_add_tex_wire,
63 polyfill_add_tex_flat_zbuf,
64 polyfill_add_tex_gouraud_zbuf,
65 0, 0, 0, 0, 0, 0, 0, 0, 0
68 struct pimage pfill_fb, pfill_tex;
72 static struct pvertex *edgebuf, *left, *right;
73 static int edgebuf_size;
77 #define CHECKEDGE(x) \
80 assert(x < fbheight); \
86 void polyfill_fbheight(int height)
88 int newsz = (height * 2 + EDGEPAD * 3) * sizeof *edgebuf;
90 if(newsz > edgebuf_size) {
92 if(!(edgebuf = malloc(newsz))) {
93 fprintf(stderr, "failed to allocate edge table buffer (%d bytes)\n", newsz);
98 left = edgebuf + EDGEPAD;
99 right = edgebuf + height + EDGEPAD * 2;
102 memset(edgebuf, 0xaa, EDGEPAD * sizeof *edgebuf);
103 memset(edgebuf + height + EDGEPAD, 0xaa, EDGEPAD * sizeof *edgebuf);
104 memset(edgebuf + height * 2 + EDGEPAD * 2, 0xaa, EDGEPAD * sizeof *edgebuf);
111 void polyfill(int mode, struct pvertex *verts, int nverts)
114 if(!fillfunc[mode]) {
115 fprintf(stderr, "polyfill mode %d not implemented\n", mode);
120 fillfunc[mode](verts, nverts);
123 void polyfill_wire(struct pvertex *verts, int nverts)
126 int i, x0, y0, x1, y1;
127 struct pvertex *v = verts;
128 uint32_t color = PACK_RGB(v->r, v->g, v->b);
130 for(i=0; i<nverts - 1; i++) {
136 if(clip_line(&x0, &y0, &x1, &y1, 0, 0, pfill_fb.width, pfill_fb.height)) {
137 draw_line(x0, y0, x1, y1, color);
140 x0 = verts[0].x >> 8;
141 y0 = verts[0].y >> 8;
142 if(clip_line(&x1, &y1, &x0, &y0, 0, 0, pfill_fb.width, pfill_fb.height)) {
143 draw_line(x1, y1, x0, y0, color);
148 void polyfill_tex_wire(struct pvertex *verts, int nverts)
150 polyfill_wire(verts, nverts); /* TODO */
153 void polyfill_alpha_wire(struct pvertex *verts, int nverts)
155 polyfill_wire(verts, nverts); /* TODO */
158 void polyfill_alpha_tex_wire(struct pvertex *verts, int nverts)
160 polyfill_wire(verts, nverts); /* TODO */
163 void polyfill_add_wire(struct pvertex *verts, int nverts)
165 polyfill_wire(verts, nverts); /* TODO */
168 void polyfill_add_tex_wire(struct pvertex *verts, int nverts)
170 polyfill_wire(verts, nverts); /* TODO */
173 #define VNEXT(p) (((p) == vlast) ? varr : (p) + 1)
174 #define VPREV(p) ((p) == varr ? vlast : (p) - 1)
175 #define VSUCC(p, side) ((side) == 0 ? VNEXT(p) : VPREV(p))
177 /* extra bits of precision to use when interpolating colors.
178 * try tweaking this if you notice strange quantization artifacts.
180 #define COLOR_SHIFT 12
183 #define POLYFILL polyfill_flat
189 #include "polytmpl.h"
192 #define POLYFILL polyfill_gouraud
198 #include "polytmpl.h"
201 #define POLYFILL polyfill_tex_flat
207 #include "polytmpl.h"
210 #define POLYFILL polyfill_tex_gouraud
216 #include "polytmpl.h"
219 #define POLYFILL polyfill_alpha_flat
225 #include "polytmpl.h"
228 #define POLYFILL polyfill_alpha_gouraud
234 #include "polytmpl.h"
237 #define POLYFILL polyfill_alpha_tex_flat
243 #include "polytmpl.h"
246 #define POLYFILL polyfill_alpha_tex_gouraud
252 #include "polytmpl.h"
255 #define POLYFILL polyfill_add_flat
261 #include "polytmpl.h"
264 #define POLYFILL polyfill_add_gouraud
270 #include "polytmpl.h"
273 #define POLYFILL polyfill_add_tex_flat
279 #include "polytmpl.h"
282 #define POLYFILL polyfill_add_tex_gouraud
288 #include "polytmpl.h"
291 /* ---- zbuffer variants ----- */
293 #define POLYFILL polyfill_flat_zbuf
299 #include "polytmpl.h"
302 #define POLYFILL polyfill_gouraud_zbuf
308 #include "polytmpl.h"
311 #define POLYFILL polyfill_tex_flat_zbuf
317 #include "polytmpl.h"
320 #define POLYFILL polyfill_tex_gouraud_zbuf
326 #include "polytmpl.h"
329 #define POLYFILL polyfill_alpha_flat_zbuf
335 #include "polytmpl.h"
338 #define POLYFILL polyfill_alpha_gouraud_zbuf
344 #include "polytmpl.h"
347 #define POLYFILL polyfill_alpha_tex_flat_zbuf
353 #include "polytmpl.h"
356 #define POLYFILL polyfill_alpha_tex_gouraud_zbuf
362 #include "polytmpl.h"
365 #define POLYFILL polyfill_add_flat_zbuf
371 #include "polytmpl.h"
374 #define POLYFILL polyfill_add_gouraud_zbuf
380 #include "polytmpl.h"
383 #define POLYFILL polyfill_add_tex_flat_zbuf
389 #include "polytmpl.h"
392 #define POLYFILL polyfill_add_tex_gouraud_zbuf
398 #include "polytmpl.h"