1 | /********************************************************************** <BR>
|
---|
2 | This file is part of Crack dot Com's free source code release of
|
---|
3 | Golgotha. <a href="http://www.crack.com/golgotha_release"> <BR> for
|
---|
4 | information about compiling & licensing issues visit this URL</a>
|
---|
5 | <PRE> If that doesn't help, contact Jonathan Clark at
|
---|
6 | golgotha_source@usa.net (Subject should have "GOLG" in it)
|
---|
7 | ***********************************************************************/
|
---|
8 |
|
---|
9 | #ifndef I4_POLY_CLIP_HH
|
---|
10 | #define I4_POLY_CLIP_HH
|
---|
11 |
|
---|
12 | #include "poly/poly.hh"
|
---|
13 |
|
---|
14 | enum { I4_CLIP_PLANE_Z1=32,
|
---|
15 | I4_CLIP_PLANE_Z2=16,
|
---|
16 | I4_CLIP_PLANE_Y1=8,
|
---|
17 | I4_CLIP_PLANE_Y2=4,
|
---|
18 | I4_CLIP_PLANE_X1=2,
|
---|
19 | I4_CLIP_PLANE_X2=1 };
|
---|
20 |
|
---|
21 |
|
---|
22 | static inline int i4_clip_get_side_on(i4_float x1, i4_float y1, i4_float z1,
|
---|
23 | i4_float x2, i4_float y2, i4_float z2,
|
---|
24 | i4_vertex_class *v,
|
---|
25 | int plane)
|
---|
26 | {
|
---|
27 | switch (plane)
|
---|
28 | {
|
---|
29 | case I4_CLIP_PLANE_Z1 : return v->v.z>z1;
|
---|
30 | case I4_CLIP_PLANE_Z2 : return v->v.z<z2;
|
---|
31 | case I4_CLIP_PLANE_Y1 : return v->py>y1;
|
---|
32 | case I4_CLIP_PLANE_Y2 : return v->py<y2;
|
---|
33 | case I4_CLIP_PLANE_X1 : return v->px>x1;
|
---|
34 | case I4_CLIP_PLANE_X2 : return v->px<x2;
|
---|
35 | }
|
---|
36 | return 0;
|
---|
37 | }
|
---|
38 |
|
---|
39 |
|
---|
40 |
|
---|
41 | template <class class_with_intersect_and_project_functions>
|
---|
42 | void i4_poly_clip(class_with_intersect_and_project_functions &cwipf,
|
---|
43 | i4_polygon_class &poly,
|
---|
44 | i4_polygon_class &output_poly,
|
---|
45 | i4_float x1, i4_float y1, i4_float z1,
|
---|
46 | i4_float x2, i4_float y2, i4_float z2)
|
---|
47 | {
|
---|
48 | i4_polygon_class other, *p1, *p2, *swap;
|
---|
49 | p1=&poly; p2=&other;
|
---|
50 |
|
---|
51 | int prev, cur, prev_side_on, cur_side_on;
|
---|
52 |
|
---|
53 | w32 plane=I4_CLIP_PLANE_Z1;
|
---|
54 | while (plane)
|
---|
55 | {
|
---|
56 | p2->t_verts=0;
|
---|
57 | prev=p1->t_verts-1;
|
---|
58 | prev_side_on=i4_clip_get_side_on(x1, y1, z1, x2, y2, z2, p1->vert+prev, plane);
|
---|
59 |
|
---|
60 | for (cur=0; cur<p1->t_verts; cur++)
|
---|
61 | {
|
---|
62 | i4_vertex_class *v2=p1->vert+cur, *o;
|
---|
63 | i4_vertex_class *v1=p1->vert+prev;
|
---|
64 |
|
---|
65 | cur_side_on=i4_clip_get_side_on(x1, y1, z1, x2, y2, z2, p1->vert+cur, plane);
|
---|
66 |
|
---|
67 | if (prev_side_on ^ cur_side_on) // are they on different sides?
|
---|
68 | {
|
---|
69 | i4_float t=cwipf.intersect(v1, v2, plane);
|
---|
70 | if (t<0) t=0;
|
---|
71 | else if (t>1) t=1;
|
---|
72 | o=p2->add_vert(i4_3d_point_class(v1->v.x + (v2->v.x-v1->v.x)*t,
|
---|
73 | v1->v.y + (v2->v.y-v1->v.y)*t,
|
---|
74 | v1->v.z + (v2->v.z-v1->v.z)*t),
|
---|
75 | v1->s + (v2->s-v1->s)*t,
|
---|
76 | v1->t + (v2->t-v1->t)*t,
|
---|
77 | v1->r + (v2->r-v1->r)*t,
|
---|
78 | v1->g + (v2->g-v1->g)*t,
|
---|
79 | v1->b + (v2->b-v1->b)*t);
|
---|
80 |
|
---|
81 | cwipf.project(o); // reproject the new vertex, it's v.x & v.y are different
|
---|
82 | }
|
---|
83 |
|
---|
84 | if (cur_side_on)
|
---|
85 | {
|
---|
86 | o=p2->add_vert(v2->v, v2->s, v2->t, v2->r, v2->g, v2->b);
|
---|
87 | o->px=v2->px;
|
---|
88 | o->py=v2->py; // copy the projected x & y
|
---|
89 | }
|
---|
90 |
|
---|
91 | prev=cur;
|
---|
92 | prev_side_on=cur_side_on;
|
---|
93 | }
|
---|
94 |
|
---|
95 | plane=plane>>1;
|
---|
96 |
|
---|
97 | if (p2==&other)
|
---|
98 | {
|
---|
99 | p2=&output_poly;
|
---|
100 | p1=&other;
|
---|
101 | }
|
---|
102 | else
|
---|
103 | {
|
---|
104 | p1=&output_poly;
|
---|
105 | p2=&other;
|
---|
106 | }
|
---|
107 | }
|
---|
108 | }
|
---|
109 |
|
---|
110 |
|
---|
111 | #endif
|
---|