/**************************************************************************** * VCGLib o o * * Visual and Computer Graphics Library o o * * _ O _ * * Copyright(C) 2004 \/)\/ * * Visual Computing Lab /\/| * * ISTI - Italian National Research Council | * * \ * * All rights reserved. * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License (http://www.gnu.org/licenses/gpl.txt) * * for more details. * * * ****************************************************************************/ /**************************************************************************** History $Log: not supported by cvs2svn $ Revision 1.15 2006/02/13 13:15:52 cignoni Added Scale and Translate methods. Added many drawing hints and raised the default num. of steps when drawing circles. Added MouseDown without coords (for remembering changes of keys modifiers) Added ZMode to the default modes under Alt+left Added DrawPostApply (to be completed) Revision 1.14 2005/10/17 01:29:46 cignoni Main restructuring. Removed the Draw function and slightly changed the meaning of the trackball itself. See the notes at the beginning of trackball.h Revision 1.13 2005/04/17 17:48:24 ganovelli modes deallocation commented (quick and dirty solution..to debug) Revision 1.12 2004/12/17 10:28:10 ricciodimare *** empty log message *** Revision 1.11 2004/09/28 15:30:12 ponchio Added a 'else'. Revision 1.10 2004/09/09 14:38:52 ponchio #include #include #include "trackball.h" #include #include using namespace vcg; #include //debug! using namespace std; Transform::Transform() { track.SetIdentity(); radius=1.0f; center=Point3f(0,0,0); } Trackball::Trackball(): current_button(0), current_mode(NULL), dragging(false), spinnable(true), spinning(false), history_size(10) { //here we add mode modes[0] = NULL; modes[BUTTON_LEFT] = new SphereMode(); modes[BUTTON_LEFT | KEY_CTRL] = new PlaneMode(Plane3f(0, Point3f(1, 0, 0))); modes[BUTTON_MIDDLE] = new PlaneMode(Plane3f(0, Point3f(1, 0, 0))); modes[BUTTON_LEFT | KEY_SHIFT] = new ScaleMode(); modes[BUTTON_LEFT | KEY_ALT ] = new ZMode(); modes[WHEEL] = new ScaleMode(); SetCurrentAction(); } Trackball::~Trackball() { map::iterator i; //for(i = modes.begin(); i != modes.end(); i++) // delete (*i).second; } void Trackball::SetIdentity() { track.SetIdentity(); Reset(); } void Trackball::SetPosition(const Point3f &c, int /* millisec */) { center = c; } void Trackball::GetView() { camera.GetView(); } void Trackball::DrawPostApply() { glPushMatrix(); glTranslate(center); glMultMatrix(track.InverseMatrix()); Matrix44f r; track.rot.ToMatrix(r); glMultMatrix(r); DrawIcon(); glTranslate(-center); glMultMatrix(track.Matrix()); } void Trackball::Apply(bool ToDraw) { glTranslate(center); if(ToDraw) { if(DH.DrawTrack) { glBegin(GL_LINE_STRIP); for(vector::iterator vi=Hits.begin();vi!=Hits.end();++vi) glVertex(*vi); glEnd(); } glPushMatrix(); Matrix44f r; track.rot.ToMatrix(r); glMultMatrix(r); DrawIcon(); glPopMatrix(); } glMultMatrix(track.Matrix()); glTranslate(-center); } void Trackball::ApplyInverse() { glTranslate(center); glMultMatrix(track.InverseMatrix()); glTranslate(-center); } void Trackball::Scale(const float s) { track.sca*=s; } void Trackball::Translate(Point3f tr) { Matrix44f m; track.rot.ToMatrix(m); track.tra = last_track.tra + Inverse(m)*tr/track.sca;; } /***************************************************************/ void Trackball::DrawCircle() { int nside=DH.CircleStep; const double pi2=3.14159265*2.0; glBegin(GL_LINE_LOOP); for(double i=0;iApply(this, Point3f(float(x), float(y), 0)); } void Trackball::MouseUp(int /* x */, int /* y */, int button) { current_button &= (~button); SetCurrentAction(); } // it assumes that a notch of 1.0 is a single step of the wheel void Trackball::MouseWheel(float notch ) { if(current_mode == NULL) { SphereMode tm; tm.TrackMode::Apply(this, notch); } else current_mode->Apply(this, notch); } void Trackball::ButtonDown(Trackball::Button button) { current_button |= button; SetCurrentAction(); } void Trackball::ButtonUp(Trackball::Button button) { current_button &= (~button); SetCurrentAction(); } //spinning interface void Trackball::SetSpinnable(bool /* on*/ ){} bool Trackball::IsSpinnable() { return spinnable; } void Trackball::SetSpinning(Quaternionf &/* spin*/){} void Trackball::StopSpinning(){} bool Trackball::IsSpinning() { return spinning; } //interfaccia navigation: void Trackball::Back(){} void Trackball::Forward(){} void Trackball::Home(){} void Trackball::HistorySize(int /* lenght */){} void Trackball::SetCurrentAction() { //I use strict matching. assert(modes.count(0)); if(!modes.count(current_button)) current_mode = NULL; else current_mode = modes[current_button]; last_point = Point3f(0, 0, -1); last_track = track; // last_view = view; } ////return center of trackball in Window coordinates. //Point3f Trackball::ScreenOrigin() { // return camera.Project(ModelOrigin()); //} //return center of trackball in Model coordinates //Point3f Trackball::ModelOrigin() { // return center; //} //Matrix44f Trackball::ScreenToModel() { // return camera.inverse; //} // //Similarityf Trackball::ModelToLocal() { // Similarityf m = local * last_track; // return m; //}