manipulated_frame.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 <QGLViewer/manipulatedFrame.h>
00013 
00014 using namespace qglviewer;
00015 
00016 /*****************************************************************************
00017 ** Manipulated Frame
00018 *****************************************************************************/
00019 
00020 class MyManipulatedFrame : public QGLViewer
00021 {
00022 protected :
00023   virtual void init();
00024   virtual void draw();
00025   virtual QString helpString() const;
00026 };
00027 
00028 /*****************************************************************************
00029 ** Manipulated Frame
00030 *****************************************************************************/
00031 
00032 static void drawSpiral()
00033 {
00034   const float nbSteps = 200.0;
00035   glBegin(GL_QUAD_STRIP);
00036   for (float i=0; i<nbSteps; ++i)
00037         {
00038           float ratio = i/nbSteps;
00039           float angle = 21.0*ratio;
00040           float c = cos(angle);
00041           float s = sin(angle);
00042           float r1 = 1.0 - 0.8*ratio;
00043           float r2 = 0.8 - 0.8*ratio;
00044           float alt = ratio - 0.5;
00045           const float nor = .5;
00046           const float up = sqrt(1.0-nor*nor);
00047           glColor3f(1.0-ratio, 0.2f , ratio);
00048           glNormal3f(nor*c, up, nor*s);
00049           glVertex3f(r1*c, alt, r1*s);
00050           glVertex3f(r2*c, alt+0.05, r2*s);
00051         }
00052   glEnd();
00053 }
00054 
00055 
00056 void MyManipulatedFrame::init()
00057 {
00058   setMouseBinding(Qt::ShiftModifier, Qt::LeftButton, QGLViewer::CAMERA, QGLViewer::ROTATE);
00059   setMouseBinding(Qt::ShiftModifier, Qt::RightButton, QGLViewer::CAMERA, QGLViewer::TRANSLATE);
00060   setMouseBinding(Qt::ShiftModifier, Qt::MidButton, QGLViewer::CAMERA, QGLViewer::ZOOM);
00061   setWheelBinding(Qt::ShiftModifier, QGLViewer::CAMERA, QGLViewer::ZOOM);
00062 
00063   setMouseBinding(Qt::NoModifier, Qt::LeftButton, QGLViewer::FRAME, QGLViewer::ROTATE);
00064   setMouseBinding(Qt::NoModifier, Qt::RightButton, QGLViewer::FRAME, QGLViewer::TRANSLATE);
00065   setMouseBinding(Qt::NoModifier, Qt::MidButton, QGLViewer::FRAME, QGLViewer::ZOOM);
00066   setWheelBinding(Qt::NoModifier, QGLViewer::FRAME, QGLViewer::ZOOM);
00067 
00068 #ifdef GL_RESCALE_NORMAL  // OpenGL 1.2 Only...
00069   glEnable(GL_RESCALE_NORMAL);
00070 #endif
00071 
00072   // Make sure the manipulatedFrame is not easily clipped by the zNear and zFar planes
00073   setSceneRadius(30);
00074   camera()->fitSphere(qglviewer::Vec(0,0,0), 1);
00075 
00076   // Add a manipulated frame to the viewer.
00077   // If you are not "using namespace qglqglviewer", you need
00078   // to specify: new qglviewer::ManipulatedFrame().
00079   setManipulatedFrame(new qglviewer::ManipulatedFrame());
00080 
00081   help();
00082   restoreStateFromFile();
00083 }
00084 
00085 void MyManipulatedFrame::draw()
00086 {
00087   // Here we are in the world coordinate system. Draw unit size axis.
00088   drawAxis();
00089 
00090   // Save the current model view matrix (not needed here in fact)
00091   glPushMatrix();
00092 
00093   // Multiply matrix to get in the frame coordinate system.
00094   glMultMatrixd(manipulatedFrame()->matrix());
00095 
00096   // Scale down the drawings
00097   glScalef(0.3f, 0.3f, 0.3f);
00098 
00099   // Draw an axis using the QGLViewer static function
00100   drawAxis();
00101 
00102   // Draws a frame-related spiral.
00103   drawSpiral();
00104 
00105   // Restore the original (world) coordinate system
00106   glPopMatrix();
00107 }
00108 
00109 QString MyManipulatedFrame::helpString() const
00110 {
00111   QString text("<h2>M a n i p u l a t e d F r a m e</h2>");
00112   text += "A <i>ManipulatedFrame</i> converts mouse gestures into <i>Frame</i> displacements. ";
00113   text += "In this example, such an object defines the position of the spiral that can hence be manipulated.<br><br>";
00114   text += "Adding two lines of code will then allow you to move the objects of ";
00115   text += "your scene using the mouse. The button bindings of the <i>ManipulatedFrame</i> ";
00116   text += "are the same than for the camera. Spinning is possible.<br><br>";
00117   text += "Default key bindings have been changed in this example : press <b>Shift</b> ";
00118   text += "while moving the mouse to move the camera instead of the ManipulatedFrame.";
00119   return text;
00120 }
00121 
00122 /*****************************************************************************
00123 ** Main
00124 *****************************************************************************/
00125 
00126 int main(int argc, char** argv)
00127 {
00128   QApplication application(argc,argv);
00129 
00130   MyManipulatedFrame viewer;
00131 
00132   viewer.setWindowTitle("QGL Demo - Manipulated Frame");
00133 
00134   viewer.show();
00135 
00136   return application.exec();
00137 }


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