dgl.cc 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "math/mPoint.h"
  23. #include "graphics/TextureManager.h"
  24. #include "graphics/dgl.h"
  25. #include "graphics/color.h"
  26. #include "math/mPoint.h"
  27. #include "math/mRect.h"
  28. #include "graphics/gFont.h"
  29. #include "console/console.h"
  30. #include "math/mMatrix.h"
  31. #include "memory/frameAllocator.h"
  32. #include "debug/profiler.h"
  33. #include "string/unicode.h"
  34. namespace {
  35. ColorI sg_bitmapModulation(255, 255, 255, 255);
  36. ColorI sg_textAnchorColor(255, 255, 255, 255);
  37. ColorI sg_stackColor(255, 255, 255, 255);
  38. RectI sgCurrentClipRect;
  39. } // namespace {}
  40. //--------------------------------------------------------------------------
  41. void dglSetBitmapModulation(const ColorF& in_rColor)
  42. {
  43. ColorF c = in_rColor;
  44. c.clamp();
  45. sg_bitmapModulation = c;
  46. sg_textAnchorColor = sg_bitmapModulation;
  47. }
  48. void dglGetBitmapModulation(ColorF* color)
  49. {
  50. *color = sg_bitmapModulation;
  51. }
  52. void dglGetBitmapModulation(ColorI* color)
  53. {
  54. *color = sg_bitmapModulation;
  55. }
  56. void dglClearBitmapModulation()
  57. {
  58. sg_bitmapModulation.set(255, 255, 255, 255);
  59. }
  60. void dglSetTextAnchorColor(const ColorF& in_rColor)
  61. {
  62. ColorF c = in_rColor;
  63. c.clamp();
  64. sg_textAnchorColor = c;
  65. }
  66. //--------------------------------------------------------------------------
  67. void dglDrawBitmapStretchSR(TextureObject* texture,
  68. const RectI& dstRect,
  69. const RectI& srcRect,
  70. const U32 in_flip,
  71. F32 fSpin,
  72. bool bSilhouette)
  73. {
  74. AssertFatal(texture != NULL, "GSurface::drawBitmapStretchSR: NULL Handle");
  75. if(!dstRect.isValidRect())
  76. return;
  77. AssertFatal(srcRect.isValidRect() == true,
  78. "GSurface::drawBitmapStretchSR: routines assume normal rects");
  79. glDisable(GL_LIGHTING);
  80. glEnable(GL_TEXTURE_2D);
  81. glBindTexture(GL_TEXTURE_2D, texture->getGLTextureName());
  82. //glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  83. if (bSilhouette)
  84. {
  85. glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_BLEND);
  86. ColorF kModulationColor;
  87. dglGetBitmapModulation(&kModulationColor);
  88. glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, kModulationColor.address());
  89. }
  90. else
  91. {
  92. glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  93. }
  94. glEnable(GL_BLEND);
  95. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  96. F32 texLeft = F32(srcRect.point.x) / F32(texture->getTextureWidth());
  97. F32 texRight = F32(srcRect.point.x + srcRect.extent.x) / F32(texture->getTextureWidth());
  98. F32 texTop = F32(srcRect.point.y) / F32(texture->getTextureHeight());
  99. F32 texBottom = F32(srcRect.point.y + srcRect.extent.y) / F32(texture->getTextureHeight());
  100. Point2F scrPoints[4];
  101. if(fSpin == 0.0f)
  102. {
  103. scrPoints[0].x = (F32)dstRect.point.x;
  104. scrPoints[0].y = (F32)dstRect.point.y;
  105. scrPoints[1].x = (F32)(dstRect.point.x + dstRect.extent.x);
  106. scrPoints[1].y = (F32)dstRect.point.y;
  107. scrPoints[2].x = (F32)dstRect.point.x;
  108. scrPoints[2].y = (F32)(dstRect.point.y + dstRect.extent.y);
  109. scrPoints[3].x = (F32)(dstRect.point.x + dstRect.extent.x);
  110. scrPoints[3].y = (F32)(dstRect.point.y + dstRect.extent.y);
  111. //screenLeft = dstRect.point.x;
  112. //screenRight = dstRect.point.x + dstRect.extent.x;
  113. //screenTop = dstRect.point.y;
  114. //screenBottom = dstRect.point.y + dstRect.extent.y;
  115. }
  116. else
  117. {
  118. //WE NEED TO IMPLEMENT A FAST 2D ROTATION -- NOT THIS SLOWER 3D ROTATION
  119. MatrixF rotMatrix( EulerF( 0.0, 0.0, mDegToRad(fSpin) ) );
  120. Point3F offset( dstRect.point.x + dstRect.extent.x / 2.0f,
  121. dstRect.point.y + dstRect.extent.y / 2.0f, 0.0 );
  122. Point3F points[4];
  123. points[0] = Point3F(-dstRect.extent.x / 2.0f, dstRect.extent.y / 2.0f, 0.0);
  124. points[1] = Point3F( dstRect.extent.x / 2.0f, dstRect.extent.y / 2.0f, 0.0);
  125. points[2] = Point3F(-dstRect.extent.x / 2.0f, -dstRect.extent.y / 2.0f, 0.0);
  126. points[3] = Point3F( dstRect.extent.x / 2.0f, -dstRect.extent.y / 2.0f, 0.0);
  127. for( int i=0; i<4; i++ )
  128. {
  129. rotMatrix.mulP( points[i] );
  130. points[i] += offset;
  131. scrPoints[i].x = points[i].x;
  132. scrPoints[i].y = points[i].y;
  133. }
  134. }
  135. if(in_flip & GFlip_X)
  136. {
  137. F32 temp = texLeft;
  138. texLeft = texRight;
  139. texRight = temp;
  140. }
  141. if(in_flip & GFlip_Y)
  142. {
  143. F32 temp = texTop;
  144. texTop = texBottom;
  145. texBottom = temp;
  146. }
  147. glColor4ub(sg_bitmapModulation.red,
  148. sg_bitmapModulation.green,
  149. sg_bitmapModulation.blue,
  150. sg_bitmapModulation.alpha);
  151. #ifdef TORQUE_OS_IOS
  152. GLfloat verts[] = {
  153. (GLfloat)scrPoints[0].x, (GLfloat)scrPoints[0].y,
  154. (GLfloat)scrPoints[1].x, (GLfloat)scrPoints[1].y,
  155. (GLfloat)scrPoints[2].x, (GLfloat)scrPoints[2].y,
  156. (GLfloat)scrPoints[3].x, (GLfloat)scrPoints[3].y,
  157. };
  158. GLfloat texVerts[] = {
  159. (GLfloat)texLeft, (GLfloat)texTop,
  160. (GLfloat)texRight, (GLfloat)texTop,
  161. (GLfloat)texLeft, (GLfloat)texBottom,
  162. (GLfloat)texRight, (GLfloat)texBottom,
  163. };
  164. glDisableClientState(GL_COLOR_ARRAY);
  165. glDisableClientState(GL_POINT_SIZE_ARRAY_OES);
  166. glEnableClientState(GL_VERTEX_ARRAY);
  167. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  168. glVertexPointer(2, GL_FLOAT, 0, verts);
  169. glTexCoordPointer(2, GL_FLOAT, 0, texVerts);
  170. glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
  171. #else
  172. glBegin(GL_TRIANGLE_FAN);
  173. glTexCoord2f(texLeft, texBottom);
  174. glVertex2f(scrPoints[2].x, scrPoints[2].y);
  175. glTexCoord2f(texRight, texBottom);
  176. glVertex2f(scrPoints[3].x, scrPoints[3].y);
  177. glTexCoord2f(texRight, texTop);
  178. glVertex2f(scrPoints[1].x, scrPoints[1].y);
  179. glTexCoord2f(texLeft, texTop);
  180. glVertex2f(scrPoints[0].x, scrPoints[0].y);
  181. glEnd();
  182. #endif
  183. if (bSilhouette)
  184. {
  185. glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, ColorF(0.0f, 0.0f, 0.0f, 0.0f).address());
  186. }
  187. glDisable(GL_BLEND);
  188. glDisable(GL_TEXTURE_2D);
  189. }
  190. void dglDrawBitmap(TextureObject* texture, const Point2I& in_rAt, const U32 in_flip)
  191. {
  192. AssertFatal(texture != NULL, "GSurface::drawBitmap: NULL Handle");
  193. // All non-StretchSR bitmaps are transformed into StretchSR calls...
  194. //
  195. RectI subRegion(0, 0,
  196. texture->getBitmapWidth(),
  197. texture->getBitmapHeight());
  198. RectI stretch(in_rAt.x, in_rAt.y,
  199. texture->getBitmapWidth(),
  200. texture->getBitmapHeight());
  201. dglDrawBitmapStretchSR(texture,
  202. stretch,
  203. subRegion,
  204. in_flip);
  205. }
  206. void dglDrawBitmapTile(TextureObject* texture, const RectI& dstRect, const U32 in_flip, F32 fSpin, bool bSilhouette)
  207. {
  208. AssertFatal(texture != NULL, "GSurface::drawBitmapTile: NULL Handle");
  209. // since the texture coords are calculated from the texture sub-rect we pass in,
  210. // but no actual indexing of that sub-rect happens, we can pass any texture coords
  211. // that we like to dglDrawBitmapSR(). We use this to force tiling.
  212. RectI subregion(0,0, dstRect.extent.x, dstRect.extent.y);
  213. dglDrawBitmapStretchSR(texture, dstRect, subregion, in_flip, fSpin, bSilhouette);
  214. }
  215. void dglDrawBitmapStretch(TextureObject* texture, const RectI& dstRect, const U32 in_flip, F32 fSpin, bool bSilhouette)
  216. {
  217. AssertFatal(texture != NULL, "GSurface::drawBitmapStretch: NULL Handle");
  218. AssertFatal(dstRect.isValidRect() == true,
  219. "GSurface::drawBitmapStretch: routines assume normal rects");
  220. RectI subRegion(0, 0,
  221. texture->getBitmapWidth(),
  222. texture->getBitmapHeight());
  223. dglDrawBitmapStretchSR(texture,
  224. dstRect,
  225. subRegion,
  226. in_flip,
  227. fSpin,
  228. bSilhouette);
  229. }
  230. void dglDrawBitmapSR(TextureObject *texture, const Point2I& in_rAt, const RectI& srcRect, const U32 in_flip)
  231. {
  232. AssertFatal(texture != NULL, "GSurface::drawBitmapSR: NULL Handle");
  233. AssertFatal(srcRect.isValidRect() == true,
  234. "GSurface::drawBitmapSR: routines assume normal rects");
  235. RectI stretch(in_rAt.x, in_rAt.y,
  236. srcRect.len_x(),
  237. srcRect.len_y());
  238. dglDrawBitmapStretchSR(texture,
  239. stretch,
  240. srcRect,
  241. in_flip);
  242. }
  243. U32 dglDrawText(GFont* font,
  244. const Point2I& ptDraw,
  245. const UTF16* in_string,
  246. const ColorI* colorTable,
  247. const U32 maxColorIndex,
  248. F32 rot)
  249. {
  250. return dglDrawTextN(font, ptDraw, in_string, dStrlen(in_string), colorTable, maxColorIndex, rot);
  251. }
  252. U32 dglDrawText(GFont* font,
  253. const Point2I& ptDraw,
  254. const UTF8* in_string,
  255. const ColorI* colorTable,
  256. const U32 maxColorIndex,
  257. F32 rot)
  258. {
  259. // Just a note - dStrlen(utf8) isn't strictly correct but it's guaranteed to be
  260. // as long or longer than the real length. dglDrawTextN fails gracefully
  261. // if you specify overlong, so this is ok.
  262. return dglDrawTextN(font, ptDraw, in_string, dStrlen((const UTF8 *) in_string), colorTable, maxColorIndex, rot);
  263. }
  264. struct TextVertex
  265. {
  266. Point2F p;
  267. Point2F t;
  268. ColorI c;
  269. TextVertex() { set( 0.0f, 0.0f, 0.0f, 0.0f, ColorI(0, 0, 0) ); }
  270. void set(F32 x, F32 y, F32 tx, F32 ty, ColorI color)
  271. {
  272. p.x = x;
  273. p.y = y;
  274. t.x = tx;
  275. t.y = ty;
  276. c = color;
  277. }
  278. };
  279. //------------------------------------------------------------------------------
  280. U32 dglDrawTextN(GFont* font,
  281. const Point2I& ptDraw,
  282. const UTF8* in_string,
  283. U32 n,
  284. const ColorI* colorTable,
  285. const U32 maxColorIndex,
  286. F32 rot)
  287. {
  288. PROFILE_START(DrawText_UTF8);
  289. U32 len = dStrlen(in_string) + 1;
  290. FrameTemp<UTF16> ubuf(len);
  291. convertUTF8toUTF16(in_string, ubuf, len);
  292. U32 tmp = dglDrawTextN(font, ptDraw, ubuf, n, colorTable, maxColorIndex, rot);
  293. PROFILE_END();
  294. return tmp;
  295. }
  296. //-----------------------------------------------------------------------------
  297. #ifdef TORQUE_OS_IOS
  298. U32 dglDrawTextN(GFont* font,
  299. const Point2I& ptDraw,
  300. const UTF16* in_string,
  301. U32 n,
  302. const ColorI* colorTable,
  303. const U32 maxColorIndex,
  304. F32 rot)
  305. {
  306. // return on zero length strings
  307. if( n < 1 )
  308. return ptDraw.x;
  309. PROFILE_START(DrawText);
  310. MatrixF rotMatrix( EulerF( 0.0, 0.0, mDegToRad( rot ) ) );
  311. Point3F offset( ptDraw.x, ptDraw.y, 0.0 );
  312. Point3F points[4];
  313. U32 nCharCount = 0;
  314. Point2I pt;
  315. UTF16 c;
  316. pt.x = 0;
  317. ColorI currentColor;
  318. S32 currentPt = 0;
  319. TextureObject *lastTexture = NULL;
  320. currentColor = sg_bitmapModulation;
  321. FrameTemp<TextVertex> vert(4*n);
  322. glDisable(GL_LIGHTING);
  323. glEnable(GL_TEXTURE_2D);
  324. glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  325. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  326. glEnable(GL_BLEND);
  327. //Luma: Optimise by setting states once before inner loop
  328. glEnableClientState ( GL_VERTEX_ARRAY );
  329. glEnableClientState ( GL_COLOR_ARRAY );
  330. glEnableClientState ( GL_TEXTURE_COORD_ARRAY );
  331. glVertexPointer ( 2, GL_FLOAT, sizeof(TextVertex), &(vert[0].p) );
  332. glColorPointer ( 4, GL_UNSIGNED_BYTE, sizeof(TextVertex), &(vert[0].c) );
  333. glTexCoordPointer ( 2, GL_FLOAT, sizeof(TextVertex), &(vert[0].t) );
  334. // first build the point, color, and coord arrays
  335. U32 i;
  336. for(i = 0,c = in_string[i];in_string[i] && i < n;i++,c = in_string[i])
  337. {
  338. nCharCount++;
  339. if(nCharCount > n)
  340. break;
  341. // We have to do a little dance here since \t = 0x9, \n = 0xa, and \r = 0xd
  342. if ((c >= 1 && c <= 7) ||
  343. (c >= 11 && c <= 12) ||
  344. (c == 14))
  345. {
  346. // Color code
  347. if (colorTable)
  348. {
  349. static U8 remap[15] =
  350. {
  351. 0x0, // 0 special null terminator
  352. 0x0, // 1 ascii start-of-heading??
  353. 0x1,
  354. 0x2,
  355. 0x3,
  356. 0x4,
  357. 0x5,
  358. 0x6,
  359. 0x0, // 8 special backspace
  360. 0x0, // 9 special tab
  361. 0x0, // a special \n
  362. 0x7,
  363. 0x8,
  364. 0x0, // a special \r
  365. 0x9
  366. };
  367. U8 remapped = remap[c];
  368. // Ignore if the color is greater than the specified max index:
  369. if ( remapped <= maxColorIndex )
  370. {
  371. const ColorI &clr = colorTable[remapped];
  372. sg_bitmapModulation = clr;
  373. currentColor = clr;
  374. }
  375. }
  376. continue;
  377. }
  378. // reset color?
  379. if ( c == 15 )
  380. {
  381. currentColor = sg_textAnchorColor;
  382. sg_bitmapModulation = sg_textAnchorColor;
  383. continue;
  384. }
  385. // push color:
  386. if ( c == 16 )
  387. {
  388. sg_stackColor = sg_bitmapModulation;
  389. continue;
  390. }
  391. // pop color:
  392. if ( c == 17 )
  393. {
  394. currentColor = sg_stackColor;
  395. sg_bitmapModulation = sg_stackColor;
  396. continue;
  397. }
  398. // Tab character
  399. if ( c == dT('\t') )
  400. {
  401. const PlatformFont::CharInfo &ci = font->getCharInfo( dT(' ') );
  402. pt.x += ci.xIncrement * GFont::TabWidthInSpaces;
  403. continue;
  404. }
  405. if( !font->isValidChar( c ) )
  406. continue;
  407. const PlatformFont::CharInfo &ci = font->getCharInfo(c);
  408. if(ci.bitmapIndex == -1)
  409. {
  410. pt.x += ci.xOrigin + ci.xIncrement;
  411. continue;
  412. }
  413. TextureObject *newObj = font->getTextureHandle(ci.bitmapIndex);
  414. if(newObj != lastTexture)
  415. {
  416. if(currentPt)
  417. {
  418. glBindTexture(GL_TEXTURE_2D, lastTexture->getGLTextureName());
  419. //Luma: More optimal rendering
  420. for (S32 i=0; i<currentPt; i+=4)
  421. {
  422. glDrawArrays(GL_TRIANGLE_STRIP, i, 4);
  423. }
  424. currentPt = 0;
  425. }
  426. lastTexture = newObj;
  427. }
  428. if(ci.width != 0 && ci.height != 0)
  429. {
  430. pt.y = font->getBaseline() - ci.yOrigin;
  431. pt.x += ci.xOrigin;
  432. F32 texLeft = F32(ci.xOffset) / F32(lastTexture->getTextureWidth());
  433. F32 texRight = F32(ci.xOffset + ci.width) / F32(lastTexture->getTextureWidth());
  434. F32 texTop = F32(ci.yOffset) / F32(lastTexture->getTextureHeight());
  435. F32 texBottom = F32(ci.yOffset + ci.height) / F32(lastTexture->getTextureHeight());
  436. F32 screenLeft = pt.x;
  437. F32 screenRight = pt.x + ci.width;
  438. F32 screenTop = pt.y;
  439. F32 screenBottom = pt.y + ci.height;
  440. points[0] = Point3F(screenLeft, screenTop, 0.0);
  441. points[1] = Point3F(screenRight, screenTop, 0.0);
  442. points[2] = Point3F( screenLeft, screenBottom, 0.0);
  443. points[3] = Point3F( screenRight, screenBottom, 0.0);
  444. for( int i=0; i<4; i++ )
  445. {
  446. rotMatrix.mulP( points[i] );
  447. points[i] += offset;
  448. }
  449. vert[currentPt++].set(points[0].x, points[0].y, texLeft, texTop, currentColor);
  450. vert[currentPt++].set(points[1].x, points[1].y, texRight, texTop, currentColor);
  451. vert[currentPt++].set(points[2].x, points[2].y, texLeft, texBottom, currentColor);
  452. vert[currentPt++].set(points[3].x, points[3].y, texRight, texBottom, currentColor);
  453. pt.x += ci.xIncrement - ci.xOrigin;
  454. }
  455. else
  456. pt.x += ci.xIncrement;
  457. }
  458. if(currentPt)
  459. {
  460. //Luma: More optimal rendering
  461. glBindTexture(GL_TEXTURE_2D, lastTexture->getGLTextureName());
  462. for (S32 i=0; i<currentPt; i+=4)
  463. {
  464. glDrawArrays(GL_TRIANGLE_STRIP, i, 4);
  465. }
  466. }
  467. glDisableClientState ( GL_VERTEX_ARRAY );
  468. glDisableClientState ( GL_COLOR_ARRAY );
  469. glDisableClientState ( GL_TEXTURE_COORD_ARRAY );
  470. glDisable(GL_BLEND);
  471. glDisable(GL_TEXTURE_2D);
  472. pt.x += ptDraw.x; // DAW: Account for the fact that we removed the drawing point from the text start at the beginning.
  473. AssertFatal(pt.x >= ptDraw.x, "How did this happen?");
  474. PROFILE_END();
  475. return pt.x - ptDraw.x;
  476. }
  477. #else
  478. U32 dglDrawTextN(GFont* font,
  479. const Point2I& ptDraw,
  480. const UTF16* in_string,
  481. U32 n,
  482. const ColorI* colorTable,
  483. const U32 maxColorIndex,
  484. F32 rot)
  485. {
  486. // return on zero length strings
  487. if( n < 1 )
  488. return ptDraw.x;
  489. PROFILE_START(DrawText);
  490. MatrixF rotMatrix( EulerF( 0.0, 0.0, mDegToRad( rot ) ) );
  491. Point3F offset( (F32)ptDraw.x, (F32)ptDraw.y, 0.0f );
  492. Point3F points[4];
  493. U32 nCharCount = 0;
  494. Point2I pt;
  495. UTF16 c;
  496. pt.x = 0;
  497. ColorI currentColor;
  498. S32 currentPt = 0;
  499. TextureObject *lastTexture = NULL;
  500. currentColor = sg_bitmapModulation;
  501. FrameTemp<TextVertex> vert(4*n);
  502. glDisable(GL_LIGHTING);
  503. glEnable(GL_TEXTURE_2D);
  504. glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  505. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  506. glEnable(GL_BLEND);
  507. glEnableClientState ( GL_VERTEX_ARRAY );
  508. glVertexPointer ( 2, GL_FLOAT, sizeof(TextVertex), &(vert[0].p) );
  509. glEnableClientState ( GL_COLOR_ARRAY );
  510. glColorPointer ( 4, GL_UNSIGNED_BYTE, sizeof(TextVertex), &(vert[0].c) );
  511. glEnableClientState ( GL_TEXTURE_COORD_ARRAY );
  512. glTexCoordPointer ( 2, GL_FLOAT, sizeof(TextVertex), &(vert[0].t) );
  513. // first build the point, color, and coord arrays
  514. U32 i;
  515. for(i = 0,c = in_string[i];in_string[i] && i < n;i++,c = in_string[i])
  516. {
  517. nCharCount++;
  518. if(nCharCount > n)
  519. break;
  520. // We have to do a little dance here since \t = 0x9, \n = 0xa, and \r = 0xd
  521. if ((c >= 1 && c <= 7) ||
  522. (c >= 11 && c <= 12) ||
  523. (c == 14))
  524. {
  525. // Color code
  526. if (colorTable)
  527. {
  528. static U8 remap[15] =
  529. {
  530. 0x0, // 0 special null terminator
  531. 0x0, // 1 ascii start-of-heading??
  532. 0x1,
  533. 0x2,
  534. 0x3,
  535. 0x4,
  536. 0x5,
  537. 0x6,
  538. 0x0, // 8 special backspace
  539. 0x0, // 9 special tab
  540. 0x0, // a special \n
  541. 0x7,
  542. 0x8,
  543. 0x0, // a special \r
  544. 0x9
  545. };
  546. U8 remapped = remap[c];
  547. // Ignore if the color is greater than the specified max index:
  548. if ( remapped <= maxColorIndex )
  549. {
  550. const ColorI &clr = colorTable[remapped];
  551. sg_bitmapModulation = clr;
  552. currentColor = clr;
  553. }
  554. }
  555. continue;
  556. }
  557. // reset color?
  558. if ( c == 15 )
  559. {
  560. currentColor = sg_textAnchorColor;
  561. sg_bitmapModulation = sg_textAnchorColor;
  562. continue;
  563. }
  564. // push color:
  565. if ( c == 16 )
  566. {
  567. sg_stackColor = sg_bitmapModulation;
  568. continue;
  569. }
  570. // pop color:
  571. if ( c == 17 )
  572. {
  573. currentColor = sg_stackColor;
  574. sg_bitmapModulation = sg_stackColor;
  575. continue;
  576. }
  577. // Tab character
  578. if ( c == dT('\t') )
  579. {
  580. const PlatformFont::CharInfo &ci = font->getCharInfo( dT(' ') );
  581. pt.x += ci.xIncrement * GFont::TabWidthInSpaces;
  582. continue;
  583. }
  584. if( !font->isValidChar( c ) )
  585. continue;
  586. const PlatformFont::CharInfo &ci = font->getCharInfo(c);
  587. if(ci.bitmapIndex == -1)
  588. {
  589. pt.x += ci.xOrigin + ci.xIncrement;
  590. continue;
  591. }
  592. TextureObject *newObj = font->getTextureHandle(ci.bitmapIndex);
  593. if(newObj != lastTexture)
  594. {
  595. if(currentPt)
  596. {
  597. glBindTexture(GL_TEXTURE_2D, lastTexture->getGLTextureName());
  598. glDrawArrays( GL_QUADS, 0, currentPt );
  599. currentPt = 0;
  600. }
  601. lastTexture = newObj;
  602. }
  603. if(ci.width != 0 && ci.height != 0)
  604. {
  605. pt.y = font->getBaseline() - ci.yOrigin;
  606. pt.x += ci.xOrigin;
  607. F32 texLeft = F32(ci.xOffset) / F32(lastTexture->getTextureWidth());
  608. F32 texRight = F32(ci.xOffset + ci.width) / F32(lastTexture->getTextureWidth());
  609. F32 texTop = F32(ci.yOffset) / F32(lastTexture->getTextureHeight());
  610. F32 texBottom = F32(ci.yOffset + ci.height) / F32(lastTexture->getTextureHeight());
  611. F32 screenLeft = (F32)pt.x;
  612. F32 screenRight = (F32)(pt.x + ci.width);
  613. F32 screenTop = (F32)pt.y;
  614. F32 screenBottom = (F32)(pt.y + ci.height);
  615. points[0] = Point3F(screenLeft, screenBottom, 0.0);
  616. points[1] = Point3F(screenRight, screenBottom, 0.0);
  617. points[2] = Point3F( screenRight, screenTop, 0.0);
  618. points[3] = Point3F( screenLeft, screenTop, 0.0);
  619. for( int i=0; i<4; i++ )
  620. {
  621. rotMatrix.mulP( points[i] );
  622. points[i] += offset;
  623. }
  624. vert[currentPt++].set(points[0].x, points[0].y, texLeft, texBottom, currentColor);
  625. vert[currentPt++].set(points[1].x, points[1].y, texRight, texBottom, currentColor);
  626. vert[currentPt++].set(points[2].x, points[2].y, texRight, texTop, currentColor);
  627. vert[currentPt++].set(points[3].x, points[3].y, texLeft, texTop, currentColor);
  628. pt.x += ci.xIncrement - ci.xOrigin;
  629. }
  630. else
  631. pt.x += ci.xIncrement;
  632. }
  633. if(currentPt)
  634. {
  635. glBindTexture(GL_TEXTURE_2D, lastTexture->getGLTextureName());
  636. glDrawArrays( GL_QUADS, 0, currentPt );
  637. }
  638. glDisableClientState ( GL_VERTEX_ARRAY );
  639. glDisableClientState ( GL_COLOR_ARRAY );
  640. glDisableClientState ( GL_TEXTURE_COORD_ARRAY );
  641. glDisable(GL_BLEND);
  642. glDisable(GL_TEXTURE_2D);
  643. pt.x += ptDraw.x; // DAW: Account for the fact that we removed the drawing point from the text start at the beginning.
  644. AssertFatal(pt.x >= ptDraw.x, "How did this happen?");
  645. PROFILE_END();
  646. return pt.x - ptDraw.x;
  647. }
  648. #endif
  649. // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- //
  650. // Drawing primitives
  651. void dglDrawLine(S32 x1, S32 y1, S32 x2, S32 y2, const ColorI &color)
  652. {
  653. glEnable(GL_BLEND);
  654. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  655. glDisable(GL_TEXTURE_2D);
  656. glColor4ub(color.red, color.green, color.blue, color.alpha);
  657. #ifdef TORQUE_OS_IOS
  658. GLfloat verts[] = {
  659. (GLfloat)(x1 + 0.5f), (GLfloat)(y1 + 0.5f),
  660. (GLfloat)(x2 + 0.5f), (GLfloat)(y2 + 0.5f),
  661. };
  662. glVertexPointer(2, GL_FLOAT, 0, verts );
  663. glDrawArrays(GL_LINES, 0, 2);//draw last two
  664. #else
  665. glBegin(GL_LINES);
  666. glVertex2f((F32)x1 + 0.5f, (F32)y1 + 0.5f);
  667. glVertex2f((F32)x2 + 0.5f, (F32)y2 + 0.5f);
  668. glEnd();
  669. //glBegin(GL_POINTS);
  670. //glVertex2f((F32)x2 + 0.5, (F32)y2 + 0.5);
  671. //glEnd();
  672. #endif
  673. }
  674. void dglDrawLine(const Point2I &startPt, const Point2I &endPt, const ColorI &color)
  675. {
  676. dglDrawLine(startPt.x, startPt.y, endPt.x, endPt.y, color);
  677. }
  678. void dglDrawRect(const Point2I &upperL, const Point2I &lowerR, const ColorI &color, const float &lineWidth)
  679. {
  680. glEnable(GL_BLEND);
  681. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  682. glDisable(GL_TEXTURE_2D);
  683. glLineWidth(lineWidth);
  684. glColor4ub(color.red, color.green, color.blue, color.alpha);
  685. #ifdef TORQUE_OS_IOS
  686. GLfloat verts[] = {
  687. (GLfloat)(upperL.x), (GLfloat)(upperL.y),
  688. (GLfloat)(lowerR.x), (GLfloat)(upperL.y),
  689. (GLfloat)(lowerR.x), (GLfloat)(lowerR.y),
  690. (GLfloat)(upperL.x), (GLfloat)(lowerR.y),
  691. };
  692. glVertexPointer(2, GL_FLOAT, 0, verts );
  693. glDrawArrays(GL_LINE_LOOP, 0, 4 );//draw last two
  694. #else
  695. glBegin(GL_LINE_LOOP);
  696. glVertex2f((F32)upperL.x + 0.5f, (F32)upperL.y + 0.5f);
  697. glVertex2f((F32)lowerR.x + 0.5f, (F32)upperL.y + 0.5f);
  698. glVertex2f((F32)lowerR.x + 0.5f, (F32)lowerR.y + 0.5f);
  699. glVertex2f((F32)upperL.x + 0.5f, (F32)lowerR.y + 0.5f);
  700. glEnd();
  701. #endif
  702. }
  703. // the fill convention for lined rects is that they outline the rectangle border of the
  704. // filled region specified.
  705. void dglDrawRect(const RectI &rect, const ColorI &color, const float &lineWidth)
  706. {
  707. Point2I lowerR(rect.point.x + rect.extent.x - 1, rect.point.y + rect.extent.y - 1);
  708. dglDrawRect(rect.point, lowerR, color, lineWidth);
  709. }
  710. // the fill convention says that pixel at upperL will be filled and
  711. // that pixel at lowerR will NOT be filled.
  712. void dglDrawRectFill(const Point2I &upperL, const Point2I &lowerR, const ColorI &color)
  713. {
  714. glEnable(GL_BLEND);
  715. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  716. glDisable(GL_TEXTURE_2D);
  717. glColor4ub(color.red, color.green, color.blue, color.alpha);
  718. #ifdef TORQUE_OS_IOS
  719. GLfloat vertices[] = {
  720. (GLfloat)upperL.x, (GLfloat)upperL.y,
  721. (GLfloat)upperL.x, (GLfloat)lowerR.y,
  722. (GLfloat)lowerR.x, (GLfloat)upperL.y,
  723. (GLfloat)lowerR.x, (GLfloat)lowerR.y,
  724. };
  725. glVertexPointer(2, GL_FLOAT, 0, vertices);
  726. glEnableClientState(GL_VERTEX_ARRAY);
  727. glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
  728. #else
  729. glRecti((S32)upperL.x, (S32)upperL.y, (S32)lowerR.x, (S32)lowerR.y);
  730. #endif
  731. }
  732. void dglDrawRectFill(const RectI &rect, const ColorI &color)
  733. {
  734. Point2I lowerR(rect.point.x + rect.extent.x, rect.point.y + rect.extent.y);
  735. dglDrawRectFill(rect.point, lowerR, color);
  736. }
  737. void dglDraw2DSquare( const Point2F &screenPoint, F32 width, F32 spinAngle )
  738. {
  739. width *= 0.5;
  740. MatrixF rotMatrix( EulerF( 0.0, 0.0, spinAngle ) );
  741. Point3F offset( screenPoint.x, screenPoint.y, 0.0 );
  742. Point3F points[4];
  743. points[0] = Point3F(-width, -width, 0.0);
  744. points[1] = Point3F(-width, width, 0.0);
  745. points[2] = Point3F( width, width, 0.0);
  746. points[3] = Point3F( width, -width, 0.0);
  747. for( int i=0; i<4; i++ )
  748. {
  749. rotMatrix.mulP( points[i] );
  750. points[i] += offset;
  751. }
  752. #ifdef TORQUE_OS_IOS
  753. GLfloat verts[] = {
  754. 0.0, 0.0,
  755. 1.0, 0.0,
  756. 0.0, 1.0,//may need to switch last two
  757. 1.0, 1.0,
  758. };
  759. GLfloat texVerts[] = {
  760. (GLfloat)points[0].x, (GLfloat)points[0].y,
  761. (GLfloat)points[1].x, (GLfloat)points[1].y,
  762. (GLfloat)points[3].x, (GLfloat)points[3].y,
  763. (GLfloat)points[2].x, (GLfloat)points[2].y,
  764. };
  765. glDisableClientState(GL_COLOR_ARRAY);
  766. glDisableClientState(GL_POINT_SIZE_ARRAY_OES);
  767. glEnableClientState(GL_VERTEX_ARRAY);
  768. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  769. glVertexPointer(2, GL_FLOAT, 0, verts);
  770. glTexCoordPointer(2, GL_FLOAT, 0, texVerts);
  771. glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
  772. #else
  773. glBegin(GL_TRIANGLE_FAN);
  774. glTexCoord2f(0.0, 0.0);
  775. glVertex2fv(points[0]);
  776. glTexCoord2f(0.0, 1.0);
  777. glVertex2fv(points[1]);
  778. glTexCoord2f(1.0, 1.0);
  779. glVertex2fv(points[2]);
  780. glTexCoord2f(1.0, 0.0);
  781. glVertex2fv(points[3]);
  782. glEnd();
  783. #endif
  784. }
  785. void dglDrawBillboard( const Point3F &position, F32 width, F32 spinAngle )
  786. {
  787. MatrixF modelview;
  788. dglGetModelview( &modelview );
  789. modelview.transpose();
  790. width *= 0.5;
  791. Point3F points[4];
  792. points[0] = Point3F(-width, 0.0, -width);
  793. points[1] = Point3F(-width, 0.0, width);
  794. points[2] = Point3F( width, 0.0, width);
  795. points[3] = Point3F( width, 0.0, -width);
  796. MatrixF rotMatrix( EulerF( 0.0, spinAngle, 0.0 ) );
  797. for( int i=0; i<4; i++ )
  798. {
  799. rotMatrix.mulP( points[i] );
  800. modelview.mulP( points[i] );
  801. points[i] += position;
  802. }
  803. #ifdef TORQUE_OS_IOS
  804. GLfloat verts[] = {
  805. 0.0, 1.0,
  806. 0.0, 0.0,
  807. 1.0, 1.0,//may need to switch last two
  808. 1.0, 0.0,
  809. };
  810. GLfloat texVerts[] = {
  811. (GLfloat)points[0].x, (GLfloat)points[0].y,
  812. (GLfloat)points[1].x, (GLfloat)points[1].y,
  813. (GLfloat)points[3].x, (GLfloat)points[3].y,
  814. (GLfloat)points[2].x, (GLfloat)points[2].y,
  815. };
  816. glDisableClientState(GL_COLOR_ARRAY);
  817. glDisableClientState(GL_POINT_SIZE_ARRAY_OES);
  818. glEnableClientState(GL_VERTEX_ARRAY);
  819. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  820. glVertexPointer(2, GL_FLOAT, 0, verts);
  821. glTexCoordPointer(2, GL_FLOAT, 0, texVerts);
  822. glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
  823. #else
  824. glBegin(GL_TRIANGLE_FAN);
  825. glTexCoord2f(0.0, 1.0);
  826. glVertex3fv(points[0]);
  827. glTexCoord2f(0.0, 0.0);
  828. glVertex3fv(points[1]);
  829. glTexCoord2f(1.0, 0.0);
  830. glVertex3fv(points[2]);
  831. glTexCoord2f(1.0, 1.0);
  832. glVertex3fv(points[3]);
  833. glEnd();
  834. #endif
  835. }
  836. void dglWireCube(const Point3F & extent, const Point3F & center)
  837. {
  838. static Point3F cubePoints[8] =
  839. {
  840. Point3F(-1, -1, -1), Point3F(-1, -1, 1), Point3F(-1, 1, -1), Point3F(-1, 1, 1),
  841. Point3F( 1, -1, -1), Point3F( 1, -1, 1), Point3F( 1, 1, -1), Point3F( 1, 1, 1)
  842. };
  843. static U32 cubeFaces[6][4] =
  844. {
  845. { 0, 2, 6, 4 }, { 0, 2, 3, 1 }, { 0, 1, 5, 4 },
  846. { 3, 2, 6, 7 }, { 7, 6, 4, 5 }, { 3, 7, 5, 1 }
  847. };
  848. glDisable(GL_CULL_FACE);
  849. #ifdef TORQUE_OS_IOS
  850. //PUAP -Mat untested
  851. for (S32 i = 0; i < 6; i++)
  852. {
  853. GLfloat verts[] = {
  854. 0, 0, 0,
  855. 0, 0, 0,
  856. 0, 0, 0,
  857. 0, 0, 0,
  858. };
  859. int j = 0;
  860. for(int vert = 0; vert < 4; vert++)
  861. {
  862. int idx = cubeFaces[i][vert];
  863. //glVertex3f(cubePoints[idx].x * extent.x + center.x,
  864. //cubePoints[idx].y * extent.y + center.y,
  865. //cubePoints[idx].z * extent.z + center.z);
  866. verts[j] = cubePoints[idx].x * extent.x + center.x;
  867. verts[++j] = cubePoints[idx].y * extent.y + center.y;
  868. verts[++j] = cubePoints[idx].z * extent.z + center.z;
  869. }
  870. glVertexPointer(3, GL_FLOAT, 0, verts);
  871. glDrawArrays(GL_LINE_LOOP, 0, 4);
  872. }
  873. #else
  874. for (S32 i = 0; i < 6; i++)
  875. {
  876. glBegin(GL_LINE_LOOP);
  877. for(int vert = 0; vert < 4; vert++)
  878. {
  879. int idx = cubeFaces[i][vert];
  880. glVertex3f(cubePoints[idx].x * extent.x + center.x,
  881. cubePoints[idx].y * extent.y + center.y,
  882. cubePoints[idx].z * extent.z + center.z);
  883. }
  884. glEnd();
  885. }
  886. #endif
  887. }
  888. void dglSolidCube(const Point3F & extent, const Point3F & center)
  889. {
  890. static Point3F cubePoints[8] =
  891. {
  892. Point3F(-1, -1, -1), Point3F(-1, -1, 1), Point3F(-1, 1, -1), Point3F(-1, 1, 1),
  893. Point3F( 1, -1, -1), Point3F( 1, -1, 1), Point3F( 1, 1, -1), Point3F( 1, 1, 1)
  894. };
  895. static U32 cubeFaces[6][4] =
  896. {
  897. { 0, 2, 6, 4 }, { 0, 2, 3, 1 }, { 0, 1, 5, 4 },
  898. { 3, 2, 6, 7 }, { 7, 6, 4, 5 }, { 3, 7, 5, 1 }
  899. };
  900. #ifdef TORQUE_OS_IOS
  901. //PUAP -Mat untested
  902. for (S32 i = 0; i < 6; i++)
  903. {
  904. GLfloat verts[] = {
  905. 0, 0, 0,
  906. 0, 0, 0,
  907. 0, 0, 0,
  908. 0, 0, 0,
  909. };
  910. int j = 0;
  911. for(int vert = 0; vert < 4; vert++)
  912. {
  913. int idx = cubeFaces[i][vert];
  914. //glVertex3f(cubePoints[idx].x * extent.x + center.x,
  915. //cubePoints[idx].y * extent.y + center.y,
  916. //cubePoints[idx].z * extent.z + center.z);
  917. verts[j] = cubePoints[idx].x * extent.x + center.x;
  918. verts[++j] = cubePoints[idx].y * extent.y + center.y;
  919. verts[++j] = cubePoints[idx].z * extent.z + center.z;
  920. }
  921. glVertexPointer(3, GL_FLOAT, 0, verts);
  922. glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
  923. }
  924. #else
  925. for (S32 i = 0; i < 6; i++)
  926. {
  927. glBegin(GL_TRIANGLE_FAN);
  928. for(int vert = 0; vert < 4; vert++)
  929. {
  930. int idx = cubeFaces[i][vert];
  931. glVertex3f(cubePoints[idx].x * extent.x + center.x,
  932. cubePoints[idx].y * extent.y + center.y,
  933. cubePoints[idx].z * extent.z + center.z);
  934. }
  935. glEnd();
  936. }
  937. #endif
  938. }
  939. void dglSetClipRect(const RectI &clipRect)
  940. {
  941. glMatrixMode(GL_PROJECTION);
  942. glLoadIdentity();
  943. U32 screenHeight = Platform::getWindowSize().y;
  944. #ifdef TORQUE_OS_IOS
  945. glOrthof(clipRect.point.x, clipRect.point.x + clipRect.extent.x,
  946. clipRect.extent.y, 0,
  947. 0, 1);
  948. #else
  949. glOrtho(clipRect.point.x, clipRect.point.x + clipRect.extent.x,
  950. clipRect.extent.y, 0,
  951. 0, 1);
  952. #endif
  953. glTranslatef(0.0f, (F32)-clipRect.point.y, 0.0f);
  954. glMatrixMode(GL_MODELVIEW);
  955. glLoadIdentity();
  956. glViewport(clipRect.point.x, screenHeight - (clipRect.point.y + clipRect.extent.y),
  957. clipRect.extent.x, clipRect.extent.y);
  958. sgCurrentClipRect = clipRect;
  959. }
  960. const RectI& dglGetClipRect()
  961. {
  962. return sgCurrentClipRect;
  963. }
  964. bool dglPointToScreen( Point3F &point3D, Point3F &screenPoint )
  965. {
  966. #ifdef TORQUE_OS_IOS
  967. GLfloat glMV[16];
  968. GLfloat glPR[16];
  969. GLint glVP[4];
  970. glGetFloatv(GL_PROJECTION_MATRIX, glPR);
  971. glGetFloatv(GL_MODELVIEW_MATRIX, glMV);
  972. RectI viewport;
  973. dglGetViewport(&viewport);
  974. glVP[0] = viewport.point.x;
  975. glVP[1] = viewport.point.y + viewport.extent.y;
  976. glVP[2] = viewport.extent.x;
  977. glVP[3] = -viewport.extent.y;
  978. MatrixF mv;
  979. dglGetModelview(&mv);
  980. MatrixF pr;
  981. dglGetProjection(&pr);
  982. F64 x, y, z;
  983. // PUAP -Mat untested
  984. int result = gluProject( point3D.x, point3D.y, point3D.z, (const F64 *)&glMV, (const F64 *)&glPR, (const GLint *)&glVP, &x, &y, &z );
  985. screenPoint.x = x;
  986. screenPoint.y = y;
  987. screenPoint.z = z;
  988. return (result == GL_TRUE);
  989. #else
  990. GLdouble glMV[16];
  991. GLdouble glPR[16];
  992. GLint glVP[4];
  993. glGetDoublev(GL_PROJECTION_MATRIX, glPR);
  994. glGetDoublev(GL_MODELVIEW_MATRIX, glMV);
  995. RectI viewport;
  996. dglGetViewport(&viewport);
  997. glVP[0] = viewport.point.x;
  998. glVP[1] = viewport.point.y + viewport.extent.y;
  999. glVP[2] = viewport.extent.x;
  1000. glVP[3] = -viewport.extent.y;
  1001. MatrixF mv;
  1002. dglGetModelview(&mv);
  1003. MatrixF pr;
  1004. dglGetProjection(&pr);
  1005. F64 x, y, z;
  1006. int result = gluProject( (GLdouble)point3D.x, (GLdouble)point3D.y, (GLdouble)point3D.z, (const F64 *)&glMV, (const F64 *)&glPR, (const GLint *)&glVP, &x, &y, &z );
  1007. screenPoint.x = (F32)x;
  1008. screenPoint.y = (F32)y;
  1009. screenPoint.z = (F32)z;
  1010. return (result == GL_TRUE);
  1011. #endif
  1012. }
  1013. bool dglIsInCanonicalState()
  1014. {
  1015. bool ret = true;
  1016. // Canonical state:
  1017. // BLEND disabled
  1018. // TEXTURE_2D disabled on both texture units.
  1019. // ActiveTexture set to 0
  1020. // LIGHTING off
  1021. // winding : clockwise ?
  1022. // cullface : disabled
  1023. ret &= glIsEnabled(GL_BLEND) == GL_FALSE;
  1024. ret &= glIsEnabled(GL_CULL_FACE) == GL_FALSE;
  1025. GLint temp;
  1026. #ifdef TORQUE_OS_IOS
  1027. // PUAP -Mat removed unsupported textureARB and Fog stuff
  1028. if (dglDoesSupportARBMultitexture() == true) {
  1029. //glActiveTextureARB(GL_TEXTURE1_ARB);
  1030. ret &= glIsEnabled(GL_TEXTURE_2D) == GL_FALSE;
  1031. glGetTexEnviv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, &temp);
  1032. ret &= temp == GL_REPLACE;
  1033. //glActiveTextureARB(GL_TEXTURE0_ARB);
  1034. ret &= glIsEnabled(GL_TEXTURE_2D) == GL_FALSE;
  1035. glGetTexEnviv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, &temp);
  1036. ret &= temp == GL_REPLACE;
  1037. //glClientActiveTextureARB(GL_TEXTURE1_ARB);
  1038. ret &= glIsEnabled(GL_TEXTURE_COORD_ARRAY) == GL_FALSE;
  1039. //glClientActiveTextureARB(GL_TEXTURE0_ARB);
  1040. ret &= glIsEnabled(GL_TEXTURE_COORD_ARRAY) == GL_FALSE;
  1041. } else {
  1042. ret &= glIsEnabled(GL_TEXTURE_2D) == GL_FALSE;
  1043. glGetTexEnviv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, &temp);
  1044. ret &= temp == GL_REPLACE;
  1045. ret &= glIsEnabled(GL_TEXTURE_COORD_ARRAY) == GL_FALSE;
  1046. }
  1047. ret &= glIsEnabled(GL_LIGHTING) == GL_FALSE;
  1048. ret &= glIsEnabled(GL_COLOR_ARRAY) == GL_FALSE;
  1049. ret &= glIsEnabled(GL_VERTEX_ARRAY) == GL_FALSE;
  1050. ret &= glIsEnabled(GL_NORMAL_ARRAY) == GL_FALSE;
  1051. //if (dglDoesSupportFogCoord())
  1052. //ret &= glIsEnabled(GL_FOG_COORDINATE_ARRAY_EXT) == GL_FALSE;
  1053. #else
  1054. if (dglDoesSupportARBMultitexture() == true) {
  1055. glActiveTextureARB(GL_TEXTURE1_ARB);
  1056. ret &= glIsEnabled(GL_TEXTURE_2D) == GL_FALSE;
  1057. glGetTexEnviv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, &temp);
  1058. ret &= temp == GL_REPLACE;
  1059. glActiveTextureARB(GL_TEXTURE0_ARB);
  1060. ret &= glIsEnabled(GL_TEXTURE_2D) == GL_FALSE;
  1061. glGetTexEnviv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, &temp);
  1062. ret &= temp == GL_REPLACE;
  1063. glClientActiveTextureARB(GL_TEXTURE1_ARB);
  1064. ret &= glIsEnabled(GL_TEXTURE_COORD_ARRAY) == GL_FALSE;
  1065. glClientActiveTextureARB(GL_TEXTURE0_ARB);
  1066. ret &= glIsEnabled(GL_TEXTURE_COORD_ARRAY) == GL_FALSE;
  1067. } else {
  1068. ret &= glIsEnabled(GL_TEXTURE_2D) == GL_FALSE;
  1069. glGetTexEnviv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, &temp);
  1070. ret &= temp == GL_REPLACE;
  1071. ret &= glIsEnabled(GL_TEXTURE_COORD_ARRAY) == GL_FALSE;
  1072. }
  1073. ret &= glIsEnabled(GL_LIGHTING) == GL_FALSE;
  1074. ret &= glIsEnabled(GL_COLOR_ARRAY) == GL_FALSE;
  1075. ret &= glIsEnabled(GL_VERTEX_ARRAY) == GL_FALSE;
  1076. ret &= glIsEnabled(GL_NORMAL_ARRAY) == GL_FALSE;
  1077. if (dglDoesSupportFogCoord())
  1078. ret &= glIsEnabled(GL_FOG_COORDINATE_ARRAY_EXT) == GL_FALSE;
  1079. #endif
  1080. return ret;
  1081. }
  1082. void dglSetCanonicalState()
  1083. {
  1084. #ifdef TORQUE_OS_IOS
  1085. // PUAP -Mat removed unsupported textureARB and Fog stuff
  1086. glDisable(GL_BLEND);
  1087. glDisable(GL_CULL_FACE);
  1088. glBlendFunc(GL_ONE, GL_ZERO);
  1089. glDisable(GL_LIGHTING);
  1090. if (dglDoesSupportARBMultitexture() == true) {
  1091. //glActiveTextureARB(GL_TEXTURE1_ARB);
  1092. glDisable(GL_TEXTURE_2D);
  1093. glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
  1094. //glActiveTextureARB(GL_TEXTURE0_ARB);
  1095. glDisable(GL_TEXTURE_2D);
  1096. glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
  1097. } else {
  1098. glDisable(GL_TEXTURE_2D);
  1099. glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
  1100. }
  1101. glDisableClientState(GL_COLOR_ARRAY);
  1102. glDisableClientState(GL_VERTEX_ARRAY);
  1103. glDisableClientState(GL_NORMAL_ARRAY);
  1104. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  1105. //if (dglDoesSupportFogCoord())
  1106. //glDisableClientState(GL_FOG_COORDINATE_ARRAY_EXT);
  1107. #else
  1108. glDisable(GL_BLEND);
  1109. glDisable(GL_CULL_FACE);
  1110. glBlendFunc(GL_ONE, GL_ZERO);
  1111. glDisable(GL_LIGHTING);
  1112. if (dglDoesSupportARBMultitexture() == true) {
  1113. glActiveTextureARB(GL_TEXTURE1_ARB);
  1114. glDisable(GL_TEXTURE_2D);
  1115. glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
  1116. glActiveTextureARB(GL_TEXTURE0_ARB);
  1117. glDisable(GL_TEXTURE_2D);
  1118. glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
  1119. } else {
  1120. glDisable(GL_TEXTURE_2D);
  1121. glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
  1122. }
  1123. glDisableClientState(GL_COLOR_ARRAY);
  1124. glDisableClientState(GL_VERTEX_ARRAY);
  1125. glDisableClientState(GL_NORMAL_ARRAY);
  1126. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  1127. if (dglDoesSupportFogCoord())
  1128. glDisableClientState(GL_FOG_COORDINATE_ARRAY_EXT);
  1129. #endif
  1130. }
  1131. void dglGetTransformState(S32* mvDepth,
  1132. S32* pDepth,
  1133. S32* t0Depth,
  1134. F32* t0Matrix,
  1135. S32* t1Depth,
  1136. F32* t1Matrix,
  1137. S32* vp)
  1138. {
  1139. glGetIntegerv(GL_MODELVIEW_STACK_DEPTH, (GLint*)mvDepth);
  1140. glGetIntegerv(GL_PROJECTION_STACK_DEPTH, (GLint*)pDepth);
  1141. glGetIntegerv(GL_TEXTURE_STACK_DEPTH, (GLint*)t0Depth);
  1142. glGetFloatv(GL_TEXTURE_MATRIX, t0Matrix);
  1143. if (dglDoesSupportARBMultitexture())
  1144. {
  1145. #ifdef TORQUE_OS_IOS
  1146. // PUAP -Mat removed unsupported textureARB stuff
  1147. //glActiveTextureARB(GL_TEXTURE1_ARB);
  1148. glGetIntegerv(GL_TEXTURE_STACK_DEPTH, (GLint*)t1Depth);
  1149. glGetFloatv(GL_TEXTURE_MATRIX, t1Matrix);
  1150. //glActiveTextureARB(GL_TEXTURE0_ARB);
  1151. #else
  1152. glActiveTextureARB(GL_TEXTURE1_ARB);
  1153. glGetIntegerv(GL_TEXTURE_STACK_DEPTH, (GLint*)t1Depth);
  1154. glGetFloatv(GL_TEXTURE_MATRIX, t1Matrix);
  1155. glActiveTextureARB(GL_TEXTURE0_ARB);
  1156. #endif
  1157. }
  1158. else
  1159. {
  1160. *t1Depth = 0;
  1161. for (U32 i = 0; i < 16; i++)
  1162. t1Matrix[i] = 0;
  1163. }
  1164. RectI v;
  1165. dglGetViewport(&v);
  1166. vp[0] = v.point.x;
  1167. vp[1] = v.point.y;
  1168. vp[2] = v.extent.x;
  1169. vp[3] = v.extent.y;
  1170. }
  1171. bool dglCheckState(const S32 mvDepth, const S32 pDepth,
  1172. const S32 t0Depth, const F32* t0Matrix,
  1173. const S32 t1Depth, const F32* t1Matrix,
  1174. const S32* vp)
  1175. {
  1176. GLint md, pd;
  1177. RectI v;
  1178. glGetIntegerv(GL_MODELVIEW_STACK_DEPTH, &md);
  1179. glGetIntegerv(GL_PROJECTION_STACK_DEPTH, &pd);
  1180. GLint t0d, t1d;
  1181. GLfloat t0m[16], t1m[16];
  1182. glGetIntegerv(GL_TEXTURE_STACK_DEPTH, &t0d);
  1183. glGetFloatv(GL_TEXTURE_MATRIX, t0m);
  1184. if (dglDoesSupportARBMultitexture())
  1185. {
  1186. #ifdef TORQUE_OS_IOS
  1187. // PUAP -Mat removed unsupported textureARB and Fog stuff
  1188. //glActiveTextureARB(GL_TEXTURE1_ARB);
  1189. glGetIntegerv(GL_TEXTURE_STACK_DEPTH, &t1d);
  1190. glGetFloatv(GL_TEXTURE_MATRIX, t1m);
  1191. //glActiveTextureARB(GL_TEXTURE0_ARB);
  1192. #else
  1193. glActiveTextureARB(GL_TEXTURE1_ARB);
  1194. glGetIntegerv(GL_TEXTURE_STACK_DEPTH, &t1d);
  1195. glGetFloatv(GL_TEXTURE_MATRIX, t1m);
  1196. glActiveTextureARB(GL_TEXTURE0_ARB);
  1197. #endif
  1198. }
  1199. else
  1200. {
  1201. t1d = 0;
  1202. for (U32 i = 0; i < 16; i++)
  1203. t1m[i] = 0;
  1204. }
  1205. dglGetViewport(&v);
  1206. return ((md == mvDepth) &&
  1207. (pd == pDepth) &&
  1208. (t0d == t0Depth) &&
  1209. (dMemcmp(t0m, t0Matrix, sizeof(F32) * 16) == 0) &&
  1210. (t1d == t1Depth) &&
  1211. (dMemcmp(t1m, t1Matrix, sizeof(F32) * 16) == 0) &&
  1212. ((v.point.x == vp[0]) &&
  1213. (v.point.y == vp[1]) &&
  1214. (v.extent.x == vp[2]) &&
  1215. (v.extent.y == vp[3])));
  1216. }
  1217. // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- //
  1218. // Advanced hardware functionality.
  1219. // now owns the global stuff for FSAA, rather than sceneGraph.
  1220. // define the globals first. don't bracket them, as it doesn't hurt to have
  1221. // them defined always...
  1222. S32 gFSAASamples = 1; ///< 1==no FSAA.
  1223. #if defined(TORQUE_OS_OSX)
  1224. // new FSAA simple-method handling
  1225. ConsoleFunctionGroupBegin( MacFSAA, "Mac-specific FSAA control functions.");
  1226. //------------------------------------------------------------------------------
  1227. ConsoleFunction( setFSAA, void, 2, 2, "setFSAA(int);")
  1228. {
  1229. gFSAASamples = dAtoi(argv[1]);
  1230. if (gFSAASamples<1)
  1231. gFSAASamples = 1;
  1232. else if (gFSAASamples>gGLState.maxFSAASamples)
  1233. gFSAASamples = gGLState.maxFSAASamples;
  1234. dglSetFSAASamples(gFSAASamples);
  1235. }
  1236. //------------------------------------------------------------------------------
  1237. ConsoleFunction( increaseFSAA, void, 1, 1, "increaseFSAA()" )
  1238. {
  1239. if (gFSAASamples<gGLState.maxFSAASamples)
  1240. {
  1241. gFSAASamples<<=1;
  1242. dglSetFSAASamples(gFSAASamples);
  1243. }
  1244. }
  1245. //------------------------------------------------------------------------------
  1246. ConsoleFunction( decreaseFSAA, void, 1, 1, "decreaseFSAA()" )
  1247. {
  1248. if (gFSAASamples>1)
  1249. {
  1250. gFSAASamples>>=1;
  1251. dglSetFSAASamples(gFSAASamples);
  1252. }
  1253. }
  1254. ConsoleFunctionGroupEnd( MacFSAA );
  1255. #endif
  1256. //------------------------------------------------------------------------------
  1257. ConsoleFunction(png2jpg, S32, 2, 3, "( pngFilename [ , quality ] ) Use the png2jpg function to save a PNG file specified by pngFilename as a similarly named JPEG file with the optionally specified quality.\n"
  1258. "@param pngFilename The path and file name of the PNG file to convert.\n"
  1259. "@param quality An optional quality between 0 and 100. The default quality is 90.\n"
  1260. "@return Returns -1 if the file could not be opened, 0 on other failures, and 1 if the conversion worked")
  1261. {
  1262. extern U32 gJpegQuality;
  1263. const char * rgbname = NULL;
  1264. const char * alphaname = NULL;
  1265. const char * bmpname = argv[1];
  1266. if(argc == 3)
  1267. gJpegQuality = dAtoi(argv[2]);
  1268. else
  1269. gJpegQuality = 90;
  1270. Con::printf("Converting file: %s", argv[1]);
  1271. if (!rgbname)
  1272. {
  1273. char * buf = new char[dStrlen(bmpname)+32];
  1274. dStrcpy(buf,bmpname);
  1275. char * pos = dStrstr((const char*)buf,".png");
  1276. if (!pos)
  1277. pos = buf + dStrlen(buf);
  1278. dStrcpy(pos,".jpg");
  1279. rgbname = buf;
  1280. }
  1281. if (!alphaname)
  1282. {
  1283. char * buf = new char[dStrlen(bmpname)+32];
  1284. dStrcpy(buf,bmpname);
  1285. char * pos = dStrstr((const char*)buf,".png");
  1286. if (!pos)
  1287. pos = buf + dStrlen(buf);
  1288. dStrcpy(pos,".alpha.jpg");
  1289. alphaname = buf;
  1290. }
  1291. GBitmap bmp;
  1292. FileStream fs;
  1293. if (fs.open(bmpname, FileStream::Read) == false) {
  1294. Con::printf("Error: unable to open file: %s for reading\n", bmpname);
  1295. return -1;
  1296. }
  1297. if (bmp.readPNG(fs) == false) {
  1298. Con::printf("Error: unable to read %s as a .PNG\n", bmpname);
  1299. return -1;
  1300. }
  1301. fs.close();
  1302. if (bmp.getFormat() != GBitmap::RGB &&
  1303. bmp.getFormat() != GBitmap::RGBA) {
  1304. Con::printf("Error: %s is not a 24 or 32-bit .PNG\n", bmpname);
  1305. return false;
  1306. }
  1307. GBitmap * outRGB = NULL;
  1308. GBitmap * outAlpha = NULL;
  1309. GBitmap workRGB, workAlpha;
  1310. if (bmp.getFormat() == GBitmap::RGB)
  1311. outRGB = &bmp;
  1312. else
  1313. {
  1314. S32 w = bmp.getWidth();
  1315. S32 h = bmp.getHeight();
  1316. workRGB.allocateBitmap(w,h,false,GBitmap::RGB);
  1317. workAlpha.allocateBitmap(w,h,false,GBitmap::Alpha);
  1318. U8 * rgbBits = workRGB.getWritableBits();
  1319. U8 * alphaBits = workAlpha.getWritableBits();
  1320. U8 * bmpBits = bmp.getWritableBits();
  1321. for (S32 i=0; i<w; i++)
  1322. {
  1323. for (S32 j=0; j<h; j++)
  1324. {
  1325. rgbBits[i*3 + j*3*w + 0] = bmpBits[i*4 + j*4*w + 0];
  1326. rgbBits[i*3 + j*3*w + 1] = bmpBits[i*4 + j*4*w + 1];
  1327. rgbBits[i*3 + j*3*w + 2] = bmpBits[i*4 + j*4*w + 2];
  1328. alphaBits[i + j*w] = bmpBits[i*4 + j*4*w + 3];
  1329. }
  1330. }
  1331. Con::printf("texture: width=%i, height=%i\n",w,h);
  1332. outRGB = &workRGB;
  1333. outAlpha = &workAlpha;
  1334. }
  1335. if (outRGB)
  1336. {
  1337. FileStream fws;
  1338. if (fws.open(rgbname, FileStream::Write) == false)
  1339. {
  1340. Con::printf("Error: unable to open file: %s for writing\n", rgbname);
  1341. return -1;
  1342. }
  1343. if (dStrstr(rgbname,".png"))
  1344. {
  1345. if (outRGB->writePNG(fws) == false)
  1346. {
  1347. fws.close();
  1348. Con::printf("Error: couldn't write RGB as a png\n");
  1349. return -1;
  1350. }
  1351. }
  1352. else if (outRGB->writeJPEG(fws) == false)
  1353. {
  1354. Con::printf("Error: couldn't write RGB as a jpg\n");
  1355. return -1;
  1356. }
  1357. fws.close();
  1358. }
  1359. if (outAlpha)
  1360. {
  1361. gJpegQuality = 60;
  1362. FileStream fws;
  1363. if (fws.open(alphaname, FileStream::Write) == false)
  1364. {
  1365. Con::printf("Error: unable to open file: %s for writing\n", alphaname);
  1366. return -1;
  1367. }
  1368. if (dStrstr(alphaname,".png"))
  1369. {
  1370. if (outAlpha->writePNG(fws) == false)
  1371. {
  1372. fws.close();
  1373. Con::printf("Error: couldn't write alpha as a png\n");
  1374. return -1;
  1375. }
  1376. }
  1377. else if (outAlpha->writeJPEG(fws) == false)
  1378. {
  1379. Con::printf("Error: couldn't write alpha as a jpg\n");
  1380. return -1;
  1381. }
  1382. fws.close();
  1383. }
  1384. return(0);
  1385. }
  1386. #ifdef TORQUE_OS_IOS
  1387. GLfloat gVertexFloats[8];
  1388. GLfloat gTextureVerts[8];
  1389. #endif