X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?a=blobdiff_plain;f=src%2Fpolyfill.c;h=122b23491f3013278917f50d5356c9e472581491;hb=dce48eb322ddf2a3c3622fc132ece21ba7653da3;hp=215cd785c9ec5ff128b4089d09223362d2e1660e;hpb=633743214adddf6ec20f8b1bee1782e6966023af;p=dosdemo diff --git a/src/polyfill.c b/src/polyfill.c index 215cd78..122b234 100644 --- a/src/polyfill.c +++ b/src/polyfill.c @@ -1,5 +1,8 @@ #include #include +#include +#include +#include #include "polyfill.h" #include "gfxutil.h" #include "demo.h" @@ -10,6 +13,8 @@ void (*fillfunc[])(struct pvertex*, int) = { 0, 0, 0 }; +struct pimage pimg_fb, pimg_texture; + void polyfill(int mode, struct pvertex *verts, int nverts) { #ifndef NDEBUG @@ -35,93 +40,103 @@ void polyfill_wire(struct pvertex *verts, int nverts) ++v; x1 = v->x >> 8; y1 = v->y >> 8; - if(clip_line(&x0, &y0, &x1, &y1, 0, 0, fb_width, fb_height)) { + if(clip_line(&x0, &y0, &x1, &y1, 0, 0, pimg_fb.width, pimg_fb.height)) { draw_line(x0, y0, x1, y1, color); } } x0 = verts[0].x >> 8; y0 = verts[0].y >> 8; - if(clip_line(&x1, &y1, &x0, &y0, 0, 0, fb_width, fb_height)) { + if(clip_line(&x1, &y1, &x0, &y0, 0, 0, pimg_fb.width, pimg_fb.height)) { draw_line(x1, y1, x0, y0, color); } } -#define NEXTIDX(x) ((x) ? (x) - 1 : nverts - 1) -#define PREVIDX(x) (((x) + 1) % nverts) +static uint32_t scan_edge(struct pvertex *v0, struct pvertex *v1, struct pvertex *edge); -#define CALC_EDGE(which) \ - do { \ - which##_x = pv[which##_beg].x; \ - which##_dx = pv[which##_end].x - pv[which##_beg].x; \ - which##_slope = (which##_dx << 8) / which##_dy; \ - } while(0) +#define NEXTIDX(x) (((x) - 1 + nverts) % nverts) +#define PREVIDX(x) (((x) + 1) % nverts) void polyfill_flat(struct pvertex *pv, int nverts) { - int i, sline, x, slen, top = 0; - int left_beg, left_end, right_beg, right_end; - int32_t left_dy, left_dx, right_dy, right_dx; - int32_t left_slope, right_slope; - int32_t left_x, right_x, y; - uint16_t color = ((pv->r << 8) & 0xf800) | ((pv->g << 3) & 0x7e0) | - ((pv->b >> 3) & 0x1f); - uint16_t *pixptr; - - /* find topmost */ - for(i=1; i pv[botidx].y) botidx = i; } - if((right_dy = pv[right_end].y - pv[right_beg].y)) { - CALC_EDGE(right); + left = (struct pvertex*)alloca(pimg_fb.height * sizeof *left); + right = (struct pvertex*)alloca(pimg_fb.height * sizeof *right); + memset(left, 0, pimg_fb.height * sizeof *left); + memset(right, 0, pimg_fb.height * sizeof *right); + + for(i=0; i> 8) == (y1 >> 8)) { + if(y0 > y1) { + int idx = y0 >> 8; + left[idx].x = pv[i].x < pv[next].x ? pv[i].x : pv[next].x; + right[idx].x = pv[i].x < pv[next].x ? pv[next].x : pv[i].x; + } + } else { + struct pvertex *edge = y0 > y1 ? left : right; + uint32_t res = scan_edge(pv + i, pv + next, edge); + uint32_t tmp = (res >> 16) & 0xffff; + if(tmp > slbot) slbot = tmp; + if((tmp = res & 0xffff) < sltop) { + sltop = tmp; + } + } } - y = pv[top].y; - sline = pv[top].y >> 8; + for(i=sltop; i<=slbot; i++) { + int32_t x; + uint16_t *pixptr; - for(;;) { - if(y >= pv[left_end].y) { - while(y >= pv[left_end].y) { - left_beg = left_end; - if(left_beg == right_beg) return; - left_end = PREVIDX(left_end); - } + x = left[i].x; + pixptr = pimg_fb.pixels + i * pimg_fb.width + (x >> 8); - left_dy = pv[left_end].y - pv[left_beg].y; - CALC_EDGE(left); + while(x <= right[i].x) { +#ifdef DEBUG_POLYFILL + *pixptr++ += 15; +#else + *pixptr++ = color; +#endif + x += 256; } + } +} - if(y >= pv[right_end].y) { - while(y >= pv[right_end].y) { - right_beg = right_end; - if(left_beg == right_beg) return; - right_end = NEXTIDX(right_end); - } - - right_dy = pv[right_end].y - pv[right_beg].y; - CALC_EDGE(right); - } +static uint32_t scan_edge(struct pvertex *v0, struct pvertex *v1, struct pvertex *edge) +{ + int i; + int32_t x, dx, dy, slope; + int32_t start_idx, end_idx; + + if(v0->y > v1->y) { + struct pvertex *tmp = v0; + v0 = v1; + v1 = tmp; + } - x = left_x >> 8; - slen = (right_x >> 8) - (left_x >> 8); + dy = v1->y - v0->y; + dx = v1->x - v0->x; + slope = (dx << 8) / dy; - pixptr = (uint16_t*)fb_pixels + sline * fb_width + x; - for(i=0; iy >> 8; + end_idx = v1->y >> 8; - ++sline; - y += 256; - left_x += left_slope; - right_x += right_slope; + x = v0->x; + for(i=start_idx; i