10 static int clip_edge(struct g3d_vertex *poly, int *vnumptr,
11 const struct g3d_vertex *v0, const struct g3d_vertex *v1,
12 const struct cplane *plane);
13 static int clip_edge_frustum(struct g3d_vertex *poly, int *vnumptr,
14 const struct g3d_vertex *v0, const struct g3d_vertex *v1, int fplane);
15 static float distance_signed(float *pos, const struct cplane *plane);
16 static int intersect(const struct ray *ray, const struct cplane *plane, float *t);
19 int clip_poly(struct g3d_vertex *vout, int *voutnum,
20 const struct g3d_vertex *vin, int vnum, struct cplane *plane)
23 int edges_clipped = 0;
26 for(i=0; i<vnum; i++) {
27 int res = clip_edge(vout, &out_vnum, vin + i, vin + (i + 1) % vnum, plane);
34 assert(edges_clipped == 0);
39 return edges_clipped > 0 ? 0 : 1;
43 int clip_frustum(struct g3d_vertex *vout, int *voutnum,
44 const struct g3d_vertex *vin, int vnum, int fplane)
47 int edges_clipped = 0;
50 for(i=0; i<vnum; i++) {
51 int res = clip_edge_frustum(vout, &out_vnum, vin + i, vin + (i + 1) % vnum, fplane);
58 assert(edges_clipped == 0);
63 return edges_clipped > 0 ? 0 : 1;
66 #define LERP_VATTR(res, v0, v1, t) \
68 (res)->nx = (v0)->nx + ((v1)->nx - (v0)->nx) * (t); \
69 (res)->ny = (v0)->ny + ((v1)->ny - (v0)->ny) * (t); \
70 (res)->nz = (v0)->nz + ((v1)->nz - (v0)->nz) * (t); \
71 (res)->u = (v0)->u + ((v1)->u - (v0)->u) * (t); \
72 (res)->v = (v0)->v + ((v1)->v - (v0)->v) * (t); \
73 (res)->r = (v0)->r + ((v1)->r - (v0)->r) * (t); \
74 (res)->g = (v0)->g + ((v1)->g - (v0)->g) * (t); \
75 (res)->b = (v0)->b + ((v1)->b - (v0)->b) * (t); \
81 * 0 -> straddling and clipped
84 * also returns the size of the polygon through vnumptr
86 static int clip_edge(struct g3d_vertex *poly, int *vnumptr,
87 const struct g3d_vertex *v0, const struct g3d_vertex *v1,
88 const struct cplane *plane)
90 float pos0[3], pos1[3];
93 int i, vnum = *vnumptr;
95 pos0[0] = v0->x; pos0[1] = v0->y; pos0[2] = v0->z;
96 pos1[0] = v1->x; pos1[1] = v1->y; pos1[2] = v1->z;
98 d0 = distance_signed(pos0, plane);
99 d1 = distance_signed(pos1, plane);
102 ray.origin[i] = pos0[i];
103 ray.dir[i] = pos1[i] - pos0[i];
110 poly[vnum++] = *v1; /* append v1 */
115 struct g3d_vertex *vptr = poly + vnum;
117 intersect(&ray, plane, &t);
119 vptr->x = ray.origin[0] + ray.dir[0] * t;
120 vptr->y = ray.origin[1] + ray.dir[1] * t;
121 vptr->z = ray.origin[2] + ray.dir[2] * t;
124 LERP_VATTR(vptr, v0, v1, t);
125 vnum++; /* append new vertex on the intersection point */
131 struct g3d_vertex *vptr = poly + vnum;
133 intersect(&ray, plane, &t);
135 vptr->x = ray.origin[0] + ray.dir[0] + t;
136 vptr->y = ray.origin[1] + ray.dir[1] + t;
137 vptr->z = ray.origin[2] + ray.dir[2] + t;
140 LERP_VATTR(vptr, v0, v1, t);
141 vnum++; /* append new vertex on the intersection point */
143 /* then append v1 ... */
156 static float distance_signed(float *pos, const struct cplane *plane)
158 float dx = pos[0] - plane->x;
159 float dy = pos[1] - plane->y;
160 float dz = pos[2] - plane->z;
161 return dx * plane->nx + dy * plane->ny + dz * plane->nz;
164 static int intersect(const struct ray *ray, const struct cplane *plane, float *t)
166 float orig_pt_dir[3];
168 float ndotdir = plane->nx * ray->dir[0] + plane->ny * ray->dir[1] + plane->nz * ray->dir[2];
169 if(fabs(ndotdir) < 1e-4) {
174 orig_pt_dir[0] = plane->x - ray->origin[0];
175 orig_pt_dir[1] = plane->y - ray->origin[1];
176 orig_pt_dir[2] = plane->z - ray->origin[2];
178 *t = (plane->nx * orig_pt_dir[0] + plane->ny * orig_pt_dir[1] + plane->nz * orig_pt_dir[2]) / ndotdir;
182 /* homogeneous frustum clipper helpers */
184 static int inside_frustum_plane(const struct g3d_vertex *v, int fplane)
188 return v->x >= -v->w;
192 return v->y >= -v->w;
196 return v->z >= -v->w;
204 static float intersect_frustum(const struct g3d_vertex *a, const struct g3d_vertex *b, int fplane)
208 return (-a->w - a->x) / (b->x - a->x + b->w - a->w);
210 return (a->w - a->x) / (b->x - a->x - b->w + a->w);
212 return (-a->w - a->y) / (b->y - a->y + b->w - a->w);
214 return (a->w - a->y) / (b->y - a->y - b->w + a->w);
216 return (-a->w - a->z) / (b->z - a->z + b->w - a->w);
218 return (a->w - a->z) / (b->z - a->z - b->w + a->w);
225 static int clip_edge_frustum(struct g3d_vertex *poly, int *vnumptr,
226 const struct g3d_vertex *v0, const struct g3d_vertex *v1, int fplane)
232 in0 = inside_frustum_plane(v0, fplane);
233 in1 = inside_frustum_plane(v1, fplane);
239 poly[vnum++] = *v1; /* append v1 */
244 struct g3d_vertex *vptr = poly + vnum;
246 t = intersect_frustum(v0, v1, fplane);
248 vptr->x = v0->x + (v1->x - v0->x) * t;
249 vptr->y = v0->y + (v1->y - v0->y) * t;
250 vptr->z = v0->z + (v1->z - v0->z) * t;
251 vptr->w = v0->w + (v1->w - v0->w) * t;
253 LERP_VATTR(vptr, v0, v1, t);
254 ++vnum; /* append new vertex on the intersection point */
260 struct g3d_vertex *vptr = poly + vnum;
262 t = intersect_frustum(v0, v1, fplane);
264 vptr->x = v0->x + (v1->x - v0->x) * t;
265 vptr->y = v0->y + (v1->y - v0->y) * t;
266 vptr->z = v0->z + (v1->z - v0->z) * t;
267 vptr->w = v0->w + (v1->w - v0->w) * t;
269 LERP_VATTR(vptr, v0, v1, t);
270 ++vnum; /* append new vertex on the intersection point */
272 /* then append v1 ... */