[852] | 1 | /*
|
---|
| 2 | glh - is a platform-indepenedent C++ OpenGL helper library
|
---|
| 3 |
|
---|
| 4 |
|
---|
| 5 | Copyright (c) 2000 Cass Everitt
|
---|
| 6 | Copyright (c) 2001 NVIDIA Corporation
|
---|
| 7 | All rights reserved.
|
---|
| 8 |
|
---|
| 9 | Redistribution and use in source and binary forms, with or
|
---|
| 10 | without modification, are permitted provided that the following
|
---|
| 11 | conditions are met:
|
---|
| 12 |
|
---|
| 13 | * Redistributions of source code must retain the above
|
---|
| 14 | copyright notice, this list of conditions and the following
|
---|
| 15 | disclaimer.
|
---|
| 16 |
|
---|
| 17 | * Redistributions in binary form must reproduce the above
|
---|
| 18 | copyright notice, this list of conditions and the following
|
---|
| 19 | disclaimer in the documentation and/or other materials
|
---|
| 20 | provided with the distribution.
|
---|
| 21 |
|
---|
| 22 | * The names of contributors to this software may not be used
|
---|
| 23 | to endorse or promote products derived from this software
|
---|
| 24 | without specific prior written permission.
|
---|
| 25 |
|
---|
| 26 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
---|
| 27 | ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
---|
| 28 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
---|
| 29 | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
---|
| 30 | REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 31 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
---|
| 32 | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
---|
| 33 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
---|
| 34 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
---|
| 35 | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
---|
| 36 | ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
---|
| 37 | POSSIBILITY OF SUCH DAMAGE.
|
---|
| 38 |
|
---|
| 39 |
|
---|
| 40 | Cass Everitt - cass@r3.nu
|
---|
| 41 | */
|
---|
| 42 |
|
---|
| 43 | #ifndef GLH_INTERACTORS_H
|
---|
| 44 | #define GLH_INTERACTORS_H
|
---|
| 45 |
|
---|
| 46 | #include <math.h>
|
---|
| 47 |
|
---|
| 48 | namespace glh
|
---|
| 49 | {
|
---|
| 50 |
|
---|
| 51 | class translator
|
---|
| 52 | {
|
---|
| 53 | public:
|
---|
| 54 | translator()
|
---|
| 55 | {
|
---|
| 56 | scale = .01f;
|
---|
| 57 | invert_increment = false;
|
---|
| 58 | parent_rotation = 0;
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | void pan (int dx, int dy) { update(dx, dy, 0); }
|
---|
| 62 | void dolly(int dz) { update( 0, 0, dz); }
|
---|
| 63 |
|
---|
| 64 | void update(int dx, int dy, int dz)
|
---|
| 65 | {
|
---|
| 66 | vec3f v(dx, dy, dz);
|
---|
| 67 |
|
---|
| 68 | // apply parent rotation
|
---|
| 69 | if(parent_rotation != 0)
|
---|
| 70 | parent_rotation->mult_vec(v);
|
---|
| 71 |
|
---|
| 72 | if(invert_increment)
|
---|
| 73 | t -= v * scale;
|
---|
| 74 | else
|
---|
| 75 | t += v * scale;
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 |
|
---|
| 79 | matrix4f get_transform()
|
---|
| 80 | {
|
---|
| 81 | matrix4f m;
|
---|
| 82 | m.set_translate(t);
|
---|
| 83 | return m;
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | matrix4f get_inverse_transform()
|
---|
| 87 | {
|
---|
| 88 | matrix4f m;
|
---|
| 89 | m.set_translate(-t);
|
---|
| 90 | return m;
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | bool invert_increment;
|
---|
| 94 | const rotationf * parent_rotation;
|
---|
| 95 | vec3f t;
|
---|
| 96 | float scale;
|
---|
| 97 | };
|
---|
| 98 |
|
---|
| 99 |
|
---|
| 100 |
|
---|
| 101 | class trackball
|
---|
| 102 | {
|
---|
| 103 | public:
|
---|
| 104 | trackball()
|
---|
| 105 | {
|
---|
| 106 | r = rotationf(vec3f(0, 1, 0), 0);
|
---|
| 107 | centroid = vec3f(0,0,0);
|
---|
| 108 | scale = -.01f;
|
---|
| 109 | invert_increment = false;
|
---|
| 110 | parent_rotation = 0;
|
---|
| 111 | legacy_mode = false;
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | void rotate(int x0, int y0, int x1, int y1)
|
---|
| 115 | { update(x0, y0, x1, y1); }
|
---|
| 116 |
|
---|
| 117 | void update(int x0, int y0, int x1, int y1)
|
---|
| 118 | {
|
---|
| 119 | int dx = x1 - x0;
|
---|
| 120 | int dy = y1 - y0;
|
---|
| 121 | if(dx == 0 && dy == 0)
|
---|
| 122 | {
|
---|
| 123 | incr = rotationf();
|
---|
| 124 | return;
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | if(legacy_mode)
|
---|
| 128 | {
|
---|
| 129 | vec3f v(dy, -dx, 0);
|
---|
| 130 | float len = v.normalize();
|
---|
| 131 | if(parent_rotation != 0)
|
---|
| 132 | parent_rotation->mult_vec(v);
|
---|
| 133 | //r.mult_dir(vec3f(v), v);
|
---|
| 134 | if(invert_increment)
|
---|
| 135 | incr.set_value(v, -len * scale);
|
---|
| 136 | else
|
---|
| 137 | incr.set_value(v, len * scale);
|
---|
| 138 | }
|
---|
| 139 | else
|
---|
| 140 | {
|
---|
| 141 | vec3f a(x0, y0, 0);
|
---|
| 142 | vec3f b(x1, y1, 0);
|
---|
| 143 | a -= offset;
|
---|
| 144 | b -= offset;
|
---|
| 145 | a /= radius;
|
---|
| 146 | b /= radius;
|
---|
| 147 |
|
---|
| 148 | float tmpscale = 1;
|
---|
| 149 | a[2] = pow(2.0f, -0.5f * a.length());
|
---|
| 150 | a.normalize();
|
---|
| 151 | b[2] = pow(2.0f, -0.5f * b.length());
|
---|
| 152 | b.normalize();
|
---|
| 153 |
|
---|
| 154 |
|
---|
| 155 |
|
---|
| 156 | vec3f axis = a.cross(b);
|
---|
| 157 | axis.normalize();
|
---|
| 158 |
|
---|
| 159 | float angle = acos(a.dot(b));
|
---|
| 160 |
|
---|
| 161 | if(parent_rotation != 0) parent_rotation->mult_vec(axis);
|
---|
| 162 |
|
---|
| 163 | if(invert_increment)
|
---|
| 164 | incr.set_value(axis, -angle * tmpscale);
|
---|
| 165 | else
|
---|
| 166 | incr.set_value(axis, angle * tmpscale);
|
---|
| 167 |
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 | // fixme: shouldn't operator*() preserve 'r' in this case?
|
---|
| 171 | if(incr[3] != 0)
|
---|
| 172 | r = incr * r;
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | void increment_rotation()
|
---|
| 176 | {
|
---|
| 177 | // fixme: shouldn't operator*() preserve 'r' in this case?
|
---|
| 178 | if(incr[3] != 0)
|
---|
| 179 | r = incr * r;
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | matrix4f get_transform()
|
---|
| 183 | {
|
---|
| 184 | matrix4f mt, mr, minvt;
|
---|
| 185 | mt.set_translate(centroid);
|
---|
| 186 | r.get_value(mr);
|
---|
| 187 | minvt.set_translate(-centroid);
|
---|
| 188 | return mt * mr * minvt;
|
---|
| 189 | }
|
---|
| 190 |
|
---|
| 191 | matrix4f get_inverse_transform()
|
---|
| 192 | {
|
---|
| 193 | matrix4f mt, mr, minvt;
|
---|
| 194 | mt.set_translate(centroid);
|
---|
| 195 | r.inverse().get_value(mr);
|
---|
| 196 | minvt.set_translate(-centroid);
|
---|
| 197 | return mt * mr * minvt;
|
---|
| 198 | }
|
---|
| 199 |
|
---|
| 200 | bool invert_increment;
|
---|
| 201 | const rotationf * parent_rotation;
|
---|
| 202 | rotationf r;
|
---|
| 203 | vec3f centroid;
|
---|
| 204 | float scale;
|
---|
| 205 | bool legacy_mode;
|
---|
| 206 | rotationf incr;
|
---|
| 207 | float radius;
|
---|
| 208 | vec3f offset;
|
---|
| 209 | };
|
---|
| 210 |
|
---|
| 211 | }
|
---|
| 212 |
|
---|
| 213 | #endif
|
---|