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