OpenWalnut  1.2.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
WGETextureUtils.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 WGETEXTUREUTILS_H
26 #define WGETEXTUREUTILS_H
27 
28 #include <string>
29 
30 #include <osg/Node>
31 #include <osg/StateSet>
32 #include <osg/TexMat>
33 #include <osg/Texture1D>
34 #include <osg/Texture2D>
35 #include <osg/Texture3D>
36 
37 #include <boost/lexical_cast.hpp>
38 
39 #include "shaders/WGEPropertyUniform.h"
40 #include "callbacks/WGEPropertyTransformationCallback.h"
41 
42 #include "WExportWGE.h"
43 
44 template < typename T > class WGETexture;
45 class WDataTexture3D;
46 
47 namespace wge
48 {
49  /**
50  * Binds the specified texture to the specified unit. It automatically adds several uniforms which then can be utilized in the shader:
51  * - u_textureXUnit: the unit number (useful for accessing correct gl_TexCoord and so on)
52  * - u_textureXSampler: the needed sampler
53  * - u_textureXSizeX: width of the texture in pixels
54  * - u_textureXSizeY: height of the texture in pixels
55  * - u_textureXSizeZ: depth of the texture in pixels
56  *
57  * \param node where to bind
58  * \param unit the unit to use
59  * \param texture the texture to use.
60  * \param prefix if specified, defines the uniform name prefix. (Sampler, Unit, Sizes, ...)
61  * \tparam T the type of texture. Usually osg::Texture3D or osg::Texture2D.
62  */
63  template < typename T >
64  void bindTexture( osg::ref_ptr< osg::Node > node, osg::ref_ptr< T > texture, size_t unit = 0, std::string prefix = "" );
65 
66  /**
67  * Binds the specified texture to the specified unit. It automatically adds several uniforms which then can be utilized in the shader:
68  * - u_textureXUnit: the unit number (useful for accessing correct gl_TexCoord and so on)
69  * - u_textureXSampler: the needed sampler
70  * - u_textureXSizeX: width of the texture in pixels
71  * - u_textureXSizeY: height of the texture in pixels
72  * - u_textureXSizeZ: depth of the texture in pixels
73  * If the specified texture is a WGETexture, it additionally adds u_textureXMin and u_textureXScale for unscaling.
74  *
75  * \param node where to bind
76  * \param unit the unit to use
77  * \param texture the texture to use.
78  * \param prefix if specified, defines the uniform name prefix. (Sampler, Unit, Sizes, ...)
79  * \tparam T the type of texture. Usually osg::Texture3D or osg::Texture2D.
80  */
81  template < typename T >
82  void bindTexture( osg::ref_ptr< osg::Node > node, osg::ref_ptr< WGETexture< T > > texture, size_t unit = 0, std::string prefix = "" );
83 
84  /**
85  * Removes the binding associated with the specified unit.
86  *
87  * \param unit the unit to unbind
88  * \param node the node from which the binding should be removed
89  * \param count the number of units beginning at the specified one should be unbound? 1 is the default.
90  */
91  void WGE_EXPORT unbindTexture( osg::ref_ptr< osg::Node > node, size_t unit, size_t count = 1 );
92 
93  /**
94  * Returns the maximum number of textures that can be bound to a node. Call this only from withing the OSG thread!
95  *
96  * \return the max number of texture units.
97  */
98  size_t WGE_EXPORT getMaxTexUnits();
99 
100  /**
101  * This generates an 1D texture only containing white noise in its channels.
102  *
103  * \param sizeX size in x direction (in pixels)
104  * \param channels the number of channels. Valid are 1, 3 and 4.
105  *
106  * \return the generated texture.
107  */
108  osg::ref_ptr< WGETexture< osg::Texture1D > > WGE_EXPORT genWhiteNoiseTexture( size_t sizeX, size_t channels );
109 
110  /**
111  * This generates an 2D texture only containing white noise in its channels.
112  *
113  * \param sizeX size in x direction (in pixels)
114  * \param sizeY size in y direction (in pixels)
115  * \param channels the number of channels. Valid are 1, 3 and 4.
116  *
117  * \return the generated texture.
118  */
119  osg::ref_ptr< WGETexture< osg::Texture2D > > WGE_EXPORT genWhiteNoiseTexture( size_t sizeX, size_t sizeY, size_t channels );
120 
121  /**
122  * This generates an 3D texture only containing white noise in its channels.
123  *
124  * \param sizeX size in x direction (in pixels)
125  * \param sizeY size in y direction (in pixels)
126  * \param sizeZ size in z direction (in pixels)
127  * \param channels the number of channels. Valid are 1, 3 and 4.
128  *
129  * \return the generated texture.
130  */
131  osg::ref_ptr< WGETexture< osg::Texture3D > > WGE_EXPORT genWhiteNoiseTexture( size_t sizeX, size_t sizeY, size_t sizeZ, size_t channels );
132 
133  /**
134  * Generates an image only containing white noise in its channels.
135  *
136  * \param sizeX size in x direction (in pixels)
137  * \param sizeY size in y direction (in pixels)
138  * \param sizeZ size in z direction (in pixels)
139  * \param channels the number of channels. Valid are 1, 3 and 4.
140  *
141  * \return the generated image.
142  */
143  osg::ref_ptr< osg::Image > WGE_EXPORT genWhiteNoiseImage( size_t sizeX, size_t sizeY, size_t sizeZ, size_t channels = 1 );
144 }
145 
146 template < typename T >
147 void wge::bindTexture( osg::ref_ptr< osg::Node > node, osg::ref_ptr< T > texture, size_t unit, std::string prefix )
148 {
149  if( prefix == "" )
150  {
151  prefix = "u_texture" + boost::lexical_cast< std::string >( unit );
152  }
153 
154  osg::StateSet* state = node->getOrCreateStateSet();
155  state->setTextureAttributeAndModes( unit, texture, osg::StateAttribute::ON );
156  state->addUniform( new osg::Uniform( ( prefix + "Sampler" ).c_str(), static_cast< int >( unit ) ) );
157  state->addUniform( new osg::Uniform( ( prefix + "Unit" ).c_str(), static_cast< int >( unit ) ) );
158  state->addUniform( new osg::Uniform( ( prefix + "SizeX" ).c_str(), static_cast< int >( texture->getTextureWidth() ) ) );
159  state->addUniform( new osg::Uniform( ( prefix + "SizeY" ).c_str(), static_cast< int >( texture->getTextureHeight() ) ) );
160  state->addUniform( new osg::Uniform( ( prefix + "SizeZ" ).c_str(), static_cast< int >( texture->getTextureDepth() ) ) );
161 }
162 
163 template < typename T >
164 void wge::bindTexture( osg::ref_ptr< osg::Node > node, osg::ref_ptr< WGETexture< T > > texture, size_t unit, std::string prefix )
165 {
166  if( prefix == "" )
167  {
168  prefix = "u_texture" + boost::lexical_cast< std::string >( unit );
169  }
170 
171  wge::bindTexture< T >( node, osg::ref_ptr< T >( texture ), unit, prefix );
172 
173  // set the texture matrix to the stateset
174  osg::TexMat* texMat = new osg::TexMat( texture->transformation()->get() );
175  // use a callback to update the tex matrix if needed according to transformation property of texture
176  texMat->setUpdateCallback( new WGEPropertyTransformationCallback< osg::StateAttribute, osg::TexMat >( texture->transformation() ) );
177  node->getOrCreateStateSet()->setTextureAttributeAndModes( unit, texMat, osg::StateAttribute::ON );
178 
179  // add some additional uniforms containing scaling information
180  texture->applyUniforms( prefix, node->getOrCreateStateSet() );
181 }
182 
183 #endif // WGETEXTUREUTILS_H
184