debugging the BSP
[dosdemo] / src / polytest.c
index 5b43eea..dfca5ef 100644 (file)
@@ -1,23 +1,22 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <math.h>
 #include "screen.h"
 #include "demo.h"
 #include "3dgfx.h"
-
-struct mesh {
-       int prim;
-       struct g3d_vertex *varr;
-       unsigned int *iarr;
-       int vcount, icount;
-};
+#include "gfxutil.h"
+#include "polyfill.h"  /* just for struct pimage */
+#include "cfgopt.h"
+#include "mesh.h"
+#include "bsptree.h"
 
 static int init(void);
 static void destroy(void);
 static void start(long trans_time);
 static void draw(void);
-static void draw_mesh(struct mesh *mesh);
-static int gen_cube(struct mesh *mesh, float sz);
+static void draw_lowres_raster(void);
+static int gen_texture(struct pimage *img, int xsz, int ysz);
 
 static struct screen scr = {
        "polytest",
@@ -27,7 +26,16 @@ static struct screen scr = {
        draw
 };
 
-static struct mesh cube;
+static float cam_theta, cam_phi = 25;
+static float cam_dist = 3;
+static struct g3d_mesh cube, torus;
+static struct bsptree torus_bsp;
+
+static struct pimage tex;
+
+#define LOWRES_SCALE   10
+static uint16_t *lowres_pixels;
+static int lowres_width, lowres_height;
 
 struct screen *polytest_screen(void)
 {
@@ -36,13 +44,41 @@ struct screen *polytest_screen(void)
 
 static int init(void)
 {
-       gen_cube(&cube, 1.0);
+       int i;
+
+       gen_cube_mesh(&cube, 1.0, 0);
+       gen_torus_mesh(&torus, 1.0, 0.25, 24, 12);
+       /* scale texcoords */
+       for(i=0; i<torus.vcount; i++) {
+               torus.varr[i].u *= 4.0;
+               torus.varr[i].v *= 2.0;
+       }
+
+       init_bsp(&torus_bsp);
+       if(bsp_add_mesh(&torus_bsp, &torus) == -1) {
+               fprintf(stderr, "failed to construct torus BSP tree\n");
+               return -1;
+       }
+
+       gen_texture(&tex, 128, 128);
+
+#ifdef DEBUG_POLYFILL
+       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
+
        return 0;
 }
 
 static void destroy(void)
 {
+       free(lowres_pixels);
        free(cube.varr);
+       free(torus.varr);
+       free(torus.iarr);
+       destroy_bsp(&torus_bsp);
 }
 
 static void start(long trans_time)
@@ -52,114 +88,135 @@ static void start(long trans_time)
        g3d_perspective(50.0, 1.3333333, 0.5, 100.0);
 
        g3d_enable(G3D_CULL_FACE);
+       g3d_enable(G3D_LIGHTING);
+       g3d_enable(G3D_LIGHT0);
+
+       g3d_polygon_mode(G3D_TEX_GOURAUD);
 }
 
+static void update(void)
+{
+       mouse_orbit_update(&cam_theta, &cam_phi, &cam_dist);
+}
+
+
 static void draw(void)
 {
-       static int prev_mx, prev_my;
-       static float theta, phi = 25;
+       float vdir[3];
+       float mat[16];
 
-       int dx = mouse_x - prev_mx;
-       int dy = mouse_y - prev_my;
-       prev_mx = mouse_x;
-       prev_my = mouse_y;
+       update();
 
-       if(dx || dy) {
-               theta += dx * 2.0;
-               phi += dy * 2.0;
+       memset(fb_pixels, 0, fb_width * fb_height * 2);
 
-               if(phi < -90) phi = -90;
-               if(phi > 90) phi = 90;
+       g3d_matrix_mode(G3D_MODELVIEW);
+       g3d_load_identity();
+       g3d_translate(0, 0, -cam_dist);
+       if(opt.sball) {
+               g3d_mult_matrix(sball_matrix);
+       } else {
+               g3d_rotate(cam_phi, 1, 0, 0);
+               g3d_rotate(cam_theta, 0, 1, 0);
        }
 
-       /*float angle = (float)time_msec / 50.0;*/
+       /* calc world-space view direction */
+       g3d_get_matrix(G3D_MODELVIEW, mat);
+       /* transform (0, 0, -1) with transpose(mat3x3) */
+       vdir[0] = -mat[2];
+       vdir[1] = -mat[6];
+       vdir[2] = -mat[10];
 
-       memset(fb_pixels, 0, fb_width * fb_height * 2);
+
+       g3d_light_pos(0, -10, 10, 20);
+
+       zsort_mesh(&torus);
+
+       g3d_mtl_diffuse(0.4, 0.7, 1.0);
+       g3d_set_texture(tex.width, tex.height, tex.pixels);
+
+       /*draw_mesh(&torus);*/
+       draw_bsp(&torus_bsp, vdir[0], vdir[1], vdir[2]);
+
+       /*draw_mesh(&cube);*/
+       swap_buffers(fb_pixels);
+}
+
+static void draw_debug(void)
+{
+       update();
+
+       memset(lowres_pixels, 0, lowres_width * lowres_height * 2);
 
        g3d_matrix_mode(G3D_MODELVIEW);
        g3d_load_identity();
-       g3d_translate(0, 0, -5);
-       g3d_rotate(phi, 1, 0, 0);
-       g3d_rotate(theta, 0, 1, 0);
+       g3d_translate(0, 0, -cam_dist);
+       g3d_rotate(cam_phi, 1, 0, 0);
+       g3d_rotate(cam_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);
 }
 
-static void draw_mesh(struct mesh *mesh)
+
+static void draw_huge_pixel(uint16_t *dest, int dest_width, uint16_t color)
 {
-       if(mesh->iarr) {
-               /*g3d_draw_indexed(mesh->prim, mesh->iarr, mesh->icount, mesh->varr);*/
-       } else {
-               g3d_draw(mesh->prim, mesh->varr, mesh->vcount);
+       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;
        }
 }
 
-#define NORMAL(vp, x, y, z) do { vp->nx = x; vp->ny = y; vp->nz = z; } while(0)
-#define COLOR(vp, cr, cg, cb) do { vp->r = cr; vp->g = cg; vp->b = cb; } while(0)
-#define TEXCOORD(vp, tu, tv) do { vp->u = tu; vp->v = tv; } while(0)
-#define VERTEX(vp, vx, vy, vz) \
-       do { \
-               vp->x = vx; vp->y = vy; vp->z = vz; vp->w = 1.0f; \
-               ++vp; \
-       } while(0)
-
-static int gen_cube(struct mesh *mesh, float sz)
+static void draw_lowres_raster(void)
 {
-       struct g3d_vertex *vptr;
-       float hsz = sz / 2.0;
+       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;
+       }
+}
 
-       mesh->prim = G3D_QUADS;
-       mesh->iarr = 0;
-       mesh->icount = 0;
+static int gen_texture(struct pimage *img, int xsz, int ysz)
+{
+       int i, j;
+       uint16_t *pix;
 
-       mesh->vcount = 24;
-       if(!(mesh->varr = malloc(mesh->vcount * sizeof *mesh->varr))) {
+       if(!(img->pixels = malloc(xsz * ysz * sizeof *pix))) {
                return -1;
        }
-       vptr = mesh->varr;
-
-       /* -Z */
-       NORMAL(vptr, 0, 0, -1);
-       COLOR(vptr, 255, 0, 255);
-       VERTEX(vptr, hsz, -hsz, -hsz);
-       VERTEX(vptr, -hsz, -hsz, -hsz);
-       VERTEX(vptr, -hsz, hsz, -hsz);
-       VERTEX(vptr, hsz, hsz, -hsz);
-       /* -Y */
-       NORMAL(vptr, 0, -1, 0);
-       COLOR(vptr, 0, 255, 255);
-       VERTEX(vptr, -hsz, -hsz, -hsz);
-       VERTEX(vptr, hsz, -hsz, -hsz);
-       VERTEX(vptr, hsz, -hsz, hsz);
-       VERTEX(vptr, -hsz, -hsz, hsz);
-       /* -X */
-       NORMAL(vptr, -1, 0, 0);
-       COLOR(vptr, 255, 255, 0);
-       VERTEX(vptr, -hsz, -hsz, -hsz);
-       VERTEX(vptr, -hsz, -hsz, hsz);
-       VERTEX(vptr, -hsz, hsz, hsz);
-       VERTEX(vptr, -hsz, hsz, -hsz);
-       /* +X */
-       NORMAL(vptr, 1, 0, 0);
-       COLOR(vptr, 255, 0, 0);
-       VERTEX(vptr, hsz, -hsz, hsz);
-       VERTEX(vptr, hsz, -hsz, -hsz);
-       VERTEX(vptr, hsz, hsz, -hsz);
-       VERTEX(vptr, hsz, hsz, hsz);
-       /* +Y */
-       NORMAL(vptr, 0, 1, 0);
-       COLOR(vptr, 0, 255, 0);
-       VERTEX(vptr, -hsz, hsz, hsz);
-       VERTEX(vptr, hsz, hsz, hsz);
-       VERTEX(vptr, hsz, hsz, -hsz);
-       VERTEX(vptr, -hsz, hsz, -hsz);
-       /* +Z */
-       NORMAL(vptr, 0, 0, 1);
-       COLOR(vptr, 0, 0, 255);
-       VERTEX(vptr, -hsz, -hsz, hsz);
-       VERTEX(vptr, hsz, -hsz, hsz);
-       VERTEX(vptr, hsz, hsz, hsz);
-       VERTEX(vptr, -hsz, hsz, hsz);
+       pix = img->pixels;
+
+       for(i=0; i<ysz; i++) {
+               for(j=0; j<xsz; j++) {
+                       int val = i ^ j;
+
+                       *pix++ = PACK_RGB16(val, val, val);
+               }
+       }
 
+       img->width = xsz;
+       img->height = ysz;
        return 0;
 }