setting up to improve the rasterizer
authorJohn Tsiombikas <nuclear@mutantstargoat.com>
Sun, 2 Oct 2016 09:05:41 +0000 (12:05 +0300)
committerJohn Tsiombikas <nuclear@mutantstargoat.com>
Sun, 2 Oct 2016 09:05:41 +0000 (12:05 +0300)
src/3dgfx.c
src/3dgfx.h
src/gfxutil.h
src/polyfill.c
src/polyfill.h
src/polytest.c
src/tunnel.c

index 31e649d..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;
@@ -335,7 +346,7 @@ void g3d_draw_indexed(int prim, const struct g3d_vertex *varr, int varr_size,
                        }
                }
 
-               polyfill_flat(pv, vnum);
+               polyfill(st->fill_mode, pv, vnum);
        }
 }
 
index c80ded1..4a3cde5 100644 (file)
@@ -30,6 +30,12 @@ enum {
 /* arg to g3d_front_face */
 enum { G3D_CCW, G3D_CW };
 
+/* arg to g3d_polygon_mode */
+enum {
+       G3D_WIRE,
+       G3D_FLAT
+};
+
 /* matrix stacks */
 enum {
        G3D_MODELVIEW,
@@ -49,6 +55,7 @@ void g3d_setopt(unsigned int opt, unsigned int mask);
 unsigned int g3d_getopt(unsigned int mask);
 
 void g3d_front_face(unsigned int order);
+void g3d_polygon_mode(int pmode);
 
 void g3d_matrix_mode(int mmode);
 
index 8de5607..f811987 100644 (file)
@@ -1,6 +1,12 @@
 #ifndef GFXUTIL_H_
 #define GFXUTIL_H_
 
+#define PACK_RGB16(r, g, b) \
+       (((((r) >> 3) & 0x1f) << 11) | ((((g) >> 2) & 0x3f) << 5) | (((b) >> 3) & 0x1f))
+
+#define PACK_RGB32(r, g, b) \
+       ((((r) & 0xff) << 16) | (((g) & 0xff) << 8) | ((b) & 0xff))
+
 int clip_line(int *x0, int *y0, int *x1, int *y1, int xmin, int ymin, int xmax, int ymax);
 void draw_line(int x0, int y0, int x1, int y1, unsigned short color);
 
index 3608b33..79d1d69 100644 (file)
@@ -10,6 +10,8 @@ void (*fillfunc[])(struct pvertex*, int) = {
        0, 0, 0
 };
 
+struct pimage pimg_fb, pimg_texture;
+
 void polyfill(int mode, struct pvertex *verts, int nverts)
 {
 #ifndef NDEBUG
@@ -35,13 +37,13 @@ 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, fb_width, fb_height)) {
+               if(clip_line(&x0, &y0, &x1, &y1, 0, 0, pimg_fb.width, pimg_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, fb_width, fb_height)) {
+       if(clip_line(&x1, &y1, &x0, &y0, 0, 0, pimg_fb.width, pimg_fb.height)) {
                draw_line(x1, y1, x0, y0, color);
        }
 }
@@ -114,9 +116,9 @@ void polyfill_flat(struct pvertex *pv, int nverts)
                x = left_x >> 8;
                slen = (right_x >> 8) - (left_x >> 8);
 
-               pixptr = (uint16_t*)fb_pixels + sline * fb_width + x;
+               pixptr = pimg_fb.pixels + sline * pimg_fb.width + x;
                for(i=0; i<slen; i++) {
-                       *pixptr++ += 10;
+                       *pixptr++ += 15;
                }
 
                ++sline;
index 7c12732..b39574a 100644 (file)
@@ -18,6 +18,14 @@ struct pvertex {
        unsigned char r, g, b;
 };
 
+struct pimage {
+       uint16_t *pixels;
+       int width, height;
+};
+
+extern struct pimage pimg_fb;
+extern struct pimage pimg_texture;
+
 void polyfill(int mode, struct pvertex *verts, int nverts);
 void polyfill_wire(struct pvertex *verts, int nverts);
 void polyfill_flat(struct pvertex *verts, int nverts);
index f1835d4..b9ad891 100644 (file)
@@ -5,6 +5,7 @@
 #include "screen.h"
 #include "demo.h"
 #include "3dgfx.h"
+#include "gfxutil.h"
 
 struct mesh {
        int prim;
@@ -21,6 +22,7 @@ static void draw_mesh(struct mesh *mesh);
 static int gen_cube(struct mesh *mesh, float sz);
 static int gen_torus(struct mesh *mesh, float rad, float ringrad, int usub, int vsub);
 static void zsort(struct mesh *m);
+static void draw_lowres_raster(void);
 
 static struct screen scr = {
        "polytest",
@@ -30,8 +32,13 @@ static struct screen scr = {
        draw
 };
 
+static float theta, phi = 25;
 static struct mesh cube, torus;
 
+#define LOWRES_SCALE   16
+static uint16_t *lowres_pixels;
+static int lowres_width, lowres_height;
+
 struct screen *polytest_screen(void)
 {
        return &scr;
@@ -41,11 +48,17 @@ static int init(void)
 {
        gen_cube(&cube, 1.0);
        gen_torus(&torus, 1.0, 0.25, 24, 12);
+
+       lowres_width = fb_width / LOWRES_SCALE;
+       lowres_height = fb_height / LOWRES_SCALE;
+       lowres_pixels = malloc(lowres_width * lowres_height * 2);
+
        return 0;
 }
 
 static void destroy(void)
 {
+       free(lowres_pixels);
        free(cube.varr);
        free(torus.varr);
        free(torus.iarr);
@@ -60,27 +73,35 @@ static void start(long trans_time)
        g3d_enable(G3D_CULL_FACE);
 }
 
-static void draw(void)
+static void update(void)
 {
        static int prev_mx, prev_my;
-       static float theta, phi = 25;
+       static unsigned int prev_bmask;
 
-       int dx = mouse_x - prev_mx;
-       int dy = mouse_y - prev_my;
-       prev_mx = mouse_x;
-       prev_my = mouse_y;
+       if(mouse_bmask) {
+               if(mouse_bmask ^ prev_bmask == 0) {
+                       int dx = mouse_x - prev_mx;
+                       int dy = mouse_y - prev_my;
 
-       if(dx || dy) {
-               theta += dx * 2.0;
-               phi += dy * 2.0;
+                       if(dx || dy) {
+                               theta += dx * 2.0;
+                               phi += dy * 2.0;
 
-               if(phi < -90) phi = -90;
-               if(phi > 90) phi = 90;
+                               if(phi < -90) phi = -90;
+                               if(phi > 90) phi = 90;
+                       }
+               }
        }
+       prev_mx = mouse_x;
+       prev_my = mouse_y;
+       prev_bmask = mouse_bmask;
+}
 
-       /*float angle = (float)time_msec / 50.0;*/
+static void draw(void)
+{
+       update();
 
-       memset(fb_pixels, 0, fb_width * fb_height * 2);
+       memset(lowres_pixels, 0, lowres_width * lowres_height * 2);
 
        g3d_matrix_mode(G3D_MODELVIEW);
        g3d_load_identity();
@@ -88,9 +109,19 @@ static void draw(void)
        g3d_rotate(phi, 1, 0, 0);
        g3d_rotate(theta, 0, 1, 0);
 
+       g3d_framebuffer(lowres_width, lowres_height, lowres_pixels);
        /*zsort(&torus);*/
        draw_mesh(&cube);
 
+       draw_lowres_raster();
+
+
+       g3d_framebuffer(fb_width, fb_height, fb_pixels);
+
+       g3d_polygon_mode(G3D_WIRE);
+       draw_mesh(&cube);
+       g3d_polygon_mode(G3D_FLAT);
+
        swap_buffers(fb_pixels);
 }
 
@@ -288,3 +319,31 @@ static void zsort(struct mesh *m)
 
        qsort(m->iarr, nfaces, m->prim * sizeof *m->iarr, zsort_cmp);
 }
+
+static void draw_huge_pixel(uint16_t *dest, int dest_width, uint16_t color)
+{
+       int i, j;
+       uint16_t grid_color = PACK_RGB16(127, 127, 127);
+
+       for(i=0; i<LOWRES_SCALE; i++) {
+               for(j=0; j<LOWRES_SCALE; j++) {
+                       dest[j] = i == 0 || j == 0 ? grid_color : color;
+               }
+               dest += dest_width;
+       }
+}
+
+static void draw_lowres_raster(void)
+{
+       int i, j;
+       uint16_t *sptr = lowres_pixels;
+       uint16_t *dptr = fb_pixels;
+
+       for(i=0; i<lowres_height; i++) {
+               for(j=0; j<lowres_width; j++) {
+                       draw_huge_pixel(dptr, fb_width, *sptr++);
+                       dptr += LOWRES_SCALE;
+               }
+               dptr += fb_width * LOWRES_SCALE - fb_width;
+       }
+}
index 63f0f9f..d10c691 100644 (file)
@@ -6,6 +6,7 @@
 #include "imago2.h"
 #include "demo.h"
 #include "screen.h"
+#include "gfxutil.h"
 
 #ifndef M_PI
 #define M_PI   3.1415926535
@@ -213,11 +214,6 @@ static void tunnel_color(int *rp, int *gp, int *bp, long toffs, unsigned int tpa
        *bp = (b * fog) >> 8;
 }
 
-#define PACK_RGB16(r, g, b) \
-       (((((r) >> 3) & 0x1f) << 11) | ((((g) >> 2) & 0x3f) << 5) | (((b) >> 3) & 0x1f))
-#define PACK_RGB32(r, g, b) \
-       ((((r) & 0xff) << 16) | (((g) & 0xff) << 8) | ((b) & 0xff))
-
 static void draw_tunnel_range(unsigned short *pix, int xoffs, int yoffs, int starty, int num_lines, long tm)
 {
        int i, j;