partial zbuffer implementation #2
authorJohn Tsiombikas <nuclear@member.fsf.org>
Thu, 9 Jul 2020 22:14:06 +0000 (01:14 +0300)
committerJohn Tsiombikas <nuclear@member.fsf.org>
Thu, 9 Jul 2020 22:14:06 +0000 (01:14 +0300)
src/3dgfx/3dgfx.c
src/3dgfx/3dgfx.h
src/scr/polytest.c

index 16024f8..ef569e6 100644 (file)
@@ -70,6 +70,8 @@ struct g3d_state {
 
        int vport[4];
 
+       uint16_t clear_color, clear_depth;
+
        /* immediate mode */
        int imm_prim;
        int imm_numv, imm_pcount;
@@ -132,6 +134,8 @@ void g3d_reset(void)
        g3d_light_ambient(0.1, 0.1, 0.1);
 
        g3d_mtl_diffuse(1, 1, 1);
+
+       st->clear_depth = 65535;
 }
 
 void g3d_framebuffer(int width, int height, void *pixels)
@@ -156,7 +160,6 @@ void g3d_framebuffer(int width, int height, void *pixels)
 
        st->width = width;
        st->height = height;
-       st->pixels = pixels;
 
        pfill_fb.pixels = pixels;
        pfill_fb.width = width;
@@ -168,7 +171,6 @@ void g3d_framebuffer(int width, int height, void *pixels)
 /* set the framebuffer pointer, without resetting the size */
 void g3d_framebuffer_addr(void *pixels)
 {
-       st->pixels = pixels;
        pfill_fb.pixels = pixels;
 }
 
@@ -180,6 +182,26 @@ void g3d_viewport(int x, int y, int w, int h)
        st->vport[3] = h;
 }
 
