2 * $Logfile: /Freespace2/code/Render/3dClipper.cpp $
7 * Polygon clipping functions
10 * Revision 1.2 2002/05/03 13:34:33 theoddone33
13 * Revision 1.1.1.1 2002/05/03 03:28:10 root
17 * 2 10/07/98 10:53a Dave
20 * 1 10/07/98 10:51a Dave
22 * 13 4/10/98 5:20p John
23 * Changed RGB in lighting structure to be ubytes. Removed old
24 * not-necessary 24 bpp software stuff.
26 * 12 3/23/98 5:00p John
27 * Improved missile trails. Made smooth alpha under hardware. Made end
28 * taper. Made trail touch weapon.
30 * 11 3/16/98 4:51p John
31 * Added low-level code to clip all polygons against an arbritary plane.
32 * Took out all old model_interp_zclip and used this new method instead.
34 * 10 1/23/98 5:08p John
35 * Took L out of vertex structure used B (blue) instead. Took all small
36 * fireballs out of fireball types and used particles instead. Fixed some
37 * debris explosion things. Restructured fireball code. Restructured
38 * some lighting code. Made dynamic lighting on by default. Made groups
39 * of lasers only cast one light. Made fireballs not cast light.
41 * 9 9/09/97 3:39p Sandeep
42 * warning level 4 bugs
44 * 8 3/10/97 5:20p John
45 * Differentiated between Gouraud and Flat shading. Since we only do flat
46 * shading as of now, we don't need to interpolate L in the outer loop.
47 * This should save a few percent.
49 * 7 3/10/97 2:25p John
50 * Made pofview zbuffer. Made textest work with new model code. Took
51 * out some unnecessary Asserts in the 3d clipper.
54 * 6 12/23/96 11:00a John
55 * Restructured POF stuff to support LOD in one pof.
57 * 5 11/06/96 2:33p John
58 * Added more asserts for checking that non-tiled UV's are between 0 and
59 * 1.0. Put code in the model_init code that checks for polys that have
60 * a vertex duplicated and throws them out.
65 #include "3dinternal.h"
70 vertex temp_points[TMAP_MAX_VERTS];
71 vertex *free_points[TMAP_MAX_VERTS];
73 void init_free_points(void)
77 for (i=0;i<TMAP_MAX_VERTS;i++)
78 free_points[i] = &temp_points[i];
82 vertex *get_temp_point()
86 p = free_points[free_point_num++];
88 p->flags = PF_TEMP_POINT;
93 void free_temp_point(vertex *p)
95 Assert(p->flags & PF_TEMP_POINT);
97 free_points[--free_point_num] = p;
99 p->flags &= ~PF_TEMP_POINT;
102 //clips an edge against one plane.
103 vertex *clip_edge(int plane_flag,vertex *on_pnt,vertex *off_pnt, uint flags)
108 tmp = get_temp_point();
110 if ( plane_flag & CC_OFF_USER ) {
112 // Clip with user-defined plane
113 vector w, ray_direction;
116 vm_vec_sub(&ray_direction,(vector *)&off_pnt->x,(vector *)&on_pnt->x);
118 vm_vec_sub(&w,(vector *)&on_pnt->x,&G3_user_clip_point);
120 den = -vm_vec_dot(&G3_user_clip_normal,&ray_direction);
121 if ( den == 0.0f ) { // Ray & plane are parallel, so there is no intersection
125 num = vm_vec_dot(&G3_user_clip_normal,&w);
130 tmp->x = on_pnt->x + (off_pnt->x-on_pnt->x) * ratio;
131 tmp->y = on_pnt->y + (off_pnt->y-on_pnt->y) * ratio;
132 tmp->z = on_pnt->z + (off_pnt->z-on_pnt->z) * ratio;
137 //compute clipping value k = (xs-zs) / (xs-xe-zs+ze)
138 //use x or y as appropriate, and negate x/y value as appropriate
140 if (plane_flag & (CC_OFF_RIGHT | CC_OFF_LEFT)) {
149 if (plane_flag & (CC_OFF_LEFT | CC_OFF_BOT)) {
154 kn = a - on_pnt->z; //xs-zs
155 kd = kn - b + off_pnt->z; //xs-zs-xe+ze
159 tmp->x = on_pnt->x + (off_pnt->x-on_pnt->x) * ratio;
160 tmp->y = on_pnt->y + (off_pnt->y-on_pnt->y) * ratio;
162 if (plane_flag & (CC_OFF_TOP|CC_OFF_BOT)) {
168 if (plane_flag & (CC_OFF_LEFT|CC_OFF_BOT))
173 if (flags & TMAP_FLAG_TEXTURED) {
174 tmp->u = on_pnt->u + (off_pnt->u-on_pnt->u) * ratio;
175 tmp->v = on_pnt->v + (off_pnt->v-on_pnt->v) * ratio;
178 if (flags & TMAP_FLAG_GOURAUD ) {
179 if (flags & TMAP_FLAG_RAMP) {
183 on_b = i2fl(on_pnt->b);
184 off_b = i2fl(off_pnt->b);
186 tmp->b = ubyte(fl2i(on_b + (off_b-on_b) * ratio));
188 if (flags & TMAP_FLAG_RGB) {
189 float on_r, on_b, on_g;
190 float off_r, off_b, off_g;
192 on_r = i2fl(on_pnt->r);
193 off_r = i2fl(off_pnt->r);
194 on_g = i2fl(on_pnt->g);
195 off_g = i2fl(off_pnt->g);
197 on_b = i2fl(on_pnt->b);
198 off_b = i2fl(off_pnt->b);
200 tmp->r = ubyte(fl2i(on_r + (off_r-on_r) * ratio));
201 tmp->g = ubyte(fl2i(on_g + (off_g-on_g) * ratio));
202 tmp->b = ubyte(fl2i(on_b + (off_b-on_b) * ratio));
206 if (flags & TMAP_FLAG_ALPHA) {
210 on_a = i2fl(on_pnt->a);
211 off_a = i2fl(off_pnt->a);
213 tmp->a = ubyte(fl2i(on_a + (off_a-on_a) * ratio));
221 //clips a line to the viewing pyramid.
222 void clip_line(vertex **p0,vertex **p1,ubyte codes_or, uint flags)
227 for (plane_flag=1;plane_flag<=CC_OFF_USER;plane_flag<<=1)
228 if (codes_or & plane_flag) {
230 if ((*p0)->codes & plane_flag)
231 {vertex *t=*p0; *p0=*p1; *p1=t;} //swap!
235 *p1 = clip_edge(plane_flag,*p0,*p1,flags);
236 codes_or = (unsigned char)((*p0)->codes | (*p1)->codes); //get new codes
238 if (old_p1->flags & PF_TEMP_POINT)
239 free_temp_point(old_p1);
244 int clip_plane(int plane_flag,vertex **src,vertex **dest,int *nv,ccodes *cc,uint flags)
247 vertex **save_dest=dest;
249 //copy first two verts to end
253 cc->vand = 0xff; cc->vor = 0;
255 for (i=1;i<=*nv;i++) {
257 if (src[i]->codes & plane_flag) { //cur point off?
259 if (! (src[i-1]->codes & plane_flag)) { //prev not off?
261 *dest = clip_edge(plane_flag,src[i-1],src[i],flags);
262 cc->vor |= (*dest)->codes;
263 cc->vand &= (*dest)->codes;
267 if (! (src[i+1]->codes & plane_flag)) {
269 *dest = clip_edge(plane_flag,src[i+1],src[i],flags);
270 cc->vor |= (*dest)->codes;
271 cc->vand &= (*dest)->codes;
275 //see if must free discarded point
277 if (src[i]->flags & PF_TEMP_POINT)
278 free_temp_point(src[i]);
280 else { //cur not off, copy to dest buffer
284 cc->vor |= src[i]->codes;
285 cc->vand &= src[i]->codes;
289 return (dest-save_dest);
293 vertex **clip_polygon(vertex **src,vertex **dest,int *nv,ccodes *cc,uint flags)
298 for (plane_flag=1;plane_flag<=CC_OFF_USER;plane_flag<<=1)
300 if (cc->vor & plane_flag) {
302 *nv = clip_plane(plane_flag,src,dest,nv,cc,flags);
304 if (cc->vand) //clipped away
307 t = src; src = dest; dest = t;
311 return src; //we swapped after we copied