zbuffer done
authorJohn Tsiombikas <nuclear@member.fsf.org>
Fri, 10 Jul 2020 12:20:23 +0000 (15:20 +0300)
committerJohn Tsiombikas <nuclear@member.fsf.org>
Fri, 10 Jul 2020 12:20:23 +0000 (15:20 +0300)
src/3dgfx/polyfill.h
src/3dgfx/polytmpl.h
src/glut/main.c
src/scr/polytest.c

index fed8eda..6c95667 100644 (file)
@@ -66,7 +66,7 @@ struct pvertex {
        int32_t x, y; /* 24.8 fixed point */
        int32_t u, v; /* 16.16 fixed point */
        int32_t r, g, b, a;  /* int 0-255 */
        int32_t x, y; /* 24.8 fixed point */
        int32_t u, v; /* 16.16 fixed point */
        int32_t r, g, b, a;  /* int 0-255 */
-       uint16_t z;     /* 0-65535 */
+       int32_t z;      /* 0-65535 */
 };
 
 struct pimage {
 };
 
 struct pimage {
index 01bbd01..167d5e1 100644 (file)
@@ -13,7 +13,7 @@ static uint32_t SCANEDGE(struct pvertex *v0, struct pvertex *v1, struct pvertex
        int32_t u, v, du, dv, uslope, vslope;
 #endif
 #ifdef ZBUF
        int32_t u, v, du, dv, uslope, vslope;
 #endif
 #ifdef ZBUF
-       int z, dz, zslope;
+       int32_t z, dz, zslope;
 #endif
        int32_t start_idx, end_idx;
 
 #endif
        int32_t start_idx, end_idx;
 
@@ -115,7 +115,7 @@ void POLYFILL(struct pvertex *pv, int nverts)
        int32_t u, v, du, dv, uslope, vslope;
 #endif
 #ifdef ZBUF
        int32_t u, v, du, dv, uslope, vslope;
 #endif
 #ifdef ZBUF
-       int z, dz, zslope;
+       int32_t z, dz, zslope;
 #endif
 
        for(i=1; i<nverts; i++) {
 #endif
 
        for(i=1; i<nverts; i++) {
@@ -300,6 +300,9 @@ void POLYFILL(struct pvertex *pv, int nverts)
 
                /* go across the scanline interpolating if necessary */
                while(x <= right[i].x) {
 
                /* go across the scanline interpolating if necessary */
                while(x <= right[i].x) {
+                       /*if(x == (180 << 8) && i == 174) {
+                               asm("int $3");
+                       }*/
 #if defined(GOURAUD) || defined(TEXMAP) || defined(BLEND_ALPHA) || defined(BLEND_ADD)
                        int cr, cg, cb;
 #endif
 #if defined(GOURAUD) || defined(TEXMAP) || defined(BLEND_ALPHA) || defined(BLEND_ADD)
                        int cr, cg, cb;
 #endif
@@ -311,11 +314,11 @@ void POLYFILL(struct pvertex *pv, int nverts)
 #endif
 
 #ifdef ZBUF
 #endif
 
 #ifdef ZBUF
-                       int cz = z;
+                       int32_t cz = z;
                        z += zslope;
 
                        if(z <= *zptr) {
                        z += zslope;
 
                        if(z <= *zptr) {
-                               *zptr = z;
+                               *zptr++ = z;
                        } else {
 #ifdef GOURAUD
                                r += rslope;
                        } else {
 #ifdef GOURAUD
                                r += rslope;
@@ -329,9 +332,12 @@ void POLYFILL(struct pvertex *pv, int nverts)
                                u += uslope;
                                v += vslope;
 #endif
                                u += uslope;
                                v += vslope;
 #endif
-                               goto skip_pixel;
+                               /* skip pixel */
+                               pixptr++;
+                               zptr++;
+                               x += 256;
+                               continue;
                        }
                        }
-                       zptr++;
 #endif
 
 #ifdef GOURAUD
 #endif
 
 #ifdef GOURAUD
@@ -407,9 +413,6 @@ void POLYFILL(struct pvertex *pv, int nverts)
                        color = G3D_PACK_RGB(cr, cg, cb);
 #endif
 
                        color = G3D_PACK_RGB(cr, cg, cb);
 #endif
 
-#ifdef ZBUF
-skip_pixel:
-#endif
 #ifdef DEBUG_OVERDRAW
                        *pixptr++ += DEBUG_OVERDRAW;
 #else
 #ifdef DEBUG_OVERDRAW
                        *pixptr++ += DEBUG_OVERDRAW;
 #else
index fb663e6..79833d9 100644 (file)
@@ -73,7 +73,7 @@ static cgm_quat rot = {0, 0, 0, 1};
 int main(int argc, char **argv)
 {
        glutInit(&argc, argv);
 int main(int argc, char **argv)
 {
        glutInit(&argc, argv);
-       glutInitWindowSize(800, 600);
+       glutInitWindowSize(1280, 960);
        glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
        glutCreateWindow("Mindlapse");
 
        glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
        glutCreateWindow("Mindlapse");
 
@@ -428,14 +428,28 @@ static void map_mouse_pos(int *xp, int *yp)
 
 static void mouse_button(int bn, int st, int x, int y)
 {
 
 static void mouse_button(int bn, int st, int x, int y)
 {
+       int bit;
+
        map_mouse_pos(&x, &y);
        mouse_x = x;
        mouse_y = y;
 
        map_mouse_pos(&x, &y);
        mouse_x = x;
        mouse_y = y;
 
+       switch(bn) {
+       case GLUT_LEFT_BUTTON:
+               bit = 0;
+               break;
+       case GLUT_RIGHT_BUTTON:
+               bit = 1;
+               break;
+       case GLUT_MIDDLE_BUTTON:
+               bit = 2;
+               break;
+       }
+
        if(st == GLUT_DOWN) {
        if(st == GLUT_DOWN) {
-               mouse_bmask |= 1 << bn;
+               mouse_bmask |= 1 << bit;
        } else {
        } else {
-               mouse_bmask &= ~(1 << bn);
+               mouse_bmask &= ~(1 << bit);
        }
 }
 
        }
 }
 
index ca04256..6648547 100644 (file)
@@ -42,6 +42,9 @@ static int use_bsp = 0;
 static uint16_t *lowres_pixels;
 static int lowres_width, lowres_height;
 
 static uint16_t *lowres_pixels;
 static int lowres_width, lowres_height;
 
+static int use_zbuf;
+
+
 struct screen *polytest_screen(void)
 {
        return &scr;
 struct screen *polytest_screen(void)
 {
        return &scr;
@@ -94,7 +97,7 @@ static void start(long trans_time)
 {
        g3d_matrix_mode(G3D_PROJECTION);
        g3d_load_identity();
 {
        g3d_matrix_mode(G3D_PROJECTION);
        g3d_load_identity();
-       g3d_perspective(50.0, 1.3333333, 0.5, 100.0);
+       g3d_perspective(50.0, 1.3333333, 1.0, 10.0);
 
        g3d_enable(G3D_CULL_FACE);
        g3d_enable(G3D_LIGHTING);
 
        g3d_enable(G3D_CULL_FACE);
        g3d_enable(G3D_LIGHTING);
@@ -103,7 +106,7 @@ static void start(long trans_time)
        g3d_polygon_mode(G3D_GOURAUD);
        g3d_enable(G3D_TEXTURE_2D);
 
        g3d_polygon_mode(G3D_GOURAUD);
        g3d_enable(G3D_TEXTURE_2D);
 
-       g3d_enable(G3D_DEPTH_TEST);
+       g3d_clear_color(20, 30, 50);
 }
 
 static void update(void)
 }
 
 static void update(void)
@@ -119,9 +122,14 @@ static void draw(void)
 
        update();
 
 
        update();
 
-       //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);
+       if(use_zbuf) {
+               g3d_clear(G3D_COLOR_BUFFER_BIT | G3D_DEPTH_BUFFER_BIT);
+               g3d_enable(G3D_DEPTH_TEST);
+       } else {
+               g3d_clear(G3D_COLOR_BUFFER_BIT);
+               g3d_disable(G3D_DEPTH_TEST);
+       }
+
 
        g3d_matrix_mode(G3D_MODELVIEW);
        g3d_load_identity();
 
        g3d_matrix_mode(G3D_MODELVIEW);
        g3d_load_identity();
@@ -147,18 +155,23 @@ static void draw(void)
 
                draw_bsp(&torus_bsp, vdir[0], vdir[1], vdir[2]);
        } else {
 
                draw_bsp(&torus_bsp, vdir[0], vdir[1], vdir[2]);
        } else {
-               //zsort_mesh(&torus);
+               if(!use_zbuf) {
+                       zsort_mesh(&torus);
+               }
                draw_mesh(&torus);
        }
 
                draw_mesh(&torus);
        }
 
+       /* show zbuffer */
        /*{
                int i;
                for(i=0; i<FB_WIDTH*FB_HEIGHT; i++) {
                        unsigned int z = pfill_zbuf[i];
        /*{
                int i;
                for(i=0; i<FB_WIDTH*FB_HEIGHT; i++) {
                        unsigned int z = pfill_zbuf[i];
-                       fb_pixels[i] = z;
+                       fb_pixels[i] = (z >> 5) & 0x7e0;
                }
        }*/
 
                }
        }*/
 
+       cs_dputs(fb_pixels, 140, 0, use_zbuf ? "zbuffer" : "zsort");
+
        /*draw_mesh(&cube);*/
        swap_buffers(fb_pixels);
 }
        /*draw_mesh(&cube);*/
        swap_buffers(fb_pixels);
 }
@@ -227,6 +240,11 @@ static void keypress(int key)
                use_bsp = !use_bsp;
                printf("drawing with %s\n", use_bsp ? "BSP tree" : "z-sorting");
                break;
                use_bsp = !use_bsp;
                printf("drawing with %s\n", use_bsp ? "BSP tree" : "z-sorting");
                break;
+
+       case 'z':
+               use_zbuf = !use_zbuf;
+               printf("Z-buffer %s\n", use_zbuf ? "on" : "off");
+               break;
        }
 }
 
        }
 }