1 /* $Id: points.c,v 1.7 2005-08-02 06:14:48 chris 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.7 2005-08-02 06:14:48 chris 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)
61 vm_vec_sub(&tempv,src,&View_position);
63 vm_vec_rotate(&dest->p3_vec,&tempv,&View_matrix);
65 dest->p3_flags = 0; //no projected
67 return g3_code_point(dest);
71 //checks for overflow & divides if ok, fillig in r
72 //returns true if div is ok, else false
73 int checkmuldiv(fix *r,fix a,fix b,fix c)
91 *r = fixdivquadlong(q.low,q.high,c);
97 void g3_project_point(g3s_point *p)
102 if (p->p3_flags & PF_PROJECTED || p->p3_codes & CC_BEHIND)
105 if (checkmuldiv(&tx,p->p3_x,Canv_w2,p->p3_z) && checkmuldiv(&ty,p->p3_y,Canv_h2,p->p3_z)) {
106 p->p3_sx = Canv_w2 + tx;
107 p->p3_sy = Canv_h2 - ty;
108 p->p3_flags |= PF_PROJECTED;
111 p->p3_flags |= PF_OVERFLOW;
115 if ((p->p3_flags & PF_PROJECTED) || (p->p3_codes & CC_BEHIND))
118 if ( p->p3_z <= 0 ) {
119 p->p3_flags |= PF_OVERFLOW;
124 p->p3_sx = fl2f(fCanv_w2 + (f2fl(p->p3_x)*fCanv_w2 / fz));
125 p->p3_sy = fl2f(fCanv_h2 - (f2fl(p->p3_y)*fCanv_h2 / fz));
127 p->p3_flags |= PF_PROJECTED;
131 //from a 2d point, compute the vector through that point
132 void g3_point_2_vec(vms_vector *v,short sx,short sy)
137 tempv.x = fixmuldiv(fixdiv((sx<<16) - Canv_w2,Canv_w2),Matrix_scale.z,Matrix_scale.x);
138 tempv.y = -fixmuldiv(fixdiv((sy<<16) - Canv_h2,Canv_h2),Matrix_scale.z,Matrix_scale.y);
141 vm_vec_normalize(&tempv);
143 vm_copy_transpose_matrix(&tempm,&Unscaled_matrix);
145 vm_vec_rotate(v,&tempv,&tempm);
149 //delta rotation functions
150 vms_vector *g3_rotate_delta_x(vms_vector *dest,fix dx)
152 dest->x = fixmul(View_matrix.rvec.x,dx);
153 dest->y = fixmul(View_matrix.uvec.x,dx);
154 dest->z = fixmul(View_matrix.fvec.x,dx);
159 vms_vector *g3_rotate_delta_y(vms_vector *dest,fix dy)
161 dest->x = fixmul(View_matrix.rvec.y,dy);
162 dest->y = fixmul(View_matrix.uvec.y,dy);
163 dest->z = fixmul(View_matrix.fvec.y,dy);
168 vms_vector *g3_rotate_delta_z(vms_vector *dest,fix dz)
170 dest->x = fixmul(View_matrix.rvec.z,dz);
171 dest->y = fixmul(View_matrix.uvec.z,dz);
172 dest->z = fixmul(View_matrix.fvec.z,dz);
178 vms_vector *g3_rotate_delta_vec(vms_vector *dest,vms_vector *src)
180 return vm_vec_rotate(dest,src,&View_matrix);
183 ubyte g3_add_delta_vec(g3s_point *dest,g3s_point *src,vms_vector *deltav)
185 vm_vec_add(&dest->p3_vec,&src->p3_vec,deltav);
187 dest->p3_flags = 0; //not projected
189 return g3_code_point(dest);
192 //calculate the depth of a point - returns the z coord of the rotated point
193 fix g3_calc_point_depth(vms_vector *pnt)
198 fixmulaccum(&q,(pnt->x - View_position.x),View_matrix.fvec.x);
199 fixmulaccum(&q,(pnt->y - View_position.y),View_matrix.fvec.y);
200 fixmulaccum(&q,(pnt->z - View_position.z),View_matrix.fvec.z);
202 return fixquadadjust(&q);