backported from rtxon: check_clip_poly (needed for new BSP code)
authorJohn Tsiombikas <nuclear@member.fsf.org>
Thu, 30 Aug 2018 06:04:46 +0000 (09:04 +0300)
committerJohn Tsiombikas <nuclear@member.fsf.org>
Thu, 30 Aug 2018 06:04:46 +0000 (09:04 +0300)
also commented out the BSP drawing in polytest for now.

src/polyclip.c
src/polyclip.h
src/polytest.c

index 28e8505..35e6cd6 100644 (file)
@@ -11,6 +11,8 @@ struct ray {
 static int clip_edge(struct g3d_vertex *poly, int *vnumptr,
                const struct g3d_vertex *v0, const struct g3d_vertex *v1,
                const struct cplane *plane);
+static int check_clip_edge(const struct g3d_vertex *v0,
+               const struct g3d_vertex *v1, const struct cplane *plane);
 static int clip_edge_frustum(struct g3d_vertex *poly, int *vnumptr,
                const struct g3d_vertex *v0, const struct g3d_vertex *v1, int fplane);
 static float distance_signed(float *pos, const struct cplane *plane);
@@ -43,6 +45,21 @@ int clip_poly(struct g3d_vertex *vout, int *voutnum,
        return edges_clipped > 0 ? 0 : 1;
 }
 
+int check_clip_poly(const struct g3d_vertex *v, int vnum, struct cplane *plane)
+{
+       int i, nextidx, res;
+       int edges_clipped = 0;
+
+       for(i=0; i<vnum; i++) {
+               nextidx = i + 1;
+               if(nextidx >= vnum) nextidx = 0;
+               res = check_clip_edge(v + i, v + nextidx, plane);
+               if(res == 0) {
+                       ++edges_clipped;
+               }
+       }
+       return edges_clipped ? 0 : res;
+}
 
 int clip_frustum(struct g3d_vertex *vout, int *voutnum,
                const struct g3d_vertex *vin, int vnum, int fplane)
@@ -163,6 +180,27 @@ static int clip_edge(struct g3d_vertex *poly, int *vnumptr,
        return 0;
 }
 
+/* same as above, but only checks for clipping and classifies the edge */
+static int check_clip_edge(const struct g3d_vertex *v0,
+               const struct g3d_vertex *v1, const struct cplane *plane)
+{
+       float pos0[3], pos1[3];
+       float d0, d1;
+
+       pos0[0] = v0->x; pos0[1] = v0->y; pos0[2] = v0->z;
+       pos1[0] = v1->x; pos1[1] = v1->y; pos1[2] = v1->z;
+
+       d0 = distance_signed(pos0, plane);
+       d1 = distance_signed(pos1, plane);
+
+       if(d0 > 0.0f && d1 > 0.0f) {
+               return 1;
+       }
+       if(d0 < 0.0f && d1 < 0.0f) {
+               return -1;
+       }
+       return 0;
+}
 
 static float distance_signed(float *pos, const struct cplane *plane)
 {
index 0b460cf..adee29d 100644 (file)
@@ -25,6 +25,12 @@ enum {
 int clip_poly(struct g3d_vertex *vout, int *voutnum,
                const struct g3d_vertex *vin, int vnum, struct cplane *plane);
 
+/* only checks if the polygon would be clipped by the plane, and classifies it
+ * as inside/outside/straddling, without actually producing a clipped polygon.
+ * return values are the same as clip_poly.
+ */
+int check_clip_poly(const struct g3d_vertex *v, int vnum, struct cplane *plane);
+
 /* Special-case frustum clipper (might be slightly faster) */
 int clip_frustum(struct g3d_vertex *vout, int *voutnum,
                const struct g3d_vertex *vin, int vnum, int fplane);
index bbc1c0b..41442d5 100644 (file)
@@ -35,7 +35,7 @@ static struct bsptree torus_bsp;
 
 static struct pimage tex;
 
-static int use_bsp = 1;
+static int use_bsp = 0;
 
 #define LOWRES_SCALE   10
 static uint16_t *lowres_pixels;
@@ -58,11 +58,13 @@ 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);
 
@@ -82,7 +84,9 @@ static void destroy(void)
        free(cube.varr);
        free(torus.varr);
        free(torus.iarr);
+       /*
        destroy_bsp(&torus_bsp);
+       */
 }
 
 static void start(long trans_time)