OpenWalnut  1.2.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
WGETextureHud.h
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #ifndef WGETEXTUREHUD_H
26 #define WGETEXTUREHUD_H
27 
28 #include <list>
29 #include <string>
30 
31 #include <boost/thread.hpp>
32 
33 #include <osg/Projection>
34 #include <osg/Geode>
35 #include <osg/Texture2D>
36 #include <osg/TexMat>
37 
38 #include "WGEGroupNode.h"
39 
40 /**
41  * This class implements a HUD showing several textures on screen. This is especially useful as debugging tool during offscreen rendering. It is
42  * possible to add and remove textures to it. The size of the texture on screen depends on the screen size, as well as the layout of each texture
43  * depends on the screen size.
44  */
45 class WGETextureHud: public osg::Projection
46 {
47 public:
48 
49  /**
50  * Default constructor.
51  */
52  WGETextureHud();
53 
54  /**
55  * Destructor.
56  */
57  virtual ~WGETextureHud();
58 
59  /**
60  * Class implementing one texture HUD entry representing a texture in the HUD.
61  */
62  class WGETextureHudEntry: public osg::MatrixTransform
63  {
64  public: // NOLINT
65 
66  /**
67  * Constructor.
68  *
69  * \param texture the texture to show in the HUD
70  * \param name a telling name to support the illustrative function of the HUD
71  * \param transparency true if transparency should be shown
72  */
73  WGETextureHudEntry( osg::ref_ptr< osg::Texture2D > texture, std::string name, bool transparency = false );
74 
75  /**
76  * Destructor.
77  */
79 
80  /**
81  * Returns the real width of the contained texture.
82  *
83  * \return the real width.
84  */
85  unsigned int getRealWidth() const;
86 
87  /**
88  * Returns the real height of the contained texture.
89  *
90  * \return the real height.
91  */
92  unsigned int getRealHeight() const;
93 
94  /**
95  * Get the texture matrix state for this entry.
96  *
97  * \return the texture matrix state
98  */
99  osg::ref_ptr< osg::TexMat > getTextureMatrix() const;
100 
101  /**
102  * Returns the name of the entry.
103  *
104  * \return name of the entry.
105  */
106  std::string getName() const;
107 
108  /**
109  * Gets the texture associated with the entry.
110  *
111  * \return the texture
112  */
113  osg::ref_ptr< osg::Texture2D > getTexture() const;
114 
115  protected:
116 
117  /**
118  * The texture.
119  */
120  osg::ref_ptr< osg::Texture2D > m_texture;
121 
122  /**
123  * The texture matrix for this entry.
124  */
125  osg::ref_ptr< osg::TexMat > m_texMat;
126 
127  /**
128  * The name for this HUD entry.
129  */
130  std::string m_name;
131 
132  private:
133  };
134 
135  /**
136  * Adds the specified HUD element to the HUD.
137  *
138  * \param texture the texture to show.
139  */
140  void addTexture( osg::ref_ptr< WGETextureHudEntry > texture );
141 
142  /**
143  * Remove the texture from the HUD.
144  *
145  * \param texture the texture to remove.
146  */
147  void removeTexture( osg::ref_ptr< WGETextureHudEntry > texture );
148 
149  /**
150  * Remove the texture from the HUD.
151  *
152  * \param texture the texture to remove.
153  */
154  void removeTexture( osg::ref_ptr< osg::Texture > texture );
155 
156  /**
157  * Gets the maximum width of a tex element.
158  *
159  * \return the maximum width.
160  */
161  unsigned int getMaxElementWidth() const;
162 
163  /**
164  * Sets the new maximum width of a texture column.
165  *
166  * \param width the new width
167  */
168  void setMaxElementWidth( unsigned int width );
169 
170  /**
171  * Sets the viewport of the camera housing this HUD. It is needed to have proper scaling of each texture tile. You can use
172  * \ref WGEViewportCallback to handle this automatically.
173  *
174  * \param viewport the viewport
175  */
176  void setViewport( osg::Viewport* viewport );
177 
178  /**
179  * Set the viewport to be used for textures too. This is useful if an offscreen rendering renders only into a part of the texture. If
180  * coupling is disabled, the whole texture gets rendered.
181  *
182  * \param couple if true, the viewport set by \ref setViewport gets also used for texture space.
183  */
184  void coupleViewportWithTextureViewport( bool couple = true );
185 
186  /**
187  * Returns the render bin used by the HUD.
188  *
189  * \return the bin number
190  */
191  size_t getRenderBin() const;
192 
193 protected:
194 
195  /**
196  * The group Node where all those texture reside in. Theoretically, it is nonsense to use a separate group inside a osg::Projection since it
197  * also is a group node. But WGEGroupNode offers all those nice and thread-safe insert/remove methods.
198  */
199  osg::ref_ptr< WGEGroupNode > m_group;
200 
201  /**
202  * The maximum element width.
203  */
204  unsigned int m_maxElementWidth;
205 
206  /**
207  * The render bin to use
208  */
209  size_t m_renderBin;
210 
211  /**
212  * The current viewport of
213  */
214  osg::Viewport* m_viewport;
215 
216  /**
217  * The viewport in texture space to allow viewing parts of the texture.
218  */
220 
221 private:
222 
223  /**
224  * Callback which aligns and renders the textures.
225  */
226  class SafeUpdateCallback : public osg::NodeCallback
227  {
228  public: // NOLINT
229 
230  /**
231  * Constructor.
232  *
233  * \param hud just set the creating HUD as pointer for later reference.
234  */
235  explicit SafeUpdateCallback( WGETextureHud* hud ): m_hud( hud )
236  {
237  };
238 
239  /**
240  * operator () - called during the update traversal.
241  *
242  * \param node the osg node
243  * \param nv the node visitor
244  */
245  virtual void operator()( osg::Node* node, osg::NodeVisitor* nv );
246 
247  /**
248  * Pointer used to access members of the hud. This is faster than casting the first parameter of operator() to WGETextureHud.
249  */
251  };
252 };
253 
254 #endif // WGETEXTUREHUD_H
255