glh_interactors.h
Go to the documentation of this file.
00001 /*
00002     glh - is a platform-indepenedent C++ OpenGL helper library 
00003 
00004 
00005     Copyright (c) 2000 Cass Everitt
00006         Copyright (c) 2001 NVIDIA Corporation
00007     All rights reserved.
00008 
00009     Redistribution and use in source and binary forms, with or
00010         without modification, are permitted provided that the following
00011         conditions are met:
00012 
00013      * Redistributions of source code must retain the above
00014            copyright notice, this list of conditions and the following
00015            disclaimer.
00016 
00017      * Redistributions in binary form must reproduce the above
00018            copyright notice, this list of conditions and the following
00019            disclaimer in the documentation and/or other materials
00020            provided with the distribution.
00021 
00022      * The names of contributors to this software may not be used
00023            to endorse or promote products derived from this software
00024            without specific prior written permission. 
00025 
00026        THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00027            ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00028            LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00029            FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00030            REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00031            INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00032            BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00033            LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00034            CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00035            LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00036            ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
00037            POSSIBILITY OF SUCH DAMAGE. 
00038 
00039 
00040     Cass Everitt - cass@r3.nu
00041 */
00042 
00043 #ifndef GLH_INTERACTORS_H
00044 #define GLH_INTERACTORS_H
00045 
00046 #include <math.h>
00047 
00048 namespace glh
00049 {
00050 
00051   class translator
00052   {
00053   public:
00054         translator()
00055         {
00056           scale = .01f;
00057           invert_increment = false;
00058           parent_rotation = 0;
00059         }
00060 
00061         void pan  (int dx, int dy) { update(dx, dy,  0); }
00062         void dolly(int dz)         { update( 0,  0, dz); }
00063 
00064         void update(int dx, int dy, int dz)
00065         {
00066           vec3f v(dx, dy, dz);
00067 
00068           // apply parent rotation
00069           if(parent_rotation != 0)
00070                   parent_rotation->mult_vec(v);
00071 
00072           if(invert_increment)
00073                   t -= v * scale;
00074           else
00075                   t += v * scale;
00076         }
00077 
00078 
00079         matrix4f get_transform()
00080         {
00081                 matrix4f m;
00082                 m.set_translate(t);
00083                 return m;
00084         }       
00085 
00086         matrix4f get_inverse_transform()
00087         {
00088                 matrix4f m;
00089                 m.set_translate(-t);
00090                 return m;
00091         }       
00092 
00093         bool invert_increment;
00094         const rotationf * parent_rotation;
00095         vec3f t;
00096         float scale;
00097   };
00098 
00099 
00100 
00101   class trackball
00102   {
00103   public:
00104         trackball()
00105         {
00106                 r = rotationf(vec3f(0, 1, 0), 0);
00107                 centroid = vec3f(0,0,0);
00108                 scale = -.01f;
00109                 invert_increment = false;
00110                 parent_rotation = 0;
00111                 legacy_mode = false;
00112         }
00113 
00114         void rotate(int x0, int y0, int x1, int y1)
00115         { update(x0, y0, x1, y1); }
00116 
00117         void update(int x0, int y0, int x1, int y1)
00118         {
00119                 int dx = x1 - x0;
00120                 int dy = y1 - y0;
00121         if(dx == 0 && dy == 0)
00122                 {
00123                         incr = rotationf();
00124                         return;
00125                 }
00126                 
00127                 if(legacy_mode)
00128                 {
00129                         vec3f v(dy, -dx, 0);
00130                         float len = v.normalize();
00131                         if(parent_rotation != 0)
00132                                 parent_rotation->mult_vec(v);
00133                         //r.mult_dir(vec3f(v), v);
00134                         if(invert_increment)
00135                                 incr.set_value(v, -len * scale);
00136                         else
00137                                 incr.set_value(v, len * scale);
00138                 }
00139                 else
00140                 {
00141                         vec3f a(x0, y0, 0);
00142                         vec3f b(x1, y1, 0);
00143                         a -= offset;
00144                         b -= offset;
00145                         a /= radius;
00146                         b /= radius;
00147 
00148                         float tmpscale = 1;
00149                         a[2] = pow(2.0f, -0.5f * a.length());
00150                         a.normalize();
00151                         b[2] = pow(2.0f, -0.5f * b.length());
00152                         b.normalize();
00153 
00154 
00155 
00156                         vec3f axis = a.cross(b);
00157                         axis.normalize();
00158 
00159                         float angle = acos(a.dot(b));
00160 
00161                         if(parent_rotation != 0) parent_rotation->mult_vec(axis);
00162 
00163                         if(invert_increment)
00164                                 incr.set_value(axis, -angle * tmpscale);
00165                         else
00166                                 incr.set_value(axis, angle * tmpscale);
00167                                 
00168                 }
00169 
00170                 // fixme: shouldn't operator*() preserve 'r' in this case? 
00171                 if(incr[3] != 0)
00172                         r = incr * r;
00173         }
00174 
00175         void increment_rotation()
00176         {
00177                 // fixme: shouldn't operator*() preserve 'r' in this case? 
00178                 if(incr[3] != 0)
00179                         r = incr * r;
00180         }
00181 
00182         matrix4f get_transform()
00183         {
00184                 matrix4f mt, mr, minvt;
00185                 mt.set_translate(centroid);
00186                 r.get_value(mr);
00187                 minvt.set_translate(-centroid);
00188                 return mt * mr * minvt;
00189         }
00190 
00191         matrix4f get_inverse_transform()
00192         {
00193                 matrix4f mt, mr, minvt;
00194                 mt.set_translate(centroid);
00195                 r.inverse().get_value(mr);
00196                 minvt.set_translate(-centroid);
00197                 return mt * mr * minvt;
00198         }
00199 
00200         bool invert_increment;
00201         const rotationf * parent_rotation;
00202         rotationf r;
00203         vec3f centroid;
00204         float scale;
00205         bool legacy_mode;
00206         rotationf incr;
00207         float radius;
00208         vec3f offset;
00209   }; 
00210 
00211 }
00212 
00213 #endif


nao_openni
Author(s): Bener SUAY
autogenerated on Mon Jan 6 2014 11:27:50