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) || defined(TORQUE_OS_EMSCRIPTEN)
  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) || defined(TORQUE_OS_EMSCRIPTEN)
  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. MatrixF rotMatrix( EulerF( 0.0, 0.0, mDegToRad( rot ) ) );
  312. Point3F offset( ptDraw.x, ptDraw.y, 0.0 );
  313. Point3F points[4];
  314. U32 nCharCount = 0;
  315. Point2I pt;
  316. UTF16 c;
  317. pt.x = 0;
  318. ColorI currentColor;
  319. S32 currentPt = 0;
  320. TextureObject *lastTexture = NULL;
  321. currentColor = sg_bitmapModulation;
  322. FrameTemp<TextVertex> vert(4*n);
  323. glDisable(GL_LIGHTING);
  324. glEnable(GL_TEXTURE_2D);
  325. glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  326. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  327. glEnable(GL_BLEND);
  328. //Luma: Optimise by setting states once before inner loop
  329. glEnableClientState ( GL_VERTEX_ARRAY );
  330. glEnableClientState ( GL_COLOR_ARRAY );
  331. glEnableClientState ( GL_TEXTURE_COORD_ARRAY );
  332. glVertexPointer ( 2, GL_FLOAT, sizeof(TextVertex), &(vert[0].p) );
  333. glColorPointer ( 4, GL_UNSIGNED_BYTE, sizeof(TextVertex), &(vert[0].c) );
  334. glTexCoordPointer ( 2, GL_FLOAT, sizeof(TextVertex), &(vert[0].t) );
  335. // first build the point, color, and coord arrays
  336. U32 i;
  337. for(i = 0,c = in_string[i];in_string[i] && i < n;i++,c = in_string[i])
  338. {
  339. nCharCount++;
  340. if(nCharCount > n)
  341. break;
  342. // We have to do a little dance here since \t = 0x9, \n = 0xa, and \r = 0xd
  343. if ((c >= 1 && c <= 7) ||
  344. (c >= 11 && c <= 12) ||
  345. (c == 14))
  346. {
  347. // Color code
  348. if (colorTable)
  349. {
  350. static U8 remap[15] =
  351. {
  352. 0x0, // 0 special null terminator
  353. 0x0, // 1 ascii start-of-heading??
  354. 0x1,
  355. 0x2,
  356. 0x3,
  357. 0x4,
  358. 0x5,
  359. 0x6,
  360. 0x0, // 8 special backspace
  361. 0x0, // 9 special tab
  362. 0x0, // a special \n
  363. 0x7,
  364. 0x8,
  365. 0x0, // a special \r
  366. 0x9
  367. };
  368. U8 remapped = remap[c];
  369. // Ignore if the color is greater than the specified max index:
  370. if ( remapped <= maxColorIndex )
  371. {
  372. const ColorI &clr = colorTable[remapped];
  373. sg_bitmapModulation = clr;
  374. currentColor = clr;
  375. }
  376. }
  377. continue;
  378. }
  379. // reset color?
  380. if ( c == 15 )
  381. {
  382. currentColor = sg_textAnchorColor;
  383. sg_bitmapModulation = sg_textAnchorColor;
  384. continue;
  385. }
  386. // push color:
  387. if ( c == 16 )
  388. {
  389. sg_stackColor = sg_bitmapModulation;
  390. continue;
  391. }
  392. // pop color:
  393. if ( c == 17 )
  394. {
  395. currentColor = sg_stackColor;
  396. sg_bitmapModulation = sg_stackColor;
  397. continue;
  398. }
  399. // Tab character
  400. if ( c == dT('\t') )
  401. {
  402. const PlatformFont::CharInfo &ci = font->getCharInfo( dT(' ') );
  403. pt.x += ci.xIncrement * GFont::TabWidthInSpaces;
  404. continue;
  405. }
  406. if( !font->isValidChar( c ) )
  407. continue;
  408. const PlatformFont::CharInfo &ci = font->getCharInfo(c);
  409. if(ci.bitmapIndex == -1)
  410. {
  411. pt.x += ci.xOrigin + ci.xIncrement;
  412. continue;
  413. }
  414. TextureObject *newObj = font->getTextureHandle(ci.bitmapIndex);
  415. if(newObj != lastTexture)
  416. {
  417. if(currentPt)
  418. {
  419. glBindTexture(GL_TEXTURE_2D, lastTexture->getGLTextureName());
  420. //Luma: More optimal rendering
  421. for (S32 i=0; i<currentPt; i+=4)
  422. {
  423. glDrawArrays(GL_TRIANGLE_STRIP, i, 4);
  424. }
  425. currentPt = 0;
  426. }
  427. lastTexture = newObj;
  428. }
  429. if(ci.width != 0 && ci.height != 0)
  430. {
  431. pt.y = font->getBaseline() - ci.yOrigin;
  432. pt.x += ci.xOrigin;
  433. F32 texLeft = F32(ci.xOffset) / F32(lastTexture->getTextureWidth());
  434. F32 texRight = F32(ci.xOffset + ci.width) / F32(lastTexture->getTextureWidth());
  435. F32 texTop = F32(ci.yOffset) / F32(lastTexture->getTextureHeight());
  436. F32 texBottom = F32(ci.yOffset + ci.height) / F32(lastTexture->getTextureHeight());
  437. F32 screenLeft = pt.x;
  438. F32 screenRight = pt.x + ci.width;
  439. F32 screenTop = pt.y;
  440. F32 screenBottom = pt.y + ci.height;
  441. points[0] = Point3F(screenLeft, screenTop, 0.0);
  442. points[1] = Point3F(screenRight, screenTop, 0.0);
  443. points[2] = Point3F( screenLeft, screenBottom, 0.0);
  444. points[3] = Point3F( screenRight, screenBottom, 0.0);
  445. for( int i=0; i<4; i++ )
  446. {
  447. rotMatrix.mulP( points[i] );
  448. points[i] += offset;
  449. }
  450. vert[currentPt++].set(points[0].x, points[0].y, texLeft, texTop, currentColor);
  451. vert[currentPt++].set(points[1].x, points[1].y, texRight, texTop, currentColor);
  452. vert[currentPt++].set(points[2].x, points[2].y, texLeft, texBottom, currentColor);
  453. vert[currentPt++].set(points[3].x, points[3].y, texRight, texBottom, currentColor);
  454. pt.x += ci.xIncrement - ci.xOrigin;
  455. }
  456. else
  457. pt.x += ci.xIncrement;
  458. }
  459. if(currentPt)
  460. {
  461. //Luma: More optimal rendering
  462. glBindTexture(GL_TEXTURE_2D, lastTexture->getGLTextureName());
  463. for (S32 i=0; i<currentPt; i+=4)
  464. {
  465. glDrawArrays(GL_TRIANGLE_STRIP, i, 4);
  466. }
  467. }
  468. glDisableClientState ( GL_VERTEX_ARRAY );
  469. glDisableClientState ( GL_COLOR_ARRAY );
  470. glDisableClientState ( GL_TEXTURE_COORD_ARRAY );
  471. glDisable(GL_BLEND);
  472. glDisable(GL_TEXTURE_2D);
  473. pt.x += ptDraw.x; // DAW: Account for the fact that we removed the drawing point from the text start at the beginning.
  474. AssertFatal(pt.x >= ptDraw.x, "How did this happen?");
  475. PROFILE_END();
  476. return pt.x - ptDraw.x;
  477. }
  478. #else
  479. U32 dglDrawTextN(GFont* font,
  480. const Point2I& ptDraw,
  481. const UTF16* in_string,
  482. U32 n,
  483. const ColorI* colorTable,
  484. const U32 maxColorIndex,
  485. F32 rot)
  486. {
  487. // return on zero length strings
  488. if( n < 1 )
  489. return ptDraw.x;
  490. PROFILE_START(DrawText);
  491. MatrixF rotMatrix( EulerF( 0.0, 0.0, mDegToRad( rot ) ) );
  492. Point3F offset( (F32)ptDraw.x, (F32)ptDraw.y, 0.0f );
  493. Point3F points[4];
  494. U32 nCharCount = 0;
  495. Point2I pt;
  496. UTF16 c;
  497. pt.x = 0;
  498. ColorI currentColor;
  499. S32 currentPt = 0;
  500. TextureObject *lastTexture = NULL;
  501. currentColor = sg_bitmapModulation;
  502. FrameTemp<TextVertex> vert(4*n);
  503. glDisable(GL_LIGHTING);
  504. glEnable(GL_TEXTURE_2D);
  505. glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  506. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  507. glEnable(GL_BLEND);
  508. glEnableClientState ( GL_VERTEX_ARRAY );
  509. glVertexPointer ( 2, GL_FLOAT, sizeof(TextVertex), &(vert[0].p) );
  510. glEnableClientState ( GL_COLOR_ARRAY );
  511. glColorPointer ( 4, GL_UNSIGNED_BYTE, sizeof(TextVertex), &(vert[0].c) );
  512. glEnableClientState ( GL_TEXTURE_COORD_ARRAY );
  513. glTexCoordPointer ( 2, GL_FLOAT, sizeof(TextVertex), &(vert[0].t) );
  514. // first build the point, color, and coord arrays
  515. U32 i;
  516. for(i = 0,c = in_string[i];in_string[i] && i < n;i++,c = in_string[i])
  517. {
  518. nCharCount++;
  519. if(nCharCount > n)
  520. break;
  521. // We have to do a little dance here since \t = 0x9, \n = 0xa, and \r = 0xd
  522. if ((c >= 1 && c <= 7) ||
  523. (c >= 11 && c <= 12) ||
  524. (c == 14))
  525. {
  526. // Color code
  527. if (colorTable)
  528. {
  529. static U8 remap[15] =
  530. {
  531. 0x0, // 0 special null terminator
  532. 0x0, // 1 ascii start-of-heading??
  533. 0x1,
  534. 0x2,
  535. 0x3,
  536. 0x4,
  537. 0x5,
  538. 0x6,
  539. 0x0, // 8 special backspace
  540. 0x0, // 9 special tab
  541. 0x0, // a special \n
  542. 0x7,
  543. 0x8,
  544. 0x0, // a special \r
  545. 0x9
  546. };
  547. U8 remapped = remap[c];
  548. // Ignore if the color is greater than the specified max index:
  549. if ( remapped <= maxColorIndex )
  550. {
  551. const ColorI &clr = colorTable[remapped];
  552. sg_bitmapModulation = clr;
  553. currentColor = clr;
  554. }
  555. }
  556. continue;
  557. }
  558. // reset color?
  559. if ( c == 15 )
  560. {
  561. currentColor = sg_textAnchorColor;
  562. sg_bitmapModulation = sg_textAnchorColor;
  563. continue;
  564. }
  565. // push color:
  566. if ( c == 16 )
  567. {
  568. sg_stackColor = sg_bitmapModulation;
  569. continue;
  570. }
  571. // pop color:
  572. if ( c == 17 )
  573. {
  574. currentColor = sg_stackColor;
  575. sg_bitmapModulation = sg_stackColor;
  576. continue;
  577. }
  578. // Tab character
  579. if ( c == dT('\t') )
  580. {
  581. const PlatformFont::CharInfo &ci = font->getCharInfo( dT(' ') );
  582. pt.x += ci.xIncrement * GFont::TabWidthInSpaces;
  583. continue;
  584. }
  585. if( !font->isValidChar( c ) )
  586. continue;
  587. const PlatformFont::CharInfo &ci = font->getCharInfo(c);
  588. if(ci.bitmapIndex == -1)
  589. {
  590. pt.x += ci.xOrigin + ci.xIncrement;
  591. continue;
  592. }
  593. TextureObject *newObj = font->getTextureHandle(ci.bitmapIndex);
  594. if(newObj != lastTexture)
  595. {
  596. if(currentPt)
  597. {
  598. glBindTexture(GL_TEXTURE_2D, lastTexture->getGLTextureName());
  599. glDrawArrays( GL_QUADS, 0, currentPt );
  600. currentPt = 0;
  601. }
  602. lastTexture = newObj;
  603. }
  604. if(ci.width != 0 && ci.height != 0)
  605. {
  606. pt.y = font->getBaseline() - ci.yOrigin;
  607. pt.x += ci.xOrigin;
  608. F32 texLeft = F32(ci.xOffset) / F32(lastTexture->getTextureWidth());
  609. F32 texRight = F32(ci.xOffset + ci.width) / F32(lastTexture->getTextureWidth());
  610. F32 texTop = F32(ci.yOffset) / F32(lastTexture->getTextureHeight());
  611. F32 texBottom = F32(ci.yOffset + ci.height) / F32(lastTexture->getTextureHeight());
  612. F32 screenLeft = (F32)pt.x;
  613. F32 screenRight = (F32)(pt.x + ci.width);
  614. F32 screenTop = (F32)pt.y;
  615. F32 screenBottom = (F32)(pt.y + ci.height);
  616. points[0] = Point3F(screenLeft, screenBottom, 0.0);
  617. points[1] = Point3F(screenRight, screenBottom, 0.0);
  618. points[2] = Point3F( screenRight, screenTop, 0.0);
  619. points[3] = Point3F( screenLeft, screenTop, 0.0);
  620. for( int i=0; i<4; i++ )
  621. {
  622. rotMatrix.mulP( points[i] );
  623. points[i] += offset;
  624. }
  625. vert[currentPt++].set(points[0].x, points[0].y, texLeft, texBottom, currentColor);
  626. vert[currentPt++].set(points[1].x, points[1].y, texRight, texBottom, currentColor);
  627. vert[currentPt++].set(points[2].x, points[2].y, texRight, texTop, currentColor);
  628. vert[currentPt++].set(points[3].x, points[3].y, texLeft, texTop, currentColor);
  629. pt.x += ci.xIncrement - ci.xOrigin;
  630. }
  631. else
  632. pt.x += ci.xIncrement;
  633. }
  634. if(currentPt)
  635. {
  636. glBindTexture(GL_TEXTURE_2D, lastTexture->getGLTextureName());
  637. glDrawArrays( GL_QUADS, 0, currentPt );
  638. }
  639. glDisableClientState ( GL_VERTEX_ARRAY );
  640. glDisableClientState ( GL_COLOR_ARRAY );
  641. glDisableClientState ( GL_TEXTURE_COORD_ARRAY );
  642. glDisable(GL_BLEND);
  643. glDisable(GL_TEXTURE_2D);
  644. pt.x += ptDraw.x; // DAW: Account for the fact that we removed the drawing point from the text start at the beginning.
  645. AssertFatal(pt.x >= ptDraw.x, "How did this happen?");
  646. PROFILE_END();
  647. return pt.x - ptDraw.x;
  648. }
  649. #endif
  650. // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- //
  651. // Drawing primitives
  652. void dglDrawLine(S32 x1, S32 y1, S32 x2, S32 y2, const ColorI &color)
  653. {
  654. glEnable(GL_BLEND);
  655. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  656. glDisable(GL_TEXTURE_2D);
  657. glColor4ub(color.red, color.green, color.blue, color.alpha);
  658. #if defined(TORQUE_OS_IOS) || defined(TORQUE_OS_ANDROID) || defined(TORQUE_OS_EMSCRIPTEN)
  659. GLfloat verts[] = {
  660. (GLfloat)(x1 + 0.5f), (GLfloat)(y1 + 0.5f),
  661. (GLfloat)(x2 + 0.5f), (GLfloat)(y2 + 0.5f),
  662. };
  663. glVertexPointer(2, GL_FLOAT, 0, verts );
  664. glDrawArrays(GL_LINES, 0, 2);//draw last two
  665. #else
  666. glBegin(GL_LINES);
  667. glVertex2f((F32)x1 + 0.5f, (F32)y1 + 0.5f);
  668. glVertex2f((F32)x2 + 0.5f, (F32)y2 + 0.5f);
  669. glEnd();
  670. //glBegin(GL_POINTS);
  671. //glVertex2f((F32)x2 + 0.5, (F32)y2 + 0.5);
  672. //glEnd();
  673. #endif
  674. }
  675. void dglDrawLine(const Point2I &startPt, const Point2I &endPt, const ColorI &color)
  676. {
  677. dglDrawLine(startPt.x, startPt.y, endPt.x, endPt.y, color);
  678. }
  679. void dglDrawRect(const Point2I &upperL, const Point2I &lowerR, const ColorI &color, const float &lineWidth)
  680. {
  681. glEnable(GL_BLEND);
  682. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  683. glDisable(GL_TEXTURE_2D);
  684. glLineWidth(lineWidth);
  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 verts[] = {
  688. (GLfloat)(upperL.x), (GLfloat)(upperL.y),
  689. (GLfloat)(lowerR.x), (GLfloat)(upperL.y),
  690. (GLfloat)(lowerR.x), (GLfloat)(lowerR.y),
  691. (GLfloat)(upperL.x), (GLfloat)(lowerR.y),
  692. };
  693. glVertexPointer(2, GL_FLOAT, 0, verts );
  694. glDrawArrays(GL_LINE_LOOP, 0, 4 );//draw last two
  695. #else
  696. glBegin(GL_LINE_LOOP);
  697. glVertex2f((F32)upperL.x + 0.5f, (F32)upperL.y + 0.5f);
  698. glVertex2f((F32)lowerR.x + 0.5f, (F32)upperL.y + 0.5f);
  699. glVertex2f((F32)lowerR.x + 0.5f, (F32)lowerR.y + 0.5f);
  700. glVertex2f((F32)upperL.x + 0.5f, (F32)lowerR.y + 0.5f);
  701. glEnd();
  702. #endif
  703. }
  704. // the fill convention for lined rects is that they outline the rectangle border of the
  705. // filled region specified.
  706. void dglDrawRect(const RectI &rect, const ColorI &color, const float &lineWidth)
  707. {
  708. Point2I lowerR(rect.point.x + rect.extent.x - 1, rect.point.y + rect.extent.y - 1);
  709. dglDrawRect(rect.point, lowerR, color, lineWidth);
  710. }
  711. // the fill convention says that pixel at upperL will be filled and
  712. // that pixel at lowerR will NOT be filled.
  713. void dglDrawRectFill(const Point2I &upperL, const Point2I &lowerR, const ColorI &color)
  714. {
  715. glEnable(GL_BLEND);
  716. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  717. glDisable(GL_TEXTURE_2D);
  718. glColor4ub(color.red, color.green, color.blue, color.alpha);
  719. #if defined(TORQUE_OS_IOS) || defined(TORQUE_OS_ANDROID) || defined(TORQUE_OS_EMSCRIPTEN)
  720. GLfloat vertices[] = {
  721. (GLfloat)upperL.x, (GLfloat)upperL.y,
  722. (GLfloat)upperL.x, (GLfloat)lowerR.y,
  723. (GLfloat)lowerR.x, (GLfloat)upperL.y,
  724. (GLfloat)lowerR.x, (GLfloat)lowerR.y,
  725. };
  726. glVertexPointer(2, GL_FLOAT, 0, vertices);
  727. glEnableClientState(GL_VERTEX_ARRAY);
  728. glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
  729. #else
  730. glRecti((S32)upperL.x, (S32)upperL.y, (S32)lowerR.x, (S32)lowerR.y);
  731. #endif
  732. }
  733. void dglDrawRectFill(const RectI &rect, const ColorI &color)
  734. {
  735. Point2I lowerR(rect.point.x + rect.extent.x, rect.point.y + rect.extent.y);
  736. dglDrawRectFill(rect.point, lowerR, color);
  737. }
  738. void dglDraw2DSquare( const Point2F &screenPoint, F32 width, F32 spinAngle )
  739. {
  740. width *= 0.5;
  741. MatrixF rotMatrix( EulerF( 0.0, 0.0, spinAngle ) );
  742. Point3F offset( screenPoint.x, screenPoint.y, 0.0 );
  743. Point3F points[4];
  744. points[0] = Point3F(-width, -width, 0.0);
  745. points[1] = Point3F(-width, width, 0.0);
  746. points[2] = Point3F( width, width, 0.0);
  747. points[3] = Point3F( width, -width, 0.0);
  748. for( int i=0; i<4; i++ )
  749. {
  750. rotMatrix.mulP( points[i] );
  751. points[i] += offset;
  752. }
  753. #if defined(TORQUE_OS_IOS) || defined(TORQUE_OS_ANDROID) || defined(TORQUE_OS_EMSCRIPTEN)
  754. GLfloat verts[] = {
  755. 0.0, 0.0,
  756. 1.0, 0.0,
  757. 0.0, 1.0,//may need to switch last two
  758. 1.0, 1.0,
  759. };
  760. GLfloat texVerts[] = {
  761. (GLfloat)points[0].x, (GLfloat)points[0].y,
  762. (GLfloat)points[1].x, (GLfloat)points[1].y,
  763. (GLfloat)points[3].x, (GLfloat)points[3].y,
  764. (GLfloat)points[2].x, (GLfloat)points[2].y,
  765. };
  766. glDisableClientState(GL_COLOR_ARRAY);
  767. //glDisableClientState(GL_POINT_SIZE_ARRAY_OES);
  768. glEnableClientState(GL_VERTEX_ARRAY);
  769. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  770. glVertexPointer(2, GL_FLOAT, 0, verts);
  771. glTexCoordPointer(2, GL_FLOAT, 0, texVerts);
  772. glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
  773. #else
  774. glBegin(GL_TRIANGLE_FAN);
  775. glTexCoord2f(0.0, 0.0);
  776. glVertex2fv(points[0]);
  777. glTexCoord2f(0.0, 1.0);
  778. glVertex2fv(points[1]);
  779. glTexCoord2f(1.0, 1.0);
  780. glVertex2fv(points[2]);
  781. glTexCoord2f(1.0, 0.0);
  782. glVertex2fv(points[3]);
  783. glEnd();
  784. #endif
  785. }
  786. void dglDrawBillboard( const Point3F &position, F32 width, F32 spinAngle )
  787. {
  788. MatrixF modelview;
  789. dglGetModelview( &modelview );
  790. modelview.transpose();
  791. width *= 0.5;
  792. Point3F points[4];
  793. points[0] = Point3F(-width, 0.0, -width);
  794. points[1] = Point3F(-width, 0.0, width);
  795. points[2] = Point3F( width, 0.0, width);
  796. points[3] = Point3F( width, 0.0, -width);
  797. MatrixF rotMatrix( EulerF( 0.0, spinAngle, 0.0 ) );
  798. for( int i=0; i<4; i++ )
  799. {
  800. rotMatrix.mulP( points[i] );
  801. modelview.mulP( points[i] );
  802. points[i] += position;
  803. }
  804. #if defined(TORQUE_OS_IOS) || defined(TORQUE_OS_ANDROID) || defined(TORQUE_OS_EMSCRIPTEN)
  805. GLfloat verts[] = {
  806. 0.0, 1.0,
  807. 0.0, 0.0,
  808. 1.0, 1.0,//may need to switch last two
  809. 1.0, 0.0,
  810. };
  811. GLfloat texVerts[] = {
  812. (GLfloat)points[0].x, (GLfloat)points[0].y,
  813. (GLfloat)points[1].x, (GLfloat)points[1].y,
  814. (GLfloat)points[3].x, (GLfloat)points[3].y,
  815. (GLfloat)points[2].x, (GLfloat)points[2].y,
  816. };
  817. glDisableClientState(GL_COLOR_ARRAY);
  818. //glDisableClientState(GL_POINT_SIZE_ARRAY_OES);
  819. glEnableClientState(GL_VERTEX_ARRAY);
  820. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  821. glVertexPointer(2, GL_FLOAT, 0, verts);
  822. glTexCoordPointer(2, GL_FLOAT, 0, texVerts);
  823. glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
  824. #else
  825. glBegin(GL_TRIANGLE_FAN);
  826. glTexCoord2f(0.0, 1.0);
  827. glVertex3fv(points[0]);
  828. glTexCoord2f(0.0, 0.0);
  829. glVertex3fv(points[1]);
  830. glTexCoord2f(1.0, 0.0);
  831. glVertex3fv(points[2]);
  832. glTexCoord2f(1.0, 1.0);
  833. glVertex3fv(points[3]);
  834. glEnd();
  835. #endif
  836. }
  837. void dglWireCube(const Point3F & extent, const Point3F & center)
  838. {
  839. static Point3F cubePoints[8] =
  840. {
  841. Point3F(-1, -1, -1), Point3F(-1, -1, 1), Point3F(-1, 1, -1), Point3F(-1, 1, 1),
  842. Point3F( 1, -1, -1), Point3F( 1, -1, 1), Point3F( 1, 1, -1), Point3F( 1, 1, 1)
  843. };
  844. static U32 cubeFaces[6][4] =
  845. {
  846. { 0, 2, 6, 4 }, { 0, 2, 3, 1 }, { 0, 1, 5, 4 },
  847. { 3, 2, 6, 7 }, { 7, 6, 4, 5 }, { 3, 7, 5, 1 }
  848. };
  849. glDisable(GL_CULL_FACE);
  850. #if defined(TORQUE_OS_IOS) || defined(TORQUE_OS_ANDROID) || defined(TORQUE_OS_EMSCRIPTEN)
  851. //PUAP -Mat untested
  852. for (S32 i = 0; i < 6; i++)
  853. {
  854. GLfloat verts[] = {
  855. 0, 0, 0,
  856. 0, 0, 0,
  857. 0, 0, 0,
  858. 0, 0, 0,
  859. };
  860. int j = 0;
  861. for(int vert = 0; vert < 4; vert++)
  862. {
  863. int idx = cubeFaces[i][vert];
  864. //glVertex3f(cubePoints[idx].x * extent.x + center.x,
  865. //cubePoints[idx].y * extent.y + center.y,
  866. //cubePoints[idx].z * extent.z + center.z);
  867. verts[j] = cubePoints[idx].x * extent.x + center.x;
  868. verts[++j] = cubePoints[idx].y * extent.y + center.y;
  869. verts[++j] = cubePoints[idx].z * extent.z + center.z;
  870. }
  871. glVertexPointer(3, GL_FLOAT, 0, verts);
  872. glDrawArrays(GL_LINE_LOOP, 0, 4);
  873. }
  874. #else
  875. for (S32 i = 0; i < 6; i++)
  876. {
  877. glBegin(GL_LINE_LOOP);
  878. for(int vert = 0; vert < 4; vert++)
  879. {
  880. int idx = cubeFaces[i][vert];
  881. glVertex3f(cubePoints[idx].x * extent.x + center.x,
  882. cubePoints[idx].y * extent.y + center.y,
  883. cubePoints[idx].z * extent.z + center.z);
  884. }
  885. glEnd();
  886. }
  887. #endif
  888. }
  889. void dglSolidCube(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. #if defined(TORQUE_OS_IOS) || defined(TORQUE_OS_ANDROID) || defined(TORQUE_OS_EMSCRIPTEN)
  902. //PUAP -Mat untested
  903. for (S32 i = 0; i < 6; i++)
  904. {
  905. GLfloat verts[] = {
  906. 0, 0, 0,
  907. 0, 0, 0,
  908. 0, 0, 0,
  909. 0, 0, 0,
  910. };
  911. int j = 0;
  912. for(int vert = 0; vert < 4; vert++)
  913. {
  914. int idx = cubeFaces[i][vert];
  915. //glVertex3f(cubePoints[idx].x * extent.x + center.x,
  916. //cubePoints[idx].y * extent.y + center.y,
  917. //cubePoints[idx].z * extent.z + center.z);
  918. verts[j] = cubePoints[idx].x * extent.x + center.x;
  919. verts[++j] = cubePoints[idx].y * extent.y + center.y;
  920. verts[++j] = cubePoints[idx].z * extent.z + center.z;
  921. }
  922. glVertexPointer(3, GL_FLOAT, 0, verts);
  923. glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
  924. }
  925. #else
  926. for (S32 i = 0; i < 6; i++)
  927. {
  928. glBegin(GL_TRIANGLE_FAN);
  929. for(int vert = 0; vert < 4; vert++)
  930. {
  931. int idx = cubeFaces[i][vert];
  932. glVertex3f(cubePoints[idx].x * extent.x + center.x,
  933. cubePoints[idx].y * extent.y + center.y,
  934. cubePoints[idx].z * extent.z + center.z);
  935. }
  936. glEnd();
  937. }
  938. #endif
  939. }
  940. void dglSetClipRect(const RectI &clipRect)
  941. {
  942. glMatrixMode(GL_PROJECTION);
  943. glLoadIdentity();
  944. U32 screenHeight = Platform::getWindowSize().y;
  945. #if defined(TORQUE_OS_IOS) || defined(TORQUE_OS_ANDROID)
  946. glOrthof(clipRect.point.x, clipRect.point.x + clipRect.extent.x,
  947. clipRect.extent.y, 0,
  948. 0, 1);
  949. #else
  950. glOrtho(clipRect.point.x, clipRect.point.x + clipRect.extent.x,
  951. clipRect.extent.y, 0,
  952. 0, 1);
  953. #endif
  954. glTranslatef(0.0f, (F32)-clipRect.point.y, 0.0f);
  955. glMatrixMode(GL_MODELVIEW);
  956. glLoadIdentity();
  957. glViewport(clipRect.point.x, screenHeight - (clipRect.point.y + clipRect.extent.y),
  958. clipRect.extent.x, clipRect.extent.y);
  959. sgCurrentClipRect = clipRect;
  960. }
  961. const RectI& dglGetClipRect()
  962. {
  963. return sgCurrentClipRect;
  964. }
  965. bool dglPointToScreen( Point3F &point3D, Point3F &screenPoint )
  966. {
  967. #if defined(TORQUE_OS_IOS) || defined(TORQUE_OS_ANDROID) || defined(TORQUE_OS_EMSCRIPTEN)
  968. GLfloat glMV[16];
  969. GLfloat glPR[16];
  970. GLint glVP[4];
  971. glGetFloatv(GL_PROJECTION_MATRIX, glPR);
  972. glGetFloatv(GL_MODELVIEW_MATRIX, glMV);
  973. RectI viewport;
  974. dglGetViewport(&viewport);
  975. glVP[0] = viewport.point.x;
  976. glVP[1] = viewport.point.y + viewport.extent.y;
  977. glVP[2] = viewport.extent.x;
  978. glVP[3] = -viewport.extent.y;
  979. MatrixF mv;
  980. dglGetModelview(&mv);
  981. MatrixF pr;
  982. dglGetProjection(&pr);
  983. F64 x, y, z;
  984. // PUAP -Mat untested
  985. int result = gluProject( point3D.x, point3D.y, point3D.z, (const F64 *)&glMV, (const F64 *)&glPR, (const GLint *)&glVP, &x, &y, &z );
  986. screenPoint.x = x;
  987. screenPoint.y = y;
  988. screenPoint.z = z;
  989. return (result == GL_TRUE);
  990. #else
  991. GLdouble glMV[16];
  992. GLdouble glPR[16];
  993. GLint glVP[4];
  994. glGetDoublev(GL_PROJECTION_MATRIX, glPR);
  995. glGetDoublev(GL_MODELVIEW_MATRIX, glMV);
  996. RectI viewport;
  997. dglGetViewport(&viewport);
  998. glVP[0] = viewport.point.x;
  999. glVP[1] = viewport.point.y + viewport.extent.y;
  1000. glVP[2] = viewport.extent.x;
  1001. glVP[3] = -viewport.extent.y;
  1002. MatrixF mv;
  1003. dglGetModelview(&mv);
  1004. MatrixF pr;
  1005. dglGetProjection(&pr);
  1006. F64 x, y, z;
  1007. int result = gluProject( (GLdouble)point3D.x, (GLdouble)point3D.y, (GLdouble)point3D.z, (const F64 *)&glMV, (const F64 *)&glPR, (const GLint *)&glVP, &x, &y, &z );
  1008. screenPoint.x = (F32)x;
  1009. screenPoint.y = (F32)y;
  1010. screenPoint.z = (F32)z;
  1011. return (result == GL_TRUE);
  1012. #endif
  1013. }
  1014. bool dglIsInCanonicalState()
  1015. {
  1016. bool ret = true;
  1017. // Canonical state:
  1018. // BLEND disabled
  1019. // TEXTURE_2D disabled on both texture units.
  1020. // ActiveTexture set to 0
  1021. // LIGHTING off
  1022. // winding : clockwise ?
  1023. // cullface : disabled
  1024. ret &= glIsEnabled(GL_BLEND) == GL_FALSE;
  1025. ret &= glIsEnabled(GL_CULL_FACE) == GL_FALSE;
  1026. GLint temp;
  1027. #if defined(TORQUE_OS_IOS) || defined(TORQUE_OS_ANDROID) || defined(TORQUE_OS_EMSCRIPTEN)
  1028. // PUAP -Mat removed unsupported textureARB and Fog stuff
  1029. if (dglDoesSupportARBMultitexture() == true) {
  1030. //glActiveTextureARB(GL_TEXTURE1_ARB);
  1031. ret &= glIsEnabled(GL_TEXTURE_2D) == GL_FALSE;
  1032. glGetTexEnviv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, &temp);
  1033. ret &= temp == GL_REPLACE;
  1034. //glActiveTextureARB(GL_TEXTURE0_ARB);
  1035. ret &= glIsEnabled(GL_TEXTURE_2D) == GL_FALSE;
  1036. glGetTexEnviv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, &temp);
  1037. ret &= temp == GL_REPLACE;
  1038. //glClientActiveTextureARB(GL_TEXTURE1_ARB);
  1039. ret &= glIsEnabled(GL_TEXTURE_COORD_ARRAY) == GL_FALSE;
  1040. //glClientActiveTextureARB(GL_TEXTURE0_ARB);
  1041. ret &= glIsEnabled(GL_TEXTURE_COORD_ARRAY) == GL_FALSE;
  1042. } else {
  1043. ret &= glIsEnabled(GL_TEXTURE_2D) == GL_FALSE;
  1044. glGetTexEnviv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, &temp);
  1045. ret &= temp == GL_REPLACE;
  1046. ret &= glIsEnabled(GL_TEXTURE_COORD_ARRAY) == GL_FALSE;
  1047. }
  1048. ret &= glIsEnabled(GL_LIGHTING) == GL_FALSE;
  1049. ret &= glIsEnabled(GL_COLOR_ARRAY) == GL_FALSE;
  1050. ret &= glIsEnabled(GL_VERTEX_ARRAY) == GL_FALSE;
  1051. ret &= glIsEnabled(GL_NORMAL_ARRAY) == GL_FALSE;
  1052. //if (dglDoesSupportFogCoord())
  1053. //ret &= glIsEnabled(GL_FOG_COORDINATE_ARRAY_EXT) == GL_FALSE;
  1054. #else
  1055. if (dglDoesSupportARBMultitexture() == true) {
  1056. glActiveTextureARB(GL_TEXTURE1_ARB);
  1057. ret &= glIsEnabled(GL_TEXTURE_2D) == GL_FALSE;
  1058. glGetTexEnviv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, &temp);
  1059. ret &= temp == GL_REPLACE;
  1060. glActiveTextureARB(GL_TEXTURE0_ARB);
  1061. ret &= glIsEnabled(GL_TEXTURE_2D) == GL_FALSE;
  1062. glGetTexEnviv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, &temp);
  1063. ret &= temp == GL_REPLACE;
  1064. glClientActiveTextureARB(GL_TEXTURE1_ARB);
  1065. ret &= glIsEnabled(GL_TEXTURE_COORD_ARRAY) == GL_FALSE;
  1066. glClientActiveTextureARB(GL_TEXTURE0_ARB);
  1067. ret &= glIsEnabled(GL_TEXTURE_COORD_ARRAY) == GL_FALSE;
  1068. } else {
  1069. ret &= glIsEnabled(GL_TEXTURE_2D) == GL_FALSE;
  1070. glGetTexEnviv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, &temp);
  1071. ret &= temp == GL_REPLACE;
  1072. ret &= glIsEnabled(GL_TEXTURE_COORD_ARRAY) == GL_FALSE;
  1073. }
  1074. ret &= glIsEnabled(GL_LIGHTING) == GL_FALSE;
  1075. ret &= glIsEnabled(GL_COLOR_ARRAY) == GL_FALSE;
  1076. ret &= glIsEnabled(GL_VERTEX_ARRAY) == GL_FALSE;
  1077. ret &= glIsEnabled(GL_NORMAL_ARRAY) == GL_FALSE;
  1078. if (dglDoesSupportFogCoord())
  1079. ret &= glIsEnabled(GL_FOG_COORDINATE_ARRAY_EXT) == GL_FALSE;
  1080. #endif
  1081. return ret;
  1082. }
  1083. void dglSetCanonicalState()
  1084. {
  1085. #if defined(TORQUE_OS_IOS) || defined(TORQUE_OS_ANDROID) || defined(TORQUE_OS_EMSCRIPTEN)
  1086. // PUAP -Mat removed unsupported textureARB and Fog stuff
  1087. glDisable(GL_BLEND);
  1088. glDisable(GL_CULL_FACE);
  1089. glBlendFunc(GL_ONE, GL_ZERO);
  1090. glDisable(GL_LIGHTING);
  1091. if (dglDoesSupportARBMultitexture() == true) {
  1092. //glActiveTextureARB(GL_TEXTURE1_ARB);
  1093. glDisable(GL_TEXTURE_2D);
  1094. glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
  1095. //glActiveTextureARB(GL_TEXTURE0_ARB);
  1096. glDisable(GL_TEXTURE_2D);
  1097. glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
  1098. } else {
  1099. glDisable(GL_TEXTURE_2D);
  1100. glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
  1101. }
  1102. glDisableClientState(GL_COLOR_ARRAY);
  1103. glDisableClientState(GL_VERTEX_ARRAY);
  1104. glDisableClientState(GL_NORMAL_ARRAY);
  1105. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  1106. //if (dglDoesSupportFogCoord())
  1107. //glDisableClientState(GL_FOG_COORDINATE_ARRAY_EXT);
  1108. #else
  1109. glDisable(GL_BLEND);
  1110. glDisable(GL_CULL_FACE);
  1111. glBlendFunc(GL_ONE, GL_ZERO);
  1112. glDisable(GL_LIGHTING);
  1113. if (dglDoesSupportARBMultitexture() == true) {
  1114. glActiveTextureARB(GL_TEXTURE1_ARB);
  1115. glDisable(GL_TEXTURE_2D);
  1116. glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
  1117. glActiveTextureARB(GL_TEXTURE0_ARB);
  1118. glDisable(GL_TEXTURE_2D);
  1119. glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
  1120. } else {
  1121. glDisable(GL_TEXTURE_2D);
  1122. glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
  1123. }
  1124. glDisableClientState(GL_COLOR_ARRAY);
  1125. glDisableClientState(GL_VERTEX_ARRAY);
  1126. glDisableClientState(GL_NORMAL_ARRAY);
  1127. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  1128. if (dglDoesSupportFogCoord())
  1129. glDisableClientState(GL_FOG_COORDINATE_ARRAY_EXT);
  1130. #endif
  1131. }
  1132. void dglGetTransformState(S32* mvDepth,
  1133. S32* pDepth,
  1134. S32* t0Depth,
  1135. F32* t0Matrix,
  1136. S32* t1Depth,
  1137. F32* t1Matrix,
  1138. S32* vp)
  1139. {
  1140. glGetIntegerv(GL_MODELVIEW_STACK_DEPTH, (GLint*)mvDepth);
  1141. glGetIntegerv(GL_PROJECTION_STACK_DEPTH, (GLint*)pDepth);
  1142. glGetIntegerv(GL_TEXTURE_STACK_DEPTH, (GLint*)t0Depth);
  1143. glGetFloatv(GL_TEXTURE_MATRIX, t0Matrix);
  1144. if (dglDoesSupportARBMultitexture())
  1145. {
  1146. #if defined(TORQUE_OS_IOS) || defined(TORQUE_OS_ANDROID) || defined(TORQUE_OS_EMSCRIPTEN)
  1147. // PUAP -Mat removed unsupported textureARB stuff
  1148. //glActiveTextureARB(GL_TEXTURE1_ARB);
  1149. glGetIntegerv(GL_TEXTURE_STACK_DEPTH, (GLint*)t1Depth);
  1150. glGetFloatv(GL_TEXTURE_MATRIX, t1Matrix);
  1151. //glActiveTextureARB(GL_TEXTURE0_ARB);
  1152. #else
  1153. glActiveTextureARB(GL_TEXTURE1_ARB);
  1154. glGetIntegerv(GL_TEXTURE_STACK_DEPTH, (GLint*)t1Depth);
  1155. glGetFloatv(GL_TEXTURE_MATRIX, t1Matrix);
  1156. glActiveTextureARB(GL_TEXTURE0_ARB);
  1157. #endif
  1158. }
  1159. else
  1160. {
  1161. *t1Depth = 0;
  1162. for (U32 i = 0; i < 16; i++)
  1163. t1Matrix[i] = 0;
  1164. }
  1165. RectI v;
  1166. dglGetViewport(&v);
  1167. vp[0] = v.point.x;
  1168. vp[1] = v.point.y;
  1169. vp[2] = v.extent.x;
  1170. vp[3] = v.extent.y;
  1171. }
  1172. bool dglCheckState(const S32 mvDepth, const S32 pDepth,
  1173. const S32 t0Depth, const F32* t0Matrix,
  1174. const S32 t1Depth, const F32* t1Matrix,
  1175. const S32* vp)
  1176. {
  1177. GLint md, pd;
  1178. RectI v;
  1179. glGetIntegerv(GL_MODELVIEW_STACK_DEPTH, &md);
  1180. glGetIntegerv(GL_PROJECTION_STACK_DEPTH, &pd);
  1181. GLint t0d, t1d;
  1182. GLfloat t0m[16], t1m[16];
  1183. glGetIntegerv(GL_TEXTURE_STACK_DEPTH, &t0d);
  1184. glGetFloatv(GL_TEXTURE_MATRIX, t0m);
  1185. if (dglDoesSupportARBMultitexture())
  1186. {
  1187. #if defined(TORQUE_OS_IOS) || defined(TORQUE_OS_ANDROID) || defined(TORQUE_OS_EMSCRIPTEN)
  1188. // PUAP -Mat removed unsupported textureARB and Fog stuff
  1189. //glActiveTextureARB(GL_TEXTURE1_ARB);
  1190. glGetIntegerv(GL_TEXTURE_STACK_DEPTH, &t1d);
  1191. glGetFloatv(GL_TEXTURE_MATRIX, t1m);
  1192. //glActiveTextureARB(GL_TEXTURE0_ARB);
  1193. #else
  1194. glActiveTextureARB(GL_TEXTURE1_ARB);
  1195. glGetIntegerv(GL_TEXTURE_STACK_DEPTH, &t1d);
  1196. glGetFloatv(GL_TEXTURE_MATRIX, t1m);
  1197. glActiveTextureARB(GL_TEXTURE0_ARB);
  1198. #endif
  1199. }
  1200. else
  1201. {
  1202. t1d = 0;
  1203. for (U32 i = 0; i < 16; i++)
  1204. t1m[i] = 0;
  1205. }
  1206. dglGetViewport(&v);
  1207. return ((md == mvDepth) &&
  1208. (pd == pDepth) &&
  1209. (t0d == t0Depth) &&
  1210. (dMemcmp(t0m, t0Matrix, sizeof(F32) * 16) == 0) &&
  1211. (t1d == t1Depth) &&
  1212. (dMemcmp(t1m, t1Matrix, sizeof(F32) * 16) == 0) &&
  1213. ((v.point.x == vp[0]) &&
  1214. (v.point.y == vp[1]) &&
  1215. (v.extent.x == vp[2]) &&
  1216. (v.extent.y == vp[3])));
  1217. }
  1218. #if defined(TORQUE_OS_IOS) || defined(TORQUE_OS_ANDROID) || defined(TORQUE_OS_EMSCRIPTEN)
  1219. GLfloat gVertexFloats[8];
  1220. GLfloat gTextureVerts[8];
  1221. #endif