debugging the BSP
authorJohn Tsiombikas <nuclear@mutantstargoat.com>
Sun, 18 Feb 2018 20:03:44 +0000 (22:03 +0200)
committerJohn Tsiombikas <nuclear@mutantstargoat.com>
Sun, 18 Feb 2018 20:03:44 +0000 (22:03 +0200)
src/3dgfx.c
src/3dgfx.h
src/bsptree.c
src/mesh.c
src/polytest.c
src/vmath.h

index 3999747..a4af73a 100644 (file)
@@ -404,6 +404,9 @@ void g3d_draw_indexed(int prim, const struct g3d_vertex *varr, int varr_size,
        int vnum = prim; /* primitive vertex counts correspond to enum values */
        int mvtop = st->mtop[G3D_MODELVIEW];
        int ptop = st->mtop[G3D_PROJECTION];
+       struct g3d_vertex *tmpv;
+
+       tmpv = alloca(prim * 6 * sizeof *tmpv);
 
        /* calc the normal matrix */
        memcpy(st->norm_mat, st->mat[G3D_MODELVIEW][mvtop], 16 * sizeof(float));
@@ -432,7 +435,6 @@ void g3d_draw_indexed(int prim, const struct g3d_vertex *varr, int varr_size,
 
                /* clipping */
                for(i=0; i<6; i++) {
-                       struct g3d_vertex tmpv[16];
                        memcpy(tmpv, v, vnum * sizeof *v);
 
                        if(clip_frustum(v, &vnum, tmpv, vnum, i) < 0) {
index fd7f625..2a82284 100644 (file)
@@ -82,6 +82,8 @@ void g3d_ortho(float left, float right, float bottom, float top, float znear, fl
 void g3d_frustum(float left, float right, float bottom, float top, float znear, float zfar);
 void g3d_perspective(float vfov, float aspect, float znear, float zfar);
 
+/* returns pointer to the *internal* matrix, and if argument m is not null,
+ * also copies the internal matrix there. */
 const float *g3d_get_matrix(int which, float *m);
 
 void g3d_light_pos(int idx, float x, float y, float z);
index e9353d1..30f67ef 100644 (file)
@@ -18,6 +18,7 @@ static void free_tree(struct bspnode *n);
 
 static struct bspnode *new_node(struct g3d_vertex *v, int vnum);
 static struct bspnode *add_poly_tree(struct bspnode *n, struct g3d_vertex *v, int vnum);
+static void draw_bsp_tree(struct bspnode *n, const vec3_t *vdir);
 
 static void save_bsp_tree(struct bspnode *n, FILE *fp);
 static struct bspnode *load_bsp_tree(FILE *fp);
@@ -118,6 +119,11 @@ 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)
 {
+       vec3_t vdir;
+       vdir.x = view_x;
+       vdir.y = view_y;
+       vdir.z = view_z;
+       draw_bsp_tree(bsp->root, &vdir);
 }
 
 static int count_nodes(struct bspnode *n)
@@ -233,6 +239,24 @@ static struct bspnode *add_poly_tree(struct bspnode *n, struct g3d_vertex *v, in
        return n;
 }
 
+static void draw_bsp_tree(struct bspnode *n, const vec3_t *vdir)
+{
+       float dot;
+
+       if(!n) return;
+
+       dot = vdir->x * n->plane.nx + vdir->y * n->plane.ny + vdir->z * n->plane.nz;
+       if(dot >= 0.0f) {
+               draw_bsp_tree(n->front, vdir);
+               g3d_draw_indexed(n->vcount, n->verts, n->vcount, 0, 0);
+               draw_bsp_tree(n->back, vdir);
+       } else {
+               draw_bsp_tree(n->back, vdir);
+               g3d_draw_indexed(n->vcount, n->verts, n->vcount, 0, 0);
+               draw_bsp_tree(n->front, vdir);
+       }
+}
+
 static void save_bsp_tree(struct bspnode *n, FILE *fp)
 {
        /* TODO */
index 0dc6bf6..af5936e 100644 (file)
@@ -304,7 +304,6 @@ int gen_cube_mesh(struct g3d_mesh *mesh, float sz, int sub)
        int i;
        struct g3d_mesh *m;
        struct g3d_mesh tmpmesh;
-       float xform[16];
        static float rotface[][4] = {
                {0, 0, 1, 0},
                {90, 0, 1, 0},
@@ -324,8 +323,7 @@ int gen_cube_mesh(struct g3d_mesh *mesh, float sz, int sub)
                g3d_load_identity();
                g3d_rotate(rotface[i][0], rotface[i][1], rotface[i][2], rotface[i][3]);
                g3d_translate(0, 0, sz / 2.0f);
-               g3d_get_matrix(G3D_MODELVIEW, xform);
-               apply_mesh_xform(m, xform);
+               apply_mesh_xform(m, g3d_get_matrix(G3D_MODELVIEW, 0));
                if(i > 0) {
                        if(append_mesh(mesh, m) == -1) {
                                return -1;
index 8d81262..dfca5ef 100644 (file)
@@ -102,6 +102,9 @@ static void update(void)
 
 static void draw(void)
 {
+       float vdir[3];
+       float mat[16];
+
        update();
 
        memset(fb_pixels, 0, fb_width * fb_height * 2);
@@ -116,6 +119,14 @@ static void draw(void)
                g3d_rotate(cam_theta, 0, 1, 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];
+
+
        g3d_light_pos(0, -10, 10, 20);
 
        zsort_mesh(&torus);
@@ -123,8 +134,8 @@ static void draw(void)
        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);*/
+       /*draw_mesh(&torus);*/
+       draw_bsp(&torus_bsp, vdir[0], vdir[1], vdir[2]);
 
        /*draw_mesh(&cube);*/
        swap_buffers(fb_pixels);
index 6d1add9..3cdbedf 100644 (file)
@@ -126,4 +126,19 @@ static INLINE quat_t quat_rotate(quat_t q, float angle, float x, float y, float
        return quat_mul(q, rq);
 }
 
+static INLINE void mat4_transpose(float *mat)
+{
+       int i, j;
+
+       for(i=0; i<4; i++) {
+               for(j=0; j<i; j++) {
+                       int rowidx = i * 4 + j;
+                       int colidx = j * 4 + i;
+                       float tmp = mat[rowidx];
+                       mat[rowidx] = mat[colidx];
+                       mat[colidx] = tmp;
+               }
+       }
+}
+
 #endif /* VMATH_H_ */