1 /* $Id: points.c,v 1.6 2004-08-28 23:17:45 schaffner Exp $ */
3 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
4 SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
5 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
6 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
7 IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
8 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
9 FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
10 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
11 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
12 COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
16 * Routines for point definition, rotation, etc.
25 static char rcsid[] = "$Id: points.c,v 1.6 2004-08-28 23:17:45 schaffner Exp $";
32 //code a point. fills in the p3_codes field of the point, and returns the codes
33 ubyte g3_code_point(g3s_point *p)
37 if (p->p3_x > p->p3_z)
40 if (p->p3_y > p->p3_z)
43 if (p->p3_x < -p->p3_z)
46 if (p->p3_y < -p->p3_z)
52 return p->p3_codes = cc;
56 //rotates a point. returns codes. does not check if already rotated
57 ubyte g3_rotate_point(g3s_point *dest,vms_vector *src)
65 vm_vec_sub(&tempv,src,&View_position);
67 vm_vec_rotate(&dest->p3_vec,&tempv,&View_matrix);
69 dest->p3_flags = 0; //no projected
71 return g3_code_point(dest);
75 //checks for overflow & divides if ok, fillig in r
76 //returns true if div is ok, else false
77 int checkmuldiv(fix *r,fix a,fix b,fix c)
95 *r = fixdivquadlong(q.low,q.high,c);
101 void g3_project_point(g3s_point *p)
106 if (p->p3_flags & PF_PROJECTED || p->p3_codes & CC_BEHIND)
109 if (checkmuldiv(&tx,p->p3_x,Canv_w2,p->p3_z) && checkmuldiv(&ty,p->p3_y,Canv_h2,p->p3_z)) {
110 p->p3_sx = Canv_w2 + tx;
111 p->p3_sy = Canv_h2 - ty;
112 p->p3_flags |= PF_PROJECTED;
115 p->p3_flags |= PF_OVERFLOW;
119 if ((p->p3_flags & PF_PROJECTED) || (p->p3_codes & CC_BEHIND))
122 if ( p->p3_z <= 0 ) {
123 p->p3_flags |= PF_OVERFLOW;
128 p->p3_sx = fl2f(fCanv_w2 + (f2fl(p->p3_x)*fCanv_w2 / fz));
129 p->p3_sy = fl2f(fCanv_h2 - (f2fl(p->p3_y)*fCanv_h2 / fz));
131 p->p3_flags |= PF_PROJECTED;
135 //from a 2d point, compute the vector through that point
136 void g3_point_2_vec(vms_vector *v,short sx,short sy)
141 tempv.x = fixmuldiv(fixdiv((sx<<16) - Canv_w2,Canv_w2),Matrix_scale.z,Matrix_scale.x);
142 tempv.y = -fixmuldiv(fixdiv((sy<<16) - Canv_h2,Canv_h2),Matrix_scale.z,Matrix_scale.y);
145 vm_vec_normalize(&tempv);
147 vm_copy_transpose_matrix(&tempm,&Unscaled_matrix);
149 vm_vec_rotate(v,&tempv,&tempm);
153 //delta rotation functions
154 vms_vector *g3_rotate_delta_x(vms_vector *dest,fix dx)
156 dest->x = fixmul(View_matrix.rvec.x,dx);
157 dest->y = fixmul(View_matrix.uvec.x,dx);
158 dest->z = fixmul(View_matrix.fvec.x,dx);
163 vms_vector *g3_rotate_delta_y(vms_vector *dest,fix dy)
165 dest->x = fixmul(View_matrix.rvec.y,dy);
166 dest->y = fixmul(View_matrix.uvec.y,dy);
167 dest->z = fixmul(View_matrix.fvec.y,dy);
172 vms_vector *g3_rotate_delta_z(vms_vector *dest,fix dz)
174 dest->x = fixmul(View_matrix.rvec.z,dz);
175 dest->y = fixmul(View_matrix.uvec.z,dz);
176 dest->z = fixmul(View_matrix.fvec.z,dz);
182 vms_vector *g3_rotate_delta_vec(vms_vector *dest,vms_vector *src)
184 return vm_vec_rotate(dest,src,&View_matrix);
187 ubyte g3_add_delta_vec(g3s_point *dest,g3s_point *src,vms_vector *deltav)
189 vm_vec_add(&dest->p3_vec,&src->p3_vec,deltav);
191 dest->p3_flags = 0; //not projected
193 return g3_code_point(dest);
196 //calculate the depth of a point - returns the z coord of the rotated point
197 fix g3_calc_point_depth(vms_vector *pnt)
202 fixmulaccum(&q,(pnt->x - View_position.x),View_matrix.fvec.x);
203 fixmulaccum(&q,(pnt->y - View_position.y),View_matrix.fvec.y);
204 fixmulaccum(&q,(pnt->z - View_position.z),View_matrix.fvec.z);
206 return fixquadadjust(&q);