X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?a=blobdiff_plain;f=src%2Fgeom.c;h=25c4d0afcd888786cacff9e01ccaeee7a42c21cb;hb=f1410762198ad4b0ca8c0b47bd484ff2d3e09ddd;hp=81e9df4117d181cd2263e0fc9aa42304077d8d13;hpb=bf7eceb7d1cef825a64a1db7a20628f7d7005654;p=meshfrac diff --git a/src/geom.c b/src/geom.c index 81e9df4..25c4d0a 100644 --- a/src/geom.c +++ b/src/geom.c @@ -1,4 +1,5 @@ #include +#include #include "geom.h" #include "dynarr.h" @@ -31,7 +32,7 @@ void poly_normal(const struct poly *poly, cgm_vec3 *n) va = poly->verts[1].pos; cgm_vsub(&va, &poly->verts[0].pos); vb = poly->verts[2].pos; - cgm_vsub(&vb, &poly->verts[2].pos); + cgm_vsub(&vb, &poly->verts[0].pos); cgm_vcross(n, &va, &vb); cgm_vnormalize(n); @@ -130,6 +131,17 @@ float ray_poly(const cgm_ray *ray, const struct poly *poly) return t; } +int init_poly(struct poly *p) +{ + p->verts = dynarr_alloc(0, sizeof *p->verts); + assert(p->verts); + return p->verts ? 0 : -1; +} + +void destroy_poly(struct poly *p) +{ + dynarr_free(p->verts); +} /* returns: * 1 -> both inside @@ -196,7 +208,7 @@ int clip_poly(struct poly *pout, const struct poly *pin, const struct plane *pla int i, nextidx, res = 0, vnum; int edges_clipped = 0; - dynarr_clear(pout->verts); + DYNARR_CLEAR(pout->verts); vnum = dynarr_size(pin->verts); for(i=0; i 0 ? 0 : 1; } + +int poly_poly(const struct poly *p1, const struct poly *p2) +{ + int i, vnum1, vnum2; + float t; + cgm_ray ray; + + vnum1 = dynarr_size(p1->verts); + vnum2 = dynarr_size(p2->verts); + + for(i=0; iverts[i].pos; + ray.dir = p1->verts[(i + 1) & vnum1].pos; + cgm_vsub(&ray.dir, &ray.origin); + + if((t = ray_poly(&ray, p2)) >= 0.0f && t <= 1.0f) { + return 1; + } + } + + for(i=0; iverts[i].pos; + ray.dir = p2->verts[(i + 1) & vnum2].pos; + cgm_vsub(&ray.dir, &ray.origin); + + if((t = ray_poly(&ray, p1)) >= 0.0f && t <= 1.0f) { + return 1; + } + } + + return 0; +}