OpenWalnut  1.3.1
WGETextureUtils.cpp
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 #include "../common/exceptions/WPreconditionNotMet.h"
26 
27 #include "WGETexture.h"
28 
29 #include "WGETextureUtils.h"
30 
31 void wge::unbindTexture( osg::ref_ptr< osg::Node > node, size_t unit, size_t count )
32 {
33  for( size_t i = unit; i < unit + count; ++i )
34  {
35  node->getOrCreateStateSet()->removeTextureAttribute( i, osg::StateAttribute::TEXTURE );
36  node->getOrCreateStateSet()->removeTextureAttribute( i, osg::StateAttribute::TEXMAT );
37  }
38 }
39 
41 {
42  // GLint ret;
43  // glGetIntegerv( GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &ret );
44  // 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
45  // texture coordinate attributes.
46  return 8;
47 }
48 
49 osg::ref_ptr< WGETexture< osg::Texture1D > > wge::genWhiteNoiseTexture( size_t sizeX, size_t channels )
50 {
51  // put it into an texture
52  osg::ref_ptr< WGETexture1D > randTexture = new WGETexture1D( genWhiteNoiseImage( sizeX, 1, 1, channels ) );
53  randTexture->setTextureWidth( sizeX );
54  randTexture->setFilter( osg::Texture1D::MIN_FILTER, osg::Texture1D::NEAREST );
55  randTexture->setFilter( osg::Texture1D::MAG_FILTER, osg::Texture1D::NEAREST );
56  randTexture->setWrap( osg::Texture1D::WRAP_S, osg::Texture1D::REPEAT );
57 
58  return randTexture;
59 }
60 
61 osg::ref_ptr< WGETexture< osg::Texture2D > > wge::genWhiteNoiseTexture( size_t sizeX, size_t sizeY, size_t channels )
62 {
63  osg::ref_ptr< WGETexture2D > randTexture = new WGETexture2D( genWhiteNoiseImage( sizeX, sizeY, 1, channels ) );
64  randTexture->setTextureWidth( sizeX );
65  randTexture->setTextureHeight( sizeY );
66  randTexture->setFilter( osg::Texture2D::MIN_FILTER, osg::Texture2D::NEAREST );
67  randTexture->setFilter( osg::Texture2D::MAG_FILTER, osg::Texture2D::NEAREST );
68  randTexture->setWrap( osg::Texture2D::WRAP_S, osg::Texture2D::REPEAT );
69  randTexture->setWrap( osg::Texture2D::WRAP_T, osg::Texture2D::REPEAT );
70 
71  return randTexture;
72 }
73 
74 osg::ref_ptr< WGETexture< osg::Texture3D > > wge::genWhiteNoiseTexture( size_t sizeX, size_t sizeY, size_t sizeZ, size_t channels )
75 {
76  osg::ref_ptr< WGETexture3D > randTexture = new WGETexture3D( genWhiteNoiseImage( sizeX, sizeY, sizeZ, channels ) );
77  randTexture->setTextureWidth( sizeX );
78  randTexture->setTextureHeight( sizeY );
79  randTexture->setTextureDepth( sizeZ );
80  randTexture->setFilter( osg::Texture2D::MIN_FILTER, osg::Texture2D::NEAREST );
81  randTexture->setFilter( osg::Texture2D::MAG_FILTER, osg::Texture2D::NEAREST );
82  randTexture->setWrap( osg::Texture2D::WRAP_S, osg::Texture2D::REPEAT );
83  randTexture->setWrap( osg::Texture2D::WRAP_T, osg::Texture2D::REPEAT );
84  randTexture->setWrap( osg::Texture2D::WRAP_R, osg::Texture2D::REPEAT );
85 
86  return randTexture;
87 }
88 
89 osg::ref_ptr< osg::Image > wge::genWhiteNoiseImage( size_t sizeX, size_t sizeY, size_t sizeZ, size_t channels )
90 {
91  WPrecond( ( channels == 1 ) || ( channels == 3 ) || ( channels == 4 ), "Invalid number of channels. Valid are: 1, 3 and 4." );
92 
93  // create an osg::Image at first.
94  std::srand( time( 0 ) );
95  osg::ref_ptr< osg::Image > randImage = new osg::Image();
96  GLenum type = GL_LUMINANCE;
97  if( channels == 3 )
98  {
99  type = GL_RGB;
100  }
101  else if( channels == 4 )
102  {
103  type = GL_RGBA;
104  }
105  randImage->allocateImage( sizeX, sizeY, sizeZ, type, GL_UNSIGNED_BYTE );
106  unsigned char *randomLuminance = randImage->data(); // should be 4 megs
107  for( size_t i = 0; i < channels * sizeX * sizeY * sizeZ; ++i )
108  {
109  // - stylechecker says "use rand_r" but I am not sure about portability.
110  unsigned char r = static_cast< unsigned char >( std::rand() % 255 ); // NOLINT - no we want std::rand instead of rand_r
111  randomLuminance[ i ] = r;
112  }
113 
114  return randImage;
115 }
116