zbuffer partial implementation
[dosdemo] / src / 3dgfx / 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 #ifdef BLEND_ALPHA
9         int32_t a, da, aslope;
10 #endif
11 #endif  /* GOURAUD */
12 #ifdef TEXMAP
13         int32_t u, v, du, dv, uslope, vslope;
14 #endif
15 #ifdef ZBUF
16         int z, dz, zslope;
17 #endif
18         int32_t start_idx, end_idx;
19
20         if(v0->y > v1->y) {
21                 struct pvertex *tmp = v0;
22                 v0 = v1;
23                 v1 = tmp;
24         }
25
26         x = v0->x;
27         dy = v1->y - v0->y;
28         dx = v1->x - v0->x;
29         slope = (dx << 8) / dy;
30 #ifdef GOURAUD
31         r = (v0->r << COLOR_SHIFT);
32         g = (v0->g << COLOR_SHIFT);
33         b = (v0->b << COLOR_SHIFT);
34         dr = (v1->r << COLOR_SHIFT) - r;
35         dg = (v1->g << COLOR_SHIFT) - g;
36         db = (v1->b << COLOR_SHIFT) - b;
37         rslope = (dr << 8) / dy;
38         gslope = (dg << 8) / dy;
39         bslope = (db << 8) / dy;
40 #ifdef BLEND_ALPHA
41         a = (v0->a << COLOR_SHIFT);
42         da = (v1->a << COLOR_SHIFT) - a;
43         aslope = (da << 8) / dy;
44 #endif  /* BLEND_ALPHA */
45 #endif  /* GOURAUD */
46 #ifdef TEXMAP
47         u = v0->u;
48         v = v0->v;
49         du = v1->u - v0->u;
50         dv = v1->v - v0->v;
51         uslope = (du << 8) / dy;
52         vslope = (dv << 8) / dy;
53 #endif
54 #ifdef ZBUF
55         z = v0->z;
56         dz = v1->z - v0->z;
57         zslope = (dz << 8) / dy;
58 #endif
59
60         start_idx = v0->y >> 8;
61         end_idx = v1->y >> 8;
62
63         for(i=start_idx; i<end_idx; i++) {
64                 CHECKEDGE(i);
65                 edge[i].x = x;
66                 x += slope;
67 #ifdef GOURAUD
68                 /* we'll store the color in the edge tables with COLOR_SHIFT extra bits of precision */
69                 edge[i].r = r;
70                 edge[i].g = g;
71                 edge[i].b = b;
72                 r += rslope;
73                 g += gslope;
74                 b += bslope;
75 #ifdef BLEND_ALPHA
76                 edge[i].a = a;
77                 a += aslope;
78 #endif
79 #endif  /* GOURAUD */
80 #ifdef TEXMAP
81                 edge[i].u = u;
82                 edge[i].v = v;
83                 u += uslope;
84                 v += vslope;
85 #endif
86 #ifdef ZBUF
87                 edge[i].z = z;
88                 z += zslope;
89 #endif
90         }
91
92         return (uint32_t)start_idx | ((uint32_t)(end_idx - 1) << 16);
93 }
94
95 void POLYFILL(struct pvertex *pv, int nverts)
96 {
97         int i, winding;
98         int topidx = 0, botidx = 0, sltop = pfill_fb.height, slbot = 0;
99         g3d_pixel color;
100         /* the following variables are used for interpolating horizontally accros scanlines */
101 #if defined(GOURAUD) || defined(TEXMAP) || defined(ZBUF)
102         int mid;
103         int32_t dx, tmp;
104 #else
105         /* flat version, just pack the color now */
106         color = G3D_PACK_RGB(pv[0].r, pv[0].g, pv[0].b);
107 #endif
108 #ifdef GOURAUD
109         int32_t r, g, b, dr, dg, db, rslope, gslope, bslope;
110 #ifdef BLEND_ALPHA
111         int32_t a, da, aslope;
112 #endif
113 #endif
114 #ifdef TEXMAP
115         int32_t u, v, du, dv, uslope, vslope;
116 #endif
117 #ifdef ZBUF
118         int z, dz, zslope;
119 #endif
120
121         for(i=1; i<nverts; i++) {
122                 if(pv[i].y < pv[topidx].y) topidx = i;
123                 if(pv[i].y > pv[botidx].y) botidx = i;
124         }
125
126         winding = 0;
127         for(i=0; i<nverts; i++) {
128                 int next = NEXTIDX(i);
129                 winding += ((pv[next].x - pv[i].x) >> 4) * ((pv[next].y + pv[i].y) >> 4);
130         }
131
132         for(i=0; i<nverts; i++) {
133                 int next = NEXTIDX(i);
134                 int32_t y0 = pv[i].y;
135                 int32_t y1 = pv[next].y;
136
137                 if((y0 >> 8) == (y1 >> 8)) {
138                         /*if(y0 > y1) {*/
139                                 int i0, i1;
140                                 int idx = y0 >> 8;
141                                 if(pv[i].x < pv[next].x) {
142                                         i0 = i;
143                                         i1 = next;
144                                 } else {
145                                         i0 = next;
146                                         i1 = i;
147                                 }
148                                 CHECKEDGE(idx);
149                                 left[idx].x = pv[i0].x;
150                                 right[idx].x = pv[i1].x;
151 #ifdef GOURAUD
152                                 left[idx].r = pv[i0].r << COLOR_SHIFT;
153                                 left[idx].g = pv[i0].g << COLOR_SHIFT;
154                                 left[idx].b = pv[i0].b << COLOR_SHIFT;
155                                 right[idx].r = pv[i1].r << COLOR_SHIFT;
156                                 right[idx].g = pv[i1].g << COLOR_SHIFT;
157                                 right[idx].b = pv[i1].b << COLOR_SHIFT;
158 #ifdef BLEND_ALPHA
159                                 left[idx].a = pv[i0].a << COLOR_SHIFT;
160                                 right[idx].a = pv[i1].a << COLOR_SHIFT;
161 #endif  /* BLEND_ALPHA */
162 #endif
163 #ifdef TEXMAP
164                                 left[idx].u = pv[i0].u;
165                                 left[idx].v = pv[i0].v;
166                                 right[idx].u = pv[i1].u;
167                                 right[idx].v = pv[i1].v;
168 #endif
169 #ifdef ZBUF
170                                 left[idx].z = pv[i0].z;
171                                 right[idx].z = pv[i1].z;
172 #endif
173                                 CHECKEDGE(idx);
174                                 if(idx > slbot) slbot = idx;
175                                 if(idx < sltop) sltop = idx;
176                         /*}*/
177                 } else {
178                         struct pvertex *edge;
179                         uint32_t res, tmp;
180
181                         if(winding < 0) {
182                                 /* clockwise */
183                                 edge = y0 > y1 ? left : right;
184                         } else {
185                                 /* counter-clockwise */
186                                 edge = y0 > y1 ? right : left;
187                         }
188                         res = SCANEDGE(pv + i, pv + next, edge);
189                         tmp = (res >> 16) & 0xffff;
190                         if(tmp > slbot) slbot = tmp;
191                         if((tmp = res & 0xffff) < sltop) {
192                                 sltop = tmp;
193                         }
194                 }
195         }
196
197         /* calculate the slopes of all attributes across the largest span out
198          * of the three: middle, top, or bottom.
199          */
200 #ifndef HIGH_QUALITY
201 #if defined(GOURAUD) || defined(TEXMAP)
202         mid = (sltop + slbot) >> 1;
203         CHECKEDGE(sltop);
204         CHECKEDGE(slbot);
205         CHECKEDGE(mid);
206         dx = right[mid].x - left[mid].x;
207         if((tmp = right[sltop].x - left[sltop].x) > dx) {
208                 dx = tmp;
209                 mid = sltop;
210         }
211         if((tmp = right[slbot].x - left[slbot].x) > dx) {
212                 dx = tmp;
213                 mid = slbot;
214         }
215         if(!dx) dx = 256;       /* avoid division by zero */
216 #endif
217         CHECKEDGE(idx);
218 #ifdef GOURAUD
219         dr = right[mid].r - left[mid].r;
220         dg = right[mid].g - left[mid].g;
221         db = right[mid].b - left[mid].b;
222         rslope = (dr << 8) / dx;
223         gslope = (dg << 8) / dx;
224         bslope = (db << 8) / dx;
225 #ifdef BLEND_ALPHA
226         da = right[mid].a - left[mid].a;
227         aslope = (da << 8) / dx;
228 #endif  /* BLEND_ALPHA */
229 #endif
230 #ifdef TEXMAP
231         du = right[mid].u - left[mid].u;
232         dv = right[mid].v - left[mid].v;
233         uslope = (du << 8) / dx;
234         vslope = (dv << 8) / dx;
235 #endif
236 #ifdef ZBUF
237         dz = right[mid].z - left[mid].z;
238         zslope = (dz << 8) / dx;
239 #endif
240 #endif  /* !defined(HIGH_QUALITY) */
241
242         /* for each scanline ... */
243         for(i=sltop; i<=slbot; i++) {
244                 g3d_pixel *pixptr;
245                 int32_t x;
246 #ifdef ZBUF
247                 uint16_t *zptr;
248 #endif
249
250                 CHECKEDGE(i);
251                 x = left[i].x;
252                 pixptr = pfill_fb.pixels + i * pfill_fb.width + (x >> 8);
253
254 #ifdef GOURAUD
255                 r = left[i].r;
256                 g = left[i].g;
257                 b = left[i].b;
258 #ifdef BLEND_ALPHA
259                 a = left[i].a;
260 #endif  /* BLEND_ALPHA */
261 #endif
262 #ifdef TEXMAP
263                 u = left[i].u;
264                 v = left[i].v;
265 #endif
266 #ifdef ZBUF
267                 z = left[i].z;
268                 zptr = pfill_zbuf + i * pfill_fb.width + (x >> 8);
269 #endif
270                 CHECKEDGE(i);
271
272 #if defined(HIGH_QUALITY) && (defined(GOURAUD) || defined(TEXMAP) || defined(ZBUF))
273                 if(!(dx = right[i].x - left[i].x)) dx = 256;
274
275                 CHECKEDGE(i);
276 #ifdef GOURAUD
277                 dr = right[i].r - left[i].r;
278                 dg = right[i].g - left[i].g;
279                 db = right[i].b - left[i].b;
280                 rslope = (dr << 8) / dx;
281                 gslope = (dg << 8) / dx;
282                 bslope = (db << 8) / dx;
283 #ifdef BLEND_ALPHA
284                 da = right[i].a - left[i].a;
285                 aslope = (da << 8) / dx;
286 #endif  /* BLEND_ALPHA */
287 #endif  /* GOURAUD */
288 #ifdef TEXMAP
289                 du = right[i].u - left[i].u;
290                 dv = right[i].v - left[i].v;
291                 uslope = (du << 8) / dx;
292                 vslope = (dv << 8) / dx;
293 #endif
294 #ifdef ZBUF
295                 dz = right[i].z - left[i].z;
296                 zslope = (dz << 8) / dx;
297 #endif
298 #endif  /* HIGH_QUALITY */
299                 CHECKEDGE(i);
300
301                 /* go across the scanline interpolating if necessary */
302                 while(x <= right[i].x) {
303 #if defined(GOURAUD) || defined(TEXMAP) || defined(BLEND_ALPHA) || defined(BLEND_ADD)
304                         int cr, cg, cb;
305 #endif
306 #if defined(BLEND_ALPHA) || defined(BLEND_ADD)
307                         g3d_pixel fbcol;
308 #endif
309 #ifdef BLEND_ALPHA
310                         int alpha, inv_alpha;
311 #endif
312
313 #ifdef ZBUF
314                         int cz = z;
315                         z += zslope;
316
317                         if(z <= *zptr) {
318                                 *zptr = z;
319                         } else {
320 #ifdef GOURAUD
321                                 r += rslope;
322                                 g += gslope;
323                                 b += bslope;
324 #ifdef BLEND_ALPHA
325                                 a += aslope;
326 #endif
327 #endif
328 #ifdef TEXMAP
329                                 u += uslope;
330                                 v += vslope;
331 #endif
332                                 goto skip_pixel;
333                         }
334                         zptr++;
335 #endif
336
337 #ifdef GOURAUD
338                         /* we upped the color precision to while interpolating the
339                          * edges, now drop the extra bits before packing
340                          */
341                         cr = r < 0 ? 0 : (r >> COLOR_SHIFT);
342                         cg = g < 0 ? 0 : (g >> COLOR_SHIFT);
343                         cb = b < 0 ? 0 : (b >> COLOR_SHIFT);
344                         r += rslope;
345                         g += gslope;
346                         b += bslope;
347 #ifdef BLEND_ALPHA
348                         a += aslope;
349 #else
350                         if(cr > 255) cr = 255;
351                         if(cg > 255) cg = 255;
352                         if(cb > 255) cb = 255;
353 #endif  /* BLEND_ALPHA */
354 #endif  /* GOURAUD */
355 #ifdef TEXMAP
356                         {
357                                 int tx = (u >> (16 - pfill_tex.xshift)) & pfill_tex.xmask;
358                                 int ty = (v >> (16 - pfill_tex.yshift)) & pfill_tex.ymask;
359                                 g3d_pixel texel = pfill_tex.pixels[(ty << pfill_tex.xshift) + tx];
360 #ifdef GOURAUD
361                                 /* This is not correct, should be /255, but it's much faster
362                                  * to shift by 8 (/256), and won't make a huge difference
363                                  */
364                                 cr = (cr * G3D_UNPACK_R(texel)) >> 8;
365                                 cg = (cg * G3D_UNPACK_G(texel)) >> 8;
366                                 cb = (cb * G3D_UNPACK_B(texel)) >> 8;
367 #else
368                                 cr = G3D_UNPACK_R(texel);
369                                 cg = G3D_UNPACK_G(texel);
370                                 cb = G3D_UNPACK_B(texel);
371 #endif
372                         }
373                         u += uslope;
374                         v += vslope;
375 #endif
376
377 #if defined(BLEND_ALPHA) || defined(BLEND_ADD)
378 #if !defined(GOURAUD) && !defined(TEXMAP)
379                         /* flat version: cr,cg,cb are uninitialized so far */
380                         cr = pv[0].r;
381                         cg = pv[0].g;
382                         cb = pv[0].b;
383 #endif
384                         fbcol = *pixptr;
385
386 #ifdef BLEND_ALPHA
387 #ifdef GOURAUD
388                         alpha = a >> COLOR_SHIFT;
389 #else
390                         alpha = pv[0].a;
391 #endif
392                         inv_alpha = 255 - alpha;
393                         cr = (cr * alpha + G3D_UNPACK_R(fbcol) * inv_alpha) >> 8;
394                         cg = (cg * alpha + G3D_UNPACK_G(fbcol) * inv_alpha) >> 8;
395                         cb = (cb * alpha + G3D_UNPACK_B(fbcol) * inv_alpha) >> 8;
396 #else   /* !BLEND_ALPHA (so BLEND_ADD) */
397                         cr += G3D_UNPACK_R(fbcol);
398                         cg += G3D_UNPACK_R(fbcol);
399                         cb += G3D_UNPACK_R(fbcol);
400 #endif
401                         if(cr > 255) cr = 255;
402                         if(cg > 255) cg = 255;
403                         if(cb > 255) cb = 255;
404 #endif  /* BLEND(ALPHA|ADD) */
405
406 #if defined(GOURAUD) || defined(TEXMAP) || defined(BLEND_ALPHA) || defined(BLEND_ADD)
407                         color = G3D_PACK_RGB(cr, cg, cb);
408 #endif
409
410 #ifdef ZBUF
411 skip_pixel:
412 #endif
413 #ifdef DEBUG_OVERDRAW
414                         *pixptr++ += DEBUG_OVERDRAW;
415 #else
416                         *pixptr++ = color;
417 #endif
418                         x += 256;
419                 }
420         }
421 }
422