thumbnail.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 <math.h>
00013 #include <QKeyEvent>
00014 
00015 /*****************************************************************************
00016 ** Interface
00017 *****************************************************************************/
00018 
00019 class Viewer : public QGLViewer
00020 {
00021 protected :
00022   virtual void draw();
00023   virtual void postDraw();
00024   virtual void init();
00025   virtual QString helpString() const;
00026   virtual void keyPressEvent(QKeyEvent *e);
00027 
00028   void drawThumbnail();
00029   void drawSpiral(bool thumb);
00030 
00031 private :
00032   bool thumbnail_;
00033 };
00034 
00035 /*****************************************************************************
00036  ** Implementation
00037  *****************************************************************************/
00038 
00039 using namespace std;
00040 
00041 void Viewer::draw()
00042 {
00043   drawSpiral(false);
00044 }
00045 
00046 // Draws a spiral. If thumb is true, the spiral is simplified and drawns in wireframe.
00047 void Viewer::drawSpiral(bool thumb)
00048 {
00049   float nbSteps;
00050   if (thumb)
00051     {
00052       nbSteps = 50.0;
00053       glColor3f(1.0, 1.0 ,1.0);
00054     }
00055   else
00056     nbSteps = 200.0;
00057 
00058 
00059   glBegin(GL_QUAD_STRIP);
00060   for (float i=0; i<nbSteps; ++i)
00061     {
00062       float ratio = i/nbSteps;
00063       float angle = 21.0*ratio;
00064       float c = cos(angle);
00065       float s = sin(angle);
00066       float r1 = 1.0 - 0.8*ratio;
00067       float r2 = 0.8 - 0.8*ratio;
00068       float alt = ratio - 0.5;
00069       const float nor = 0.5;
00070       const float up = sqrt(1.0-nor*nor);
00071       if (!thumb)
00072             glColor3f(1.0-ratio, 0.2f , ratio);
00073       glNormal3f(nor*c, up, nor*s);
00074       glVertex3f(r1*c, alt, r1*s);
00075       glVertex3f(r2*c, alt+0.05, r2*s);
00076     }
00077   glEnd();
00078 }
00079 
00080 // Manages the thumbnail display.
00081 void Viewer::drawThumbnail()
00082 {
00083   if (thumbnail_)
00084     {
00085       int viewport[4];
00086       int scissor[4];
00087 
00088       // The viewport and the scissor are changed to fit the lower left
00089       // corner. Original values are saved.
00090       glGetIntegerv(GL_VIEWPORT,viewport);
00091       glGetIntegerv(GL_SCISSOR_BOX,scissor);
00092 
00093       glViewport(0,0,width()/2,height()/2);
00094       glScissor(0,0,width()/2,height()/2);
00095 
00096       // The Z-buffer is cleared to make the thumbnail appear over the
00097       // original image.
00098       glClear(GL_DEPTH_BUFFER_BIT);
00099 
00100       // Here starts the drawing, with specific GL flags
00101       glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
00102       glDisable(GL_LIGHTING);
00103       drawSpiral(true);
00104       glEnable(GL_LIGHTING);
00105       glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
00106       // Here ends the drawing. OpenGL state is restored.
00107 
00108       // The viewport and the scissor are restored.
00109       glScissor(scissor[0],scissor[1],scissor[2],scissor[3]);
00110       glViewport(viewport[0],viewport[1],viewport[2],viewport[3]);
00111     }
00112 }
00113 // The thumbnail has to be drawn at the very end to allow a correct
00114 // display of the visual hints (axes, grid, etc).
00115 void Viewer::postDraw()
00116 {
00117   QGLViewer::postDraw();
00118   drawThumbnail();
00119 }
00120 
00121 void Viewer::keyPressEvent(QKeyEvent *e)
00122 {
00123   switch (e->key())
00124     {
00125     case Qt::Key_T : thumbnail_ = !thumbnail_; updateGL(); break;
00126     default        : QGLViewer::keyPressEvent(e);
00127     }
00128 }
00129 void Viewer::init()
00130 {
00131   // Restore previous viewer state.
00132   restoreStateFromFile();
00133 
00134   help();
00135 
00136   thumbnail_ = true;
00137 
00138   setKeyDescription(Qt::Key_T, "Toggles thumbnail display");
00139 }
00140 
00141 QString Viewer::helpString() const
00142 {
00143   QString text("<h2>T h u m b n a i l</h2>");
00144   text += "A thumbnailed view of the scene is displayed in the lower left corner.<br><br>";
00145   text += "Such display may be useful for illustration (e.g. to show the data structure) or to debug your ";
00146   text += "application. It uses <code>glScissor</code> and <code>glViewport</code> to restrict the display area.<br><br>";
00147   text += "Press <b>T</b> to toggle the thumbnail display";
00148   return text;
00149 }
00150 
00151 /*****************************************************************************
00152  ** Main
00153  *****************************************************************************/
00154 
00155 int main(int argc, char** argv)
00156 {
00157   QApplication application(argc,argv);
00158 
00159   Viewer viewer;
00160 
00161   viewer.setWindowTitle("thumbnail");
00162 
00163   viewer.show();
00164 
00165   return application.exec();
00166 }


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