backported g3d changes:
[dosdemo] / src / polyfill.c
index 7bffaa3..3f94e25 100644 (file)
@@ -2,18 +2,40 @@
 #include <stdlib.h>
 #include <string.h>
 #include <assert.h>
+#if defined(__WATCOMC__) || defined(_MSC_VER) || defined(__DJGPP__)
+#include <malloc.h>
+#else
 #include <alloca.h>
+#endif
 #include "polyfill.h"
 #include "gfxutil.h"
-#include "demo.h"
 
+#define FILL_POLY_BITS 0x03
+
+/* mode bits: 00-wire 01-flat 10-gouraud 11-reserved
+ *     bit 2: texture
+ *     bit 3: blend
+ */
 void (*fillfunc[])(struct pvertex*, int) = {
        polyfill_wire,
        polyfill_flat,
-       0, 0, 0
+       polyfill_gouraud,
+       0,
+       polyfill_tex_wire,
+       polyfill_tex_flat,
+       polyfill_tex_gouraud,
+       0,
+       polyfill_blend_wire,
+       polyfill_blend_flat,
+       polyfill_blend_gouraud,
+       0,
+       polyfill_blend_tex_wire,
+       polyfill_blend_tex_flat,
+       polyfill_blend_tex_gouraud,
+       0
 };
 
