BSP construction debugging
authorJohn Tsiombikas <nuclear@member.fsf.org>
Sun, 18 Feb 2018 16:54:25 +0000 (18:54 +0200)
committerJohn Tsiombikas <nuclear@member.fsf.org>
Sun, 18 Feb 2018 16:54:25 +0000 (18:54 +0200)
src/bsptree.c
src/bsptree.h
src/mesh.c
src/polytest.c

index 6ad4046..dc8f2af 100644 (file)
@@ -92,7 +92,7 @@ int bsp_add_poly(struct bsptree *bsp, struct g3d_vertex *v, int vnum)
        return 0;
 }
 
-void bsp_add_mesh(struct bsptree *bsp, struct g3d_mesh *m)
+int bsp_add_mesh(struct bsptree *bsp, struct g3d_mesh *m)
 {
        int i, j, nfaces;
        struct g3d_vertex v[4];
@@ -109,8 +109,11 @@ void bsp_add_mesh(struct bsptree *bsp, struct g3d_mesh *m)
                                v[j] = *vptr++;
                        }
                }
-               bsp_add_poly(bsp, v, m->prim);
+               if(bsp_add_poly(bsp, v, m->prim) == -1) {
+                       return -1;
+               }
        }
+       return 0;
 }
 
 void draw_bsp(struct bsptree *bsp, float view_x, float view_y, float view_z)
@@ -180,39 +183,42 @@ static struct bspnode *add_poly_tree(struct bspnode *n, struct g3d_vertex *v, in
        clipped = alloca((vnum + 1) * sizeof *clipped);
 
        clipres = clip_poly(clipped, &clipped_vnum, v, vnum, &n->plane);
-       if(clipres > 0) {       /* polygon completely in the positive subspace */
+       if(clipres > 0) {
+               /* polygon completely in the positive subspace */
                if(!(nres = add_poly_tree(n->front, v, vnum))) {
                        return 0;
                }
                n->front = nres;
-       }
-       if(clipres < 0) {       /* polygon completely in the negative subspace */
+
+       } else if(clipres < 0) {
+               /* polygon completely in the negative subspace */
                if(!(nres = add_poly_tree(n->back, v, vnum))) {
                        return 0;
                }
                n->back = nres;
-       }
 
-       /* polygon is straddling the plane */
-       if(!(nres = add_poly_tree(n->front, clipped, clipped_vnum))) {
-               return 0;
-       }
-       n->front = nres;
+       } else {
+               /* polygon is straddling the plane */
+               if(!(nres = add_poly_tree(n->front, clipped, clipped_vnum))) {
+                       return 0;
+               }
+               n->front = nres;
 
-       negplane.x = n->plane.x;
-       negplane.y = n->plane.y;
-       negplane.z = n->plane.z;
-       negplane.nx = -n->plane.nx;
-       negplane.ny = -n->plane.ny;
-       negplane.nz = -n->plane.nz;
+               negplane.x = n->plane.x;
+               negplane.y = n->plane.y;
+               negplane.z = n->plane.z;
+               negplane.nx = -n->plane.nx;
+               negplane.ny = -n->plane.ny;
+               negplane.nz = -n->plane.nz;
 
-       clipres = clip_poly(clipped, &clipped_vnum, v, vnum, &negplane);
-       assert(clipres == 0);
+               clipres = clip_poly(clipped, &clipped_vnum, v, vnum, &negplane);
+               /*assert(clipres == 0);*/
 
-       if(!(nres = add_poly_tree(n->back, clipped, clipped_vnum))) {
-               return 0;
+               if(!(nres = add_poly_tree(n->back, clipped, clipped_vnum))) {
+                       return 0;
+               }
+               n->back = nres;
        }
-       n->back = nres;
        return n;
 }
 
index 53b4cd2..ff29051 100644 (file)
@@ -23,7 +23,7 @@ int save_bsp(struct bsptree *bsp, const char *fname);
 int load_bsp(struct bsptree *bsp, const char *fname);
 
 int bsp_add_poly(struct bsptree *bsp, struct g3d_vertex *v, int vnum);
-void bsp_add_mesh(struct bsptree *bsp, struct g3d_mesh *m);
+int bsp_add_mesh(struct bsptree *bsp, struct g3d_mesh *m);
 
 void draw_bsp(struct bsptree *bsp, float view_x, float view_y, float view_z);
 
index bde8266..0dc6bf6 100644 (file)
@@ -394,6 +394,7 @@ int gen_torus_mesh(struct g3d_mesh *mesh, float rad, float ringrad, int usub, in
                        int chess = (i & 1) == (j & 1);
 
                        torusvec(&vptr->x, theta, phi, rad, ringrad);
+                       vptr->w = 1.0f;
 
                        vptr->nx = (vptr->x - rcent[0]) / ringrad;
                        vptr->ny = (vptr->y - rcent[1]) / ringrad;
index 85b3128..8d81262 100644 (file)
@@ -9,6 +9,7 @@
 #include "polyfill.h"  /* just for struct pimage */
 #include "cfgopt.h"
 #include "mesh.h"
+#include "bsptree.h"
 
 static int init(void);
 static void destroy(void);
@@ -28,6 +29,7 @@ static struct screen scr = {
 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;
 
@@ -52,6 +54,12 @@ static int init(void)
                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
@@ -70,6 +78,7 @@ static void destroy(void)
        free(cube.varr);
        free(torus.varr);
        free(torus.iarr);
+       destroy_bsp(&torus_bsp);
 }
 
 static void start(long trans_time)
@@ -115,6 +124,7 @@ static void draw(void)
        g3d_set_texture(tex.width, tex.height, tex.pixels);
 
        draw_mesh(&torus);
+       /*draw_bsp(&torus_bsp);*/
 
        /*draw_mesh(&cube);*/
        swap_buffers(fb_pixels);