zbuffer done
[dosdemo] / src / scr / polytest.c
index 0b05313..6648547 100644 (file)
@@ -10,6 +10,7 @@
 #include "cfgopt.h"
 #include "mesh.h"
 #include "bsptree.h"
+#include "util.h"
 
 static int init(void);
 static void destroy(void);
@@ -41,6 +42,9 @@ static int use_bsp = 0;
 static uint16_t *lowres_pixels;
 static int lowres_width, lowres_height;
 
+static int use_zbuf;
+
+
 struct screen *polytest_screen(void)
 {
        return &scr;
@@ -66,11 +70,11 @@ static int init(void)
        }
        */
 
-       gen_texture(&tex, 128, 128);
+       gen_texture(&tex, 64, 64);
 
 #ifdef DEBUG_POLYFILL
-       lowres_width = fb_width / LOWRES_SCALE;
-       lowres_height = fb_height / LOWRES_SCALE;
+       lowres_width = FB_WIDTH / LOWRES_SCALE;
+       lowres_height = FB_HEIGHT / LOWRES_SCALE;
        lowres_pixels = malloc(lowres_width * lowres_height * 2);
        scr.draw = draw_debug;
 #endif
@@ -93,7 +97,7 @@ static void start(long trans_time)
 {
        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);
@@ -101,6 +105,8 @@ static void start(long trans_time)
 
        g3d_polygon_mode(G3D_GOURAUD);
        g3d_enable(G3D_TEXTURE_2D);
+
+       g3d_clear_color(20, 30, 50);
 }
 
 static void update(void)
@@ -116,7 +122,14 @@ static void draw(void)
 
        update();
 
-       memset(fb_pixels, 0, fb_width * fb_height * 2);
+       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();
@@ -128,9 +141,8 @@ static void draw(void)
                g3d_rotate(cam_theta, 0, 1, 0);
        }
 
-       g3d_light_pos(0, -10, 10, 20);
-
-       g3d_mtl_diffuse(0.4, 0.7, 1.0);
+       g3d_light_dir(0, -10, 10, 10);
+       g3d_mtl_diffuse(1.0, 1.0, 1.0);
        g3d_set_texture(tex.width, tex.height, tex.pixels);
 
        if(use_bsp) {
@@ -143,10 +155,23 @@ static void draw(void)
 
                draw_bsp(&torus_bsp, vdir[0], vdir[1], vdir[2]);
        } else {
-               zsort_mesh(&torus);
+               if(!use_zbuf) {
+                       zsort_mesh(&torus);
+               }
                draw_mesh(&torus);
        }
 
+       /* show zbuffer */
+       /*{
+               int i;
+               for(i=0; i<FB_WIDTH*FB_HEIGHT; i++) {
+                       unsigned int z = pfill_zbuf[i];
+                       fb_pixels[i] = (z >> 5) & 0x7e0;
+               }
+       }*/
+
+       cs_dputs(fb_pixels, 140, 0, use_zbuf ? "zbuffer" : "zsort");
+
        /*draw_mesh(&cube);*/
        swap_buffers(fb_pixels);
 }
@@ -170,7 +195,7 @@ static void draw_debug(void)
        draw_lowres_raster();
 
 
-       g3d_framebuffer(fb_width, fb_height, fb_pixels);
+       g3d_framebuffer(FB_WIDTH, FB_HEIGHT, fb_pixels);
 
        g3d_polygon_mode(G3D_WIRE);
        draw_mesh(&cube);
@@ -201,10 +226,10 @@ static void draw_lowres_raster(void)
 
        for(i=0; i<lowres_height; i++) {
                for(j=0; j<lowres_width; j++) {
-                       draw_huge_pixel(dptr, fb_width, *sptr++);
+                       draw_huge_pixel(dptr, FB_WIDTH, *sptr++);
                        dptr += LOWRES_SCALE;
                }
-               dptr += fb_width * LOWRES_SCALE - fb_width;
+               dptr += FB_WIDTH * LOWRES_SCALE - FB_WIDTH;
        }
 }
 
@@ -215,6 +240,11 @@ static void keypress(int key)
                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;
        }
 }
 
@@ -230,9 +260,12 @@ static int gen_texture(struct pimage *img, int xsz, int ysz)
 
        for(i=0; i<ysz; i++) {
                for(j=0; j<xsz; j++) {
-                       int val = i ^ j;
+                       int val = (i << 2) ^ (j << 2);
+                       uint16_t r = val;
+                       uint16_t g = val << 1;
+                       uint16_t b = val << 2;
 
-                       *pix++ = PACK_RGB16(val, val, val);
+                       *pix++ = PACK_RGB16(r, g, b);
                }
        }