keyframe_interpolation.cpp
Go to the documentation of this file.
00001 
00006 /*****************************************************************************
00007 ** Includes
00008 *****************************************************************************/
00009 
00010 #include <qapplication.h>
00011 #include <QGLViewer/qglviewer.h>
00012 #include <QKeyEvent>
00013 
00014 using namespace qglviewer;
00015 
00016 /*****************************************************************************
00017  ** KeyframeInterpolation
00018  *****************************************************************************/
00019 
00020 class KeyframeInterpolation : public QGLViewer
00021 {
00022 public :
00023   KeyframeInterpolation();
00024 
00025 protected :
00026   virtual void draw();
00027   virtual void keyPressEvent(QKeyEvent *e);
00028   virtual QString helpString() const;
00029 
00030 private :
00031   qglviewer::ManipulatedFrame** keyFrame_;
00032   qglviewer::KeyFrameInterpolator kfi_;
00033   const int nbKeyFrames;
00034   int currentKF_;
00035 };
00036 
00037 /*****************************************************************************
00038  ** KeyframeInterpolation
00039  *****************************************************************************/
00040 
00041 KeyframeInterpolation::KeyframeInterpolation()
00042   : nbKeyFrames(4)
00043 {
00044   restoreStateFromFile();
00045 
00046   // myFrame is the Frame that will be interpolated.
00047   Frame* myFrame = new Frame();
00048 
00049   // Set myFrame as the KeyFrameInterpolator interpolated Frame.
00050   kfi_.setFrame(myFrame);
00051   kfi_.setLoopInterpolation();
00052 
00053   // An array of manipulated (key) frames.
00054   keyFrame_ = new ManipulatedFrame*[nbKeyFrames];
00055 
00056   // Create an initial path
00057   for (int i=0; i<nbKeyFrames; i++)
00058         {
00059           keyFrame_[i] = new ManipulatedFrame();
00060           keyFrame_[i]->setPosition(-1.0 + 2.0*i/(nbKeyFrames-1), 0.0, 0.0);
00061           kfi_.addKeyFrame(keyFrame_[i]);
00062         }
00063 
00064   currentKF_ = 0;
00065   setManipulatedFrame(keyFrame_[currentKF_]);
00066 
00067   // Enable direct frame manipulation when the mouse hovers.
00068   setMouseTracking(true);
00069 
00070   setKeyDescription(Qt::Key_Plus, "Increases interpolation speed");
00071   setKeyDescription(Qt::Key_Minus, "Decreases interpolation speed");
00072   setKeyDescription(Qt::Key_Left, "Selects previous key frame");
00073   setKeyDescription(Qt::Key_Right, "Selects next key frame");
00074   setKeyDescription(Qt::Key_Return, "Starts/stops interpolation");
00075 
00076   help();
00077 
00078   connect(&kfi_, SIGNAL(interpolated()), SLOT(updateGL()));
00079   kfi_.startInterpolation();
00080 }
00081 
00082 QString KeyframeInterpolation::helpString() const
00083 {
00084   QString text("<h2>K e y F r a m e s</h2>");
00085   text += "A <i>KeyFrameInterpolator</i> holds an interpolated path defined by key frames. ";
00086   text += "It can then smoothly make its associed frame follow that path. Key frames can interactively be manipulated, even ";
00087   text += "during interpolation.<br><br>";
00088   text += "Note that the camera holds 12 such keyFrameInterpolators, binded to F1-F12. Press <b>Alt+Fx</b> to define new key ";
00089   text += "frames, and then press <b>Fx</b> to make the camera follow the path. Press <b>C</b> to visualize these paths.<br><br>";
00090   text += "<b>+/-</b> changes the interpolation speed. Negative values are allowed.<br><br>";
00091   text += "<b>Return</b> starts-stops the interpolation.<br><br>";
00092   text += "Use the left and right arrows to change the manipulated KeyFrame. ";
00093   text += "Press <b>Control</b> to move it or simply hover over it.";
00094   return text;
00095 }
00096 
00097 void KeyframeInterpolation::keyPressEvent(QKeyEvent *e)
00098 {
00099   switch (e->key())
00100         {
00101         case Qt::Key_Left :
00102           currentKF_ = (currentKF_+nbKeyFrames-1) % nbKeyFrames;
00103           setManipulatedFrame(keyFrame_[currentKF_]);
00104           updateGL();
00105           break;
00106         case Qt::Key_Right :
00107           currentKF_ = (currentKF_+1) % nbKeyFrames;
00108           setManipulatedFrame(keyFrame_[currentKF_]);
00109           updateGL();
00110           break;
00111         case Qt::Key_Return :
00112           kfi_.toggleInterpolation();
00113           break;
00114         case Qt::Key_Plus :
00115           kfi_.setInterpolationSpeed(kfi_.interpolationSpeed()+0.25);
00116           break;
00117         case Qt::Key_Minus :
00118           kfi_.setInterpolationSpeed(kfi_.interpolationSpeed()-0.25);
00119           break;
00120         // case Qt::Key_C :
00121           // kfi_.setClosedPath(!kfi_.closedPath());
00122           // break;
00123         default:
00124           QGLViewer::keyPressEvent(e);
00125         }
00126 }
00127 
00128 void KeyframeInterpolation::draw()
00129 {
00130   // Draw interpolated frame
00131   glPushMatrix();
00132   glMultMatrixd(kfi_.frame()->matrix());
00133   drawAxis(0.3f);
00134   glPopMatrix();
00135 
00136   kfi_.drawPath(5, 10);
00137 
00138   for (int i=0; i<nbKeyFrames; ++i)
00139         {
00140           glPushMatrix();
00141           glMultMatrixd(kfi_.keyFrame(i).matrix());
00142 
00143           if ((i == currentKF_) || (keyFrame_[i]->grabsMouse()))
00144         drawAxis(0.4f);
00145           else
00146         drawAxis(0.2f);
00147 
00148           glPopMatrix();
00149         }
00150 }
00151 
00152 /*****************************************************************************
00153 ** Main
00154 *****************************************************************************/
00155 
00156 int main(int argc, char** argv)
00157 {
00158   QApplication application(argc,argv);
00159 
00160   KeyframeInterpolation viewer;
00161 
00162   viewer.setWindowTitle("keyFrames");
00163 
00164   viewer.show();
00165 
00166   return application.exec();
00167 }


qglv_gallery
Author(s): Daniel Stonier
autogenerated on Sat Jun 18 2016 08:19:24