+void g3d_clear_color(unsigned char r, unsigned char g, unsigned char b)
+{
+       st->clear_color = PACK_RGB16(r, g, b);
+}
+
+void g3d_clear_depth(uint16_t zval)
+{
+       st->clear_depth = zval;
+}
+
+void g3d_clear(unsigned int mask)
+{
+       if(mask & G3D_COLOR_BUFFER_BIT) {
+               memset16(pfill_fb.pixels, st->clear_color, pfill_fb.width * pfill_fb.height);
+       }
+       if(mask & G3D_DEPTH_BUFFER_BIT) {
+               memset16(pfill_zbuf, st->clear_depth, pfill_fb.width * pfill_fb.height);
+       }
+}
+
 void g3d_enable(unsigned int opt)
 {
        st->opt |= opt;
@@ -545,7 +567,11 @@ void g3d_draw_indexed(int prim, const struct g3d_vertex *varr, int varr_size,
                        if(v[i].w != 0.0f) {
                                v[i].x /= v[i].w;
                                v[i].y /= v[i].w;
-                               /*v[i].z /= v[i].w;*/
+#ifdef ENABLE_ZBUFFER
+                               if(st->opt & G3D_DEPTH_TEST) {
+                                       v[i].z /= v[i].w;
+                               }
+#endif
                        }
 
                        /* viewport transformation */
@@ -555,6 +581,12 @@ void g3d_draw_indexed(int prim, const struct g3d_vertex *varr, int varr_size,
                        /* convert pos to 24.8 fixed point */
                        pv[i].x = cround64(v[i].x * 256.0f);
                        pv[i].y = cround64(v[i].y * 256.0f);
+#ifdef ENABLE_ZBUFFER
+                       if(st->opt & G3D_DEPTH_TEST) {
+                               /* after div/w z is in [-1, 1], remap it to [0, 65535] */
+                               pv[i].z = cround64(v[i].z * 32767.5f + 32767.5f);
+                       }
+#endif
                        /* convert tex coords to 16.16 fixed point */
                        pv[i].u = cround64(v[i].u * 65536.0f);
                        pv[i].v = cround64(v[i].v * 65536.0f);
@@ -583,7 +615,7 @@ void g3d_draw_indexed(int prim, const struct g3d_vertex *varr, int varr_size,
                case 1:
                        if(st->opt & (G3D_ALPHA_BLEND | G3D_ADD_BLEND)) {
                                int r, g, b, inv_alpha;
-                               g3d_pixel *dest = st->pixels + (pv[0].y >> 8) * st->width + (pv[0].x >> 8);
+                               g3d_pixel *dest = pfill_fb.pixels + (pv[0].y >> 8) * st->width + (pv[0].x >> 8);
                                if(st->opt & G3D_ALPHA_BLEND) {
                                        inv_alpha = 255 - pv[0].a;
                                        r = ((int)pv[0].r * pv[0].a + G3D_UNPACK_R(*dest) * inv_alpha) >> 8;
@@ -599,7 +631,7 @@ void g3d_draw_indexed(int prim, const struct g3d_vertex *varr, int varr_size,
                                }
                                *dest++ = G3D_PACK_RGB(r, g, b);
                        } else {
-                               g3d_pixel *dest = st->pixels + (pv[0].y >> 8) * st->width + (pv[0].x >> 8);
+                               g3d_pixel *dest = pfill_fb.pixels + (pv[0].y >> 8) * st->width + (pv[0].x >> 8);
                                *dest = G3D_PACK_RGB(pv[0].r, pv[0].g, pv[0].b);
                        }
                        break;
@@ -621,6 +653,11 @@ void g3d_draw_indexed(int prim, const struct g3d_vertex *varr, int varr_size,
                        } else if(st->opt & G3D_ADD_BLEND) {
                                fill_mode |= POLYFILL_ADD_BIT;
                        }
+#ifdef ENABLE_ZBUFFER
+                       if(st->opt & G3D_DEPTH_TEST) {
+                               fill_mode |= POLYFILL_ZBUF_BIT;
+                       }
+#endif
                        polyfill(fill_mode, pv, vnum);
                }
        }
index 2aeb8be..c04863f 100644 (file)
@@ -80,6 +80,12 @@ enum {
        G3D_NUM_MATRICES
 };
 
+/* clear bits */
+enum {
+       G3D_COLOR_BUFFER_BIT = 1,
+       G3D_DEPTH_BUFFER_BIT = 2
+};
+
 int g3d_init(void);
 void g3d_destroy(void);
 void g3d_reset(void);
@@ -88,6 +94,10 @@ void g3d_framebuffer(int width, int height, void *pixels);
 void g3d_framebuffer_addr(void *pixels);
 void g3d_viewport(int x, int y, int w, int h);
 
+void g3d_clear_color(unsigned char r, unsigned char g, unsigned char b);
+void g3d_clear_depth(uint16_t zval);
+void g3d_clear(unsigned int mask);
+
 void g3d_enable(unsigned int opt);
 void g3d_disable(unsigned int opt);
 void g3d_setopt(unsigned int opt, unsigned int mask);
index 15649c0..ca04256 100644 (file)
@@ -102,6 +102,8 @@ static void start(long trans_time)
 
        g3d_polygon_mode(G3D_GOURAUD);
        g3d_enable(G3D_TEXTURE_2D);
+
+       g3d_enable(G3D_DEPTH_TEST);
 }
 
 static void update(void)
@@ -117,7 +119,9 @@ static void draw(void)
 
        update();
 
-       memset16(fb_pixels, PACK_RGB16(20, 30, 50), FB_WIDTH * FB_HEIGHT);
+       //memset16(fb_pixels, PACK_RGB16(20, 30, 50), FB_WIDTH * FB_HEIGHT);
+       g3d_clear_color(20, 30, 50);
+       g3d_clear(G3D_COLOR_BUFFER_BIT | G3D_DEPTH_BUFFER_BIT);
 
        g3d_matrix_mode(G3D_MODELVIEW);
        g3d_load_identity();
@@ -143,10 +147,18 @@ static void draw(void)
 
                draw_bsp(&torus_bsp, vdir[0], vdir[1], vdir[2]);
        } else {
-               zsort_mesh(&torus);
+               //zsort_mesh(&torus);
                draw_mesh(&torus);
        }
 
+       /*{
+               int i;
+               for(i=0; i<FB_WIDTH*FB_HEIGHT; i++) {
+                       unsigned int z = pfill_zbuf[i];
+                       fb_pixels[i] = z;
+               }
+       }*/
+
        /*draw_mesh(&cube);*/
        swap_buffers(fb_pixels);
 }