arbeit
Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members

vectorGraphics.h

Go to the documentation of this file.
00001 /*
00002  * $Id: vectorGraphics.h,v 1.6 2003/10/09 08:27:51 jmk Exp $
00003  */
00004 
00005 //vectorGraphics.h
00006 
00007 #ifndef __VECTOR_GRAPHICS_DOT_H
00008 #define __VECTOR_GRAPHICS_DOT_H
00009 
00010 #include <mathGutz.h>
00011 
00012 namespace gutz{
00013 
00014 //=================================================================================
00015 // Functions
00016 //=================================================================================
00017 
00018 ///lookat
00019 template <class T>
00020 mat4<T>  look_at(const vec3<T> & eye, const vec3<T> & at, const vec3<T> & up);
00021 
00022 ///perspective
00023 template <class T>
00024 mat4<T>  perspective(const T fovy, const T aspect, const T n, const T f);
00025 
00026 ///frustum
00027 template <class T>
00028 mat4<T>  frustum(const T l, const T r, const T b, const T t, const T n, const T f);
00029 
00030 template <class T>
00031 mat4<T>  ortho(const T l, const T r, const T b, const T t, const T n, const T f);
00032 
00033 ///trackball
00034 template <class T>
00035 quat<T>  trackball(const vec2<T> & pt1, 
00036                    const vec2<T> & pt2, 
00037                    const T trackballsize);
00038 
00039 template <class T>
00040 quat<T> & add_quats(quat<T>& p, const quat<T>& q1, const quat<T>& q2)
00041 {
00042    quat<T> t1, t2;
00043 
00044    t1 = q1;
00045    t1.x *= q2.w;
00046    t1.y *= q2.w;
00047    t1.z *= q2.w;
00048 
00049    t2 = q2;
00050    t2.x *= q1.w;
00051    t2.y *= q1.w;
00052    t2.z *= q1.w;
00053 
00054    p.x = (q2.y * q1.z) - (q2.z * q1.y) + t1.x + t2.x;
00055    p.y = (q2.z * q1.x) - (q2.x * q1.z) + t1.y + t2.y;
00056    p.z = (q2.x * q1.y) - (q2.y * q1.x) + t1.z + t2.z;
00057    p.w = q1.w * q2.w - (q1.x * q2.x + q1.y * q2.y + q1.z * q2.z);
00058 
00059    return p;
00060 }
00061 
00062 
00063 
00064 
00065 //=================================================================================
00066 // Function implementations
00067 //=================================================================================
00068 
00069 //---------------------------------------------------------------------------------
00070 // Lookat
00071 //---------------------------------------------------------------------------------
00072 template <class T>
00073 mat4<T> look_at(const vec3<T> & eye, const vec3<T> & at, const vec3<T> & up)
00074 {
00075    vec3<T> x, y, z;
00076 
00077    // make rotation matrix
00078 
00079    // Z vector
00080    z = eye - at;
00081    z.normalize();
00082 
00083    // Y vector
00084    y = up;
00085 
00086    // X vector = Y cross Z
00087    x = y.cross(z);
00088 
00089    // Recompute Y = Z cross X
00090    y = z.cross(x);
00091 
00092    // cross product gives area of parallelogram, which is < 1.0 for
00093    // non-perpendicular unit-length vectors; so normalize x, y here
00094    x.normalize();
00095    y.normalize();
00096 
00097    return mat4<T>(x.x,           y.x,           z.x,           0, 
00098                   x.y,           y.y,           z.y,           0,
00099                   x.z,           y.z,           z.z,           0,
00100                   -(eye.dot(x)), -(eye.dot(y)), -(eye.dot(z)), 1);
00101 #if 0
00102    M.m[0] = x.x;  M.m[4] = x.y;  M.m[8] = x.z;  M.m[12] = -x.x * eye.x - x.y * eye.y - x.z*eye.z;
00103    M.m[1] = y.x;  M.m[5] = y.y;  M.m[9] = y.z;  M.m[13] = -y.x * eye.x - y.y * eye.y - y.z*eye.z;
00104    M.m[2] = z.x;  M.m[6] = z.y;  M.m[10] = z.z; M.m[14] = -z.x * eye.x - z.y * eye.y - z.z*eye.z;
00105    M.m[3] = 0;    M.m[7] = 0;    M.m[11] = 0;   M.m[15] = 1;
00106    return M;
00107 #endif
00108 
00109 }
00110 
00111 
00112 //---------------------------------------------------------------------------------
00113 // Frustum
00114 //---------------------------------------------------------------------------------
00115 
00116 template <class T>
00117 mat4<T>  frustum(const T l, const T r, const T b, const T t, const T n, const T f)
00118 {
00119    return mat4<T> (
00120            T((2.0*n) / (r-l)),
00121            0.0,
00122            0.0,
00123            0.0,
00124 
00125            0.0,
00126            T(2.0*n) / (t-b),
00127            0.0,
00128            0.0,
00129 
00130            (r+l) / (r-l),
00131            (t+b) / (t-b),
00132            -(f+n) / (f-n),
00133            -1.0,
00134 
00135            0.0,
00136            0.0,
00137            -(2.0f*f*n) / (f-n),
00138            0.0);
00139 }
00140 
00141 //---------------------------------------------------------------------------------
00142 // Perspective
00143 //---------------------------------------------------------------------------------
00144 
00145 template <class T>
00146 mat4<T>  perspective(const T fovy, const T aspect, const T n, const T f)
00147 {
00148    T xmin, xmax, ymin, ymax;
00149 
00150    ymax = n * tanf(fovy * M_PI/180 * 0.5);
00151    ymin = -ymax;
00152 
00153    xmin = ymin * aspect;
00154    xmax = ymax * aspect;
00155 
00156    return frustum(xmin, xmax, ymin, ymax, n, f);
00157 }
00158 
00159 //---------------------------------------------------------------------------------
00160 // Orthographic
00161 //---------------------------------------------------------------------------------
00162 
00163 template <class T>
00164 mat4<T>  ortho(const T l, const T r, const T b, const T t, const T n, const T f)
00165 {
00166    return mat4<T> (
00167              T((2.0) / (r-l)),
00168              0.0,
00169              0.0,
00170              0.0,
00171 
00172              0.0,
00173              T(2.0) / (t-b),
00174              0.0,
00175              0.0,
00176 
00177              0.0,
00178              0.0,
00179              -T(2.0) / (f-n),
00180              0.0,
00181 
00182              -(r+l) / (r-l),
00183              -(t+b) / (t-b),
00184              -(f+n) / (f-n),
00185              1.0);
00186 }
00187 
00188 
00189 //---------------------------------------------------------------------------------
00190 // Trackball
00191 //---------------------------------------------------------------------------------
00192 
00193 /// a helper function for the trackball
00194 template <class T>
00195 T tb_project_to_sphere(const T r, const T x, const T y)
00196 {
00197    T z;
00198 
00199    const T d = sqrtf(x*x + y*y);
00200    if (d < r * 0.70710678118654752440) {    /* Inside sphere */
00201       z = sqrtf(r*r - d*d);
00202    } else {           /* On hyperbola */
00203       const T t = r / 1.41421356237309504880f;
00204       z = t*t / d;
00205    }
00206    return z;
00207 }
00208 
00209 /// The trackball function
00210 template <class T>
00211 quat<T>   trackball(const vec2<T> & pt1, 
00212                     const vec2<T> & pt2, 
00213                     const T trackballsize)
00214 {
00215 
00216    vec3<T> a; // Axis of rotation
00217    T phi;  // how much to rotate about axis
00218    vec3<T> d;
00219    T t;
00220 
00221    if (pt1 == pt2) 
00222    {
00223       // Zero rotation
00224       return quatf();
00225    }
00226 
00227    // First, figure out z-coordinates for projection of P1 and P2 to
00228    // deformed sphere
00229 
00230    vec3<T> p1(pt1.x,pt1.y,tb_project_to_sphere(trackballsize,pt1.x,pt1.y));
00231    vec3<T> p2(pt2.x,pt2.y,tb_project_to_sphere(trackballsize,pt2.x,pt2.y));
00232 
00233    //  Now, we want the cross product of P1 and P2
00234    //a = p2.cross(p1);
00235    a = p1.cross(p2);
00236 
00237    //  Figure out how much to rotate around that axis.
00238    d = p1 - p2;
00239    t = d.norm() / (trackballsize);
00240 
00241    // Avoid problems with out-of-control values...
00242 
00243    if (t > 1)
00244       t = 1;
00245    if (t < -1) 
00246       t = -1;
00247    phi = 2 * asin(t);
00248 
00249    return quat<T>(phi,a);
00250 }
00251 
00252 
00253 
00254 
00255 }//end namespace gutz
00256 
00257 
00258 #endif
00259 

Send questions, comments, and bug reports to:
jmk