Texture.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #include "Texture.h"
  2. #include "Renderer.h"
  3. #include "Image.h"
  4. //=====================================================================================================================================
  5. // Statics =
  6. //=====================================================================================================================================
  7. int Texture::textureUnitsNum = -1;
  8. bool Texture::mipmappingEnabled = true;
  9. bool Texture::compressionEnabled = false;
  10. int Texture::anisotropyLevel = 8;
  11. //=====================================================================================================================================
  12. // Constructor =
  13. //=====================================================================================================================================
  14. Texture::Texture():
  15. glId(numeric_limits<uint>::max()),
  16. type(GL_TEXTURE_2D)
  17. {
  18. }
  19. //=====================================================================================================================================
  20. // load =
  21. //=====================================================================================================================================
  22. bool Texture::load( const char* filename )
  23. {
  24. type = GL_TEXTURE_2D;
  25. if( glId != numeric_limits<uint>::max() )
  26. {
  27. ERROR( "Texture already loaded" );
  28. return false;
  29. }
  30. Image img;
  31. if( !img.load( filename ) ) return false;
  32. // bind the texture
  33. glGenTextures( 1, &glId );
  34. bind(0);
  35. if( mipmappingEnabled )
  36. {
  37. glTexParameteri( type, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR );
  38. }
  39. else
  40. {
  41. glTexParameteri( type, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
  42. }
  43. glTexParameteri( type, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
  44. glTexParameterf( type, GL_TEXTURE_MAX_ANISOTROPY_EXT, anisotropyLevel );
  45. // leave to GL_REPEAT. There is not real performance impact
  46. glTexParameteri( type, GL_TEXTURE_WRAP_S, GL_REPEAT );
  47. glTexParameteri( type, GL_TEXTURE_WRAP_T, GL_REPEAT );
  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( type, 0, intFormat, img.width, img.height, 0, format, GL_UNSIGNED_BYTE, img.data );
  60. if( mipmappingEnabled )
  61. {
  62. glGenerateMipmap(type);
  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. {
  72. DEBUG_ERR( glGetError() != GL_NO_ERROR ); // dont enter the func holding an error
  73. type = GL_TEXTURE_2D;
  74. DEBUG_ERR( internalFormat>0 && internalFormat<=4 ); // deprecated internal format
  75. DEBUG_ERR( glId != numeric_limits<uint>::max() ) // Texture already loaded
  76. // GL stuff
  77. glGenTextures( 1, &glId );
  78. bind();
  79. if( mipmappingEnabled )
  80. glTexParameteri( type, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR );
  81. else
  82. glTexParameteri( type, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
  83. texParameter( GL_TEXTURE_MAG_FILTER, GL_LINEAR );
  84. texParameter( GL_TEXTURE_WRAP_S, GL_REPEAT );
  85. texParameter( GL_TEXTURE_WRAP_T, GL_REPEAT );
  86. // allocate to vram
  87. glTexImage2D( type, 0, internalFormat, width_, height_, 0, format_, type_, NULL );
  88. if( mipmappingEnabled )
  89. glGenerateMipmap( type );
  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( type, glId );
  128. }