| 1 | /**************************************************************************** |
| 2 | * VCGLib o o * |
| 3 | * Visual and Computer Graphics Library o o * |
| 4 | * _ O _ * |
| 5 | * Copyright(C) 2004 \/)\/ * |
| 6 | * Visual Computing Lab /\/| * |
| 7 | * ISTI - Italian National Research Council | * |
| 8 | * \ * |
| 9 | * All rights reserved. * |
| 10 | * * |
| 11 | * This program is free software; you can redistribute it and/or modify * |
| 12 | * it under the terms of the GNU General Public License as published by * |
| 13 | * the Free Software Foundation; either version 2 of the License, or * |
| 14 | * (at your option) any later version. * |
| 15 | * * |
| 16 | * This program is distributed in the hope that it will be useful, * |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 19 | * GNU General Public License (http://www.gnu.org/licenses/gpl.txt) * |
| 20 | * for more details. * |
| 21 | * * |
| 22 | ****************************************************************************/ |
| 23 | /**************************************************************************** |
| 24 | History |
| 25 | |
| 26 | $Log: trackball.cpp,v $ |
| 27 | Revision 1.27 2008/02/24 18:05:08 ponchio |
| 28 | Should work as before. I didn't test cylinder and other exotic modes. |
| 29 | |
| 30 | Revision 1.26 2008/02/24 14:37:00 ponchio |
| 31 | Restored trackball functionality. Not very much tested, and code will need some |
| 32 | cleanup. |
| 33 | |
| 34 | Revision 1.25 2008/02/22 18:57:46 benedetti |
| 35 | first attempt to correct after quaternion ToMatrix() inversion (does not work yet) |
| 36 | |
| 37 | Revision 1.24 2007/12/21 12:29:34 tarini |
| 38 | *** empty log message *** |
| 39 | |
| 40 | Revision 1.23 2007/10/12 14:02:39 corsini |
| 41 | solve memory leak in dtor |
| 42 | |
| 43 | Revision 1.22 2007/07/09 22:47:18 benedetti |
| 44 | Removed using namespace std and modified accordingly. |
| 45 | |
| 46 | Revision 1.21 2007/06/20 12:59:43 corsini |
| 47 | adjust wheel back-compatibility |
| 48 | |
| 49 | Revision 1.20 2007/06/13 17:15:08 benedetti |
| 50 | Added one-level undo system and sticky trackmodes. |
| 51 | |
| 52 | Revision 1.19 2007/05/15 15:00:27 benedetti |
| 53 | Moved the drawing code to trackmodes, some other minor changes |
| 54 | |
| 55 | Revision 1.18 2007/02/26 01:30:02 cignoni |
| 56 | Added reflection Name |
| 57 | |
| 58 | Revision 1.17 2007/01/15 15:04:15 tarini |
| 59 | added "ToAscii" and "SetFromAscii" methods to load/store current trackball status from/to ascii strings |
| 60 | (intended uses: clipboard operations and comments inside png snapshots!) |
| 61 | |
| 62 | Revision 1.16 2006/07/26 13:54:45 cignoni |
| 63 | Reversed the direction of wheel scaling and added middle mouse panning |
| 64 | |
| 65 | Revision 1.15 2006/02/13 13:15:52 cignoni |
| 66 | Added Scale and Translate methods. |
| 67 | Added many drawing hints and raised the default num. of steps when drawing circles. |
| 68 | Added MouseDown without coords (for remembering changes of keys modifiers) |
| 69 | Added ZMode to the default modes under Alt+left |
| 70 | Added DrawPostApply (to be completed) |
| 71 | |
| 72 | Revision 1.14 2005/10/17 01:29:46 cignoni |
| 73 | Main restructuring. Removed the Draw function and slightly changed the meaning of the trackball itself. |
| 74 | See the notes at the beginning of trackball.h |
| 75 | |
| 76 | Revision 1.13 2005/04/17 17:48:24 ganovelli |
| 77 | modes deallocation commented (quick and dirty solution..to debug) |
| 78 | |
| 79 | Revision 1.12 2004/12/17 10:28:10 ricciodimare |
| 80 | *** empty log message *** |
| 81 | |
| 82 | Revision 1.11 2004/09/28 15:30:12 ponchio |
| 83 | Added a 'else'. |
| 84 | |
| 85 | Revision 1.10 2004/09/09 14:38:52 ponchio |
| 86 | #include <gl... -> #include <GL... |
| 87 | |
| 88 | Revision 1.9 2004/07/11 22:06:55 cignoni |
| 89 | Added scaling by wheel |
| 90 | |
| 91 | Revision 1.8 2004/06/09 14:01:13 cignoni |
| 92 | Heavily restructured. To be completed only rotation works... |
| 93 | |
| 94 | Revision 1.7 2004/05/14 03:15:09 ponchio |
| 95 | Redesigned partial version. |
| 96 | |
| 97 | Revision 1.6 2004/05/12 20:55:18 ponchio |
| 98 | *** empty log message *** |
| 99 | |
| 100 | Revision 1.5 2004/05/07 12:46:08 cignoni |
| 101 | Restructured and adapted in a better way to opengl |
| 102 | |
| 103 | Revision 1.4 2004/04/07 10:54:10 cignoni |
| 104 | Commented out unused parameter names and other minor warning related issues |
| 105 | |
| 106 | Revision 1.3 2004/03/31 15:08:03 ponchio |
| 107 | Fixed current_action initialization. |
| 108 | |
| 109 | Revision 1.2 2004/03/25 14:55:25 ponchio |
| 110 | Adding copyright. |
| 111 | |
| 112 | |
| 113 | ****************************************************************************/ |
| 114 | |
| 115 | #include <GL/glew.h> |
| 116 | #include "trackball.h" |
| 117 | |
| 118 | #include <wrap/gl/math.h> |
| 119 | #include <wrap/gl/space.h> |
| 120 | |
| 121 | using namespace vcg; |
| 122 | |
| 123 | Transform::Transform() { |
| 124 | track.SetIdentity(); |
| 125 | radius=1.0f; |
| 126 | center=Point3f(0,0,0); |
| 127 | } |
| 128 | |
| 129 | Trackball::Trackball(): current_button(0), current_mode(NULL), inactive_mode(NULL), |
| 130 | dragging(false), spinnable(true), spinning(false), |
| 131 | history_size(10){ |
| 132 | setDefaultMapping (); |
| 133 | } |
| 134 | |
| 135 | Trackball::~Trackball() |
| 136 | { |
| 137 | std::map<int, TrackMode *>::iterator it; |
| 138 | for(it = modes.begin(); it != modes.end(); it++) |
| 139 | { |
| 140 | if ((*it).second) |
| 141 | delete (*it).second; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | |
| 146 | void Trackball::setDefaultMapping () { |
| 147 | inactive_mode = new InactiveMode (); |
| 148 | modes.clear (); |
| 149 | modes[0] = NULL; |
| 150 | modes[BUTTON_LEFT] = new SphereMode (); |
| 151 | modes[BUTTON_LEFT | KEY_CTRL] = new PanMode (); |
| 152 | modes[BUTTON_MIDDLE] = new PanMode (); |
| 153 | modes[BUTTON_LEFT | KEY_SHIFT] = new ScaleMode (); |
| 154 | modes[BUTTON_LEFT | KEY_ALT] = new ZMode (); |
| 155 | modes[WHEEL] = new ScaleMode (); |
| 156 | SetCurrentAction (); |
| 157 | } |
| 158 | |
| 159 | void Trackball::SetIdentity() { |
| 160 | track.SetIdentity(); |
| 161 | Reset(); |
| 162 | } |
| 163 | void Trackball::SetPosition(const Point3f &c, int /* millisec */) { |
| 164 | center = c; |
| 165 | } |
| 166 | |
| 167 | void Trackball::GetView() { |
| 168 | camera.GetView(); |
| 169 | } |
| 170 | |
| 171 | // the drawing code has been moved to the trackmodes |
| 172 | void Trackball::DrawPostApply() { |
| 173 | if(current_mode !=NULL){ |
| 174 | current_mode->Draw(this); |
| 175 | }else{ |
| 176 | assert(inactive_mode != NULL); |
| 177 | inactive_mode->Draw(this); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | void Trackball::Apply () { |
| 182 | glTranslate (center); |
| 183 | glMultMatrix (track.Matrix()); |
| 184 | glTranslate (-center); |
| 185 | } |
| 186 | |
| 187 | void Trackball::Apply(bool ToDraw) { |
| 188 | Apply(); |
| 189 | if(ToDraw){ |
| 190 | DrawPostApply(); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | |
| 195 | void Trackball::ApplyInverse() { |
| 196 | glTranslate(center); |
| 197 | glMultMatrix(track.InverseMatrix()); |
| 198 | glTranslate(-center); |
| 199 | } |
| 200 | |
| 201 | void Trackball::Scale(const float s) |
| 202 | { |
| 203 | track.sca*=s; |
| 204 | } |
| 205 | |
| 206 | void Trackball::Translate(Point3f tr) |
| 207 | { |
| 208 | Quaternionf irot = track.rot; |
| 209 | irot.Invert(); |
| 210 | track.tra = last_track.tra + irot.Rotate(tr)/track.sca; |
| 211 | } |
| 212 | |
| 213 | /***************************************************************/ |
| 214 | // DrawCircle () e DrawPlane() have been moved to trackutils.h |
| 215 | // the drawing code has been moved to the trackmodes |
| 216 | /* |
| 217 | void Trackball::DrawCircle() { |
| 218 | int nside=DH.CircleStep; |
| 219 | const double pi2=3.14159265*2.0; |
| 220 | glBegin(GL_LINE_LOOP); |
| 221 | for(double i=0;i<nside;i++){ |
| 222 | glNormal3d(cos(i*pi2/nside), sin(i*pi2/nside), 0.0); |
| 223 | glVertex3d(cos(i*pi2/nside), sin(i*pi2/nside), 0.0); |
| 224 | } |
| 225 | glEnd(); |
| 226 | DrawPlaneHandle(); |
| 227 | } |
| 228 | |
| 229 | void Trackball::DrawPlane() { |
| 230 | const int nl=10; |
| 231 | float w=5.0f/3.0f; |
| 232 | float u; |
| 233 | glBegin(GL_LINES); |
| 234 | glNormal3f(0.0,0.0,1.0); |
| 235 | for( u=-w; u<=w+0.01f; u+=2*w/nl){ |
| 236 | glVertex3f(-w, +u, 0); |
| 237 | glVertex3f(+w, +u, 0); |
| 238 | glVertex3f(+u, -w, 0); |
| 239 | glVertex3f(+u, +w, 0); |
| 240 | } |
| 241 | glEnd(); |
| 242 | } |
| 243 | */ |
| 244 | |
| 245 | void Trackball::ToAscii(char* result){ |
| 246 | float * f = (float*) &track; |
| 247 | sprintf(result, "trackball(%f,%f,%f,%f,%f,%f,%f,%f,%f)", |
| 248 | f[0],f[1],f[2],f[3],f[4],f[5],f[6],f[7],f[8] ); |
| 249 | } |
| 250 | |
| 251 | bool Trackball::SetFromAscii(const char * st){ |
| 252 | float * f = (float*) &track; |
| 253 | int res= sscanf(st, "trackball(%f,%f,%f,%f,%f,%f,%f,%f,%f)", |
| 254 | f+0,f+1,f+2,f+3,f+4,f+5,f+6,f+7,f+8 ); |
| 255 | |
| 256 | return (res==9); |
| 257 | |
| 258 | } |
| 259 | |
| 260 | // DrawPlaneHandle() e DrawIcon() have been moved to trackutils.h |
| 261 | // the drawing code has been moved to the trackmodes |
| 262 | /* |
| 263 | void Trackball::DrawPlaneHandle() { |
| 264 | float r=1.0; |
| 265 | float dr=r/10.0f; |
| 266 | glBegin(GL_LINE_STRIP); |
| 267 | glVertex3f(+r+dr, +r, 0.0); |
| 268 | glVertex3f(+r , +r+dr,0.0); |
| 269 | glVertex3f(+r-dr, +r, 0.0); |
| 270 | glVertex3f(+r , +r-dr,0.0); |
| 271 | glVertex3f(+r+dr, +r, 0.0); |
| 272 | glEnd(); |
| 273 | glBegin(GL_LINE_STRIP); |
| 274 | glVertex3f(-r+dr, -r, 0.0); |
| 275 | glVertex3f(-r , -r+dr,0.0); |
| 276 | glVertex3f(-r-dr, -r, 0.0); |
| 277 | glVertex3f(-r , -r-dr,0.0); |
| 278 | glVertex3f(-r+dr, -r, 0.0); |
| 279 | glEnd(); |
| 280 | } |
| 281 | |
| 282 | void Trackball::DrawIcon() { |
| 283 | glPushMatrix(); |
| 284 | |
| 285 | glScale(radius); |
| 286 | /// Here start the real drawing stuff |
| 287 | float amb[4] ={.3f,.3f,.3f,1.0f}; |
| 288 | float col[4] ={.5f,.5f,.8f,1.0f}; |
| 289 | //float col2[4]={.9f,.9f,1.0f,1.0f}; |
| 290 | glPushAttrib(GL_ENABLE_BIT | GL_LINE_BIT | GL_CURRENT_BIT | GL_LIGHTING_BIT); |
| 291 | |
| 292 | |
| 293 | if(current_mode == NULL ) glLineWidth(DH.LineWidthStill); |
| 294 | else glLineWidth(DH.LineWidthMoving); |
| 295 | |
| 296 | glEnable(GL_LIGHTING); |
| 297 | glEnable(GL_LIGHT0); |
| 298 | glEnable(GL_LINE_SMOOTH); |
| 299 | glEnable(GL_BLEND); |
| 300 | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
| 301 | glColor(DH.color); |
| 302 | |
| 303 | glMaterialfv(GL_FRONT_AND_BACK,GL_EMISSION,amb); |
| 304 | glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,col); |
| 305 | glPushMatrix(); |
| 306 | DrawCircle(); |
| 307 | glPushMatrix(); |
| 308 | |
| 309 | glRotatef(90,1,0,0); |
| 310 | DrawCircle(); |
| 311 | glRotatef(90,0,1,0); |
| 312 | DrawCircle(); |
| 313 | |
| 314 | glPopMatrix(); |
| 315 | glPopMatrix(); |
| 316 | |
| 317 | //glColor4f(1.0,.8f,.8f,1.0f); |
| 318 | |
| 319 | glPopAttrib(); |
| 320 | |
| 321 | glPopMatrix(); |
| 322 | } |
| 323 | */ |
| 324 | |
| 325 | void Trackball::Reset() { |
| 326 | track.SetIdentity(); |
| 327 | undo_track = track; |
| 328 | std::map<int, TrackMode *>::iterator i; |
| 329 | for(i = modes.begin(); i != modes.end(); i++){ |
| 330 | TrackMode * mode=(*i).second; |
| 331 | if(mode!=NULL) |
| 332 | mode->Reset(); |
| 333 | } |
| 334 | assert(inactive_mode != NULL); |
| 335 | inactive_mode->Reset(); |
| 336 | } |
| 337 | |
| 338 | //interface |
| 339 | void Trackball::MouseDown(int button) { |
| 340 | undo_track = track; |
| 341 | current_button |= button; |
| 342 | SetCurrentAction(); |
| 343 | Hits.clear(); |
| 344 | } |
| 345 | void Trackball::MouseDown(int x, int y, int button) { |
| 346 | undo_track = track; |
| 347 | current_button |= button; |
| 348 | SetCurrentAction(); |
| 349 | last_point = Point3f((float)x, (float)y, 0); |
| 350 | Hits.clear(); |
| 351 | } |
| 352 | |
| 353 | void Trackball::MouseMove(int x, int y) { |
| 354 | if(current_mode == NULL) return; |
| 355 | if(last_point[2] == -1) { //changed mode in the middle of moving |
| 356 | last_point = Point3f((float)x, (float)y, 0); |
| 357 | return; |
| 358 | } |
| 359 | undo_track = track; |
| 360 | current_mode->Apply(this, Point3f(float(x), float(y), 0)); |
| 361 | } |
| 362 | |
| 363 | void Trackball::MouseUp(int /* x */, int /* y */, int button) { |
| 364 | undo_track = track; |
| 365 | current_button &= (~button); |
| 366 | SetCurrentAction(); |
| 367 | } |
| 368 | |
| 369 | // it assumes that a notch of 1.0 is a single step of the wheel |
| 370 | void Trackball::MouseWheel(float notch) |
| 371 | { |
| 372 | undo_track = track; |
| 373 | int buttons = current_button; |
| 374 | current_button = WHEEL | (buttons&(KEY_SHIFT|KEY_CTRL|KEY_ALT)); |
| 375 | SetCurrentAction(); |
| 376 | if (current_mode == NULL) |
| 377 | { |
| 378 | ScaleMode scalemode; |
| 379 | scalemode.Apply (this, notch); |
| 380 | } |
| 381 | else |
| 382 | { |
| 383 | current_mode->Apply(this, notch); |
| 384 | } |
| 385 | current_button = buttons; |
| 386 | SetCurrentAction(); |
| 387 | } |
| 388 | |
| 389 | void Trackball::MouseWheel(float notch, int button) |
| 390 | { |
| 391 | undo_track = track; |
| 392 | current_button |= button; |
| 393 | SetCurrentAction(); |
| 394 | if (current_mode == NULL) { |
| 395 | ScaleMode scalemode; |
| 396 | scalemode.Apply (this, notch); |
| 397 | } else { |
| 398 | current_mode->Apply (this, notch); |
| 399 | } |
| 400 | current_button &= (~button); |
| 401 | SetCurrentAction (); |
| 402 | } |
| 403 | |
| 404 | void Trackball::ButtonDown(Trackball::Button button) { |
| 405 | bool old_sticky=false, new_sticky=false; |
| 406 | assert (modes.count (0)); |
| 407 | if ( ( modes.count (current_button) ) && ( modes[current_button] != NULL ) ) { |
| 408 | old_sticky = modes[current_button]->isSticky(); |
| 409 | } |
| 410 | current_button |= button; |
| 411 | if ( ( modes.count (current_button) ) && ( modes[current_button] != NULL ) ) { |
| 412 | new_sticky = modes[current_button]->isSticky(); |
| 413 | } |
| 414 | if ( old_sticky || new_sticky) |
| 415 | return; |
| 416 | SetCurrentAction(); |
| 417 | } |
| 418 | |
| 419 | void Trackball::ButtonUp(Trackball::Button button) { |
| 420 | bool old_sticky=false, new_sticky=false; |
| 421 | assert ( modes.count (0) ); |
| 422 | if ( ( modes.count (current_button) ) && ( modes[current_button] != NULL ) ) { |
| 423 | old_sticky = modes[current_button]->isSticky(); |
| 424 | } |
| 425 | current_button &= (~button); |
| 426 | if ( ( modes.count (current_button) ) && ( modes[current_button] != NULL ) ) { |
| 427 | new_sticky = modes[current_button]->isSticky(); |
| 428 | } |
| 429 | if ( old_sticky || new_sticky) |
| 430 | return; |
| 431 | SetCurrentAction(); |
| 432 | } |
| 433 | |
| 434 | void Trackball::Undo(){ |
| 435 | track = undo_track; |
| 436 | if(current_mode != NULL) |
| 437 | current_mode->Undo(); |
| 438 | } |
| 439 | |
| 440 | |
| 441 | //spinning interface |
| 442 | void Trackball::SetSpinnable(bool /* on*/ ){} |
| 443 | bool Trackball::IsSpinnable() { |
| 444 | return spinnable; |
| 445 | } |
| 446 | void Trackball::SetSpinning(Quaternionf &/* spin*/){} |
| 447 | void Trackball::StopSpinning(){} |
| 448 | bool Trackball::IsSpinning() { |
| 449 | return spinning; |
| 450 | } |
| 451 | |
| 452 | //navigation interface: |
| 453 | void Trackball::Back(){} |
| 454 | void Trackball::Forward(){} |
| 455 | void Trackball::Home(){} |
| 456 | void Trackball::HistorySize(int /* length */){} |
| 457 | |
| 458 | void Trackball::SetCurrentAction () |
| 459 | { |
| 460 | //I use strict matching. |
| 461 | assert (modes.count (0)); |
| 462 | if (!modes.count (current_button)) { |
| 463 | current_mode = NULL; |
| 464 | } else { |
| 465 | current_mode = modes[current_button]; |
| 466 | if(current_mode != NULL) |
| 467 | current_mode->SetAction(); |
| 468 | } |
| 469 | last_point = Point3f (0, 0, -1); |
| 470 | last_track = track; |
| 471 | } |
| 472 | |
| 473 | ////return center of trackball in Window coordinates. |
| 474 | //Point3f Trackball::ScreenOrigin() { |
| 475 | // return camera.Project(ModelOrigin()); |
| 476 | //} |
| 477 | |
| 478 | |
| 479 | //return center of trackball in Model coordinates |
| 480 | //Point3f Trackball::ModelOrigin() { |
| 481 | // return center; |
| 482 | //} |
| 483 | |
| 484 | //Matrix44f Trackball::ScreenToModel() { |
| 485 | // return camera.inverse; |
| 486 | //} |
| 487 | // |
| 488 | //Similarityf Trackball::ModelToLocal() { |
| 489 | // Similarityf m = local * last_track; |
| 490 | // return m; |
| 491 | //} |
| 492 |