backported g3d changes:
[dosdemo] / src / polyfill.c
index 79d1d69..3f94e25 100644 (file)
@@ -1,16 +1,41 @@
 #include <stdio.h>
 #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)
 {
@@ -37,93 +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);
        }
 }
 
-#define NEXTIDX(x) (((x) - 1 + nverts) % nverts)
-#define PREVIDX(x) (((x) + 1) % nverts)
-
-#define CALC_EDGE(which) \
-       do { \
-               which##_x = pv[which##_beg].x; \
-               which##_dx = pv[which##_end].x - pv[which##_beg].x; \
-               which##_slope = (which##_dx << 8) / which##_dy; \
-       } while(0)
-
-void polyfill_flat(struct pvertex *pv, int nverts)
+void polyfill_tex_wire(struct pvertex *verts, int nverts)
 {
-       int i, sline, x, slen, top = 0;
-       int left_beg, left_end, right_beg, right_end;
-       int32_t left_dy, left_dx, right_dy, right_dx;
-       int32_t left_slope, right_slope;
-       int32_t left_x, right_x, y;
-       uint16_t color = ((pv->r << 8) & 0xf800) | ((pv->g << 3) & 0x7e0) |
-               ((pv->b >> 3) & 0x1f);
-       uint16_t *pixptr;
-
-       /* find topmost */
-       for(i=1; i<nverts; i++) {
-               if(pv[i].y < pv[top].y) {
-                       top = i;
-               }
-       }
-       left_beg = right_beg = top;
-       left_end = PREVIDX(left_beg);
-       right_end = NEXTIDX(right_beg);
-
-       if((left_dy = pv[left_end].y - pv[left_beg].y)) {
-               CALC_EDGE(left);
-       }
-
-       if((right_dy = pv[right_end].y - pv[right_beg].y)) {
-               CALC_EDGE(right);
-       }
-
-       y = pv[top].y;
-       sline = pv[top].y >> 8;
-
-       for(;;) {
-               if(y >= pv[left_end].y) {
-                       while(y >= pv[left_end].y) {
-                               left_beg = left_end;
-                               if(left_beg == right_beg) return;
-                               left_end = PREVIDX(left_end);
-                       }
-
-                       left_dy = pv[left_end].y - pv[left_beg].y;
-                       CALC_EDGE(left);
-               }
-
-               if(y >= pv[right_end].y) {
-                       while(y >= pv[right_end].y) {
-                               right_beg = right_end;
-                               if(left_beg == right_beg) return;
-                               right_end = NEXTIDX(right_end);
-                       }
+       polyfill_wire(verts, nverts);   /* TODO */
+}
 
-                       right_dy = pv[right_end].y - pv[right_beg].y;
-                       CALC_EDGE(right);
-               }
+void polyfill_blend_wire(struct pvertex *verts, int nverts)
+{
+       polyfill_wire(verts, nverts);   /* TODO */
+}
 
-               x = left_x >> 8;
-               slen = (right_x >> 8) - (left_x >> 8);
+void polyfill_blend_tex_wire(struct pvertex *verts, int nverts)
+{
+       polyfill_wire(verts, nverts);   /* TODO */
+}
 
-               pixptr = pimg_fb.pixels + sline * pimg_fb.width + x;
-               for(i=0; i<slen; i++) {
-                       *pixptr++ += 15;
-               }
+#define NEXTIDX(x) (((x) - 1 + nverts) % nverts)
+#define PREVIDX(x) (((x) + 1) % nverts)
 
-               ++sline;
-               y += 256;
-               left_x += left_slope;
-               right_x += right_slope;
-       }
-}
+/* 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