| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- #include "Texture.h"
- #include "Renderer.h"
- #include "Image.h"
- //=====================================================================================================================================
- // Statics =
- //=====================================================================================================================================
- int Texture::textureUnitsNum = -1;
- bool Texture::mipmappingEnabled = true;
- bool Texture::compressionEnabled = false;
- int Texture::anisotropyLevel = 8;
- //=====================================================================================================================================
- // Constructor =
- //=====================================================================================================================================
- Texture::Texture():
- glId(numeric_limits<uint>::max()),
- type(GL_TEXTURE_2D)
- {
- }
- //=====================================================================================================================================
- // load =
- //=====================================================================================================================================
- bool Texture::load( const char* filename )
- {
- type = GL_TEXTURE_2D;
- if( glId != numeric_limits<uint>::max() )
- {
- ERROR( "Texture already loaded" );
- return false;
- }
- Image img;
- if( !img.load( filename ) ) return false;
- // bind the texture
- glGenTextures( 1, &glId );
- bind(0);
- if( mipmappingEnabled )
- {
- glTexParameteri( type, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR );
- }
- else
- {
- glTexParameteri( type, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
- }
- glTexParameteri( type, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
- glTexParameterf( type, GL_TEXTURE_MAX_ANISOTROPY_EXT, anisotropyLevel );
- // leave to GL_REPEAT. There is not real performance impact
- glTexParameteri( type, GL_TEXTURE_WRAP_S, GL_REPEAT );
- glTexParameteri( type, GL_TEXTURE_WRAP_T, GL_REPEAT );
- int format = (img.bpp==32) ? GL_RGBA : GL_RGB;
- int intFormat; // the internal format of the image
- if( compressionEnabled )
- {
- //int_format = (img.bpp==32) ? GL_COMPRESSED_RGBA_S3TC_DXT1_EXT : GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
- intFormat = (img.bpp==32) ? GL_COMPRESSED_RGBA : GL_COMPRESSED_RGB;
- }
- else
- {
- intFormat = (img.bpp==32) ? GL_RGBA : GL_RGB;
- }
- glTexImage2D( type, 0, intFormat, img.width, img.height, 0, format, GL_UNSIGNED_BYTE, img.data );
- if( mipmappingEnabled )
- {
- glGenerateMipmap(type);
- }
- img.unload();
- return true;
- }
- //=====================================================================================================================================
- // createEmpty2D =
- //=====================================================================================================================================
- bool Texture::createEmpty2D( float width_, float height_, int internalFormat, int format_, GLenum type_ )
- {
- DEBUG_ERR( glGetError() != GL_NO_ERROR ); // dont enter the func holding an error
- type = GL_TEXTURE_2D;
- DEBUG_ERR( internalFormat>0 && internalFormat<=4 ); // deprecated internal format
- DEBUG_ERR( glId != numeric_limits<uint>::max() ) // Texture already loaded
- // GL stuff
- glGenTextures( 1, &glId );
- bind();
- if( mipmappingEnabled )
- glTexParameteri( type, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR );
- else
- glTexParameteri( type, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
- texParameter( GL_TEXTURE_MAG_FILTER, GL_LINEAR );
- texParameter( GL_TEXTURE_WRAP_S, GL_REPEAT );
- texParameter( GL_TEXTURE_WRAP_T, GL_REPEAT );
- // allocate to vram
- glTexImage2D( type, 0, internalFormat, width_, height_, 0, format_, type_, NULL );
- if( mipmappingEnabled )
- glGenerateMipmap( type );
- GLenum errid = glGetError();
- if( errid != GL_NO_ERROR )
- {
- ERROR( "OpenGL Error: " << gluErrorString( errid ) );
- return false;
- }
- return true;
- }
- //=====================================================================================================================================
- // createEmpty2DMSAA =
- //=====================================================================================================================================
- bool Texture::createEmpty2DMSAA( float width, float height, int samples_num, int internal_format )
- {
- /*type = GL_TEXTURE_2D_MULTISAMPLE;
- DEBUG_ERR( internal_format>0 && internal_format<=4 ); // deprecated internal format
- DEBUG_ERR( glId != numeric_limits<uint>::max() ) // Texture already loaded
- glGenTextures( 1, &glId );
- bind();
-
- // allocate
- glTexImage2DMultisample( type, samples_num, internal_format, width, height, false );*/
- return true;
- }
- //=====================================================================================================================================
- // unload =
- //=====================================================================================================================================
- void Texture::unload()
- {
- glDeleteTextures( 1, &glId );
- }
- //=====================================================================================================================================
- // bind =
- //=====================================================================================================================================
- void Texture::bind( uint unit ) const
- {
- if( unit >= static_cast<uint>(textureUnitsNum) )
- WARNING("Max tex units passed");
- glActiveTexture( GL_TEXTURE0+unit );
- glBindTexture( type, glId );
- }
|