dgl.cc 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596
  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. F32 adjustedRadius = radius - (lineWidth/2);
  998. const S32 num_segments = (const S32)round(10 * sqrtf(adjustedRadius));
  999. F32 theta = 2 * 3.1415926f / F32(num_segments);
  1000. F32 c = cosf(theta);//precalculate the sine and cosine
  1001. F32 s = sinf(theta);
  1002. F32 t;
  1003. F32 x = adjustedRadius;//we start at angle = 0
  1004. F32 y = 0;
  1005. glEnable(GL_BLEND);
  1006. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  1007. glDisable(GL_TEXTURE_2D);
  1008. glLineWidth(lineWidth);
  1009. glColor4ub(color.red, color.green, color.blue, color.alpha);
  1010. vector<GLfloat> verts;
  1011. for (int ii = 0; ii < num_segments; ii++)
  1012. {
  1013. verts.push_back(GLfloat(x + center.x));
  1014. verts.push_back(GLfloat(y + center.y));
  1015. //apply the rotation matrix
  1016. t = x;
  1017. x = c * x - s * y;
  1018. y = s * t + c * y;
  1019. }
  1020. verts.push_back(GLfloat(verts[0]));
  1021. verts.push_back(GLfloat(verts[1]));
  1022. glEnableClientState(GL_VERTEX_ARRAY);
  1023. glVertexPointer(2, GL_FLOAT, 0, verts.data());
  1024. glDrawArrays(GL_LINE_LOOP, 0, num_segments + 1);//draw last two
  1025. glDisableClientState(GL_VERTEX_ARRAY);
  1026. }
  1027. void dglDrawCircleFill(const Point2I &center, const F32 radius, const ColorI &color)
  1028. {
  1029. const S32 num_segments = (const S32)round(10 * sqrtf(radius));
  1030. F32 theta = 2 * 3.1415926f / F32(num_segments);
  1031. F32 c = cosf(theta);//precalculate the sine and cosine
  1032. F32 s = sinf(theta);
  1033. F32 t;
  1034. F32 x = radius;//we start at angle = 0
  1035. F32 y = 0;
  1036. glEnable(GL_BLEND);
  1037. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  1038. glDisable(GL_TEXTURE_2D);
  1039. glColor4ub(color.red, color.green, color.blue, color.alpha);
  1040. vector<GLfloat> verts;
  1041. verts.push_back(GLfloat(center.x));
  1042. verts.push_back(GLfloat(center.y));
  1043. for (int ii = 0; ii < num_segments; ii++)
  1044. {
  1045. verts.push_back(GLfloat(x + center.x));
  1046. verts.push_back(GLfloat(y + center.y));
  1047. //apply the rotation matrix
  1048. t = x;
  1049. x = c * x - s * y;
  1050. y = s * t + c * y;
  1051. }
  1052. verts.push_back(GLfloat(verts[2]));
  1053. verts.push_back(GLfloat(verts[3]));
  1054. glEnableClientState(GL_VERTEX_ARRAY);
  1055. glVertexPointer(2, GL_FLOAT, 0, verts.data());
  1056. glDrawArrays(GL_TRIANGLE_FAN, 0, num_segments+2);
  1057. glDisableClientState(GL_VERTEX_ARRAY);
  1058. }
  1059. void dglSetClipRect(const RectI &clipRect)
  1060. {
  1061. glMatrixMode(GL_PROJECTION);
  1062. glLoadIdentity();
  1063. U32 screenHeight = Platform::getWindowSize().y;
  1064. #if defined(TORQUE_OS_IOS) || defined(TORQUE_OS_ANDROID)
  1065. glOrthof(clipRect.point.x, clipRect.point.x + clipRect.extent.x,
  1066. clipRect.extent.y, 0,
  1067. 0, 1);
  1068. #else
  1069. glOrtho(clipRect.point.x, clipRect.point.x + clipRect.extent.x,
  1070. clipRect.extent.y, 0,
  1071. 0, 1);
  1072. #endif
  1073. glTranslatef(0.0f, (F32)-clipRect.point.y, 0.0f);
  1074. glMatrixMode(GL_MODELVIEW);
  1075. glLoadIdentity();
  1076. glViewport(clipRect.point.x, screenHeight - (clipRect.point.y + clipRect.extent.y),
  1077. clipRect.extent.x, clipRect.extent.y);
  1078. sgCurrentClipRect = clipRect;
  1079. }
  1080. const RectI& dglGetClipRect()
  1081. {
  1082. return sgCurrentClipRect;
  1083. }
  1084. bool dglPointToScreen( Point3F &point3D, Point3F &screenPoint )
  1085. {
  1086. #if defined(TORQUE_OS_IOS) || defined(TORQUE_OS_ANDROID) || defined(TORQUE_OS_EMSCRIPTEN)
  1087. GLfloat glMV[16];
  1088. GLfloat glPR[16];
  1089. GLint glVP[4];
  1090. glGetFloatv(GL_PROJECTION_MATRIX, glPR);
  1091. glGetFloatv(GL_MODELVIEW_MATRIX, glMV);
  1092. RectI viewport;
  1093. dglGetViewport(&viewport);
  1094. glVP[0] = viewport.point.x;
  1095. glVP[1] = viewport.point.y + viewport.extent.y;
  1096. glVP[2] = viewport.extent.x;
  1097. glVP[3] = -viewport.extent.y;
  1098. MatrixF mv;
  1099. dglGetModelview(&mv);
  1100. MatrixF pr;
  1101. dglGetProjection(&pr);
  1102. F64 x, y, z;
  1103. // PUAP -Mat untested
  1104. int result = gluProject( point3D.x, point3D.y, point3D.z, (const F64 *)&glMV, (const F64 *)&glPR, (const GLint *)&glVP, &x, &y, &z );
  1105. screenPoint.x = x;
  1106. screenPoint.y = y;
  1107. screenPoint.z = z;
  1108. return (result == GL_TRUE);
  1109. #else
  1110. GLdouble glMV[16];
  1111. GLdouble glPR[16];
  1112. GLint glVP[4];
  1113. glGetDoublev(GL_PROJECTION_MATRIX, glPR);
  1114. glGetDoublev(GL_MODELVIEW_MATRIX, glMV);
  1115. RectI viewport;
  1116. dglGetViewport(&viewport);
  1117. glVP[0] = viewport.point.x;
  1118. glVP[1] = viewport.point.y + viewport.extent.y;
  1119. glVP[2] = viewport.extent.x;
  1120. glVP[3] = -viewport.extent.y;
  1121. MatrixF mv;
  1122. dglGetModelview(&mv);
  1123. MatrixF pr;
  1124. dglGetProjection(&pr);
  1125. F64 x, y, z;
  1126. int result = gluProject( (GLdouble)point3D.x, (GLdouble)point3D.y, (GLdouble)point3D.z, (const F64 *)&glMV, (const F64 *)&glPR, (const GLint *)&glVP, &x, &y, &z );
  1127. screenPoint.x = (F32)x;
  1128. screenPoint.y = (F32)y;
  1129. screenPoint.z = (F32)z;
  1130. return (result == GL_TRUE);
  1131. #endif
  1132. }
  1133. bool dglIsInCanonicalState()
  1134. {
  1135. bool ret = true;
  1136. // Canonical state:
  1137. // BLEND disabled
  1138. // TEXTURE_2D disabled on both texture units.
  1139. // ActiveTexture set to 0
  1140. // LIGHTING off
  1141. // winding : clockwise ?
  1142. // cullface : disabled
  1143. ret &= glIsEnabled(GL_BLEND) == GL_FALSE;
  1144. ret &= glIsEnabled(GL_CULL_FACE) == GL_FALSE;
  1145. GLint temp;
  1146. #if defined(TORQUE_OS_IOS) || defined(TORQUE_OS_ANDROID) || defined(TORQUE_OS_EMSCRIPTEN)
  1147. // PUAP -Mat removed unsupported textureARB and Fog stuff
  1148. if (dglDoesSupportARBMultitexture() == true) {
  1149. //glActiveTextureARB(GL_TEXTURE1_ARB);
  1150. ret &= glIsEnabled(GL_TEXTURE_2D) == GL_FALSE;
  1151. glGetTexEnviv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, &temp);
  1152. ret &= temp == GL_REPLACE;
  1153. //glActiveTextureARB(GL_TEXTURE0_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. //glClientActiveTextureARB(GL_TEXTURE1_ARB);
  1158. ret &= glIsEnabled(GL_TEXTURE_COORD_ARRAY) == GL_FALSE;
  1159. //glClientActiveTextureARB(GL_TEXTURE0_ARB);
  1160. ret &= glIsEnabled(GL_TEXTURE_COORD_ARRAY) == GL_FALSE;
  1161. } else {
  1162. ret &= glIsEnabled(GL_TEXTURE_2D) == GL_FALSE;
  1163. glGetTexEnviv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, &temp);
  1164. ret &= temp == GL_REPLACE;
  1165. ret &= glIsEnabled(GL_TEXTURE_COORD_ARRAY) == GL_FALSE;
  1166. }
  1167. ret &= glIsEnabled(GL_LIGHTING) == GL_FALSE;
  1168. ret &= glIsEnabled(GL_COLOR_ARRAY) == GL_FALSE;
  1169. ret &= glIsEnabled(GL_VERTEX_ARRAY) == GL_FALSE;
  1170. ret &= glIsEnabled(GL_NORMAL_ARRAY) == GL_FALSE;
  1171. //if (dglDoesSupportFogCoord())
  1172. //ret &= glIsEnabled(GL_FOG_COORDINATE_ARRAY_EXT) == GL_FALSE;
  1173. #else
  1174. if (dglDoesSupportARBMultitexture() == true) {
  1175. glActiveTextureARB(GL_TEXTURE1_ARB);
  1176. ret &= glIsEnabled(GL_TEXTURE_2D) == GL_FALSE;
  1177. glGetTexEnviv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, &temp);
  1178. ret &= temp == GL_REPLACE;
  1179. glActiveTextureARB(GL_TEXTURE0_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. glClientActiveTextureARB(GL_TEXTURE1_ARB);
  1184. ret &= glIsEnabled(GL_TEXTURE_COORD_ARRAY) == GL_FALSE;
  1185. glClientActiveTextureARB(GL_TEXTURE0_ARB);
  1186. ret &= glIsEnabled(GL_TEXTURE_COORD_ARRAY) == GL_FALSE;
  1187. } else {
  1188. ret &= glIsEnabled(GL_TEXTURE_2D) == GL_FALSE;
  1189. glGetTexEnviv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, &temp);
  1190. ret &= temp == GL_REPLACE;
  1191. ret &= glIsEnabled(GL_TEXTURE_COORD_ARRAY) == GL_FALSE;
  1192. }
  1193. ret &= glIsEnabled(GL_LIGHTING) == GL_FALSE;
  1194. ret &= glIsEnabled(GL_COLOR_ARRAY) == GL_FALSE;
  1195. ret &= glIsEnabled(GL_VERTEX_ARRAY) == GL_FALSE;
  1196. ret &= glIsEnabled(GL_NORMAL_ARRAY) == GL_FALSE;
  1197. if (dglDoesSupportFogCoord())
  1198. ret &= glIsEnabled(GL_FOG_COORDINATE_ARRAY_EXT) == GL_FALSE;
  1199. #endif
  1200. return ret;
  1201. }
  1202. void dglSetCanonicalState()
  1203. {
  1204. #if defined(TORQUE_OS_IOS) || defined(TORQUE_OS_ANDROID) || defined(TORQUE_OS_EMSCRIPTEN)
  1205. // PUAP -Mat removed unsupported textureARB and Fog stuff
  1206. glDisable(GL_BLEND);
  1207. glDisable(GL_CULL_FACE);
  1208. glBlendFunc(GL_ONE, GL_ZERO);
  1209. glDisable(GL_LIGHTING);
  1210. if (dglDoesSupportARBMultitexture() == true) {
  1211. //glActiveTextureARB(GL_TEXTURE1_ARB);
  1212. glDisable(GL_TEXTURE_2D);
  1213. glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
  1214. //glActiveTextureARB(GL_TEXTURE0_ARB);
  1215. glDisable(GL_TEXTURE_2D);
  1216. glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
  1217. } else {
  1218. glDisable(GL_TEXTURE_2D);
  1219. glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
  1220. }
  1221. glDisableClientState(GL_COLOR_ARRAY);
  1222. glDisableClientState(GL_VERTEX_ARRAY);
  1223. glDisableClientState(GL_NORMAL_ARRAY);
  1224. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  1225. //if (dglDoesSupportFogCoord())
  1226. //glDisableClientState(GL_FOG_COORDINATE_ARRAY_EXT);
  1227. #else
  1228. glDisable(GL_BLEND);
  1229. glDisable(GL_CULL_FACE);
  1230. glBlendFunc(GL_ONE, GL_ZERO);
  1231. glDisable(GL_LIGHTING);
  1232. if (dglDoesSupportARBMultitexture() == true) {
  1233. glActiveTextureARB(GL_TEXTURE1_ARB);
  1234. glDisable(GL_TEXTURE_2D);
  1235. glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
  1236. glActiveTextureARB(GL_TEXTURE0_ARB);
  1237. glDisable(GL_TEXTURE_2D);
  1238. glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
  1239. } else {
  1240. glDisable(GL_TEXTURE_2D);
  1241. glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
  1242. }
  1243. glDisableClientState(GL_COLOR_ARRAY);
  1244. glDisableClientState(GL_VERTEX_ARRAY);
  1245. glDisableClientState(GL_NORMAL_ARRAY);
  1246. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  1247. if (dglDoesSupportFogCoord())
  1248. glDisableClientState(GL_FOG_COORDINATE_ARRAY_EXT);
  1249. #endif
  1250. }
  1251. void dglGetTransformState(S32* mvDepth,
  1252. S32* pDepth,
  1253. S32* t0Depth,
  1254. F32* t0Matrix,
  1255. S32* t1Depth,
  1256. F32* t1Matrix,
  1257. S32* vp)
  1258. {
  1259. glGetIntegerv(GL_MODELVIEW_STACK_DEPTH, (GLint*)mvDepth);
  1260. glGetIntegerv(GL_PROJECTION_STACK_DEPTH, (GLint*)pDepth);
  1261. glGetIntegerv(GL_TEXTURE_STACK_DEPTH, (GLint*)t0Depth);
  1262. glGetFloatv(GL_TEXTURE_MATRIX, t0Matrix);
  1263. if (dglDoesSupportARBMultitexture())
  1264. {
  1265. #if defined(TORQUE_OS_IOS) || defined(TORQUE_OS_ANDROID) || defined(TORQUE_OS_EMSCRIPTEN)
  1266. // PUAP -Mat removed unsupported textureARB stuff
  1267. //glActiveTextureARB(GL_TEXTURE1_ARB);
  1268. glGetIntegerv(GL_TEXTURE_STACK_DEPTH, (GLint*)t1Depth);
  1269. glGetFloatv(GL_TEXTURE_MATRIX, t1Matrix);
  1270. //glActiveTextureARB(GL_TEXTURE0_ARB);
  1271. #else
  1272. glActiveTextureARB(GL_TEXTURE1_ARB);
  1273. glGetIntegerv(GL_TEXTURE_STACK_DEPTH, (GLint*)t1Depth);
  1274. glGetFloatv(GL_TEXTURE_MATRIX, t1Matrix);
  1275. glActiveTextureARB(GL_TEXTURE0_ARB);
  1276. #endif
  1277. }
  1278. else
  1279. {
  1280. *t1Depth = 0;
  1281. for (U32 i = 0; i < 16; i++)
  1282. t1Matrix[i] = 0;
  1283. }
  1284. RectI v;
  1285. dglGetViewport(&v);
  1286. vp[0] = v.point.x;
  1287. vp[1] = v.point.y;
  1288. vp[2] = v.extent.x;
  1289. vp[3] = v.extent.y;
  1290. }
  1291. bool dglCheckState(const S32 mvDepth, const S32 pDepth,
  1292. const S32 t0Depth, const F32* t0Matrix,
  1293. const S32 t1Depth, const F32* t1Matrix,
  1294. const S32* vp)
  1295. {
  1296. GLint md, pd;
  1297. RectI v;
  1298. glGetIntegerv(GL_MODELVIEW_STACK_DEPTH, &md);
  1299. glGetIntegerv(GL_PROJECTION_STACK_DEPTH, &pd);
  1300. GLint t0d, t1d;
  1301. GLfloat t0m[16], t1m[16];
  1302. glGetIntegerv(GL_TEXTURE_STACK_DEPTH, &t0d);
  1303. glGetFloatv(GL_TEXTURE_MATRIX, t0m);
  1304. if (dglDoesSupportARBMultitexture())
  1305. {
  1306. #if defined(TORQUE_OS_IOS) || defined(TORQUE_OS_ANDROID) || defined(TORQUE_OS_EMSCRIPTEN)
  1307. // PUAP -Mat removed unsupported textureARB and Fog stuff
  1308. //glActiveTextureARB(GL_TEXTURE1_ARB);
  1309. glGetIntegerv(GL_TEXTURE_STACK_DEPTH, &t1d);
  1310. glGetFloatv(GL_TEXTURE_MATRIX, t1m);
  1311. //glActiveTextureARB(GL_TEXTURE0_ARB);
  1312. #else
  1313. glActiveTextureARB(GL_TEXTURE1_ARB);
  1314. glGetIntegerv(GL_TEXTURE_STACK_DEPTH, &t1d);
  1315. glGetFloatv(GL_TEXTURE_MATRIX, t1m);
  1316. glActiveTextureARB(GL_TEXTURE0_ARB);
  1317. #endif
  1318. }
  1319. else
  1320. {
  1321. t1d = 0;
  1322. for (U32 i = 0; i < 16; i++)
  1323. t1m[i] = 0;
  1324. }
  1325. dglGetViewport(&v);
  1326. return ((md == mvDepth) &&
  1327. (pd == pDepth) &&
  1328. (t0d == t0Depth) &&
  1329. (dMemcmp(t0m, t0Matrix, sizeof(F32) * 16) == 0) &&
  1330. (t1d == t1Depth) &&
  1331. (dMemcmp(t1m, t1Matrix, sizeof(F32) * 16) == 0) &&
  1332. ((v.point.x == vp[0]) &&
  1333. (v.point.y == vp[1]) &&
  1334. (v.extent.x == vp[2]) &&
  1335. (v.extent.y == vp[3])));
  1336. }
  1337. #if defined(TORQUE_OS_IOS) || defined(TORQUE_OS_ANDROID) || defined(TORQUE_OS_EMSCRIPTEN)
  1338. GLfloat gVertexFloats[8];
  1339. GLfloat gTextureVerts[8];
  1340. #endif