OpenWalnut  1.5.0dev
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 "../common/WStringUtils.h"
38 
39 #include "shaders/WGEPropertyUniform.h"
40 #include "callbacks/WGEPropertyTransformationCallback.h"
41 
42 template < typename T > class WGETexture;
43 class WDataTexture3D;
44 
45 namespace wge
46 {
47  /**
48  * Binds the specified texture to the specified unit. It automatically adds several uniforms which then can be utilized in the shader:
49  * - u_textureXUnit: the unit number (useful for accessing correct gl_TexCoord and so on)
50  * - u_textureXSampler: the needed sampler
51  * - u_textureXSizeX: width of the texture in pixels
52  * - u_textureXSizeY: height of the texture in pixels
53  * - u_textureXSizeZ: depth of the texture in pixels
54  *
55  * \warning this is not OSG-thread-safe. This method binds the texture immediately. So please use this only for nodes which are not yet used
56  * in the scene graph. For safely binding textures to a node already in the scene, utilize an update callback.
57  *
58  * \param node where to bind
59  * \param unit the unit to use
60  * \param texture the texture to use.
61  * \param prefix if specified, defines the uniform name prefix. (Sampler, Unit, Sizes, ...)
62  * \tparam T the type of texture. Usually osg::Texture3D or osg::Texture2D.
63  */
64  template < typename T >
65  void bindTexture( osg::ref_ptr< osg::Node > node, osg::ref_ptr< T > texture, size_t unit = 0, std::string prefix = "" );
66 
67  /**
68  * Binds the specified texture to the specified unit. It automatically adds several uniforms which then can be utilized in the shader:
69  * - u_textureXUnit: the unit number (useful for accessing correct gl_TexCoord and so on)
70  * - u_textureXSampler: the needed sampler
71  * - u_textureXSizeX: width of the texture in pixels
72  * - u_textureXSizeY: height of the texture in pixels
73  * - u_textureXSizeZ: depth of the texture in pixels
74  * If the specified texture is a WGETexture, it additionally adds u_textureXMin and u_textureXScale for unscaling.
75  *
76  * \warning this is not OSG-thread-safe. This method binds the texture immediately. So please use this only for nodes which are not yet used
77  * in the scene graph. For safely binding textures to a node already in the scene, utilize an update callback.
78  *
79  * \param node where to bind
80  * \param unit the unit to use
81  * \param texture the texture to use.
82  * \param prefix if specified, defines the uniform name prefix. (Sampler, Unit, Sizes, ...)
83  * \tparam T the type of texture. Usually osg::Texture3D or osg::Texture2D.
84  */
85  template < typename T >
86  void bindTexture( osg::ref_ptr< osg::Node > node, osg::ref_ptr< WGETexture< T > > texture, size_t unit = 0, std::string prefix = "" );
87 
88  /**
89  * Removes the binding associated with the specified unit.
90  *
91  * \param unit the unit to unbind
92  * \param node the node from which the binding should be removed
93  * \param count the number of units beginning at the specified one should be unbound? 1 is the default.
94  */
95  void unbindTexture( osg::ref_ptr< osg::Node > node, size_t unit, size_t count = 1 );
96 
97  /**
98  * Returns the maximum number of textures that can be bound to a node. Call this only from withing the OSG thread!
99  *
100  * \return the max number of texture units.
101  */
102  size_t getMaxTexUnits();
103 
104  /**
105  * This generates an 1D texture only containing white noise in its channels.
106  *
107  * \param sizeX size in x direction (in pixels)
108  * \param channels the number of channels. Valid are 1, 3 and 4.
109  *
110  * \return the generated texture.
111  */
112  osg::ref_ptr< WGETexture< osg::Texture1D > > genWhiteNoiseTexture( size_t sizeX, size_t channels );
113 
114  /**
115  * This generates an 2D texture only containing white noise in its channels.
116  *
117  * \param sizeX size in x direction (in pixels)
118  * \param sizeY size in y direction (in pixels)
119  * \param channels the number of channels. Valid are 1, 3 and 4.
120  *
121  * \return the generated texture.
122  */
123  osg::ref_ptr< WGETexture< osg::Texture2D > > genWhiteNoiseTexture( size_t sizeX, size_t sizeY, size_t channels );
124 
125  /**
126  * This generates an 3D texture only containing white noise in its channels.
127  *
128  * \param sizeX size in x direction (in pixels)
129  * \param sizeY size in y direction (in pixels)
130  * \param sizeZ size in z direction (in pixels)
131  * \param channels the number of channels. Valid are 1, 3 and 4.
132  *
133  * \return the generated texture.
134  */
135  osg::ref_ptr< WGETexture< osg::Texture3D > > genWhiteNoiseTexture( size_t sizeX, size_t sizeY, size_t sizeZ, size_t channels );
136 
137  /**
138  * Generates an image only containing white noise in its channels.
139  *
140  * \param sizeX size in x direction (in pixels)
141  * \param sizeY size in y direction (in pixels)
142  * \param sizeZ size in z direction (in pixels)
143  * \param channels the number of channels. Valid are 1, 3 and 4.
144  *
145  * \return the generated image.
146  */
147  osg::ref_ptr< osg::Image > genWhiteNoiseImage( size_t sizeX, size_t sizeY, size_t sizeZ, size_t channels = 1 );
148 
149  /**
150  * Generates a 3D turing noise texture. For details, see:
151  *
152  * Eichelbaum, Sebastian, et al. "Fabric-like visualization of tensor field data on arbitrary surfaces
153  * in image space." New Developments in the Visualization and Processing of Tensor Fields. Springer Berlin Heidelberg, 2012. 71-92.
154  *
155  * \param sizeX The size of the textures in voxels, should be a power of 2.
156  * \param sizeY The size of the textures in voxels, should be a power of 2.
157  * \param sizeZ The size of the textures in voxels, should be a power of 2.
158  * \param channels The number of channels; either 1, 3 or 4.
159  *
160  * \return The texture.
161  */
162  osg::ref_ptr< WGETexture< osg::Texture3D > > genTuringNoiseTexture( std::size_t sizeX, std::size_t sizeY,
163  std::size_t sizeZ, std::size_t channels = 1 );
164 }
165 
166 template < typename T >
167 void wge::bindTexture( osg::ref_ptr< osg::Node > node, osg::ref_ptr< T > texture, size_t unit, std::string prefix )
168 {
169  if( prefix == "" )
170  {
171  prefix = "u_texture" + string_utils::toString( unit );
172  }
173 
174  osg::StateSet* state = node->getOrCreateStateSet();
175  state->setTextureAttributeAndModes( unit, texture, osg::StateAttribute::ON );
176  state->addUniform( new osg::Uniform( ( prefix + "Sampler" ).c_str(), static_cast< int >( unit ) ) );
177  state->addUniform( new osg::Uniform( ( prefix + "Unit" ).c_str(), static_cast< int >( unit ) ) );
178  state->addUniform( new osg::Uniform( ( prefix + "SizeX" ).c_str(), static_cast< int >( texture->getTextureWidth() ) ) );
179  state->addUniform( new osg::Uniform( ( prefix + "SizeY" ).c_str(), static_cast< int >( texture->getTextureHeight() ) ) );
180  state->addUniform( new osg::Uniform( ( prefix + "SizeZ" ).c_str(), static_cast< int >( texture->getTextureDepth() ) ) );
181 }
182 
183 template < typename T >
184 void wge::bindTexture( osg::ref_ptr< osg::Node > node, osg::ref_ptr< WGETexture< T > > texture, size_t unit, std::string prefix )
185 {
186  if( prefix == "" )
187  {
188  prefix = "u_texture" + string_utils::toString( unit );
189  }
190 
191  wge::bindTexture< T >( node, osg::ref_ptr< T >( texture ), unit, prefix );
192 
193  // set the texture matrix to the stateset
194  osg::TexMat* texMat = new osg::TexMat( texture->transformation()->get() );
195  // use a callback to update the tex matrix if needed according to transformation property of texture
196  texMat->setUpdateCallback( new WGEPropertyTransformationCallback< osg::StateAttribute, osg::TexMat >( texture->transformation() ) );
197  node->getOrCreateStateSet()->setTextureAttributeAndModes( unit, texMat, osg::StateAttribute::ON );
198 
199  // add some additional uniforms containing scaling information
200  texture->applyUniforms( prefix, node->getOrCreateStateSet() );
201 }
202 
203 #endif // WGETEXTUREUTILS_H
204 
This class allows simple creation of WGETexture3D by using a specified grid and value-set.
This calls serves a simple purpose: have a texture and its scaling information together which allows ...
Definition: WGETexture.h:53
std::string toString(const T &value)
Convert a given value to a string.
Definition: WStringUtils.h:120
Extend the wge utils namespace with additional methods relating WDataTexture3D.
osg::ref_ptr< WGETexture< osg::Texture3D > > genTuringNoiseTexture(std::size_t sizeX, std::size_t sizeY, std::size_t sizeZ, std::size_t channels=1)
Generates a 3D turing noise texture.
osg::ref_ptr< WGETexture< osg::Texture1D > > genWhiteNoiseTexture(size_t sizeX, size_t channels)
This generates an 1D texture only containing white noise in its channels.
void bindTexture(osg::ref_ptr< osg::Node > node, osg::ref_ptr< WDataTexture3D > texture, size_t unit=0, std::string prefix="")
Binds the specified texture to the specified unit.
osg::ref_ptr< osg::Image > genWhiteNoiseImage(size_t sizeX, size_t sizeY, size_t sizeZ, size_t channels=1)
Generates an image only containing white noise in its channels.
size_t getMaxTexUnits()
Returns the maximum number of textures that can be bound to a node.
void unbindTexture(osg::ref_ptr< osg::Node > node, size_t unit, size_t count=1)
Removes the binding associated with the specified unit.