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 29KB

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