47 #if defined(VL_OPENGL_ES1) || defined(VL_OPENGL_ES2) 48 return internal_format;
50 switch(internal_format)
77 return GL_LUMINANCE_ALPHA;
142 return GL_RED_INTEGER;
150 return GL_RG_INTEGER;
164 return GL_RGB_INTEGER;
179 return GL_RGBA_INTEGER;
184 return GL_DEPTH_STENCIL;
191 return GL_DEPTH_COMPONENT;
200 return GL_ALPHA_INTEGER;
208 return GL_RED_INTEGER;
216 return GL_LUMINANCE_INTEGER_EXT;
224 return GL_LUMINANCE_ALPHA_INTEGER_EXT;
235 switch( internal_format )
256 return GL_UNSIGNED_BYTE;
291 return GL_UNSIGNED_SHORT;
313 return GL_UNSIGNED_INT;
326 return GL_UNSIGNED_INT_24_8;
349 return GL_FLOAT_32_UNSIGNED_INT_24_8_REV;
352 return GL_UNSIGNED_BYTE;
374 void Texture::reset()
394 VL_DEBUG_SET_OBJECT_NAME()
405 VL_DEBUG_SET_OBJECT_NAME()
410 Log::error(
"2D texture constructor failed!\n");
416 VL_DEBUG_SET_OBJECT_NAME()
421 Log::error(
"3D texture constructor failed!\n");
427 VL_DEBUG_SET_OBJECT_NAME()
435 #if defined(VL_OPENGL) 450 Log::bug(
"Texture constructor called with an invalid Image!\n");
455 VL_DEBUG_SET_OBJECT_NAME()
465 #if defined(VL_OPENGL) 480 Log::bug(
"Texture constructor called with an invalid Image!\n");
485 VL_DEBUG_SET_OBJECT_NAME()
495 return handle() != 0 && (a|b|c);
498 bool Texture::supports(
ETextureDimension tex_dimension,
ETextureFormat tex_format,
int mip_level,
EImageDimension img_dimension,
int w,
int h,
int d,
bool border,
int samples,
bool fixedsamplelocations,
bool verbose)
512 if (verbose)
Log::error(
"Texture::supports(): multisample textures not supported by the current hardware.\n");
518 if (verbose)
Log::error(
"Texture::supports(): multisample textures cannot have borders.\n");
524 if (verbose)
Log::error(
"Texture::supports(): multisample textures cannot have mip levels other than 0.\n");
536 if (verbose)
Log::error(
"Texture::supports(): texture buffer not supported by the current hardware.\n");
542 if (verbose)
Log::error(
"Texture::supports(): a texture buffer cannot have borders.\n");
548 if (verbose)
Log::error(
"Texture::supports(): a texture buffer cannot have mip levels other than 0.\n");
562 if (verbose)
Log::error(
"Texture::supports(): texture cubemap not supported by the current hardware.\n");
568 if (verbose)
Log::error(
"Texture::supports(): cubemaps must have square faces.\n");
579 if (verbose)
Log::error(
"Texture::supports(): you cannot create a texture array with borders.\n");
585 if (verbose)
Log::error(
"Texture::supports(): texture array not supported by the current hardware.\n");
593 if (verbose)
Log::error(
"Texture::supports(): the image dimensions are not suitable to create a texture array." 594 "To create a 1D texture array you need a 2D image and to create a 2D texture array you need a 3D image.\n");
606 if (verbose)
Log::error(
"Texture::supports(): texture rectangle not supported by the current hardware.\n");
610 if ( mip_level != 0 )
612 if (verbose)
Log::error(
"Texture::supports(): TD_TEXTURE_RECTANGLE textures do not support mipmapping level other than zero.\n");
618 if (verbose)
Log::error(
"Texture::supports(): TD_TEXTURE_RECTANGLE textures do not allow textures borders\n");
624 #if defined(VL_OPENGL) 627 int default_format = getDefaultFormat(tex_format);
628 int default_type = getDefaultType(tex_format);
637 glTexImage2DMultisample(GL_PROXY_TEXTURE_2D_MULTISAMPLE, samples, tex_format, w, h, fixedsamplelocations );
640 if (verbose)
Log::error(
Say(
"Texture::supports(): 2d multisample texture requested with too many samples for the current hardware! (%n)\n") << samples );
643 glGetTexLevelParameteriv(GL_PROXY_TEXTURE_2D_MULTISAMPLE, 0, GL_TEXTURE_WIDTH, &width);
VL_CHECK_OGL();
648 glTexImage3DMultisample(GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY, samples, tex_format, w, h, d, fixedsamplelocations );
VL_CHECK_OGL();
651 if (verbose)
Log::error(
Say(
"Texture::supports(): multisample texture array requested with too many samples for the current hardware! (%n)\n") << samples );
654 glGetTexLevelParameteriv(GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY, 0, GL_TEXTURE_WIDTH, &width);
VL_CHECK_OGL();
659 glTexImage2D(GL_PROXY_TEXTURE_CUBE_MAP, mip_level, tex_format, w + (border?2:0), h + (border?2:0), border?1:0, default_format, default_type,
NULL);
660 glGetTexLevelParameteriv(GL_PROXY_TEXTURE_CUBE_MAP, mip_level, GL_TEXTURE_WIDTH, &width);
665 glTexImage3D(GL_PROXY_TEXTURE_2D_ARRAY, mip_level, tex_format, w + (border?2:0), h + (border?2:0), d + (border?2:0), border?1:0, default_format, default_type,
NULL);
666 glGetTexLevelParameteriv(GL_PROXY_TEXTURE_2D_ARRAY, mip_level, GL_TEXTURE_WIDTH, &width);
671 glTexImage3D(GL_PROXY_TEXTURE_3D, mip_level, tex_format, w + (border?2:0), h + (border?2:0), d + (border?2:0), border?1:0, default_format, default_type,
NULL);
672 glGetTexLevelParameteriv(GL_PROXY_TEXTURE_3D, mip_level, GL_TEXTURE_WIDTH, &width);
677 glTexImage2D(GL_PROXY_TEXTURE_RECTANGLE, mip_level, tex_format, w, h, 0, default_format, default_type,
NULL);
678 glGetTexLevelParameteriv(GL_PROXY_TEXTURE_RECTANGLE, mip_level, GL_TEXTURE_WIDTH, &width);
683 glTexImage2D(GL_PROXY_TEXTURE_1D_ARRAY, mip_level, tex_format, w, h, 0, default_format, default_type,
NULL);
684 glGetTexLevelParameteriv(GL_PROXY_TEXTURE_1D_ARRAY, mip_level, GL_TEXTURE_WIDTH, &width);
689 glTexImage2D(GL_PROXY_TEXTURE_2D, mip_level, tex_format, w + (border?2:0), h + (border?2:0), border?1:0, default_format, default_type,
NULL);
690 glGetTexLevelParameteriv(GL_PROXY_TEXTURE_2D, mip_level, GL_TEXTURE_WIDTH, &width);
695 glTexImage1D(GL_PROXY_TEXTURE_1D, mip_level, tex_format, w + (border?2:0), border?1:0, default_format, default_type,
NULL);
696 glGetTexLevelParameteriv(GL_PROXY_TEXTURE_1D, mip_level, GL_TEXTURE_WIDTH, &width);
699 GLenum err = glGetError();
700 return err == 0 && width != 0;
717 if( ! buffer_object || ! buffer_object->
handle() || ! glIsBuffer( buffer_object->
handle() ) )
719 Log::bug(
"Texture::createTexture() requires a non NULL valid buffer object in order to create a texture buffer!\n" );
727 GLint buffer_size = 0;
728 glBindBuffer(GL_TEXTURE_BUFFER, buffer_object->
handle());
729 glGetBufferParameteriv(GL_TEXTURE_BUFFER, GL_BUFFER_SIZE, &buffer_size);
730 glBindBuffer(GL_TEXTURE_BUFFER, 0);
731 if ( buffer_size == 0 )
733 Log::bug(
"Texture::createTexture(): cannot create a texture buffer with an empty buffer object!\n");
VL_TRAP();
738 if ( !
supports(tex_dimension , tex_format, 0,
ID_None, w, h, d, border, samples, fixedsamplelocations,
true) )
741 Log::bug(
"Texture::createTexture(): the format/size combination requested is not supported!\n");
VL_TRAP();
749 Log::bug(
"Texture::createTexture(): texture creation failed!\n");
765 int default_format = getDefaultFormat(tex_format);
766 int default_type = getDefaultType(tex_format);
770 glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, samples, tex_format, w, h, fixedsamplelocations );
VL_CHECK_OGL();
775 glTexImage3DMultisample(GL_TEXTURE_2D_MULTISAMPLE_ARRAY, samples, tex_format, w, h, d, fixedsamplelocations );
VL_CHECK_OGL();
782 glTexBuffer(GL_TEXTURE_BUFFER, tex_format, buffer_object->
handle());
783 unsigned int glerr = glGetError();
784 if (glerr != GL_NO_ERROR)
787 Log::bug(
"Texture::createTexture(): glTexBuffer() failed with error: '" + msg +
"'.\n" );
788 Log::error(
"Probably you supplied a non supported texture format! Review the glTexBuffer() man page for a complete list of supported texture formats.\n");
795 glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, tex_format, w + (border?2:0), h + (border?2:0), border?1:0, default_format, default_type,
NULL);
796 glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0, tex_format, w + (border?2:0), h + (border?2:0), border?1:0, default_format, default_type,
NULL);
797 glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0, tex_format, w + (border?2:0), h + (border?2:0), border?1:0, default_format, default_type,
NULL);
798 glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, 0, tex_format, w + (border?2:0), h + (border?2:0), border?1:0, default_format, default_type,
NULL);
799 glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0, tex_format, w + (border?2:0), h + (border?2:0), border?1:0, default_format, default_type,
NULL);
800 glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0, tex_format, w + (border?2:0), h + (border?2:0), border?1:0, default_format, default_type,
NULL);
806 VL_glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, tex_format, w + (border?2:0), h + (border?2:0), d + (border?2:0), border?1:0, default_format, default_type,
NULL);
812 VL_glTexImage3D(GL_TEXTURE_3D, 0, tex_format, w + (border?2:0), h + (border?2:0), d + (border?2:0), border?1:0, default_format, default_type,
NULL);
818 glTexImage2D(GL_TEXTURE_RECTANGLE, 0, tex_format, w, h, 0, default_format, default_type,
NULL);
824 glTexImage2D(GL_TEXTURE_1D_ARRAY, 0, tex_format, w, h, 0, default_format, default_type,
NULL);
830 glTexImage2D(GL_TEXTURE_2D, 0, tex_format, w + (border?2:0), h + (border?2:0), border?1:0, default_format, default_type,
NULL);
836 glTexImage1D(GL_TEXTURE_1D, 0, tex_format, w + (border?2:0), border?1:0, default_format, default_type,
NULL);
848 #if defined(VL_OPENGL_ES1) || defined(VL_OPENGL_ES2) 851 Log::bug(
"Texture::setMipLevel(): under OpenGL ES the texture internal format must match the source image format!\n");
858 Log::bug(
"You cannot call Texture::setMipLevel() on a texture buffer or on a multisample texture!\n");
864 Log::error(
"Texture::setMipLevel(): texture hasn't been created yet, please call createTexture() first!\n");
869 if ( !
supports(
dimension(),
internalFormat(), mip_level, img->
dimension(), img->
width(), img->
height(), img->
depth(),
border(), 0, 0,
true) )
872 Log::error(
"Texture::setMipLevel(): the format/size combination requested is not supported.\n");
885 bool use_glu =
false;
886 GLint generate_mipmap_orig = GL_FALSE;
902 Log::error(
"Texture::setMipLevel(): automatic mipmaps generation for levels below 0 requires OpenGL 1.4 minimum.\n");
906 #define VL_IS_POW_2(x) ((x != 0) && ((x & (x - 1)) == 0)) 908 Log::warning(
"Texture::setMipLevel(): the image will be rescaled to the nearest upper power of 2.\n");
912 if ( use_glu && is_compressed )
914 Log::error(
"Texture::setMipLevel(): could not generate compressed mipmaps, OpenGL 1.4 required.\n");
920 Log::error(
"Texture::setMipLevel(): could not generate 3D mipmaps, OpenGL 1.4 required.\n");
1078 glPixelStorei( GL_UNPACK_ALIGNMENT, 4 );
1090 class InOutCondition
1094 InOutCondition(
Texture* tex): mTex(tex) {}
1112 InOutCondition in_out_condition(
this);
1161 std::vector<const vl::Image*> mipmaps;
1162 mipmaps.push_back(img);
1163 for(
int i=0; i<(int)img->mipmaps().size(); ++i)
1164 mipmaps.push_back( img->mipmaps()[i].get() );
1174 if (mipmaps.size() > 1)
1176 for(
int i=0; i<(int)mipmaps.size(); ++i)
1180 if (mipmaps.size() == 1)
1268 for(
int i=0; comp[i]; ++i)
1270 if(comp[i] == format)
int height() const
The vertical dimension of the texture in texels.
ETextureFormat format() const
void prepareTexture2D(int width, int height, ETextureFormat format, bool border=false)
Prepares for creation an empty 2D texture.
bool mFixedSamplesLocation
Texture()
Constructs an null texture that can be initialized later using one of the prepareTexture*() functions...
bool Has_Cubemap_Textures
unsigned int handle() const
OpenGL texture handle as returned by glGenTextures().
bool Has_Texture_Multisample
Wraps the OpenGL function glTexParameter(), see also http://www.opengl.org/sdk/docs/man/xhtml/glTexPa...
bool Has_glGenerateMipmaps
static bool supports(ETextureDimension tex_dimension, ETextureFormat tex_format, int mip_level, EImageDimension img_dimension, int w, int h, int d, bool border, int samples, bool fixedsamplelocations, bool verbose)
Checks whether the specified texture type, format and dimension combination is supported by the curre...
A simple String formatting class.
const unsigned char * pixels() const
Raw pointer to pixels.
void setObjectName(const char *name)
The name of the object, by default set to the object's class name in debug builds.
static void warning(const String &message)
Use this function to provide information about situations that might lead to errors or loss of data...
The String class implements an advanced UTF16 (Unicode BMP) string manipulation engine.
void setImage(const Image *image)
ref< TexParameter > mTexParameter
const SetupParams * setupParams() const
See SetupParams.
void setBorder(bool border)
Whether the texture has a 1 pixel texture border or not.
static void error(const String &message)
Use this function to provide information about run-time errors: file not found, out of memory...
static bool isCompressedFormat(int format)
Returns true if the specified format is compressed.
bool Has_Texture_Rectangle
ETextureDimension mDimension
Wraps an OpenGL texture object representing and managing all the supported texture types...
bool createTexture()
Creates a texture using the parameters specified by the last prepareTexture*() called.
int requiredMemory() const
Returns the number of bytes requested to store the image.
bool isValid() const
Returns true if the image has valid width/height/depth, pitch and byte alignment, type/format combina...
VLCORE_EXPORT ref< Image > loadImage(VirtualFile *file)
Loads an image from the specified file.
void setDepth(int z)
The z dimension of the texture in texels.
bool border() const
Whether the texture has a 1 pixel texture border or not.
int byteAlignment() const
Returns the byte-alignment of the row of the image.
unsigned char * pixelsXN()
X-negative cubemap face.
void setInternalFormat(ETextureFormat format)
The texture internal format as pecified by the internalFormat parameter of glTexImage*().
Visualization Library main namespace.
void prepareTexture1D(int width, ETextureFormat format, bool border=false)
Prepares for creation an empty 1D texture.
unsigned char * pixelsZN()
Z-negative cubemap face.
BufferObject * bufferObject()
The buffer object bound to a buffer object texture.
static void bug(const String &message)
Use this function to provide information about programming errors: wrong parameter initialization...
void setHeight(int y)
The vertical dimension of the texture in texels.
void prepareTexture3D(int width, int height, int depth, ETextureFormat format, bool border=false)
Prepares for creation an empty 3D texture.
bool isDepthTexture() const
Returns true if the texture is a depth or depth/stencil textre.
int samples() const
The number of samples of a multisample texture.
void setDimension(ETextureDimension dimension)
The texture type (1d, 2d, cubemap etc.) as specified by the target parameter of glTexImage*().
unsigned char * pixelsZP()
Z-positive cubemap face.
ref< BufferObject > mBufferObject
EImageDimension dimension() const
bool setMipLevel(int mip_level, const Image *img, bool gen_mipmaps)
Copies the texture image to the specified mip-maping level.
bool Has_GL_GENERATE_MIPMAP
unsigned char * pixelsYP()
Y-positive cubemap face.
ETextureDimension dimension() const
ETextureFormat internalFormat() const
The texture internal format as pecified by the internalFormat parameter of glTexImage*().
bool isValid() const
Returns true if the current texture configuration seems valid.
unsigned char * pixelsYN()
Y-negative cubemap face.
ETextureDimension dimension() const
The texture type (1d, 2d, cubemap etc.) as specified by the target parameter of glTexImage*().
VLGRAPHICS_EXPORT const char * getGLErrorString(int err)
Returns a readable string corresponding to the given OpenGL error code as returned by glGetError() ...
The BufferObject class is a Buffer that can upload its data on the GPU memory.
unsigned int handle() const
SetupParams wraps all the parameters needed to crate a Texture.
void prepareTextureCubemap(int width, int height, ETextureFormat format, bool border=false)
Prepares for creation an empty cubemap texture.
void clone(const Texture &other)
Copies all the texture parameters form the specified texture, including the OpenGL texture handle...
Implements a generic 1d, 2d, 3d and cubemap image that can have mipmaps.
The ref<> class is used to reference-count an Object.
int depth() const
The z dimension of the texture in texels.
unsigned char * pixelsXP()
X-positive cubemap face.
ref< SetupParams > mSetupParams
void setWidth(int x)
The horizontal dimension of the texture in texels.
void destroyTexture()
Destroys the GL texture object if managed() is true (default).
const Image * image() const
EImageFormat format() const
int width() const
The horizontal dimension of the texture in texels.