- added mesh operations: apply_mesh_xform, append_mesh, indexify_mesh (untested)...
[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 << COLOR_SHIFT);
26         g = (v0->g << COLOR_SHIFT);
27         b = (v0->b << COLOR_SHIFT);
28         dr = (v1->r << COLOR_SHIFT) - r;
29         dg = (v1->g << COLOR_SHIFT) - g;
30         db = (v1->b << COLOR_SHIFT) - 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 COLOR_SHIFT 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         /* +1 to avoid crashing due to off-by-one rounding errors in the rasterization */
97         left = alloca((pfill_fb.height + 1) * sizeof *left);
98         right = alloca((pfill_fb.height + 1) * sizeof *right);
99
100         for(i=0; i<nverts; i++) {
101                 int next = NEXTIDX(i);
102                 int32_t y0 = pv[i].y;
103                 int32_t y1 = pv[next].y;
104
105                 if((y0 >> 8) == (y1 >> 8)) {
106                         /*if(y0 > y1) {*/
107                                 int i0, i1;
108                                 int idx = y0 >> 8;
109                                 if(pv[i].x < pv[next].x) {
110                                         i0 = i;
111                                         i1 = next;
112                                 } else {
113                                         i0 = next;
114                                         i1 = i;
115                                 }
116                                 left[idx].x = pv[i0].x;
117                                 right[idx].x = pv[i1].x;
118 #ifdef GOURAUD
119                                 left[idx].r = pv[i0].r << COLOR_SHIFT;
120                                 left[idx].g = pv[i0].g << COLOR_SHIFT;
121                                 left[idx].b = pv[i0].b << COLOR_SHIFT;
122                                 right[idx].r = pv[i1].r << COLOR_SHIFT;
123                                 right[idx].g = pv[i1].g << COLOR_SHIFT;
124                                 right[idx].b = pv[i1].b << COLOR_SHIFT;
125 #endif
126 #ifdef TEXMAP
127                                 left[idx].u = pv[i0].u;
128                                 left[idx].v = pv[i0].v;
129                                 right[idx].u = pv[i1].u;
130                                 right[idx].v = pv[i1].v;
131 #endif
132                                 if(idx > slbot) slbot = idx;
133                                 if(idx < sltop) sltop = idx;
134                         /*}*/
135                 } else {
136                         struct pvertex *edge = y0 > y1 ? left : right;
137                         uint32_t res = SCANEDGE(pv + i, pv + next, edge);
138                         uint32_t tmp = (res >> 16) & 0xffff;
139                         if(tmp > slbot) slbot = tmp;
140                         if((tmp = res & 0xffff) < sltop) {
141                                 sltop = tmp;
142                         }
143                 }
144         }
145
146         /* calculate the slopes of all attributes across the largest span out
147          * of the three: middle, top, or bottom.
148          */
149 #ifndef HIGH_QUALITY
150 #if defined(GOURAUD) || defined(TEXMAP)
151         mid = (sltop + slbot) >> 1;
152         dx = right[mid].x - left[mid].x;
153         if((tmp = right[sltop].x - left[sltop].x) > dx) {
154                 dx = tmp;
155                 mid = sltop;
156         }
157         if((tmp = right[slbot].x - left[slbot].x) > dx) {
158                 dx = tmp;
159                 mid = slbot;
160         }
161         if(!dx) dx = 256;       /* avoid division by zero */
162 #endif
163 #ifdef GOURAUD
164         dr = right[mid].r - left[mid].r;
165         dg = right[mid].g - left[mid].g;
166         db = right[mid].b - left[mid].b;
167         rslope = (dr << 8) / dx;
168         gslope = (dg << 8) / dx;
169         bslope = (db << 8) / dx;
170 #endif
171 #ifdef TEXMAP
172         du = right[mid].u - left[mid].u;
173         dv = right[mid].v - left[mid].v;
174         uslope = (du << 8) / dx;
175         vslope = (dv << 8) / dx;
176 #endif
177 #endif  /* !defined(HIGH_QUALITY) */
178
179         /* for each scanline ... */
180         for(i=sltop; i<=slbot; i++) {
181                 uint16_t *pixptr;
182                 int32_t x;
183
184                 x = left[i].x;
185                 pixptr = pfill_fb.pixels + i * pfill_fb.width + (x >> 8);
186
187 #ifdef GOURAUD
188                 r = left[i].r;
189                 g = left[i].g;
190                 b = left[i].b;
191 #endif
192 #ifdef TEXMAP
193                 u = left[i].u;
194                 v = left[i].v;
195 #endif
196
197 #if defined(HIGH_QUALITY) && (defined(GOURAUD) || defined(TEXMAP))
198                 if(!(dx = right[i].x - left[i].x)) dx = 256;
199
200 #ifdef GOURAUD
201                 dr = right[i].r - left[i].r;
202                 dg = right[i].g - left[i].g;
203                 db = right[i].b - left[i].b;
204                 rslope = (dr << 8) / dx;
205                 gslope = (dg << 8) / dx;
206                 bslope = (db << 8) / dx;
207 #endif
208 #ifdef TEXMAP
209                 du = right[i].u - left[i].u;
210                 dv = right[i].v - left[i].v;
211                 uslope = (du << 8) / dx;
212                 vslope = (dv << 8) / dx;
213 #endif
214 #endif  /* HIGH_QUALITY */
215
216                 /* go across the scanline interpolating if necessary */
217                 while(x <= right[i].x) {
218 #if defined(GOURAUD) || defined(TEXMAP)
219                         int cr, cg, cb;
220 #endif
221 #ifdef GOURAUD
222                         /* we upped the color precision to while interpolating the
223                          * edges, now drop the extra bits before packing
224                          */
225                         cr = r < 0 ? 0 : (r >> COLOR_SHIFT);
226                         cg = g < 0 ? 0 : (g >> COLOR_SHIFT);
227                         cb = b < 0 ? 0 : (b >> COLOR_SHIFT);
228                         if(cr > 255) cr = 255;
229                         if(cg > 255) cg = 255;
230                         if(cb > 255) cb = 255;
231                         r += rslope;
232                         g += gslope;
233                         b += bslope;
234 #endif
235 #ifdef TEXMAP
236                         {
237                                 int tx = (u >> (16 - pfill_tex.xshift)) & pfill_tex.xmask;
238                                 int ty = (v >> (16 - pfill_tex.yshift)) & pfill_tex.ymask;
239                                 uint16_t texel = pfill_tex.pixels[(ty << pfill_tex.xshift) + tx];
240 #ifdef GOURAUD
241                                 /* This is not correct, should be /255, but it's much faster
242                                  * to shift by 8 (/256), and won't make a huge difference
243                                  */
244                                 cr = (cr * UNPACK_R16(texel)) >> 8;
245                                 cg = (cg * UNPACK_G16(texel)) >> 8;
246                                 cb = (cb * UNPACK_B16(texel)) >> 8;
247 #else
248                                 cr = UNPACK_R16(texel);
249                                 cg = UNPACK_G16(texel);
250                                 cb = UNPACK_B16(texel);
251 #endif
252                         }
253                         u += uslope;
254                         v += vslope;
255 #endif
256 #if defined(GOURAUD) || defined(TEXMAP)
257                         color = PACK_RGB16(cr, cg, cb);
258 #endif
259
260 #ifdef DEBUG_OVERDRAW
261                         *pixptr++ += DEBUG_OVERDRAW;
262 #else
263                         *pixptr++ = color;
264 #endif
265                         x += 256;
266                 }
267         }
268 }
269