Open Source Tomb Raider Engine
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093
  1. /*!
  2. * \file src/Texture.cpp
  3. * \brief Texture registry
  4. *
  5. * \author Mongoose
  6. */
  7. #include <string.h>
  8. #include <stdlib.h>
  9. #include <stdio.h>
  10. #include <stdarg.h>
  11. #include "SDL_ttf.h"
  12. #ifdef __APPLE__
  13. #include <OpenGL/gl.h>
  14. #include <OpenGL/glu.h>
  15. #else
  16. #include <GL/gl.h>
  17. #include <GL/glu.h>
  18. #endif
  19. #include "utils/tga.h"
  20. #include "Texture.h"
  21. //Texture *gTextureManager = 0x0;
  22. gl_font_t *gFontTest = 0x0;
  23. ////////////////////////////////////////////////////////////
  24. // Constructors
  25. ////////////////////////////////////////////////////////////
  26. Texture::Texture()
  27. {
  28. mTextureIds = NULL;
  29. mFlags = 0;
  30. mTextureId = -1;
  31. mTextureId2 = -1;
  32. mTextureCount = 0;
  33. mTextureLimit = 0;
  34. //gTextureManager = this;
  35. initSDL_TTF();
  36. }
  37. Texture::~Texture()
  38. {
  39. if (gFontTest)
  40. {
  41. glDeleteLists(gFontTest->drawListBase, gFontTest->count);
  42. delete gFontTest;
  43. }
  44. reset();
  45. }
  46. ////////////////////////////////////////////////////////////
  47. // Public Accessors
  48. ////////////////////////////////////////////////////////////
  49. unsigned char *Texture::generateColorTexture(unsigned char rgba[4],
  50. unsigned int width,
  51. unsigned int height)
  52. {
  53. unsigned char *image;
  54. unsigned int i, size;
  55. image = new unsigned char[height*width*4];
  56. for (i = 0, size = width*height; i < size; ++i)
  57. {
  58. /* RBGA */
  59. image[i*4] = rgba[0];
  60. image[i*4+1] = rgba[1];
  61. image[i*4+2] = rgba[2];
  62. image[i*4+3] = rgba[3];
  63. }
  64. return image;
  65. }
  66. void glEnterMode2d(unsigned int width, unsigned int height)
  67. {
  68. glPushAttrib(GL_ENABLE_BIT);
  69. glDisable(GL_DEPTH_TEST);
  70. glDisable(GL_CULL_FACE);
  71. glEnable(GL_TEXTURE_2D);
  72. /* This allows alpha blending of 2D textures with the scene */
  73. glEnable(GL_BLEND);
  74. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  75. glViewport(0, 0, width, height);
  76. glMatrixMode(GL_PROJECTION);
  77. glPushMatrix();
  78. glLoadIdentity();
  79. glOrtho(0.0, (GLdouble)width, (GLdouble)height, 0.0, 0.0, 1.0);
  80. glMatrixMode(GL_MODELVIEW);
  81. glPushMatrix();
  82. glLoadIdentity();
  83. glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  84. }
  85. void glExitMode2d()
  86. {
  87. glMatrixMode(GL_MODELVIEW);
  88. glPopMatrix();
  89. glMatrixMode(GL_PROJECTION);
  90. glPopMatrix();
  91. glPopAttrib();
  92. glMatrixMode(GL_MODELVIEW);
  93. }
  94. void bufferedPrintf(char *string, unsigned int len, char *s, ...)
  95. {
  96. va_list args;
  97. if (s && s[0])
  98. {
  99. va_start(args, s);
  100. vsnprintf(string, len-1, s, args);
  101. string[len-1] = 0;
  102. va_end(args);
  103. }
  104. }
  105. void glPrint2d(float x, float y, float scale, char *string)
  106. {
  107. gl_font_t *font = gFontTest;
  108. if (!font)
  109. {
  110. static int errors = 0;
  111. if (errors < 10)
  112. printf("ERROR: glPrint2d failed, %i\n", ++errors);
  113. return;
  114. }
  115. glPushMatrix();
  116. glBindTexture(GL_TEXTURE_2D, font->textureId);
  117. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  118. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  119. glTranslatef(x, y, 0);
  120. glScalef(scale, scale, 1);
  121. /*! \fixme
  122. * Add utf-8 dencoding of char* string
  123. *
  124. * Also this string must be preprocessed to have glyph offsets
  125. * instead of ASCII text in it and support counts over 256 */
  126. glListBase(font->drawListBase - font->utf8Offset);
  127. glCallLists(strlen(string), GL_BYTE, string);
  128. glPopMatrix();
  129. }
  130. void glPrint3d(float x, float y, float z,
  131. float pitch, float yaw, float roll,
  132. float scale,
  133. char *string)
  134. {
  135. gl_font_t *font = gFontTest;
  136. if (!font)
  137. {
  138. static int errors = 0;
  139. if (errors < 10)
  140. printf("ERROR: glPrint3d failed, %i\n", ++errors);
  141. return;
  142. }
  143. glPushMatrix();
  144. glBindTexture(GL_TEXTURE_2D, font->textureId);
  145. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  146. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  147. glTranslatef(x, y, z);
  148. glRotatef(roll, 1, 0, 0);
  149. glRotatef(yaw, 0, 1, 0);
  150. glRotatef(pitch, 0, 0, 1);
  151. glScalef(scale, scale, scale);
  152. /*! \fixme
  153. * Add utf-8 dencoding of char* string
  154. *
  155. * Also this string must be preprocessed to have glyph offsets
  156. * instead of ASCII text in it and support counts over 256 */
  157. glListBase(font->drawListBase - font->utf8Offset);
  158. glCallLists(strlen(string), GL_BYTE, string);
  159. glPopMatrix();
  160. }
  161. int Texture::loadFontTTF(const char *filename,
  162. unsigned int utf8Offset, unsigned int count)
  163. {
  164. ttf_texture_t *texture;
  165. unsigned char rgb[3] = {0xff, 0xff, 0xff};
  166. if (!filename || !filename[0])
  167. {
  168. printf("fontTest> Passed bad filename\n");
  169. return -1;
  170. }
  171. texture = generateFontTexture(filename, 24, 256, rgb,
  172. //0x303f, 0x3093-0x303f, // hiragana
  173. //32, 126 - 32, // english
  174. utf8Offset, count,
  175. false);
  176. if (texture)
  177. {
  178. #ifdef DUMP_TTF_TGA
  179. tgaSaveFilename(texture->texture, 256, 256, 4, "ttf_font.tga");
  180. #endif
  181. gFontTest = generateFont(texture);
  182. /*! \fixme Until UTF8 decoder is working, we map from
  183. ASCII when rendering */
  184. gFontTest->utf8Offset = 32; // hack to use ASCII strings to test unicode
  185. delete [] texture->texture;
  186. delete [] texture->glyphs;
  187. delete texture;
  188. return gFontTest->textureId;
  189. }
  190. else
  191. {
  192. return -2;
  193. }
  194. }
  195. gl_font_t *Texture::generateFont(ttf_texture_t *texture)
  196. {
  197. const float spacing = 4.0;
  198. unsigned int i;
  199. float u, v, u2, v2;
  200. int h;
  201. gl_font_t *font;
  202. if (!texture)
  203. return NULL;
  204. printf("Generating gl font from texture...\n");
  205. font = new gl_font_t;
  206. font->utf8Offset = texture->utf8Offset;
  207. font->count = texture->count;
  208. font->textureId = loadBuffer(texture->texture,
  209. texture->width, texture->width, RGBA, 32);
  210. glColor3f(1.0, 1.0, 1.0);
  211. font->drawListBase = glGenLists(texture->count);
  212. glBindTexture(GL_TEXTURE_2D, font->textureId);
  213. for (i = 0; i < texture->count; i++)
  214. {
  215. /* Generate texture coordinates for this TTF glyph */
  216. u = (float)texture->glyphs[i].x / (float)texture->width;
  217. v = (float)texture->glyphs[i].y / (float)texture->width;
  218. u2 = (((float)texture->glyphs[i].x + (float)texture->glyphs[i].w) /
  219. (float)texture->width);
  220. v2 = (((float)texture->glyphs[i].y + (float)texture->glyphs[i].h) /
  221. (float)texture->width);
  222. #ifdef DEBUG_TTF_OFFSET
  223. if (i+texture->utf8Offset == 'y' || i+texture->utf8Offset == 'x')
  224. {
  225. printf("%c: %i %i %i\n",
  226. i+texture->utf8Offset,
  227. texture->fontDescent,
  228. texture->glyphs[i].miny, texture->glyphs[i].maxy);
  229. }
  230. #endif
  231. if (texture->glyphs[i].h < texture->fontHeight)
  232. {
  233. h = texture->fontHeight - texture->glyphs[i].h;
  234. }
  235. else
  236. {
  237. h = 0;
  238. }
  239. // After migrating to SDL2_TTF, some characters where rendered slightly
  240. // vertically offset. However, dumping the font texture as TGA did not show
  241. // this offset. It turns out, this fixes the issue...? O.o --xythobuz
  242. // h += -texture->fontHeight/2-(texture->fontDescent + texture->glyphs[i].miny);
  243. h -= texture->fontHeight/2 + texture->fontDescent;
  244. /* Make a list for this TTF glyph, one nonuniform Quad per glyph */
  245. glNewList(font->drawListBase + i, GL_COMPILE);
  246. glBegin(GL_QUADS);
  247. glTexCoord2f(u2, v); /* Top, right */
  248. glVertex3i(texture->glyphs[i].w, h, 0);
  249. glTexCoord2f(u, v); /* Top, left */
  250. glVertex3i(0, h, 0);
  251. glTexCoord2f(u, v2); /* Bottom, left */
  252. glVertex3i(0, h+texture->glyphs[i].h, 0);
  253. glTexCoord2f(u2, v2); /* Bottom, right */
  254. glVertex3i(texture->glyphs[i].w, h+texture->glyphs[i].h, 0);
  255. glEnd();
  256. /* Move To The Left Of The Character */
  257. glTranslated(texture->glyphs[i].w + spacing, 0, 0);
  258. glEndList();
  259. }
  260. return font;
  261. }
  262. ttf_texture_t *Texture::generateFontTexture(const char *filename, int pointSize,
  263. unsigned int textureWidth,
  264. unsigned char color[3],
  265. unsigned int utf8Offset,
  266. unsigned int count,
  267. char verbose)
  268. {
  269. unsigned int i;
  270. int xx = 0, yy = 0, hh = 0, k, h, w, offset;
  271. unsigned char b;
  272. unsigned char *image;
  273. SDL_Surface* glyph;
  274. SDL_Color sdlColor;
  275. TTF_Font *font;
  276. ttf_texture_t *texture;
  277. FILE *f;
  278. sdlColor.r = color[0];
  279. sdlColor.g = color[1];
  280. sdlColor.b = color[2];
  281. if (!(mFlags & fUseSDL_TTF))
  282. {
  283. printf("SDL_TTF couldn't be used... exiting\n");
  284. return NULL;
  285. }
  286. if (pointSize < 8)
  287. {
  288. pointSize = 8;
  289. }
  290. /* Work around for TTF_OpenFont for file not found segfault */
  291. f = fopen(filename, "rb");
  292. if (!f)
  293. {
  294. printf("generateFontTexture> Couldn't load '%s'\n", filename);
  295. perror(filename);
  296. return NULL;
  297. }
  298. fclose(f);
  299. // Open the font file at the requested point size
  300. font = TTF_OpenFont(filename, pointSize);
  301. if (font == NULL)
  302. {
  303. fprintf(stderr, "generateFontTexture> Couldn't load %d pt font from %s: %s\n",
  304. pointSize, filename, SDL_GetError());
  305. return NULL;
  306. }
  307. // TTF_STYLE_NORMAL, TTF_STYLE_BOLD, TTF_STYLE_ITALIC, TTF_STYLE_UNDERLINE
  308. int renderStyle = TTF_STYLE_NORMAL;
  309. TTF_SetFontStyle(font, renderStyle);
  310. TTF_SetFontHinting(font, TTF_HINTING_LIGHT);
  311. /* Allocate a new TTF font texture */
  312. printf("Creating font texture from '%s'...\n", filename);
  313. texture = new ttf_texture_t;
  314. texture->width = textureWidth;
  315. texture->utf8Offset = utf8Offset;
  316. texture->count = count;
  317. texture->glyphs = new ttf_glyph_t[count];
  318. texture->texture = new unsigned char[textureWidth*textureWidth*4];
  319. memset(texture->texture, 0, textureWidth*textureWidth*4);
  320. texture->fontHeight = TTF_FontHeight(font);
  321. texture->fontAscent = TTF_FontAscent(font);
  322. texture->fontDescent = TTF_FontDescent(font);
  323. texture->fontSpacing = TTF_FontLineSkip(font);
  324. for (i = 0; i < count; ++i)
  325. {
  326. glyph = TTF_RenderGlyph_Blended(font, (Uint16)(i + utf8Offset), sdlColor);
  327. if (glyph)
  328. {
  329. image = (unsigned char*)glyph->pixels;
  330. TTF_GlyphMetrics(font, (Uint16)(i + utf8Offset),
  331. &texture->glyphs[i].minx, &texture->glyphs[i].maxx,
  332. &texture->glyphs[i].miny, &texture->glyphs[i].maxy,
  333. &texture->glyphs[i].advance);
  334. texture->glyphs[i].w = glyph->w;
  335. texture->glyphs[i].h = glyph->h;
  336. if (xx + texture->glyphs[i].w > ((int)textureWidth - 1))
  337. {
  338. yy += hh;
  339. hh = 2;
  340. xx = 2;
  341. texture->glyphs[i].x = 0;
  342. texture->glyphs[i].y = yy;
  343. }
  344. else
  345. {
  346. (texture->glyphs[i].h > hh) ? hh = texture->glyphs[i].h: 0;
  347. texture->glyphs[i].x = xx;
  348. texture->glyphs[i].y = yy;
  349. }
  350. xx += glyph->w;
  351. if (verbose)
  352. {
  353. printf("0x%x : %ix%i @ %i, %i\n", i + utf8Offset,
  354. texture->glyphs[i].w, texture->glyphs[i].h,
  355. texture->glyphs[i].x, texture->glyphs[i].y);
  356. }
  357. /* Blit @ xx, yy - in pixels */
  358. for (k = 0; k < glyph->w*glyph->h; ++k)
  359. {
  360. w = texture->glyphs[i].x + k%glyph->w;
  361. h = texture->glyphs[i].y + k/glyph->w;
  362. offset = (w + h*textureWidth);
  363. if (verbose)
  364. {
  365. printf("Offset: %i; Pixel: %i,%i; Data: 0x%08x\n",
  366. offset, w, h, *((unsigned int *)(void *)&image[k*4]));
  367. }
  368. /* 32-bit ARGB to RGBA */
  369. b = image[k*4+3];
  370. texture->texture[offset*4] = image[k*4] = image[k*4+1];
  371. texture->texture[offset*4+1] = image[k*4+1] = image[k*4+2];
  372. texture->texture[offset*4+2] = image[k*4+2] = image[k*4+3];
  373. texture->texture[offset*4+3] = image[k*4+3] = b;
  374. }
  375. }
  376. }
  377. TTF_CloseFont(font);
  378. return texture;
  379. }
  380. ////////////////////////////////////////////////////////////
  381. // Public Mutators
  382. ////////////////////////////////////////////////////////////
  383. int Texture::loadColorTexture(unsigned char rgba[4],
  384. unsigned int width, unsigned int height)
  385. {
  386. unsigned char *image;
  387. int id;
  388. image = generateColorTexture(rgba, width, height);
  389. id = loadBuffer(image, width, height, RGBA, 32);
  390. delete [] image;
  391. return id;
  392. }
  393. void Texture::initSDL_TTF()
  394. {
  395. // Initialize the TTF library
  396. if (TTF_Init() < 0)
  397. {
  398. fprintf(stderr, "initSDL_TTF> TTF_Init() failed!\n");
  399. fprintf(stderr, "initSDL_TTF> Error is [%s].\n", SDL_GetError());
  400. }
  401. else
  402. {
  403. mFlags |= fUseSDL_TTF;
  404. printf("Started SDL_TTF subsystem!\n");
  405. atexit(TTF_Quit);
  406. }
  407. }
  408. void Texture::setFlag(TextureFlag flag)
  409. {
  410. mFlags |= flag;
  411. }
  412. void Texture::clearFlag(TextureFlag flag)
  413. {
  414. mFlags |= flag;
  415. mFlags ^= flag;
  416. }
  417. void Texture::reset()
  418. {
  419. if (mTextureIds)
  420. {
  421. glDeleteTextures(mTextureLimit, mTextureIds);
  422. delete [] mTextureIds;
  423. }
  424. mTextureIds = 0x0;
  425. mTextureCount = 0;
  426. mTextureLimit = 0;
  427. }
  428. void Texture::disableMultiTexture()
  429. {
  430. mFlags ^= fUseMultiTexture;
  431. mTextureId = -1;
  432. mTextureId2 = -1;
  433. glDisable(GL_TEXTURE_2D);
  434. glActiveTextureARB(GL_TEXTURE0_ARB);
  435. }
  436. void Texture::useMultiTexture(float aU, float aV, float bU, float bV)
  437. {
  438. if (!(mFlags & fUseMultiTexture))
  439. return;
  440. glMultiTexCoord2fARB(GL_TEXTURE0_ARB, aU, aV);
  441. glMultiTexCoord2fARB(GL_TEXTURE1_ARB, bU, bV);
  442. }
  443. void Texture::useMultiTexture(float u, float v)
  444. {
  445. if (!(mFlags & fUseMultiTexture))
  446. return;
  447. glMultiTexCoord2fARB(GL_TEXTURE0_ARB, u, v);
  448. glMultiTexCoord2fARB(GL_TEXTURE1_ARB, u, v);
  449. }
  450. void Texture::bindMultiTexture(int texture0, int texture1)
  451. {
  452. if (//(int)a == mTextureId && (int)b == mTextureId2 ||
  453. !mTextureIds ||
  454. texture0 < 0 || texture0 > (int)mTextureCount ||
  455. texture1 < 0 || texture1 > (int)mTextureCount)
  456. {
  457. return;
  458. }
  459. mFlags |= fUseMultiTexture;
  460. mTextureId = texture0;
  461. mTextureId2 = texture1;
  462. glActiveTextureARB(GL_TEXTURE0_ARB);
  463. glEnable(GL_TEXTURE_2D);
  464. glBindTexture(GL_TEXTURE_2D, mTextureIds[texture0]);
  465. glActiveTextureARB(GL_TEXTURE1_ARB);
  466. glEnable(GL_TEXTURE_2D);
  467. glBindTexture(GL_TEXTURE_2D, mTextureIds[texture1]);
  468. }
  469. void Texture::setMaxTextureCount(unsigned int n)
  470. {
  471. mTextureLimit = n;
  472. mTextureIds = new unsigned int[n];
  473. glGenTextures(n, mTextureIds);
  474. }
  475. int Texture::getTextureCount()
  476. {
  477. return (mTextureCount-1);
  478. }
  479. int Texture::loadBuffer(unsigned char *image,
  480. unsigned int width, unsigned int height,
  481. ColorMode mode, unsigned int bpp)
  482. {
  483. int id;
  484. id = loadBufferSlot(image, width, height, mode, bpp, mTextureCount++);
  485. if (id < 0)
  486. {
  487. return id;
  488. }
  489. return ++id;
  490. }
  491. void convertARGB32bppToRGBA32bpp(unsigned char *image,
  492. unsigned int w, unsigned int h)
  493. {
  494. unsigned int i, size = w*h;
  495. unsigned char swap;
  496. for (i = 0; i < size; ++i)
  497. {
  498. /* 32-bit ARGB to RGBA */
  499. swap = image[i*4+3];
  500. image[i*4] = image[i*4+1];
  501. image[i*4+1] = image[i*4+2];
  502. image[i*4+2] = image[i*4+3];
  503. image[i*4+3] = swap;
  504. }
  505. }
  506. GLint deprecated_gluBuild2DMipmaps(GLenum target, GLint internalFormat, GLsizei width, GLsizei height, GLenum format, GLenum type, const void *data);
  507. int Texture::loadBufferSlot(unsigned char *image,
  508. unsigned int width, unsigned int height,
  509. ColorMode mode, unsigned int bpp,
  510. unsigned int slot)
  511. {
  512. unsigned char bytes;
  513. unsigned int glcMode;
  514. if (!mTextureIds || slot >= mTextureLimit)
  515. {
  516. printf("Texture::Load> ERROR Not initialized or out of free slots\n");
  517. return -1000;
  518. }
  519. if (!width || !height || !image)
  520. {
  521. printf("Texture::Load> ERROR Assertion 'image is valid' failed\n");
  522. return -1;
  523. }
  524. switch (mode)
  525. {
  526. case GREYSCALE:
  527. if (bpp != 8)
  528. {
  529. printf("Texture::Load> ERROR Unsupported GREYSCALE, %i bpp\n", bpp);
  530. return -2;
  531. }
  532. bytes = 1;
  533. glcMode = GL_LUMINANCE;
  534. break;
  535. case RGB:
  536. if (bpp != 24)
  537. {
  538. printf("Texture::Load> ERROR Unsupported RGB, %i bpp\n", bpp);
  539. return -2;
  540. }
  541. bytes = 3;
  542. glcMode = GL_RGB;
  543. break;
  544. case ARGB:
  545. if (bpp == 32)
  546. {
  547. convertARGB32bppToRGBA32bpp(image, width, height);
  548. }
  549. else
  550. {
  551. printf("Texture::Load> ERROR Unsupported ARGB, %i bpp\n", bpp);
  552. return -2;
  553. }
  554. bytes = 4;
  555. glcMode = GL_RGBA;
  556. break;
  557. case RGBA:
  558. if (bpp != 32)
  559. {
  560. printf("Texture::Load> ERROR Unsupported RGBA, %i bpp\n", bpp);
  561. return -2;
  562. }
  563. bytes = 4;
  564. glcMode = GL_RGBA;
  565. break;
  566. }
  567. glClearColor(0.0, 0.0, 0.0, 0.0);
  568. glEnable(GL_DEPTH_TEST);
  569. glShadeModel(GL_SMOOTH);
  570. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  571. glBindTexture(GL_TEXTURE_2D, mTextureIds[slot]);
  572. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  573. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  574. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  575. if (mFlags & fUseMipmaps)
  576. {
  577. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
  578. GL_NEAREST_MIPMAP_LINEAR);
  579. //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  580. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
  581. GL_LINEAR_MIPMAP_LINEAR);
  582. //gluBuild2DMipmaps(GL_TEXTURE_2D, bytes, width, height, glcMode, GL_UNSIGNED_BYTE, image);
  583. // gluBuild2DMipmaps is deprecated. Replacement by xythobuz
  584. deprecated_gluBuild2DMipmaps(GL_TEXTURE_2D, bytes, width, height, glcMode, GL_UNSIGNED_BYTE, image);
  585. }
  586. else
  587. {
  588. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  589. glTexImage2D(GL_TEXTURE_2D, 0, glcMode, width, height, 0,
  590. glcMode, GL_UNSIGNED_BYTE, image);
  591. }
  592. //glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  593. return slot;
  594. }
  595. // http://mmmovania.blogspot.de/2011/01/opengl-30-and-above-deprecated-func-and.html
  596. // http://www.g-truc.net/post-0256.html
  597. GLint deprecated_gluBuild2DMipmaps(GLenum target, GLint internalFormat, GLsizei width, GLsizei height, GLenum format, GLenum type, const void *data) {
  598. glTexParameteri(target, GL_GENERATE_MIPMAP, GL_TRUE);
  599. glTexImage2D(target, 0, internalFormat, width, height, 0, format, type, data);
  600. return 0;
  601. }
  602. void Texture::bindTextureId(unsigned int n)
  603. {
  604. if ((int)n == mTextureId || !mTextureIds || n > mTextureCount)
  605. {
  606. return;
  607. }
  608. mTextureId = n;
  609. glEnable(GL_TEXTURE_2D);
  610. //glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  611. glBindTexture(GL_TEXTURE_2D, mTextureIds[n]);
  612. }
  613. void Texture::glScreenShot(char *base, unsigned int width, unsigned int height) {
  614. FILE *f;
  615. int sz = width * height;
  616. unsigned char *image = new unsigned char[sz * 3];
  617. char filename[1024];
  618. static int count = 0;
  619. bool done = false;
  620. if (!image || !width || !height) {
  621. if (image)
  622. delete [] image;
  623. printf("glScreenShot> ERROR: Couldn't allocate image!\n");
  624. return;
  625. }
  626. // Don't overwrite files
  627. while (!done)
  628. {
  629. snprintf(filename, 1024, "%s-%04i.tga", base, count++);
  630. f = fopen(filename, "rb");
  631. if (f)
  632. fclose(f);
  633. else
  634. done = true;
  635. }
  636. // Capture frame buffer
  637. glReadPixels(0, 0, width, height, GL_BGR, GL_UNSIGNED_BYTE, image);
  638. tgaSaveFilename(image, width, height, 0, "%s", filename);
  639. printf("Took screenshot '%s'.\n", filename);
  640. delete [] image;
  641. }
  642. int Texture::loadTGA(const char *filename)
  643. {
  644. FILE *f;
  645. unsigned char *image = NULL;
  646. unsigned char *image2 = NULL;
  647. unsigned int w, h;
  648. char type;
  649. int id = -1;
  650. f = fopen(filename, "rb");
  651. if (!f)
  652. {
  653. perror("Couldn't load file");
  654. }
  655. else if (!tgaCheck(f))
  656. {
  657. tgaLoad(f, &image, &w, &h, &type);
  658. type += 2;
  659. image2 = scaleBuffer(image, w, h, (type == 4) ? 4 : 3);
  660. if (image2)
  661. {
  662. image = image2;
  663. w = h = 256;
  664. }
  665. if (image)
  666. {
  667. id = loadBuffer(image, w, h,
  668. (type == 4) ? RGBA : RGB,
  669. (type == 4) ? 32 : 24);
  670. delete [] image;
  671. }
  672. fclose(f);
  673. }
  674. if (id == -1)
  675. {
  676. printf("Texture::loadTGA> ERROR: Failed to load '%s'\n", filename);
  677. }
  678. return id;
  679. }
  680. ////////////////////////////////////////////////////////////
  681. // Private Accessors
  682. ////////////////////////////////////////////////////////////
  683. int Texture::nextPower(int seed)
  684. {
  685. int i;
  686. for(i = 1; i < seed; i *= 2)
  687. ;
  688. return i;
  689. }
  690. /* This code based off on gluScaleImage() */
  691. unsigned char *Texture::scaleBuffer(unsigned char *image,
  692. int width, int height,
  693. int components)
  694. {
  695. int i, j, k;
  696. float* tempin;
  697. float* tempout;
  698. float sx, sy;
  699. // int components = 3;
  700. unsigned char *timage;
  701. int original_height = height;
  702. int original_width = width;
  703. if (!image || !width || !height)
  704. {
  705. return NULL;
  706. }
  707. height = nextPower(height);
  708. width = nextPower(width);
  709. if (height > 256)
  710. height = 256;
  711. if (width > 256)
  712. width = 256;
  713. // Check to see if scaling is needed
  714. if (height == original_height && width == original_width)
  715. return NULL;
  716. //printf("%i\n", components);
  717. timage = new unsigned char[height * width * components];
  718. tempin = new float[original_width * original_height * components * sizeof(float)];
  719. tempout = new float[width * height * components * sizeof(float)];
  720. if (!tempout || !tempin || !timage)
  721. {
  722. if (tempout)
  723. delete [] tempout;
  724. if (tempin)
  725. delete [] tempin;
  726. if (timage)
  727. delete [] timage;
  728. printf("Oh shit out of memory!\n");
  729. return NULL;
  730. }
  731. // Copy user data to float format.
  732. for (i = 0; i < original_height * original_width * components; ++i)
  733. {
  734. tempin[i] = (float)image[i];
  735. }
  736. // Determine which filter to use by checking ratios.
  737. if (width > 1)
  738. {
  739. sx = (float)(original_width - 1) / (float)(width - 1);
  740. }
  741. else
  742. {
  743. sx = (float)(original_width - 1);
  744. }
  745. if (height > 1)
  746. {
  747. sy = (float)(original_height - 1) / (float) (height - 1);
  748. }
  749. else
  750. {
  751. sy = (float)(original_height - 1);
  752. }
  753. if (sx < 1.0 && sy < 1.0)
  754. {
  755. /* Magnify both width and height: use weighted sample of 4 pixels */
  756. int i0, i1, j0, j1;
  757. float alpha, beta;
  758. float* src00;
  759. float* src01;
  760. float* src10;
  761. float* src11;
  762. float s1, s2;
  763. float* dst;
  764. for(i = 0; i < height; ++i)
  765. {
  766. i0 = (int)(i * sy);
  767. i1 = i0 + 1;
  768. if (i1 >= original_height)
  769. {
  770. i1 = original_height - 1;
  771. }
  772. alpha = i * sy - i0;
  773. for (j = 0; j < width; ++j)
  774. {
  775. j0 = (int) (j * sx);
  776. j1 = j0 + 1;
  777. if (j1 >= original_width)
  778. {
  779. j1 = original_width - 1;
  780. }
  781. beta = j * sx - j0;
  782. /* Compute weighted average of pixels in rect (i0,j0)-(i1,j1) */
  783. src00 = tempin + (i0 * original_width + j0) * components;
  784. src01 = tempin + (i0 * original_width + j1) * components;
  785. src10 = tempin + (i1 * original_width + j0) * components;
  786. src11 = tempin + (i1 * original_width + j1) * components;
  787. dst = tempout + (i * width + j) * components;
  788. for (k = 0; k < components; ++k)
  789. {
  790. s1 = *src00++ * (1.0f - beta) + *src01++ * beta;
  791. s2 = *src10++ * (1.0f - beta) + *src11++ * beta;
  792. *dst++ = s1 * (1.0f - alpha) + s2 * alpha;
  793. }
  794. }
  795. }
  796. }
  797. else
  798. {
  799. /* Shrink width and/or height: use an unweighted box filter */
  800. int i0, i1;
  801. int j0, j1;
  802. int ii, jj;
  803. float sum;
  804. float* dst;
  805. for (i = 0; i < height; ++i)
  806. {
  807. i0 = (int) (i * sy);
  808. i1 = i0 + 1;
  809. if (i1 >= original_height)
  810. {
  811. i1 = original_height - 1;
  812. }
  813. for (j = 0; j < width; ++j)
  814. {
  815. j0 = (int) (j * sx);
  816. j1 = j0 + 1;
  817. if (j1 >= original_width)
  818. {
  819. j1 = original_width - 1;
  820. }
  821. dst = tempout + (i * width + j) * components;
  822. /* Compute average of pixels in the rectangle (i0,j0)-(i1,j1) */
  823. for (k = 0; k < components; ++k)
  824. {
  825. sum = 0.0;
  826. for (ii = i0; ii <= i1; ++ii)
  827. {
  828. for (jj = j0; jj <= j1; ++jj)
  829. {
  830. sum += *(tempin + (ii * original_width + jj)
  831. * components + k);
  832. }
  833. }
  834. sum /= ( j1 - j0 + 1 ) * ( i1 - i0 + 1 );
  835. *dst++ = sum;
  836. }
  837. }
  838. }
  839. }
  840. // Copy to our results.
  841. for( i = 0; i < height * width * components; ++i)
  842. {
  843. timage[i] = (unsigned char)tempout[i];
  844. }
  845. // Delete our temp buffers.
  846. delete[] tempin;
  847. delete[] tempout;
  848. delete[] image;
  849. return timage;
  850. }
  851. ////////////////////////////////////////////////////////////
  852. // Private Mutators
  853. ////////////////////////////////////////////////////////////