added texture mapping
[dosdemo] / src / polytmpl.h
1 static uint32_t SCANEDGE(struct pvertex *v0, struct pvertex *v1, struct pvertex *edge)
2 {
3         int i;
4         int32_t x, dx, dy, slope;
5 #ifdef GOURAUD
6         int r, g, b, dr, dg, db;
7         int32_t rslope, gslope, bslope;
8 #endif
9 #ifdef TEXMAP
10         int32_t u, v, du, dv, uslope, vslope;
11 #endif
12         int32_t start_idx, end_idx;
13
14         if(v0->y > v1->y) {
15                 struct pvertex *tmp = v0;
16                 v0 = v1;
17                 v1 = tmp;
18         }
19
20         x = v0->x;
21         dy = v1->y - v0->y;
22         dx = v1->x - v0->x;
23         slope = (dx << 8) / dy;
24 #ifdef GOURAUD
25         r = (v0->r << 8);
26         g = (v0->g << 8);
27         b = (v0->b << 8);
28         dr = (v1->r << 8) - r;
29         dg = (v1->g << 8) - g;
30         db = (v1->b << 8) - b;
31         rslope = (dr << 8) / dy;
32         gslope = (dg << 8) / dy;
33         bslope = (db << 8) / dy;
34 #endif
35 #ifdef TEXMAP
36         u = v0->u;
37         v = v0->v;
38         du = v1->u - v0->u;
39         dv = v1->v - v0->v;
40         uslope = (du << 8) / dy;
41         vslope = (dv << 8) / dy;
42 #endif
43
44         start_idx = v0->y >> 8;
45         end_idx = v1->y >> 8;
46
47         for(i=start_idx; i<end_idx; i++) {
48                 edge[i].x = x;
49                 x += slope;
50 #ifdef GOURAUD
51                 /* we'll store the color in the edge tables with 8 extra bits of precision */
52                 edge[i].r = r;
53                 edge[i].g = g;
54                 edge[i].b = b;
55                 r += rslope;
56                 g += gslope;
57                 b += bslope;
58 #endif
59 #ifdef TEXMAP
60                 edge[i].u = u;
61                 edge[i].v = v;
62                 u += uslope;
63                 v += vslope;
64 #endif
65         }
66
67         return (uint32_t)start_idx | ((uint32_t)(end_idx - 1) << 16);
68 }
69
70 void POLYFILL(struct pvertex *pv, int nverts)
71 {
72         int i;
73         int topidx = 0, botidx = 0, sltop = pfill_fb.height, slbot = 0;
74         struct pvertex *left, *right;
75         uint16_t color;
76         /* the following variables are used for interpolating horizontally accros scanlines */
77 #if defined(GOURAUD) || defined(TEXMAP)
78         /*int mid;*/
79         int32_t dx/*, tmp*/;
80 #else
81         /* flat version, just pack the color now */
82         color = PACK_RGB16(pv[0].r, pv[0].g, pv[0].b);
83 #endif
84 #ifdef GOURAUD
85         int32_t r, g, b, dr, dg, db, rslope, gslope, bslope;
86 #endif
87 #ifdef TEXMAP
88         int32_t u, v, du, dv, uslope, vslope;
89 #endif
90
91         for(i=1; i<nverts; i++) {
92                 if(pv[i].y < pv[topidx].y) topidx = i;
93                 if(pv[i].y > pv[botidx].y) botidx = i;
94         }
95
96         left = alloca(pfill_fb.height * sizeof *left);
97         right = alloca(pfill_fb.height * sizeof *right);
98
99         for(i=0; i<nverts; i++) {
100                 int next = NEXTIDX(i);
101                 int32_t y0 = pv[i].y;
102                 int32_t y1 = pv[next].y;
103
104                 if((y0 >> 8) == (y1 >> 8)) {
105                         if(y0 > y1) {
106                                 int idx = y0 >> 8;
107                                 left[idx].x = pv[i].x < pv[next].x ? pv[i].x : pv[next].x;
108                                 right[idx].x = pv[i].x < pv[next].x ? pv[next].x : pv[i].x;
109                         }
110                 } else {
111                         struct pvertex *edge = y0 > y1 ? left : right;
112                         uint32_t res = SCANEDGE(pv + i, pv + next, edge);
113                         uint32_t tmp = (res >> 16) & 0xffff;
114                         if(tmp > slbot) slbot = tmp;
115                         if((tmp = res & 0xffff) < sltop) {
116                                 sltop = tmp;
117                         }
118                 }
119         }
120
121         /* find the mid-point and calculate slopes for all attributes */
122 #if 0
123 #if defined(GOURAUD) || defined(TEXMAP)
124         mid = (sltop + slbot) >> 1;
125         dx = right[mid].x - left[mid].x;
126         if((tmp = right[sltop].x - left[sltop].x) > dx) {
127                 dx = tmp;
128                 mid = sltop;
129         }
130         if((tmp = right[slbot].x - left[slbot].x) > dx) {
131                 dx = tmp;
132                 mid = slbot;
133         }
134         if(!dx) {
135                 dx = 256;       /* 1 */
136         }
137 #endif
138 #ifdef GOURAUD
139         dr = right[mid].r - left[mid].r;
140         dg = right[mid].g - left[mid].g;
141         db = right[mid].b - left[mid].b;
142         rslope = (dr << 8) / dx;
143         gslope = (dg << 8) / dx;
144         bslope = (db << 8) / dx;
145 #endif
146 #ifdef TEXMAP
147         du = right[mid].u - left[mid].u;
148         dv = right[mid].v - left[mid].v;
149         uslope = (du << 8) / dx;
150         vslope = (dv << 8) / dx;
151 #endif
152 #endif  /* 0 */
153
154         for(i=sltop; i<=slbot; i++) {
155                 uint16_t *pixptr;
156                 int32_t x;
157
158                 x = left[i].x;
159                 pixptr = pfill_fb.pixels + i * pfill_fb.width + (x >> 8);
160
161 #if defined(GOURAUD) || defined(TEXMAP)
162                 if(!(dx = right[i].x - left[i].x)) dx = 256;    /* 1 */
163 #endif
164 #ifdef GOURAUD
165                 r = left[i].r;
166                 g = left[i].g;
167                 b = left[i].b;
168                 dr = right[i].r - left[i].r;
169                 dg = right[i].g - left[i].g;
170                 db = right[i].b - left[i].b;
171                 rslope = (dr << 8) / dx;
172                 gslope = (dg << 8) / dx;
173                 bslope = (db << 8) / dx;
174 #endif
175 #ifdef TEXMAP
176                 u = left[i].u;
177                 v = left[i].v;
178                 du = right[i].u - left[i].u;
179                 dv = right[i].v - left[i].v;
180                 uslope = (du << 8) / dx;
181                 vslope = (dv << 8) / dx;
182 #endif
183
184                 while(x <= right[i].x) {
185 #if defined(GOURAUD) || defined(TEXMAP)
186                         int cr, cg, cb;
187 #endif
188 #ifdef GOURAUD
189                         /* drop the extra 8 bits when packing */
190                         cr = r >> 8;
191                         cg = g >> 8;
192                         cb = b >> 8;
193                         r += rslope;
194                         g += gslope;
195                         b += bslope;
196 #endif
197 #ifdef TEXMAP
198                         {
199                                 int tx = (u >> (16 - pfill_tex.xshift)) & pfill_tex.xmask;
200                                 int ty = (v >> (16 - pfill_tex.yshift)) & pfill_tex.ymask;
201                                 uint16_t texel = pfill_tex.pixels[(ty << pfill_tex.xshift) + tx];
202 #ifdef GOURAUD
203                                 /* XXX this is not correct, should be /255, but it might not make a huge difference */
204                                 cr = (cr * UNPACK_R16(texel)) >> 8;
205                                 cg = (cg * UNPACK_G16(texel)) >> 8;
206                                 cb = (cb * UNPACK_B16(texel)) >> 8;
207 #else
208                                 cr = UNPACK_R16(texel);
209                                 cg = UNPACK_G16(texel);
210                                 cb = UNPACK_B16(texel);
211 #endif
212                         }
213                         u += uslope;
214                         v += vslope;
215 #endif
216 #if defined(GOURAUD) || defined(TEXMAP)
217                         color = PACK_RGB16(cr, cg, cb);
218 #endif
219
220 #ifdef DEBUG_OVERDRAW
221                         *pixptr++ += DEBUG_OVERDRAW;
222 #else
223                         *pixptr++ = color;
224 #endif
225                         x += 256;
226                 }
227         }
228 }
229