10 /*#define DEBUG_OVERDRAW G3D_PACK_RGB(10, 10, 10)*/
12 #define FILL_POLY_BITS 0x03
14 /*void polyfill_tex_flat_new(struct pvertex *varr);*/
16 /* mode bits: 00-wire 01-flat 10-gouraud 11-reserved
20 void (*fillfunc[])(struct pvertex*) = {
31 polyfill_gouraud_zbuf,
34 polyfill_tex_flat_zbuf,
35 polyfill_tex_gouraud_zbuf,
39 struct pimage pfill_fb, pfill_tex;
41 struct pgradient pgrad;
44 static struct pvertex *edgebuf, *left, *right;
45 static int edgebuf_size;
49 #define CHECKEDGE(x) \
52 assert(x < fbheight); \
58 void polyfill_fbheight(int height)
60 int newsz = (height * 2 + EDGEPAD * 3) * sizeof *edgebuf;
62 if(newsz > edgebuf_size) {
64 if(!(edgebuf = malloc(newsz))) {
65 fprintf(stderr, "failed to allocate edge table buffer (%d bytes)\n", newsz);
70 left = edgebuf + EDGEPAD;
71 right = edgebuf + height + EDGEPAD * 2;
74 memset(edgebuf, 0xaa, EDGEPAD * sizeof *edgebuf);
75 memset(edgebuf + height + EDGEPAD, 0xaa, EDGEPAD * sizeof *edgebuf);
76 memset(edgebuf + height * 2 + EDGEPAD * 2, 0xaa, EDGEPAD * sizeof *edgebuf);
83 void polyfill(int mode, struct pvertex *verts)
87 fprintf(stderr, "polyfill mode %d not implemented\n", mode);
92 fillfunc[mode](verts);
95 void polyfill_wire(struct pvertex *verts)
97 int i, x0, y0, x1, y1;
98 struct pvertex *v = verts;
99 int color = find_color(v->l, v->l, v->l);
107 if(clip_line(&x0, &y0, &x1, &y1, 0, 0, pfill_fb.width, pfill_fb.height)) {
108 draw_line(x0, y0, x1, y1, color);
111 x0 = verts[0].x >> 8;
112 y0 = verts[0].y >> 8;
113 if(clip_line(&x1, &y1, &x0, &y0, 0, 0, pfill_fb.width, pfill_fb.height)) {
114 draw_line(x1, y1, x0, y0, color);
118 void polyfill_tex_wire(struct pvertex *verts)
120 polyfill_wire(verts); /* TODO */
123 void polyfill_alpha_wire(struct pvertex *verts)
125 polyfill_wire(verts); /* TODO */
128 void polyfill_alpha_tex_wire(struct pvertex *verts)
130 polyfill_wire(verts); /* TODO */
133 void polyfill_add_wire(struct pvertex *verts)
135 polyfill_wire(verts); /* TODO */
138 void polyfill_add_tex_wire(struct pvertex *verts)
140 polyfill_wire(verts); /* TODO */
143 #define VNEXT(p) (((p) == varr + 2) ? varr : (p) + 1)
144 #define VPREV(p) ((p) == varr ? varr + 2 : (p) - 1)
145 #define VSUCC(p, side) ((side) == 0 ? VNEXT(p) : VPREV(p))
147 /* extra bits of precision to use when interpolating colors.
148 * try tweaking this if you notice strange quantization artifacts.
150 #define COLOR_SHIFT 12
153 #define POLYFILL polyfill_flat
157 #include "polytmpl.h"
160 #define POLYFILL polyfill_gouraud
164 #include "polytmpl.h"
167 #define POLYFILL polyfill_tex_flat
171 #include "polytmpl.h"
174 #define POLYFILL polyfill_tex_gouraud
178 #include "polytmpl.h"
181 /* ---- zbuffer variants ----- */
183 #define POLYFILL polyfill_flat_zbuf
187 #include "polytmpl.h"
190 #define POLYFILL polyfill_gouraud_zbuf
194 #include "polytmpl.h"
197 #define POLYFILL polyfill_tex_flat_zbuf
201 #include "polytmpl.h"
204 #define POLYFILL polyfill_tex_gouraud_zbuf
208 #include "polytmpl.h"
212 void polyfill_tex_flat_new(struct pvertex *varr)
214 int i, line, top, bot;
215 struct pvertex *v, *vn, *tab;
216 int32_t x, y0, y1, dx, dy, slope, fx, fy;
218 g3d_pixel *fbptr, *pptr, color;
219 int32_t tu, tv, du, dv, uslope, vslope;
223 top = pfill_fb.height;
227 /* scan the edge between the current and next vertex */
231 if(vn->y == v->y) continue; /* XXX ??? */
234 /* inrementing Y: left side */
237 /* decrementing Y: right side, flip vertices to trace bottom->up */
243 /* calculate edge slope */
246 slope = (dx << 8) / dy;
252 uslope = (du << 8) / dy;
253 vslope = (dv << 8) / dy;
255 y0 = (v->y + 0x100) & 0xffffff00; /* start from the next scanline */
256 fy = y0 - v->y; /* fractional part before the next scanline */
257 fx = (fy * slope) >> 8; /* X adjust for the step to the next scanline */
258 x = v->x + fx; /* adjust X */
259 y1 = vn->y & 0xffffff00; /* last scanline of the edge <= vn->y */
261 /* also adjust other interpolated attributes */
262 tu += (fy * uslope) >> 8;
263 tv += (fy * vslope) >> 8;
266 if(line < top) top = line;
267 if((y1 >> 8) > bot) bot = y1 >> 8;
269 if(line > 0) tab += line;
271 while(line <= (y1 >> 8) && line < pfill_fb.height) {
273 int val = x < 0 ? 0 : x >> 8;
274 tab->x = val < pfill_fb.width ? val : pfill_fb.width - 1;
287 if(bot >= pfill_fb.height) bot = pfill_fb.height - 1;
289 fbptr = pfill_fb.pixels + top * pfill_fb.width;
290 for(i=top; i<=bot; i++) {
292 len = right[i].x - start;
293 /* XXX we probably need more precision in left/right.x */
295 dx = len == 0 ? 256 : (len << 8);
300 pptr = fbptr + start;
304 tx = (tu >> (16 - pfill_tex.xshift)) & pfill_tex.xmask;
305 ty = (tv >> (16 - pfill_tex.yshift)) & pfill_tex.ymask;
306 texel = pfill_tex.pixels[(ty << pfill_tex.xshift) + tx];
315 /* This is not correct, should be /255, but it's much faster
316 * to shift by 8 (/256), and won't make a huge difference
318 cr = (cr * G3D_UNPACK_R(texel)) >> 8;
319 cg = (cg * G3D_UNPACK_G(texel)) >> 8;
320 cb = (cb * G3D_UNPACK_B(texel)) >> 8;
322 if(cr >= 255) cr = 255;
323 if(cg >= 255) cg = 255;
324 if(cb >= 255) cb = 255;
325 color = G3D_PACK_RGB(cr, cg, cb);
328 fbptr += pfill_fb.width;