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.3KB

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