X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?p=dosdemo;a=blobdiff_plain;f=src%2Fpolyclip.c;h=35e6cd62a0fedf07263adcfc8fadae41b14ec1d7;hp=d02f645cd81540416a3725b5e264a3aeffbad73e;hb=d956a9d9273eebfacfda58cb3bafff017269d5dc;hpb=9b942b99fe10222bee50c47bfc809de268d331ec diff --git a/src/polyclip.c b/src/polyclip.c index d02f645..35e6cd6 100644 --- a/src/polyclip.c +++ b/src/polyclip.c @@ -1,3 +1,4 @@ +#include #include #include #include "polyclip.h" @@ -10,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); @@ -20,52 +23,71 @@ static int inside_frustum_plane(const struct g3d_vertex *v, int fplane); int clip_poly(struct g3d_vertex *vout, int *voutnum, const struct g3d_vertex *vin, int vnum, struct cplane *plane) { - int i; + int i, nextidx, res; int edges_clipped = 0; - int out_vnum = 0; + + *voutnum = 0; for(i=0; i= vnum) nextidx = 0; + res = clip_edge(vout, voutnum, vin + i, vin + nextidx, plane); if(res == 0) { ++edges_clipped; } } - if(out_vnum <= 0) { + if(*voutnum <= 0) { assert(edges_clipped == 0); return -1; } - *voutnum = out_vnum; 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) 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) { - int i; + int i, nextidx, res; int edges_clipped = 0; - int out_vnum = 0; if(vnum == 1) { /* special case: point clipping */ return inside_frustum_plane(vin, fplane) ? 1 : -1; } + *voutnum = 0; + for(i=0; i= vnum) nextidx = 0; + res = clip_edge_frustum(vout, voutnum, vin + i, vin + nextidx, fplane); if(res == 0) { ++edges_clipped; } } - if(out_vnum <= 0) { + if(*voutnum <= 0) { assert(edges_clipped == 0); return -1; } - *voutnum = out_vnum; return edges_clipped > 0 ? 0 : 1; } @@ -138,9 +160,9 @@ static int clip_edge(struct g3d_vertex *poly, int *vnumptr, intersect(&ray, plane, &t); - vptr->x = ray.origin[0] + ray.dir[0] + t; - vptr->y = ray.origin[1] + ray.dir[1] + t; - vptr->z = ray.origin[2] + ray.dir[2] + t; + vptr->x = ray.origin[0] + ray.dir[0] * t; + vptr->y = ray.origin[1] + ray.dir[1] * t; + vptr->z = ray.origin[2] + ray.dir[2] * t; vptr->w = 1.0f; LERP_VATTR(vptr, v0, v1, t); @@ -158,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) { @@ -172,7 +215,7 @@ static int intersect(const struct ray *ray, const struct cplane *plane, float *t float orig_pt_dir[3]; float ndotdir = plane->nx * ray->dir[0] + plane->ny * ray->dir[1] + plane->nz * ray->dir[2]; - if(fabs(ndotdir) < 1e-4) { + if(fabs(ndotdir) < 1e-6) { *t = 0.0f; return 0; }