setting up to improve the rasterizer
[dosdemo] / src / 3dgfx.c
index 49b4c2c..41b3959 100644 (file)
@@ -15,6 +15,7 @@ typedef float g3d_matrix[16];
 struct g3d_state {
        unsigned int opt;
        int frontface;
+       int fill_mode;
 
        g3d_matrix mat[G3D_NUM_MATRICES][STACK_SIZE];
        int mtop[G3D_NUM_MATRICES];
@@ -46,6 +47,7 @@ int g3d_init(void)
                fprintf(stderr, "failed to allocate G3D context\n");
                return -1;
        }
+       st->fill_mode = POLYFILL_FLAT;
 
        for(i=0; i<G3D_NUM_MATRICES; i++) {
                g3d_matrix_mode(i);
@@ -64,6 +66,10 @@ void g3d_framebuffer(int width, int height, void *pixels)
        st->width = width;
        st->height = height;
        st->pixels = pixels;
+
+       pimg_fb.pixels = pixels;
+       pimg_fb.width = width;
+       pimg_fb.height = height;
 }
 
 void g3d_enable(unsigned int opt)
@@ -91,6 +97,11 @@ void g3d_front_face(unsigned int order)
        st->frontface = order;
 }
 
+void g3d_polygon_mode(int pmode)
+{
+       st->fill_mode = pmode;
+}
+
 void g3d_matrix_mode(int mmode)
 {
        st->mmode = mmode;
@@ -251,16 +262,25 @@ void g3d_perspective(float vfov_deg, float aspect, float znear, float zfar)
        g3d_mult_matrix(m);
 }
 
-#define CROSS(res, a, b) \
-       do { \
-               (res)[0] = (a)[1] * (b)[2] - (a)[2] * (b)[1]; \
-               (res)[1] = (a)[2] * (b)[0] - (a)[0] * (b)[2]; \
-               (res)[2] = (a)[0] * (b)[1] - (a)[1] * (b)[0]; \
-       } while(0)
+const float *g3d_get_matrix(int which, float *m)
+{
+       int top = st->mtop[which];
+
+       if(m) {
+               memcpy(m, st->mat[which][top], 16 * sizeof(float));
+       }
+       return st->mat[which][top];
+}
 
 void g3d_draw(int prim, const struct g3d_vertex *varr, int varr_size)
 {
-       int i;
+       g3d_draw_indexed(prim, varr, varr_size, 0, 0);
+}
+
+void g3d_draw_indexed(int prim, const struct g3d_vertex *varr, int varr_size,
+               const int16_t *iarr, int iarr_size)
+{
+       int i, j, nfaces;
        struct pvertex pv[4];
        struct g3d_vertex v[4];
        int vnum = prim; /* primitive vertex counts correspond to enum values */
@@ -271,11 +291,12 @@ void g3d_draw(int prim, const struct g3d_vertex *varr, int varr_size)
        memcpy(st->norm_mat, st->mat[G3D_MODELVIEW][mvtop], 16 * sizeof(float));
        st->norm_mat[12] = st->norm_mat[13] = st->norm_mat[14] = 0.0f;
 
-       while(varr_size >= vnum) {
-               varr_size -= vnum;
+       nfaces = (iarr ? iarr_size : varr_size) / vnum;
+
+       for(j=0; j<nfaces; j++) {
 
                for(i=0; i<vnum; i++) {
-                       v[i] = *varr++;
+                       v[i] = iarr ? varr[*iarr++] : *varr++;
 
                        xform4_vec3(st->mat[G3D_MODELVIEW][mvtop], &v[i].x);
                        xform3_vec3(st->norm_mat, &v[i].nx);
@@ -325,7 +346,7 @@ void g3d_draw(int prim, const struct g3d_vertex *varr, int varr_size)
                        }
                }
 
-               polyfill_flat(pv, vnum);
+               polyfill(st->fill_mode, pv, vnum);
        }
 }