dgl.cc 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847
  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/gColor.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. #include <vector>
  37. namespace {
  38. ColorI sg_bitmapModulation(255, 255, 255, 255);
  39. ColorI sg_textAnchorColor(255, 255, 255, 255);
  40. ColorI sg_stackColor(255, 255, 255, 255);
  41. RectI sgCurrentClipRect;
  42. } // namespace {}
  43. //--------------------------------------------------------------------------
  44. void dglSetBitmapModulation(const ColorF& in_rColor)
  45. {
  46. ColorF c = in_rColor;
  47. c.clamp();
  48. sg_bitmapModulation = c;
  49. sg_textAnchorColor = sg_bitmapModulation;
  50. }
  51. void dglGetBitmapModulation(ColorF* color)
  52. {
  53. *color = sg_bitmapModulation;
  54. }
  55. void dglGetBitmapModulation(ColorI* color)
  56. {
  57. *color = sg_bitmapModulation;
  58. }
  59. void dglClearBitmapModulation()
  60. {
  61. sg_bitmapModulation.set(255, 255, 255, 255);
  62. }
  63. void dglSetTextAnchorColor(const ColorF& in_rColor)
  64. {
  65. ColorF c = in_rColor;
  66. c.clamp();
  67. sg_textAnchorColor = c;
  68. }
  69. //--------------------------------------------------------------------------
  70. void dglDrawBitmapStretchSR(TextureObject* texture,
  71. const RectI& dstRect,
  72. const RectI& srcRect,
  73. const U32 in_flip,
  74. F32 fSpin,
  75. bool bSilhouette)
  76. {
  77. AssertFatal(texture != NULL, "GSurface::drawBitmapStretchSR: NULL Handle");
  78. if(!dstRect.isValidRect())
  79. return;
  80. AssertFatal(srcRect.isValidRect() == true,
  81. "GSurface::drawBitmapStretchSR: routines assume normal rects");
  82. glDisable(GL_LIGHTING);
  83. glEnable(GL_TEXTURE_2D);
  84. glBindTexture(GL_TEXTURE_2D, texture->getGLTextureName());
  85. //glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  86. if (bSilhouette)
  87. {
  88. glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_BLEND);
  89. ColorF kModulationColor;
  90. dglGetBitmapModulation(&kModulationColor);
  91. glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, kModulationColor.address());
  92. }
  93. else
  94. {
  95. glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  96. }
  97. glEnable(GL_BLEND);
  98. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  99. F32 texLeft = F32(srcRect.point.x) / F32(texture->getTextureWidth());
  100. F32 texRight = F32(srcRect.point.x + srcRect.extent.x) / F32(texture->getTextureWidth());
  101. F32 texTop = F32(srcRect.point.y) / F32(texture->getTextureHeight());
  102. F32 texBottom = F32(srcRect.point.y + srcRect.extent.y) / F32(texture->getTextureHeight());
  103. Point2F scrPoints[4];
  104. if(fSpin == 0.0f)
  105. {
  106. scrPoints[0].x = (F32)dstRect.point.x;
  107. scrPoints[0].y = (F32)dstRect.point.y;
  108. scrPoints[1].x = (F32)(dstRect.point.x + dstRect.extent.x);
  109. scrPoints[1].y = (F32)dstRect.point.y;
  110. scrPoints[2].x = (F32)dstRect.point.x;
  111. scrPoints[2].y = (F32)(dstRect.point.y + dstRect.extent.y);
  112. scrPoints[3].x = (F32)(dstRect.point.x + dstRect.extent.x);
  113. scrPoints[3].y = (F32)(dstRect.point.y + dstRect.extent.y);
  114. //screenLeft = dstRect.point.x;
  115. //screenRight = dstRect.point.x + dstRect.extent.x;
  116. //screenTop = dstRect.point.y;
  117. //screenBottom = dstRect.point.y + dstRect.extent.y;
  118. }
  119. else
  120. {
  121. //WE NEED TO IMPLEMENT A FAST 2D ROTATION -- NOT THIS SLOWER 3D ROTATION
  122. MatrixF rotMatrix( EulerF( 0.0, 0.0, mDegToRad(fSpin) ) );
  123. Point3F offset( dstRect.point.x + dstRect.extent.x / 2.0f,
  124. dstRect.point.y + dstRect.extent.y / 2.0f, 0.0 );
  125. Point3F points[4];
  126. points[0] = Point3F(-dstRect.extent.x / 2.0f, dstRect.extent.y / 2.0f, 0.0);
  127. points[1] = Point3F( dstRect.extent.x / 2.0f, dstRect.extent.y / 2.0f, 0.0);
  128. points[2] = Point3F(-dstRect.extent.x / 2.0f, -dstRect.extent.y / 2.0f, 0.0);
  129. points[3] = Point3F( dstRect.extent.x / 2.0f, -dstRect.extent.y / 2.0f, 0.0);
  130. for( int i=0; i<4; i++ )
  131. {
  132. rotMatrix.mulP( points[i] );
  133. points[i] += offset;
  134. scrPoints[i].x = points[i].x;
  135. scrPoints[i].y = points[i].y;
  136. }
  137. }
  138. if(in_flip & GFlip_X)
  139. {
  140. F32 temp = texLeft;
  141. texLeft = texRight;
  142. texRight = temp;
  143. }
  144. if(in_flip & GFlip_Y)
  145. {
  146. F32 temp = texTop;
  147. texTop = texBottom;
  148. texBottom = temp;
  149. }
  150. glColor4ub(sg_bitmapModulation.red,
  151. sg_bitmapModulation.green,
  152. sg_bitmapModulation.blue,
  153. sg_bitmapModulation.alpha);
  154. #if defined(TORQUE_OS_IOS) || defined(TORQUE_OS_ANDROID) || defined(TORQUE_OS_EMSCRIPTEN)
  155. GLfloat verts[] = {
  156. (GLfloat)scrPoints[0].x, (GLfloat)scrPoints[0].y,
  157. (GLfloat)scrPoints[1].x, (GLfloat)scrPoints[1].y,
  158. (GLfloat)scrPoints[2].x, (GLfloat)scrPoints[2].y,
  159. (GLfloat)scrPoints[3].x, (GLfloat)scrPoints[3].y,
  160. };
  161. GLfloat texVerts[] = {
  162. (GLfloat)texLeft, (GLfloat)texTop,
  163. (GLfloat)texRight, (GLfloat)texTop,
  164. (GLfloat)texLeft, (GLfloat)texBottom,
  165. (GLfloat)texRight, (GLfloat)texBottom,
  166. };
  167. glDisableClientState(GL_COLOR_ARRAY);
  168. //glDisableClientState(GL_POINT_SIZE_ARRAY_OES);
  169. glEnableClientState(GL_VERTEX_ARRAY);
  170. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  171. glVertexPointer(2, GL_FLOAT, 0, verts);
  172. glTexCoordPointer(2, GL_FLOAT, 0, texVerts);
  173. glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
  174. #else
  175. glBegin(GL_TRIANGLE_FAN);
  176. glTexCoord2f(texLeft, texBottom);
  177. glVertex2f(scrPoints[2].x, scrPoints[2].y);
  178. glTexCoord2f(texRight, texBottom);
  179. glVertex2f(scrPoints[3].x, scrPoints[3].y);
  180. glTexCoord2f(texRight, texTop);
  181. glVertex2f(scrPoints[1].x, scrPoints[1].y);
  182. glTexCoord2f(texLeft, texTop);
  183. glVertex2f(scrPoints[0].x, scrPoints[0].y);
  184. glEnd();
  185. #endif
  186. if (bSilhouette)
  187. {
  188. glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, ColorF(0.0f, 0.0f, 0.0f, 0.0f).address());
  189. }
  190. glDisable(GL_BLEND);
  191. glDisable(GL_TEXTURE_2D);
  192. }
  193. void dglDrawBitmap(TextureObject* texture, const Point2I& in_rAt, const U32 in_flip)
  194. {
  195. AssertFatal(texture != NULL, "GSurface::drawBitmap: NULL Handle");
  196. // All non-StretchSR bitmaps are transformed into StretchSR calls...
  197. //
  198. RectI subRegion(0, 0,
  199. texture->getBitmapWidth(),
  200. texture->getBitmapHeight());
  201. RectI stretch(in_rAt.x, in_rAt.y,
  202. texture->getBitmapWidth(),
  203. texture->getBitmapHeight());
  204. dglDrawBitmapStretchSR(texture,
  205. stretch,
  206. subRegion,
  207. in_flip);
  208. }
  209. void dglDrawBitmapTile(TextureObject* texture, const RectI& dstRect, const U32 in_flip, F32 fSpin, bool bSilhouette)
  210. {
  211. AssertFatal(texture != NULL, "GSurface::drawBitmapTile: NULL Handle");
  212. // since the texture coords are calculated from the texture sub-rect we pass in,
  213. // but no actual indexing of that sub-rect happens, we can pass any texture coords
  214. // that we like to dglDrawBitmapSR(). We use this to force tiling.
  215. RectI subregion(0,0, dstRect.extent.x, dstRect.extent.y);
  216. dglDrawBitmapStretchSR(texture, dstRect, subregion, in_flip, fSpin, bSilhouette);
  217. }
  218. void dglDrawBitmapStretch(TextureObject* texture, const RectI& dstRect, const U32 in_flip, F32 fSpin, bool bSilhouette)
  219. {
  220. AssertFatal(texture != NULL, "GSurface::drawBitmapStretch: NULL Handle");
  221. AssertFatal(dstRect.isValidRect() == true,
  222. "GSurface::drawBitmapStretch: routines assume normal rects");
  223. RectI subRegion(0, 0,
  224. texture->getBitmapWidth(),
  225. texture->getBitmapHeight());
  226. dglDrawBitmapStretchSR(texture,
  227. dstRect,
  228. subRegion,
  229. in_flip,
  230. fSpin,
  231. bSilhouette);
  232. }
  233. void dglDrawBitmapSR(TextureObject *texture, const Point2I& in_rAt, const RectI& srcRect, const U32 in_flip)
  234. {
  235. AssertFatal(texture != NULL, "GSurface::drawBitmapSR: NULL Handle");
  236. AssertFatal(srcRect.isValidRect() == true,
  237. "GSurface::drawBitmapSR: routines assume normal rects");
  238. RectI stretch(in_rAt.x, in_rAt.y,
  239. srcRect.len_x(),
  240. srcRect.len_y());
  241. dglDrawBitmapStretchSR(texture,
  242. stretch,
  243. srcRect,
  244. in_flip);
  245. }
  246. U32 dglDrawText(GFont* font,
  247. const Point2I& ptDraw,
  248. const UTF16* in_string,
  249. const ColorI* colorTable,
  250. const U32 maxColorIndex,
  251. F32 rot)
  252. {
  253. return dglDrawTextN(font, ptDraw, in_string, dStrlen(in_string), colorTable, maxColorIndex, rot);
  254. }
  255. U32 dglDrawText(GFont* font,
  256. const Point2I& ptDraw,
  257. const UTF8* in_string,
  258. const ColorI* colorTable,
  259. const U32 maxColorIndex,
  260. F32 rot)
  261. {
  262. // Just a note - dStrlen(utf8) isn't strictly correct but it's guaranteed to be
  263. // as long or longer than the real length. dglDrawTextN fails gracefully
  264. // if you specify overlong, so this is ok.
  265. return dglDrawTextN(font, ptDraw, in_string, dStrlen((const UTF8 *) in_string), colorTable, maxColorIndex, rot);
  266. }
  267. struct TextVertex
  268. {
  269. Point2F p;
  270. Point2F t;
  271. ColorI c;
  272. TextVertex() { set( 0.0f, 0.0f, 0.0f, 0.0f, ColorI(0, 0, 0) ); }
  273. void set(F32 x, F32 y, F32 tx, F32 ty, ColorI color)
  274. {
  275. p.x = x;
  276. p.y = y;
  277. t.x = tx;
  278. t.y = ty;
  279. c = color;
  280. }
  281. };
  282. //------------------------------------------------------------------------------
  283. U32 dglDrawTextN(GFont* font,
  284. const Point2I& ptDraw,
  285. const UTF8* in_string,
  286. U32 n,
  287. const ColorI* colorTable,
  288. const U32 maxColorIndex,
  289. F32 rot)
  290. {
  291. PROFILE_START(DrawText_UTF8);
  292. U32 len = dStrlen(in_string) + 1;
  293. FrameTemp<UTF16> ubuf(len);
  294. convertUTF8toUTF16(in_string, ubuf, len);
  295. U32 tmp = dglDrawTextN(font, ptDraw, ubuf, n, colorTable, maxColorIndex, rot);
  296. PROFILE_END();
  297. return tmp;
  298. }
  299. //-----------------------------------------------------------------------------
  300. #if defined(TORQUE_OS_IOS) || defined(TORQUE_OS_ANDROID) || defined(TORQUE_OS_EMSCRIPTEN)
  301. U32 dglDrawTextN(GFont* font,
  302. const Point2I& ptDraw,
  303. const UTF16* in_string,
  304. U32 n,
  305. const ColorI* colorTable,
  306. const U32 maxColorIndex,
  307. F32 rot)
  308. {
  309. // return on zero length strings
  310. if( n < 1 )
  311. return ptDraw.x;
  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) || defined(TORQUE_OS_EMSCRIPTEN)
  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 dglDrawTriangleFill(const Point2I &pt1, const Point2I &pt2, const Point2I &pt3, const ColorI &color)
  681. {
  682. glEnable(GL_BLEND);
  683. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  684. glDisable(GL_TEXTURE_2D);
  685. glColor4ub(color.red, color.green, color.blue, color.alpha);
  686. #if defined(TORQUE_OS_IOS) || defined(TORQUE_OS_ANDROID) || defined(TORQUE_OS_EMSCRIPTEN)
  687. GLfloat vertices[] = {
  688. (GLfloat)pt1.x, (GLfloat)pt1.y,
  689. (GLfloat)pt2.x, (GLfloat)pt2.y,
  690. (GLfloat)pt3.x, (GLfloat)pt3.y,
  691. };
  692. glVertexPointer(2, GL_FLOAT, 0, vertices);
  693. glEnableClientState(GL_VERTEX_ARRAY);
  694. glDrawArrays(GL_TRIANGLE_STRIP, 0, 3);
  695. #else
  696. glBegin(GL_TRIANGLES);
  697. glVertex2f((GLfloat)pt1.x, (GLfloat)pt1.y);
  698. glVertex2f((GLfloat)pt2.x, (GLfloat)pt2.y);
  699. glVertex2f((GLfloat)pt3.x, (GLfloat)pt3.y);
  700. glEnd();
  701. #endif
  702. }
  703. void dglDrawRect(const Point2I &upperL, const Point2I &lowerR, const ColorI &color, const float &lineWidth)
  704. {
  705. glEnable(GL_BLEND);
  706. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  707. glDisable(GL_TEXTURE_2D);
  708. glLineWidth(lineWidth);
  709. glColor4ub(color.red, color.green, color.blue, color.alpha);
  710. #if defined(TORQUE_OS_IOS) || defined(TORQUE_OS_ANDROID) || defined(TORQUE_OS_EMSCRIPTEN)
  711. GLfloat verts[] = {
  712. (GLfloat)(upperL.x), (GLfloat)(upperL.y),
  713. (GLfloat)(lowerR.x), (GLfloat)(upperL.y),
  714. (GLfloat)(lowerR.x), (GLfloat)(lowerR.y),
  715. (GLfloat)(upperL.x), (GLfloat)(lowerR.y),
  716. };
  717. glVertexPointer(2, GL_FLOAT, 0, verts );
  718. glDrawArrays(GL_LINE_LOOP, 0, 4 );//draw last two
  719. #else
  720. glBegin(GL_LINE_LOOP);
  721. glVertex2f((F32)upperL.x + 0.5f, (F32)upperL.y + 0.5f);
  722. glVertex2f((F32)lowerR.x + 0.5f, (F32)upperL.y + 0.5f);
  723. glVertex2f((F32)lowerR.x + 0.5f, (F32)lowerR.y + 0.5f);
  724. glVertex2f((F32)upperL.x + 0.5f, (F32)lowerR.y + 0.5f);
  725. glEnd();
  726. #endif
  727. }
  728. // the fill convention for lined rects is that they outline the rectangle border of the
  729. // filled region specified.
  730. void dglDrawRect(const RectI &rect, const ColorI &color, const float &lineWidth)
  731. {
  732. Point2I lowerR(rect.point.x + rect.extent.x - 1, rect.point.y + rect.extent.y - 1);
  733. dglDrawRect(rect.point, lowerR, color, lineWidth);
  734. }
  735. // the fill convention says that pixel at upperL will be filled and
  736. // that pixel at lowerR will NOT be filled.
  737. void dglDrawRectFill(const Point2I &upperL, const Point2I &lowerR, const ColorI &color)
  738. {
  739. glEnable(GL_BLEND);
  740. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  741. glDisable(GL_TEXTURE_2D);
  742. glColor4ub(color.red, color.green, color.blue, color.alpha);
  743. #if defined(TORQUE_OS_IOS) || defined(TORQUE_OS_ANDROID) || defined(TORQUE_OS_EMSCRIPTEN)
  744. GLfloat vertices[] = {
  745. (GLfloat)upperL.x, (GLfloat)upperL.y,
  746. (GLfloat)upperL.x, (GLfloat)lowerR.y,
  747. (GLfloat)lowerR.x, (GLfloat)upperL.y,
  748. (GLfloat)lowerR.x, (GLfloat)lowerR.y,
  749. };
  750. glVertexPointer(2, GL_FLOAT, 0, vertices);
  751. glEnableClientState(GL_VERTEX_ARRAY);
  752. glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
  753. #else
  754. glRecti((S32)upperL.x, (S32)upperL.y, (S32)lowerR.x, (S32)lowerR.y);
  755. #endif
  756. }
  757. void dglDrawRectFill(const RectI &rect, const ColorI &color)
  758. {
  759. Point2I lowerR(rect.point.x + rect.extent.x, rect.point.y + rect.extent.y);
  760. dglDrawRectFill(rect.point, lowerR, color);
  761. }
  762. //Start in the top left and move counter-clockwise around the quad.
  763. void dglDrawQuadFill(const Point2I &point1, const Point2I &point2, const Point2I &point3, const Point2I &point4, const ColorI &color)
  764. {
  765. glEnable(GL_BLEND);
  766. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  767. glDisable(GL_TEXTURE_2D);
  768. glColor4ub(color.red, color.green, color.blue, color.alpha);
  769. //Points 3 and 4 are switched by design.
  770. GLfloat vertices[] = {
  771. (GLfloat)point1.x, (GLfloat)point1.y,
  772. (GLfloat)point2.x, (GLfloat)point2.y,
  773. (GLfloat)point4.x, (GLfloat)point4.y,
  774. (GLfloat)point3.x, (GLfloat)point3.y,
  775. };
  776. glVertexPointer(2, GL_FLOAT, 0, vertices);
  777. glEnableClientState(GL_VERTEX_ARRAY);
  778. glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
  779. }
  780. void dglDrawDot(const Point2F &screenPoint,const ColorI &color)
  781. {
  782. glEnable(GL_BLEND);
  783. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  784. glBegin(GL_POINTS);
  785. glColor4ub(color.red, color.green, color.blue, color.alpha);
  786. glVertex2f(screenPoint.x, screenPoint.y);
  787. glDisable(GL_BLEND);
  788. glEnd();
  789. }
  790. void dglDraw2DSquare( const Point2F &screenPoint, F32 width, F32 spinAngle )
  791. {
  792. width *= 0.5;
  793. MatrixF rotMatrix( EulerF( 0.0, 0.0, spinAngle ) );
  794. Point3F offset( screenPoint.x, screenPoint.y, 0.0 );
  795. Point3F points[4];
  796. points[0] = Point3F(-width, -width, 0.0);
  797. points[1] = Point3F(-width, width, 0.0);
  798. points[2] = Point3F( width, width, 0.0);
  799. points[3] = Point3F( width, -width, 0.0);
  800. for( int i=0; i<4; i++ )
  801. {
  802. rotMatrix.mulP( points[i] );
  803. points[i] += offset;
  804. }
  805. #if defined(TORQUE_OS_IOS) || defined(TORQUE_OS_ANDROID) || defined(TORQUE_OS_EMSCRIPTEN)
  806. GLfloat verts[] = {
  807. 0.0, 0.0,
  808. 1.0, 0.0,
  809. 0.0, 1.0,//may need to switch last two
  810. 1.0, 1.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, 0.0);
  828. glVertex2fv(points[0]);
  829. glTexCoord2f(0.0, 1.0);
  830. glVertex2fv(points[1]);
  831. glTexCoord2f(1.0, 1.0);
  832. glVertex2fv(points[2]);
  833. glTexCoord2f(1.0, 0.0);
  834. glVertex2fv(points[3]);
  835. glEnd();
  836. #endif
  837. }
  838. void dglDrawBillboard( const Point3F &position, F32 width, F32 spinAngle )
  839. {
  840. MatrixF modelview;
  841. dglGetModelview( &modelview );
  842. modelview.transpose();
  843. width *= 0.5;
  844. Point3F points[4];
  845. points[0] = Point3F(-width, 0.0, -width);
  846. points[1] = Point3F(-width, 0.0, width);
  847. points[2] = Point3F( width, 0.0, width);
  848. points[3] = Point3F( width, 0.0, -width);
  849. MatrixF rotMatrix( EulerF( 0.0, spinAngle, 0.0 ) );
  850. for( int i=0; i<4; i++ )
  851. {
  852. rotMatrix.mulP( points[i] );
  853. modelview.mulP( points[i] );
  854. points[i] += position;
  855. }
  856. #if defined(TORQUE_OS_IOS) || defined(TORQUE_OS_ANDROID) || defined(TORQUE_OS_EMSCRIPTEN)
  857. GLfloat verts[] = {
  858. 0.0, 1.0,
  859. 0.0, 0.0,
  860. 1.0, 1.0,//may need to switch last two
  861. 1.0, 0.0,
  862. };
  863. GLfloat texVerts[] = {
  864. (GLfloat)points[0].x, (GLfloat)points[0].y,
  865. (GLfloat)points[1].x, (GLfloat)points[1].y,
  866. (GLfloat)points[3].x, (GLfloat)points[3].y,
  867. (GLfloat)points[2].x, (GLfloat)points[2].y,
  868. };
  869. glDisableClientState(GL_COLOR_ARRAY);
  870. //glDisableClientState(GL_POINT_SIZE_ARRAY_OES);
  871. glEnableClientState(GL_VERTEX_ARRAY);
  872. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  873. glVertexPointer(2, GL_FLOAT, 0, verts);
  874. glTexCoordPointer(2, GL_FLOAT, 0, texVerts);
  875. glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
  876. #else
  877. glBegin(GL_TRIANGLE_FAN);
  878. glTexCoord2f(0.0, 1.0);
  879. glVertex3fv(points[0]);
  880. glTexCoord2f(0.0, 0.0);
  881. glVertex3fv(points[1]);
  882. glTexCoord2f(1.0, 0.0);
  883. glVertex3fv(points[2]);
  884. glTexCoord2f(1.0, 1.0);
  885. glVertex3fv(points[3]);
  886. glEnd();
  887. #endif
  888. }
  889. void dglWireCube(const Point3F & extent, const Point3F & center)
  890. {
  891. static Point3F cubePoints[8] =
  892. {
  893. Point3F(-1, -1, -1), Point3F(-1, -1, 1), Point3F(-1, 1, -1), Point3F(-1, 1, 1),
  894. Point3F( 1, -1, -1), Point3F( 1, -1, 1), Point3F( 1, 1, -1), Point3F( 1, 1, 1)
  895. };
  896. static U32 cubeFaces[6][4] =
  897. {
  898. { 0, 2, 6, 4 }, { 0, 2, 3, 1 }, { 0, 1, 5, 4 },
  899. { 3, 2, 6, 7 }, { 7, 6, 4, 5 }, { 3, 7, 5, 1 }
  900. };
  901. glDisable(GL_CULL_FACE);
  902. #if defined(TORQUE_OS_IOS) || defined(TORQUE_OS_ANDROID) || defined(TORQUE_OS_EMSCRIPTEN)
  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_LINE_LOOP, 0, 4);
  925. }
  926. #else
  927. for (S32 i = 0; i < 6; i++)
  928. {
  929. glBegin(GL_LINE_LOOP);
  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 dglSolidCube(const Point3F & extent, const Point3F & center)
  942. {
  943. static Point3F cubePoints[8] =
  944. {
  945. Point3F(-1, -1, -1), Point3F(-1, -1, 1), Point3F(-1, 1, -1), Point3F(-1, 1, 1),
  946. Point3F( 1, -1, -1), Point3F( 1, -1, 1), Point3F( 1, 1, -1), Point3F( 1, 1, 1)
  947. };
  948. static U32 cubeFaces[6][4] =
  949. {
  950. { 0, 2, 6, 4 }, { 0, 2, 3, 1 }, { 0, 1, 5, 4 },
  951. { 3, 2, 6, 7 }, { 7, 6, 4, 5 }, { 3, 7, 5, 1 }
  952. };
  953. #if defined(TORQUE_OS_IOS) || defined(TORQUE_OS_ANDROID) || defined(TORQUE_OS_EMSCRIPTEN)
  954. //PUAP -Mat untested
  955. for (S32 i = 0; i < 6; i++)
  956. {
  957. GLfloat verts[] = {
  958. 0, 0, 0,
  959. 0, 0, 0,
  960. 0, 0, 0,
  961. 0, 0, 0,
  962. };
  963. int j = 0;
  964. for(int vert = 0; vert < 4; vert++)
  965. {
  966. int idx = cubeFaces[i][vert];
  967. //glVertex3f(cubePoints[idx].x * extent.x + center.x,
  968. //cubePoints[idx].y * extent.y + center.y,
  969. //cubePoints[idx].z * extent.z + center.z);
  970. verts[j] = cubePoints[idx].x * extent.x + center.x;
  971. verts[++j] = cubePoints[idx].y * extent.y + center.y;
  972. verts[++j] = cubePoints[idx].z * extent.z + center.z;
  973. }
  974. glVertexPointer(3, GL_FLOAT, 0, verts);
  975. glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
  976. }
  977. #else
  978. for (S32 i = 0; i < 6; i++)
  979. {
  980. glBegin(GL_TRIANGLE_FAN);
  981. for(int vert = 0; vert < 4; vert++)
  982. {
  983. int idx = cubeFaces[i][vert];
  984. glVertex3f(cubePoints[idx].x * extent.x + center.x,
  985. cubePoints[idx].y * extent.y + center.y,
  986. cubePoints[idx].z * extent.z + center.z);
  987. }
  988. glEnd();
  989. }
  990. #endif
  991. }
  992. //Draws an unfilled circle with line segments.
  993. //Circle drawing code was modified from this source with gratitude. It is in the public domain.
  994. //http://slabode.exofire.net/circle_draw.shtml
  995. void dglDrawCircle(const Point2I &center, const F32 radius, const ColorI &color, const F32 &lineWidth)
  996. {
  997. if(lineWidth <= 0)
  998. {
  999. return;
  1000. }
  1001. F32 adjustedRadius = radius - (lineWidth/2);
  1002. const S32 num_segments = (const S32)round(10 * sqrtf(adjustedRadius));
  1003. F32 theta = 2 * 3.1415926f / F32(num_segments);
  1004. F32 c = cosf(theta);//precalculate the sine and cosine
  1005. F32 s = sinf(theta);
  1006. F32 t;
  1007. F32 x = adjustedRadius;//we start at angle = 0
  1008. F32 y = 0;
  1009. glEnable(GL_BLEND);
  1010. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  1011. glDisable(GL_TEXTURE_2D);
  1012. glLineWidth(lineWidth);
  1013. glColor4ub(color.red, color.green, color.blue, color.alpha);
  1014. vector<GLfloat> verts;
  1015. for (int ii = 0; ii < num_segments; ii++)
  1016. {
  1017. verts.push_back(GLfloat(x + center.x));
  1018. verts.push_back(GLfloat(y + center.y));
  1019. //apply the rotation matrix
  1020. t = x;
  1021. x = c * x - s * y;
  1022. y = s * t + c * y;
  1023. }
  1024. verts.push_back(GLfloat(verts[0]));
  1025. verts.push_back(GLfloat(verts[1]));
  1026. glEnableClientState(GL_VERTEX_ARRAY);
  1027. glVertexPointer(2, GL_FLOAT, 0, verts.data());
  1028. glDrawArrays(GL_LINE_LOOP, 0, num_segments + 1);//draw last two
  1029. glDisableClientState(GL_VERTEX_ARRAY);
  1030. }
  1031. void dglDrawCircleFill(const Point2I &center, const F32 radius, const ColorI &color)
  1032. {
  1033. const S32 num_segments = (const S32)round(10 * sqrtf(radius));
  1034. F32 theta = 2 * 3.1415926f / F32(num_segments);
  1035. F32 c = cosf(theta);//precalculate the sine and cosine
  1036. F32 s = sinf(theta);
  1037. F32 t;
  1038. F32 x = radius;//we start at angle = 0
  1039. F32 y = 0;
  1040. glEnable(GL_BLEND);
  1041. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  1042. glDisable(GL_TEXTURE_2D);
  1043. glColor4ub(color.red, color.green, color.blue, color.alpha);
  1044. vector<GLfloat> verts;
  1045. verts.push_back(GLfloat(center.x));
  1046. verts.push_back(GLfloat(center.y));
  1047. for (int ii = 0; ii < num_segments; ii++)
  1048. {
  1049. verts.push_back(GLfloat(x + center.x));
  1050. verts.push_back(GLfloat(y + center.y));
  1051. //apply the rotation matrix
  1052. t = x;
  1053. x = c * x - s * y;
  1054. y = s * t + c * y;
  1055. }
  1056. verts.push_back(GLfloat(verts[2]));
  1057. verts.push_back(GLfloat(verts[3]));
  1058. glEnableClientState(GL_VERTEX_ARRAY);
  1059. glVertexPointer(2, GL_FLOAT, 0, verts.data());
  1060. glDrawArrays(GL_TRIANGLE_FAN, 0, num_segments+2);
  1061. glDisableClientState(GL_VERTEX_ARRAY);
  1062. }
  1063. void dglSetClipRect(const RectI &clipRect)
  1064. {
  1065. glMatrixMode(GL_PROJECTION);
  1066. glLoadIdentity();
  1067. U32 screenHeight = Platform::getWindowSize().y;
  1068. #if defined(TORQUE_OS_IOS) || defined(TORQUE_OS_ANDROID)
  1069. glOrthof(clipRect.point.x, clipRect.point.x + clipRect.extent.x,
  1070. clipRect.extent.y, 0,
  1071. 0, 1);
  1072. #else
  1073. glOrtho(clipRect.point.x, clipRect.point.x + clipRect.extent.x,
  1074. clipRect.extent.y, 0,
  1075. 0, 1);
  1076. #endif
  1077. glTranslatef(0.0f, (F32)-clipRect.point.y, 0.0f);
  1078. glMatrixMode(GL_MODELVIEW);
  1079. glLoadIdentity();
  1080. glViewport(clipRect.point.x, screenHeight - (clipRect.point.y + clipRect.extent.y),
  1081. clipRect.extent.x, clipRect.extent.y);
  1082. sgCurrentClipRect = clipRect;
  1083. }
  1084. const RectI& dglGetClipRect()
  1085. {
  1086. return sgCurrentClipRect;
  1087. }
  1088. bool dglPointToScreen( Point3F &point3D, Point3F &screenPoint )
  1089. {
  1090. #if defined(TORQUE_OS_IOS) || defined(TORQUE_OS_ANDROID) || defined(TORQUE_OS_EMSCRIPTEN)
  1091. GLfloat glMV[16];
  1092. GLfloat glPR[16];
  1093. GLint glVP[4];
  1094. glGetFloatv(GL_PROJECTION_MATRIX, glPR);
  1095. glGetFloatv(GL_MODELVIEW_MATRIX, glMV);
  1096. RectI viewport;
  1097. dglGetViewport(&viewport);
  1098. glVP[0] = viewport.point.x;
  1099. glVP[1] = viewport.point.y + viewport.extent.y;
  1100. glVP[2] = viewport.extent.x;
  1101. glVP[3] = -viewport.extent.y;
  1102. MatrixF mv;
  1103. dglGetModelview(&mv);
  1104. MatrixF pr;
  1105. dglGetProjection(&pr);
  1106. F64 x, y, z;
  1107. // PUAP -Mat untested
  1108. int result = gluProject( point3D.x, point3D.y, point3D.z, (const F64 *)&glMV, (const F64 *)&glPR, (const GLint *)&glVP, &x, &y, &z );
  1109. screenPoint.x = x;
  1110. screenPoint.y = y;
  1111. screenPoint.z = z;
  1112. return (result == GL_TRUE);
  1113. #else
  1114. GLdouble glMV[16];
  1115. GLdouble glPR[16];
  1116. GLint glVP[4];
  1117. glGetDoublev(GL_PROJECTION_MATRIX, glPR);
  1118. glGetDoublev(GL_MODELVIEW_MATRIX, glMV);
  1119. RectI viewport;
  1120. dglGetViewport(&viewport);
  1121. glVP[0] = viewport.point.x;
  1122. glVP[1] = viewport.point.y + viewport.extent.y;
  1123. glVP[2] = viewport.extent.x;
  1124. glVP[3] = -viewport.extent.y;
  1125. MatrixF mv;
  1126. dglGetModelview(&mv);
  1127. MatrixF pr;
  1128. dglGetProjection(&pr);
  1129. F64 x, y, z;
  1130. int result = gluProject( (GLdouble)point3D.x, (GLdouble)point3D.y, (GLdouble)point3D.z, (const F64 *)&glMV, (const F64 *)&glPR, (const GLint *)&glVP, &x, &y, &z );
  1131. screenPoint.x = (F32)x;
  1132. screenPoint.y = (F32)y;
  1133. screenPoint.z = (F32)z;
  1134. return (result == GL_TRUE);
  1135. #endif
  1136. }
  1137. bool dglIsInCanonicalState()
  1138. {
  1139. bool ret = true;
  1140. // Canonical state:
  1141. // BLEND disabled
  1142. // TEXTURE_2D disabled on both texture units.
  1143. // ActiveTexture set to 0
  1144. // LIGHTING off
  1145. // winding : clockwise ?
  1146. // cullface : disabled
  1147. ret &= glIsEnabled(GL_BLEND) == GL_FALSE;
  1148. ret &= glIsEnabled(GL_CULL_FACE) == GL_FALSE;
  1149. GLint temp;
  1150. #if defined(TORQUE_OS_IOS) || defined(TORQUE_OS_ANDROID) || defined(TORQUE_OS_EMSCRIPTEN)
  1151. // PUAP -Mat removed unsupported textureARB and Fog stuff
  1152. if (dglDoesSupportARBMultitexture() == true) {
  1153. //glActiveTextureARB(GL_TEXTURE1_ARB);
  1154. ret &= glIsEnabled(GL_TEXTURE_2D) == GL_FALSE;
  1155. glGetTexEnviv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, &temp);
  1156. ret &= temp == GL_REPLACE;
  1157. //glActiveTextureARB(GL_TEXTURE0_ARB);
  1158. ret &= glIsEnabled(GL_TEXTURE_2D) == GL_FALSE;
  1159. glGetTexEnviv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, &temp);
  1160. ret &= temp == GL_REPLACE;
  1161. //glClientActiveTextureARB(GL_TEXTURE1_ARB);
  1162. ret &= glIsEnabled(GL_TEXTURE_COORD_ARRAY) == GL_FALSE;
  1163. //glClientActiveTextureARB(GL_TEXTURE0_ARB);
  1164. ret &= glIsEnabled(GL_TEXTURE_COORD_ARRAY) == GL_FALSE;
  1165. } else {
  1166. ret &= glIsEnabled(GL_TEXTURE_2D) == GL_FALSE;
  1167. glGetTexEnviv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, &temp);
  1168. ret &= temp == GL_REPLACE;
  1169. ret &= glIsEnabled(GL_TEXTURE_COORD_ARRAY) == GL_FALSE;
  1170. }
  1171. ret &= glIsEnabled(GL_LIGHTING) == GL_FALSE;
  1172. ret &= glIsEnabled(GL_COLOR_ARRAY) == GL_FALSE;
  1173. ret &= glIsEnabled(GL_VERTEX_ARRAY) == GL_FALSE;
  1174. ret &= glIsEnabled(GL_NORMAL_ARRAY) == GL_FALSE;
  1175. //if (dglDoesSupportFogCoord())
  1176. //ret &= glIsEnabled(GL_FOG_COORDINATE_ARRAY_EXT) == GL_FALSE;
  1177. #else
  1178. if (dglDoesSupportARBMultitexture() == true) {
  1179. glActiveTextureARB(GL_TEXTURE1_ARB);
  1180. ret &= glIsEnabled(GL_TEXTURE_2D) == GL_FALSE;
  1181. glGetTexEnviv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, &temp);
  1182. ret &= temp == GL_REPLACE;
  1183. glActiveTextureARB(GL_TEXTURE0_ARB);
  1184. ret &= glIsEnabled(GL_TEXTURE_2D) == GL_FALSE;
  1185. glGetTexEnviv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, &temp);
  1186. ret &= temp == GL_REPLACE;
  1187. glClientActiveTextureARB(GL_TEXTURE1_ARB);
  1188. ret &= glIsEnabled(GL_TEXTURE_COORD_ARRAY) == GL_FALSE;
  1189. glClientActiveTextureARB(GL_TEXTURE0_ARB);
  1190. ret &= glIsEnabled(GL_TEXTURE_COORD_ARRAY) == GL_FALSE;
  1191. } else {
  1192. ret &= glIsEnabled(GL_TEXTURE_2D) == GL_FALSE;
  1193. glGetTexEnviv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, &temp);
  1194. ret &= temp == GL_REPLACE;
  1195. ret &= glIsEnabled(GL_TEXTURE_COORD_ARRAY) == GL_FALSE;
  1196. }
  1197. ret &= glIsEnabled(GL_LIGHTING) == GL_FALSE;
  1198. ret &= glIsEnabled(GL_COLOR_ARRAY) == GL_FALSE;
  1199. ret &= glIsEnabled(GL_VERTEX_ARRAY) == GL_FALSE;
  1200. ret &= glIsEnabled(GL_NORMAL_ARRAY) == GL_FALSE;
  1201. if (dglDoesSupportFogCoord())
  1202. ret &= glIsEnabled(GL_FOG_COORDINATE_ARRAY_EXT) == GL_FALSE;
  1203. #endif
  1204. return ret;
  1205. }
  1206. void dglSetCanonicalState()
  1207. {
  1208. #if defined(TORQUE_OS_IOS) || defined(TORQUE_OS_ANDROID) || defined(TORQUE_OS_EMSCRIPTEN)
  1209. // PUAP -Mat removed unsupported textureARB and Fog stuff
  1210. glDisable(GL_BLEND);
  1211. glDisable(GL_CULL_FACE);
  1212. glBlendFunc(GL_ONE, GL_ZERO);
  1213. glDisable(GL_LIGHTING);
  1214. if (dglDoesSupportARBMultitexture() == true) {
  1215. //glActiveTextureARB(GL_TEXTURE1_ARB);
  1216. glDisable(GL_TEXTURE_2D);
  1217. glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
  1218. //glActiveTextureARB(GL_TEXTURE0_ARB);
  1219. glDisable(GL_TEXTURE_2D);
  1220. glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
  1221. } else {
  1222. glDisable(GL_TEXTURE_2D);
  1223. glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
  1224. }
  1225. glDisableClientState(GL_COLOR_ARRAY);
  1226. glDisableClientState(GL_VERTEX_ARRAY);
  1227. glDisableClientState(GL_NORMAL_ARRAY);
  1228. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  1229. //if (dglDoesSupportFogCoord())
  1230. //glDisableClientState(GL_FOG_COORDINATE_ARRAY_EXT);
  1231. #else
  1232. glDisable(GL_BLEND);
  1233. glDisable(GL_CULL_FACE);
  1234. glBlendFunc(GL_ONE, GL_ZERO);
  1235. glDisable(GL_LIGHTING);
  1236. if (dglDoesSupportARBMultitexture() == true) {
  1237. glActiveTextureARB(GL_TEXTURE1_ARB);
  1238. glDisable(GL_TEXTURE_2D);
  1239. glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
  1240. glActiveTextureARB(GL_TEXTURE0_ARB);
  1241. glDisable(GL_TEXTURE_2D);
  1242. glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
  1243. } else {
  1244. glDisable(GL_TEXTURE_2D);
  1245. glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
  1246. }
  1247. glDisableClientState(GL_COLOR_ARRAY);
  1248. glDisableClientState(GL_VERTEX_ARRAY);
  1249. glDisableClientState(GL_NORMAL_ARRAY);
  1250. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  1251. if (dglDoesSupportFogCoord())
  1252. glDisableClientState(GL_FOG_COORDINATE_ARRAY_EXT);
  1253. #endif
  1254. }
  1255. void dglGetTransformState(S32* mvDepth,
  1256. S32* pDepth,
  1257. S32* t0Depth,
  1258. F32* t0Matrix,
  1259. S32* t1Depth,
  1260. F32* t1Matrix,
  1261. S32* vp)
  1262. {
  1263. glGetIntegerv(GL_MODELVIEW_STACK_DEPTH, (GLint*)mvDepth);
  1264. glGetIntegerv(GL_PROJECTION_STACK_DEPTH, (GLint*)pDepth);
  1265. glGetIntegerv(GL_TEXTURE_STACK_DEPTH, (GLint*)t0Depth);
  1266. glGetFloatv(GL_TEXTURE_MATRIX, t0Matrix);
  1267. if (dglDoesSupportARBMultitexture())
  1268. {
  1269. #if defined(TORQUE_OS_IOS) || defined(TORQUE_OS_ANDROID) || defined(TORQUE_OS_EMSCRIPTEN)
  1270. // PUAP -Mat removed unsupported textureARB stuff
  1271. //glActiveTextureARB(GL_TEXTURE1_ARB);
  1272. glGetIntegerv(GL_TEXTURE_STACK_DEPTH, (GLint*)t1Depth);
  1273. glGetFloatv(GL_TEXTURE_MATRIX, t1Matrix);
  1274. //glActiveTextureARB(GL_TEXTURE0_ARB);
  1275. #else
  1276. glActiveTextureARB(GL_TEXTURE1_ARB);
  1277. glGetIntegerv(GL_TEXTURE_STACK_DEPTH, (GLint*)t1Depth);
  1278. glGetFloatv(GL_TEXTURE_MATRIX, t1Matrix);
  1279. glActiveTextureARB(GL_TEXTURE0_ARB);
  1280. #endif
  1281. }
  1282. else
  1283. {
  1284. *t1Depth = 0;
  1285. for (U32 i = 0; i < 16; i++)
  1286. t1Matrix[i] = 0;
  1287. }
  1288. RectI v;
  1289. dglGetViewport(&v);
  1290. vp[0] = v.point.x;
  1291. vp[1] = v.point.y;
  1292. vp[2] = v.extent.x;
  1293. vp[3] = v.extent.y;
  1294. }
  1295. bool dglCheckState(const S32 mvDepth, const S32 pDepth,
  1296. const S32 t0Depth, const F32* t0Matrix,
  1297. const S32 t1Depth, const F32* t1Matrix,
  1298. const S32* vp)
  1299. {
  1300. GLint md, pd;
  1301. RectI v;
  1302. glGetIntegerv(GL_MODELVIEW_STACK_DEPTH, &md);
  1303. glGetIntegerv(GL_PROJECTION_STACK_DEPTH, &pd);
  1304. GLint t0d, t1d;
  1305. GLfloat t0m[16], t1m[16];
  1306. glGetIntegerv(GL_TEXTURE_STACK_DEPTH, &t0d);
  1307. glGetFloatv(GL_TEXTURE_MATRIX, t0m);
  1308. if (dglDoesSupportARBMultitexture())
  1309. {
  1310. #if defined(TORQUE_OS_IOS) || defined(TORQUE_OS_ANDROID) || defined(TORQUE_OS_EMSCRIPTEN)
  1311. // PUAP -Mat removed unsupported textureARB and Fog stuff
  1312. //glActiveTextureARB(GL_TEXTURE1_ARB);
  1313. glGetIntegerv(GL_TEXTURE_STACK_DEPTH, &t1d);
  1314. glGetFloatv(GL_TEXTURE_MATRIX, t1m);
  1315. //glActiveTextureARB(GL_TEXTURE0_ARB);
  1316. #else
  1317. glActiveTextureARB(GL_TEXTURE1_ARB);
  1318. glGetIntegerv(GL_TEXTURE_STACK_DEPTH, &t1d);
  1319. glGetFloatv(GL_TEXTURE_MATRIX, t1m);
  1320. glActiveTextureARB(GL_TEXTURE0_ARB);
  1321. #endif
  1322. }
  1323. else
  1324. {
  1325. t1d = 0;
  1326. for (U32 i = 0; i < 16; i++)
  1327. t1m[i] = 0;
  1328. }
  1329. dglGetViewport(&v);
  1330. return ((md == mvDepth) &&
  1331. (pd == pDepth) &&
  1332. (t0d == t0Depth) &&
  1333. (dMemcmp(t0m, t0Matrix, sizeof(F32) * 16) == 0) &&
  1334. (t1d == t1Depth) &&
  1335. (dMemcmp(t1m, t1Matrix, sizeof(F32) * 16) == 0) &&
  1336. ((v.point.x == vp[0]) &&
  1337. (v.point.y == vp[1]) &&
  1338. (v.extent.x == vp[2]) &&
  1339. (v.extent.y == vp[3])));
  1340. }
  1341. #if defined(TORQUE_OS_IOS) || defined(TORQUE_OS_ANDROID) || defined(TORQUE_OS_EMSCRIPTEN)
  1342. GLfloat gVertexFloats[8];
  1343. GLfloat gTextureVerts[8];
  1344. #endif
  1345. //--------------------------------------------------------------------------
  1346. // Function to draw a box which can have 4 different colors in each corner blended together
  1347. #if defined(TORQUE_OS_IOS) || defined(TORQUE_OS_ANDROID) || defined(TORQUE_OS_EMSCRIPTEN)
  1348. void dglDrawBlendBox(const RectI& bounds, ColorF& c1, ColorF& c2, ColorF& c3, ColorF& c4)
  1349. {
  1350. GLfloat left = bounds.point.x, right = bounds.point.x + bounds.extent.x;
  1351. GLfloat top = bounds.point.y, bottom = bounds.point.y + bounds.extent.y;
  1352. glEnable(GL_BLEND);
  1353. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  1354. glDisable(GL_TEXTURE_2D);
  1355. const GLfloat verts[] = {
  1356. left, top,
  1357. right, top,
  1358. left, bottom,
  1359. right, bottom,
  1360. };
  1361. const GLubyte squareColors[] = {
  1362. static_cast<GLubyte>(255 * c1.red), static_cast<GLubyte>(255 * c1.green), static_cast<GLubyte>(255 * c1.blue), static_cast<GLubyte>(255 * c1.alpha),
  1363. static_cast<GLubyte>(255 * c2.red), static_cast<GLubyte>(255 * c2.green), static_cast<GLubyte>(255 * c2.blue), static_cast<GLubyte>(255 * c2.alpha),
  1364. static_cast<GLubyte>(255 * c3.red), static_cast<GLubyte>(255 * c3.green), static_cast<GLubyte>(255 * c3.blue), static_cast<GLubyte>(255 * c3.alpha),
  1365. static_cast<GLubyte>(255 * c4.red), static_cast<GLubyte>(255 * c4.green), static_cast<GLubyte>(255 * c4.blue), static_cast<GLubyte>(255 * c4.alpha),
  1366. };
  1367. glVertexPointer(2, GL_FLOAT, 0, verts);
  1368. glEnableClientState(GL_VERTEX_ARRAY);
  1369. glColorPointer(4, GL_UNSIGNED_BYTE, 0, squareColors);
  1370. glEnableClientState(GL_COLOR_ARRAY);
  1371. glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
  1372. }
  1373. //--------------------------------------------------------------------------
  1374. /// Function to draw a set of boxes blending throughout an array of colors
  1375. void dglDrawBlendRangeBox(const RectI& bounds, bool vertical, U8 numColors, ColorI* colors)
  1376. {
  1377. S32 left = bounds.point.x, right = bounds.point.x + bounds.extent.x;
  1378. S32 top = bounds.point.y, bottom = bounds.point.y + bounds.extent.y;
  1379. // Calculate increment value
  1380. S32 x_inc = S32(mFloor((right - left) / (numColors - 1)));
  1381. S32 y_inc = S32(mFloor((bottom - top) / (numColors - 1)));
  1382. glEnable(GL_BLEND);
  1383. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  1384. glDisable(GL_TEXTURE_2D);
  1385. GLfloat verts[] = {
  1386. 0.0f, 0.0f, 0.0f, 0.0f,
  1387. 0.0f, 0.0f, 0.0f, 0.0f,
  1388. };
  1389. glVertexPointer(2, GL_FLOAT, 0, verts);
  1390. glEnableClientState(GL_VERTEX_ARRAY);
  1391. for (U16 i = 0; i < numColors - 1; i++)
  1392. {
  1393. // If we are at the end, x_inc and y_inc need to go to the end (otherwise there is a rendering bug)
  1394. if (i == numColors - 2)
  1395. {
  1396. x_inc += right - left - 1;
  1397. y_inc += bottom - top - 1;
  1398. }
  1399. if (vertical) // Vertical (+y) colors go up and down
  1400. {
  1401. // First color
  1402. glColor4ub(colors[i].red, colors[i].green, colors[i].blue, colors[i].alpha);
  1403. verts[0] = (F32)left;
  1404. verts[1] = (F32)top;
  1405. verts[2] = (F32)(left + x_inc);
  1406. verts[3] = (F32)top;
  1407. // Second color
  1408. glColor4ub(colors[i + 1].red, colors[i + 1].green, colors[i + 1].blue, colors[i + 1].alpha);
  1409. verts[4] = (F32)left;
  1410. verts[5] = (F32)bottom;
  1411. verts[6] = (F32)(left + x_inc);
  1412. verts[7] = (F32)bottom;
  1413. glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
  1414. left += x_inc;
  1415. }
  1416. else // Horizontal (+x) colors go lateral
  1417. {
  1418. // First color
  1419. glColor4ub(colors[i].red, colors[i].green, colors[i].blue, colors[i].alpha);
  1420. verts[0] = (F32)left;
  1421. verts[1] = (F32)(top + y_inc);
  1422. verts[2] = (F32)right;
  1423. verts[3] = (F32)(top + y_inc);
  1424. // Second color
  1425. glColor4ub(colors[i + 1].red, colors[i + 1].green, colors[i + 1].blue, colors[i + 1].alpha);
  1426. verts[4] = (F32)left;
  1427. verts[5] = (F32)top;
  1428. verts[6] = (F32)right;
  1429. verts[7] = (F32)top;
  1430. glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
  1431. top += y_inc;
  1432. }
  1433. }
  1434. }
  1435. #else
  1436. void dglDrawBlendBox(const RectI& bounds, ColorF& c1, ColorF& c2, ColorF& c3, ColorF& c4)
  1437. {
  1438. F32 l = (F32)bounds.point.x;
  1439. F32 r = (F32)(bounds.point.x + bounds.extent.x);
  1440. F32 t = (F32)bounds.point.y;
  1441. F32 b = (F32)(bounds.point.y + bounds.extent.y);
  1442. glEnable(GL_BLEND);
  1443. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  1444. glDisable(GL_TEXTURE_2D);
  1445. glBegin(GL_QUADS);
  1446. // Color
  1447. glColor4fv(c1.address());
  1448. glVertex2f(l, t);
  1449. glColor4fv(c4.address());
  1450. glVertex2f(l, b);
  1451. glColor4fv(c3.address());
  1452. glVertex2f(r, b);
  1453. glColor4fv(c2.address());
  1454. glVertex2f(r, t);
  1455. glEnd();
  1456. }
  1457. //--------------------------------------------------------------------------
  1458. /// Function to draw a set of boxes blending throughout an array of colors
  1459. void dglDrawBlendRangeBox(const RectI& bounds, bool vertical, U8 numColors, ColorI* colors)
  1460. {
  1461. F32 l = (F32)bounds.point.x;
  1462. F32 r = (F32)(bounds.point.x + bounds.extent.x);
  1463. F32 t = (F32)bounds.point.y;
  1464. F32 b = (F32)(bounds.point.y + bounds.extent.y);
  1465. // Calculate increment value
  1466. F32 x_inc = F32((r - l) / (numColors - 1));
  1467. F32 y_inc = F32((b - t) / (numColors - 1));
  1468. glEnable(GL_BLEND);
  1469. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  1470. glDisable(GL_TEXTURE_2D);
  1471. glBegin(GL_QUADS);
  1472. if (!vertical) // Horizontal (+x)
  1473. {
  1474. glColor4ub(colors[0].red, colors[0].green, colors[0].blue, colors[0].alpha);
  1475. glVertex2f(l, t);
  1476. glVertex2f(l, b);
  1477. glVertex2f(l - 1, b);
  1478. glVertex2f(l - 1, t);
  1479. glVertex2f(r, t);
  1480. glVertex2f(r, b);
  1481. glVertex2f(r + 1, b);
  1482. glVertex2f(r + 1, t);
  1483. }
  1484. else // Vertical (+y)
  1485. {
  1486. glColor4ub(colors[0].red, colors[0].green, colors[0].blue, colors[0].alpha);
  1487. glVertex2f(l, t);
  1488. glVertex2f(r, t);
  1489. glVertex2f(r, t - 1);
  1490. glVertex2f(l, t - 1);
  1491. glVertex2f(l, b);
  1492. glVertex2f(r, b);
  1493. glVertex2f(r, b + 1);
  1494. glVertex2f(l, b + 1);
  1495. }
  1496. for (U16 i = 0; i < numColors - 1; i++)
  1497. {
  1498. if (!vertical) // Horizontal (+x)
  1499. {
  1500. // First color
  1501. glColor4ub(colors[i].red, colors[i].green, colors[i].blue, colors[i].alpha);
  1502. glVertex2f(l, t);
  1503. glVertex2f(l, b);
  1504. // Second color
  1505. glColor4ub(colors[i + 1].red, colors[i + 1].green, colors[i + 1].blue, colors[i + 1].alpha);
  1506. glVertex2f(l + x_inc, b);
  1507. glVertex2f(l + x_inc, t);
  1508. l += x_inc;
  1509. }
  1510. else // Vertical (+y)
  1511. {
  1512. // First color
  1513. glColor4ub(colors[i].red, colors[i].green, colors[i].blue, colors[i].alpha);
  1514. glVertex2f(l, t);
  1515. glVertex2f(r, t);
  1516. // Second color
  1517. glColor4ub(colors[i + 1].red, colors[i + 1].green, colors[i + 1].blue, colors[i + 1].alpha);
  1518. glVertex2f(r, t + y_inc);
  1519. glVertex2f(l, t + y_inc);
  1520. t += y_inc;
  1521. }
  1522. }
  1523. glEnd();
  1524. }
  1525. #endif
  1526. void dglRenderCheckers(const RectI& bounds, const U8 size, const ColorF& c1, const ColorF& c2)
  1527. {
  1528. F32 l = (F32)bounds.point.x;
  1529. F32 r = (F32)(bounds.point.x + bounds.extent.x);
  1530. F32 t = (F32)bounds.point.y;
  1531. F32 b = (F32)(bounds.point.y + bounds.extent.y);
  1532. glDisable(GL_BLEND);
  1533. glDisable(GL_TEXTURE_2D);
  1534. glBegin(GL_QUADS);
  1535. glColor4fv(c2.address());
  1536. glVertex2f(l, t);
  1537. glColor4fv(c2.address());
  1538. glVertex2f(l, b);
  1539. glColor4fv(c2.address());
  1540. glVertex2f(r, b);
  1541. glColor4fv(c2.address());
  1542. glVertex2f(r, t);
  1543. glEnd();
  1544. for (U32 y = 0; y < bounds.extent.y; y += size)
  1545. {
  1546. for (U32 x = ((y / size) % 2 == 0 ? 0 : size); x < bounds.extent.x; x += (size * 2))
  1547. {
  1548. glBegin(GL_QUADS);
  1549. glColor4fv(c1.address());
  1550. glVertex2f(l + x, t + y);
  1551. glColor4fv(c1.address());
  1552. glVertex2f(l + x, getMin(b, t + y + size));
  1553. glColor4fv(c1.address());
  1554. glVertex2f(getMin(r, l + x + size), getMin(b, t + y + size));
  1555. glColor4fv(c1.address());
  1556. glVertex2f(getMin(r, l + x + size), t + y);
  1557. glEnd();
  1558. }
  1559. }
  1560. }