gfxDrawUtil.cpp 62 KB

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