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.
30 void (*tmap_drawer_ptr)(grs_bitmap *bm,int nv,g3s_point **vertlist) = draw_tmap;
31 void (*flat_drawer_ptr)(int nv,int *vertlist) = gr_upoly_tmap;
32 int (*line_drawer_ptr)(fix x0,fix y0,fix x1,fix y1) = gr_line;
34 //specifies 2d drawing routines to use instead of defaults. Passing
35 //NULL for either or both restores defaults
36 void g3_set_special_render(void (*tmap_drawer)(),void (*flat_drawer)(),int (*line_drawer)(fix, fix, fix, fix))
38 tmap_drawer_ptr = (tmap_drawer)?tmap_drawer:draw_tmap;
39 flat_drawer_ptr = (flat_drawer)?flat_drawer:gr_upoly_tmap;
40 line_drawer_ptr = (line_drawer)?line_drawer:gr_line;
43 //deal with a clipped line
44 bool must_clip_line(g3s_point *p0,g3s_point *p1,ubyte codes_or)
48 if ((p0->p3_flags&PF_TEMP_POINT) || (p1->p3_flags&PF_TEMP_POINT))
50 ret = 0; //line has already been clipped, so give up
54 clip_line(&p0,&p1,codes_or);
56 ret = g3_draw_line(p0,p1);
61 if (p0->p3_flags & PF_TEMP_POINT)
64 if (p1->p3_flags & PF_TEMP_POINT)
70 //draws a line. takes two points. returns true if drew
71 bool g3_draw_line(g3s_point *p0,g3s_point *p1)
75 if (p0->p3_codes & p1->p3_codes)
78 codes_or = p0->p3_codes | p1->p3_codes;
80 if (codes_or & CC_BEHIND)
81 return must_clip_line(p0,p1,codes_or);
83 if (!(p0->p3_flags&PF_PROJECTED))
86 if (p0->p3_flags&PF_OVERFLOW)
87 return must_clip_line(p0,p1,codes_or);
90 if (!(p1->p3_flags&PF_PROJECTED))
93 if (p1->p3_flags&PF_OVERFLOW)
94 return must_clip_line(p0,p1,codes_or);
96 return (bool) (*line_drawer_ptr)(p0->p3_sx,p0->p3_sy,p1->p3_sx,p1->p3_sy);
100 //returns true if a plane is facing the viewer. takes the unrotated surface
101 //normal of the plane, and a point on it. The normal need not be normalized
102 bool g3_check_normal_facing(vms_vector *v,vms_vector *norm)
106 vm_vec_sub(&tempv,&View_position,v);
108 return (vm_vec_dot(&tempv,norm) > 0);
111 bool do_facing_check(vms_vector *norm,g3s_point **vertlist,vms_vector *p)
113 if (norm) { //have normal
115 Assert(norm->x || norm->y || norm->z);
117 return g3_check_normal_facing(p,norm);
119 else { //normal not specified, so must compute
123 //get three points (rotated) and compute normal
125 vm_vec_perp(&tempv,&vertlist[0]->p3_vec,&vertlist[1]->p3_vec,&vertlist[2]->p3_vec);
127 return (vm_vec_dot(&tempv,&vertlist[1]->p3_vec) < 0);
131 //like g3_draw_poly(), but checks to see if facing. If surface normal is
132 //NULL, this routine must compute it, which will be slow. It is better to
133 //pre-compute the normal, and pass it to this function. When the normal
134 //is passed, this function works like g3_check_normal_facing() plus
136 //returns -1 if not facing, 1 if off screen, 0 if drew
137 bool g3_check_and_draw_poly(int nv,g3s_point **pointlist,vms_vector *norm,vms_vector *pnt)
139 if (do_facing_check(norm,pointlist,pnt))
140 return g3_draw_poly(nv,pointlist);
145 bool g3_check_and_draw_tmap(int nv,g3s_point **pointlist,g3s_uvl *uvl_list,grs_bitmap *bm,vms_vector *norm,vms_vector *pnt)
147 if (do_facing_check(norm,pointlist,pnt))
148 return g3_draw_tmap(nv,pointlist,uvl_list,bm);
153 //deal with face that must be clipped
154 bool must_clip_flat_face(int nv,g3s_codes cc)
160 bufptr = clip_polygon(Vbuf0,Vbuf1,&nv,&cc);
162 if (nv>0 && !(cc.or&CC_BEHIND) && !cc.and) {
165 g3s_point *p = bufptr[i];
167 if (!(p->p3_flags&PF_PROJECTED))
170 if (p->p3_flags&PF_OVERFLOW) {
175 Vertex_list[i*2] = p->p3_sx;
176 Vertex_list[i*2+1] = p->p3_sy;
179 (*flat_drawer_ptr)(nv,(int *)Vertex_list);
189 if (Vbuf1[i]->p3_flags & PF_TEMP_POINT)
190 free_temp_point(Vbuf1[i]);
192 // Assert(free_point_num==0);
198 //draw a flat-shaded face.
199 //returns 1 if off screen, 0 if drew
200 bool g3_draw_poly(int nv,g3s_point **pointlist)
206 cc.or = 0; cc.and = 0xff;
212 bufptr[i] = pointlist[i];
214 cc.and &= bufptr[i]->p3_codes;
215 cc.or |= bufptr[i]->p3_codes;
219 return 1; //all points off screen
222 return must_clip_flat_face(nv,cc);
224 //now make list of 2d coords (& check for overflow)
227 g3s_point *p = bufptr[i];
229 if (!(p->p3_flags&PF_PROJECTED))
232 if (p->p3_flags&PF_OVERFLOW)
233 return must_clip_flat_face(nv,cc);
235 Vertex_list[i*2] = p->p3_sx;
236 Vertex_list[i*2+1] = p->p3_sy;
239 (*flat_drawer_ptr)(nv,(int *)Vertex_list);
241 return 0; //say it drew
244 bool must_clip_tmap_face(int nv,g3s_codes cc,grs_bitmap *bm);
246 //draw a texture-mapped face.
247 //returns 1 if off screen, 0 if drew
248 bool g3_draw_tmap(int nv,g3s_point **pointlist,g3s_uvl *uvl_list,grs_bitmap *bm)
254 cc.or = 0; cc.and = 0xff;
261 p = bufptr[i] = pointlist[i];
263 cc.and &= p->p3_codes;
264 cc.or |= p->p3_codes;
266 p->p3_u = uvl_list[i].u;
267 p->p3_v = uvl_list[i].v;
268 p->p3_l = uvl_list[i].l;
270 p->p3_flags |= PF_UVS + PF_LS;
275 return 1; //all points off screen
278 return must_clip_tmap_face(nv,cc,bm);
280 //now make list of 2d coords (& check for overflow)
283 g3s_point *p = bufptr[i];
285 if (!(p->p3_flags&PF_PROJECTED))
288 if (p->p3_flags&PF_OVERFLOW) {
289 Int3(); //should not overflow after clip
294 (*tmap_drawer_ptr)(bm,nv,bufptr);
296 return 0; //say it drew
300 bool must_clip_tmap_face(int nv,g3s_codes cc,grs_bitmap *bm)
305 bufptr = clip_polygon(Vbuf0,Vbuf1,&nv,&cc);
307 if (nv && !(cc.or&CC_BEHIND) && !cc.and) {
310 g3s_point *p = bufptr[i];
312 if (!(p->p3_flags&PF_PROJECTED))
315 if (p->p3_flags&PF_OVERFLOW) {
316 Int3(); //should not overflow after clip
321 (*tmap_drawer_ptr)(bm,nv,bufptr);
328 if (bufptr[i]->p3_flags & PF_TEMP_POINT)
329 free_temp_point(bufptr[i]);
331 // Assert(free_point_num==0);
338 int checkmuldiv(fix *r,fix a,fix b,fix c);
342 //draw a sortof sphere - i.e., the 2d radius is proportional to the 3d
343 //radius, but not to the distance from the eye
344 int g3_draw_sphere(g3s_point *pnt,fix rad)
346 if (! (pnt->p3_codes & CC_BEHIND)) {
348 if (! (pnt->p3_flags & PF_PROJECTED))
349 g3_project_point(pnt);
351 if (! (pnt->p3_codes & PF_OVERFLOW)) {
354 r2 = fixmul(rad,Matrix_scale.x);
356 if (checkmuldiv(&t,r2,Canv_w2,pnt->p3_z))
357 return gr_disk(pnt->p3_sx,pnt->p3_sy,t);
361 return gr_disk(pnt->p3_sx, pnt->p3_sy, fl2f(((f2fl(r2) * fCanv_w2) / f2fl(pnt->p3_z))));