LibCarna Version 3.4.0
Loading...
Searching...
No Matches
Texture.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2010 - 2016 Leonid Kostrykin
3 *
4 * Chair of Medical Engineering (mediTEC)
5 * RWTH Aachen University
6 * Pauwelsstr. 20
7 * 52074 Aachen
8 * Germany
9 *
10 *
11 * Copyright (C) 2021 - 2025 Leonid Kostrykin
12 *
13 */
14
15#ifndef TEXTURE_H_6014714286
16#define TEXTURE_H_6014714286
17
18#include <LibCarna/LibCarna.hpp>
22
28namespace LibCarna
29{
30
31namespace base
32{
33
34
35// ----------------------------------------------------------------------------------
36// bindGLTextureObject
37// ----------------------------------------------------------------------------------
38
45template< unsigned int dimension >
46void bindGLTextureObject( unsigned int unit, unsigned int id )
47{
48 static_assert( dimension >= 1 && dimension <= 3, "Texture dimension must be 1, 2 or 3." );
49}
50
51
54template< >
55void LIBCARNA bindGLTextureObject< 1 >( unsigned int unit, unsigned int id );
56
57
60template< >
61void LIBCARNA bindGLTextureObject< 2 >( unsigned int unit, unsigned int id );
62
63
66template< >
67void LIBCARNA bindGLTextureObject< 3 >( unsigned int unit, unsigned int id );
68
69
70
71// ----------------------------------------------------------------------------------
72// TextureBase
73// ----------------------------------------------------------------------------------
74
80class LIBCARNA TextureBase
81{
82
84
85public:
86
90 const unsigned int id;
91
95 virtual ~TextureBase();
96
101 const static unsigned int SETUP_UNIT = 0;
102
106 static unsigned int maxTextureSize();
107
108protected:
109
114
119 ( const Eigen::Matrix< unsigned int, 1, 1 >& size
120 , int internalFormat
121 , int pixelFormat
122 , int bufferType
123 , const void* bufferPtr );
124
129 ( const math::Vector2ui& size
130 , int internalFormat
131 , int pixelFormat
132 , int bufferType
133 , const void* bufferPtr );
134
139 ( const math::Vector3ui& size
140 , int internalFormat
141 , int pixelFormat
142 , int bufferType
143 , const void* bufferPtr );
144
145}; // TextureBase
146
147
148
149// ----------------------------------------------------------------------------------
150// Texture
151// ----------------------------------------------------------------------------------
152
158template< unsigned int dimension >
159class Texture : public TextureBase
160{
161
162public:
163
166 typedef Eigen::Matrix< unsigned int, dimension, 1 > Resolution;
167
186
190 const static unsigned int DIMENSION = dimension;
191
196 const Resolution& size() const;
197
201 bool isValid() const;
202
203 const int internalFormat;
204 const int pixelFormat;
205
210 void bind( unsigned int unit ) const;
211
229 void update( const Resolution& size, int bufferType, const void* bufferPtr );
230
233 void update( const Resolution& size );
234
235private:
236
237 std::unique_ptr< Resolution > mySize;
238
239}; // Texture
240
241
242template< unsigned int dimension >
243Texture< dimension >::Texture( int internalFormat, int pixelFormat )
244 : internalFormat( internalFormat )
245 , pixelFormat( pixelFormat )
246{
247 static_assert( dimension >= 1 && dimension <= 3, "Texture dimension must be 1, 2 or 3." );
248 LIBCARNA_ASSERT_EX( id != 0, "Texture acquisition failed!" );
249}
250
251
252template< unsigned int dimension >
253void Texture< dimension >::bind( unsigned int unit ) const
254{
256}
257
258
259template< unsigned int dimension >
261{
262 return mySize.get() != nullptr;
263}
264
265
266template< unsigned int dimension >
267const Eigen::Matrix< unsigned int, dimension, 1 >& Texture< dimension >::size() const
268{
269 LIBCARNA_ASSERT( isValid() );
270 return *mySize;
271}
272
273
274template< unsigned int dimension >
275void Texture< dimension >::update( const Eigen::Matrix< unsigned int, dimension, 1 >& size, int bufferType, const void* bufferPtr )
276{
277 /* Ensure that texture size is positive.
278 */
279 for( unsigned int i = 0; i < dimension; ++i )
280 {
281 LIBCARNA_ASSERT_EX( size( i, 0 ) >= 1, "Texture only supports positive sizes!" );
282 }
283
284 /* Ensure that z-component of the texture size is even if this is a 3D texture.
285 */
286 if( dimension == 3 )
287 {
288 LIBCARNA_ASSERT_EX( size( 2, 0 ) % 2 == 0, "3D textures must have even depth!" );
289 }
290
291 /* Update the OpenGL texture object.
292 */
293 this->bind( SETUP_UNIT );
294 uploadGLTextureData( size, internalFormat, pixelFormat, bufferType, bufferPtr );
295}
296
297
298template< unsigned int dimension >
299void Texture< dimension >::update( const Eigen::Matrix< unsigned int, dimension, 1 >& size )
300{
301 update( size, 0, nullptr );
302}
303
304
305
306} // namespace LibCarna :: base
307
308} // namespace LibCarna
309
310#endif // TEXTURE_H_6014714286
Defines LibCarna::base::LibCarnaException and LibCarna::base::AssertionFailure.
#define LIBCARNA_ASSERT_EX(expression, description)
If the given expression is false, a break point is raised in debug mode and an AssertionFailure throw...
#define LIBCARNA_ASSERT(expression)
If the given expression is false, a break point is raised in debug mode and an AssertionFailure throw...
Contains forward-declarations.
Represents an association.
AssociatedObjectType * get() const
Returns raw pointer to the referenced object.
Texture base class with dimension-independent definitions.
Definition Texture.hpp:81
void uploadGLTextureData(const math::Vector2ui &size, int internalFormat, int pixelFormat, int bufferType, const void *bufferPtr)
Wraps glTexImage2d.
void uploadGLTextureData(const Eigen::Matrix< unsigned int, 1, 1 > &size, int internalFormat, int pixelFormat, int bufferType, const void *bufferPtr)
Wraps glTexImage1d.
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.hpp:46
static unsigned int maxTextureSize()
Queries GL_MAX_TEXTURE_SIZE from the current GL context.
TextureBase()
Creates OpenGL texture object.
const unsigned int id
Holds the ID of the represented OpenGL texture object.
Definition Texture.hpp:90
void uploadGLTextureData(const math::Vector3ui &size, int internalFormat, int pixelFormat, int bufferType, const void *bufferPtr)
Wraps glTexImage3d.
virtual ~TextureBase()
Deletes the maintained OpenGL texture object.
Represents an OpenGL texture object. This class realizes the RAII-idiom.
Definition Texture.hpp:160
const int internalFormat
Holds the number of color components in the texture, e.g. GL_RGBA8UI or GL_INTENSITY16.
Definition Texture.hpp:203
static const unsigned int DIMENSION
Holds the dimension of this texture.
Definition Texture.hpp:190
Texture(int internalFormat, int pixelFormat)
Creates OpenGL texture object.
Definition Texture.hpp:243
Eigen::Matrix< unsigned int, dimension, 1 > Resolution
Definition Texture.hpp:166
void update(const Resolution &size)
Definition Texture.hpp:299
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.hpp:253
void update(const Resolution &size, int bufferType, const void *bufferPtr)
Definition Texture.hpp:275
const Resolution & size() const
Tells the resolution of this texture.
Definition Texture.hpp:267
const int pixelFormat
Holds the format of the pixel data, e.g. GL_RED, GL_RGB or GL_RGBA.
Definition Texture.hpp:204
bool isValid() const
Tells whether the texture has been initialized, i.e. it has a size.
Definition Texture.hpp:260
Defines LibCarna::base::math namespace and LIBCARNA_FOR_VECTOR3UI.
Eigen::Matrix< unsigned int, 2, 1 > Vector2ui
Defines vector.
Definition math.hpp:204
Eigen::Matrix< unsigned int, 3, 1 > Vector3ui
Defines vector.
Definition math.hpp:203
void bindGLTextureObject< 3 >(unsigned int unit, unsigned int id)
void bindGLTextureObject< 1 >(unsigned int unit, unsigned int id)
void bindGLTextureObject< 2 >(unsigned int unit, unsigned int id)
Defines LibCarna::base::noncopyable and NON_COPYABLE.
#define NON_COPYABLE
Marks the class that it is placed in as non-copyable.