Open Source Tomb Raider Engine
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Texture.cpp 27KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336
  1. /* -*- Mode: C++; tab-width: 3; indent-tabs-mode: t; c-basic-offset: 3 -*- */
  2. /*==========================================================================
  3. *
  4. * Project : MTK, Freyja, OpenRaider
  5. * Author : Terry 'Mongoose' Hendrix II
  6. * Website : http://www.westga.edu/~stu7440
  7. * Email : stu7440@westga.edu
  8. * Object : Texture
  9. * Comments: This is the Texture class.
  10. *
  11. * See file COPYING for license details.
  12. *
  13. *
  14. *-- History ----------------------------------------------------------
  15. *
  16. * 2003.06.30,
  17. * Mongoose - API update, SDL_TTF support moved here, misc features
  18. * SDL_TTF support based on Sam Lantinga's public domain
  19. * SDL_TTF demo functions and algorithms
  20. *
  21. * 2001.05.29:
  22. * Mongoose - Removed legacy code and done clean up
  23. *
  24. * 2001.02.19:
  25. * Mongoose - Moved from GooseEgg to mtk
  26. *
  27. * 2000.04.29:
  28. * Mongoose - Created from old PPM/PCX codebases I have done before
  29. ==========================================================================*/
  30. #include <string.h>
  31. #include <stdlib.h>
  32. #include <stdio.h>
  33. #include <stdarg.h>
  34. #ifdef DEBUG_MEMEORY
  35. # include "memeory_test.h"
  36. #endif
  37. #ifdef USING_MTK_TGA
  38. # include "mtk_tga.h"
  39. #endif
  40. #ifdef USING_MTK_PNG
  41. # include "mtk_png.h"
  42. #endif
  43. #ifdef HAVE_SDL_TTF
  44. # include <SDL/SDL_ttf.h>
  45. #endif
  46. #ifdef __APPLE__
  47. #include <OpenGL/gl.h>
  48. #include <OpenGL/glu.h>
  49. #else
  50. #include <GL/gl.h>
  51. #include <GL/glu.h>
  52. #endif
  53. #include "Texture.h"
  54. //Texture *gTextureManager = 0x0;
  55. gl_font_t *gFontTest = 0x0;
  56. ////////////////////////////////////////////////////////////
  57. // Constructors
  58. ////////////////////////////////////////////////////////////
  59. Texture::Texture()
  60. {
  61. mTextureIds = NULL;
  62. mFlags = 0;
  63. mTextureId = -1;
  64. mTextureId2 = -1;
  65. mTextureCount = 0;
  66. mTextureLimit = 0;
  67. //gTextureManager = this;
  68. initSDL_TTF();
  69. }
  70. Texture::~Texture()
  71. {
  72. if (gFontTest)
  73. {
  74. glDeleteLists(gFontTest->drawListBase, gFontTest->count);
  75. delete gFontTest;
  76. }
  77. reset();
  78. }
  79. ////////////////////////////////////////////////////////////
  80. // Public Accessors
  81. ////////////////////////////////////////////////////////////
  82. unsigned char *Texture::generateColorTexture(unsigned char rgba[4],
  83. unsigned int width,
  84. unsigned int height)
  85. {
  86. unsigned char *image;
  87. unsigned int i, size;
  88. image = new unsigned char[height*width*4];
  89. for (i = 0, size = width*height; i < size; ++i)
  90. {
  91. /* RBGA */
  92. image[i*4] = rgba[0];
  93. image[i*4+1] = rgba[1];
  94. image[i*4+2] = rgba[2];
  95. image[i*4+3] = rgba[3];
  96. }
  97. return image;
  98. }
  99. void glEnterMode2d(unsigned int width, unsigned int height)
  100. {
  101. glPushAttrib(GL_ENABLE_BIT);
  102. glDisable(GL_DEPTH_TEST);
  103. glDisable(GL_CULL_FACE);
  104. glEnable(GL_TEXTURE_2D);
  105. /* This allows alpha blending of 2D textures with the scene */
  106. glEnable(GL_BLEND);
  107. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  108. glViewport(0, 0, width, height);
  109. glMatrixMode(GL_PROJECTION);
  110. glPushMatrix();
  111. glLoadIdentity();
  112. glOrtho(0.0, (GLdouble)width, (GLdouble)height, 0.0, 0.0, 1.0);
  113. glMatrixMode(GL_MODELVIEW);
  114. glPushMatrix();
  115. glLoadIdentity();
  116. glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  117. }
  118. void glExitMode2d()
  119. {
  120. glMatrixMode(GL_MODELVIEW);
  121. glPopMatrix();
  122. glMatrixMode(GL_PROJECTION);
  123. glPopMatrix();
  124. glPopAttrib();
  125. glMatrixMode(GL_MODELVIEW);
  126. }
  127. void bufferedPrintf(char *string, unsigned int len, char *s, ...)
  128. {
  129. va_list args;
  130. if (s && s[0])
  131. {
  132. va_start(args, s);
  133. vsnprintf(string, len-1, s, args);
  134. string[len-1] = 0;
  135. va_end(args);
  136. }
  137. }
  138. void glPrint2d(float x, float y, float scale, char *string)
  139. {
  140. gl_font_t *font = gFontTest;
  141. if (!font)
  142. {
  143. static int errors = 0;
  144. if (errors < 10)
  145. printf("ERROR: glPrintf2d failed, %i\n", ++errors);
  146. return;
  147. }
  148. glPushMatrix();
  149. glBindTexture(GL_TEXTURE_2D, font->textureId);
  150. glTranslatef(x, y, 0);
  151. glScalef(scale, scale, 1);
  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. void glPrint3d(float x, float y, float z,
  162. float pitch, float yaw, float roll,
  163. float scale,
  164. char *string)
  165. {
  166. gl_font_t *font = gFontTest;
  167. if (!font)
  168. {
  169. static int errors = 0;
  170. if (errors < 10)
  171. printf("ERROR: glPrintf3d failed, %i\n", ++errors);
  172. return;
  173. }
  174. glPushMatrix();
  175. glBindTexture(GL_TEXTURE_2D, font->textureId);
  176. glTranslatef(x, y, z);
  177. glRotatef(roll, 1, 0, 0);
  178. glRotatef(yaw, 0, 1, 0);
  179. glRotatef(pitch, 0, 0, 1);
  180. glScalef(scale, scale, scale);
  181. /* FIXME:
  182. * Add utf-8 dencoding of char* string
  183. *
  184. * Also this string must be preprocessed to have glyph offsets
  185. * instead of ASCII text in it and support counts over 256 */
  186. glListBase(font->drawListBase - font->utf8Offset);
  187. glCallLists(strlen(string), GL_BYTE, string);
  188. glPopMatrix();
  189. }
  190. int Texture::loadFontTTF(char *filename,
  191. unsigned int utf8Offset, unsigned int count)
  192. {
  193. ttf_texture_t *texture;
  194. unsigned char rgb[3] = {0xff, 0xff, 0xff};
  195. if (!filename || !filename[0])
  196. {
  197. printf("fontTest> Passed bad filename\n");
  198. return -1;
  199. }
  200. texture = generateFontTexture(filename, 24, 256, rgb,
  201. //0x303f, 0x3093-0x303f, // hiragana
  202. //32, 126 - 32, // english
  203. utf8Offset, count,
  204. false);
  205. if (texture)
  206. {
  207. #ifdef DUMP_TTF_TGA
  208. FILE *f = fopen("/tmp/font.tga", "wb");
  209. if (f)
  210. {
  211. mtk_image__tga_save(f, texture->texture, 256, 256, 4);
  212. fclose(f);
  213. }
  214. else
  215. {
  216. perror("/tmp/ttf_font.tga");
  217. }
  218. #endif
  219. gFontTest = generateFont(texture);
  220. /* FIXME: Until UTF8 decoder is working, we map from
  221. ASCII when rendering */
  222. gFontTest->utf8Offset = 32; // hack to use ASCII strings to test unicode
  223. delete [] texture->texture;
  224. delete [] texture->glyphs;
  225. delete texture;
  226. }
  227. else
  228. {
  229. return -2;
  230. }
  231. return 0;
  232. }
  233. gl_font_t *Texture::generateFont(ttf_texture_t *texture)
  234. {
  235. #ifdef HAVE_SDL_TTF
  236. const float spacing = 4.0;
  237. unsigned int i;
  238. float u, v, u2, v2;
  239. int h;
  240. gl_font_t *font;
  241. if (!texture)
  242. return NULL;
  243. printf("@ Generating gl font from texture...\n");
  244. font = new gl_font_t;
  245. font->utf8Offset = texture->utf8Offset;
  246. font->count = texture->count;
  247. font->textureId = loadBuffer(texture->texture,
  248. texture->width, texture->width, RGBA, 32);
  249. glColor3f(1.0, 1.0, 1.0);
  250. font->drawListBase = glGenLists(texture->count);
  251. glBindTexture(GL_TEXTURE_2D, font->textureId);
  252. for (i = 0; i < texture->count; i++)
  253. {
  254. /* Generate texture coordinates for this TTF glyph */
  255. u = (float)texture->glyphs[i].x / (float)texture->width;
  256. v = (float)texture->glyphs[i].y / (float)texture->width;
  257. u2 = (((float)texture->glyphs[i].x + (float)texture->glyphs[i].w) /
  258. (float)texture->width);
  259. v2 = (((float)texture->glyphs[i].y + (float)texture->glyphs[i].h) /
  260. (float)texture->width);
  261. #ifdef DEBUG_TTF_OFFSET
  262. if (i+texture->utf8Offset == 'y' || i+texture->utf8Offset == 'x')
  263. {
  264. printf("%c: %i %i %i\n",
  265. i+texture->utf8Offset,
  266. texture->fontDescent,
  267. texture->glyphs[i].miny, texture->glyphs[i].maxy);
  268. }
  269. #endif
  270. if (texture->glyphs[i].h < texture->fontHeight)
  271. {
  272. h = texture->fontHeight - texture->glyphs[i].h;
  273. }
  274. else
  275. {
  276. h = 0;
  277. }
  278. h += -texture->fontHeight/2-(texture->fontDescent + texture->glyphs[i].miny);
  279. /* Make a list for this TTF glyph, one nonuniform Quad per glyph */
  280. glNewList(font->drawListBase + i, GL_COMPILE);
  281. glBegin(GL_QUADS);
  282. glTexCoord2f(u2, v); /* Top, right */
  283. glVertex3i(texture->glyphs[i].w, h, 0);
  284. glTexCoord2f(u, v); /* Top, left */
  285. glVertex3i(0, h, 0);
  286. glTexCoord2f(u, v2); /* Bottom, left */
  287. glVertex3i(0, h+texture->glyphs[i].h, 0);
  288. glTexCoord2f(u2, v2); /* Bottom, right */
  289. glVertex3i(texture->glyphs[i].w, h+texture->glyphs[i].h, 0);
  290. glEnd();
  291. /* Move To The Left Of The Character */
  292. glTranslated(texture->glyphs[i].w + spacing, 0, 0);
  293. glEndList();
  294. }
  295. return font;
  296. #else
  297. return NULL;
  298. #endif
  299. }
  300. ttf_texture_t *Texture::generateFontTexture(char *filename, int pointSize,
  301. unsigned int textureWidth,
  302. unsigned char color[3],
  303. unsigned int utf8Offset,
  304. unsigned int count,
  305. char verbose)
  306. {
  307. #ifdef HAVE_SDL_TTF
  308. unsigned int i;
  309. int xx = 0, yy = 0, hh = 0, k, h, w, offset;
  310. unsigned char b;
  311. unsigned char *image;
  312. SDL_Surface* glyph;
  313. SDL_Color sdlColor;
  314. TTF_Font *font;
  315. ttf_texture_t *texture;
  316. FILE *f;
  317. sdlColor.r = color[0];
  318. sdlColor.g = color[1];
  319. sdlColor.b = color[2];
  320. if (!mFlags & fUseSDL_TTF)
  321. {
  322. printf("SDL_TTF couldn't be used... exiting\n");
  323. return NULL;
  324. }
  325. if (pointSize < 8)
  326. {
  327. pointSize = 8;
  328. }
  329. /* Work around for TTF_OpenFont for file not found segfault */
  330. f = fopen(filename, "rb");
  331. if (!f)
  332. {
  333. printf("generateFontTexture> Couldn't load '%s'\n", filename);
  334. perror(filename);
  335. return NULL;
  336. }
  337. fclose(f);
  338. // Open the font file at the requested point size
  339. font = TTF_OpenFont(filename, pointSize);
  340. if (font == NULL)
  341. {
  342. fprintf(stderr, "generateFontTexture> Couldn't load %d pt font from %s: %s\n",
  343. pointSize, filename, SDL_GetError());
  344. return NULL;
  345. }
  346. // TTF_STYLE_NORMAL, TTF_STYLE_BOLD, TTF_STYLE_ITALIC, TTF_STYLE_UNDERLINE
  347. int renderStyle = TTF_STYLE_NORMAL;
  348. TTF_SetFontStyle(font, renderStyle);
  349. /* Allocate a new TTF font texture */
  350. printf("@ Creating font texture from '%s'...\n", filename);
  351. texture = new ttf_texture_t;
  352. texture->width = textureWidth;
  353. texture->utf8Offset = utf8Offset;
  354. texture->count = count;
  355. texture->glyphs = new ttf_glyph_t[count];
  356. texture->texture = new unsigned char[textureWidth*textureWidth*4];
  357. memset(texture->texture, 0, textureWidth*textureWidth*4);
  358. texture->fontHeight = TTF_FontHeight(font);
  359. texture->fontAscent = TTF_FontAscent(font);
  360. texture->fontDescent = TTF_FontDescent(font);
  361. texture->fontSpacing = TTF_FontLineSkip(font);
  362. for (i = 0; i < count; ++i)
  363. {
  364. glyph = TTF_RenderGlyph_Blended(font, i + utf8Offset, sdlColor);
  365. if (glyph)
  366. {
  367. image = (unsigned char*)glyph->pixels;
  368. TTF_GlyphMetrics(font, i + utf8Offset,
  369. &texture->glyphs[i].minx, &texture->glyphs[i].maxx,
  370. &texture->glyphs[i].miny, &texture->glyphs[i].maxy,
  371. &texture->glyphs[i].advance);
  372. texture->glyphs[i].w = glyph->w;
  373. texture->glyphs[i].h = glyph->h;
  374. if (xx + texture->glyphs[i].w > ((int)textureWidth - 1))
  375. {
  376. yy += hh;
  377. hh = 2;
  378. xx = 2;
  379. texture->glyphs[i].x = 0;
  380. texture->glyphs[i].y = yy;
  381. }
  382. else
  383. {
  384. (texture->glyphs[i].h > hh) ? hh = texture->glyphs[i].h: 0;
  385. texture->glyphs[i].x = xx;
  386. texture->glyphs[i].y = yy;
  387. }
  388. xx += glyph->w;
  389. if (verbose)
  390. {
  391. printf("0x%x : %ix%i @ %i, %i\n", i + utf8Offset,
  392. texture->glyphs[i].w, texture->glyphs[i].h,
  393. texture->glyphs[i].x, texture->glyphs[i].y);
  394. }
  395. /* Blit @ xx, yy - in pixels */
  396. for (k = 0; k < glyph->w*glyph->h; ++k)
  397. {
  398. w = texture->glyphs[i].x + k%glyph->w;
  399. h = texture->glyphs[i].y + k/glyph->w;
  400. offset = (w + h*textureWidth);
  401. if (verbose)
  402. {
  403. printf("Offset: %i; Pixel: %i,%i; Data: 0x%08x\n",
  404. offset, w, h, *((unsigned int *)&image[k*4]));
  405. }
  406. /* 32-bit ARGB to RGBA */
  407. b = image[k*4+3];
  408. texture->texture[offset*4] = image[k*4] = image[k*4+1];
  409. texture->texture[offset*4+1] = image[k*4+1] = image[k*4+2];
  410. texture->texture[offset*4+2] = image[k*4+2] = image[k*4+3];
  411. texture->texture[offset*4+3] = image[k*4+3] = b;
  412. }
  413. }
  414. }
  415. TTF_CloseFont(font);
  416. return texture;
  417. #else
  418. printf("SDL_TTF not enabled in this build.\n");
  419. return NULL;
  420. #endif
  421. }
  422. ////////////////////////////////////////////////////////////
  423. // Public Mutators
  424. ////////////////////////////////////////////////////////////
  425. int Texture::loadColorTexture(unsigned char rgba[4],
  426. unsigned int width, unsigned int height)
  427. {
  428. unsigned char *image;
  429. int id = -1;
  430. image = generateColorTexture(rgba, width, height);
  431. id = loadBuffer(image, width, height, RGBA, 32);
  432. delete [] image;
  433. return id;
  434. }
  435. void Texture::initSDL_TTF()
  436. {
  437. #ifdef HAVE_SDL_TTF
  438. // Initialize the TTF library
  439. if (TTF_Init() < 0)
  440. {
  441. fprintf(stderr, "initSDL_TTF> TTF_Init() failed!\n");
  442. fprintf(stderr, "initSDL_TTF> Error is [%s].\n", SDL_GetError());
  443. }
  444. else
  445. {
  446. mFlags |= fUseSDL_TTF;
  447. printf("@ Started SDL_TTF subsystem...\n");
  448. atexit(TTF_Quit);
  449. }
  450. #endif
  451. }
  452. void Texture::setFlag(TextureFlag flag)
  453. {
  454. mFlags |= flag;
  455. }
  456. void Texture::clearFlag(TextureFlag flag)
  457. {
  458. mFlags |= flag;
  459. mFlags ^= flag;
  460. }
  461. void Texture::reset()
  462. {
  463. if (mTextureIds)
  464. {
  465. glDeleteTextures(mTextureLimit, mTextureIds);
  466. delete [] mTextureIds;
  467. }
  468. mTextureIds = 0x0;
  469. mTextureCount = 0;
  470. mTextureLimit = 0;
  471. }
  472. void Texture::disableMultiTexture()
  473. {
  474. mFlags ^= fUseMultiTexture;
  475. mTextureId = -1;
  476. mTextureId2 = -1;
  477. glDisable(GL_TEXTURE_2D);
  478. glActiveTextureARB(GL_TEXTURE0_ARB);
  479. }
  480. void Texture::useMultiTexture(float aU, float aV, float bU, float bV)
  481. {
  482. if (!(mFlags & fUseMultiTexture))
  483. return;
  484. glMultiTexCoord2fARB(GL_TEXTURE0_ARB, aU, aV);
  485. glMultiTexCoord2fARB(GL_TEXTURE1_ARB, bU, bV);
  486. }
  487. void Texture::useMultiTexture(float u, float v)
  488. {
  489. if (!(mFlags & fUseMultiTexture))
  490. return;
  491. glMultiTexCoord2fARB(GL_TEXTURE0_ARB, u, v);
  492. glMultiTexCoord2fARB(GL_TEXTURE1_ARB, u, v);
  493. }
  494. void Texture::bindMultiTexture(int texture0, int texture1)
  495. {
  496. if (//(int)a == mTextureId && (int)b == mTextureId2 ||
  497. !mTextureIds ||
  498. texture0 < 0 || texture0 > (int)mTextureCount ||
  499. texture1 < 0 || texture1 > (int)mTextureCount)
  500. {
  501. return;
  502. }
  503. mFlags |= fUseMultiTexture;
  504. mTextureId = texture0;
  505. mTextureId2 = texture1;
  506. glActiveTextureARB(GL_TEXTURE0_ARB);
  507. glEnable(GL_TEXTURE_2D);
  508. glBindTexture(GL_TEXTURE_2D, mTextureIds[texture0]);
  509. glActiveTextureARB(GL_TEXTURE1_ARB);
  510. glEnable(GL_TEXTURE_2D);
  511. glBindTexture(GL_TEXTURE_2D, mTextureIds[texture1]);
  512. }
  513. void Texture::setMaxTextureCount(unsigned int n)
  514. {
  515. mTextureLimit = n;
  516. mTextureIds = new unsigned int[n];
  517. glGenTextures(n, mTextureIds);
  518. }
  519. int Texture::getTextureCount()
  520. {
  521. return (mTextureCount-1);
  522. }
  523. int Texture::loadBuffer(unsigned char *image,
  524. unsigned int width, unsigned int height,
  525. ColorMode mode, unsigned int bpp)
  526. {
  527. int id;
  528. id = loadBufferSlot(image, width, height, mode, bpp, mTextureCount++);
  529. if (id < 0)
  530. {
  531. return id;
  532. }
  533. return ++id;
  534. }
  535. void convertARGB32bppToRGBA32bpp(unsigned char *image,
  536. unsigned int w, unsigned int h)
  537. {
  538. unsigned int i, size = w*h;
  539. unsigned char swap;
  540. for (i = 0; i < size; ++i)
  541. {
  542. /* 32-bit ARGB to RGBA */
  543. swap = image[i*4+3];
  544. image[i*4] = image[i*4+1];
  545. image[i*4+1] = image[i*4+2];
  546. image[i*4+2] = image[i*4+3];
  547. image[i*4+3] = swap;
  548. }
  549. }
  550. GLint deprecated_gluBuild2DMipmaps(GLenum target, GLint internalFormat, GLsizei width, GLsizei height, GLenum format, GLenum type, const void *data);
  551. int Texture::loadBufferSlot(unsigned char *image,
  552. unsigned int width, unsigned int height,
  553. ColorMode mode, unsigned int bpp,
  554. unsigned int slot)
  555. {
  556. unsigned char bytes;
  557. unsigned int glcMode;
  558. if (!mTextureIds || slot >= mTextureLimit)
  559. {
  560. printf("Texture::Load> ERROR Not initalizied or out of free slots\n");
  561. return -1000;
  562. }
  563. if (!width || !height || !image)
  564. {
  565. printf("Texture::Load> ERROR Assertion 'image is valid' failed\n");
  566. return -1;
  567. }
  568. switch (mode)
  569. {
  570. case GREYSCALE:
  571. if (bpp != 8)
  572. {
  573. printf("Texture::Load> ERROR Unsupported GREYSCALE, %i bpp\n", bpp);
  574. return -2;
  575. }
  576. bytes = 1;
  577. glcMode = GL_LUMINANCE;
  578. break;
  579. case RGB:
  580. if (bpp != 24)
  581. {
  582. printf("Texture::Load> ERROR Unsupported RGBA, %i bpp\n", bpp);
  583. return -2;
  584. }
  585. bytes = 3;
  586. glcMode = GL_RGB;
  587. break;
  588. case ARGB:
  589. if (bpp == 32)
  590. {
  591. convertARGB32bppToRGBA32bpp(image, width, height);
  592. }
  593. case RGBA:
  594. if (bpp != 32)
  595. {
  596. printf("Texture::Load> ERROR Unsupported RGBA/ARGB, %i bpp\n", bpp);
  597. return -2;
  598. }
  599. bytes = 4;
  600. glcMode = GL_RGBA;
  601. break;
  602. default:
  603. printf("Texture::Load> ERROR Unknown color mode %i\n", mode);
  604. return -2;
  605. }
  606. glClearColor(0.0, 0.0, 0.0, 0.0);
  607. glEnable(GL_DEPTH_TEST);
  608. glShadeModel(GL_SMOOTH);
  609. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  610. glBindTexture(GL_TEXTURE_2D, mTextureIds[slot]);
  611. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  612. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  613. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  614. if (mFlags & fUseMipmaps)
  615. {
  616. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
  617. GL_NEAREST_MIPMAP_LINEAR);
  618. //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  619. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
  620. GL_LINEAR_MIPMAP_LINEAR);
  621. //gluBuild2DMipmaps(GL_TEXTURE_2D, bytes, width, height, glcMode, GL_UNSIGNED_BYTE, image);
  622. // gluBuild2DMipmaps is deprecated. Replacement by xythobuz
  623. deprecated_gluBuild2DMipmaps(GL_TEXTURE_2D, bytes, width, height, glcMode, GL_UNSIGNED_BYTE, image);
  624. }
  625. else
  626. {
  627. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  628. glTexImage2D(GL_TEXTURE_2D, 0, glcMode, width, height, 0,
  629. glcMode, GL_UNSIGNED_BYTE, image);
  630. }
  631. //glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  632. return slot;
  633. }
  634. // http://mmmovania.blogspot.de/2011/01/opengl-30-and-above-deprecated-func-and.html
  635. // http://www.g-truc.net/post-0256.html
  636. GLint deprecated_gluBuild2DMipmaps(GLenum target, GLint internalFormat, GLsizei width, GLsizei height, GLenum format, GLenum type, const void *data) {
  637. glTexParameteri(target, GL_GENERATE_MIPMAP, GL_TRUE);
  638. glTexImage2D(target, 0, internalFormat, width, height, 0, format, type, data);
  639. return 0;
  640. }
  641. void Texture::bindTextureId(unsigned int n)
  642. {
  643. if ((int)n == mTextureId || !mTextureIds || n > mTextureCount)
  644. {
  645. return;
  646. }
  647. mTextureId = n;
  648. glEnable(GL_TEXTURE_2D);
  649. //glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  650. glBindTexture(GL_TEXTURE_2D, mTextureIds[n]);
  651. }
  652. void Texture::glScreenShot(char *base, unsigned int width, unsigned int height)
  653. {
  654. #ifdef USING_MTK_PNG
  655. FILE *f;
  656. int sz = width*height;
  657. unsigned char *image = new unsigned char[sz*3];
  658. char filename[1024];
  659. static int count = 0;
  660. bool done = false;
  661. if (!image)
  662. {
  663. printf("glScreenShot> ERROR: Couldn't allocate image!\n");
  664. return;
  665. }
  666. while (!done)
  667. {
  668. snprintf(filename, 1024, "%s-%03i.png", base, count++);
  669. f = fopen(filename, "rb");
  670. if (f)
  671. fclose(f);
  672. else
  673. done = true;
  674. }
  675. f = fopen(filename, "wb");
  676. if (!f)
  677. {
  678. printf("glScreenShot> ERROR: Couldn't write screenshot.\n");
  679. perror("glScreenShot> ERROR: ");
  680. return;
  681. }
  682. // Capture frame buffer
  683. glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, image);
  684. mtk_image__png_save(f, image, width, height, 3);
  685. fclose(f);
  686. delete [] image;
  687. printf("glScreenShot> Took screenshot '%s'.\n", filename);
  688. #else
  689. FILE *f;
  690. int sz = width*height;
  691. unsigned char *image = new unsigned char[sz*3];
  692. unsigned char *swap_row = new unsigned char[width*3];
  693. char filename[1024];
  694. static int count = 0;
  695. bool done = false;
  696. int i, j, size;
  697. unsigned char comment_lenght;
  698. unsigned char colormap_type;
  699. unsigned char image_type;
  700. unsigned short colormap_index;
  701. unsigned short colormap_lenght;
  702. unsigned char colormap_bbp;
  703. unsigned short origin_x;
  704. unsigned short origin_y;
  705. unsigned short swidth;
  706. unsigned short sheight;
  707. char comment[32] = "Mongoose TGA 0.0.1\0";
  708. unsigned char tmp, bpp, desc_flags;
  709. if (!image || !width || !height)
  710. {
  711. if (image)
  712. {
  713. delete [] image;
  714. }
  715. printf("glScreenShot> ERROR: Couldn't allocate image!\n");
  716. return;
  717. }
  718. // Don't overwrite files
  719. while (!done)
  720. {
  721. snprintf(filename, 1024, "%s-%04i.tga", base, count++);
  722. f = fopen(filename, "rb");
  723. if (f)
  724. fclose(f);
  725. else
  726. done = true;
  727. }
  728. f = fopen(filename, "wb");
  729. if (!f)
  730. {
  731. printf("glScreenShot> ERROR: Couldn't write screenshot.\n");
  732. perror("glScreenShot> ERROR: ");
  733. return;
  734. }
  735. // Capture frame buffer
  736. glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, image);
  737. // Flip vertical
  738. for (i = 0, j = (int)height-1; i < (int)height/2; ++i, --j)
  739. {
  740. memcpy(swap_row, &image[i*width*3], width*3);
  741. memcpy(&image[i*width*3], &image[j*width*3], width*3);
  742. memcpy(&image[j*width*3], swap_row, width*3);
  743. }
  744. delete [] swap_row;
  745. comment_lenght = strlen(comment);
  746. colormap_type = 0;
  747. image_type = 2;
  748. colormap_index = 0;
  749. colormap_lenght = 0;
  750. colormap_bbp = 0;
  751. origin_x = origin_y = 0;
  752. swidth = width;
  753. sheight = height;
  754. bpp = 24;
  755. desc_flags = 32;
  756. // Write TGA header
  757. fwrite(&comment_lenght, 1, 1, f);
  758. fwrite(&colormap_type, 1, 1, f);
  759. fwrite(&image_type, 1, 1, f);
  760. fwrite(&colormap_index, 2, 1, f);
  761. fwrite(&colormap_lenght, 2, 1, f);
  762. fwrite(&colormap_bbp, 1, 1, f);
  763. fwrite(&origin_x, 2, 1, f);
  764. fwrite(&origin_y, 2, 1, f);
  765. fwrite(&swidth, 2, 1, f);
  766. fwrite(&sheight, 2, 1, f);
  767. fwrite(&bpp, 1, 1, f);
  768. fwrite(&desc_flags, 1, 1, f);
  769. // Write comment
  770. fwrite(&comment, 1, comment_lenght, f);
  771. size = width * height * 3;
  772. for (i = 0; i < size; i += 3)
  773. {
  774. tmp = image[i];
  775. image[i] = image[i + 2];
  776. image[i + 2] = tmp;
  777. }
  778. // Write image data
  779. if (fwrite(image, size, 1, f) < 1)
  780. {
  781. perror("glScreenShot> Disk write failed.\n");
  782. fclose(f);
  783. return;
  784. }
  785. fclose(f);
  786. delete [] image;
  787. printf("Took screenshot '%s'.\n", filename);
  788. #endif
  789. }
  790. int Texture::loadPNG(char *filename)
  791. {
  792. #ifdef USING_MTK_PNG
  793. FILE *f;
  794. unsigned char *image = NULL;
  795. unsigned char *image2 = NULL;
  796. unsigned int w, h;
  797. char type;
  798. int id = -1;
  799. f = fopen(filename, "rb");
  800. if (!f)
  801. {
  802. perror("Couldn't load file");
  803. }
  804. else if (!mtk_image__png_check(f))
  805. {
  806. mtk_image__png_load(f, &image, &w, &h, &type);
  807. type += 2;
  808. image2 = scaleBuffer(image, w, h, (type == 4) ? 4 : 3);
  809. if (image2)
  810. {
  811. image = image2;
  812. w = h = 256;
  813. }
  814. if (image)
  815. {
  816. id = loadBuffer(image, w, h,
  817. (type == 4) ? RGBA : RGB,
  818. (type == 4) ? 32 : 24);
  819. printf("%c", (id == -1) ? 'x' : 'o');
  820. fflush(stdout);
  821. delete [] image;
  822. }
  823. if (f)
  824. {
  825. fclose(f);
  826. }
  827. }
  828. if (id == -1)
  829. {
  830. printf("Texture::loadPNG> ERROR: Failed to load '%s'\n", filename);
  831. }
  832. return id;
  833. #else
  834. printf("ERROR: MTK PNG support not enabled in this build\n");
  835. return -1;
  836. #endif
  837. }
  838. int Texture::loadTGA(char *filename)
  839. {
  840. #ifdef USING_MTK_TGA
  841. FILE *f;
  842. unsigned char *image = NULL;
  843. unsigned char *image2 = NULL;
  844. unsigned int w, h;
  845. char type;
  846. int id = -1;
  847. f = fopen(filename, "rb");
  848. if (!f)
  849. {
  850. perror("Couldn't load file");
  851. }
  852. else if (!mtk_image__tga_check(f))
  853. {
  854. mtk_image__tga_load(f, &image, &w, &h, &type);
  855. type += 2;
  856. image2 = scaleBuffer(image, w, h, (type == 4) ? 4 : 3);
  857. if (image2)
  858. {
  859. image = image2;
  860. w = h = 256;
  861. }
  862. if (image)
  863. {
  864. id = loadBuffer(image, w, h,
  865. (type == 4) ? RGBA : RGB,
  866. (type == 4) ? 32 : 24);
  867. printf("%c", (id == -1) ? 'x' : 'o');
  868. fflush(stdout);
  869. delete [] image;
  870. }
  871. fclose(f);
  872. }
  873. if (id == -1)
  874. {
  875. printf("Texture::loadTGA> ERROR: Failed to load '%s'\n", filename);
  876. }
  877. return id;
  878. #else
  879. printf("ERROR: MTK TGA support not enabled in this build\n");
  880. return -1;
  881. #endif
  882. }
  883. ////////////////////////////////////////////////////////////
  884. // Private Accessors
  885. ////////////////////////////////////////////////////////////
  886. int Texture::nextPower(int seed)
  887. {
  888. int i;
  889. for(i = 1; i < seed; i *= 2)
  890. ;
  891. return i;
  892. }
  893. /* This code based off on gluScaleImage() */
  894. unsigned char *Texture::scaleBuffer(unsigned char *image,
  895. int width, int height,
  896. int components)
  897. {
  898. int i, j, k;
  899. float* tempin;
  900. float* tempout;
  901. float sx, sy;
  902. // int components = 3;
  903. unsigned char *timage;
  904. int original_height = height;
  905. int original_width = width;
  906. if (!image || !width || !height)
  907. {
  908. return NULL;
  909. }
  910. height = nextPower(height);
  911. width = nextPower(width);
  912. if (height > 256)
  913. height = 256;
  914. if (width > 256)
  915. width = 256;
  916. // Check to see if scaling is needed
  917. if (height == original_height && width == original_width)
  918. return NULL;
  919. //printf("%i\n", components);
  920. timage = new unsigned char[height * width * components];
  921. tempin = new float[original_width * original_height * components * sizeof(float)];
  922. tempout = new float[width * height * components * sizeof(float)];
  923. if (!tempout || !tempin)
  924. {
  925. if (tempout)
  926. delete [] tempout;
  927. if (tempin)
  928. delete [] tempin;
  929. printf("Oh shit out of memory!\n");
  930. return NULL;
  931. }
  932. // Copy user data to float format.
  933. for (i = 0; i < original_height * original_width * components; ++i)
  934. {
  935. tempin[i] = (float)image[i];
  936. }
  937. // Determine which filter to use by checking ratios.
  938. if (width > 1)
  939. {
  940. sx = (float)(original_width - 1) / (float)(width - 1);
  941. }
  942. else
  943. {
  944. sx = (float)(original_width - 1);
  945. }
  946. if (height > 1)
  947. {
  948. sy = (float)(original_height - 1) / (float) (height - 1);
  949. }
  950. else
  951. {
  952. sy = (float)(original_height - 1);
  953. }
  954. if (sx < 1.0 && sy < 1.0)
  955. {
  956. /* Magnify both width and height: use weighted sample of 4 pixels */
  957. int i0, i1, j0, j1;
  958. float alpha, beta;
  959. float* src00;
  960. float* src01;
  961. float* src10;
  962. float* src11;
  963. float s1, s2;
  964. float* dst;
  965. for(i = 0; i < height; ++i)
  966. {
  967. i0 = (int)(i * sy);
  968. i1 = i0 + 1;
  969. if (i1 >= original_height)
  970. {
  971. i1 = original_height - 1;
  972. }
  973. alpha = i * sy - i0;
  974. for (j = 0; j < width; ++j)
  975. {
  976. j0 = (int) (j * sx);
  977. j1 = j0 + 1;
  978. if (j1 >= original_width)
  979. {
  980. j1 = original_width - 1;
  981. }
  982. beta = j * sx - j0;
  983. /* Compute weighted average of pixels in rect (i0,j0)-(i1,j1) */
  984. src00 = tempin + (i0 * original_width + j0) * components;
  985. src01 = tempin + (i0 * original_width + j1) * components;
  986. src10 = tempin + (i1 * original_width + j0) * components;
  987. src11 = tempin + (i1 * original_width + j1) * components;
  988. dst = tempout + (i * width + j) * components;
  989. for (k = 0; k < components; ++k)
  990. {
  991. s1 = *src00++ * (1.0 - beta) + *src01++ * beta;
  992. s2 = *src10++ * (1.0 - beta) + *src11++ * beta;
  993. *dst++ = s1 * (1.0 - alpha) + s2 * alpha;
  994. }
  995. }
  996. }
  997. }
  998. else
  999. {
  1000. /* Shrink width and/or height: use an unweighted box filter */
  1001. int i0, i1;
  1002. int j0, j1;
  1003. int ii, jj;
  1004. float sum;
  1005. float* dst;
  1006. for (i = 0; i < height; ++i)
  1007. {
  1008. i0 = (int) (i * sy);
  1009. i1 = i0 + 1;
  1010. if (i1 >= original_height)
  1011. {
  1012. i1 = original_height - 1;
  1013. }
  1014. for (j = 0; j < width; ++j)
  1015. {
  1016. j0 = (int) (j * sx);
  1017. j1 = j0 + 1;
  1018. if (j1 >= original_width)
  1019. {
  1020. j1 = original_width - 1;
  1021. }
  1022. dst = tempout + (i * width + j) * components;
  1023. /* Compute average of pixels in the rectangle (i0,j0)-(i1,j1) */
  1024. for (k = 0; k < components; ++k)
  1025. {
  1026. sum = 0.0;
  1027. for (ii = i0; ii <= i1; ++ii)
  1028. {
  1029. for (jj = j0; jj <= j1; ++jj)
  1030. {
  1031. sum += *(tempin + (ii * original_width + jj)
  1032. * components + k);
  1033. }
  1034. }
  1035. sum /= ( j1 - j0 + 1 ) * ( i1 - i0 + 1 );
  1036. *dst++ = sum;
  1037. }
  1038. }
  1039. }
  1040. }
  1041. // Copy to our results.
  1042. for( i = 0; i < height * width * components; ++i)
  1043. {
  1044. timage[i] = (unsigned char)tempout[i];
  1045. }
  1046. // Delete our temp buffers.
  1047. delete[] tempin;
  1048. delete[] tempout;
  1049. delete[] image;
  1050. return timage;
  1051. }
  1052. ////////////////////////////////////////////////////////////
  1053. // Private Mutators
  1054. ////////////////////////////////////////////////////////////