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