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