Carna  Version 3.3.2
Texture.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2010 - 2015 Leonid Kostrykin
3  *
4  * Chair of Medical Engineering (mediTEC)
5  * RWTH Aachen University
6  * Pauwelsstr. 20
7  * 52074 Aachen
8  * Germany
9  *
10  */
11 
12 #ifndef TEXTURE_H_6014714286
13 #define TEXTURE_H_6014714286
14 
15 #include <Carna/Carna.h>
16 #include <Carna/base/noncopyable.h>
17 #include <Carna/base/math.h>
19 
24 namespace Carna
25 {
26 
27 namespace base
28 {
29 
30 
31 // ----------------------------------------------------------------------------------
32 // bindGLTextureObject
33 // ----------------------------------------------------------------------------------
34 
41 template< unsigned int dimension >
42 void bindGLTextureObject( unsigned int unit, unsigned int id )
43 {
44  static_assert( dimension >= 1 && dimension <= 3, "Texture dimension must be 1, 2 or 3." );
45 }
46 
47 
50 template< >
51 void CARNA_LIB bindGLTextureObject< 1 >( unsigned int unit, unsigned int id );
52 
53 
56 template< >
57 void CARNA_LIB bindGLTextureObject< 2 >( unsigned int unit, unsigned int id );
58 
59 
62 template< >
63 void CARNA_LIB bindGLTextureObject< 3 >( unsigned int unit, unsigned int id );
64 
65 
66 
67 // ----------------------------------------------------------------------------------
68 // TextureBase
69 // ----------------------------------------------------------------------------------
70 
77 class CARNA_LIB TextureBase
78 {
79 
81 
82 public:
83 
87  const unsigned int id;
88 
92  virtual ~TextureBase();
93 
98  const static unsigned int SETUP_UNIT = 0;
99 
100 protected:
101 
105  TextureBase();
106 
110  void uploadGLTextureData
111  ( const Eigen::Matrix< unsigned int, 1, 1 >& size
112  , int internalFormat
113  , int pixelFormat
114  , int bufferType
115  , const void* bufferPtr );
116 
120  void uploadGLTextureData
121  ( const math::Vector2ui& size
122  , int internalFormat
123  , int pixelFormat
124  , int bufferType
125  , const void* bufferPtr );
126 
130  void uploadGLTextureData
131  ( const math::Vector3ui& size
132  , int internalFormat
133  , int pixelFormat
134  , int bufferType
135  , const void* bufferPtr );
136 
137 }; // TextureBase
138 
139 
140 
141 // ----------------------------------------------------------------------------------
142 // Texture
143 // ----------------------------------------------------------------------------------
144 
151 template< unsigned int dimension >
152 class Texture : public TextureBase
153 {
154 
155 public:
156 
159  typedef Eigen::Matrix< unsigned int, dimension, 1 > Resolution;
160 
178  Texture( int internalFormat, int pixelFormat );
179 
183  const static unsigned int DIMENSION = dimension;
184 
189  const Resolution& size() const;
190 
194  bool isValid() const;
195 
196  const int internalFormat;
197  const int pixelFormat;
198 
203  void bind( unsigned int unit ) const;
204 
222  void update( const Resolution& size, int bufferType, const void* bufferPtr );
223 
226  void update( const Resolution& size );
227 
228 private:
229 
230  std::unique_ptr< Resolution > mySize;
231 
232 }; // Texture
233 
234 
235 template< unsigned int dimension >
236 Texture< dimension >::Texture( int internalFormat, int pixelFormat )
237  : internalFormat( internalFormat )
238  , pixelFormat( pixelFormat )
239 {
240  static_assert( dimension >= 1 && dimension <= 3, "Texture dimension must be 1, 2 or 3." );
241  CARNA_ASSERT_EX( id != 0, "Texture acquisition failed!" );
242 }
243 
244 
245 template< unsigned int dimension >
246 void Texture< dimension >::bind( unsigned int unit ) const
247 {
248  bindGLTextureObject< dimension >( unit, id );
249 }
250 
251 
252 template< unsigned int dimension >
254 {
255  return mySize.get() != nullptr;
256 }
257 
258 
259 template< unsigned int dimension >
260 const Eigen::Matrix< unsigned int, dimension, 1 >& Texture< dimension >::size() const
261 {
262  CARNA_ASSERT( isValid() );
263  return *mySize;
264 }
265 
266 
267 template< unsigned int dimension >
268 void Texture< dimension >::update( const Eigen::Matrix< unsigned int, dimension, 1 >& size, int bufferType, const void* bufferPtr )
269 {
270  /* Ensure that texture size is positive.
271  */
272  for( unsigned int i = 0; i < dimension; ++i )
273  {
274  CARNA_ASSERT_EX( size( i, 0 ) >= 1, "Texture only supports positive sizes!" );
275  }
276 
277  /* Ensure that z-component of the texture size is even if this is a 3D texture.
278  */
279  if( dimension == 3 )
280  {
281  CARNA_ASSERT_EX( size( 2, 0 ) % 2 == 0, "3D textures must have even depth!" );
282  }
283 
284  /* Update the OpenGL texture object.
285  */
286  this->bind( SETUP_UNIT );
287  uploadGLTextureData( size, internalFormat, pixelFormat, bufferType, bufferPtr );
288 }
289 
290 
291 template< unsigned int dimension >
292 void Texture< dimension >::update( const Eigen::Matrix< unsigned int, dimension, 1 >& size )
293 {
294  update( size, 0, nullptr );
295 }
296 
297 
298 
299 } // namespace Carna :: base
300 
301 } // namespace Carna
302 
303 #endif // TEXTURE_H_6014714286
Defines Carna::base::math namespace and CARNA_FOR_VECTOR3UI.
const int pixelFormat
Holds the format of the pixel data, e.g. GL_RED, GL_RGB or GL_RGBA.
Definition: Texture.h:197
void bindGLTextureObject< 1 >(unsigned int unit, unsigned int id)
#define CARNA_ASSERT_EX(expression, description)
If the given expression is false, a break point is raised in debug mode and an AssertionFailure throw...
void bindGLTextureObject< 2 >(unsigned int unit, unsigned int id)
Eigen::Matrix< unsigned int, 3, 1 > Vector3ui
Defines vector.
Definition: math.h:199
const int internalFormat
Holds the number of color components in the texture, e.g. GL_RGBA8UI or GL_INTENSITY16.
Definition: Texture.h:196
void bindGLTextureObject(unsigned int unit, unsigned int id)
Binds OpenGL texture object id to texture unit. For internal usage only, use Texture::bind instead...
Definition: Texture.h:42
const unsigned int id
Holds the ID of the represented OpenGL texture object.
Definition: Texture.h:87
Represents an OpenGL texture object. This class realizes the RAII-idiom.
Definition: Texture.h:152
Defines Carna::base::CarnaException, Carna::base::AssertionFailure.
Texture base class with dimension-independent definitions.
Definition: Texture.h:77
void bindGLTextureObject< 3 >(unsigned int unit, unsigned int id)
Texture(int internalFormat, int pixelFormat)
Creates OpenGL texture object.
Definition: Texture.h:236
Eigen::Matrix< unsigned int, 2, 1 > Vector2ui
Defines vector.
Definition: math.h:200
#define CARNA_ASSERT(expression)
If the given expression is false, a break point is raised in debug mode and an AssertionFailure throw...
Eigen::Matrix< unsigned int, dimension, 1 > Resolution
Definition: Texture.h:159
#define NON_COPYABLE
Features class it is placed in as non-copyable.
Definition: noncopyable.h:109