]> icculus.org git repositories - btb/d2x.git/blob - 3d/clipper.c
Display results of configure
[btb/d2x.git] / 3d / clipper.c
1 /*
2 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
3 SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
4 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
5 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
6 IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
7 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
8 FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
9 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
10 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.  
11 COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
12 */
13
14 #ifdef HAVE_CONFIG_H
15 #include <conf.h>
16 #endif
17
18 #include "3d.h"
19 #include "globvars.h"
20 #include "clipper.h"
21 #include "error.h"
22
23 int free_point_num=0;
24
25 g3s_point temp_points[MAX_POINTS_IN_POLY];
26 g3s_point *free_points[MAX_POINTS_IN_POLY];
27
28 void init_free_points(void)
29 {
30         int i;
31
32         for (i=0;i<MAX_POINTS_IN_POLY;i++)
33                 free_points[i] = &temp_points[i];
34 }
35
36
37 g3s_point *get_temp_point()
38 {
39         g3s_point *p;
40
41         Assert (free_point_num < MAX_POINTS_IN_POLY );
42         p = free_points[free_point_num++];
43
44         p->p3_flags = PF_TEMP_POINT;
45
46         return p;
47 }
48
49 void free_temp_point(g3s_point *p)
50 {
51         Assert(p->p3_flags & PF_TEMP_POINT);
52
53         free_points[--free_point_num] = p;
54
55         p->p3_flags &= ~PF_TEMP_POINT;
56 }
57
58 //clips an edge against one plane. 
59 g3s_point *clip_edge(int plane_flag,g3s_point *on_pnt,g3s_point *off_pnt)
60 {
61         fix psx_ratio;
62         fix a,b,kn,kd;
63         g3s_point *tmp;
64
65         //compute clipping value k = (xs-zs) / (xs-xe-zs+ze)
66         //use x or y as appropriate, and negate x/y value as appropriate
67
68         if (plane_flag & (CC_OFF_RIGHT | CC_OFF_LEFT)) {
69                 a = on_pnt->p3_x;
70                 b = off_pnt->p3_x;
71         }
72         else {
73                 a = on_pnt->p3_y;
74                 b = off_pnt->p3_y;
75         }
76
77         if (plane_flag & (CC_OFF_LEFT | CC_OFF_BOT)) {
78                 a = -a;
79                 b = -b;
80         }
81
82         kn = a - on_pnt->p3_z;                                          //xs-zs
83         kd = kn - b + off_pnt->p3_z;                            //xs-zs-xe+ze
84
85         tmp = get_temp_point();
86
87         psx_ratio = fixdiv( kn, kd );
88
89         
90 // PSX_HACK!!!!
91 //      tmp->p3_x = on_pnt->p3_x + fixmuldiv(off_pnt->p3_x-on_pnt->p3_x,kn,kd);
92 //      tmp->p3_y = on_pnt->p3_y + fixmuldiv(off_pnt->p3_y-on_pnt->p3_y,kn,kd);
93
94         tmp->p3_x = on_pnt->p3_x + fixmul( (off_pnt->p3_x-on_pnt->p3_x), psx_ratio);
95         tmp->p3_y = on_pnt->p3_y + fixmul( (off_pnt->p3_y-on_pnt->p3_y), psx_ratio);
96
97         if (plane_flag & (CC_OFF_TOP|CC_OFF_BOT))
98                 tmp->p3_z = tmp->p3_y;
99         else
100                 tmp->p3_z = tmp->p3_x;
101
102         if (plane_flag & (CC_OFF_LEFT|CC_OFF_BOT))
103                 tmp->p3_z = -tmp->p3_z;
104
105         if (on_pnt->p3_flags & PF_UVS) {
106 // PSX_HACK!!!!
107 //              tmp->p3_u = on_pnt->p3_u + fixmuldiv(off_pnt->p3_u-on_pnt->p3_u,kn,kd);
108 //              tmp->p3_v = on_pnt->p3_v + fixmuldiv(off_pnt->p3_v-on_pnt->p3_v,kn,kd);
109                 tmp->p3_u = on_pnt->p3_u + fixmul((off_pnt->p3_u-on_pnt->p3_u), psx_ratio);
110                 tmp->p3_v = on_pnt->p3_v + fixmul((off_pnt->p3_v-on_pnt->p3_v), psx_ratio);
111
112                 tmp->p3_flags |= PF_UVS;
113         }
114
115         if (on_pnt->p3_flags & PF_LS) {
116 // PSX_HACK
117 //              tmp->p3_r = on_pnt->p3_r + fixmuldiv(off_pnt->p3_r-on_pnt->p3_r,kn,kd);
118 //              tmp->p3_g = on_pnt->p3_g + fixmuldiv(off_pnt->p3_g-on_pnt->p3_g,kn,kd);
119 //              tmp->p3_b = on_pnt->p3_b + fixmuldiv(off_pnt->p3_b-on_pnt->p3_b,kn,kd);
120
121                 tmp->p3_l = on_pnt->p3_l + fixmul((off_pnt->p3_l-on_pnt->p3_l), psx_ratio);
122
123                 tmp->p3_flags |= PF_LS;
124         }
125
126         g3_code_point(tmp);
127
128         return tmp;     
129 }
130
131 //clips a line to the viewing pyramid.
132 void clip_line(g3s_point **p0,g3s_point **p1,ubyte codes_or)
133 {
134         int plane_flag;
135         g3s_point *old_p1;
136
137         //might have these left over
138         (*p0)->p3_flags &= ~(PF_UVS|PF_LS);
139         (*p1)->p3_flags &= ~(PF_UVS|PF_LS);
140
141         for (plane_flag=1;plane_flag<16;plane_flag<<=1)
142                 if (codes_or & plane_flag) {
143
144                         if ((*p0)->p3_codes & plane_flag)
145                                 {g3s_point *t=*p0; *p0=*p1; *p1=t;}     //swap!
146
147                         old_p1 = *p1;
148
149                         *p1 = clip_edge(plane_flag,*p0,*p1);
150
151                         if (old_p1->p3_flags & PF_TEMP_POINT)
152                                 free_temp_point(old_p1);
153                 }
154
155 }
156
157
158 int clip_plane(int plane_flag,g3s_point **src,g3s_point **dest,int *nv,g3s_codes *cc)
159 {
160         int i;
161         g3s_point **save_dest=dest;
162
163         //copy first two verts to end
164         src[*nv] = src[0];
165         src[*nv+1] = src[1];
166
167         cc->and = 0xff; cc->or = 0;
168
169         for (i=1;i<=*nv;i++) {
170
171                 if (src[i]->p3_codes & plane_flag) {                            //cur point off?
172
173                         if (! (src[i-1]->p3_codes & plane_flag)) {      //prev not off?
174
175                                 *dest = clip_edge(plane_flag,src[i-1],src[i]);
176                                 cc->or  |= (*dest)->p3_codes;
177                                 cc->and &= (*dest)->p3_codes;
178                                 dest++;
179                         }
180
181                         if (! (src[i+1]->p3_codes & plane_flag)) {
182
183                                 *dest = clip_edge(plane_flag,src[i+1],src[i]);
184                                 cc->or  |= (*dest)->p3_codes;
185                                 cc->and &= (*dest)->p3_codes;
186                                 dest++;
187                         }
188
189                         //see if must free discarded point
190
191                         if (src[i]->p3_flags & PF_TEMP_POINT)
192                                 free_temp_point(src[i]);
193                 }
194                 else {                  //cur not off, copy to dest buffer
195
196                         *dest++ = src[i];
197
198                         cc->or  |= src[i]->p3_codes;
199                         cc->and &= src[i]->p3_codes;
200                 }
201         }
202
203         return (dest-save_dest);
204 }
205
206
207 g3s_point **clip_polygon(g3s_point **src,g3s_point **dest,int *nv,g3s_codes *cc)
208 {
209         int plane_flag;
210         g3s_point **t;
211
212         for (plane_flag=1;plane_flag<16;plane_flag<<=1)
213
214                 if (cc->or & plane_flag) {
215
216                         *nv = clip_plane(plane_flag,src,dest,nv,cc);
217
218                         if (cc->and)            //clipped away
219                                 return dest;
220
221                         t = src; src = dest; dest = t;
222
223                 }
224
225         return src;             //we swapped after we copied
226 }
227