-struct pimage pimg_fb, pimg_texture;
+struct pimage pfill_fb, pfill_tex;
 
 void polyfill(int mode, struct pvertex *verts, int nverts)
 {
@@ -40,95 +62,118 @@ void polyfill_wire(struct pvertex *verts, int nverts)
                ++v;
                x1 = v->x >> 8;
                y1 = v->y >> 8;
-               if(clip_line(&x0, &y0, &x1, &y1, 0, 0, pimg_fb.width, pimg_fb.height)) {
+               if(clip_line(&x0, &y0, &x1, &y1, 0, 0, pfill_fb.width, pfill_fb.height)) {
                        draw_line(x0, y0, x1, y1, color);
                }
        }
        x0 = verts[0].x >> 8;
        y0 = verts[0].y >> 8;
-       if(clip_line(&x1, &y1, &x0, &y0, 0, 0, pimg_fb.width, pimg_fb.height)) {
+       if(clip_line(&x1, &y1, &x0, &y0, 0, 0, pfill_fb.width, pfill_fb.height)) {
                draw_line(x1, y1, x0, y0, color);
        }
 }
 
-static void scan_edge(struct pvertex *v0, struct pvertex *v1, struct pvertex *edge);
-
-#define NEXTIDX(x) (((x) - 1 + nverts) % nverts)
-#define PREVIDX(x) (((x) + 1) % nverts)
-
-void polyfill_flat(struct pvertex *pv, int nverts)
+void polyfill_tex_wire(struct pvertex *verts, int nverts)
 {
-       int i;
-       int32_t y;
-       int topidx = 0, botidx = 0, sline;
-       struct pvertex *left, *right;
-       /*uint16_t color = PACK_RGB16(pv[0].r, pv[0].g, pv[0].b);*/
-
-       for(i=1; i<nverts; i++) {
-               if(pv[i].y < pv[topidx].y) topidx = i;
-               if(pv[i].y > pv[botidx].y) botidx = i;
-       }
-
-       left = (struct pvertex*)alloca(pimg_fb.height * sizeof *left);
-       right = (struct pvertex*)alloca(pimg_fb.height * sizeof *right);
-       memset(left, 0, pimg_fb.height * sizeof *left);
-       memset(right, 0, pimg_fb.height * sizeof *right);
-
-       for(i=0; i<nverts; i++) {
-               int next = NEXTIDX(i);
-               int32_t y0 = pv[i].y;
-               int32_t y1 = pv[next].y;
-
-               if((y0 >> 8) == (y1 >> 8)) {
-                       if(y0 > y1) {
-                               int idx = y0 >> 8;
-                               left[idx].x = pv[i].x < pv[next].x ? pv[i].x : pv[next].x;
-                               right[idx].x = pv[i].x < pv[next].x ? pv[next].x : pv[i].x;
-                       }
-               } else {
-                       scan_edge(pv + i, pv + next, y0 > y1 ? left : right);
-               }
-       }
-
-       y = pv[topidx].y;
-       while(y < pv[botidx].y) {
-               int32_t x;
-               uint16_t *pixptr;
-
-               sline = y >> 8;
-               x = left[sline].x;
-
-               pixptr = pimg_fb.pixels + sline * pimg_fb.width + (x >> 8);
+       polyfill_wire(verts, nverts);   /* TODO */
+}
 
-               while(x <= right[sline].x) {
-                       *pixptr++ += 15;
-                       x += 256;
-               }
-               y += 256;
-       }
+void polyfill_blend_wire(struct pvertex *verts, int nverts)
+{
+       polyfill_wire(verts, nverts);   /* TODO */
 }
 
-static void scan_edge(struct pvertex *v0, struct pvertex *v1, struct pvertex *edge)
+void polyfill_blend_tex_wire(struct pvertex *verts, int nverts)
 {
-       int32_t x, y, dx, dy, slope;
-       int idx;
+       polyfill_wire(verts, nverts);   /* TODO */
+}
 
-       if(v0->y > v1->y) {
-               struct pvertex *tmp = v0;
-               v0 = v1;
-               v1 = tmp;
-       }
+#define NEXTIDX(x) (((x) - 1 + nverts) % nverts)
+#define PREVIDX(x) (((x) + 1) % nverts)
 
-       dy = v1->y - v0->y;
-       dx = v1->x - v0->x;
-       slope = (dx << 8) / dy;
-       idx = v0->y >> 8;
-
-       x = v0->x;
-       y = v0->y;
-       while(y <= v1->y) {
-               edge[idx++].x = x;
-               x += slope;
-               y += 256;
-       }
-}
+/* XXX
+ * When HIGH_QUALITY is defined, the rasterizer calculates slopes for attribute
+ * interpolation on each scanline separately; otherwise the slope for each
+ * attribute would be calculated once for the whole polygon, which is faster,
+ * but produces some slight quantization artifacts, due to the limited precision
+ * of fixed-point calculations.
+ */
+#define HIGH_QUALITY
+
+/* extra bits of precision to use when interpolating colors.
+ * try tweaking this if you notice strange quantization artifacts.
+ */
+#define COLOR_SHIFT    12
+
+
+#define POLYFILL polyfill_flat
+#define SCANEDGE scanedge_flat
+#undef GOURAUD
+#undef TEXMAP
+#undef BLEND
+#include "polytmpl.h"
+#undef POLYFILL
+#undef SCANEDGE
+
+#define POLYFILL polyfill_gouraud
+#define SCANEDGE scanedge_gouraud
+#define GOURAUD
+#undef TEXMAP
+#undef BLEND
+#include "polytmpl.h"
+#undef POLYFILL
+#undef SCANEDGE
+
+#define POLYFILL polyfill_tex_flat
+#define SCANEDGE scanedge_tex_flat
+#undef GOURAUD
+#define TEXMAP
+#undef BLEND
+#include "polytmpl.h"
+#undef POLYFILL
+#undef SCANEDGE
+
+#define POLYFILL polyfill_tex_gouraud
+#define SCANEDGE scanedge_tex_gouraud
+#define GOURAUD
+#define TEXMAP
+#undef BLEND
+#include "polytmpl.h"
+#undef POLYFILL
+#undef SCANEDGE
+
+#define POLYFILL polyfill_blend_flat
+#define SCANEDGE scanedge_blend_flat
+#undef GOURAUD
+#undef TEXMAP
+#define BLEND
+#include "polytmpl.h"
+#undef POLYFILL
+#undef SCANEDGE
+
+#define POLYFILL polyfill_blend_gouraud
+#define SCANEDGE scanedge_blend_gouraud
+#define GOURAUD
+#undef TEXMAP
+#define BLEND
+#include "polytmpl.h"
+#undef POLYFILL
+#undef SCANEDGE
+
+#define POLYFILL polyfill_blend_tex_flat
+#define SCANEDGE scanedge_blend_tex_flat
+#undef GOURAUD
+#define TEXMAP
+#define BLEND
+#include "polytmpl.h"
+#undef POLYFILL
+#undef SCANEDGE
+
+#define POLYFILL polyfill_blend_tex_gouraud
+#define SCANEDGE scanedge_blend_tex_gouraud
+#define GOURAUD
+#define TEXMAP
+#define BLEND
+#include "polytmpl.h"
+#undef POLYFILL
+#undef SCANEDGE