WGETextureUtils.cpp

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 #include "../common/exceptions/WPreconditionNotMet.h"
00026 
00027 #include "WGETexture.h"
00028 
00029 #include "WGETextureUtils.h"
00030 
00031 void wge::unbindTexture( osg::ref_ptr< osg::Node > node, size_t unit, size_t count )
00032 {
00033     for( size_t i = unit; i < unit + count; ++i )
00034     {
00035         node->getOrCreateStateSet()->removeTextureAttribute( i, osg::StateAttribute::TEXTURE );
00036         node->getOrCreateStateSet()->removeTextureAttribute( i, osg::StateAttribute::TEXMAT );
00037     }
00038 }
00039 
00040 size_t wge::getMaxTexUnits()
00041 {
00042     // GLint ret;
00043     // glGetIntegerv( GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &ret );
00044     // Why do we not use these glGet things here? The answer is simple: The GLSL 1.20 Standard does not define a way to access more than 8
00045     // texture coordinate attributes.
00046     return 8;
00047 }
00048 
00049 osg::ref_ptr< WGETexture< osg::Texture1D > > wge::genWhiteNoiseTexture( size_t sizeX, size_t channels )
00050 {
00051     // put it into an texture
00052     osg::ref_ptr< WGETexture1D > randTexture = new WGETexture1D( genWhiteNoiseImage( sizeX, 1, 1, channels ) );
00053     randTexture->setTextureWidth( sizeX );
00054     randTexture->setFilter( osg::Texture1D::MIN_FILTER, osg::Texture1D::NEAREST );
00055     randTexture->setFilter( osg::Texture1D::MAG_FILTER, osg::Texture1D::NEAREST );
00056     randTexture->setWrap( osg::Texture1D::WRAP_S, osg::Texture1D::REPEAT );
00057 
00058     return randTexture;
00059 }
00060 
00061 osg::ref_ptr< WGETexture< osg::Texture2D > > wge::genWhiteNoiseTexture( size_t sizeX, size_t sizeY, size_t channels )
00062 {
00063     osg::ref_ptr< WGETexture2D > randTexture = new WGETexture2D( genWhiteNoiseImage( sizeX, sizeY, 1, channels ) );
00064     randTexture->setTextureWidth( sizeX );
00065     randTexture->setTextureHeight( sizeY );
00066     randTexture->setFilter( osg::Texture2D::MIN_FILTER, osg::Texture2D::NEAREST );
00067     randTexture->setFilter( osg::Texture2D::MAG_FILTER, osg::Texture2D::NEAREST );
00068     randTexture->setWrap( osg::Texture2D::WRAP_S, osg::Texture2D::REPEAT );
00069     randTexture->setWrap( osg::Texture2D::WRAP_T, osg::Texture2D::REPEAT );
00070 
00071     return randTexture;
00072 }
00073 
00074 osg::ref_ptr< WGETexture< osg::Texture3D > > wge::genWhiteNoiseTexture( size_t sizeX, size_t sizeY, size_t sizeZ, size_t channels )
00075 {
00076     osg::ref_ptr< WGETexture3D > randTexture = new WGETexture3D( genWhiteNoiseImage( sizeX, sizeY, sizeZ, channels ) );
00077     randTexture->setTextureWidth( sizeX );
00078     randTexture->setTextureHeight( sizeY );
00079     randTexture->setTextureDepth( sizeZ );
00080     randTexture->setFilter( osg::Texture2D::MIN_FILTER, osg::Texture2D::NEAREST );
00081     randTexture->setFilter( osg::Texture2D::MAG_FILTER, osg::Texture2D::NEAREST );
00082     randTexture->setWrap( osg::Texture2D::WRAP_S, osg::Texture2D::REPEAT );
00083     randTexture->setWrap( osg::Texture2D::WRAP_T, osg::Texture2D::REPEAT );
00084     randTexture->setWrap( osg::Texture2D::WRAP_R, osg::Texture2D::REPEAT );
00085 
00086     return randTexture;
00087 }
00088 
00089 osg::ref_ptr< osg::Image > wge::genWhiteNoiseImage( size_t sizeX, size_t sizeY, size_t sizeZ, size_t channels )
00090 {
00091     WPrecond( ( channels == 1 ) || ( channels == 3 ) || ( channels == 4 ), "Invalid number of channels. Valid are: 1, 3 and 4."  );
00092 
00093     // create an osg::Image at first.
00094     std::srand( time( 0 ) );
00095     osg::ref_ptr< osg::Image > randImage = new osg::Image();
00096     GLenum type = GL_LUMINANCE;
00097     if( channels == 3 )
00098     {
00099         type = GL_RGB;
00100     }
00101     else if( channels == 4 )
00102     {
00103         type = GL_RGBA;
00104     }
00105     randImage->allocateImage( sizeX, sizeY, sizeZ, type, GL_UNSIGNED_BYTE );
00106     unsigned char *randomLuminance = randImage->data();  // should be 4 megs
00107     for( size_t i = 0; i < channels * sizeX * sizeY * sizeZ; ++i )
00108     {
00109         // - stylechecker says "use rand_r" but I am not sure about portability.
00110         unsigned char r = static_cast< unsigned char >( std::rand() % 255 );  // NOLINT - no we want std::rand instead of rand_r
00111         randomLuminance[ i ] = r;
00112     }
00113 
00114     return randImage;
00115 }
00116 
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends