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

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