Open Source Tomb Raider Engine
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

Texture.cpp 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. /*!
  2. * \file src/Texture.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 "utils/pcx.h"
  14. #include "utils/strings.h"
  15. #include "utils/tga.h"
  16. #include "Texture.h"
  17. Texture::Texture() {
  18. mTextureIds = NULL;
  19. mFlags = 0;
  20. mTextureId = -1;
  21. mTextureId2 = -1;
  22. mTextureCount = 0;
  23. mTextureLimit = 0;
  24. }
  25. Texture::~Texture() {
  26. reset();
  27. }
  28. unsigned char *Texture::generateColorTexture(unsigned char rgba[4],
  29. unsigned int width, unsigned int height) {
  30. unsigned char *image;
  31. unsigned int i, size;
  32. assert(rgba != NULL);
  33. assert(width > 0);
  34. assert(height > 0);
  35. image = new unsigned char[height*width*4];
  36. for (i = 0, size = width*height; i < size; ++i) {
  37. /* RBGA */
  38. image[i*4] = rgba[0];
  39. image[i*4+1] = rgba[1];
  40. image[i*4+2] = rgba[2];
  41. image[i*4+3] = rgba[3];
  42. }
  43. return image;
  44. }
  45. int Texture::loadColorTexture(unsigned char rgba[4],
  46. unsigned int width, unsigned int height) {
  47. unsigned char *image;
  48. int id;
  49. assert(rgba != NULL);
  50. assert(width > 0);
  51. assert(height > 0);
  52. image = generateColorTexture(rgba, width, height);
  53. id = loadBuffer(image, width, height, RGBA, 32);
  54. delete [] image;
  55. return id;
  56. }
  57. void Texture::setFlag(TextureFlag flag) {
  58. mFlags |= flag;
  59. }
  60. void Texture::clearFlag(TextureFlag flag) {
  61. mFlags &= ~flag;
  62. }
  63. void Texture::reset() {
  64. if (mTextureIds) {
  65. glDeleteTextures(mTextureLimit, mTextureIds);
  66. delete [] mTextureIds;
  67. }
  68. mTextureIds = NULL;
  69. mTextureCount = 0;
  70. mTextureLimit = 0;
  71. }
  72. void Texture::disableMultiTexture() {
  73. mFlags ^= fUseMultiTexture;
  74. mTextureId = -1;
  75. mTextureId2 = -1;
  76. glDisable(GL_TEXTURE_2D);
  77. glActiveTextureARB(GL_TEXTURE0_ARB);
  78. }
  79. void Texture::useMultiTexture(float aU, float aV, float bU, float bV) {
  80. if (!(mFlags & fUseMultiTexture))
  81. return;
  82. glMultiTexCoord2fARB(GL_TEXTURE0_ARB, aU, aV);
  83. glMultiTexCoord2fARB(GL_TEXTURE1_ARB, bU, bV);
  84. }
  85. void Texture::useMultiTexture(float u, float v) {
  86. if (!(mFlags & fUseMultiTexture))
  87. return;
  88. glMultiTexCoord2fARB(GL_TEXTURE0_ARB, u, v);
  89. glMultiTexCoord2fARB(GL_TEXTURE1_ARB, u, v);
  90. }
  91. void Texture::bindMultiTexture(int texture0, int texture1) {
  92. assert(mTextureIds != NULL);
  93. assert(texture0 >= 0);
  94. assert((unsigned int)texture0 <= mTextureCount);
  95. assert(texture1 >= 0);
  96. assert((unsigned int)texture1 <= mTextureCount);
  97. mFlags |= fUseMultiTexture;
  98. mTextureId = texture0;
  99. mTextureId2 = texture1;
  100. glActiveTextureARB(GL_TEXTURE0_ARB);
  101. glEnable(GL_TEXTURE_2D);
  102. glBindTexture(GL_TEXTURE_2D, mTextureIds[texture0]);
  103. glActiveTextureARB(GL_TEXTURE1_ARB);
  104. glEnable(GL_TEXTURE_2D);
  105. glBindTexture(GL_TEXTURE_2D, mTextureIds[texture1]);
  106. }
  107. void Texture::setMaxTextureCount(unsigned int n) {
  108. assert(n > 0);
  109. mTextureLimit = n;
  110. mTextureIds = new unsigned int[n];
  111. glGenTextures(n, mTextureIds);
  112. }
  113. int Texture::getTextureCount() {
  114. return mTextureCount - 1;
  115. }
  116. int Texture::loadBuffer(unsigned char *image,
  117. unsigned int width, unsigned int height,
  118. ColorMode mode, unsigned int bpp) {
  119. int id;
  120. assert(image != NULL);
  121. assert(width > 0);
  122. assert(height > 0);
  123. assert((bpp == 8) || (bpp == 24) || (bpp == 32));
  124. id = loadBufferSlot(image, width, height, mode, bpp, mTextureCount++);
  125. if (id < 0)
  126. return id;
  127. return id;
  128. }
  129. void convertARGB32bppToRGBA32bpp(unsigned char *image,
  130. unsigned int w, unsigned int h) {
  131. unsigned int i, size = w * h;
  132. assert(image != NULL);
  133. assert(w > 0);
  134. assert(h > 0);
  135. for (i = 0; i < size; ++i) {
  136. /* 32-bit ARGB to RGBA */
  137. unsigned char swap = image[(i * 4) + 3];
  138. image[(i * 4)] = image[(i * 4) + 1];
  139. image[(i * 4) + 1] = image[(i * 4) + 2];
  140. image[(i * 4) + 2] = image[(i * 4) + 3];
  141. image[(i * 4) + 3] = swap;
  142. }
  143. }
  144. int Texture::loadBufferSlot(unsigned char *image,
  145. unsigned int width, unsigned int height,
  146. ColorMode mode, unsigned int bpp,
  147. unsigned int slot) {
  148. unsigned char bytes;
  149. unsigned int glcMode;
  150. assert(mTextureIds != NULL);
  151. assert(slot < mTextureLimit);
  152. assert(image != NULL);
  153. assert(width > 0);
  154. assert(height > 0);
  155. assert((bpp == 8) || (bpp == 24) || (bpp == 32));
  156. switch (mode) {
  157. case GREYSCALE:
  158. if (bpp != 8) {
  159. printf("Texture::Load ERROR Unsupported GREYSCALE, %i bpp\n", bpp);
  160. return -1;
  161. }
  162. bytes = 1;
  163. glcMode = GL_LUMINANCE;
  164. break;
  165. case RGB:
  166. if (bpp != 24) {
  167. printf("Texture::Load ERROR Unsupported RGB, %i bpp\n", bpp);
  168. return -1;
  169. }
  170. bytes = 3;
  171. glcMode = GL_RGB;
  172. break;
  173. case ARGB:
  174. if (bpp == 32) {
  175. convertARGB32bppToRGBA32bpp(image, width, height);
  176. } else {
  177. printf("Texture::Load ERROR Unsupported ARGB, %i bpp\n", bpp);
  178. return -1;
  179. }
  180. bytes = 4;
  181. glcMode = GL_RGBA;
  182. break;
  183. case RGBA:
  184. if (bpp != 32) {
  185. printf("Texture::Load ERROR Unsupported RGBA, %i bpp\n", bpp);
  186. return -1;
  187. }
  188. bytes = 4;
  189. glcMode = GL_RGBA;
  190. break;
  191. }
  192. glClearColor(0.0, 0.0, 0.0, 0.0);
  193. glEnable(GL_DEPTH_TEST);
  194. glShadeModel(GL_SMOOTH);
  195. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  196. glBindTexture(GL_TEXTURE_2D, mTextureIds[slot]);
  197. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  198. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  199. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  200. if (mFlags & fUseMipmaps) {
  201. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
  202. GL_NEAREST_MIPMAP_LINEAR);
  203. //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  204. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
  205. GL_LINEAR_MIPMAP_LINEAR);
  206. glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
  207. glTexImage2D(GL_TEXTURE_2D, 0, bytes, width, height, 0, glcMode, GL_UNSIGNED_BYTE, image);
  208. } else {
  209. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  210. glTexImage2D(GL_TEXTURE_2D, 0, glcMode, width, height, 0,
  211. glcMode, GL_UNSIGNED_BYTE, image);
  212. }
  213. //glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  214. return slot;
  215. }
  216. void Texture::bindTextureId(unsigned int n) {
  217. assert(mTextureIds != NULL);
  218. assert(n <= mTextureCount);
  219. mTextureId = n;
  220. glEnable(GL_TEXTURE_2D);
  221. //glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  222. glBindTexture(GL_TEXTURE_2D, mTextureIds[n]);
  223. }
  224. int Texture::loadPCX(const char *filename) {
  225. unsigned char *image;
  226. unsigned int w, h;
  227. int id = -1;
  228. int error = pcxLoad(filename, &image, &w, &h);
  229. if (error == 0) {
  230. id = loadBuffer(image, w, h, RGBA, 32);
  231. delete [] image;
  232. }
  233. return id;
  234. }
  235. int Texture::loadTGA(const char *filename) {
  236. FILE *f;
  237. unsigned char *image = NULL;
  238. unsigned char *image2 = NULL;
  239. unsigned int w, h;
  240. char type;
  241. int id = -1;
  242. assert(filename != NULL);
  243. assert(filename[0] != '\0');
  244. f = fopen(filename, "rb");
  245. if (!f) {
  246. perror("Couldn't load file");
  247. } else if (!tgaCheck(f)) {
  248. tgaLoad(f, &image, &w, &h, &type);
  249. type += 2;
  250. image2 = scaleBuffer(image, w, h, (type == 4) ? 4 : 3);
  251. if (image2) {
  252. image = image2;
  253. w = h = 256;
  254. }
  255. if (image) {
  256. id = loadBuffer(image, w, h,
  257. (type == 4) ? RGBA : RGB,
  258. (type == 4) ? 32 : 24);
  259. delete [] image;
  260. }
  261. fclose(f);
  262. }
  263. if (id == -1) {
  264. printf("Texture::loadTGA> ERROR: Failed to load '%s'\n", filename);
  265. }
  266. return id;
  267. }
  268. int Texture::nextPower(int seed) {
  269. int i = 1;
  270. for (; i < seed; i *= 2);
  271. return i;
  272. }
  273. /* This code based off on gluScaleImage() */
  274. unsigned char *Texture::scaleBuffer(unsigned char *image,
  275. int width, int height, int components) {
  276. int i, j, k;
  277. float* tempin;
  278. float* tempout;
  279. float sx, sy;
  280. //int components = 3;
  281. unsigned char *timage;
  282. int original_height = height;
  283. int original_width = width;
  284. assert(image != NULL);
  285. assert(width > 0);
  286. assert(height > 0);
  287. height = nextPower(height);
  288. width = nextPower(width);
  289. if (height > 256)
  290. height = 256;
  291. if (width > 256)
  292. width = 256;
  293. // Check to see if scaling is needed
  294. if (height == original_height && width == original_width)
  295. return NULL;
  296. //printf("%i\n", components);
  297. timage = new unsigned char[height * width * components];
  298. tempin = new float[original_width * original_height * components * sizeof(float)];
  299. tempout = new float[width * height * components * sizeof(float)];
  300. // Copy user data to float format.
  301. for (i = 0; i < original_height * original_width * components; ++i) {
  302. tempin[i] = (float)image[i];
  303. }
  304. // Determine which filter to use by checking ratios.
  305. if (width > 1) {
  306. sx = (float)(original_width - 1) / (float)(width - 1);
  307. } else {
  308. sx = (float)(original_width - 1);
  309. }
  310. if (height > 1) {
  311. sy = (float)(original_height - 1) / (float) (height - 1);
  312. } else {
  313. sy = (float)(original_height - 1);
  314. }
  315. if (sx < 1.0 && sy < 1.0) {
  316. /* Magnify both width and height: use weighted sample of 4 pixels */
  317. for (i = 0; i < height; ++i) {
  318. int i0 = (int)(i * sy);
  319. int i1 = i0 + 1;
  320. if (i1 >= original_height) {
  321. i1 = original_height - 1;
  322. }
  323. float alpha = i * sy - i0;
  324. for (j = 0; j < width; ++j) {
  325. int j0 = (int) (j * sx);
  326. int j1 = j0 + 1;
  327. if (j1 >= original_width) {
  328. j1 = original_width - 1;
  329. }
  330. float beta = j * sx - j0;
  331. /* Compute weighted average of pixels in rect (i0,j0)-(i1,j1) */
  332. float *src00 = tempin + (i0 * original_width + j0) * components;
  333. float *src01 = tempin + (i0 * original_width + j1) * components;
  334. float *src10 = tempin + (i1 * original_width + j0) * components;
  335. float *src11 = tempin + (i1 * original_width + j1) * components;
  336. float *dst = tempout + (i * width + j) * components;
  337. for (k = 0; k < components; ++k) {
  338. float s1 = *src00++ * (1.0f - beta) + *src01++ * beta;
  339. float s2 = *src10++ * (1.0f - beta) + *src11++ * beta;
  340. *dst++ = s1 * (1.0f - alpha) + s2 * alpha;
  341. }
  342. }
  343. }
  344. } else {
  345. /* Shrink width and/or height: use an unweighted box filter */
  346. for (i = 0; i < height; ++i) {
  347. int i0 = (int) (i * sy);
  348. int i1 = i0 + 1;
  349. if (i1 >= original_height) {
  350. i1 = original_height - 1;
  351. }
  352. for (j = 0; j < width; ++j) {
  353. int j0 = (int) (j * sx);
  354. int j1 = j0 + 1;
  355. if (j1 >= original_width) {
  356. j1 = original_width - 1;
  357. }
  358. float *dst = tempout + (i * width + j) * components;
  359. /* Compute average of pixels in the rectangle (i0,j0)-(i1,j1) */
  360. for (k = 0; k < components; ++k) {
  361. float sum = 0.0;
  362. for (int ii = i0; ii <= i1; ++ii) {
  363. for (int jj = j0; jj <= j1; ++jj) {
  364. sum += *(tempin + (ii * original_width + jj)
  365. * components + k);
  366. }
  367. }
  368. sum /= ( j1 - j0 + 1 ) * ( i1 - i0 + 1 );
  369. *dst++ = sum;
  370. }
  371. }
  372. }
  373. }
  374. // Copy to our results.
  375. for (i = 0; i < height * width * components; ++i) {
  376. timage[i] = (unsigned char)tempout[i];
  377. }
  378. // Delete our temp buffers.
  379. delete[] tempin;
  380. delete[] tempout;
  381. delete[] image;
  382. return timage;
  383. }