Open Source Tomb Raider Engine
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

TextureManager.cpp 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*!
  2. * \file src/TextureManager.cpp
  3. * \brief Texture registry
  4. *
  5. * \author Mongoose
  6. * \author xythobuz
  7. */
  8. #include <string.h>
  9. #include <stdlib.h>
  10. #include <stdio.h>
  11. #include <stdarg.h>
  12. #include "global.h"
  13. #include "Console.h"
  14. #include "OpenRaider.h"
  15. #include "utils/pcx.h"
  16. #include "utils/pixel.h"
  17. #include "utils/strings.h"
  18. #include "utils/tga.h"
  19. #include "TextureManager.h"
  20. #ifdef USING_PNG
  21. #include "utils/png.h"
  22. #endif
  23. TextureManager &getTextureManager() {
  24. static TextureManager gTextureManager;
  25. return gTextureManager;
  26. }
  27. TextureManager::TextureManager() {
  28. mFlags = 0;
  29. }
  30. TextureManager::~TextureManager() {
  31. reset();
  32. }
  33. void TextureManager::reset() {
  34. while (mTextureIds.size() > 0) {
  35. unsigned int id = mTextureIds.at(mTextureIds.size() - 1);
  36. glDeleteTextures(1, &id);
  37. mTextureIds.pop_back();
  38. }
  39. }
  40. int TextureManager::initialize() {
  41. unsigned char *image = generateColorTexture(WHITE, 32, 32, 32);
  42. loadBufferSlot(image, 32, 32, RGBA, 32, mTextureIds.size());
  43. delete [] image;
  44. //! \fixme Temporary
  45. std::string filename(getOpenRaider().getPakDir());
  46. filename += "/tr2/TITLE.PCX";
  47. if (loadPCX(filename.c_str()) < 0) {
  48. //! \fixme Error Checking. Return negative error code, check in calling place too
  49. filename = getOpenRaider().getDataDir();
  50. filename += "/splash.tga";
  51. loadTGA(filename.c_str());
  52. }
  53. return (mTextureIds.size() == 0) ? -1 : 0;
  54. }
  55. void TextureManager::setFlag(TextureFlag flag) {
  56. mFlags |= flag;
  57. }
  58. void TextureManager::clearFlag(TextureFlag flag) {
  59. mFlags &= ~flag;
  60. }
  61. int TextureManager::getTextureCount() {
  62. return mTextureIds.size();
  63. }
  64. void TextureManager::disableMultiTexture() {
  65. mFlags &= ~fUseMultiTexture;
  66. #ifdef MULTITEXTURE
  67. glDisable(GL_TEXTURE_2D);
  68. glActiveTextureARB(GL_TEXTURE0_ARB);
  69. #endif
  70. }
  71. void TextureManager::useMultiTexture(float aU, float aV, float bU, float bV) {
  72. if (!(mFlags & fUseMultiTexture))
  73. return;
  74. #ifdef MULTITEXTURE
  75. glMultiTexCoord2fARB(GL_TEXTURE0_ARB, aU, aV);
  76. glMultiTexCoord2fARB(GL_TEXTURE1_ARB, bU, bV);
  77. #endif
  78. }
  79. void TextureManager::bindMultiTexture(int texture0, int texture1) {
  80. assert(texture0 >= 0);
  81. assert(texture1 >= 0);
  82. assert((unsigned int)texture0 < mTextureIds.size());
  83. assert((unsigned int)texture1 < mTextureIds.size());
  84. mFlags |= fUseMultiTexture;
  85. #ifdef MULTITEXTURE
  86. glActiveTextureARB(GL_TEXTURE0_ARB);
  87. glEnable(GL_TEXTURE_2D);
  88. glBindTexture(GL_TEXTURE_2D, mTextureIds.at(texture0));
  89. glActiveTextureARB(GL_TEXTURE1_ARB);
  90. glEnable(GL_TEXTURE_2D);
  91. glBindTexture(GL_TEXTURE_2D, mTextureIds.at(texture1));
  92. #endif
  93. }
  94. int TextureManager::loadBufferSlot(unsigned char *image,
  95. unsigned int width, unsigned int height,
  96. ColorMode mode, unsigned int bpp,
  97. unsigned int slot) {
  98. assert(image != NULL);
  99. assert(width > 0);
  100. assert(height > 0);
  101. assert((mode == GREYSCALE) || (mode == RGB)
  102. || (mode == BGR) || (mode == ARGB)
  103. || (mode == RGBA) || (mode == BGRA));
  104. assert((bpp == 8) || (bpp == 24) || (bpp == 32));
  105. while (mTextureIds.size() <= slot) {
  106. unsigned int id;
  107. glGenTextures(1, &id);
  108. mTextureIds.push_back(id);
  109. }
  110. unsigned int glcMode;
  111. switch (mode) {
  112. case GREYSCALE:
  113. glcMode = GL_LUMINANCE;
  114. break;
  115. case BGR:
  116. bgr2rgb24(image, width, height);
  117. glcMode = GL_RGB;
  118. break;
  119. case RGB:
  120. glcMode = GL_RGB;
  121. break;
  122. case ARGB:
  123. argb2rgba32(image, width, height);
  124. glcMode = GL_RGBA;
  125. break;
  126. case BGRA:
  127. bgra2rgba32(image, width, height);
  128. glcMode = GL_RGBA;
  129. break;
  130. case RGBA:
  131. glcMode = GL_RGBA;
  132. break;
  133. }
  134. glColor3ubv(WHITE);
  135. glClearColor(0.0, 0.0, 0.0, 0.0);
  136. glEnable(GL_DEPTH_TEST);
  137. glShadeModel(GL_SMOOTH);
  138. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  139. glBindTexture(GL_TEXTURE_2D, mTextureIds[slot]);
  140. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  141. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  142. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  143. //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  144. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_LINEAR);
  145. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  146. glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
  147. glTexImage2D(GL_TEXTURE_2D, 0, bpp / 8, width, height, 0, glcMode, GL_UNSIGNED_BYTE, image);
  148. //glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  149. return slot;
  150. }
  151. void TextureManager::bindTextureId(unsigned int n) {
  152. assert(n < mTextureIds.size());
  153. glEnable(GL_TEXTURE_2D);
  154. //glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  155. glBindTexture(GL_TEXTURE_2D, mTextureIds.at(n));
  156. }
  157. int TextureManager::loadImage(const char *filename) {
  158. if (stringEndsWith(filename, ".pcx") || stringEndsWith(filename, ".PCX")) {
  159. return loadPCX(filename);
  160. } else if (stringEndsWith(filename, ".png") || stringEndsWith(filename, ".PNG")) {
  161. return loadPNG(filename);
  162. } else if (stringEndsWith(filename, ".tga") || stringEndsWith(filename, ".TGA")) {
  163. return loadTGA(filename);
  164. } else {
  165. getConsole() << "No known image file type? (" << filename << ")" << Console::endl;
  166. }
  167. return -1;
  168. }
  169. int TextureManager::loadPCX(const char *filename) {
  170. assert(filename != NULL);
  171. assert(filename[0] != '\0');
  172. unsigned char *image;
  173. unsigned int w, h, bpp;
  174. ColorMode c;
  175. int id = -1;
  176. int error = pcxLoad(filename, &image, &w, &h, &c, &bpp);
  177. if (error == 0) {
  178. unsigned char *image2 = scaleBuffer(image, &w, &h, bpp);
  179. if (image2) {
  180. delete [] image;
  181. image = image2;
  182. }
  183. id = loadBufferSlot(image, w, h, c, bpp, mTextureIds.size());
  184. delete [] image;
  185. }
  186. return id;
  187. }
  188. int TextureManager::loadPNG(const char *filename) {
  189. #ifdef USING_PNG
  190. assert(filename != NULL);
  191. assert(filename[0] != '\0');
  192. if (pngCheck(filename) != 0) {
  193. return -1;
  194. }
  195. unsigned char *image;
  196. unsigned int w, h, bpp;
  197. ColorMode c;
  198. int id = -1;
  199. int error = pngLoad(filename, &image, &w, &h, &c, &bpp);
  200. if (error == 0) {
  201. unsigned char *image2 = scaleBuffer(image, &w, &h, bpp);
  202. if (image2) {
  203. delete [] image;
  204. image = image2;
  205. }
  206. id = loadBufferSlot(image, w, h, c, bpp, mTextureIds.size());
  207. delete [] image;
  208. }
  209. return id;
  210. #else
  211. getConsole() << "No PNG support available (" << filename << ")" << Console::endl;
  212. return -1;
  213. #endif
  214. }
  215. int TextureManager::loadTGA(const char *filename) {
  216. assert(filename != NULL);
  217. assert(filename[0] != '\0');
  218. unsigned char *image;
  219. unsigned int w, h;
  220. char type;
  221. int id = -1;
  222. if (!tgaCheck(filename)) {
  223. tgaLoad(filename, &image, &w, &h, &type);
  224. unsigned char *image2 = scaleBuffer(image, &w, &h, (type == 2) ? 32 : 24);
  225. if (image2) {
  226. delete [] image;
  227. image = image2;
  228. }
  229. if (image) {
  230. id = loadBufferSlot(image, w, h,
  231. (type == 2) ? RGBA : RGB,
  232. (type == 2) ? 32 : 24,
  233. mTextureIds.size());
  234. delete [] image;
  235. }
  236. }
  237. return id;
  238. }