Texture.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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_TEXTURE),
  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(isLoaded())
  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[0]);
  61. if(mipmappingEnabled)
  62. {
  63. glGenerateMipmap(target);
  64. }
  65. return true;
  66. }
  67. //======================================================================================================================
  68. // createEmpty2D =
  69. //======================================================================================================================
  70. bool Texture::createEmpty2D(float width_, float height_, int internalFormat, int format_, GLenum type_, bool mimapping)
  71. {
  72. target = GL_TEXTURE_2D;
  73. DEBUG_ERR(internalFormat>0 && internalFormat<=4); // deprecated internal format
  74. DEBUG_ERR(isLoaded());
  75. // GL stuff
  76. glGenTextures(1, &glId);
  77. bind();
  78. if(mimapping)
  79. setTexParameter(GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  80. else
  81. setTexParameter(GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  82. setTexParameter(GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  83. setRepeat(true);
  84. // allocate to vram
  85. glTexImage2D(target, 0, internalFormat, width_, height_, 0, format_, type_, NULL);
  86. if(mimapping)
  87. glGenerateMipmap(target);
  88. return GL_OK();
  89. }
  90. //======================================================================================================================
  91. // createEmpty2DMSAA =
  92. //======================================================================================================================
  93. bool Texture::createEmpty2DMsaa(int samplesNum, int internalFormat, int width_, int height_, bool mimapping)
  94. {
  95. target = GL_TEXTURE_2D_MULTISAMPLE;
  96. DEBUG_ERR(isLoaded());
  97. glGenTextures(1, &glId);
  98. bind();
  99. // allocate
  100. glTexImage2DMultisample(target, samplesNum, internalFormat, width_, height_, false);
  101. if(mimapping)
  102. glGenerateMipmap(target);
  103. return GL_OK();
  104. }
  105. //======================================================================================================================
  106. // unload =
  107. //======================================================================================================================
  108. void Texture::unload()
  109. {
  110. if(isLoaded())
  111. glDeleteTextures(1, &glId);
  112. }
  113. //======================================================================================================================
  114. // bind =
  115. //======================================================================================================================
  116. void Texture::bind(uint unit) const
  117. {
  118. if(unit >= static_cast<uint>(textureUnitsNum))
  119. WARNING("Max tex units passed");
  120. glActiveTexture(GL_TEXTURE0+unit);
  121. glBindTexture(target, getGlId());
  122. }
  123. //======================================================================================================================
  124. // getWidth =
  125. //======================================================================================================================
  126. int Texture::getWidth() const
  127. {
  128. bind();
  129. int i;
  130. glGetTexLevelParameteriv(target, 0, GL_TEXTURE_WIDTH, &i);
  131. return i;
  132. }
  133. //======================================================================================================================
  134. // getHeight =
  135. //======================================================================================================================
  136. int Texture::getHeight() const
  137. {
  138. bind();
  139. int i;
  140. glGetTexLevelParameteriv(target, 0, GL_TEXTURE_HEIGHT, &i);
  141. return i;
  142. }
  143. //======================================================================================================================
  144. // setTexParameter [int] =
  145. //======================================================================================================================
  146. void Texture::setTexParameter(GLenum paramName, GLint value) const
  147. {
  148. bind();
  149. glTexParameteri(target, paramName, value);
  150. }
  151. //======================================================================================================================
  152. // setTexParameter [float] =
  153. //======================================================================================================================
  154. void Texture::texParameter(GLenum paramName, GLfloat value) const
  155. {
  156. bind();
  157. glTexParameterf(target, paramName, value);
  158. }
  159. //======================================================================================================================
  160. // setRepeat =
  161. //======================================================================================================================
  162. void Texture::setRepeat(bool repeat) const
  163. {
  164. bind();
  165. if(repeat)
  166. {
  167. setTexParameter(GL_TEXTURE_WRAP_S, GL_REPEAT);
  168. setTexParameter(GL_TEXTURE_WRAP_T, GL_REPEAT);
  169. }
  170. else
  171. {
  172. setTexParameter(GL_TEXTURE_WRAP_S, GL_CLAMP);
  173. setTexParameter(GL_TEXTURE_WRAP_T, GL_CLAMP);
  174. }
  175. }