OpenWalnut  1.5.0dev
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 #include <osgText/Text>
38 
39 #include "WGEGroupNode.h"
40 
41 /**
42  * This class implements a HUD showing several textures on screen. This is especially useful as debugging tool during offscreen rendering. It is
43  * 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
44  * depends on the screen size.
45  */
46 class WGETextureHud: public osg::Projection
47 {
48 public:
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  * Constructor.
67  *
68  * \param texture the texture to show in the HUD
69  * \param name a telling name to support the illustrative function of the HUD
70  * \param transparency true if transparency should be shown
71  */
72  WGETextureHudEntry( osg::ref_ptr< osg::Texture2D > texture, std::string name, bool transparency = false );
73 
74  /**
75  * Destructor.
76  */
78 
79  /**
80  * Returns the real width of the contained texture.
81  *
82  * \return the real width.
83  */
84  unsigned int getRealWidth() const;
85 
86  /**
87  * Returns the real height of the contained texture.
88  *
89  * \return the real height.
90  */
91  unsigned int getRealHeight() const;
92 
93  /**
94  * Get the texture matrix state for this entry.
95  *
96  * \return the texture matrix state
97  */
98  osg::ref_ptr< osg::TexMat > getTextureMatrix() const;
99 
100  /**
101  * Returns the name of the entry.
102  *
103  * \return name of the entry.
104  */
105  std::string getName() const;
106 
107  /**
108  * Gets the texture associated with the entry.
109  *
110  * \return the texture
111  */
112  osg::ref_ptr< osg::Texture2D > getTexture() const;
113 
114  /**
115  * Set maximum text width. This is useful to avoid oversize text. Call only from inside a OSG callback.
116  *
117  * \param width the max width
118  */
119  void setMaxTextWidth( float width );
120  protected:
121  /**
122  * The texture.
123  */
124  osg::ref_ptr< osg::Texture2D > m_texture;
125 
126  /**
127  * The texture matrix for this entry.
128  */
129  osg::ref_ptr< osg::TexMat > m_texMat;
130 
131  /**
132  * The label text.
133  */
134  osgText::Text* m_label;
135 
136  /**
137  * The name for this HUD entry.
138  */
139  std::string m_name;
140 
141  /**
142  * Mqx text width.
143  */
145  private:
146  };
147 
148  /**
149  * Adds the specified HUD element to the HUD.
150  *
151  * \param texture the texture to show.
152  */
153  void addTexture( osg::ref_ptr< WGETextureHudEntry > texture );
154 
155  /**
156  * Remove the texture from the HUD.
157  *
158  * \param texture the texture to remove.
159  */
160  void removeTexture( osg::ref_ptr< WGETextureHudEntry > texture );
161 
162  /**
163  * Remove the texture from the HUD.
164  *
165  * \param texture the texture to remove.
166  */
167  void removeTexture( osg::ref_ptr< osg::Texture > texture );
168 
169  /**
170  * Gets the maximum width of a tex element.
171  *
172  * \return the maximum width.
173  */
174  unsigned int getMaxElementWidth() const;
175 
176  /**
177  * Sets the new maximum width of a texture column.
178  *
179  * \param width the new width
180  */
181  void setMaxElementWidth( unsigned int width );
182 
183  /**
184  * Sets the viewport of the camera housing this HUD. It is needed to have proper scaling of each texture tile. You can use
185  * \ref WGEViewportCallback to handle this automatically.
186  *
187  * \param viewport the viewport
188  */
189  void setViewport( osg::Viewport* viewport );
190 
191  /**
192  * 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
193  * coupling is disabled, the whole texture gets rendered.
194  *
195  * \param couple if true, the viewport set by \ref setViewport gets also used for texture space.
196  */
197  void coupleViewportWithTextureViewport( bool couple = true );
198 
199  /**
200  * Returns the render bin used by the HUD.
201  *
202  * \return the bin number
203  */
204  size_t getRenderBin() const;
205 
206 protected:
207  /**
208  * The group Node where all those texture reside in. Theoretically, it is nonsense to use a separate group inside a osg::Projection since it
209  * also is a group node. But WGEGroupNode offers all those nice and thread-safe insert/remove methods.
210  */
211  osg::ref_ptr< WGEGroupNode > m_group;
212 
213  /**
214  * The maximum element width.
215  */
216  unsigned int m_maxElementWidth;
217 
218  /**
219  * The render bin to use
220  */
221  size_t m_renderBin;
222 
223  /**
224  * The current viewport of
225  */
226  osg::Viewport* m_viewport;
227 
228  /**
229  * The viewport in texture space to allow viewing parts of the texture.
230  */
232 
233 private:
234  /**
235  * Callback which aligns and renders the textures.
236  */
237  class SafeUpdateCallback : public osg::NodeCallback
238  {
239  public: // NOLINT
240  /**
241  * Constructor.
242  *
243  * \param hud just set the creating HUD as pointer for later reference.
244  */
245  explicit SafeUpdateCallback( WGETextureHud* hud ): m_hud( hud )
246  {
247  };
248 
249  /**
250  * operator () - called during the update traversal.
251  *
252  * \param node the osg node
253  * \param nv the node visitor
254  */
255  virtual void operator()( osg::Node* node, osg::NodeVisitor* nv );
256 
257  /**
258  * Pointer used to access members of the hud. This is faster than casting the first parameter of operator() to WGETextureHud.
259  */
261  };
262 };
263 
264 #endif // WGETEXTUREHUD_H
265 
Callback which aligns and renders the textures.
SafeUpdateCallback(WGETextureHud *hud)
Constructor.
WGETextureHud * m_hud
Pointer used to access members of the hud.
virtual void operator()(osg::Node *node, osg::NodeVisitor *nv)
operator () - called during the update traversal.
Class implementing one texture HUD entry representing a texture in the HUD.
Definition: WGETextureHud.h:63
osg::ref_ptr< osg::Texture2D > getTexture() const
Gets the texture associated with the entry.
std::string m_name
The name for this HUD entry.
osgText::Text * m_label
The label text.
std::string getName() const
Returns the name of the entry.
unsigned int getRealHeight() const
Returns the real height of the contained texture.
osg::ref_ptr< osg::Texture2D > m_texture
The texture.
osg::ref_ptr< osg::TexMat > getTextureMatrix() const
Get the texture matrix state for this entry.
unsigned int getRealWidth() const
Returns the real width of the contained texture.
float m_maxTextWidth
Mqx text width.
WGETextureHudEntry(osg::ref_ptr< osg::Texture2D > texture, std::string name, bool transparency=false)
Constructor.
void setMaxTextWidth(float width)
Set maximum text width.
osg::ref_ptr< osg::TexMat > m_texMat
The texture matrix for this entry.
This class implements a HUD showing several textures on screen.
Definition: WGETextureHud.h:47
virtual ~WGETextureHud()
Destructor.
void removeTexture(osg::ref_ptr< WGETextureHudEntry > texture)
Remove the texture from the HUD.
osg::Viewport * m_viewport
The current viewport of.
void setViewport(osg::Viewport *viewport)
Sets the viewport of the camera housing this HUD.
void coupleViewportWithTextureViewport(bool couple=true)
Set the viewport to be used for textures too.
size_t getRenderBin() const
Returns the render bin used by the HUD.
unsigned int getMaxElementWidth() const
Gets the maximum width of a tex element.
void addTexture(osg::ref_ptr< WGETextureHudEntry > texture)
Adds the specified HUD element to the HUD.
unsigned int m_maxElementWidth
The maximum element width.
void setMaxElementWidth(unsigned int width)
Sets the new maximum width of a texture column.
bool m_coupleTexViewport
The viewport in texture space to allow viewing parts of the texture.
osg::ref_ptr< WGEGroupNode > m_group
The group Node where all those texture reside in.
size_t m_renderBin
The render bin to use.
WGETextureHud()
Default constructor.