background_image.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 <qimage.h>
00013 #include <qfiledialog.h>
00014 #include <QKeyEvent>
00015 
00016 /*****************************************************************************
00017  ** Namespaces
00018  *****************************************************************************/
00019 
00020 using namespace qglviewer;
00021 using namespace std;
00022 
00023 /*****************************************************************************
00024  ** Interface
00025  *****************************************************************************/
00026 
00027 class Viewer : public QGLViewer
00028 {
00029 protected :
00030   virtual void init();
00031   virtual void draw();
00032   virtual void drawBackground();
00033   virtual void keyPressEvent(QKeyEvent *e);
00034   virtual QString helpString() const;
00035 
00036   void loadImage();
00037 
00038 private :
00039   float ratio, u_max, v_max;
00040 
00041   bool background_;
00042 };
00043 
00044 /*****************************************************************************
00045 ** Implementation
00046 *****************************************************************************/
00047 
00048 void Viewer::init()
00049 {
00050   restoreStateFromFile();
00051 
00052   // Enable GL textures
00053   glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
00054   glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
00055 
00056   // Nice texture coordinate interpolation
00057   glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );
00058 
00059   u_max = 1.0;
00060   v_max = 1.0;
00061   ratio = 1.0;
00062   background_ = true;
00063 
00064   setKeyDescription(Qt::Key_L, "Loads a new background image");
00065   setKeyDescription(Qt::Key_B, "Toggles background display");
00066 
00067   loadImage();
00068   help();
00069   qWarning("fin init");
00070 }
00071 
00072 void Viewer::draw()
00073 {
00074   drawBackground();
00075 
00076   const float nbSteps = 200.0;
00077 
00078   glBegin(GL_QUAD_STRIP);
00079   for (float i=0; i<nbSteps; ++i)
00080     {
00081       float ratio = i/nbSteps;
00082       float angle = 21.0*ratio;
00083       float c = cos(angle);
00084       float s = sin(angle);
00085       float r1 = 1.0 - 0.8*ratio;
00086       float r2 = 0.8 - 0.8*ratio;
00087       float alt = ratio - 0.5;
00088       const float nor = .5;
00089       const float up = sqrt(1.0-nor*nor);
00090       glColor3f(1.0-ratio, 0.2f , ratio);
00091       glNormal3f(nor*c, up, nor*s);
00092       glVertex3f(r1*c, alt, r1*s);
00093       glVertex3f(r2*c, alt+0.05, r2*s);
00094     }
00095   glEnd();
00096 }
00097 
00098 void Viewer::drawBackground()
00099 {
00100   if (!background_)
00101     return;
00102 
00103   glDisable(GL_LIGHTING);
00104   glEnable(GL_TEXTURE_2D);
00105   glColor3f(1,1,1);
00106 
00107   startScreenCoordinatesSystem(true);
00108 
00109   // Draws the background quad
00110   glNormal3f(0.0, 0.0, 1.0);
00111   glBegin(GL_QUADS);
00112   glTexCoord2f(0.0,   1.0-v_max);       glVertex2i(0,0);
00113   glTexCoord2f(0.0,   1.0);             glVertex2i(0,height());
00114   glTexCoord2f(u_max, 1.0);             glVertex2i(width(),height());
00115   glTexCoord2f(u_max, 1.0-v_max);       glVertex2i(width(),0);
00116   glEnd();
00117 
00118   stopScreenCoordinatesSystem();
00119 
00120   // Depth clear is not absolutely needed. An other option would have been to draw the
00121   // QUAD with a 0.999 z value (z ranges in [0, 1[ with startScreenCoordinatesSystem()).
00122   glClear(GL_DEPTH_BUFFER_BIT);
00123   glDisable(GL_TEXTURE_2D);
00124   glEnable(GL_LIGHTING);
00125 }
00126 
00127 void Viewer::loadImage()
00128 {
00129   QString name = QFileDialog::getOpenFileName(this, "Select an image", ".", "Images (*.png *.xpm *.jpg)");
00130 
00131   // In case of Cancel
00132   if (name.isEmpty())
00133     return;
00134 
00135   QImage img(name);
00136 
00137   if (img.isNull())
00138     {
00139       qWarning("Unable to load file, unsupported file format");
00140       return;
00141     }
00142 
00143   qWarning("Loading %s, %dx%d pixels", name.toLatin1().constData(), img.width(), img.height());
00144 
00145   // 1E-3 needed. Just try with width=128 and see !
00146   int newWidth  = 1<<(int)(1+log(img.width() -1+1E-3) / log(2.0));
00147   int newHeight = 1<<(int)(1+log(img.height()-1+1E-3) / log(2.0));
00148 
00149   u_max = img.width()  / (float)newWidth;
00150   v_max = img.height() / (float)newHeight;
00151 
00152   if ((img.width()!=newWidth) || (img.height()!=newHeight))
00153     {
00154       qWarning("Image size set to %dx%d pixels", newWidth, newHeight);
00155       img = img.copy(0, 0, newWidth, newHeight);
00156     }
00157 
00158   ratio = newWidth / float(newHeight);
00159 
00160   QImage glImg = QGLWidget::convertToGLFormat(img);  // flipped 32bit RGBA
00161 
00162   // Bind the img texture...
00163   glTexImage2D(GL_TEXTURE_2D, 0, 4, glImg.width(), glImg.height(), 0,
00164                GL_RGBA, GL_UNSIGNED_BYTE, glImg.bits());
00165 }
00166 
00167 void Viewer::keyPressEvent(QKeyEvent *e)
00168 {
00169   switch (e->key())
00170     {
00171     case Qt::Key_L :
00172       loadImage();
00173       break;
00174     case Qt::Key_B :
00175       background_ = !background_;
00176       updateGL();
00177       break;
00178     default: QGLViewer::keyPressEvent(e);
00179     }
00180 }
00181 
00182 QString Viewer::helpString() const
00183 {
00184   QString text("<h2>B a c k g r o u n d I m a g e</h2>");
00185   text += "This example is derivated from textureViewer.<br><br>";
00186   text += "It displays a background image in the viewer using a texture.<br><br>";
00187   text += "Press <b>L</b> to load a new image, and <b>B</b> to toggle the background display.";
00188   return text;
00189 }
00190 
00191 /*****************************************************************************
00192  ** Main
00193  *****************************************************************************/
00194 
00195 int main(int argc, char** argv)
00196 {
00197   QApplication application(argc,argv);
00198   Viewer viewer;
00199   viewer.setWindowTitle("backgroundImage");
00200   viewer.show();
00201   return application.exec();
00202 }


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