Main Page   Alphabetical List   Compound List   File List   Compound Members   File Members  

t3d.c

Go to the documentation of this file.
00001 /*
00002 ** ---------------------------------------------------------------
00003 ** t3d.c  -  Package to manage 3D matrix transformations.
00004 **
00005 ** ---------------------------------------------------------------
00006 */
00007 
00051 /*
00052 ** ---------------------------------------------------------------
00053 ** Global variables and symbols:
00054 */
00055 #include <math.h>
00056 #include "t3d.h"
00057 
00058 /*
00059 ** ---------------------------------------------------------------
00060 ** Local definitions and variables:
00061 */
00062 #ifndef rad
00063 #define rad(a)  ((a)*0.01745329252)
00064 #endif
00065 
00068 typedef struct {
00069   double x, y, z;
00070  } Vector;
00071 
00074 typedef double Matrix3d[4][4];
00075 
00078 static Matrix3d mt =
00079  {
00080   { 1.0, 0.0, 0.0, 0.0 } ,
00081   { 0.0, 1.0, 0.0, 0.0 } ,
00082   { 0.0, 0.0, 1.0, 0.0 } ,
00083   { 0.0, 0.0, 0.0, 1.0 }
00084  };
00085 
00086 /*
00087 ** ---------------------------------------------------------------
00088 ** Local functions:
00089 */
00090 
00091 /*========================  t3dVecUnit  ========================*/
00096 static void t3dVecUnit( Vector *a, Vector *b )
00097 {
00098  double len = sqrt(a->x*a->x + a->y*a->y + a->z*a->z);
00099 
00100  if( len != 0.0 )
00101  {
00102   b->x = a->x / len;
00103   b->y = a->y / len;
00104   b->z = a->z / len;
00105  }
00106 }
00107 
00108 /*========================  t3dVecCross  =======================*/
00113 static void t3dVecCross( Vector *a, Vector *b, Vector *c )
00114 {
00115  c->x = a->y * b->z - a->z * b->y;
00116  c->y = a->z * b->x - a->x * b->z;
00117  c->z = a->x * b->y - a->y * b->x;
00118 }
00119 
00120 /*=======================  t3dAccumulate  ======================*/
00125 static void t3dAccumulate( Matrix3d new_mt )
00126 {
00127  double   t0, t1, t2;
00128 
00129  t0       =  mt[0][0]*new_mt[0][0] + mt[1][0]*new_mt[0][1] 
00130            + mt[2][0]*new_mt[0][2] + mt[3][0]*new_mt[0][3];
00131  t1       =  mt[0][0]*new_mt[1][0] + mt[1][0]*new_mt[1][1] 
00132            + mt[2][0]*new_mt[1][2] + mt[3][0]*new_mt[1][3];
00133  t2       =  mt[0][0]*new_mt[2][0] + mt[1][0]*new_mt[2][1] 
00134            + mt[2][0]*new_mt[2][2] + mt[3][0]*new_mt[2][3];
00135  mt[3][0] =  mt[0][0]*new_mt[3][0] + mt[1][0]*new_mt[3][1] 
00136            + mt[2][0]*new_mt[3][2] + mt[3][0]*new_mt[3][3];
00137  mt[2][0] =  t2;
00138  mt[1][0] =  t1;
00139  mt[0][0] =  t0;
00140 
00141  t0       =  mt[0][1]*new_mt[0][0] + mt[1][1]*new_mt[0][1] 
00142            + mt[2][1]*new_mt[0][2] + mt[3][1]*new_mt[0][3];
00143  t1       =  mt[0][1]*new_mt[1][0] + mt[1][1]*new_mt[1][1] 
00144            + mt[2][1]*new_mt[1][2] + mt[3][1]*new_mt[1][3];
00145  t2       =  mt[0][1]*new_mt[2][0] + mt[1][1]*new_mt[2][1] 
00146            + mt[2][1]*new_mt[2][2] + mt[3][1]*new_mt[2][3];
00147  mt[3][1] =  mt[0][1]*new_mt[3][0] + mt[1][1]*new_mt[3][1] 
00148            + mt[2][1]*new_mt[3][2] + mt[3][1]*new_mt[3][3];
00149  mt[2][1] =  t2;
00150  mt[1][1] =  t1;
00151  mt[0][1] =  t0;
00152 
00153  t0       =  mt[0][2]*new_mt[0][0] + mt[1][2]*new_mt[0][1] 
00154            + mt[2][2]*new_mt[0][2] + mt[3][2]*new_mt[0][3];
00155  t1       =  mt[0][2]*new_mt[1][0] + mt[1][2]*new_mt[1][1] 
00156            + mt[2][2]*new_mt[1][2] + mt[3][2]*new_mt[1][3];
00157  t2       =  mt[0][2]*new_mt[2][0] + mt[1][2]*new_mt[2][1] 
00158            + mt[2][2]*new_mt[2][2] + mt[3][2]*new_mt[2][3];
00159  mt[3][2] =  mt[0][2]*new_mt[3][0] + mt[1][2]*new_mt[3][1] 
00160            + mt[2][2]*new_mt[3][2] + mt[3][2]*new_mt[3][3];
00161  mt[2][2] =  t2;
00162  mt[1][2] =  t1;
00163  mt[0][2] =  t0;
00164 
00165  t0       =  mt[0][3]*new_mt[0][0] + mt[1][3]*new_mt[0][1] 
00166            + mt[2][3]*new_mt[0][2] + mt[3][3]*new_mt[0][3];
00167  t1       =  mt[0][3]*new_mt[1][0] + mt[1][3]*new_mt[1][1] 
00168            + mt[2][3]*new_mt[1][2] + mt[3][3]*new_mt[1][3];
00169  t2       =  mt[0][3]*new_mt[2][0] + mt[1][3]*new_mt[2][1] 
00170            + mt[2][3]*new_mt[2][2] + mt[3][3]*new_mt[2][3];
00171  mt[3][3] =  mt[0][3]*new_mt[3][0] + mt[1][3]*new_mt[3][1] 
00172            + mt[2][3]*new_mt[3][2] + mt[3][3]*new_mt[3][3];
00173  mt[2][3] =  t2;
00174  mt[1][3] =  t1;
00175  mt[0][3] =  t0;
00176 }
00177 
00178 
00179 /*
00180 ** ---------------------------------------------------------------
00181 ** Public functions:
00182 */
00183 
00184 /*======================  t3dTransform  ========================*/
00185 
00186 void t3dTransform( double *x, double *y, double *z )
00187 {
00188  double r, s, t, h;
00189 
00190   r = mt[0][0]*(*x)+mt[0][1]*(*y)+mt[0][2]*(*z)+mt[0][3];
00191   s = mt[1][0]*(*x)+mt[1][1]*(*y)+mt[1][2]*(*z)+mt[1][3];
00192   t = mt[2][0]*(*x)+mt[2][1]*(*y)+mt[2][2]*(*z)+mt[2][3];
00193   h = mt[3][0]*(*x)+mt[3][1]*(*y)+mt[3][2]*(*z)+mt[3][3];
00194  *x = r/h;
00195  *y = s/h;
00196  *z = t/h;
00197 }
00198 
00199 /*======================  t3dIdentity  ========================= */
00200 
00201 void t3dIdentity( void )
00202 {
00203  mt[0][0] = 1.0; mt[0][1] = 0.0; mt[0][2] = 0.0; mt[0][3] = 0.0;
00204  mt[1][0] = 0.0; mt[1][1] = 1.0; mt[1][2] = 0.0; mt[1][3] = 0.0;
00205  mt[2][0] = 0.0; mt[2][1] = 0.0; mt[2][2] = 1.0; mt[2][3] = 0.0;
00206  mt[3][0] = 0.0; mt[3][1] = 0.0; mt[3][2] = 0.0; mt[3][3] = 1.0;
00207 }
00208 
00209 /*=========================  t3dScale  =========================*/
00210 
00211 void t3dScale( double sx, double sy, double sz )
00212 {
00213  Matrix3d scale;
00214 
00215 /*** COMPLETE HERE: 47 ***/
00216 
00217  t3dAccumulate( scale );
00218 }
00219 
00220 /*=======================  t3dTranslate  =======================*/
00221 
00222 void t3dTranslate( double tx, double ty, double tz )
00223 {
00224  Matrix3d transl;
00225 
00226 /*** COMPLETE HERE: 48 ***/
00227 
00228  t3dAccumulate( transl );
00229 }
00230 
00231 /*======================  t3dRotateBasis  ======================*/
00232 
00233 void t3dRotateBasis( double ux, double uy, double uz, 
00234                      double vx, double vy, double vz,
00235                      double wx, double wy, double wz )
00236 {
00237  Matrix3d rot;
00238 
00239 /*** COMPLETE HERE: 49 ***/
00240 
00241  t3dAccumulate( rot );
00242 }
00243 
00244 /*=========================  t3dCamera  ========================*/
00245 
00246 void t3dCamera( double eyex, double eyey, double eyez, 
00247                 double refx, double refy, double refz,
00248                 double vupx, double vupy, double vupz )
00249 {
00250  Vector  view, upvec;   /* view vector and view upper vector */
00251  Vector  u, v, w;       /* eye system unit vectors           */
00252 
00253 /* Define view vector (from ref to eye) and view up vector.
00254  */
00255 /*** COMPLETE HERE: 50 ***/
00256 
00257 /* Find the eye system unit vectors.
00258  */
00259 /*** COMPLETE HERE: 51 ***/
00260 
00261 /* Concatenate a translation of basis from origin to eye point.
00262  */
00263  t3dTranslate( -eyex, -eyey, -eyez );
00264 
00265 /* Concatenate a rotation of basis from world to eye system.
00266  */
00267  t3dRotateBasis( u.x, u.y, u.z, v.x, v.y, v.z, w.x, w.y, w.z );
00268 }
00269 
00270 /*======================  t3dPerspective  ======================*/
00271 
00272 void t3dPerspective( double d, double front, double back )
00273 {
00274  double   alpha, beta;        /* perspective parameters */
00275  Matrix3d persp;
00276 
00277 /*** COMPLETE HERE: 52 ***/
00278 
00279  t3dAccumulate( persp );
00280 }
00281 
00282 /*=====================  t3dNormalizeView  =====================*/
00283 
00284 void t3dNormalizeView( double left, double right, 
00285                        double bottom, double top, 
00286                        double front, double back )
00287 {
00288  double tx, ty, tz;           /* translation factors */
00289  double sx, sy, sz;           /* scaling factors     */
00290 
00291 /* Concatenate a translation of basis from origin to center
00292  * of view volume.
00293  */
00294 /*** COMPLETE HERE: 53 ***/
00295  t3dTranslate( -tx, -ty, -tz );
00296 
00297 /* Concatenate a scaling transformation that scales the view 
00298  * volume to a cube ranging from -1 to +1 in the 3 directions, 
00299  * also reverting the z-axis direction.
00300  */
00301 /*** COMPLETE HERE: 54 ***/
00302  t3dScale( sx, sy, -sz );
00303 }
00304 
00306 ** COMPLETE HERE: 53 ***/
00307  t3dTranslate( -tx, -ty, -tz );
00308 
00309 /* Concatenate a scaling transformation that scales the view 
00310  * volume to a cube ranging from -1 to +1 in the 3 directions, 
00311  * also reverting the z-axis direction.
00312  */
00313 /*** COMPLETE HERE: 54 ***/
00314  t3dScale( sx, sy, -sz );
00315 }
00316 

Generated on Tue Nov 8 10:58:00 2005 for Trab3 by doxygen1.2.18