sucks, maybe I should try integer rasterization with error accumulation...
[dosdemo] / src / polyfill.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <assert.h>
5 #include <alloca.h>
6 #include "polyfill.h"
7 #include "gfxutil.h"
8 #include "demo.h"
9
10 void (*fillfunc[])(struct pvertex*, int) = {
11         polyfill_wire,
12         polyfill_flat,
13         0, 0, 0
14 };
15
16 struct pimage pimg_fb, pimg_texture;
17
18 void polyfill(int mode, struct pvertex *verts, int nverts)
19 {
20 #ifndef NDEBUG
21         if(!fillfunc[mode]) {
22                 fprintf(stderr, "polyfill mode %d not implemented\n", mode);
23                 abort();
24         }
25 #endif
26
27         fillfunc[mode](verts, nverts);
28 }
29
30 void polyfill_wire(struct pvertex *verts, int nverts)
31 {
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);
36
37         for(i=0; i<nverts - 1; i++) {
38                 x0 = v->x >> 8;
39                 y0 = v->y >> 8;
40                 ++v;
41                 x1 = v->x >> 8;
42                 y1 = v->y >> 8;
43                 if(clip_line(&x0, &y0, &x1, &y1, 0, 0, pimg_fb.width, pimg_fb.height)) {
44                         draw_line(x0, y0, x1, y1, color);
45                 }
46         }
47         x0 = verts[0].x >> 8;
48         y0 = verts[0].y >> 8;
49         if(clip_line(&x1, &y1, &x0, &y0, 0, 0, pimg_fb.width, pimg_fb.height)) {
50                 draw_line(x1, y1, x0, y0, color);
51         }
52 }
53
54 static void scan_edge(struct pvertex *v0, struct pvertex *v1, struct pvertex *edge);
55
56 #define NEXTIDX(x) (((x) - 1 + nverts) % nverts)
57 #define PREVIDX(x) (((x) + 1) % nverts)
58
59 void polyfill_flat(struct pvertex *pv, int nverts)
60 {
61         int i;
62         int32_t y;
63         int topidx = 0, botidx = 0, sline;
64         struct pvertex *left, *right;
65         /*uint16_t color = PACK_RGB16(pv[0].r, pv[0].g, pv[0].b);*/
66
67         for(i=1; i<nverts; i++) {
68                 if(pv[i].y < pv[topidx].y) topidx = i;
69                 if(pv[i].y > pv[botidx].y) botidx = i;
70         }
71
72         left = (struct pvertex*)alloca(pimg_fb.height * sizeof *left);
73         right = (struct pvertex*)alloca(pimg_fb.height * sizeof *right);
74         memset(left, 0, pimg_fb.height * sizeof *left);
75         memset(right, 0, pimg_fb.height * sizeof *right);
76
77         for(i=0; i<nverts; i++) {
78                 int next = NEXTIDX(i);
79                 int32_t y0 = pv[i].y;
80                 int32_t y1 = pv[next].y;
81
82                 if((y0 >> 8) == (y1 >> 8)) {
83                         if(y0 > y1) {
84                                 int idx = y0 >> 8;
85                                 left[idx].x = pv[i].x < pv[next].x ? pv[i].x : pv[next].x;
86                                 right[idx].x = pv[i].x < pv[next].x ? pv[next].x : pv[i].x;
87                         }
88                 } else {
89                         scan_edge(pv + i, pv + next, y0 > y1 ? left : right);
90                 }
91         }
92
93         y = pv[topidx].y;
94         while(y < pv[botidx].y) {
95                 int32_t x;
96                 uint16_t *pixptr;
97
98                 sline = y >> 8;
99                 x = left[sline].x;
100
101                 pixptr = pimg_fb.pixels + sline * pimg_fb.width + (x >> 8);
102
103                 while(x <= right[sline].x) {
104                         *pixptr++ += 15;
105                         x += 256;
106                 }
107                 y += 256;
108         }
109 }
110
111 static void scan_edge(struct pvertex *v0, struct pvertex *v1, struct pvertex *edge)
112 {
113         int32_t x, y, dx, dy, slope;
114         int idx;
115
116         if(v0->y > v1->y) {
117                 struct pvertex *tmp = v0;
118                 v0 = v1;
119                 v1 = tmp;
120         }
121
122         dy = v1->y - v0->y;
123         dx = v1->x - v0->x;
124         slope = (dx << 8) / dy;
125         idx = v0->y >> 8;
126
127         x = v0->x;
128         y = v0->y;
129         while(y <= v1->y) {
130                 edge[idx++].x = x;
131                 x += slope;
132                 y += 256;
133         }
134 }