WGETextureUtils.h

00001 //---------------------------------------------------------------------------
00002 //
00003 // Project: OpenWalnut ( http://www.openwalnut.org )
00004 //
00005 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
00006 // For more information see http://www.openwalnut.org/copying
00007 //
00008 // This file is part of OpenWalnut.
00009 //
00010 // OpenWalnut is free software: you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as published by
00012 // the Free Software Foundation, either version 3 of the License, or
00013 // (at your option) any later version.
00014 //
00015 // OpenWalnut is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 // GNU Lesser General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU Lesser General Public License
00021 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
00022 //
00023 //---------------------------------------------------------------------------
00024 
00025 #ifndef WGETEXTUREUTILS_H
00026 #define WGETEXTUREUTILS_H
00027 
00028 #include <string>
00029 
00030 #include <osg/Node>
00031 #include <osg/StateSet>
00032 #include <osg/TexMat>
00033 #include <osg/Texture1D>
00034 #include <osg/Texture2D>
00035 #include <osg/Texture3D>
00036 
00037 #include <boost/lexical_cast.hpp>
00038 
00039 #include "shaders/WGEPropertyUniform.h"
00040 #include "callbacks/WGEPropertyTransformationCallback.h"
00041 
00042 #include "WExportWGE.h"
00043 
00044 template < typename T > class WGETexture;
00045 class WDataTexture3D;
00046 
00047 namespace wge
00048 {
00049     /**
00050      * Binds the specified texture to the specified unit. It automatically adds several uniforms which then can be utilized in the shader:
00051      * - u_textureXUnit: the unit number (useful for accessing correct gl_TexCoord and so on)
00052      * - u_textureXSampler: the needed sampler
00053      * - u_textureXSizeX: width of the texture in pixels
00054      * - u_textureXSizeY: height of the texture in pixels
00055      * - u_textureXSizeZ: depth of the texture in pixels
00056      *
00057      * \param node where to bind
00058      * \param unit the unit to use
00059      * \param texture the texture to use.
00060      * \param prefix if specified, defines the uniform name prefix. (Sampler, Unit, Sizes, ...)
00061      * \tparam T the type of texture. Usually osg::Texture3D or osg::Texture2D.
00062      */
00063     template < typename T >
00064     void bindTexture( osg::ref_ptr< osg::Node > node, osg::ref_ptr< T > texture, size_t unit = 0, std::string prefix = "" );
00065 
00066     /**
00067      * Binds the specified texture to the specified unit. It automatically adds several uniforms which then can be utilized in the shader:
00068      * - u_textureXUnit: the unit number (useful for accessing correct gl_TexCoord and so on)
00069      * - u_textureXSampler: the needed sampler
00070      * - u_textureXSizeX: width of the texture in pixels
00071      * - u_textureXSizeY: height of the texture in pixels
00072      * - u_textureXSizeZ: depth of the texture in pixels
00073      * If the specified texture is a WGETexture, it additionally adds u_textureXMin and u_textureXScale for unscaling.
00074      *
00075      * \param node where to bind
00076      * \param unit the unit to use
00077      * \param texture the texture to use.
00078      * \param prefix if specified, defines the uniform name prefix. (Sampler, Unit, Sizes, ...)
00079      * \tparam T the type of texture. Usually osg::Texture3D or osg::Texture2D.
00080      */
00081     template < typename T >
00082     void bindTexture( osg::ref_ptr< osg::Node > node, osg::ref_ptr< WGETexture< T > > texture, size_t unit = 0, std::string prefix = ""  );
00083 
00084     /**
00085      * Removes the binding associated with the specified unit.
00086      *
00087      * \param unit the unit to unbind
00088      * \param node the node from which the binding should be removed
00089      * \param count the number of units beginning at the specified one should be unbound? 1 is the default.
00090      */
00091     void WGE_EXPORT unbindTexture( osg::ref_ptr< osg::Node > node, size_t unit, size_t count = 1 );
00092 
00093     /**
00094      * Returns the maximum number of textures that can be bound to a node. Call this only from withing the OSG thread!
00095      *
00096      * \return the max number of texture units.
00097      */
00098     size_t WGE_EXPORT getMaxTexUnits();
00099 
00100     /**
00101      * This generates an 1D texture only containing white noise in its channels.
00102      *
00103      * \param sizeX size in x direction (in pixels)
00104      * \param channels the number of channels. Valid are 1, 3 and 4.
00105      *
00106      * \return the generated texture.
00107      */
00108     osg::ref_ptr< WGETexture< osg::Texture1D > > WGE_EXPORT genWhiteNoiseTexture( size_t sizeX, size_t channels );
00109 
00110     /**
00111      * This generates an 2D texture only containing white noise in its channels.
00112      *
00113      * \param sizeX size in x direction (in pixels)
00114      * \param sizeY size in y direction (in pixels)
00115      * \param channels the number of channels. Valid are 1, 3 and 4.
00116      *
00117      * \return the generated texture.
00118      */
00119     osg::ref_ptr< WGETexture< osg::Texture2D > > WGE_EXPORT genWhiteNoiseTexture( size_t sizeX, size_t sizeY, size_t channels );
00120 
00121     /**
00122      * This generates an 3D texture only containing white noise in its channels.
00123      *
00124      * \param sizeX size in x direction (in pixels)
00125      * \param sizeY size in y direction (in pixels)
00126      * \param sizeZ size in z direction (in pixels)
00127      * \param channels the number of channels. Valid are 1, 3 and 4.
00128      *
00129      * \return the generated texture.
00130      */
00131     osg::ref_ptr< WGETexture< osg::Texture3D > > WGE_EXPORT genWhiteNoiseTexture( size_t sizeX, size_t sizeY, size_t sizeZ, size_t channels );
00132 
00133     /**
00134      * Generates an image only containing white noise in its channels.
00135      *
00136      * \param sizeX size in x direction (in pixels)
00137      * \param sizeY size in y direction (in pixels)
00138      * \param sizeZ size in z direction (in pixels)
00139      * \param channels the number of channels. Valid are 1, 3 and 4.
00140      *
00141      * \return the generated image.
00142      */
00143     osg::ref_ptr< osg::Image > WGE_EXPORT genWhiteNoiseImage( size_t sizeX, size_t sizeY, size_t sizeZ, size_t channels = 1 );
00144 }
00145 
00146 template < typename T >
00147 void wge::bindTexture( osg::ref_ptr< osg::Node > node, osg::ref_ptr< T > texture, size_t unit, std::string prefix )
00148 {
00149     if( prefix == "" )
00150     {
00151         prefix = "u_texture" + boost::lexical_cast< std::string >( unit );
00152     }
00153 
00154     osg::StateSet* state = node->getOrCreateStateSet();
00155     state->setTextureAttributeAndModes( unit, texture, osg::StateAttribute::ON );
00156     state->addUniform( new osg::Uniform( ( prefix + "Sampler" ).c_str(), static_cast< int >( unit ) ) );
00157     state->addUniform( new osg::Uniform( ( prefix + "Unit" ).c_str(), static_cast< int >( unit ) ) );
00158     state->addUniform( new osg::Uniform( ( prefix + "SizeX" ).c_str(), static_cast< int >( texture->getTextureWidth() ) ) );
00159     state->addUniform( new osg::Uniform( ( prefix + "SizeY" ).c_str(), static_cast< int >( texture->getTextureHeight() ) ) );
00160     state->addUniform( new osg::Uniform( ( prefix + "SizeZ" ).c_str(), static_cast< int >( texture->getTextureDepth() ) ) );
00161 }
00162 
00163 template < typename T >
00164 void wge::bindTexture( osg::ref_ptr< osg::Node > node, osg::ref_ptr< WGETexture< T > > texture, size_t unit, std::string prefix )
00165 {
00166     if( prefix == "" )
00167     {
00168         prefix = "u_texture" + boost::lexical_cast< std::string >( unit );
00169     }
00170 
00171     wge::bindTexture< T >( node, osg::ref_ptr< T >( texture ), unit, prefix );
00172 
00173     // set the texture matrix to the stateset
00174     osg::TexMat* texMat = new osg::TexMat( texture->transformation()->get() );
00175     // use a callback to update the tex matrix if needed according to transformation property of texture
00176     texMat->setUpdateCallback( new WGEPropertyTransformationCallback< osg::StateAttribute, osg::TexMat >( texture->transformation() ) );
00177     node->getOrCreateStateSet()->setTextureAttributeAndModes( unit, texMat, osg::StateAttribute::ON );
00178 
00179     // add some additional uniforms containing scaling information
00180     texture->applyUniforms( prefix, node->getOrCreateStateSet() );
00181 }
00182 
00183 #endif  // WGETEXTUREUTILS_H
00184 
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends