11 static int clip_edge(struct vertex *poly, int *vnumptr,
12 const struct vertex *v0, const struct vertex *v1,
13 const struct cplane *plane);
14 static int check_clip_edge(const struct vertex *v0,
15 const struct vertex *v1, const struct cplane *plane);
16 static int clip_edge_frustum(struct vertex *poly, int *vnumptr,
17 const struct vertex *v0, const struct vertex *v1, int fplane);
18 static float distance_signed(float *pos, const struct cplane *plane);
19 static int intersect(const struct ray *ray, const struct cplane *plane, float *t);
20 static int inside_frustum_plane(const struct vertex *v, int fplane);
23 int clip_poly(struct vertex *vout, int *voutnum,
24 const struct vertex *vin, int vnum, struct cplane *plane)
27 int edges_clipped = 0;
31 for(i=0; i<vnum; i++) {
33 if(nextidx >= vnum) nextidx = 0;
34 res = clip_edge(vout, voutnum, vin + i, vin + nextidx, plane);
41 assert(edges_clipped == 0);
45 return edges_clipped > 0 ? 0 : 1;
48 int check_clip_poly(const struct vertex *v, int vnum, struct cplane *plane)
50 int i, nextidx, res = 0;
51 int edges_clipped = 0;
53 for(i=0; i<vnum; i++) {
55 if(nextidx >= vnum) nextidx = 0;
56 res = check_clip_edge(v + i, v + nextidx, plane);
61 return edges_clipped ? 0 : res;
64 int clip_frustum(struct vertex *vout, int *voutnum,
65 const struct vertex *vin, int vnum, int fplane)
68 int edges_clipped = 0;
71 /* special case: point clipping */
72 return inside_frustum_plane(vin, fplane) ? 1 : -1;
77 for(i=0; i<vnum; i++) {
79 if(nextidx >= vnum) nextidx = 0;
80 res = clip_edge_frustum(vout, voutnum, vin + i, vin + nextidx, fplane);
87 assert(edges_clipped == 0);
91 return edges_clipped > 0 ? 0 : 1;
94 #define LERP_VATTR(res, v0, v1, t) \
96 (res)->nx = (v0)->nx + ((v1)->nx - (v0)->nx) * (t); \
97 (res)->ny = (v0)->ny + ((v1)->ny - (v0)->ny) * (t); \
98 (res)->nz = (v0)->nz + ((v1)->nz - (v0)->nz) * (t); \
99 (res)->u = (v0)->u + ((v1)->u - (v0)->u) * (t); \
100 (res)->v = (v0)->v + ((v1)->v - (v0)->v) * (t); \
101 (res)->r = (v0)->r + ((v1)->r - (v0)->r) * (t); \
102 (res)->g = (v0)->g + ((v1)->g - (v0)->g) * (t); \
103 (res)->b = (v0)->b + ((v1)->b - (v0)->b) * (t); \
109 * 0 -> straddling and clipped
112 * also returns the size of the polygon through vnumptr
114 static int clip_edge(struct vertex *poly, int *vnumptr,
115 const struct vertex *v0, const struct vertex *v1,
116 const struct cplane *plane)
118 float pos0[3], pos1[3];
121 int i, vnum = *vnumptr;
123 pos0[0] = v0->x; pos0[1] = v0->y; pos0[2] = v0->z;
124 pos1[0] = v1->x; pos1[1] = v1->y; pos1[2] = v1->z;
126 d0 = distance_signed(pos0, plane);
127 d1 = distance_signed(pos1, plane);
130 ray.origin[i] = pos0[i];
131 ray.dir[i] = pos1[i] - pos0[i];
138 poly[vnum++] = *v1; /* append v1 */
143 struct vertex *vptr = poly + vnum;
145 intersect(&ray, plane, &t);
147 vptr->x = ray.origin[0] + ray.dir[0] * t;
148 vptr->y = ray.origin[1] + ray.dir[1] * t;
149 vptr->z = ray.origin[2] + ray.dir[2] * t;
152 LERP_VATTR(vptr, v0, v1, t);
153 vnum++; /* append new vertex on the intersection point */
159 struct vertex *vptr = poly + vnum;
161 intersect(&ray, plane, &t);
163 vptr->x = ray.origin[0] + ray.dir[0] * t;
164 vptr->y = ray.origin[1] + ray.dir[1] * t;
165 vptr->z = ray.origin[2] + ray.dir[2] * t;
168 LERP_VATTR(vptr, v0, v1, t);
169 vnum++; /* append new vertex on the intersection point */
171 /* then append v1 ... */
183 /* same as above, but only checks for clipping and classifies the edge */
184 static int check_clip_edge(const struct vertex *v0,
185 const struct vertex *v1, const struct cplane *plane)
187 float pos0[3], pos1[3];
190 pos0[0] = v0->x; pos0[1] = v0->y; pos0[2] = v0->z;
191 pos1[0] = v1->x; pos1[1] = v1->y; pos1[2] = v1->z;
193 d0 = distance_signed(pos0, plane);
194 d1 = distance_signed(pos1, plane);
196 if(d0 > 0.0f && d1 > 0.0f) {
199 if(d0 < 0.0f && d1 < 0.0f) {
205 static float distance_signed(float *pos, const struct cplane *plane)
207 float dx = pos[0] - plane->x;
208 float dy = pos[1] - plane->y;
209 float dz = pos[2] - plane->z;
210 return dx * plane->nx + dy * plane->ny + dz * plane->nz;
213 static int intersect(const struct ray *ray, const struct cplane *plane, float *t)
215 float orig_pt_dir[3];
217 float ndotdir = plane->nx * ray->dir[0] + plane->ny * ray->dir[1] + plane->nz * ray->dir[2];
218 if(fabs(ndotdir) < 1e-6) {
223 orig_pt_dir[0] = plane->x - ray->origin[0];
224 orig_pt_dir[1] = plane->y - ray->origin[1];
225 orig_pt_dir[2] = plane->z - ray->origin[2];
227 *t = (plane->nx * orig_pt_dir[0] + plane->ny * orig_pt_dir[1] + plane->nz * orig_pt_dir[2]) / ndotdir;
231 /* homogeneous frustum clipper helpers */
233 static int inside_frustum_plane(const struct vertex *v, int fplane)
237 return v->x >= -v->w;
241 return v->y >= -v->w;
245 return v->z >= -v->w;
253 static float intersect_frustum(const struct vertex *a, const struct vertex *b, int fplane)
257 return (-a->w - a->x) / (b->x - a->x + b->w - a->w);
259 return (a->w - a->x) / (b->x - a->x - b->w + a->w);
261 return (-a->w - a->y) / (b->y - a->y + b->w - a->w);
263 return (a->w - a->y) / (b->y - a->y - b->w + a->w);
265 return (-a->w - a->z) / (b->z - a->z + b->w - a->w);
267 return (a->w - a->z) / (b->z - a->z - b->w + a->w);
274 static int clip_edge_frustum(struct vertex *poly, int *vnumptr,
275 const struct vertex *v0, const struct vertex *v1, int fplane)
281 in0 = inside_frustum_plane(v0, fplane);
282 in1 = inside_frustum_plane(v1, fplane);
288 poly[vnum++] = *v1; /* append v1 */
293 struct vertex *vptr = poly + vnum;
295 t = intersect_frustum(v0, v1, fplane);
297 vptr->x = v0->x + (v1->x - v0->x) * t;
298 vptr->y = v0->y + (v1->y - v0->y) * t;
299 vptr->z = v0->z + (v1->z - v0->z) * t;
300 vptr->w = v0->w + (v1->w - v0->w) * t;
302 LERP_VATTR(vptr, v0, v1, t);
303 ++vnum; /* append new vertex on the intersection point */
309 struct vertex *vptr = poly + vnum;
311 t = intersect_frustum(v0, v1, fplane);
313 vptr->x = v0->x + (v1->x - v0->x) * t;
314 vptr->y = v0->y + (v1->y - v0->y) * t;
315 vptr->z = v0->z + (v1->z - v0->z) * t;
316 vptr->w = v0->w + (v1->w - v0->w) * t;
318 LERP_VATTR(vptr, v0, v1, t);
319 ++vnum; /* append new vertex on the intersection point */
321 /* then append v1 ... */