dgl.cc 45 KB

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