10 void (*fillfunc[])(struct pvertex*, int) = {
16 struct pimage pimg_fb, pimg_texture;
18 void polyfill(int mode, struct pvertex *verts, int nverts)
22 fprintf(stderr, "polyfill mode %d not implemented\n", mode);
27 fillfunc[mode](verts, nverts);
30 void polyfill_wire(struct pvertex *verts, int nverts)
32 int i, x0, y0, x1, y1;
33 struct pvertex *v = verts;
34 unsigned short color = ((v->r << 8) & 0xf800) |
35 ((v->g << 3) & 0x7e0) | ((v->b >> 3) & 0x1f);
37 for(i=0; i<nverts - 1; i++) {
43 if(clip_line(&x0, &y0, &x1, &y1, 0, 0, pimg_fb.width, pimg_fb.height)) {
44 draw_line(x0, y0, x1, y1, color);
49 if(clip_line(&x1, &y1, &x0, &y0, 0, 0, pimg_fb.width, pimg_fb.height)) {
50 draw_line(x1, y1, x0, y0, color);
54 static uint32_t scan_edge(struct pvertex *v0, struct pvertex *v1, struct pvertex *edge);
56 #define NEXTIDX(x) (((x) - 1 + nverts) % nverts)
57 #define PREVIDX(x) (((x) + 1) % nverts)
59 void polyfill_flat(struct pvertex *pv, int nverts)
62 int topidx = 0, botidx = 0, sltop = pimg_fb.height, slbot = 0;
63 struct pvertex *left, *right;
64 uint16_t color = PACK_RGB16(pv[0].r, pv[0].g, pv[0].b);
66 for(i=1; i<nverts; i++) {
67 if(pv[i].y < pv[topidx].y) topidx = i;
68 if(pv[i].y > pv[botidx].y) botidx = i;
71 left = (struct pvertex*)alloca(pimg_fb.height * sizeof *left);
72 right = (struct pvertex*)alloca(pimg_fb.height * sizeof *right);
73 memset(left, 0, pimg_fb.height * sizeof *left);
74 memset(right, 0, pimg_fb.height * sizeof *right);
76 for(i=0; i<nverts; i++) {
77 int next = NEXTIDX(i);
79 int32_t y1 = pv[next].y;
81 if((y0 >> 8) == (y1 >> 8)) {
84 left[idx].x = pv[i].x < pv[next].x ? pv[i].x : pv[next].x;
85 right[idx].x = pv[i].x < pv[next].x ? pv[next].x : pv[i].x;
88 struct pvertex *edge = y0 > y1 ? left : right;
89 uint32_t res = scan_edge(pv + i, pv + next, edge);
90 uint32_t tmp = (res >> 16) & 0xffff;
91 if(tmp > slbot) slbot = tmp;
92 if((tmp = res & 0xffff) < sltop) {
98 for(i=sltop; i<=slbot; i++) {
103 pixptr = pimg_fb.pixels + i * pimg_fb.width + (x >> 8);
105 while(x <= right[i].x) {
106 #ifdef DEBUG_POLYFILL
116 static uint32_t scan_edge(struct pvertex *v0, struct pvertex *v1, struct pvertex *edge)
119 int32_t x, dx, dy, slope;
120 int32_t start_idx, end_idx;
123 struct pvertex *tmp = v0;
130 slope = (dx << 8) / dy;
132 start_idx = v0->y >> 8;
133 end_idx = v1->y >> 8;
136 for(i=start_idx; i<end_idx; i++) {
141 return (uint32_t)start_idx | ((uint32_t)(end_idx - 1) << 16);