Carna Version 3.3.3
Loading...
Searching...
No Matches
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>
17#include <Carna/base/math.h>
19
24namespace Carna
25{
26
27namespace base
28{
29
30
31// ----------------------------------------------------------------------------------
32// bindGLTextureObject
33// ----------------------------------------------------------------------------------
34
41template< unsigned int dimension >
42void 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
50template< >
51void CARNA_LIB bindGLTextureObject< 1 >( unsigned int unit, unsigned int id );
52
53
56template< >
57void CARNA_LIB bindGLTextureObject< 2 >( unsigned int unit, unsigned int id );
58
59
62template< >
63void CARNA_LIB bindGLTextureObject< 3 >( unsigned int unit, unsigned int id );
64
65
66
67// ----------------------------------------------------------------------------------
68// TextureBase
69// ----------------------------------------------------------------------------------
70
77class CARNA_LIB TextureBase
78{
79
81
82public:
83
87 const unsigned int id;
88
92 virtual ~TextureBase();
93
98 const static unsigned int SETUP_UNIT = 0;
99
100protected:
101
106
111 ( const Eigen::Matrix< unsigned int, 1, 1 >& size
112 , int internalFormat
113 , int pixelFormat
114 , int bufferType
115 , const void* bufferPtr );
116
121 ( const math::Vector2ui& size
122 , int internalFormat
123 , int pixelFormat
124 , int bufferType
125 , const void* bufferPtr );
126
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
151template< unsigned int dimension >
152class Texture : public TextureBase
153{
154
155public:
156
159 typedef Eigen::Matrix< unsigned int, dimension, 1 > Resolution;
160
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
228private:
229
230 std::unique_ptr< Resolution > mySize;
231
232}; // Texture
233
234
235template< unsigned int dimension >
236Texture< 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
245template< unsigned int dimension >
246void Texture< dimension >::bind( unsigned int unit ) const
247{
249}
250
251
252template< unsigned int dimension >
254{
255 return mySize.get() != nullptr;
256}
257
258
259template< unsigned int dimension >
260const Eigen::Matrix< unsigned int, dimension, 1 >& Texture< dimension >::size() const
261{
262 CARNA_ASSERT( isValid() );
263 return *mySize;
264}
265
266
267template< unsigned int dimension >
268void 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
291template< unsigned int dimension >
292void 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::CarnaException, Carna::base::AssertionFailure.
#define CARNA_ASSERT_EX(expression, description)
If the given expression is false, a break point is raised in debug mode and an AssertionFailure throw...
#define CARNA_ASSERT(expression)
If the given expression is false, a break point is raised in debug mode and an AssertionFailure throw...
Represents an association.
Definition Association.h:45
AssociatedObjectType * get() const
Returns raw pointer to the referenced object.
Definition Association.h:61
Texture base class with dimension-independent definitions.
Definition Texture.h:78
void uploadGLTextureData(const Eigen::Matrix< unsigned int, 1, 1 > &size, int internalFormat, int pixelFormat, int bufferType, const void *bufferPtr)
Wraps glTexImage1d.
const unsigned int id
Holds the ID of the represented OpenGL texture object.
Definition Texture.h:87
TextureBase()
Creates OpenGL texture object.
virtual ~TextureBase()
Deletes the maintained OpenGL texture object.
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
void uploadGLTextureData(const math::Vector3ui &size, int internalFormat, int pixelFormat, int bufferType, const void *bufferPtr)
Wraps glTexImage3d.
void uploadGLTextureData(const math::Vector2ui &size, int internalFormat, int pixelFormat, int bufferType, const void *bufferPtr)
Wraps glTexImage2d.
Represents an OpenGL texture object. This class realizes the RAII-idiom.
Definition Texture.h:153
bool isValid() const
Tells whether the texture has been initialized, i.e. it has a size.
Definition Texture.h:253
void update(const Resolution &size)
Definition Texture.h:292
Eigen::Matrix< unsigned int, dimension, 1 > Resolution
Definition Texture.h:159
Texture(int internalFormat, int pixelFormat)
Creates OpenGL texture object.
Definition Texture.h:236
static const unsigned int DIMENSION
Holds the dimension of this texture.
Definition Texture.h:183
const int internalFormat
Holds the number of color components in the texture, e.g. GL_RGBA8UI or GL_INTENSITY16.
Definition Texture.h:196
void update(const Resolution &size, int bufferType, const void *bufferPtr)
Definition Texture.h:268
const int pixelFormat
Holds the format of the pixel data, e.g. GL_RED, GL_RGB or GL_RGBA.
Definition Texture.h:197
const Resolution & size() const
Tells the resolution of this texture.
Definition Texture.h:260
void bind(unsigned int unit) const
Binds this texture to unit. Consider using TextureBase::SETUP_UNIT if you're binding the texture temp...
Definition Texture.h:246
Defines Carna::base::math namespace and CARNA_FOR_VECTOR3UI.
Eigen::Matrix< unsigned int, 3, 1 > Vector3ui
Defines vector.
Definition math.h:199
Eigen::Matrix< unsigned int, 2, 1 > Vector2ui
Defines vector.
Definition math.h:200
void bindGLTextureObject< 1 >(unsigned int unit, unsigned int id)
void bindGLTextureObject< 3 >(unsigned int unit, unsigned int id)
void bindGLTextureObject< 2 >(unsigned int unit, unsigned int id)
#define NON_COPYABLE
Features class it is placed in as non-copyable.