Texture.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. #include <GL/glew.h>
  2. #include "Texture.h"
  3. #include "Renderer.h"
  4. #include "Image.h"
  5. //======================================================================================================================
  6. // Statics =
  7. //======================================================================================================================
  8. int Texture::textureUnitsNum = -1;
  9. bool Texture::mipmappingEnabled = true;
  10. bool Texture::compressionEnabled = false;
  11. int Texture::anisotropyLevel = 8;
  12. //======================================================================================================================
  13. // Constructor =
  14. //======================================================================================================================
  15. Texture::Texture():
  16. glId( numeric_limits<uint>::max() ),
  17. target( GL_TEXTURE_2D )
  18. {
  19. }
  20. //======================================================================================================================
  21. // load =
  22. //======================================================================================================================
  23. bool Texture::load( const char* filename )
  24. {
  25. target = GL_TEXTURE_2D;
  26. if( glId != numeric_limits<uint>::max() )
  27. {
  28. ERROR( "Texture already loaded" );
  29. return false;
  30. }
  31. Image img;
  32. if( !img.load( filename ) ) return false;
  33. // bind the texture
  34. glGenTextures( 1, &glId );
  35. bind(0);
  36. if( mipmappingEnabled )
  37. {
  38. setTexParameter( GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR );
  39. }
  40. else
  41. {
  42. setTexParameter( GL_TEXTURE_MIN_FILTER, GL_LINEAR );
  43. }
  44. setTexParameter( GL_TEXTURE_MAG_FILTER, GL_LINEAR );
  45. texParameter( GL_TEXTURE_MAX_ANISOTROPY_EXT, float(anisotropyLevel) );
  46. // leave to GL_REPEAT. There is not real performance impact
  47. setRepeat( true );
  48. int format = (img.bpp==32) ? GL_RGBA : GL_RGB;
  49. int intFormat; // the internal format of the image
  50. if( compressionEnabled )
  51. {
  52. //int_format = (img.bpp==32) ? GL_COMPRESSED_RGBA_S3TC_DXT1_EXT : GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
  53. intFormat = (img.bpp==32) ? GL_COMPRESSED_RGBA : GL_COMPRESSED_RGB;
  54. }
  55. else
  56. {
  57. intFormat = (img.bpp==32) ? GL_RGBA : GL_RGB;
  58. }
  59. glTexImage2D( target, 0, intFormat, img.width, img.height, 0, format, GL_UNSIGNED_BYTE, img.data );
  60. if( mipmappingEnabled )
  61. {
  62. glGenerateMipmap(target);
  63. }
  64. img.unload();
  65. return true;
  66. }
  67. //======================================================================================================================
  68. // createEmpty2D =
  69. //======================================================================================================================
  70. bool Texture::createEmpty2D( float width_, float height_, int internalFormat, int format_, GLenum type_,
  71. bool mimapping )
  72. {
  73. DEBUG_ERR( glGetError() != GL_NO_ERROR ); // dont enter the func with prev error
  74. target = GL_TEXTURE_2D;
  75. DEBUG_ERR( internalFormat>0 && internalFormat<=4 ); // deprecated internal format
  76. DEBUG_ERR( glId != numeric_limits<uint>::max() ); // Texture already loaded
  77. // GL stuff
  78. glGenTextures( 1, &glId );
  79. bind();
  80. if( mimapping )
  81. setTexParameter( GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR );
  82. else
  83. setTexParameter( GL_TEXTURE_MIN_FILTER, GL_LINEAR );
  84. setTexParameter( GL_TEXTURE_MAG_FILTER, GL_LINEAR );
  85. setRepeat( true );
  86. // allocate to vram
  87. glTexImage2D( target, 0, internalFormat, width_, height_, 0, format_, type_, NULL );
  88. if( mimapping )
  89. glGenerateMipmap( target );
  90. GLenum errid = glGetError();
  91. if( errid != GL_NO_ERROR )
  92. {
  93. ERROR( "OpenGL Error: " << gluErrorString( errid ) );
  94. return false;
  95. }
  96. return true;
  97. }
  98. //======================================================================================================================
  99. // createEmpty2DMSAA =
  100. //======================================================================================================================
  101. bool Texture::createEmpty2DMSAA( float width, float height, int samples_num, int internal_format )
  102. {
  103. /*type = GL_TEXTURE_2D_MULTISAMPLE;
  104. DEBUG_ERR( internal_format>0 && internal_format<=4 ); // deprecated internal format
  105. DEBUG_ERR( glId != numeric_limits<uint>::max() ) // Texture already loaded
  106. glGenTextures( 1, &glId );
  107. bind();
  108. // allocate
  109. glTexImage2DMultisample( type, samples_num, internal_format, width, height, false );*/
  110. return true;
  111. }
  112. //======================================================================================================================
  113. // unload =
  114. //======================================================================================================================
  115. void Texture::unload()
  116. {
  117. glDeleteTextures( 1, &glId );
  118. }
  119. //======================================================================================================================
  120. // bind =
  121. //======================================================================================================================
  122. void Texture::bind( uint unit ) const
  123. {
  124. if( unit >= static_cast<uint>(textureUnitsNum) )
  125. WARNING("Max tex units passed");
  126. glActiveTexture( GL_TEXTURE0+unit );
  127. glBindTexture( target, glId );
  128. }
  129. //======================================================================================================================
  130. // getWidth =
  131. //======================================================================================================================
  132. int Texture::getWidth() const
  133. {
  134. bind();
  135. int i;
  136. glGetTexLevelParameteriv( target, 0, GL_TEXTURE_WIDTH, &i );
  137. return i;
  138. }
  139. //======================================================================================================================
  140. // getHeight =
  141. //======================================================================================================================
  142. int Texture::getHeight() const
  143. {
  144. bind();
  145. int i;
  146. glGetTexLevelParameteriv( target, 0, GL_TEXTURE_HEIGHT, &i );
  147. return i;
  148. }
  149. //======================================================================================================================
  150. // setTexParameter [int] =
  151. //======================================================================================================================
  152. void Texture::setTexParameter( GLenum paramName, GLint value ) const
  153. {
  154. bind();
  155. glTexParameteri( target, paramName, value );
  156. }
  157. //======================================================================================================================
  158. // setTexParameter [float] =
  159. //======================================================================================================================
  160. void Texture::texParameter( GLenum paramName, GLfloat value ) const
  161. {
  162. bind();
  163. glTexParameterf( target, paramName, value );
  164. }