gfxDrawUtil.cpp 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 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 "platform/platform.h"
  23. #include "gfx/gfxDrawUtil.h"
  24. #include "core/frameAllocator.h"
  25. #include "core/strings/stringFunctions.h"
  26. #include "core/strings/unicode.h"
  27. #include "math/util/frustum.h"
  28. #include "math/util/sphereMesh.h"
  29. #include "math/mathUtils.h"
  30. #include "gfx/gfxFontRenderBatcher.h"
  31. #include "gfx/gfxTransformSaver.h"
  32. #include "gfx/gfxPrimitiveBuffer.h"
  33. #include "gfx/primBuilder.h"
  34. #include "gfx/gfxDebugEvent.h"
  35. #include "math/mPolyhedron.impl.h"
  36. GFXDrawUtil::GFXDrawUtil( GFXDevice * d)
  37. {
  38. mDevice = d;
  39. mBitmapModulation.set(0xFF, 0xFF, 0xFF, 0xFF);
  40. mTextAnchorColor.set(0xFF, 0xFF, 0xFF, 0xFF);
  41. mFontRenderBatcher = new FontRenderBatcher();
  42. _setupStateBlocks();
  43. }
  44. GFXDrawUtil::~GFXDrawUtil()
  45. {
  46. delete mFontRenderBatcher;
  47. }
  48. void GFXDrawUtil::_setupStateBlocks()
  49. {
  50. // DrawBitmapStretchSR
  51. GFXStateBlockDesc bitmapStretchSR;
  52. bitmapStretchSR.setCullMode(GFXCullNone);
  53. bitmapStretchSR.setZReadWrite(false);
  54. bitmapStretchSR.setBlend(true, GFXBlendSrcAlpha, GFXBlendInvSrcAlpha);
  55. bitmapStretchSR.samplersDefined = true;
  56. // Linear: Create wrap SB
  57. bitmapStretchSR.samplers[0] = GFXSamplerStateDesc::getWrapLinear();
  58. mBitmapStretchWrapLinearSB = mDevice->createStateBlock(bitmapStretchSR);
  59. // Linear: Create clamp SB
  60. bitmapStretchSR.samplers[0] = GFXSamplerStateDesc::getClampLinear();
  61. mBitmapStretchLinearSB = mDevice->createStateBlock(bitmapStretchSR);
  62. // Point:
  63. bitmapStretchSR.samplers[0].minFilter = GFXTextureFilterPoint;
  64. bitmapStretchSR.samplers[0].mipFilter = GFXTextureFilterPoint;
  65. bitmapStretchSR.samplers[0].magFilter = GFXTextureFilterPoint;
  66. // Point: Create clamp SB, last created clamped so no work required here
  67. mBitmapStretchSB = mDevice->createStateBlock(bitmapStretchSR);
  68. // Point: Create wrap SB, have to do this manually because getWrapLinear doesn't
  69. bitmapStretchSR.samplers[0].addressModeU = GFXAddressWrap;
  70. bitmapStretchSR.samplers[0].addressModeV = GFXAddressWrap;
  71. bitmapStretchSR.samplers[0].addressModeW = GFXAddressWrap;
  72. mBitmapStretchWrapSB = mDevice->createStateBlock(bitmapStretchSR);
  73. GFXStateBlockDesc rectFill;
  74. rectFill.setCullMode(GFXCullNone);
  75. rectFill.setZReadWrite(false);
  76. rectFill.setBlend(true, GFXBlendSrcAlpha, GFXBlendInvSrcAlpha);
  77. mRectFillSB = mDevice->createStateBlock(rectFill);
  78. }
  79. //-----------------------------------------------------------------------------
  80. // Color Modulation
  81. //-----------------------------------------------------------------------------
  82. void GFXDrawUtil::setBitmapModulation( const ColorI &modColor )
  83. {
  84. mBitmapModulation = modColor;
  85. }
  86. void GFXDrawUtil::clearBitmapModulation()
  87. {
  88. mBitmapModulation.set( 255, 255, 255, 255 );
  89. }
  90. void GFXDrawUtil::getBitmapModulation( ColorI *color )
  91. {
  92. mBitmapModulation.getColor( color );
  93. }
  94. void GFXDrawUtil::setTextAnchorColor( const ColorI &ancColor )
  95. {
  96. mTextAnchorColor = ancColor;
  97. }
  98. //-----------------------------------------------------------------------------
  99. // Draw Text
  100. //-----------------------------------------------------------------------------
  101. U32 GFXDrawUtil::drawText( GFont *font, const Point2I &ptDraw, const UTF16 *in_string,
  102. const ColorI *colorTable, const U32 maxColorIndex, F32 rot )
  103. {
  104. return drawTextN( font, ptDraw, in_string, dStrlen(in_string), colorTable, maxColorIndex, rot );
  105. }
  106. U32 GFXDrawUtil::drawText( GFont *font, const Point2I &ptDraw, const UTF8 *in_string,
  107. const ColorI *colorTable, const U32 maxColorIndex, F32 rot )
  108. {
  109. return drawTextN( font, ptDraw, in_string, dStrlen(in_string), colorTable, maxColorIndex, rot );
  110. }
  111. U32 GFXDrawUtil::drawText( GFont *font, const Point2F &ptDraw, const UTF8 *in_string, const ColorI *colorTable /*= NULL*/, const U32 maxColorIndex /*= 9*/, F32 rot /*= 0.f */ )
  112. {
  113. return drawText(font,Point2I((S32)ptDraw.x,(S32)ptDraw.y),in_string,colorTable,maxColorIndex,rot);
  114. }
  115. U32 GFXDrawUtil::drawText( GFont *font, const Point2F &ptDraw, const UTF16 *in_string, const ColorI *colorTable /*= NULL*/, const U32 maxColorIndex /*= 9*/, F32 rot /*= 0.f */ )
  116. {
  117. return drawText(font,Point2I((S32)ptDraw.x,(S32)ptDraw.y),in_string,colorTable,maxColorIndex,rot);
  118. }
  119. U32 GFXDrawUtil::drawTextN( GFont *font, const Point2I &ptDraw, const UTF8 *in_string, U32 n,
  120. const ColorI *colorTable, const U32 maxColorIndex, F32 rot )
  121. {
  122. // return on zero length strings
  123. if( n == 0 )
  124. return ptDraw.x;
  125. // Convert to UTF16 temporarily.
  126. n++; // space for null terminator
  127. FrameTemp<UTF16> ubuf( n * sizeof(UTF16) );
  128. convertUTF8toUTF16(in_string, ubuf, n);
  129. return drawTextN( font, ptDraw, ubuf, n, colorTable, maxColorIndex, rot );
  130. }
  131. U32 GFXDrawUtil::drawTextN( GFont *font, const Point2I &ptDraw, const UTF16 *in_string,
  132. U32 n, const ColorI *colorTable, const U32 maxColorIndex, F32 rot )
  133. {
  134. // return on zero length strings
  135. if( n == 0 )
  136. return ptDraw.x;
  137. // If it's over about 4000 verts we want to break it up
  138. if( n > 666 )
  139. {
  140. U32 left = drawTextN(font, ptDraw, in_string, 666, colorTable, maxColorIndex, rot);
  141. Point2I newDrawPt(left, ptDraw.y);
  142. const UTF16* str = (const UTF16*)in_string;
  143. return drawTextN(font, newDrawPt, &(str[666]), n - 666, colorTable, maxColorIndex, rot);
  144. }
  145. PROFILE_START(GFXDevice_drawTextN);
  146. const PlatformFont::CharInfo *tabci = NULL;
  147. S32 ptX = 0;
  148. // Queue everything for render.
  149. mFontRenderBatcher->init(font, n);
  150. U32 i;
  151. UTF16 c;
  152. for (i = 0, c = in_string[i]; i < n && in_string[i]; i++, c = in_string[i])
  153. {
  154. switch(c)
  155. {
  156. // We have to do a little dance here since \t = 0x9, \n = 0xa, and \r = 0xd
  157. case 1: case 2: case 3: case 4: case 5: case 6: case 7:
  158. case 11: case 12:
  159. case 14:
  160. {
  161. // Color code
  162. if (colorTable)
  163. {
  164. static U8 remap[15] =
  165. {
  166. 0x0, // 0 special null terminator
  167. 0x0, // 1 ascii start-of-heading??
  168. 0x1,
  169. 0x2,
  170. 0x3,
  171. 0x4,
  172. 0x5,
  173. 0x6,
  174. 0x0, // 8 special backspace
  175. 0x0, // 9 special tab
  176. 0x0, // a special \n
  177. 0x7,
  178. 0x8,
  179. 0x0, // a special \r
  180. 0x9
  181. };
  182. U8 remapped = remap[c];
  183. // Ignore if the color is greater than the specified max index:
  184. if ( remapped <= maxColorIndex )
  185. {
  186. const ColorI &clr = colorTable[remapped];
  187. mBitmapModulation = clr;
  188. }
  189. }
  190. // And skip rendering this character.
  191. continue;
  192. }
  193. // reset color?
  194. case 15:
  195. {
  196. mBitmapModulation = mTextAnchorColor;
  197. // And skip rendering this character.
  198. continue;
  199. }
  200. // push color:
  201. case 16:
  202. {
  203. mTextAnchorColor = mBitmapModulation;
  204. // And skip rendering this character.
  205. continue;
  206. }
  207. // pop color:
  208. case 17:
  209. {
  210. mBitmapModulation = mTextAnchorColor;
  211. // And skip rendering this character.
  212. continue;
  213. }
  214. // Tab character
  215. case dT('\t'):
  216. {
  217. if ( tabci == NULL )
  218. tabci = &(font->getCharInfo( dT(' ') ));
  219. const U32 fontTabIncrement = tabci->xIncrement * GFont::TabWidthInSpaces;
  220. ptX += fontTabIncrement;
  221. // And skip rendering this character.
  222. continue;
  223. }
  224. // Don't draw invalid characters.
  225. default:
  226. {
  227. if( !font->isValidChar( c ) )
  228. continue;
  229. }
  230. }
  231. // Queue char for rendering..
  232. mFontRenderBatcher->queueChar(c, ptX, mBitmapModulation);
  233. }
  234. mFontRenderBatcher->render(rot, Point2F((F32)ptDraw.x, (F32)ptDraw.y));
  235. PROFILE_END();
  236. return ptX + ptDraw.x;
  237. }
  238. U32 GFXDrawUtil::drawTextN( GFont *font, const Point2F &ptDraw, const UTF8 *in_string, U32 n, const ColorI *colorTable /*= NULL*/, const U32 maxColorIndex /*= 9*/, F32 rot /*= 0.f */ )
  239. {
  240. return drawTextN(font,Point2I((S32)ptDraw.x,(S32)ptDraw.y),in_string,n,colorTable,maxColorIndex,rot);
  241. }
  242. U32 GFXDrawUtil::drawTextN( GFont *font, const Point2F &ptDraw, const UTF16 *in_string, U32 n, const ColorI *colorTable /*= NULL*/, const U32 maxColorIndex /*= 9*/, F32 rot /*= 0.f */ )
  243. {
  244. return drawTextN(font,Point2I((S32)ptDraw.x,(S32)ptDraw.y),in_string,n,colorTable,maxColorIndex,rot);
  245. }
  246. //-----------------------------------------------------------------------------
  247. // Draw Bitmaps
  248. //-----------------------------------------------------------------------------
  249. void GFXDrawUtil::drawBitmap( GFXTextureObject* texture, const Point2I &in_rAt, const GFXBitmapFlip in_flip, const GFXTextureFilterType filter , bool in_wrap /*= true*/ )
  250. {
  251. drawBitmap(texture,Point2F((F32)in_rAt.x,(F32)in_rAt.y),in_flip,filter,in_wrap);
  252. }
  253. void GFXDrawUtil::drawBitmapStretch( GFXTextureObject* texture, const RectI &dstRect, const GFXBitmapFlip in_flip, const GFXTextureFilterType filter , bool in_wrap /*= true*/ )
  254. {
  255. drawBitmapStretch(texture,RectF((F32)dstRect.point.x,(F32)dstRect.point.y,(F32)dstRect.extent.x,(F32)dstRect.extent.y),in_flip,filter,in_wrap);
  256. }
  257. void GFXDrawUtil::drawBitmapSR( GFXTextureObject* texture, const Point2I &in_rAt, const RectI &srcRect, const GFXBitmapFlip in_flip, const GFXTextureFilterType filter , bool in_wrap /*= true*/ )
  258. {
  259. drawBitmapSR(texture,Point2F((F32)in_rAt.x,(F32)in_rAt.y),RectF((F32)srcRect.point.x,(F32)srcRect.point.y,(F32)srcRect.extent.x,(F32)srcRect.extent.y),in_flip,filter,in_wrap);
  260. }
  261. void GFXDrawUtil::drawBitmapStretchSR( GFXTextureObject *texture, const RectI &dstRect, const RectI &srcRect, const GFXBitmapFlip in_flip, const GFXTextureFilterType filter , bool in_wrap /*= true*/ )
  262. {
  263. RectF dstRectF = RectF((F32)dstRect.point.x,(F32)dstRect.point.y,(F32)dstRect.extent.x,(F32)dstRect.extent.y);
  264. RectF srcRectF = RectF((F32)srcRect.point.x,(F32)srcRect.point.y,(F32)srcRect.extent.x,(F32)srcRect.extent.y);
  265. drawBitmapStretchSR(texture,dstRectF,srcRectF,in_flip,filter,in_wrap);
  266. }
  267. void GFXDrawUtil::drawBitmap( GFXTextureObject*texture, const Point2F &in_rAt, const GFXBitmapFlip in_flip /*= GFXBitmapFlip_None*/, const GFXTextureFilterType filter /*= GFXTextureFilterPoint */ , bool in_wrap /*= true*/ )
  268. {
  269. AssertFatal( texture != 0, "No texture specified for drawBitmap()" );
  270. RectI subRegion( 0, 0, texture->mBitmapSize.x, texture->mBitmapSize.y );
  271. RectI stretch( in_rAt.x, in_rAt.y, texture->mBitmapSize.x, texture->mBitmapSize.y );
  272. drawBitmapStretchSR( texture, stretch, subRegion, in_flip, filter, in_wrap );
  273. }
  274. void GFXDrawUtil::drawBitmapStretch( GFXTextureObject*texture, const RectF &dstRect, const GFXBitmapFlip in_flip /*= GFXBitmapFlip_None*/, const GFXTextureFilterType filter /*= GFXTextureFilterPoint */ , bool in_wrap /*= true*/ )
  275. {
  276. AssertFatal( texture != 0, "No texture specified for drawBitmapStretch()" );
  277. RectF subRegion( 0.f, 0.f, (F32)texture->mBitmapSize.x, (F32)texture->mBitmapSize.y );
  278. drawBitmapStretchSR( texture, dstRect, subRegion, in_flip, filter, in_wrap );
  279. }
  280. void GFXDrawUtil::drawBitmapSR( GFXTextureObject*texture, const Point2F &in_rAt, const RectF &srcRect, const GFXBitmapFlip in_flip /*= GFXBitmapFlip_None*/, const GFXTextureFilterType filter /*= GFXTextureFilterPoint */ , bool in_wrap /*= true*/ )
  281. {
  282. AssertFatal( texture != 0, "No texture specified for drawBitmapSR()" );
  283. RectF stretch( in_rAt.x, in_rAt.y, srcRect.len_x(), srcRect.len_y() );
  284. drawBitmapStretchSR( texture, stretch, srcRect, in_flip, filter, in_wrap );
  285. }
  286. void GFXDrawUtil::drawBitmapStretchSR( GFXTextureObject* texture, const RectF &dstRect, const RectF &srcRect, const GFXBitmapFlip in_flip /*= GFXBitmapFlip_None*/, const GFXTextureFilterType filter /*= GFXTextureFilterPoint */ , bool in_wrap /*= true*/ )
  287. {
  288. // Sanity if no texture is specified.
  289. if(!texture)
  290. return;
  291. GFXVertexBufferHandle<GFXVertexPCT> verts(mDevice, 4, GFXBufferTypeVolatile );
  292. verts.lock();
  293. F32 texLeft = (srcRect.point.x) / (texture->mTextureSize.x);
  294. F32 texRight = (srcRect.point.x + srcRect.extent.x) / (texture->mTextureSize.x);
  295. F32 texTop = (srcRect.point.y) / (texture->mTextureSize.y);
  296. F32 texBottom = (srcRect.point.y + srcRect.extent.y) / (texture->mTextureSize.y);
  297. F32 screenLeft = dstRect.point.x;
  298. F32 screenRight = (dstRect.point.x + dstRect.extent.x);
  299. F32 screenTop = dstRect.point.y;
  300. F32 screenBottom = (dstRect.point.y + dstRect.extent.y);
  301. if( in_flip & GFXBitmapFlip_X )
  302. {
  303. F32 temp = texLeft;
  304. texLeft = texRight;
  305. texRight = temp;
  306. }
  307. if( in_flip & GFXBitmapFlip_Y )
  308. {
  309. F32 temp = texTop;
  310. texTop = texBottom;
  311. texBottom = temp;
  312. }
  313. const F32 fillConv = mDevice->getFillConventionOffset();
  314. verts[0].point.set( screenLeft - fillConv, screenTop - fillConv, 0.f );
  315. verts[1].point.set( screenRight - fillConv, screenTop - fillConv, 0.f );
  316. verts[2].point.set( screenLeft - fillConv, screenBottom - fillConv, 0.f );
  317. verts[3].point.set( screenRight - fillConv, screenBottom - fillConv, 0.f );
  318. verts[0].color = verts[1].color = verts[2].color = verts[3].color = mBitmapModulation;
  319. verts[0].texCoord.set( texLeft, texTop );
  320. verts[1].texCoord.set( texRight, texTop );
  321. verts[2].texCoord.set( texLeft, texBottom );
  322. verts[3].texCoord.set( texRight, texBottom );
  323. verts.unlock();
  324. mDevice->setVertexBuffer( verts );
  325. switch (filter)
  326. {
  327. case GFXTextureFilterPoint :
  328. mDevice->setStateBlock(in_wrap ? mBitmapStretchWrapSB : mBitmapStretchSB);
  329. break;
  330. case GFXTextureFilterLinear :
  331. mDevice->setStateBlock(in_wrap ? mBitmapStretchWrapLinearSB : mBitmapStretchLinearSB);
  332. break;
  333. default:
  334. AssertFatal(false, "No GFXDrawUtil state block defined for this filter type!");
  335. mDevice->setStateBlock(mBitmapStretchSB);
  336. break;
  337. }
  338. mDevice->setTexture( 0, texture );
  339. mDevice->setupGenericShaders( GFXDevice::GSModColorTexture );
  340. mDevice->drawPrimitive( GFXTriangleStrip, 0, 2 );
  341. }
  342. //-----------------------------------------------------------------------------
  343. // Draw Rectangle
  344. //-----------------------------------------------------------------------------
  345. void GFXDrawUtil::drawRect( const Point2I &upperLeft, const Point2I &lowerRight, const ColorI &color )
  346. {
  347. drawRect( Point2F((F32)upperLeft.x,(F32)upperLeft.y),Point2F((F32)lowerRight.x,(F32)lowerRight.y),color);
  348. }
  349. void GFXDrawUtil::drawRect( const RectI &rect, const ColorI &color )
  350. {
  351. drawRect( rect.point, Point2I(rect.point.x + rect.extent.x - 1, rect.point.y + rect.extent.y - 1), color );
  352. }
  353. void GFXDrawUtil::drawRect( const RectF &rect, const ColorI &color )
  354. {
  355. drawRect( rect.point, Point2F(rect.point.x + rect.extent.x - 1, rect.point.y + rect.extent.y - 1), color );
  356. }
  357. void GFXDrawUtil::drawRect( const Point2F &upperLeft, const Point2F &lowerRight, const ColorI &color )
  358. {
  359. //
  360. // Convert Box a----------x
  361. // | |
  362. // x----------b
  363. //
  364. // Into Triangle-Strip Outline
  365. // v0-----------v2
  366. // | a x |
  367. // | v1-----v3 |
  368. // | | | |
  369. // | v7-----v5 |
  370. // | x b |
  371. // v6-----------v4
  372. //
  373. // NorthWest and NorthEast facing offset vectors
  374. // These adjust the thickness of the line, it'd be neat if one day
  375. // they were passed in as arguments.
  376. Point2F nw(-0.5f,-0.5f); /* \ */
  377. Point2F ne(0.5f,-0.5f); /* / */
  378. GFXVertexBufferHandle<GFXVertexPC> verts (mDevice, 10, GFXBufferTypeVolatile );
  379. verts.lock();
  380. F32 ulOffset = 0.5f - mDevice->getFillConventionOffset();
  381. verts[0].point.set( upperLeft.x + ulOffset + nw.x, upperLeft.y + ulOffset + nw.y, 0.0f );
  382. verts[1].point.set( upperLeft.x + ulOffset - nw.x, upperLeft.y + ulOffset - nw.y, 0.0f );
  383. verts[2].point.set( lowerRight.x + ne.x, upperLeft.y + ulOffset + ne.y, 0.0f );
  384. verts[3].point.set( lowerRight.x - ne.x, upperLeft.y + ulOffset - ne.y, 0.0f );
  385. verts[4].point.set( lowerRight.x - nw.x, lowerRight.y - nw.y, 0.0f );
  386. verts[5].point.set( lowerRight.x + nw.x, lowerRight.y + nw.y, 0.0f );
  387. verts[6].point.set( upperLeft.x + ulOffset - ne.x, lowerRight.y - ne.y, 0.0f );
  388. verts[7].point.set( upperLeft.x + ulOffset + ne.x, lowerRight.y + ne.y, 0.0f );
  389. verts[8].point.set( upperLeft.x + ulOffset + nw.x, upperLeft.y + ulOffset + nw.y, 0.0f ); // same as 0
  390. verts[9].point.set( upperLeft.x + ulOffset - nw.x, upperLeft.y + ulOffset - nw.y, 0.0f ); // same as 1
  391. for (S32 i=0; i<10; i++)
  392. verts[i].color = color;
  393. verts.unlock();
  394. mDevice->setVertexBuffer( verts );
  395. mDevice->setStateBlock(mRectFillSB);
  396. mDevice->setupGenericShaders();
  397. mDevice->drawPrimitive( GFXTriangleStrip, 0, 8 );
  398. }
  399. //-----------------------------------------------------------------------------
  400. // Draw Rectangle Fill
  401. //-----------------------------------------------------------------------------
  402. void GFXDrawUtil::drawRectFill( const RectF &rect, const ColorI &color )
  403. {
  404. drawRectFill(rect.point, Point2F(rect.extent.x + rect.point.x - 1, rect.extent.y + rect.point.y - 1), color );
  405. }
  406. void GFXDrawUtil::drawRectFill( const Point2I &upperLeft, const Point2I &lowerRight, const ColorI &color )
  407. {
  408. drawRectFill(Point2F((F32)upperLeft.x, (F32)upperLeft.y), Point2F((F32)lowerRight.x, (F32)lowerRight.y), color);
  409. }
  410. void GFXDrawUtil::drawRectFill( const RectI &rect, const ColorI &color )
  411. {
  412. drawRectFill(rect.point, Point2I(rect.extent.x + rect.point.x - 1, rect.extent.y + rect.point.y - 1), color );
  413. }
  414. void GFXDrawUtil::drawRectFill( const Point2F &upperLeft, const Point2F &lowerRight, const ColorI &color )
  415. {
  416. //
  417. // Convert Box a----------x
  418. // | |
  419. // x----------b
  420. // Into Quad
  421. // v0---------v1
  422. // | a x |
  423. // | |
  424. // | x b |
  425. // v2---------v3
  426. //
  427. // NorthWest and NorthEast facing offset vectors
  428. Point2F nw(-0.5,-0.5); /* \ */
  429. Point2F ne(0.5,-0.5); /* / */
  430. GFXVertexBufferHandle<GFXVertexPC> verts(mDevice, 4, GFXBufferTypeVolatile);
  431. verts.lock();
  432. F32 ulOffset = 0.5f - mDevice->getFillConventionOffset();
  433. verts[0].point.set( upperLeft.x+nw.x+ulOffset, upperLeft.y+nw.y+ulOffset, 0.0f );
  434. verts[1].point.set( lowerRight.x+ne.x, upperLeft.y+ne.y+ulOffset, 0.0f );
  435. verts[2].point.set( upperLeft.x-ne.x+ulOffset, lowerRight.y-ne.y, 0.0f );
  436. verts[3].point.set( lowerRight.x-nw.x, lowerRight.y-nw.y, 0.0f );
  437. for (S32 i=0; i<4; i++)
  438. verts[i].color = color;
  439. verts.unlock();
  440. mDevice->setStateBlock(mRectFillSB);
  441. mDevice->setVertexBuffer( verts );
  442. mDevice->setupGenericShaders();
  443. mDevice->drawPrimitive( GFXTriangleStrip, 0, 2 );
  444. }
  445. void GFXDrawUtil::draw2DSquare( const Point2F &screenPoint, F32 width, F32 spinAngle )
  446. {
  447. width *= 0.5;
  448. Point3F offset( screenPoint.x, screenPoint.y, 0.0 );
  449. GFXVertexBufferHandle<GFXVertexPC> verts( mDevice, 4, GFXBufferTypeVolatile );
  450. verts.lock();
  451. verts[0].point.set( -width, -width, 0.0f );
  452. verts[1].point.set( -width, width, 0.0f );
  453. verts[2].point.set( width, -width, 0.0f );
  454. verts[3].point.set( width, width, 0.0f );
  455. verts[0].color = verts[1].color = verts[2].color = verts[3].color = mBitmapModulation;
  456. if (spinAngle == 0.0f)
  457. {
  458. for( S32 i = 0; i < 4; i++ )
  459. verts[i].point += offset;
  460. }
  461. else
  462. {
  463. MatrixF rotMatrix( EulerF( 0.0, 0.0, spinAngle ) );
  464. for( S32 i = 0; i < 4; i++ )
  465. {
  466. rotMatrix.mulP( verts[i].point );
  467. verts[i].point += offset;
  468. }
  469. }
  470. verts.unlock();
  471. mDevice->setVertexBuffer( verts );
  472. mDevice->setStateBlock(mRectFillSB);
  473. mDevice->setupGenericShaders();
  474. mDevice->drawPrimitive( GFXTriangleStrip, 0, 2 );
  475. }
  476. //-----------------------------------------------------------------------------
  477. // Draw Line
  478. //-----------------------------------------------------------------------------
  479. void GFXDrawUtil::drawLine( const Point3F &startPt, const Point3F &endPt, const ColorI &color )
  480. {
  481. drawLine( startPt.x, startPt.y, startPt.z, endPt.x, endPt.y, endPt.z, color );
  482. }
  483. void GFXDrawUtil::drawLine( const Point2F &startPt, const Point2F &endPt, const ColorI &color )
  484. {
  485. drawLine( startPt.x, startPt.y, 0.0f, endPt.x, endPt.y, 0.0f, color );
  486. }
  487. void GFXDrawUtil::drawLine( const Point2I &startPt, const Point2I &endPt, const ColorI &color )
  488. {
  489. drawLine( startPt.x, startPt.y, 0.0f, endPt.x, endPt.y, 0.0f, color );
  490. }
  491. void GFXDrawUtil::drawLine( F32 x1, F32 y1, F32 x2, F32 y2, const ColorI &color )
  492. {
  493. drawLine( x1, y1, 0.0f, x2, y2, 0.0f, color );
  494. }
  495. void GFXDrawUtil::drawLine( F32 x1, F32 y1, F32 z1, F32 x2, F32 y2, F32 z2, const ColorI &color )
  496. {
  497. GFXVertexBufferHandle<GFXVertexPC> verts( mDevice, 2, GFXBufferTypeVolatile );
  498. verts.lock();
  499. verts[0].point.set( x1, y1, z1 );
  500. verts[1].point.set( x2, y2, z2 );
  501. verts[0].color = color;
  502. verts[1].color = color;
  503. verts.unlock();
  504. mDevice->setVertexBuffer( verts );
  505. mDevice->setStateBlock( mRectFillSB );
  506. mDevice->drawPrimitive( GFXLineList, 0, 1 );
  507. }
  508. //-----------------------------------------------------------------------------
  509. // 3D World Draw Misc
  510. //-----------------------------------------------------------------------------
  511. static SphereMesh gSphere;
  512. void GFXDrawUtil::drawSphere( const GFXStateBlockDesc &desc, F32 radius, const Point3F &pos, const ColorI &color, bool drawTop, bool drawBottom, const MatrixF *xfm )
  513. {
  514. MatrixF mat;
  515. if ( xfm )
  516. mat = *xfm;
  517. else
  518. mat = MatrixF::Identity;
  519. mat.scale(Point3F(radius,radius,radius));
  520. mat.setPosition(pos);
  521. GFX->pushWorldMatrix();
  522. GFX->multWorld(mat);
  523. const SphereMesh::TriangleMesh * sphereMesh = gSphere.getMesh(2);
  524. S32 numPoly = sphereMesh->numPoly;
  525. S32 totalPoly = 0;
  526. GFXVertexBufferHandle<GFXVertexPC> verts(mDevice, numPoly*3, GFXBufferTypeVolatile);
  527. verts.lock();
  528. S32 vertexIndex = 0;
  529. for (S32 i=0; i<numPoly; i++)
  530. {
  531. if (!drawBottom)
  532. {
  533. if (sphereMesh->poly[i].pnt[0].z < -0.01f || sphereMesh->poly[i].pnt[1].z < -0.01f || sphereMesh->poly[i].pnt[2].z < -0.01f)
  534. continue;
  535. }
  536. if (!drawTop)
  537. {
  538. if (sphereMesh->poly[i].pnt[0].z > 0.01f || sphereMesh->poly[i].pnt[1].z > 0.01f || sphereMesh->poly[i].pnt[2].z > 0.01f)
  539. continue;
  540. }
  541. totalPoly++;
  542. verts[vertexIndex].point = sphereMesh->poly[i].pnt[0];
  543. verts[vertexIndex].color = color;
  544. vertexIndex++;
  545. verts[vertexIndex].point = sphereMesh->poly[i].pnt[1];
  546. verts[vertexIndex].color = color;
  547. vertexIndex++;
  548. verts[vertexIndex].point = sphereMesh->poly[i].pnt[2];
  549. verts[vertexIndex].color = color;
  550. vertexIndex++;
  551. }
  552. verts.unlock();
  553. mDevice->setStateBlockByDesc( desc );
  554. mDevice->setVertexBuffer( verts );
  555. mDevice->setupGenericShaders();
  556. mDevice->drawPrimitive( GFXTriangleList, 0, totalPoly );
  557. GFX->popWorldMatrix();
  558. }
  559. //-----------------------------------------------------------------------------
  560. static const Point3F cubePoints[8] =
  561. {
  562. Point3F(-1, -1, -1), Point3F(-1, -1, 1), Point3F(-1, 1, -1), Point3F(-1, 1, 1),
  563. Point3F( 1, -1, -1), Point3F( 1, -1, 1), Point3F( 1, 1, -1), Point3F( 1, 1, 1)
  564. };
  565. static const U32 cubeFaces[6][4] =
  566. {
  567. { 0, 4, 6, 2 }, { 0, 2, 3, 1 }, { 0, 1, 5, 4 },
  568. { 3, 2, 6, 7 }, { 7, 6, 4, 5 }, { 3, 7, 5, 1 }
  569. };
  570. void GFXDrawUtil::drawTriangle( const GFXStateBlockDesc &desc, const Point3F &p0, const Point3F &p1, const Point3F &p2, const ColorI &color, const MatrixF *xfm )
  571. {
  572. if ( desc.fillMode == GFXFillWireframe )
  573. _drawWireTriangle( desc, p0, p1, p2, color, xfm );
  574. else
  575. _drawSolidTriangle( desc, p0, p1, p2, color, xfm );
  576. }
  577. void GFXDrawUtil::_drawWireTriangle( const GFXStateBlockDesc &desc, const Point3F &p0, const Point3F &p1, const Point3F &p2, const ColorI &color, const MatrixF *xfm )
  578. {
  579. GFXVertexBufferHandle<GFXVertexPC> verts(mDevice, 4, GFXBufferTypeVolatile);
  580. verts.lock();
  581. // Set up the line strip
  582. verts[0].point = p0;
  583. verts[0].color = color;
  584. verts[1].point = p1;
  585. verts[1].color = color;
  586. verts[2].point = p2;
  587. verts[2].color = color;
  588. verts[3].point = p0;
  589. verts[3].color = color;
  590. // Apply xfm if we were passed one.
  591. if ( xfm != NULL )
  592. {
  593. for ( U32 i = 0; i < 4; i++ )
  594. xfm->mulP( verts[i].point );
  595. }
  596. verts.unlock();
  597. GFXStateBlockRef sb = mDevice->createStateBlock( desc );
  598. mDevice->setStateBlock( sb );
  599. mDevice->setVertexBuffer( verts );
  600. mDevice->setupGenericShaders();
  601. mDevice->drawPrimitive( GFXLineStrip, 0, 3 );
  602. }
  603. void GFXDrawUtil::_drawSolidTriangle( const GFXStateBlockDesc &desc, const Point3F &p0, const Point3F &p1, const Point3F &p2, const ColorI &color, const MatrixF *xfm )
  604. {
  605. GFXVertexBufferHandle<GFXVertexPC> verts(mDevice, 3, GFXBufferTypeVolatile);
  606. verts.lock();
  607. // Set up the line strip
  608. verts[0].point = p0;
  609. verts[0].color = color;
  610. verts[1].point = p1;
  611. verts[1].color = color;
  612. verts[2].point = p2;
  613. verts[2].color = color;
  614. // Apply xfm if we were passed one.
  615. if ( xfm != NULL )
  616. {
  617. for ( U32 i = 0; i < 3; i++ )
  618. xfm->mulP( verts[i].point );
  619. }
  620. verts.unlock();
  621. GFXStateBlockRef sb = mDevice->createStateBlock( desc );
  622. mDevice->setStateBlock( sb );
  623. mDevice->setVertexBuffer( verts );
  624. mDevice->setupGenericShaders();
  625. mDevice->drawPrimitive( GFXTriangleList, 0, 1 );
  626. }
  627. void GFXDrawUtil::drawPolygon( const GFXStateBlockDesc& desc, const Point3F* points, U32 numPoints, const ColorI& color, const MatrixF* xfm /* = NULL */ )
  628. {
  629. const bool isWireframe = ( desc.fillMode == GFXFillWireframe );
  630. const U32 numVerts = isWireframe ? numPoints + 1 : numPoints;
  631. GFXVertexBufferHandle< GFXVertexPC > verts( mDevice, numVerts, GFXBufferTypeVolatile );
  632. verts.lock();
  633. for( U32 i = 0; i < numPoints; ++ i )
  634. {
  635. verts[ i ].point = points[ i ];
  636. verts[ i ].color = color;
  637. }
  638. if( xfm )
  639. {
  640. for( U32 i = 0; i < numPoints; ++ i )
  641. xfm->mulP( verts[ i ].point );
  642. }
  643. if( isWireframe )
  644. {
  645. verts[ numVerts - 1 ].point = verts[ 0 ].point;
  646. verts[ numVerts - 1 ].color = color;
  647. }
  648. verts.unlock();
  649. mDevice->setStateBlockByDesc( desc );
  650. mDevice->setVertexBuffer( verts );
  651. mDevice->setupGenericShaders();
  652. if( desc.fillMode == GFXFillWireframe )
  653. mDevice->drawPrimitive( GFXLineStrip, 0, numPoints );
  654. else
  655. mDevice->drawPrimitive( GFXTriangleFan, 0, numPoints - 2 );
  656. }
  657. void GFXDrawUtil::drawCube( const GFXStateBlockDesc &desc, const Box3F &box, const ColorI &color, const MatrixF *xfm )
  658. {
  659. drawCube( desc, box.getExtents(), box.getCenter(), color, xfm );
  660. }
  661. void GFXDrawUtil::drawCube( const GFXStateBlockDesc &desc, const Point3F &size, const Point3F &pos, const ColorI &color, const MatrixF *xfm )
  662. {
  663. if ( desc.fillMode == GFXFillWireframe )
  664. _drawWireCube( desc, size, pos, color, xfm );
  665. else
  666. _drawSolidCube( desc, size, pos, color, xfm );
  667. }
  668. void GFXDrawUtil::_drawWireCube( const GFXStateBlockDesc &desc, const Point3F &size, const Point3F &pos, const ColorI &color, const MatrixF *xfm )
  669. {
  670. GFXVertexBufferHandle<GFXVertexPC> verts(mDevice, 30, GFXBufferTypeVolatile);
  671. verts.lock();
  672. Point3F halfSize = size * 0.5f;
  673. // setup 6 line loops
  674. U32 vertexIndex = 0;
  675. for(S32 i = 0; i < 6; i++)
  676. {
  677. for(S32 j = 0; j < 5; j++)
  678. {
  679. S32 idx = cubeFaces[i][j%4];
  680. verts[vertexIndex].point = cubePoints[idx] * halfSize;
  681. verts[vertexIndex].color = color;
  682. vertexIndex++;
  683. }
  684. }
  685. // Apply xfm if we were passed one.
  686. if ( xfm != NULL )
  687. {
  688. for ( U32 i = 0; i < 30; i++ )
  689. xfm->mulP( verts[i].point );
  690. }
  691. // Apply position offset
  692. for ( U32 i = 0; i < 30; i++ )
  693. verts[i].point += pos;
  694. verts.unlock();
  695. mDevice->setStateBlockByDesc( desc );
  696. mDevice->setVertexBuffer( verts );
  697. mDevice->setupGenericShaders();
  698. for( U32 i=0; i<6; i++ )
  699. mDevice->drawPrimitive( GFXLineStrip, i*5, 4 );
  700. }
  701. void GFXDrawUtil::_drawSolidCube( const GFXStateBlockDesc &desc, const Point3F &size, const Point3F &pos, const ColorI &color, const MatrixF *xfm )
  702. {
  703. GFXVertexBufferHandle<GFXVertexPC> verts(mDevice, 36, GFXBufferTypeVolatile);
  704. verts.lock();
  705. Point3F halfSize = size * 0.5f;
  706. // setup 6 line loops
  707. U32 vertexIndex = 0;
  708. U32 idx;
  709. for(S32 i = 0; i < 6; i++)
  710. {
  711. idx = cubeFaces[i][0];
  712. verts[vertexIndex].point = cubePoints[idx] * halfSize;
  713. verts[vertexIndex].color = color;
  714. vertexIndex++;
  715. idx = cubeFaces[i][1];
  716. verts[vertexIndex].point = cubePoints[idx] * halfSize;
  717. verts[vertexIndex].color = color;
  718. vertexIndex++;
  719. idx = cubeFaces[i][3];
  720. verts[vertexIndex].point = cubePoints[idx] * halfSize;
  721. verts[vertexIndex].color = color;
  722. vertexIndex++;
  723. idx = cubeFaces[i][1];
  724. verts[vertexIndex].point = cubePoints[idx] * halfSize;
  725. verts[vertexIndex].color = color;
  726. vertexIndex++;
  727. idx = cubeFaces[i][2];
  728. verts[vertexIndex].point = cubePoints[idx] * halfSize;
  729. verts[vertexIndex].color = color;
  730. vertexIndex++;
  731. idx = cubeFaces[i][3];
  732. verts[vertexIndex].point = cubePoints[idx] * halfSize;
  733. verts[vertexIndex].color = color;
  734. vertexIndex++;
  735. }
  736. // Apply xfm if we were passed one.
  737. if ( xfm != NULL )
  738. {
  739. for ( U32 i = 0; i < 36; i++ )
  740. xfm->mulV( verts[i].point );
  741. }
  742. // Apply position offset
  743. for ( U32 i = 0; i < 36; i++ )
  744. verts[i].point += pos;
  745. verts.unlock();
  746. mDevice->setStateBlockByDesc( desc );
  747. mDevice->setVertexBuffer( verts );
  748. mDevice->setupGenericShaders();
  749. mDevice->drawPrimitive( GFXTriangleList, 0, 12 );
  750. }
  751. void GFXDrawUtil::drawPolyhedron( const GFXStateBlockDesc &desc, const AnyPolyhedron &poly, const ColorI &color, const MatrixF *xfm )
  752. {
  753. if ( desc.fillMode == GFXFillWireframe )
  754. _drawWirePolyhedron( desc, poly, color, xfm );
  755. else
  756. _drawSolidPolyhedron( desc, poly, color, xfm );
  757. }
  758. void GFXDrawUtil::_drawWirePolyhedron( const GFXStateBlockDesc &desc, const AnyPolyhedron &poly, const ColorI &color, const MatrixF *xfm )
  759. {
  760. GFXDEBUGEVENT_SCOPE( GFXDrawUtil_DrawWirePolyhedron, ColorI::GREEN );
  761. const U32 numEdges = poly.getNumEdges();
  762. const Point3F* points = poly.getPoints();
  763. const Polyhedron::Edge* edges = poly.getEdges();
  764. // Allocate a temporary vertex buffer.
  765. GFXVertexBufferHandle< GFXVertexPC > verts( mDevice, numEdges * 2, GFXBufferTypeVolatile);
  766. // Fill it with the vertices for the edges.
  767. verts.lock();
  768. for( U32 i = 0; i < numEdges; ++ i )
  769. {
  770. const U32 nvert = i * 2;
  771. verts[ nvert + 0 ].point = points[ edges[ i ].vertex[ 0 ] ];
  772. verts[ nvert + 0 ].color = color;
  773. verts[ nvert + 1 ].point = points[ edges[ i ].vertex[ 1 ] ];
  774. verts[ nvert + 1 ].color = color;
  775. }
  776. if( xfm )
  777. {
  778. for( U32 i = 0; i < numEdges; ++ i )
  779. {
  780. xfm->mulP( verts[ i + 0 ].point );
  781. xfm->mulP( verts[ i + 1 ].point );
  782. }
  783. }
  784. verts.unlock();
  785. // Render the line list.
  786. mDevice->setStateBlockByDesc( desc );
  787. mDevice->setVertexBuffer( verts );
  788. mDevice->setupGenericShaders();
  789. mDevice->drawPrimitive( GFXLineList, 0, numEdges );
  790. }
  791. void GFXDrawUtil::_drawSolidPolyhedron( const GFXStateBlockDesc &desc, const AnyPolyhedron &poly, const ColorI &color, const MatrixF *xfm )
  792. {
  793. GFXDEBUGEVENT_SCOPE( GFXDrawUtil_DrawSolidPolyhedron, ColorI::GREEN );
  794. const U32 numPoints = poly.getNumPoints();
  795. const Point3F* points = poly.getPoints();
  796. const PlaneF* planes = poly.getPlanes();
  797. const Point3F viewDir = GFX->getViewMatrix().getForwardVector();
  798. // Create a temp buffer for the vertices and
  799. // put all the polyhedron's points in there.
  800. GFXVertexBufferHandle< GFXVertexPC > verts( mDevice, numPoints, GFXBufferTypeVolatile );
  801. verts.lock();
  802. for( U32 i = 0; i < numPoints; ++ i )
  803. {
  804. verts[ i ].point = points[ i ];
  805. verts[ i ].color = color;
  806. }
  807. if( xfm )
  808. {
  809. for( U32 i = 0; i < numPoints; ++ i )
  810. xfm->mulP( verts[ i ].point );
  811. }
  812. verts.unlock();
  813. // Allocate a temp buffer for the face indices.
  814. const U32 numIndices = poly.getNumEdges() * 2;
  815. const U32 numPlanes = poly.getNumPlanes();
  816. GFXPrimitiveBufferHandle prims( mDevice, numIndices, 0, GFXBufferTypeVolatile );
  817. // Unfortunately, since polygons may have varying numbers of
  818. // vertices, we also need to retain that information.
  819. FrameTemp< U32 > numIndicesForPoly( numPlanes );
  820. U32 numPolys = 0;
  821. // Create all the polygon indices.
  822. U16* indices;
  823. prims.lock( &indices );
  824. U32 idx = 0;
  825. for( U32 i = 0; i < numPlanes; ++ i )
  826. {
  827. // Since face extraction is somewhat costly, don't bother doing it for
  828. // backfacing polygons if culling is enabled.
  829. if( !desc.cullDefined || desc.cullMode != GFXCullNone )
  830. {
  831. F32 dot = mDot( planes[ i ], viewDir );
  832. // See if it faces *the same way* as the view direction. This would
  833. // normally mean that the face is *not* backfacing but since we expect
  834. // planes on the polyhedron to be facing *inwards*, we need to reverse
  835. // the logic here.
  836. if( dot > 0.f )
  837. continue;
  838. }
  839. U32 numPoints = poly.extractFace( i, &indices[ idx ], numIndices - idx );
  840. numIndicesForPoly[ numPolys ] = numPoints;
  841. idx += numPoints;
  842. numPolys ++;
  843. }
  844. prims.unlock();
  845. // Set up state.
  846. mDevice->setStateBlockByDesc( desc );
  847. mDevice->setupGenericShaders();
  848. mDevice->setVertexBuffer( verts );
  849. mDevice->setPrimitiveBuffer( prims );
  850. // Render one triangle fan for each polygon.
  851. U32 startIndex = 0;
  852. for( U32 i = 0; i < numPolys; ++ i )
  853. {
  854. U32 numVerts = numIndicesForPoly[ i ];
  855. mDevice->drawIndexedPrimitive( GFXTriangleFan, 0, 0, numPoints, startIndex, numVerts - 2 );
  856. startIndex += numVerts;
  857. }
  858. }
  859. void GFXDrawUtil::drawObjectBox( const GFXStateBlockDesc &desc, const Point3F &size, const Point3F &pos, const MatrixF &objMat, const ColorI &color )
  860. {
  861. GFXTransformSaver saver;
  862. mDevice->setStateBlockByDesc( desc );
  863. MatrixF scaledObjMat( true );
  864. scaledObjMat = objMat;
  865. scaledObjMat.scale( size );
  866. scaledObjMat.setPosition( pos );
  867. PrimBuild::color( color );
  868. PrimBuild::begin( GFXLineList, 48 );
  869. static const Point3F cubePoints[8] =
  870. {
  871. Point3F(-0.5, -0.5, -0.5), Point3F(-0.5, -0.5, 0.5), Point3F(-0.5, 0.5, -0.5), Point3F(-0.5, 0.5, 0.5),
  872. Point3F( 0.5, -0.5, -0.5), Point3F( 0.5, -0.5, 0.5), Point3F( 0.5, 0.5, -0.5), Point3F( 0.5, 0.5, 0.5)
  873. };
  874. // 8 corner points of the box
  875. for ( U32 i = 0; i < 8; i++ )
  876. {
  877. //const Point3F &start = cubePoints[i];
  878. // 3 lines per corner point
  879. for ( U32 j = 0; j < 3; j++ )
  880. {
  881. Point3F start = cubePoints[i];
  882. Point3F end = start;
  883. end[j] *= 0.8f;
  884. scaledObjMat.mulP(start);
  885. PrimBuild::vertex3fv(start);
  886. scaledObjMat.mulP(end);
  887. PrimBuild::vertex3fv(end);
  888. }
  889. }
  890. PrimBuild::end();
  891. }
  892. static const Point2F circlePoints[] =
  893. {
  894. Point2F(0.707107f, 0.707107f),
  895. Point2F(0.923880f, 0.382683f),
  896. Point2F(1.000000f, 0.000000f),
  897. Point2F(0.923880f, -0.382684f),
  898. Point2F(0.707107f, -0.707107f),
  899. Point2F(0.382683f, -0.923880f),
  900. Point2F(0.000000f, -1.000000f),
  901. Point2F(-0.382683f, -0.923880f),
  902. Point2F(-0.707107f, -0.707107f),
  903. Point2F(-0.923880f, -0.382684f),
  904. Point2F(-1.000000f, 0.000000f),
  905. Point2F(-0.923879f, 0.382684f),
  906. Point2F(-0.707107f, 0.707107f),
  907. Point2F(-0.382683f, 0.923880f),
  908. Point2F(0.000000f, 1.000000f),
  909. Point2F(0.382684f, 0.923879f)
  910. };
  911. void GFXDrawUtil::drawCapsule( const GFXStateBlockDesc &desc, const Point3F &center, F32 radius, F32 height, const ColorI &color, const MatrixF *xfm )
  912. {
  913. if ( desc.fillMode == GFXFillWireframe )
  914. _drawWireCapsule( desc, center, radius, height, color, xfm );
  915. else
  916. _drawSolidCapsule( desc, center, radius, height, color, xfm );
  917. }
  918. void GFXDrawUtil::_drawSolidCapsule( const GFXStateBlockDesc &desc, const Point3F &center, F32 radius, F32 height, const ColorI &color, const MatrixF *xfm )
  919. {
  920. MatrixF mat;
  921. if ( xfm )
  922. mat = *xfm;
  923. else
  924. mat = MatrixF::Identity;
  925. S32 numPoints = sizeof(circlePoints)/sizeof(Point2F);
  926. GFXVertexBufferHandle<GFXVertexPC> verts(mDevice, numPoints * 2 + 2, GFXBufferTypeVolatile);
  927. verts.lock();
  928. for (S32 i=0; i<numPoints + 1; i++)
  929. {
  930. S32 imod = i % numPoints;
  931. verts[2 * i].point = Point3F( circlePoints[imod].x * radius, circlePoints[imod].y * radius, height );
  932. verts[2 * i].color = color;
  933. verts[2 * i + 1].point = Point3F( circlePoints[imod].x * radius, circlePoints[imod].y * radius, -height );
  934. verts[2 * i + 1].color = color;
  935. }
  936. S32 totalNumPnts = numPoints * 2 + 2;
  937. // Apply xfm if we were passed one.
  938. for ( U32 i = 0; i < totalNumPnts; i++ )
  939. mat.mulP( verts[i].point );
  940. // Apply position offset
  941. for ( U32 i = 0; i < totalNumPnts; i++ )
  942. verts[i].point += center;
  943. verts.unlock();
  944. mDevice->setStateBlockByDesc( desc );
  945. mDevice->setVertexBuffer( verts );
  946. mDevice->setupGenericShaders();
  947. mDevice->drawPrimitive( GFXTriangleStrip, 0, 2 * numPoints );
  948. Point3F sphereCenter;
  949. MatrixF sphereMat;
  950. if ( xfm )
  951. sphereMat = *xfm;
  952. else
  953. sphereMat = MatrixF::Identity;
  954. sphereCenter.set( 0, 0, 0.5f * height );
  955. mat.mulV( sphereCenter );
  956. sphereCenter += center;
  957. drawSphere( desc, radius, sphereCenter, color, true, false, &sphereMat );
  958. sphereCenter.set( 0, 0, -0.5f * height );
  959. mat.mulV( sphereCenter );
  960. sphereCenter += center;
  961. drawSphere( desc, radius, sphereCenter, color, false, true, &sphereMat );
  962. }
  963. void GFXDrawUtil::_drawWireCapsule( const GFXStateBlockDesc &desc, const Point3F &center, F32 radius, F32 height, const ColorI &color, const MatrixF *xfm )
  964. {
  965. MatrixF mat;
  966. if ( xfm )
  967. mat = *xfm;
  968. else
  969. mat = MatrixF::Identity;
  970. mat.scale( Point3F(radius,radius,height*0.5f) );
  971. mat.setPosition(center);
  972. mDevice->pushWorldMatrix();
  973. mDevice->multWorld(mat);
  974. S32 numPoints = sizeof(circlePoints)/sizeof(Point2F);
  975. GFXVertexBufferHandle<GFXVertexPC> verts(mDevice, numPoints, GFXBufferTypeVolatile);
  976. verts.lock();
  977. for (S32 i=0; i< numPoints; i++)
  978. {
  979. S32 idx = i & (~1); // just draw the even ones
  980. F32 z = i & 1 ? 1.0f : -1.0f;
  981. verts[i].point = Point3F(circlePoints[idx].x,circlePoints[idx].y, z);
  982. verts[i].color = color;
  983. }
  984. verts.unlock();
  985. mDevice->setStateBlockByDesc( desc );
  986. mDevice->setVertexBuffer( verts );
  987. mDevice->setupGenericShaders();
  988. for (S32 i=0; i<numPoints; i += 2)
  989. mDevice->drawPrimitive(GFXLineStrip, i, 1);
  990. mDevice->popWorldMatrix();
  991. Point3F sphereCenter;
  992. sphereCenter.z = center.z + 0.5f * height;
  993. drawSphere( desc, radius,sphereCenter,color,true,false);
  994. sphereCenter.z = center.z - 0.5f * height;
  995. drawSphere( desc, radius,sphereCenter,color,false,true);
  996. }
  997. void GFXDrawUtil::drawCone( const GFXStateBlockDesc &desc, const Point3F &basePnt, const Point3F &tipPnt, F32 baseRadius, const ColorI &color )
  998. {
  999. VectorF uvec = tipPnt - basePnt;
  1000. F32 height = uvec.len();
  1001. uvec.normalize();
  1002. MatrixF mat( true );
  1003. MathUtils::getMatrixFromUpVector( uvec, &mat );
  1004. mat.setPosition(basePnt);
  1005. Point3F scale( baseRadius, baseRadius, height );
  1006. mat.scale(scale);
  1007. GFXTransformSaver saver;
  1008. mDevice->pushWorldMatrix();
  1009. mDevice->multWorld(mat);
  1010. S32 numPoints = sizeof(circlePoints)/sizeof(Point2F);
  1011. GFXVertexBufferHandle<GFXVertexPC> verts(mDevice, numPoints + 2, GFXBufferTypeVolatile);
  1012. verts.lock();
  1013. verts[0].point = Point3F(0.0f,0.0f,1.0f);
  1014. verts[0].color = color;
  1015. for (S32 i=0; i<numPoints + 1; i++)
  1016. {
  1017. S32 imod = i % numPoints;
  1018. verts[i + 1].point = Point3F(circlePoints[imod].x,circlePoints[imod].y, 0.0f);
  1019. verts[i + 1].color = color;
  1020. }
  1021. verts.unlock();
  1022. mDevice->setStateBlockByDesc( desc );
  1023. mDevice->setVertexBuffer( verts );
  1024. mDevice->setupGenericShaders( GFXDevice::GSModColorTexture );
  1025. mDevice->drawPrimitive( GFXTriangleFan, 0, numPoints );
  1026. mDevice->drawPrimitive( GFXTriangleFan, 1, numPoints-1 );
  1027. mDevice->popWorldMatrix();
  1028. }
  1029. void GFXDrawUtil::drawCylinder( const GFXStateBlockDesc &desc, const Point3F &basePnt, const Point3F &tipPnt, F32 radius, const ColorI &color )
  1030. {
  1031. VectorF uvec = tipPnt - basePnt;
  1032. F32 height = uvec.len();
  1033. uvec.normalize();
  1034. MatrixF mat( true );
  1035. MathUtils::getMatrixFromUpVector( uvec, &mat );
  1036. mat.setPosition(basePnt);
  1037. Point3F scale( radius, radius, height * 2 );
  1038. mat.scale(scale);
  1039. GFXTransformSaver saver;
  1040. mDevice->pushWorldMatrix();
  1041. mDevice->multWorld(mat);
  1042. S32 numPoints = sizeof(circlePoints)/sizeof(Point2F);
  1043. GFXVertexBufferHandle<GFXVertexPC> verts(mDevice, numPoints * 4 + 4, GFXBufferTypeVolatile);
  1044. verts.lock();
  1045. for (S32 i=0; i<numPoints + 1; i++)
  1046. {
  1047. S32 imod = i % numPoints;
  1048. verts[i].point = Point3F(circlePoints[imod].x,circlePoints[imod].y, 0.5f);
  1049. verts[i].color = color;
  1050. verts[i + numPoints + 1].point = Point3F(circlePoints[imod].x,circlePoints[imod].y, 0);
  1051. verts[i + numPoints + 1].color = color;
  1052. verts[2*numPoints + 2 + 2 * i].point = Point3F(circlePoints[imod].x,circlePoints[imod].y, 0.5f);
  1053. verts[2*numPoints + 2 + 2 * i].color = color;
  1054. verts[2*numPoints + 2 + 2 * i + 1].point = Point3F(circlePoints[imod].x,circlePoints[imod].y, 0);
  1055. verts[2*numPoints + 2 + 2 * i + 1].color = color;
  1056. }
  1057. verts.unlock();
  1058. mDevice->setStateBlockByDesc( desc );
  1059. mDevice->setVertexBuffer( verts );
  1060. mDevice->setupGenericShaders( GFXDevice::GSModColorTexture );
  1061. mDevice->drawPrimitive( GFXTriangleFan, 0, numPoints );
  1062. mDevice->drawPrimitive( GFXTriangleFan, numPoints + 1, numPoints );
  1063. mDevice->drawPrimitive( GFXTriangleStrip, 2 * numPoints + 2, 2 * numPoints);
  1064. mDevice->popWorldMatrix();
  1065. }
  1066. void GFXDrawUtil::drawArrow( const GFXStateBlockDesc &desc, const Point3F &start, const Point3F &end, const ColorI &color )
  1067. {
  1068. GFXTransformSaver saver;
  1069. // Direction and length of the arrow.
  1070. VectorF dir = end - start;
  1071. F32 len = dir.len();
  1072. dir.normalize();
  1073. len *= 0.2f;
  1074. // Base of the cone will be a distance back from the end of the arrow
  1075. // proportional to the total distance of the arrow... 0.3f looks about right.
  1076. Point3F coneBase = end - dir * len * 0.3f;
  1077. // Calculate the radius of the cone given that we want the cone to have
  1078. // an angle of 25 degrees (just because it looks good).
  1079. F32 coneLen = ( end - coneBase ).len();
  1080. F32 coneDiameter = mTan( mDegToRad(25.0f) ) * coneLen;
  1081. // Draw the cone on at the arrow's tip.
  1082. drawCone( desc, coneBase, end, coneDiameter / 2.0f, color );
  1083. // Get the difference in length from
  1084. // the start of the cone to the end
  1085. // of the cylinder so we can put the
  1086. // end of the cylinder right against where
  1087. // the cone starts.
  1088. Point3F coneDiff = end - coneBase;
  1089. // Draw the cylinder.
  1090. F32 stickRadius = len * 0.025f;
  1091. drawCylinder( desc, start, end - coneDiff, stickRadius, color );
  1092. }
  1093. void GFXDrawUtil::drawFrustum( const Frustum &f, const ColorI &color )
  1094. {
  1095. const Point3F *points = f.getPoints();
  1096. // Draw near and far planes.
  1097. for (U32 offset = 0; offset < 8; offset+=4)
  1098. {
  1099. drawLine(points[offset+0], points[offset+1], color);
  1100. drawLine(points[offset+2], points[offset+3], color);
  1101. drawLine(points[offset+0], points[offset+2], color);
  1102. drawLine(points[offset+1], points[offset+3], color);
  1103. }
  1104. // connect the near and far planes
  1105. drawLine(points[Frustum::NearTopLeft], points[Frustum::FarTopLeft], color);
  1106. drawLine(points[Frustum::NearTopRight], points[Frustum::FarTopRight], color);
  1107. drawLine(points[Frustum::NearBottomLeft], points[Frustum::FarBottomLeft], color);
  1108. drawLine(points[Frustum::NearBottomRight], points[Frustum::FarBottomRight], color);
  1109. }
  1110. void GFXDrawUtil::drawSolidPlane( const GFXStateBlockDesc &desc, const Point3F &pos, const Point2F &size, const ColorI &color )
  1111. {
  1112. GFXVertexBufferHandle<GFXVertexPC> verts(mDevice, 4, GFXBufferTypeVolatile);
  1113. verts.lock();
  1114. verts[0].point = pos + Point3F( -size.x / 2.0f, -size.y / 2.0f, 0 );
  1115. verts[0].color = color;
  1116. verts[1].point = pos + Point3F( -size.x / 2.0f, size.y / 2.0f, 0 );
  1117. verts[1].color = color;
  1118. verts[2].point = pos + Point3F( size.x / 2.0f, size.y / 2.0f, 0 );
  1119. verts[2].color = color;
  1120. verts[3].point = pos + Point3F( size.x / 2.0f, -size.y / 2.0f, 0 );
  1121. verts[3].color = color;
  1122. verts.unlock();
  1123. mDevice->setStateBlockByDesc( desc );
  1124. mDevice->setVertexBuffer( verts );
  1125. mDevice->setupGenericShaders();
  1126. mDevice->drawPrimitive( GFXTriangleFan, 0, 2 );
  1127. }
  1128. void GFXDrawUtil::drawPlaneGrid( const GFXStateBlockDesc &desc, const Point3F &pos, const Point2F &size, const Point2F &step, const ColorI &color, Plane plane )
  1129. {
  1130. // Note that when calculating the number of steps, we +0.5 to round up,
  1131. // and +1 for the last line (ie. 4 steps needs 5 lines to be rendered)
  1132. U32 uSteps = 0;
  1133. if( step.x > 0 )
  1134. uSteps = size.x / step.x + 0.5 + 1;
  1135. U32 vSteps = 0;
  1136. if( step.y > 0 )
  1137. vSteps = size.y / step.y + 0.5 + 1;
  1138. if( uSteps <= 1 || vSteps <= 1 )
  1139. return;
  1140. const U32 numVertices = uSteps * 2 + vSteps * 2;
  1141. const U32 numLines = uSteps + vSteps;
  1142. Point3F origin;
  1143. switch( plane )
  1144. {
  1145. case PlaneXY:
  1146. origin = Point3F( pos.x - ( size.x / 2.0f ), pos.y - ( size.y / 2.0f ), pos.z );
  1147. break;
  1148. case PlaneXZ:
  1149. origin = Point3F( pos.x - ( size.x / 2.0f ), pos.y, pos.z - ( size.y / 2.0f ) );
  1150. break;
  1151. case PlaneYZ:
  1152. origin = Point3F( pos.x, pos.y - ( size.x / 2.0f ), pos.z - ( size.y / 2.0f ) );
  1153. break;
  1154. }
  1155. GFXVertexBufferHandle<GFXVertexPC> verts( mDevice, numVertices, GFXBufferTypeVolatile );
  1156. verts.lock();
  1157. U32 vertCount = 0;
  1158. if( plane == PlaneXY || plane == PlaneXZ )
  1159. {
  1160. F32 start = mFloor( origin.x / step.x + 0.5f ) * step.x;
  1161. for ( U32 i = 0; i < uSteps; i++ )
  1162. {
  1163. verts[vertCount].point = Point3F( start + step.x * i, origin.y, origin.z );
  1164. verts[vertCount].color = color;
  1165. ++vertCount;
  1166. if( plane == PlaneXY )
  1167. verts[vertCount].point = Point3F( start + step.x * i, origin.y + size.y, origin.z );
  1168. else
  1169. verts[vertCount].point = Point3F( start + step.x * i, origin.y, origin.z + size.y );
  1170. verts[vertCount].color = color;
  1171. ++vertCount;
  1172. }
  1173. }
  1174. if( plane == PlaneXY || plane == PlaneYZ )
  1175. {
  1176. U32 num;
  1177. F32 stp;
  1178. if( plane == PlaneXY )
  1179. {
  1180. num = vSteps;
  1181. stp = step.y;
  1182. }
  1183. else
  1184. {
  1185. num = uSteps;
  1186. stp = step.x;
  1187. }
  1188. F32 start = mFloor( origin.y / stp + 0.5f ) * stp;
  1189. for ( U32 i = 0; i < num; i++ )
  1190. {
  1191. verts[vertCount].point = Point3F( origin.x, start + stp * i, origin.z );
  1192. verts[vertCount].color = color;
  1193. ++vertCount;
  1194. if( plane == PlaneXY )
  1195. verts[vertCount].point = Point3F( origin.x + size.x, start + stp * i, origin.z );
  1196. else
  1197. verts[vertCount].point = Point3F( origin.x, start + stp * i, origin.z + size.x );
  1198. verts[vertCount].color = color;
  1199. ++vertCount;
  1200. }
  1201. }
  1202. if( plane == PlaneXZ || plane == PlaneYZ )
  1203. {
  1204. F32 start = mFloor( origin.z / step.y + 0.5f ) * step.y;
  1205. for ( U32 i = 0; i < vSteps; i++ )
  1206. {
  1207. verts[vertCount].point = Point3F( origin.x, origin.y, start + step.y * i );
  1208. verts[vertCount].color = color;
  1209. ++vertCount;
  1210. if( plane == PlaneXZ )
  1211. verts[vertCount].point = Point3F( origin.x + size.x, origin.y, start + step.y * i );
  1212. else
  1213. verts[vertCount].point = Point3F( origin.x, origin.y + size.x, start + step.y * i );
  1214. verts[vertCount].color = color;
  1215. ++vertCount;
  1216. }
  1217. }
  1218. verts.unlock();
  1219. mDevice->setStateBlockByDesc( desc );
  1220. mDevice->setVertexBuffer( verts );
  1221. mDevice->setupGenericShaders();
  1222. mDevice->drawPrimitive( GFXLineList, 0, numLines );
  1223. }
  1224. void GFXDrawUtil::drawTransform( const GFXStateBlockDesc &desc, const MatrixF &mat, const Point3F *scale, const ColorI colors[3] )
  1225. {
  1226. GFXTransformSaver saver;
  1227. GFX->multWorld( mat );
  1228. GFXVertexBufferHandle<GFXVertexPC> verts( mDevice, 6, GFXBufferTypeVolatile );
  1229. verts.lock();
  1230. const static ColorI defColors[3] =
  1231. {
  1232. ColorI::RED,
  1233. ColorI::GREEN,
  1234. ColorI::BLUE
  1235. };
  1236. const ColorI *colArray = ( colors != NULL ) ? colors : defColors;
  1237. verts[0].point = Point3F::Zero;
  1238. verts[0].color = colArray[0];
  1239. verts[1].point = Point3F( 1, 0, 0 );
  1240. verts[1].color = colArray[0];
  1241. verts[2].point = Point3F::Zero;
  1242. verts[2].color = colArray[1];
  1243. verts[3].point = Point3F( 0, 1, 0 );
  1244. verts[3].color = colArray[1];
  1245. verts[4].point = Point3F::Zero;
  1246. verts[4].color = colArray[2];
  1247. verts[5].point = Point3F( 0, 0, 1 );
  1248. verts[5].color = colArray[2];
  1249. if ( scale )
  1250. {
  1251. verts[1].point *= *scale;
  1252. verts[3].point *= *scale;
  1253. verts[5].point *= *scale;
  1254. }
  1255. verts.unlock();
  1256. mDevice->setStateBlockByDesc( desc );
  1257. mDevice->setVertexBuffer( verts );
  1258. mDevice->setupGenericShaders();
  1259. mDevice->drawPrimitive( GFXLineList, 0, 3 );
  1260. }