Texture.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. Resource(RT_MATERIAL),
  17. glId(numeric_limits<uint>::max()),
  18. target(GL_TEXTURE_2D)
  19. {
  20. }
  21. //======================================================================================================================
  22. // load =
  23. //======================================================================================================================
  24. bool Texture::load(const char* filename)
  25. {
  26. target = GL_TEXTURE_2D;
  27. if(glId != numeric_limits<uint>::max())
  28. {
  29. ERROR("Texture already loaded");
  30. return false;
  31. }
  32. Image img;
  33. if(!img.load(filename)) return false;
  34. // bind the texture
  35. glGenTextures(1, &glId);
  36. bind(0);
  37. if(mipmappingEnabled)
  38. {
  39. setTexParameter(GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  40. }
  41. else
  42. {
  43. setTexParameter(GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  44. }
  45. setTexParameter(GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  46. texParameter(GL_TEXTURE_MAX_ANISOTROPY_EXT, float(anisotropyLevel));
  47. // leave to GL_REPEAT. There is not real performance impact
  48. setRepeat(true);
  49. int format = (img.bpp==32) ? GL_RGBA : GL_RGB;
  50. int intFormat; // the internal format of the image
  51. if(compressionEnabled)
  52. {
  53. //int_format = (img.bpp==32) ? GL_COMPRESSED_RGBA_S3TC_DXT1 : GL_COMPRESSED_RGB_S3TC_DXT1;
  54. intFormat = (img.bpp==32) ? GL_COMPRESSED_RGBA : GL_COMPRESSED_RGB;
  55. }
  56. else
  57. {
  58. intFormat = (img.bpp==32) ? GL_RGBA : GL_RGB;
  59. }
  60. glTexImage2D(target, 0, intFormat, img.width, img.height, 0, format, GL_UNSIGNED_BYTE, img.data);
  61. if(mipmappingEnabled)
  62. {
  63. glGenerateMipmap(target);
  64. }
  65. img.unload();
  66. return true;
  67. }
  68. //======================================================================================================================
  69. // createEmpty2D =
  70. //======================================================================================================================
  71. bool Texture::createEmpty2D(float width_, float height_, int internalFormat, int format_, GLenum type_,
  72. bool mimapping)
  73. {
  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. return GL_OK();
  91. }
  92. //======================================================================================================================
  93. // createEmpty2DMSAA =
  94. //======================================================================================================================
  95. bool Texture::createEmpty2DMsaa(int samplesNum, int internalFormat, int width_, int height_, bool mimapping)
  96. {
  97. target = GL_TEXTURE_2D_MULTISAMPLE;
  98. DEBUG_ERR(glId != numeric_limits<uint>::max()); // Texture already loaded
  99. glGenTextures(1, &glId);
  100. bind();
  101. // allocate
  102. glTexImage2DMultisample(target, samplesNum, internalFormat, width_, height_, false);
  103. if(mimapping)
  104. glGenerateMipmap(target);
  105. return GL_OK();
  106. }
  107. //======================================================================================================================
  108. // unload =
  109. //======================================================================================================================
  110. void Texture::unload()
  111. {
  112. glDeleteTextures(1, &glId);
  113. }
  114. //======================================================================================================================
  115. // bind =
  116. //======================================================================================================================
  117. void Texture::bind(uint unit) const
  118. {
  119. if(unit >= static_cast<uint>(textureUnitsNum))
  120. WARNING("Max tex units passed");
  121. glActiveTexture(GL_TEXTURE0+unit);
  122. glBindTexture(target, glId);
  123. }
  124. //======================================================================================================================
  125. // getWidth =
  126. //======================================================================================================================
  127. int Texture::getWidth() const
  128. {
  129. bind();
  130. int i;
  131. glGetTexLevelParameteriv(target, 0, GL_TEXTURE_WIDTH, &i);
  132. return i;
  133. }
  134. //======================================================================================================================
  135. // getHeight =
  136. //======================================================================================================================
  137. int Texture::getHeight() const
  138. {
  139. bind();
  140. int i;
  141. glGetTexLevelParameteriv(target, 0, GL_TEXTURE_HEIGHT, &i);
  142. return i;
  143. }
  144. //======================================================================================================================
  145. // setTexParameter [int] =
  146. //======================================================================================================================
  147. void Texture::setTexParameter(GLenum paramName, GLint value) const
  148. {
  149. bind();
  150. glTexParameteri(target, paramName, value);
  151. }
  152. //======================================================================================================================
  153. // setTexParameter [float] =
  154. //======================================================================================================================
  155. void Texture::texParameter(GLenum paramName, GLfloat value) const
  156. {
  157. bind();
  158. glTexParameterf(target, paramName, value);
  159. }