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