Tulip  4.6.0
Better Visualization Through Research
library/tulip-gui/include/tulip/GlOffscreenRenderer.h
00001 /*
00002  *
00003  * This file is part of Tulip (www.tulip-software.org)
00004  *
00005  * Authors: David Auber and the Tulip development Team
00006  * from LaBRI, University of Bordeaux
00007  *
00008  * Tulip is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU Lesser General Public License
00010  * as published by the Free Software Foundation, either version 3
00011  * of the License, or (at your option) any later version.
00012  *
00013  * Tulip is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00016  * See the GNU General Public License for more details.
00017  *
00018  */
00019 ///@cond DOXYGEN_HIDDEN
00020 
00021 #ifndef GLOFFSCREENRENDERER_H_
00022 #define GLOFFSCREENRENDERER_H_
00023 
00024 #include <tulip/tulipconf.h>
00025 #include <tulip/Coord.h>
00026 #include <tulip/GlScene.h>
00027 
00028 #if defined(_MSC_VER)
00029 #include <Windows.h>
00030 #endif
00031 
00032 #if defined(__APPLE__)
00033 #include <OpenGL/gl.h>
00034 #else
00035 #include <GL/gl.h>
00036 #endif
00037 
00038 #include <QImage>
00039 class QGLFramebufferObject;
00040 
00041 namespace tlp {
00042 
00043 class GlSimpleEntity;
00044 class GlGraphComposite;
00045 
00046 /**
00047  * @brief Render a scene in an image or in a texture.
00048  *
00049  * Here is an example to render a graph in a QImage to use it as preview.
00050  * @code
00051  * //Get the renderer
00052  * glOffscreenRenderer *glOffscreenRenderer = GlOffscreenRenderer::getInstance();
00053  * //Define the viewport size. Needed to initialize the offscreen rederer.
00054  * glOffscreenRenderer->setViewPortSize(200,200);
00055  * //Erase old elements
00056  * glOffscreenRenderer->clearScene();
00057  * //Change the background color of the scene to white
00058  * glOffscreenRenderer->setSceneBackgroundColor(Color(255,255,255,255));
00059  * //Add
00060  * //Center and render the scene.
00061  * glOffscreenRenderer->renderScene(true);
00062  * //Get the result
00063  * QImage preview = glOffscreenRenderer->getGLTexture(true);
00064  * @endcode
00065  **/
00066 class TLP_QT_SCOPE GlOffscreenRenderer {
00067 
00068 public :
00069   /**
00070    * @brief Get the renderer instance.
00071    **/
00072   static GlOffscreenRenderer *getInstance();
00073 
00074   ~GlOffscreenRenderer();
00075 
00076   /**
00077    * @brief Define the viewport size.
00078    **/
00079   void setViewPortSize(const unsigned int viewPortWidth, const unsigned int viewPortHeight);
00080   unsigned int getViewportWidth();
00081   unsigned int getViewportHeight();
00082   bool frameBufferOk() const;
00083 
00084   GlScene *getScene() {
00085     return &scene;
00086   }
00087   void setZoomFactor(double zoomFactor) {
00088     this->zoomFactor = zoomFactor;
00089   }
00090   void setCameraCenter(const Coord &cameraCenter) {
00091     this->cameraCenter = cameraCenter;
00092   }
00093 
00094   void setSceneBackgroundColor(const Color &color);
00095   /**
00096    * @brief Add an entity to the scene. The scene become the owner of the object.
00097    **/
00098   void addGlEntityToScene(GlSimpleEntity *entity);
00099   /**
00100    * @brief Add a graph composite to the scene. The scene become the owner of the object.
00101    **/
00102   void addGraphCompositeToScene(GlGraphComposite *graphComposite);
00103 
00104   /**
00105    * @brief Add a graph to the scene. Just create a new GraphComposite and call GlGraphComposite.
00106    **/
00107   void addGraphToScene(Graph* graph);
00108 
00109   /**
00110    * @brief Delete all the elements of the scene and clear it.
00111    **/
00112   void clearScene();
00113 
00114   /**
00115    * @brief Render the scene in a buffer. You need to call this function before getting the result with getImage or getGlTexture.
00116    **/
00117   void renderScene(const bool centerScene = true, const bool antialiased = false);
00118 
00119   void renderExternalScene(GlScene *scene, const bool antialiased = false);
00120 
00121   /**
00122    * @brief Generate a QImage from the scene. You need to call the renderScene function before this function.
00123    **/
00124   QImage getImage();
00125   /**
00126    * @brief Generate an open gl texture from the scene. You need to call the renderScene function before this function.
00127    **/
00128   GLuint getGLTexture(const bool generateMipMaps = false);
00129 
00130 private :
00131 
00132   GlOffscreenRenderer();
00133 
00134   void initFrameBuffers(const bool antialiased);
00135 
00136   static GlOffscreenRenderer *instance;
00137 
00138   unsigned int vPWidth, vPHeight;
00139   QGLFramebufferObject *glFrameBuf, *glFrameBuf2;
00140   GlScene scene;
00141   GlLayer *mainLayer;
00142   unsigned int entitiesCpt;
00143   double zoomFactor;
00144   Coord cameraCenter;
00145   bool antialiasedFbo;
00146 
00147 };
00148 
00149 
00150 }
00151 
00152 
00153 #endif /* GLOFFSCREENRENDERER_H_ */
00154 ///@endcond
 All Classes Files Functions Variables Enumerations Enumerator Properties