Tulip  5.2.1
Large graphs analysis and drawing
GlOffscreenRenderer.h
1 /*
2  *
3  * This file is part of Tulip (http://tulip.labri.fr)
4  *
5  * Authors: David Auber and the Tulip development Team
6  * from LaBRI, University of Bordeaux
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 #ifndef GLOFFSCREENRENDERER_H_
22 #define GLOFFSCREENRENDERER_H_
23 
24 #include <tulip/tulipconf.h>
25 #include <tulip/Coord.h>
26 #include <tulip/GlScene.h>
27 
28 #include <tulip/OpenGlIncludes.h>
29 
30 #include <QImage>
31 
32 class QGLFramebufferObject;
33 
34 namespace tlp {
35 
36 class GlSimpleEntity;
37 class GlGraphComposite;
38 
39 /**
40  * @brief Render a scene in an image or in a texture.
41  *
42  * Here is an example to render a graph in a QImage to use it as preview.
43  * @code
44  * //Get the renderer
45  * glOffscreenRenderer *glOffscreenRenderer = GlOffscreenRenderer::getInstance();
46  * //Define the viewport size. Needed to initialize the offscreen rederer.
47  * glOffscreenRenderer->setViewPortSize(200,200);
48  * //Erase old elements
49  * glOffscreenRenderer->clearScene();
50  * //Change the background color of the scene to white
51  * glOffscreenRenderer->setSceneBackgroundColor(Color(255,255,255,255));
52  * //Add
53  * //Center and render the scene.
54  * glOffscreenRenderer->renderScene(true);
55  * //Get the result
56  * QImage preview = glOffscreenRenderer->getGLTexture(true);
57  * @endcode
58  **/
59 class TLP_QT_SCOPE GlOffscreenRenderer {
60 
61 public:
62  /**
63  * @brief Get the renderer instance.
64  **/
65  static GlOffscreenRenderer *getInstance();
66 
67  ~GlOffscreenRenderer();
68 
69  /**
70  * @brief Define the viewport size.
71  **/
72  void setViewPortSize(const unsigned int viewPortWidth, const unsigned int viewPortHeight);
73  unsigned int getViewportWidth();
74  unsigned int getViewportHeight();
75  bool frameBufferOk() const;
76 
77  GlScene *getScene() {
78  return &scene;
79  }
80  void setZoomFactor(double zoomFactor) {
81  this->zoomFactor = zoomFactor;
82  }
83  void setCameraCenter(const Coord &cameraCenter) {
84  this->cameraCenter = cameraCenter;
85  }
86 
87  void setSceneBackgroundColor(const Color &color);
88  /**
89  * @brief Add an entity to the scene. The scene become the owner of the object.
90  **/
91  void addGlEntityToScene(GlSimpleEntity *entity);
92  /**
93  * @brief Add a graph composite to the scene. The scene become the owner of the object.
94  **/
95  void addGraphCompositeToScene(GlGraphComposite *graphComposite);
96 
97  /**
98  * @brief Add a graph to the scene. Just create a new GraphComposite and call GlGraphComposite.
99  **/
100  void addGraphToScene(Graph *graph);
101 
102  /**
103  * @brief Delete all the elements of the scene and clear it.
104  **/
105  void clearScene(bool deleteGlEntities = false);
106 
107  /**
108  * @brief Render the scene in a buffer. You need to call this function before getting the result
109  *with getImage or getGlTexture.
110  **/
111  void renderScene(const bool centerScene = true, const bool antialiased = false);
112 
113  void renderExternalScene(GlScene *scene, const bool antialiased = false);
114 
115  /**
116  * @brief Generate a QImage from the scene. You need to call the renderScene function before this
117  *function.
118  **/
119  QImage getImage();
120  /**
121  * @brief Generate an open gl texture from the scene. You need to call the renderScene function
122  *before this function.
123  **/
124  GLuint getGLTexture(const bool generateMipMaps = false);
125 
126 private:
127  GlOffscreenRenderer();
128 
129  void initFrameBuffers(const bool antialiased);
130 
131  static GlOffscreenRenderer instance;
132 
133  unsigned int vPWidth, vPHeight;
134  QGLFramebufferObject *glFrameBuf, *glFrameBuf2;
135  GlScene scene;
136  GlLayer *mainLayer;
137  unsigned int entitiesCpt;
138  double zoomFactor;
139  Coord cameraCenter;
140  bool antialiasedFbo;
141 };
142 } // namespace tlp
143 
144 #endif /* GLOFFSCREENRENDERER_H_ */
145 ///@endcond