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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325
  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. int Texture::loadBufferSlot(unsigned char *image,
  551. unsigned int width, unsigned int height,
  552. ColorMode mode, unsigned int bpp,
  553. unsigned int slot)
  554. {
  555. unsigned char bytes;
  556. unsigned int glcMode;
  557. if (!mTextureIds || slot >= mTextureLimit)
  558. {
  559. printf("Texture::Load> ERROR Not initalizied or out of free slots\n");
  560. return -1000;
  561. }
  562. if (!width || !height || !image)
  563. {
  564. printf("Texture::Load> ERROR Assertion 'image is valid' failed\n");
  565. return -1;
  566. }
  567. switch (mode)
  568. {
  569. case GREYSCALE:
  570. if (bpp != 8)
  571. {
  572. printf("Texture::Load> ERROR Unsupported GREYSCALE, %i bpp\n", bpp);
  573. return -2;
  574. }
  575. bytes = 1;
  576. glcMode = GL_LUMINANCE;
  577. break;
  578. case RGB:
  579. if (bpp != 24)
  580. {
  581. printf("Texture::Load> ERROR Unsupported RGBA, %i bpp\n", bpp);
  582. return -2;
  583. }
  584. bytes = 3;
  585. glcMode = GL_RGB;
  586. break;
  587. case ARGB:
  588. if (bpp == 32)
  589. {
  590. convertARGB32bppToRGBA32bpp(image, width, height);
  591. }
  592. case RGBA:
  593. if (bpp != 32)
  594. {
  595. printf("Texture::Load> ERROR Unsupported RGBA/ARGB, %i bpp\n", bpp);
  596. return -2;
  597. }
  598. bytes = 4;
  599. glcMode = GL_RGBA;
  600. break;
  601. default:
  602. printf("Texture::Load> ERROR Unknown color mode %i\n", mode);
  603. return -2;
  604. }
  605. glClearColor(0.0, 0.0, 0.0, 0.0);
  606. glEnable(GL_DEPTH_TEST);
  607. glShadeModel(GL_SMOOTH);
  608. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  609. glBindTexture(GL_TEXTURE_2D, mTextureIds[slot]);
  610. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  611. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  612. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  613. if (mFlags & fUseMipmaps)
  614. {
  615. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
  616. GL_NEAREST_MIPMAP_LINEAR);
  617. //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  618. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
  619. GL_LINEAR_MIPMAP_LINEAR);
  620. gluBuild2DMipmaps(GL_TEXTURE_2D, bytes, width, height,
  621. glcMode, GL_UNSIGNED_BYTE, image);
  622. }
  623. else
  624. {
  625. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  626. glTexImage2D(GL_TEXTURE_2D, 0, glcMode, width, height, 0,
  627. glcMode, GL_UNSIGNED_BYTE, image);
  628. }
  629. //glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  630. return slot;
  631. }
  632. void Texture::bindTextureId(unsigned int n)
  633. {
  634. if ((int)n == mTextureId || !mTextureIds || n > mTextureCount)
  635. {
  636. return;
  637. }
  638. mTextureId = n;
  639. glEnable(GL_TEXTURE_2D);
  640. //glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  641. glBindTexture(GL_TEXTURE_2D, mTextureIds[n]);
  642. }
  643. void Texture::glScreenShot(char *base, unsigned int width, unsigned int height)
  644. {
  645. #ifdef USING_MTK_PNG
  646. FILE *f;
  647. int sz = width*height;
  648. unsigned char *image = new unsigned char[sz*3];
  649. char filename[1024];
  650. static int count = 0;
  651. bool done = false;
  652. if (!image)
  653. {
  654. printf("glScreenShot> ERROR: Couldn't allocate image!\n");
  655. return;
  656. }
  657. while (!done)
  658. {
  659. snprintf(filename, 1024, "%s-%03i.png", base, count++);
  660. f = fopen(filename, "rb");
  661. if (f)
  662. fclose(f);
  663. else
  664. done = true;
  665. }
  666. f = fopen(filename, "wb");
  667. if (!f)
  668. {
  669. printf("glScreenShot> ERROR: Couldn't write screenshot.\n");
  670. perror("glScreenShot> ERROR: ");
  671. return;
  672. }
  673. // Capture frame buffer
  674. glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, image);
  675. mtk_image__png_save(f, image, width, height, 3);
  676. fclose(f);
  677. delete [] image;
  678. printf("glScreenShot> Took screenshot '%s'.\n", filename);
  679. #else
  680. FILE *f;
  681. int sz = width*height;
  682. unsigned char *image = new unsigned char[sz*3];
  683. unsigned char *swap_row = new unsigned char[width*3];
  684. char filename[1024];
  685. static int count = 0;
  686. bool done = false;
  687. int i, j, size;
  688. unsigned char comment_lenght;
  689. unsigned char colormap_type;
  690. unsigned char image_type;
  691. unsigned short colormap_index;
  692. unsigned short colormap_lenght;
  693. unsigned char colormap_bbp;
  694. unsigned short origin_x;
  695. unsigned short origin_y;
  696. unsigned short swidth;
  697. unsigned short sheight;
  698. char comment[32] = "Mongoose TGA 0.0.1\0";
  699. unsigned char tmp, bpp, desc_flags;
  700. if (!image || !width || !height)
  701. {
  702. if (image)
  703. {
  704. delete [] image;
  705. }
  706. printf("glScreenShot> ERROR: Couldn't allocate image!\n");
  707. return;
  708. }
  709. // Don't overwrite files
  710. while (!done)
  711. {
  712. snprintf(filename, 1024, "%s-%04i.tga", base, count++);
  713. f = fopen(filename, "rb");
  714. if (f)
  715. fclose(f);
  716. else
  717. done = true;
  718. }
  719. f = fopen(filename, "wb");
  720. if (!f)
  721. {
  722. printf("glScreenShot> ERROR: Couldn't write screenshot.\n");
  723. perror("glScreenShot> ERROR: ");
  724. return;
  725. }
  726. // Capture frame buffer
  727. glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, image);
  728. // Flip vertical
  729. for (i = 0, j = (int)height-1; i < (int)height/2; ++i, --j)
  730. {
  731. memcpy(swap_row, &image[i*width*3], width*3);
  732. memcpy(&image[i*width*3], &image[j*width*3], width*3);
  733. memcpy(&image[j*width*3], swap_row, width*3);
  734. }
  735. delete [] swap_row;
  736. comment_lenght = strlen(comment);
  737. colormap_type = 0;
  738. image_type = 2;
  739. colormap_index = 0;
  740. colormap_lenght = 0;
  741. colormap_bbp = 0;
  742. origin_x = origin_y = 0;
  743. swidth = width;
  744. sheight = height;
  745. bpp = 24;
  746. desc_flags = 32;
  747. // Write TGA header
  748. fwrite(&comment_lenght, 1, 1, f);
  749. fwrite(&colormap_type, 1, 1, f);
  750. fwrite(&image_type, 1, 1, f);
  751. fwrite(&colormap_index, 2, 1, f);
  752. fwrite(&colormap_lenght, 2, 1, f);
  753. fwrite(&colormap_bbp, 1, 1, f);
  754. fwrite(&origin_x, 2, 1, f);
  755. fwrite(&origin_y, 2, 1, f);
  756. fwrite(&swidth, 2, 1, f);
  757. fwrite(&sheight, 2, 1, f);
  758. fwrite(&bpp, 1, 1, f);
  759. fwrite(&desc_flags, 1, 1, f);
  760. // Write comment
  761. fwrite(&comment, 1, comment_lenght, f);
  762. size = width * height * 3;
  763. for (i = 0; i < size; i += 3)
  764. {
  765. tmp = image[i];
  766. image[i] = image[i + 2];
  767. image[i + 2] = tmp;
  768. }
  769. // Write image data
  770. if (fwrite(image, size, 1, f) < 1)
  771. {
  772. perror("glScreenShot> Disk write failed.\n");
  773. fclose(f);
  774. return;
  775. }
  776. fclose(f);
  777. delete [] image;
  778. printf("Took screenshot '%s'.\n", filename);
  779. #endif
  780. }
  781. int Texture::loadPNG(char *filename)
  782. {
  783. #ifdef USING_MTK_PNG
  784. FILE *f;
  785. unsigned char *image = NULL;
  786. unsigned char *image2 = NULL;
  787. unsigned int w, h;
  788. char type;
  789. int id = -1;
  790. f = fopen(filename, "rb");
  791. if (!f)
  792. {
  793. perror("Couldn't load file");
  794. }
  795. else if (!mtk_image__png_check(f))
  796. {
  797. mtk_image__png_load(f, &image, &w, &h, &type);
  798. type += 2;
  799. image2 = scaleBuffer(image, w, h, (type == 4) ? 4 : 3);
  800. if (image2)
  801. {
  802. image = image2;
  803. w = h = 256;
  804. }
  805. if (image)
  806. {
  807. id = loadBuffer(image, w, h,
  808. (type == 4) ? RGBA : RGB,
  809. (type == 4) ? 32 : 24);
  810. printf("%c", (id == -1) ? 'x' : 'o');
  811. fflush(stdout);
  812. delete [] image;
  813. }
  814. if (f)
  815. {
  816. fclose(f);
  817. }
  818. }
  819. if (id == -1)
  820. {
  821. printf("Texture::loadPNG> ERROR: Failed to load '%s'\n", filename);
  822. }
  823. return id;
  824. #else
  825. printf("ERROR: MTK PNG support not enabled in this build\n");
  826. return -1;
  827. #endif
  828. }
  829. int Texture::loadTGA(char *filename)
  830. {
  831. #ifdef USING_MTK_TGA
  832. FILE *f;
  833. unsigned char *image = NULL;
  834. unsigned char *image2 = NULL;
  835. unsigned int w, h;
  836. char type;
  837. int id = -1;
  838. f = fopen(filename, "rb");
  839. if (!f)
  840. {
  841. perror("Couldn't load file");
  842. }
  843. else if (!mtk_image__tga_check(f))
  844. {
  845. mtk_image__tga_load(f, &image, &w, &h, &type);
  846. type += 2;
  847. image2 = scaleBuffer(image, w, h, (type == 4) ? 4 : 3);
  848. if (image2)
  849. {
  850. image = image2;
  851. w = h = 256;
  852. }
  853. if (image)
  854. {
  855. id = loadBuffer(image, w, h,
  856. (type == 4) ? RGBA : RGB,
  857. (type == 4) ? 32 : 24);
  858. printf("%c", (id == -1) ? 'x' : 'o');
  859. fflush(stdout);
  860. delete [] image;
  861. }
  862. fclose(f);
  863. }
  864. if (id == -1)
  865. {
  866. printf("Texture::loadTGA> ERROR: Failed to load '%s'\n", filename);
  867. }
  868. return id;
  869. #else
  870. printf("ERROR: MTK TGA support not enabled in this build\n");
  871. return -1;
  872. #endif
  873. }
  874. ////////////////////////////////////////////////////////////
  875. // Private Accessors
  876. ////////////////////////////////////////////////////////////
  877. int Texture::nextPower(int seed)
  878. {
  879. int i;
  880. for(i = 1; i < seed; i *= 2)
  881. ;
  882. return i;
  883. }
  884. /* This code based off on gluScaleImage() */
  885. unsigned char *Texture::scaleBuffer(unsigned char *image,
  886. int width, int height,
  887. int components)
  888. {
  889. int i, j, k;
  890. float* tempin;
  891. float* tempout;
  892. float sx, sy;
  893. // int components = 3;
  894. unsigned char *timage;
  895. int original_height = height;
  896. int original_width = width;
  897. if (!image || !width || !height)
  898. {
  899. return NULL;
  900. }
  901. height = nextPower(height);
  902. width = nextPower(width);
  903. if (height > 256)
  904. height = 256;
  905. if (width > 256)
  906. width = 256;
  907. // Check to see if scaling is needed
  908. if (height == original_height && width == original_width)
  909. return NULL;
  910. //printf("%i\n", components);
  911. timage = new unsigned char[height * width * components];
  912. tempin = new float[original_width * original_height * components * sizeof(float)];
  913. tempout = new float[width * height * components * sizeof(float)];
  914. if (!tempout || !tempin)
  915. {
  916. if (tempout)
  917. delete [] tempout;
  918. if (tempin)
  919. delete [] tempin;
  920. printf("Oh shit out of memory!\n");
  921. return NULL;
  922. }
  923. // Copy user data to float format.
  924. for (i = 0; i < original_height * original_width * components; ++i)
  925. {
  926. tempin[i] = (float)image[i];
  927. }
  928. // Determine which filter to use by checking ratios.
  929. if (width > 1)
  930. {
  931. sx = (float)(original_width - 1) / (float)(width - 1);
  932. }
  933. else
  934. {
  935. sx = (float)(original_width - 1);
  936. }
  937. if (height > 1)
  938. {
  939. sy = (float)(original_height - 1) / (float) (height - 1);
  940. }
  941. else
  942. {
  943. sy = (float)(original_height - 1);
  944. }
  945. if (sx < 1.0 && sy < 1.0)
  946. {
  947. /* Magnify both width and height: use weighted sample of 4 pixels */
  948. int i0, i1, j0, j1;
  949. float alpha, beta;
  950. float* src00;
  951. float* src01;
  952. float* src10;
  953. float* src11;
  954. float s1, s2;
  955. float* dst;
  956. for(i = 0; i < height; ++i)
  957. {
  958. i0 = (int)(i * sy);
  959. i1 = i0 + 1;
  960. if (i1 >= original_height)
  961. {
  962. i1 = original_height - 1;
  963. }
  964. alpha = i * sy - i0;
  965. for (j = 0; j < width; ++j)
  966. {
  967. j0 = (int) (j * sx);
  968. j1 = j0 + 1;
  969. if (j1 >= original_width)
  970. {
  971. j1 = original_width - 1;
  972. }
  973. beta = j * sx - j0;
  974. /* Compute weighted average of pixels in rect (i0,j0)-(i1,j1) */
  975. src00 = tempin + (i0 * original_width + j0) * components;
  976. src01 = tempin + (i0 * original_width + j1) * components;
  977. src10 = tempin + (i1 * original_width + j0) * components;
  978. src11 = tempin + (i1 * original_width + j1) * components;
  979. dst = tempout + (i * width + j) * components;
  980. for (k = 0; k < components; ++k)
  981. {
  982. s1 = *src00++ * (1.0 - beta) + *src01++ * beta;
  983. s2 = *src10++ * (1.0 - beta) + *src11++ * beta;
  984. *dst++ = s1 * (1.0 - alpha) + s2 * alpha;
  985. }
  986. }
  987. }
  988. }
  989. else
  990. {
  991. /* Shrink width and/or height: use an unweighted box filter */
  992. int i0, i1;
  993. int j0, j1;
  994. int ii, jj;
  995. float sum;
  996. float* dst;
  997. for (i = 0; i < height; ++i)
  998. {
  999. i0 = (int) (i * sy);
  1000. i1 = i0 + 1;
  1001. if (i1 >= original_height)
  1002. {
  1003. i1 = original_height - 1;
  1004. }
  1005. for (j = 0; j < width; ++j)
  1006. {
  1007. j0 = (int) (j * sx);
  1008. j1 = j0 + 1;
  1009. if (j1 >= original_width)
  1010. {
  1011. j1 = original_width - 1;
  1012. }
  1013. dst = tempout + (i * width + j) * components;
  1014. /* Compute average of pixels in the rectangle (i0,j0)-(i1,j1) */
  1015. for (k = 0; k < components; ++k)
  1016. {
  1017. sum = 0.0;
  1018. for (ii = i0; ii <= i1; ++ii)
  1019. {
  1020. for (jj = j0; jj <= j1; ++jj)
  1021. {
  1022. sum += *(tempin + (ii * original_width + jj)
  1023. * components + k);
  1024. }
  1025. }
  1026. sum /= ( j1 - j0 + 1 ) * ( i1 - i0 + 1 );
  1027. *dst++ = sum;
  1028. }
  1029. }
  1030. }
  1031. }
  1032. // Copy to our results.
  1033. for( i = 0; i < height * width * components; ++i)
  1034. {
  1035. timage[i] = (unsigned char)tempout[i];
  1036. }
  1037. // Delete our temp buffers.
  1038. delete[] tempin;
  1039. delete[] tempout;
  1040. delete[] image;
  1041. return timage;
  1042. }
  1043. ////////////////////////////////////////////////////////////
  1044. // Private Mutators
  1045. ////////////////////////////////////////////////////////////