11 static int clip_edge(struct g3d_vertex *poly, int *vnumptr,
12 const struct g3d_vertex *v0, const struct g3d_vertex *v1,
13 const struct cplane *plane);
14 static int clip_edge_frustum(struct g3d_vertex *poly, int *vnumptr,
15 const struct g3d_vertex *v0, const struct g3d_vertex *v1, int fplane);
16 static float distance_signed(float *pos, const struct cplane *plane);
17 static int intersect(const struct ray *ray, const struct cplane *plane, float *t);
18 static int inside_frustum_plane(const struct g3d_vertex *v, int fplane);
21 int clip_poly(struct g3d_vertex *vout, int *voutnum,
22 const struct g3d_vertex *vin, int vnum, struct cplane *plane)
25 int edges_clipped = 0;
28 for(i=0; i<vnum; i++) {
30 if(nextidx >= vnum) nextidx = 0;
31 res = clip_edge(vout, &out_vnum, vin + i, vin + nextidx, plane);
38 assert(edges_clipped == 0);
43 return edges_clipped > 0 ? 0 : 1;
47 int clip_frustum(struct g3d_vertex *vout, int *voutnum,
48 const struct g3d_vertex *vin, int vnum, int fplane)
51 int edges_clipped = 0;
55 /* special case: point clipping */
56 return inside_frustum_plane(vin, fplane) ? 1 : -1;
59 for(i=0; i<vnum; i++) {
61 if(nextidx >= vnum) nextidx = 0;
62 res = clip_edge_frustum(vout, &out_vnum, vin + i, vin + nextidx, fplane);
69 assert(edges_clipped == 0);
74 return edges_clipped > 0 ? 0 : 1;
77 #define LERP_VATTR(res, v0, v1, t) \
79 (res)->nx = (v0)->nx + ((v1)->nx - (v0)->nx) * (t); \
80 (res)->ny = (v0)->ny + ((v1)->ny - (v0)->ny) * (t); \
81 (res)->nz = (v0)->nz + ((v1)->nz - (v0)->nz) * (t); \
82 (res)->u = (v0)->u + ((v1)->u - (v0)->u) * (t); \
83 (res)->v = (v0)->v + ((v1)->v - (v0)->v) * (t); \
84 (res)->r = (v0)->r + ((v1)->r - (v0)->r) * (t); \
85 (res)->g = (v0)->g + ((v1)->g - (v0)->g) * (t); \
86 (res)->b = (v0)->b + ((v1)->b - (v0)->b) * (t); \
92 * 0 -> straddling and clipped
95 * also returns the size of the polygon through vnumptr
97 static int clip_edge(struct g3d_vertex *poly, int *vnumptr,
98 const struct g3d_vertex *v0, const struct g3d_vertex *v1,
99 const struct cplane *plane)
101 float pos0[3], pos1[3];
104 int i, vnum = *vnumptr;
106 pos0[0] = v0->x; pos0[1] = v0->y; pos0[2] = v0->z;
107 pos1[0] = v1->x; pos1[1] = v1->y; pos1[2] = v1->z;
109 d0 = distance_signed(pos0, plane);
110 d1 = distance_signed(pos1, plane);
113 ray.origin[i] = pos0[i];
114 ray.dir[i] = pos1[i] - pos0[i];
121 poly[vnum++] = *v1; /* append v1 */
126 struct g3d_vertex *vptr = poly + vnum;
128 intersect(&ray, plane, &t);
130 vptr->x = ray.origin[0] + ray.dir[0] * t;
131 vptr->y = ray.origin[1] + ray.dir[1] * t;
132 vptr->z = ray.origin[2] + ray.dir[2] * t;
135 LERP_VATTR(vptr, v0, v1, t);
136 vnum++; /* append new vertex on the intersection point */
142 struct g3d_vertex *vptr = poly + vnum;
144 intersect(&ray, plane, &t);
146 vptr->x = ray.origin[0] + ray.dir[0] * t;
147 vptr->y = ray.origin[1] + ray.dir[1] * t;
148 vptr->z = ray.origin[2] + ray.dir[2] * t;
151 LERP_VATTR(vptr, v0, v1, t);
152 vnum++; /* append new vertex on the intersection point */
154 /* then append v1 ... */
167 static float distance_signed(float *pos, const struct cplane *plane)
169 float dx = pos[0] - plane->x;
170 float dy = pos[1] - plane->y;
171 float dz = pos[2] - plane->z;
172 return dx * plane->nx + dy * plane->ny + dz * plane->nz;
175 static int intersect(const struct ray *ray, const struct cplane *plane, float *t)
177 float orig_pt_dir[3];
179 float ndotdir = plane->nx * ray->dir[0] + plane->ny * ray->dir[1] + plane->nz * ray->dir[2];
180 if(fabs(ndotdir) < 1e-6) {
185 orig_pt_dir[0] = plane->x - ray->origin[0];
186 orig_pt_dir[1] = plane->y - ray->origin[1];
187 orig_pt_dir[2] = plane->z - ray->origin[2];
189 *t = (plane->nx * orig_pt_dir[0] + plane->ny * orig_pt_dir[1] + plane->nz * orig_pt_dir[2]) / ndotdir;
193 /* homogeneous frustum clipper helpers */
195 static int inside_frustum_plane(const struct g3d_vertex *v, int fplane)
199 return v->x >= -v->w;
203 return v->y >= -v->w;
207 return v->z >= -v->w;
215 static float intersect_frustum(const struct g3d_vertex *a, const struct g3d_vertex *b, int fplane)
219 return (-a->w - a->x) / (b->x - a->x + b->w - a->w);
221 return (a->w - a->x) / (b->x - a->x - b->w + a->w);
223 return (-a->w - a->y) / (b->y - a->y + b->w - a->w);
225 return (a->w - a->y) / (b->y - a->y - b->w + a->w);
227 return (-a->w - a->z) / (b->z - a->z + b->w - a->w);
229 return (a->w - a->z) / (b->z - a->z - b->w + a->w);
236 static int clip_edge_frustum(struct g3d_vertex *poly, int *vnumptr,
237 const struct g3d_vertex *v0, const struct g3d_vertex *v1, int fplane)
243 in0 = inside_frustum_plane(v0, fplane);
244 in1 = inside_frustum_plane(v1, fplane);
250 poly[vnum++] = *v1; /* append v1 */
255 struct g3d_vertex *vptr = poly + vnum;
257 t = intersect_frustum(v0, v1, fplane);
259 vptr->x = v0->x + (v1->x - v0->x) * t;
260 vptr->y = v0->y + (v1->y - v0->y) * t;
261 vptr->z = v0->z + (v1->z - v0->z) * t;
262 vptr->w = v0->w + (v1->w - v0->w) * t;
264 LERP_VATTR(vptr, v0, v1, t);
265 ++vnum; /* append new vertex on the intersection point */
271 struct g3d_vertex *vptr = poly + vnum;
273 t = intersect_frustum(v0, v1, fplane);
275 vptr->x = v0->x + (v1->x - v0->x) * t;
276 vptr->y = v0->y + (v1->y - v0->y) * t;
277 vptr->z = v0->z + (v1->z - v0->z) * t;
278 vptr->w = v0->w + (v1->w - v0->w) * t;
280 LERP_VATTR(vptr, v0, v1, t);
281 ++vnum; /* append new vertex on the intersection point */
283 /* then append v1 ... */