Tulip  4.2.0
Better Visualization Through Research
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Groups Pages
GlOffscreenRenderer.h
1 /*
2  *
3  * This file is part of Tulip (www.tulip-software.org)
4  *
5  * Authors: David Auber and the Tulip development Team
6  * from LaBRI, University of Bordeaux 1 and Inria Bordeaux - Sud Ouest
7  *
8  * Tulip is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation, either version 3
11  * of the License, or (at your option) any later version.
12  *
13  * Tulip is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU General Public License for more details.
17  *
18  */
19 ///@cond DOXYGEN_HIDDEN
20 
21 
22 #ifndef GLOFFSCREENRENDERER_H_
23 #define GLOFFSCREENRENDERER_H_
24 
25 #include <tulip/GlScene.h>
26 #include <QtOpenGL/QGLFramebufferObject>
27 
28 namespace tlp {
29 
30 /**
31  * @brief Render a scene in an image or in a texture.
32  *
33  * Here is an example to render a graph in a QImage to use it as preview.
34  * @code
35  * //Get the renderer
36  * glOffscreenRenderer *glOffscreenRenderer = GlOffscreenRenderer::getInstance();
37  * //Define the viewport size. Needed to initialize the offscreen rederer.
38  * glOffscreenRenderer->setViewPortSize(200,200);
39  * //Erase old elements
40  * glOffscreenRenderer->clearScene();
41  * //Change the background color of the scene to white
42  * glOffscreenRenderer->setSceneBackgroundColor(Color(255,255,255,255));
43  * //Add
44  * //Center and render the scene.
45  * glOffscreenRenderer->renderScene(true);
46  * //Get the result
47  * QImage preview = glOffscreenRenderer->getGLTexture(true);
48  * @endcode
49  **/
50 class TLP_QT_SCOPE GlOffscreenRenderer {
51 
52 public :
53  /**
54  * @brief Get the renderer instance.
55  **/
56  static GlOffscreenRenderer *getInstance();
57 
58  ~GlOffscreenRenderer();
59 
60  /**
61  * @brief Define the viewport size.
62  **/
63  void setViewPortSize(const unsigned int viewPortWidth, const unsigned int viewPortHeight);
64  unsigned int getViewportWidth();
65  unsigned int getViewportHeight();
66  bool frameBufferOk() const {
67  return glFrameBuf->isValid();
68  }
69 
70  GlScene *getScene() {
71  return &scene;
72  }
73  void setZoomFactor(double zoomFactor) {
74  this->zoomFactor = zoomFactor;
75  }
76  void setCameraCenter(const Coord &cameraCenter) {
77  this->cameraCenter = cameraCenter;
78  }
79 
80  void setSceneBackgroundColor(const Color &color);
81  /**
82  * @brief Add an entity to the scene. The scene become the owner of the object.
83  **/
84  void addGlEntityToScene(GlSimpleEntity *entity);
85  /**
86  * @brief Add a graph composite to the scene. The scene become the owner of the object.
87  **/
88  void addGraphCompositeToScene(GlGraphComposite *graphComposite);
89 
90  /**
91  * @brief Add a graph to the scene. Just create a new GraphComposite and call GlGraphComposite.
92  **/
93  void addGraphToScene(Graph* graph);
94 
95  /**
96  * @brief Delete all the elements of the scene and clear it.
97  **/
98  void clearScene();
99 
100  /**
101  * @brief Render the scene in a buffer. You need to call this function before getting the result with getImage or getGlTexture.
102  **/
103  void renderScene(const bool centerScene = true, const bool antialiased = false);
104 
105  /**
106  * @brief Generate a QImage from the scene. You need to call the renderScene function before this function.
107  **/
108  QImage getImage();
109  /**
110  * @brief Generate an open gl texture from the scene. You need to call the renderScene function before this function.
111  **/
112  GLuint getGLTexture(const bool generateMipMaps = false);
113 
114 private :
115 
116  GlOffscreenRenderer();
117 
118  static GlOffscreenRenderer *instance;
119 
120  unsigned int vPWidth, vPHeight;
121  QGLFramebufferObject *glFrameBuf, *glFrameBuf2;
122  GlScene scene;
123  GlLayer *mainLayer;
124  unsigned int entitiesCpt;
125  double zoomFactor;
126  Coord cameraCenter;
127  bool antialiasedFbo;
128 
129 };
130 
131 
132 }
133 
134 
135 #endif /* GLOFFSCREENRENDERER_H_ */
136 ///@endcond