gfxDrawUtil.cpp 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891
  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, bool gradientFill)
  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, gradientFill);
  450. }
  451. void GFXDrawUtil::drawRectFill(const Point2I& upperLeft, const Point2I& lowerRight, const ColorI& color, const F32& borderSize, const ColorI& borderColor, bool gradientFill)
  452. {
  453. drawRoundedRect(0.0f, Point2F((F32)upperLeft.x, (F32)upperLeft.y), Point2F((F32)lowerRight.x, (F32)lowerRight.y), color, borderSize, borderColor, gradientFill);
  454. }
  455. void GFXDrawUtil::drawRectFill(const RectI& rect, const ColorI& color, const F32& borderSize, const ColorI& borderColor, bool gradientFill)
  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, gradientFill);
  458. }
  459. void GFXDrawUtil::drawRectFill(const Point2F& upperLeft, const Point2F& lowerRight, const ColorI& color, const F32& borderSize,const ColorI& borderColor, bool gradientFill)
  460. {
  461. // draw a rounded rect with 0 radiuse.
  462. drawRoundedRect(0.0f, upperLeft, lowerRight, color, borderSize, borderColor, gradientFill);
  463. }
  464. void GFXDrawUtil::drawRoundedRect(const F32& cornerRadius, const RectI& rect, const ColorI& color, const F32& borderSize, const ColorI& borderColor, bool gradientFill)
  465. {
  466. drawRoundedRect(cornerRadius, rect.point, Point2I(rect.extent.x + rect.point.x - 1, rect.extent.y + rect.point.y - 1), color, borderSize, borderColor, gradientFill);
  467. }
  468. void GFXDrawUtil::drawRoundedRect(const F32& cornerRadius, const Point2I& upperLeft, const Point2I& lowerRight, const ColorI& color, const F32& borderSize, const ColorI& borderColor, bool gradientFill)
  469. {
  470. drawRoundedRect(cornerRadius, Point2F((F32)upperLeft.x, (F32)upperLeft.y), Point2F((F32)lowerRight.x, (F32)lowerRight.y), color, borderSize, borderColor, gradientFill);
  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. bool gradientFill)
  479. {
  480. // NorthWest and NorthEast facing offset vectors
  481. Point2F nw(-0.5, -0.5); /* \ */
  482. Point2F ne(0.5, -0.5); /* / */
  483. GFXVertexBufferHandle<GFXVertexPCT> verts(mDevice, 4, GFXBufferTypeVolatile);
  484. verts.lock();
  485. F32 ulOffset = 0.5f - mDevice->getFillConventionOffset();
  486. verts[0].point.set(upperLeft.x + nw.x + ulOffset, upperLeft.y + nw.y + ulOffset, 0.0f);
  487. verts[1].point.set(lowerRight.x + ne.x + ulOffset, upperLeft.y + ne.y + ulOffset, 0.0f);
  488. verts[2].point.set(upperLeft.x - ne.x + ulOffset, lowerRight.y - ne.y + ulOffset, 0.0f);
  489. verts[3].point.set(lowerRight.x - nw.x + ulOffset, lowerRight.y - nw.y + ulOffset, 0.0f);
  490. for (S32 i = 0; i < 4; i++)
  491. verts[i].color = color;
  492. if (gradientFill)
  493. {
  494. verts[0].texCoord.set(0.0f, 0.0f); // top left
  495. verts[1].texCoord.set(1.0f, 0.0f); // top right
  496. verts[2].texCoord.set(0.0f, 1.0f); // bottom left
  497. verts[3].texCoord.set(1.0f, 1.0f); // bottom right
  498. }
  499. verts.unlock();
  500. mDevice->setVertexBuffer(verts);
  501. mDevice->setStateBlock(mRectFillSB);
  502. Point2F topLeftCorner(upperLeft.x + nw.x + ulOffset, upperLeft.y + nw.y + ulOffset);
  503. Point2F bottomRightCorner(lowerRight.x - nw.x + ulOffset, lowerRight.y - nw.y + ulOffset);
  504. /*mDevice->setupGenericShaders();*/
  505. GFX->setShader(mRoundRectangleShader);
  506. GFX->setShaderConstBuffer(mRoundRectangleShaderConsts);
  507. MatrixF tempMatrix = GFX->getProjectionMatrix() * GFX->getViewMatrix() * GFX->getWorldMatrix();
  508. Point2F size((F32)(bottomRightCorner.x - topLeftCorner.x), (F32)(bottomRightCorner.y - topLeftCorner.y));
  509. F32 minExtent = mMin(size.x, size.y);
  510. F32 radius = cornerRadius;
  511. if ((minExtent * 0.5) < radius)
  512. {
  513. radius = mClampF(radius, 0.0f, (minExtent * 0.5));
  514. }
  515. mRoundRectangleShaderConsts->set(mRoundRectangleShader->getShaderConstHandle("$modelView"), tempMatrix, GFXSCT_Float4x4);
  516. mRoundRectangleShaderConsts->setSafe(mRoundRectangleShader->getShaderConstHandle("$radius"), radius);
  517. mRoundRectangleShaderConsts->setSafe(mRoundRectangleShader->getShaderConstHandle("$sizeUni"), size);
  518. mRoundRectangleShaderConsts->setSafe(mRoundRectangleShader->getShaderConstHandle("$borderSize"), borderSize);
  519. mRoundRectangleShaderConsts->setSafe(mRoundRectangleShader->getShaderConstHandle("$borderCol"), borderColor);
  520. if (gradientFill)
  521. {
  522. mRoundRectangleShaderConsts->setSafe(mRoundRectangleShader->getShaderConstHandle("$gradientFill"), 1.0f);
  523. }
  524. else
  525. {
  526. mRoundRectangleShaderConsts->setSafe(mRoundRectangleShader->getShaderConstHandle("$gradientFill"), 0.0f);
  527. }
  528. Point2F rectCenter((F32)(topLeftCorner.x + (size.x / 2.0)), (F32)(topLeftCorner.y + (size.y / 2.0)));
  529. mRoundRectangleShaderConsts->setSafe(mRoundRectangleShader->getShaderConstHandle("$rectCenter"), rectCenter);
  530. const Point2I& resolution = GFX->getActiveRenderTarget()->getSize();
  531. Point2F TargetSize(1.0 / (F32)resolution.x, 1.0 / (F32)resolution.y);
  532. mRoundRectangleShaderConsts->setSafe(mRoundRectangleShader->getShaderConstHandle("$oneOverViewport"), TargetSize);
  533. mDevice->drawPrimitive(GFXTriangleStrip, 0, 2);
  534. }
  535. void GFXDrawUtil::draw2DSquare( const Point2F &screenPoint, F32 width, F32 spinAngle )
  536. {
  537. width *= 0.5;
  538. Point3F offset( screenPoint.x, screenPoint.y, 0.0 );
  539. GFXVertexBufferHandle<GFXVertexPCT> verts( mDevice, 4, GFXBufferTypeVolatile );
  540. verts.lock();
  541. verts[0].point.set( -width, -width, 0.0f );
  542. verts[1].point.set( -width, width, 0.0f );
  543. verts[2].point.set( width, -width, 0.0f );
  544. verts[3].point.set( width, width, 0.0f );
  545. verts[0].color = verts[1].color = verts[2].color = verts[3].color = mBitmapModulation;
  546. if (spinAngle == 0.0f)
  547. {
  548. for( S32 i = 0; i < 4; i++ )
  549. verts[i].point += offset;
  550. }
  551. else
  552. {
  553. MatrixF rotMatrix( EulerF( 0.0, 0.0, spinAngle ) );
  554. for( S32 i = 0; i < 4; i++ )
  555. {
  556. rotMatrix.mulP( verts[i].point );
  557. verts[i].point += offset;
  558. }
  559. }
  560. verts.unlock();
  561. mDevice->setVertexBuffer( verts );
  562. mDevice->setStateBlock(mRectFillSB);
  563. mDevice->setupGenericShaders();
  564. mDevice->drawPrimitive( GFXTriangleStrip, 0, 2 );
  565. }
  566. //-----------------------------------------------------------------------------
  567. // Draw Circle : FILL
  568. //-----------------------------------------------------------------------------
  569. void GFXDrawUtil::drawCircleFill(const RectI& rect, const ColorI& color, F32 radius, const F32& borderSize, const ColorI& borderColor)
  570. {
  571. drawCircleFill(rect.point, Point2I(rect.extent.x + rect.point.x - 1, rect.extent.y + rect.point.y - 1), color, radius, borderSize, borderColor);
  572. }
  573. void GFXDrawUtil::drawCircleFill(const Point2I& upperLeft, const Point2I& lowerRight, const ColorI& color, F32 radius, const F32& borderSize, const ColorI& borderColor)
  574. {
  575. drawCircleFill(Point2F((F32)upperLeft.x, (F32)upperLeft.y), Point2F((F32)lowerRight.x, (F32)lowerRight.y), color, radius, borderSize, borderColor);
  576. }
  577. void GFXDrawUtil::drawCircleFill(const Point2F& upperLeft, const Point2F& lowerRight, const ColorI& color, F32 radius, const F32& borderSize, const ColorI& borderColor)
  578. {
  579. // NorthWest and NorthEast facing offset vectors
  580. Point2F nw(-0.5, -0.5); /* \ */
  581. Point2F ne(0.5, -0.5); /* / */
  582. GFXVertexBufferHandle<GFXVertexPCT> verts(mDevice, 4, GFXBufferTypeVolatile);
  583. verts.lock();
  584. F32 ulOffset = 0.5f - mDevice->getFillConventionOffset();
  585. verts[0].point.set(upperLeft.x + nw.x + ulOffset, upperLeft.y + nw.y + ulOffset, 0.0f);
  586. verts[1].point.set(lowerRight.x + ne.x + ulOffset, upperLeft.y + ne.y + ulOffset, 0.0f);
  587. verts[2].point.set(upperLeft.x - ne.x + ulOffset, lowerRight.y - ne.y + ulOffset, 0.0f);
  588. verts[3].point.set(lowerRight.x - nw.x + ulOffset, lowerRight.y - nw.y + ulOffset, 0.0f);
  589. for (S32 i = 0; i < 4; i++)
  590. verts[i].color = color;
  591. verts.unlock();
  592. mDevice->setVertexBuffer(verts);
  593. mDevice->setStateBlock(mRectFillSB);
  594. Point2F topLeftCorner(upperLeft.x + nw.x + ulOffset, upperLeft.y + nw.y + ulOffset);
  595. Point2F bottomRightCorner(lowerRight.x - nw.x + ulOffset, lowerRight.y - nw.y + ulOffset);
  596. /*mDevice->setupGenericShaders();*/
  597. GFX->setShader(mCircleShader);
  598. GFX->setShaderConstBuffer(mCircleShaderConsts);
  599. MatrixF tempMatrix = GFX->getProjectionMatrix() * GFX->getViewMatrix() * GFX->getWorldMatrix();
  600. Point2F size((F32)(bottomRightCorner.x - topLeftCorner.x), (F32)(bottomRightCorner.y - topLeftCorner.y));
  601. Point2F rectCenter((F32)(topLeftCorner.x + (size.x / 2.0)), (F32)(topLeftCorner.y + (size.y / 2.0)));
  602. mCircleShaderConsts->setSafe(mCircleShader->getShaderConstHandle("$rectCenter"), rectCenter);
  603. F32 minExtent = mMin(size.x, size.y);
  604. F32 shaderRadius = radius;
  605. if ((minExtent * 0.5) < shaderRadius)
  606. {
  607. shaderRadius = mClampF(radius, 0.0f, (minExtent * 0.5));
  608. }
  609. mCircleShaderConsts->set(mCircleShader->getShaderConstHandle("$modelView"), tempMatrix, GFXSCT_Float4x4);
  610. mCircleShaderConsts->setSafe(mCircleShader->getShaderConstHandle("$radius"), shaderRadius);
  611. mCircleShaderConsts->setSafe(mCircleShader->getShaderConstHandle("$sizeUni"), size);
  612. mCircleShaderConsts->setSafe(mCircleShader->getShaderConstHandle("$borderSize"), borderSize);
  613. mCircleShaderConsts->setSafe(mCircleShader->getShaderConstHandle("$borderCol"), borderColor);
  614. mDevice->drawPrimitive(GFXTriangleStrip, 0, 2);
  615. }
  616. //-----------------------------------------------------------------------------
  617. // Draw Lines : Single Pixel
  618. //-----------------------------------------------------------------------------
  619. void GFXDrawUtil::drawLine( const Point3F &startPt, const Point3F &endPt, const ColorI &color )
  620. {
  621. drawLine( startPt.x, startPt.y, startPt.z, endPt.x, endPt.y, endPt.z, color );
  622. }
  623. void GFXDrawUtil::drawLine( const Point2F &startPt, const Point2F &endPt, const ColorI &color )
  624. {
  625. drawLine( startPt.x, startPt.y, 0.0f, endPt.x, endPt.y, 0.0f, color );
  626. }
  627. void GFXDrawUtil::drawLine( const Point2I &startPt, const Point2I &endPt, const ColorI &color )
  628. {
  629. drawLine( startPt.x, startPt.y, 0.0f, endPt.x, endPt.y, 0.0f, color );
  630. }
  631. void GFXDrawUtil::drawLine( F32 x1, F32 y1, F32 x2, F32 y2, const ColorI &color )
  632. {
  633. drawLine( x1, y1, 0.0f, x2, y2, 0.0f, color );
  634. }
  635. void GFXDrawUtil::drawLine( F32 x1, F32 y1, F32 z1, F32 x2, F32 y2, F32 z2, const ColorI &color )
  636. {
  637. GFXVertexBufferHandle<GFXVertexPCT> verts( mDevice, 2, GFXBufferTypeVolatile );
  638. verts.lock();
  639. verts[0].point.set( x1, y1, z1 );
  640. verts[1].point.set( x2, y2, z2 );
  641. verts[0].color = color;
  642. verts[1].color = color;
  643. verts.unlock();
  644. mDevice->setVertexBuffer( verts );
  645. mDevice->setStateBlock( mRectFillSB );
  646. mDevice->setupGenericShaders();
  647. mDevice->drawPrimitive( GFXLineList, 0, 1 );
  648. }
  649. //-----------------------------------------------------------------------------
  650. // Draw Lines : Thick
  651. //-----------------------------------------------------------------------------
  652. void GFXDrawUtil::drawThickLine(const Point2I& startPt, const Point2I& endPt, const ColorI& color, const F32& thickness)
  653. {
  654. drawThickLine(startPt.x, startPt.y, 0.0f, endPt.x, endPt.y, 0.0f, color, thickness);
  655. }
  656. void GFXDrawUtil::drawThickLine(const Point2F& startPt, const Point2F& endPt, const ColorI& color, const F32& thickness)
  657. {
  658. drawThickLine(startPt.x, startPt.y, 0.0f, endPt.x, endPt.y, 0.0f, color, thickness);
  659. }
  660. void GFXDrawUtil::drawThickLine(F32 x1, F32 y1, F32 z1, F32 x2, F32 y2, F32 z2, const ColorI& color, const F32& thickness)
  661. {
  662. // less than 2 just draw an ordinary line... why you ever here....
  663. if (thickness < 2.0f)
  664. {
  665. drawLine(x1, y1, z1, x2, y2, z2, color);
  666. return;
  667. }
  668. GFXVertexBufferHandle<GFXVertexPCT> verts(mDevice, 2, GFXBufferTypeVolatile);
  669. verts.lock();
  670. verts[0].point.set(x1, y1, z1);
  671. verts[1].point.set(x2, y2, z2);
  672. verts[0].color = color;
  673. verts[1].color = color;
  674. verts.unlock();
  675. mDevice->setVertexBuffer(verts);
  676. mDevice->setStateBlock(mRectFillSB);
  677. GFX->setShader(mThickLineShader);
  678. GFX->setShaderConstBuffer(mThickLineShaderConsts);
  679. MatrixF tempMatrix = GFX->getProjectionMatrix() * GFX->getViewMatrix() * GFX->getWorldMatrix();
  680. mThickLineShaderConsts->set(mThickLineShader->getShaderConstHandle("$modelView"), tempMatrix, GFXSCT_Float4x4);
  681. mThickLineShaderConsts->setSafe(mThickLineShader->getShaderConstHandle("$thickness"), thickness);
  682. const Point2I& resolution = GFX->getActiveRenderTarget()->getSize();
  683. Point2F TargetSize(1.0 / (F32)resolution.x, 1.0 / (F32)resolution.y);
  684. mThickLineShaderConsts->setSafe(mThickLineShader->getShaderConstHandle("$oneOverViewport"), TargetSize);
  685. mDevice->drawPrimitive(GFXLineList, 0, 1);
  686. }
  687. //-----------------------------------------------------------------------------
  688. // 3D World Draw Misc
  689. //-----------------------------------------------------------------------------
  690. static SphereMesh gSphere;
  691. void GFXDrawUtil::drawSphere( const GFXStateBlockDesc &desc, F32 radius, const Point3F &pos, const ColorI &color, bool drawTop, bool drawBottom, const MatrixF *xfm )
  692. {
  693. MatrixF mat;
  694. if ( xfm )
  695. mat = *xfm;
  696. else
  697. mat = MatrixF::Identity;
  698. mat.scale(Point3F(radius,radius,radius));
  699. mat.setPosition(pos);
  700. GFX->pushWorldMatrix();
  701. GFX->multWorld(mat);
  702. const SphereMesh::TriangleMesh * sphereMesh = gSphere.getMesh(2);
  703. S32 numPoly = sphereMesh->numPoly;
  704. S32 totalPoly = 0;
  705. GFXVertexBufferHandle<GFXVertexPCT> verts(mDevice, numPoly*3, GFXBufferTypeVolatile);
  706. verts.lock();
  707. S32 vertexIndex = 0;
  708. for (S32 i=0; i<numPoly; i++)
  709. {
  710. if (!drawBottom)
  711. {
  712. 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)
  713. continue;
  714. }
  715. if (!drawTop)
  716. {
  717. 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)
  718. continue;
  719. }
  720. totalPoly++;
  721. verts[vertexIndex].point = sphereMesh->poly[i].pnt[0];
  722. verts[vertexIndex].color = color;
  723. vertexIndex++;
  724. verts[vertexIndex].point = sphereMesh->poly[i].pnt[1];
  725. verts[vertexIndex].color = color;
  726. vertexIndex++;
  727. verts[vertexIndex].point = sphereMesh->poly[i].pnt[2];
  728. verts[vertexIndex].color = color;
  729. vertexIndex++;
  730. }
  731. verts.unlock();
  732. mDevice->setStateBlockByDesc( desc );
  733. mDevice->setVertexBuffer( verts );
  734. mDevice->setupGenericShaders();
  735. mDevice->drawPrimitive( GFXTriangleList, 0, totalPoly );
  736. GFX->popWorldMatrix();
  737. }
  738. //-----------------------------------------------------------------------------
  739. static const Point3F cubePoints[8] =
  740. {
  741. Point3F(-1, -1, -1), Point3F(-1, -1, 1), Point3F(-1, 1, -1), Point3F(-1, 1, 1),
  742. Point3F( 1, -1, -1), Point3F( 1, -1, 1), Point3F( 1, 1, -1), Point3F( 1, 1, 1)
  743. };
  744. static const U32 cubeFaces[6][4] =
  745. {
  746. { 0, 4, 6, 2 }, { 0, 2, 3, 1 }, { 0, 1, 5, 4 },
  747. { 3, 2, 6, 7 }, { 7, 6, 4, 5 }, { 3, 7, 5, 1 }
  748. };
  749. void GFXDrawUtil::drawTriangle( const GFXStateBlockDesc &desc, const Point3F &p0, const Point3F &p1, const Point3F &p2, const ColorI &color, const MatrixF *xfm )
  750. {
  751. if ( desc.fillMode == GFXFillWireframe )
  752. _drawWireTriangle( desc, p0, p1, p2, color, xfm );
  753. else
  754. _drawSolidTriangle( desc, p0, p1, p2, color, xfm );
  755. }
  756. void GFXDrawUtil::_drawWireTriangle( const GFXStateBlockDesc &desc, const Point3F &p0, const Point3F &p1, const Point3F &p2, const ColorI &color, const MatrixF *xfm )
  757. {
  758. GFXVertexBufferHandle<GFXVertexPCT> verts(mDevice, 4, GFXBufferTypeVolatile);
  759. verts.lock();
  760. // Set up the line strip
  761. verts[0].point = p0;
  762. verts[0].color = color;
  763. verts[1].point = p1;
  764. verts[1].color = color;
  765. verts[2].point = p2;
  766. verts[2].color = color;
  767. verts[3].point = p0;
  768. verts[3].color = color;
  769. // Apply xfm if we were passed one.
  770. if ( xfm != NULL )
  771. {
  772. for ( U32 i = 0; i < 4; i++ )
  773. xfm->mulP( verts[i].point );
  774. }
  775. verts.unlock();
  776. GFXStateBlockRef sb = mDevice->createStateBlock( desc );
  777. mDevice->setStateBlock( sb );
  778. mDevice->setVertexBuffer( verts );
  779. mDevice->setupGenericShaders();
  780. mDevice->drawPrimitive( GFXLineStrip, 0, 3 );
  781. }
  782. void GFXDrawUtil::_drawSolidTriangle( const GFXStateBlockDesc &desc, const Point3F &p0, const Point3F &p1, const Point3F &p2, const ColorI &color, const MatrixF *xfm )
  783. {
  784. GFXVertexBufferHandle<GFXVertexPCT> verts(mDevice, 3, GFXBufferTypeVolatile);
  785. verts.lock();
  786. // Set up the line strip
  787. verts[0].point = p0;
  788. verts[0].color = color;
  789. verts[1].point = p1;
  790. verts[1].color = color;
  791. verts[2].point = p2;
  792. verts[2].color = color;
  793. // Apply xfm if we were passed one.
  794. if ( xfm != NULL )
  795. {
  796. for ( U32 i = 0; i < 3; i++ )
  797. xfm->mulP( verts[i].point );
  798. }
  799. verts.unlock();
  800. GFXStateBlockRef sb = mDevice->createStateBlock( desc );
  801. mDevice->setStateBlock( sb );
  802. mDevice->setVertexBuffer( verts );
  803. mDevice->setupGenericShaders();
  804. mDevice->drawPrimitive( GFXTriangleList, 0, 1 );
  805. }
  806. void GFXDrawUtil::drawPolygon( const GFXStateBlockDesc& desc, const Point3F* points, U32 numPoints, const ColorI& color, const MatrixF* xfm /* = NULL */ )
  807. {
  808. const bool isWireframe = ( desc.fillMode == GFXFillWireframe );
  809. const U32 numVerts = isWireframe ? numPoints + 1 : numPoints;
  810. GFXVertexBufferHandle< GFXVertexPCT > verts( mDevice, numVerts, GFXBufferTypeVolatile );
  811. verts.lock();
  812. for( U32 i = 0; i < numPoints; ++ i )
  813. {
  814. verts[ i ].point = points[ i ];
  815. verts[ i ].color = color;
  816. }
  817. if( xfm )
  818. {
  819. for( U32 i = 0; i < numPoints; ++ i )
  820. xfm->mulP( verts[ i ].point );
  821. }
  822. if( isWireframe )
  823. {
  824. verts[ numVerts - 1 ].point = verts[ 0 ].point;
  825. verts[ numVerts - 1 ].color = color;
  826. }
  827. verts.unlock();
  828. mDevice->setStateBlockByDesc( desc );
  829. mDevice->setVertexBuffer( verts );
  830. mDevice->setupGenericShaders();
  831. if( desc.fillMode == GFXFillWireframe )
  832. mDevice->drawPrimitive( GFXLineStrip, 0, numPoints );
  833. else
  834. mDevice->drawPrimitive( GFXTriangleStrip, 0, numPoints - 2 );
  835. }
  836. void GFXDrawUtil::drawCube( const GFXStateBlockDesc &desc, const Box3F &box, const ColorI &color, const MatrixF *xfm )
  837. {
  838. drawCube( desc, box.getExtents(), box.getCenter(), color, xfm );
  839. }
  840. void GFXDrawUtil::drawCube( const GFXStateBlockDesc &desc, const Point3F &size, const Point3F &pos, const ColorI &color, const MatrixF *xfm )
  841. {
  842. if ( desc.fillMode == GFXFillWireframe )
  843. _drawWireCube( desc, size, pos, color, xfm );
  844. else
  845. _drawSolidCube( desc, size, pos, color, xfm );
  846. }
  847. void GFXDrawUtil::_drawWireCube( const GFXStateBlockDesc &desc, const Point3F &size, const Point3F &pos, const ColorI &color, const MatrixF *xfm )
  848. {
  849. GFXVertexBufferHandle<GFXVertexPCT> verts(mDevice, 30, GFXBufferTypeVolatile);
  850. verts.lock();
  851. Point3F halfSize = size * 0.5f;
  852. // setup 6 line loops
  853. U32 vertexIndex = 0;
  854. for(S32 i = 0; i < 6; i++)
  855. {
  856. for(S32 j = 0; j < 5; j++)
  857. {
  858. S32 idx = cubeFaces[i][j%4];
  859. verts[vertexIndex].point = cubePoints[idx] * halfSize;
  860. verts[vertexIndex].color = color;
  861. vertexIndex++;
  862. }
  863. }
  864. // Apply xfm if we were passed one.
  865. if ( xfm != NULL )
  866. {
  867. for ( U32 i = 0; i < 30; i++ )
  868. xfm->mulV( verts[i].point );
  869. }
  870. // Apply position offset
  871. for ( U32 i = 0; i < 30; i++ )
  872. verts[i].point += pos;
  873. verts.unlock();
  874. mDevice->setStateBlockByDesc( desc );
  875. mDevice->setVertexBuffer( verts );
  876. mDevice->setupGenericShaders();
  877. for( U32 i=0; i<6; i++ )
  878. mDevice->drawPrimitive( GFXLineStrip, i*5, 4 );
  879. }
  880. void GFXDrawUtil::_drawSolidCube( const GFXStateBlockDesc &desc, const Point3F &size, const Point3F &pos, const ColorI &color, const MatrixF *xfm )
  881. {
  882. GFXVertexBufferHandle<GFXVertexPCT> verts(mDevice, 36, GFXBufferTypeVolatile);
  883. verts.lock();
  884. Point3F halfSize = size * 0.5f;
  885. // setup 6 line loops
  886. U32 vertexIndex = 0;
  887. U32 idx;
  888. for(S32 i = 0; i < 6; i++)
  889. {
  890. idx = cubeFaces[i][0];
  891. verts[vertexIndex].point = cubePoints[idx] * halfSize;
  892. verts[vertexIndex].color = color;
  893. vertexIndex++;
  894. idx = cubeFaces[i][1];
  895. verts[vertexIndex].point = cubePoints[idx] * halfSize;
  896. verts[vertexIndex].color = color;
  897. vertexIndex++;
  898. idx = cubeFaces[i][3];
  899. verts[vertexIndex].point = cubePoints[idx] * halfSize;
  900. verts[vertexIndex].color = color;
  901. vertexIndex++;
  902. idx = cubeFaces[i][1];
  903. verts[vertexIndex].point = cubePoints[idx] * halfSize;
  904. verts[vertexIndex].color = color;
  905. vertexIndex++;
  906. idx = cubeFaces[i][2];
  907. verts[vertexIndex].point = cubePoints[idx] * halfSize;
  908. verts[vertexIndex].color = color;
  909. vertexIndex++;
  910. idx = cubeFaces[i][3];
  911. verts[vertexIndex].point = cubePoints[idx] * halfSize;
  912. verts[vertexIndex].color = color;
  913. vertexIndex++;
  914. }
  915. // Apply xfm if we were passed one.
  916. if ( xfm != NULL )
  917. {
  918. for ( U32 i = 0; i < 36; i++ )
  919. xfm->mulV( verts[i].point );
  920. }
  921. // Apply position offset
  922. for ( U32 i = 0; i < 36; i++ )
  923. verts[i].point += pos;
  924. verts.unlock();
  925. mDevice->setStateBlockByDesc( desc );
  926. mDevice->setVertexBuffer( verts );
  927. mDevice->setupGenericShaders();
  928. mDevice->drawPrimitive( GFXTriangleList, 0, 12 );
  929. }
  930. void GFXDrawUtil::drawPolyhedron( const GFXStateBlockDesc &desc, const AnyPolyhedron &poly, const ColorI &color, const MatrixF *xfm )
  931. {
  932. if ( desc.fillMode == GFXFillWireframe )
  933. _drawWirePolyhedron( desc, poly, color, xfm );
  934. else
  935. _drawSolidPolyhedron( desc, poly, color, xfm );
  936. }
  937. void GFXDrawUtil::_drawWirePolyhedron( const GFXStateBlockDesc &desc, const AnyPolyhedron &poly, const ColorI &color, const MatrixF *xfm )
  938. {
  939. GFXDEBUGEVENT_SCOPE( GFXDrawUtil_DrawWirePolyhedron, ColorI::GREEN );
  940. const U32 numEdges = poly.getNumEdges();
  941. const Point3F* points = poly.getPoints();
  942. const Polyhedron::Edge* edges = poly.getEdges();
  943. // Allocate a temporary vertex buffer.
  944. GFXVertexBufferHandle< GFXVertexPCT > verts( mDevice, numEdges * 2, GFXBufferTypeVolatile);
  945. // Fill it with the vertices for the edges.
  946. verts.lock();
  947. for( U32 i = 0; i < numEdges; ++ i )
  948. {
  949. const U32 nvert = i * 2;
  950. verts[ nvert + 0 ].point = points[ edges[ i ].vertex[ 0 ] ];
  951. verts[ nvert + 0 ].color = color;
  952. verts[ nvert + 1 ].point = points[ edges[ i ].vertex[ 1 ] ];
  953. verts[ nvert + 1 ].color = color;
  954. }
  955. if( xfm )
  956. {
  957. for( U32 i = 0; i < numEdges; ++ i )
  958. {
  959. xfm->mulP( verts[ i + 0 ].point );
  960. xfm->mulP( verts[ i + 1 ].point );
  961. }
  962. }
  963. verts.unlock();
  964. // Render the line list.
  965. mDevice->setStateBlockByDesc( desc );
  966. mDevice->setVertexBuffer( verts );
  967. mDevice->setupGenericShaders();
  968. mDevice->drawPrimitive( GFXLineList, 0, numEdges );
  969. }
  970. void GFXDrawUtil::_drawSolidPolyhedron( const GFXStateBlockDesc &desc, const AnyPolyhedron &poly, const ColorI &color, const MatrixF *xfm )
  971. {
  972. GFXDEBUGEVENT_SCOPE( GFXDrawUtil_DrawSolidPolyhedron, ColorI::GREEN );
  973. const U32 numPoints = poly.getNumPoints();
  974. const Point3F* points = poly.getPoints();
  975. const PlaneF* planes = poly.getPlanes();
  976. const Point3F viewDir = GFX->getViewMatrix().getForwardVector();
  977. // Create a temp buffer for the vertices and
  978. // put all the polyhedron's points in there.
  979. GFXVertexBufferHandle< GFXVertexPCT > verts( mDevice, numPoints, GFXBufferTypeVolatile );
  980. verts.lock();
  981. for( U32 i = 0; i < numPoints; ++ i )
  982. {
  983. verts[ i ].point = points[ i ];
  984. verts[ i ].color = color;
  985. }
  986. if( xfm )
  987. {
  988. for( U32 i = 0; i < numPoints; ++ i )
  989. xfm->mulP( verts[ i ].point );
  990. }
  991. verts.unlock();
  992. // Allocate a temp buffer for the face indices.
  993. const U32 numIndices = poly.getNumEdges() * 3;
  994. const U32 numPlanes = poly.getNumPlanes();
  995. GFXPrimitiveBufferHandle prims( mDevice, numIndices, 0, GFXBufferTypeVolatile );
  996. // Unfortunately, since polygons may have varying numbers of
  997. // vertices, we also need to retain that information.
  998. FrameTemp< U32 > numIndicesForPoly( numPlanes );
  999. U32 numPolys = 0;
  1000. // Create all the polygon indices.
  1001. U16* indices;
  1002. prims.lock( &indices );
  1003. U32 idx = 0;
  1004. for( U32 i = 0; i < numPlanes; ++ i )
  1005. {
  1006. // Since face extraction is somewhat costly, don't bother doing it for
  1007. // backfacing polygons if culling is enabled.
  1008. if( !desc.cullDefined || desc.cullMode != GFXCullNone )
  1009. {
  1010. F32 dot = mDot( planes[ i ], viewDir );
  1011. // See if it faces *the same way* as the view direction. This would
  1012. // normally mean that the face is *not* backfacing but since we expect
  1013. // planes on the polyhedron to be facing *inwards*, we need to reverse
  1014. // the logic here.
  1015. if( dot > 0.f )
  1016. continue;
  1017. }
  1018. U32 polyIDx = poly.extractFace( i, &indices[ idx ], numIndices - idx );
  1019. numIndicesForPoly[ numPolys ] = polyIDx;
  1020. idx += polyIDx;
  1021. numPolys ++;
  1022. }
  1023. prims.unlock();
  1024. // Set up state.
  1025. mDevice->setStateBlockByDesc( desc );
  1026. mDevice->setupGenericShaders();
  1027. mDevice->setVertexBuffer( verts );
  1028. mDevice->setPrimitiveBuffer( prims );
  1029. // Render one triangle fan for each polygon.
  1030. U32 startIndex = 0;
  1031. for( U32 i = 0; i < numPolys; ++ i )
  1032. {
  1033. U32 numVerts = numIndicesForPoly[ i ];
  1034. mDevice->drawIndexedPrimitive( GFXTriangleStrip, 0, 0, numPoints, startIndex, numVerts - 2 );
  1035. startIndex += numVerts;
  1036. }
  1037. }
  1038. void GFXDrawUtil::drawObjectBox( const GFXStateBlockDesc &desc, const Point3F &size, const Point3F &pos, const MatrixF &objMat, const ColorI &color )
  1039. {
  1040. GFXTransformSaver saver;
  1041. mDevice->setStateBlockByDesc( desc );
  1042. MatrixF scaledObjMat( true );
  1043. scaledObjMat = objMat;
  1044. scaledObjMat.scale( size );
  1045. scaledObjMat.setPosition( pos );
  1046. //to linear is done in primbuilder
  1047. PrimBuild::color( color );
  1048. PrimBuild::begin( GFXLineList, 48 );
  1049. Point3F cubePts[8];
  1050. for (U32 i = 0; i < 8; i++)
  1051. {
  1052. cubePts[i] = cubePoints[i]/2;
  1053. }
  1054. // 8 corner points of the box
  1055. for ( U32 i = 0; i < 8; i++ )
  1056. {
  1057. //const Point3F &start = cubePoints[i];
  1058. // 3 lines per corner point
  1059. for ( U32 j = 0; j < 3; j++ )
  1060. {
  1061. Point3F start = cubePoints[i];
  1062. Point3F end = start;
  1063. end[j] *= 0.8f;
  1064. scaledObjMat.mulP(start);
  1065. PrimBuild::vertex3fv(start);
  1066. scaledObjMat.mulP(end);
  1067. PrimBuild::vertex3fv(end);
  1068. }
  1069. }
  1070. PrimBuild::end();
  1071. }
  1072. static const Point2F circlePoints[] =
  1073. {
  1074. Point2F(0.707107f, 0.707107f),
  1075. Point2F(0.923880f, 0.382683f),
  1076. Point2F(1.000000f, 0.000000f),
  1077. Point2F(0.923880f, -0.382684f),
  1078. Point2F(0.707107f, -0.707107f),
  1079. Point2F(0.382683f, -0.923880f),
  1080. Point2F(0.000000f, -1.000000f),
  1081. Point2F(-0.382683f, -0.923880f),
  1082. Point2F(-0.707107f, -0.707107f),
  1083. Point2F(-0.923880f, -0.382684f),
  1084. Point2F(-1.000000f, 0.000000f),
  1085. Point2F(-0.923879f, 0.382684f),
  1086. Point2F(-0.707107f, 0.707107f),
  1087. Point2F(-0.382683f, 0.923880f),
  1088. Point2F(0.000000f, 1.000000f),
  1089. Point2F(0.382684f, 0.923879f)
  1090. };
  1091. void GFXDrawUtil::drawCapsule( const GFXStateBlockDesc &desc, const Point3F &center, F32 radius, F32 height, const ColorI &color, const MatrixF *xfm )
  1092. {
  1093. if ( desc.fillMode == GFXFillWireframe )
  1094. _drawWireCapsule( desc, center, radius, height, color, xfm );
  1095. else
  1096. _drawSolidCapsule( desc, center, radius, height, color, xfm );
  1097. }
  1098. void GFXDrawUtil::_drawSolidCapsule( const GFXStateBlockDesc &desc, const Point3F &center, F32 radius, F32 height, const ColorI &color, const MatrixF *xfm )
  1099. {
  1100. MatrixF mat;
  1101. if ( xfm )
  1102. mat = *xfm;
  1103. else
  1104. mat = MatrixF::Identity;
  1105. S32 numPoints = sizeof(circlePoints)/sizeof(Point2F);
  1106. GFXVertexBufferHandle<GFXVertexPCT> verts(mDevice, numPoints * 2 + 2, GFXBufferTypeVolatile);
  1107. verts.lock();
  1108. for (S32 i=0; i<numPoints + 1; i++)
  1109. {
  1110. S32 imod = i % numPoints;
  1111. verts[2 * i].point = Point3F( circlePoints[imod].x * radius, circlePoints[imod].y * radius, height/2 );
  1112. verts[2 * i].color = color;
  1113. verts[2 * i + 1].point = Point3F( circlePoints[imod].x * radius, circlePoints[imod].y * radius, -height/2 );
  1114. verts[2 * i + 1].color = color;
  1115. }
  1116. S32 totalNumPnts = numPoints * 2 + 2;
  1117. // Apply xfm if we were passed one.
  1118. for ( U32 i = 0; i < totalNumPnts; i++ )
  1119. mat.mulV( verts[i].point );
  1120. // Apply position offset
  1121. for ( U32 i = 0; i < totalNumPnts; i++ )
  1122. verts[i].point += center;
  1123. verts.unlock();
  1124. mDevice->setStateBlockByDesc( desc );
  1125. mDevice->setVertexBuffer( verts );
  1126. mDevice->setupGenericShaders();
  1127. mDevice->drawPrimitive( GFXTriangleStrip, 0, 2 * numPoints );
  1128. Point3F sphereCenter;
  1129. MatrixF sphereMat;
  1130. if ( xfm )
  1131. sphereMat = *xfm;
  1132. else
  1133. sphereMat = MatrixF::Identity;
  1134. sphereCenter.set( 0, 0, 0.5f * height );
  1135. mat.mulV( sphereCenter );
  1136. sphereCenter += center;
  1137. drawSphere( desc, radius, sphereCenter, color, true, false, &sphereMat );
  1138. sphereCenter.set( 0, 0, -0.5f * height );
  1139. mat.mulV( sphereCenter );
  1140. sphereCenter += center;
  1141. drawSphere( desc, radius, sphereCenter, color, false, true, &sphereMat );
  1142. }
  1143. void GFXDrawUtil::_drawWireCapsule( const GFXStateBlockDesc &desc, const Point3F &center, F32 radius, F32 height, const ColorI &color, const MatrixF *xfm )
  1144. {
  1145. MatrixF mat;
  1146. if (xfm)
  1147. mat = *xfm;
  1148. else
  1149. mat = MatrixF::Identity;
  1150. S32 numPoints = sizeof(circlePoints) / sizeof(Point2F);
  1151. GFXVertexBufferHandle<GFXVertexPCT> verts(mDevice, numPoints * 2 + 2, GFXBufferTypeVolatile);
  1152. verts.lock();
  1153. for (S32 i = 0; i < numPoints + 1; i++)
  1154. {
  1155. S32 imod = i % numPoints;
  1156. verts[2 * i].point = Point3F(circlePoints[imod].x * radius, circlePoints[imod].y * radius, height / 2);
  1157. verts[2 * i].color = color;
  1158. verts[2 * i + 1].point = Point3F(circlePoints[imod].x * radius, circlePoints[imod].y * radius, -height / 2);
  1159. verts[2 * i + 1].color = color;
  1160. }
  1161. S32 totalNumPnts = numPoints * 2 + 2;
  1162. // Apply xfm if we were passed one.
  1163. for (U32 i = 0; i < totalNumPnts; i++)
  1164. mat.mulV(verts[i].point);
  1165. // Apply position offset
  1166. for (U32 i = 0; i < totalNumPnts; i++)
  1167. verts[i].point += center;
  1168. verts.unlock();
  1169. mDevice->setStateBlockByDesc(desc);
  1170. mDevice->setVertexBuffer(verts);
  1171. mDevice->setupGenericShaders();
  1172. mDevice->drawPrimitive(GFXTriangleStrip, 0, 2 * numPoints);
  1173. Point3F sphereCenter;
  1174. MatrixF sphereMat;
  1175. if (xfm)
  1176. sphereMat = *xfm;
  1177. else
  1178. sphereMat = MatrixF::Identity;
  1179. sphereCenter.set(0, 0, 0.5f * height);
  1180. mat.mulV(sphereCenter);
  1181. sphereCenter += center;
  1182. drawSphere(desc, radius, sphereCenter, color, true, false, &sphereMat);
  1183. sphereCenter.set(0, 0, -0.5f * height);
  1184. mat.mulV(sphereCenter);
  1185. sphereCenter += center;
  1186. drawSphere(desc, radius, sphereCenter, color, false, true, &sphereMat);
  1187. }
  1188. void GFXDrawUtil::drawCone( const GFXStateBlockDesc &desc, const Point3F &basePnt, const Point3F &tipPnt, F32 baseRadius, const ColorI &color )
  1189. {
  1190. VectorF dir = tipPnt - basePnt;
  1191. F32 height = dir.len();
  1192. dir.normalize();
  1193. MatrixF mat(true);
  1194. MathUtils::getMatrixFromUpVector(dir, &mat);
  1195. mat.setPosition(basePnt);
  1196. mat.scale(Point3F(baseRadius, baseRadius, height));
  1197. GFXTransformSaver saver;
  1198. mDevice->pushWorldMatrix();
  1199. mDevice->multWorld(mat);
  1200. const S32 numPoints = sizeof(circlePoints) / sizeof(Point2F);
  1201. // Vertex index layout
  1202. const S32 baseCenterIdx = 0;
  1203. const S32 baseStartIdx = 1;
  1204. const S32 tipIdx = baseStartIdx + numPoints;
  1205. const S32 sideStartIdx = tipIdx + 1;
  1206. const S32 totalVerts = sideStartIdx + numPoints * 3;
  1207. GFXVertexBufferHandle<GFXVertexPCT> verts(mDevice, totalVerts, GFXBufferTypeVolatile);
  1208. verts.lock();
  1209. // Base center vertex (at origin in local space)
  1210. verts[baseCenterIdx].point = Point3F(0, 0, 0);
  1211. verts[baseCenterIdx].color = color;
  1212. // Base circle vertices
  1213. for (S32 i = 0; i < numPoints; i++)
  1214. {
  1215. verts[baseStartIdx + i].point = Point3F(circlePoints[i].x, circlePoints[i].y, 0);
  1216. verts[baseStartIdx + i].color = color;
  1217. }
  1218. // Tip vertex (pointing "up" in local Z)
  1219. verts[tipIdx].point = Point3F(0, 0, 1);
  1220. verts[tipIdx].color = color;
  1221. // Side triangles: one triangle per segment
  1222. for (S32 i = 0; i < numPoints; i++)
  1223. {
  1224. S32 triBase = sideStartIdx + i * 3;
  1225. // Each triangle is (tip, base[i], base[(i+1)%numPoints])
  1226. verts[triBase + 0].point = verts[tipIdx].point;
  1227. verts[triBase + 1].point = verts[baseStartIdx + i].point;
  1228. verts[triBase + 2].point = verts[baseStartIdx + ((i + 1) % numPoints)].point;
  1229. verts[triBase + 0].color = color;
  1230. verts[triBase + 1].color = color;
  1231. verts[triBase + 2].color = color;
  1232. }
  1233. verts.unlock();
  1234. mDevice->setStateBlockByDesc(desc);
  1235. mDevice->setVertexBuffer(verts);
  1236. mDevice->setupGenericShaders();
  1237. // Draw base cap using triangle fan
  1238. mDevice->drawPrimitive(GFXTriangleList, baseCenterIdx, numPoints - 2);
  1239. // Draw sides using triangle list
  1240. mDevice->drawPrimitive(GFXTriangleList, sideStartIdx, numPoints);
  1241. mDevice->popWorldMatrix();
  1242. }
  1243. void GFXDrawUtil::drawCylinder( const GFXStateBlockDesc &desc, const Point3F &basePnt, const Point3F &tipPnt, F32 radius, const ColorI &color )
  1244. {
  1245. VectorF dir = tipPnt - basePnt;
  1246. F32 height = dir.len();
  1247. dir.normalize();
  1248. MatrixF mat(true);
  1249. MathUtils::getMatrixFromUpVector(dir, &mat);
  1250. mat.setPosition(basePnt);
  1251. mat.scale(Point3F(radius, radius, height));
  1252. GFXTransformSaver saver;
  1253. mDevice->pushWorldMatrix();
  1254. mDevice->multWorld(mat);
  1255. const S32 numPoints = sizeof(circlePoints) / sizeof(Point2F);
  1256. // Vertex index layout
  1257. const S32 baseCenterIdx = 0;
  1258. const S32 topCenterIdx = 1;
  1259. const S32 baseStartIdx = 2;
  1260. const S32 topStartIdx = baseStartIdx + numPoints;
  1261. const S32 sideStartIdx = topStartIdx + numPoints;
  1262. const S32 totalVerts = sideStartIdx + numPoints * 6;
  1263. GFXVertexBufferHandle<GFXVertexPCT> verts(mDevice, totalVerts, GFXBufferTypeVolatile);
  1264. verts.lock();
  1265. // Base center
  1266. verts[baseCenterIdx].point = Point3F(0, 0, 0);
  1267. verts[baseCenterIdx].color = color;
  1268. // Top center
  1269. verts[topCenterIdx].point = Point3F(0, 0, 1);
  1270. verts[topCenterIdx].color = color;
  1271. // Base circle
  1272. for (S32 i = 0; i < numPoints; ++i)
  1273. {
  1274. verts[baseStartIdx + i].point = Point3F(circlePoints[i].x, circlePoints[i].y, 0);
  1275. verts[baseStartIdx + i].color = color;
  1276. }
  1277. // Top circle
  1278. for (S32 i = 0; i < numPoints; ++i)
  1279. {
  1280. verts[topStartIdx + i].point = Point3F(circlePoints[i].x, circlePoints[i].y, 1.0f);
  1281. verts[topStartIdx + i].color = color;
  1282. }
  1283. // Side triangles
  1284. for (S32 i = 0; i < numPoints; ++i)
  1285. {
  1286. S32 next = (i + 1) % numPoints;
  1287. S32 idx = sideStartIdx + i * 6;
  1288. // First triangle (base[i], base[next], top[i])
  1289. verts[idx + 0].point = verts[baseStartIdx + i].point;
  1290. verts[idx + 1].point = verts[baseStartIdx + next].point;
  1291. verts[idx + 2].point = verts[topStartIdx + i].point;
  1292. // Second triangle (top[i], base[next], top[next])
  1293. verts[idx + 3].point = verts[topStartIdx + i].point;
  1294. verts[idx + 4].point = verts[baseStartIdx + next].point;
  1295. verts[idx + 5].point = verts[topStartIdx + next].point;
  1296. for (int j = 0; j < 6; ++j)
  1297. verts[idx + j].color = color;
  1298. }
  1299. verts.unlock();
  1300. mDevice->setStateBlockByDesc(desc);
  1301. mDevice->setVertexBuffer(verts);
  1302. mDevice->setupGenericShaders();
  1303. // Draw base cap
  1304. mDevice->drawPrimitive(GFXTriangleList, baseCenterIdx, numPoints - 2);
  1305. // Draw top cap
  1306. mDevice->drawPrimitive(GFXTriangleList, topCenterIdx, numPoints - 2);
  1307. // Draw sides (2 triangles per segment)
  1308. mDevice->drawPrimitive(GFXTriangleList, sideStartIdx, numPoints * 2);
  1309. mDevice->popWorldMatrix();
  1310. }
  1311. void GFXDrawUtil::drawArrow( const GFXStateBlockDesc &desc, const Point3F &start, const Point3F &end, const ColorI &color, F32 baseRad )
  1312. {
  1313. GFXTransformSaver saver;
  1314. // Direction and length of the arrow.
  1315. VectorF dir = end - start;
  1316. F32 len = dir.len();
  1317. dir.normalize();
  1318. len *= 0.2f;
  1319. // Base of the cone will be a distance back from the end of the arrow
  1320. // proportional to the total distance of the arrow... 0.3f looks about right.
  1321. Point3F coneBase = end - dir * len * 0.3f;
  1322. // Calculate the radius of the cone given that we want the cone to have
  1323. // an angle of 25 degrees (just because it looks good).
  1324. F32 coneLen = (baseRad != 0.0f) ? baseRad * 4.0 :( end - coneBase ).len();
  1325. F32 coneDiameter = (baseRad != 0.0f) ? baseRad*4.0f : mTan( mDegToRad(25.0f) ) * coneLen;
  1326. // Draw the cone on at the arrow's tip.
  1327. drawCone( desc, coneBase, end, coneDiameter / 2.0f, color );
  1328. // Get the difference in length from
  1329. // the start of the cone to the end
  1330. // of the cylinder so we can put the
  1331. // end of the cylinder right against where
  1332. // the cone starts.
  1333. Point3F coneDiff = end - coneBase;
  1334. // Draw the cylinder.
  1335. F32 stickRadius = (baseRad != 0.0f) ? baseRad : len * 0.025f;
  1336. drawCylinder( desc, start, end - coneDiff, stickRadius, color );
  1337. }
  1338. void GFXDrawUtil::drawFrustum( const Frustum &f, const ColorI &color )
  1339. {
  1340. const Point3F *points = f.getPoints();
  1341. // Draw near and far planes.
  1342. for (U32 offset = 0; offset < 8; offset+=4)
  1343. {
  1344. drawLine(points[offset+0], points[offset+1], color);
  1345. drawLine(points[offset+2], points[offset+3], color);
  1346. drawLine(points[offset+0], points[offset+2], color);
  1347. drawLine(points[offset+1], points[offset+3], color);
  1348. }
  1349. // connect the near and far planes
  1350. drawLine(points[Frustum::NearTopLeft], points[Frustum::FarTopLeft], color);
  1351. drawLine(points[Frustum::NearTopRight], points[Frustum::FarTopRight], color);
  1352. drawLine(points[Frustum::NearBottomLeft], points[Frustum::FarBottomLeft], color);
  1353. drawLine(points[Frustum::NearBottomRight], points[Frustum::FarBottomRight], color);
  1354. }
  1355. void GFXDrawUtil::drawSolidPlane( const GFXStateBlockDesc &desc, const Point3F &pos, const Point2F &size, const ColorI &color )
  1356. {
  1357. GFXVertexBufferHandle<GFXVertexPCT> verts(mDevice, 4, GFXBufferTypeVolatile);
  1358. verts.lock();
  1359. verts[0].point = pos + Point3F( -size.x / 2.0f, -size.y / 2.0f, 0 );
  1360. verts[0].color = color;
  1361. verts[1].point = pos + Point3F( -size.x / 2.0f, size.y / 2.0f, 0 );
  1362. verts[1].color = color;
  1363. verts[2].point = pos + Point3F( size.x / 2.0f, size.y / 2.0f, 0 );
  1364. verts[2].color = color;
  1365. verts[3].point = pos + Point3F( size.x / 2.0f, -size.y / 2.0f, 0 );
  1366. verts[3].color = color;
  1367. verts.unlock();
  1368. mDevice->setStateBlockByDesc( desc );
  1369. mDevice->setVertexBuffer( verts );
  1370. mDevice->setupGenericShaders();
  1371. mDevice->drawPrimitive( GFXTriangleStrip, 0, 2 );
  1372. }
  1373. void GFXDrawUtil::drawPlaneGrid( const GFXStateBlockDesc &desc, const Point3F &pos, const Point2F &size, const Point2F &step, const ColorI &color, Plane plane )
  1374. {
  1375. // Note that when calculating the number of steps, we +0.5 to round up,
  1376. // and +1 for the last line (ie. 4 steps needs 5 lines to be rendered)
  1377. U32 uSteps = 0;
  1378. if( step.x > 0 )
  1379. uSteps = size.x / step.x + 0.5 + 1;
  1380. U32 vSteps = 0;
  1381. if( step.y > 0 )
  1382. vSteps = size.y / step.y + 0.5 + 1;
  1383. if( uSteps <= 1 || vSteps <= 1 )
  1384. return;
  1385. const U32 numVertices = uSteps * 2 + vSteps * 2;
  1386. const U32 numLines = uSteps + vSteps;
  1387. Point3F origin;
  1388. switch( plane )
  1389. {
  1390. case PlaneXY:
  1391. origin = Point3F( pos.x - ( size.x / 2.0f ), pos.y - ( size.y / 2.0f ), pos.z );
  1392. break;
  1393. case PlaneXZ:
  1394. origin = Point3F( pos.x - ( size.x / 2.0f ), pos.y, pos.z - ( size.y / 2.0f ) );
  1395. break;
  1396. case PlaneYZ:
  1397. origin = Point3F( pos.x, pos.y - ( size.x / 2.0f ), pos.z - ( size.y / 2.0f ) );
  1398. break;
  1399. }
  1400. GFXVertexBufferHandle<GFXVertexPCT> verts( mDevice, numVertices, GFXBufferTypeVolatile );
  1401. verts.lock();
  1402. U32 vertCount = 0;
  1403. if( plane == PlaneXY || plane == PlaneXZ )
  1404. {
  1405. F32 start = mFloor( origin.x / step.x + 0.5f ) * step.x;
  1406. for ( U32 i = 0; i < uSteps; i++ )
  1407. {
  1408. verts[vertCount].point = Point3F( start + step.x * i, origin.y, origin.z );
  1409. verts[vertCount].color = color;
  1410. ++vertCount;
  1411. if( plane == PlaneXY )
  1412. verts[vertCount].point = Point3F( start + step.x * i, origin.y + size.y, origin.z );
  1413. else
  1414. verts[vertCount].point = Point3F( start + step.x * i, origin.y, origin.z + size.y );
  1415. verts[vertCount].color = color;
  1416. ++vertCount;
  1417. }
  1418. }
  1419. if( plane == PlaneXY || plane == PlaneYZ )
  1420. {
  1421. U32 num;
  1422. F32 stp;
  1423. if( plane == PlaneXY )
  1424. {
  1425. num = vSteps;
  1426. stp = step.y;
  1427. }
  1428. else
  1429. {
  1430. num = uSteps;
  1431. stp = step.x;
  1432. }
  1433. F32 start = mFloor( origin.y / stp + 0.5f ) * stp;
  1434. for ( U32 i = 0; i < num; i++ )
  1435. {
  1436. verts[vertCount].point = Point3F( origin.x, start + stp * i, origin.z );
  1437. verts[vertCount].color = color;
  1438. ++vertCount;
  1439. if( plane == PlaneXY )
  1440. verts[vertCount].point = Point3F( origin.x + size.x, start + stp * i, origin.z );
  1441. else
  1442. verts[vertCount].point = Point3F( origin.x, start + stp * i, origin.z + size.x );
  1443. verts[vertCount].color = color;
  1444. ++vertCount;
  1445. }
  1446. }
  1447. if( plane == PlaneXZ || plane == PlaneYZ )
  1448. {
  1449. F32 start = mFloor( origin.z / step.y + 0.5f ) * step.y;
  1450. for ( U32 i = 0; i < vSteps; i++ )
  1451. {
  1452. verts[vertCount].point = Point3F( origin.x, origin.y, start + step.y * i );
  1453. verts[vertCount].color = color;
  1454. ++vertCount;
  1455. if( plane == PlaneXZ )
  1456. verts[vertCount].point = Point3F( origin.x + size.x, origin.y, start + step.y * i );
  1457. else
  1458. verts[vertCount].point = Point3F( origin.x, origin.y + size.x, start + step.y * i );
  1459. verts[vertCount].color = color;
  1460. ++vertCount;
  1461. }
  1462. }
  1463. verts.unlock();
  1464. mDevice->setStateBlockByDesc( desc );
  1465. mDevice->setVertexBuffer( verts );
  1466. mDevice->setupGenericShaders();
  1467. mDevice->drawPrimitive( GFXLineList, 0, numLines );
  1468. }
  1469. void GFXDrawUtil::drawTransform( const GFXStateBlockDesc &desc, const MatrixF &mat, const Point3F *scale, const ColorI colors[3] )
  1470. {
  1471. GFXTransformSaver saver;
  1472. GFX->multWorld( mat );
  1473. GFXVertexBufferHandle<GFXVertexPCT> verts( mDevice, 6, GFXBufferTypeVolatile );
  1474. verts.lock();
  1475. const static ColorI defColors[3] =
  1476. {
  1477. ColorI::RED,
  1478. ColorI::GREEN,
  1479. ColorI::BLUE
  1480. };
  1481. const ColorI *colArray = ( colors != NULL ) ? colors : defColors;
  1482. verts[0].point = Point3F::Zero;
  1483. verts[0].color = colArray[0];
  1484. verts[1].point = Point3F( 1, 0, 0 );
  1485. verts[1].color = colArray[0];
  1486. verts[2].point = Point3F::Zero;
  1487. verts[2].color = colArray[1];
  1488. verts[3].point = Point3F( 0, 1, 0 );
  1489. verts[3].color = colArray[1];
  1490. verts[4].point = Point3F::Zero;
  1491. verts[4].color = colArray[2];
  1492. verts[5].point = Point3F( 0, 0, 1 );
  1493. verts[5].color = colArray[2];
  1494. if ( scale )
  1495. {
  1496. verts[1].point *= *scale;
  1497. verts[3].point *= *scale;
  1498. verts[5].point *= *scale;
  1499. }
  1500. verts.unlock();
  1501. mDevice->setStateBlockByDesc( desc );
  1502. mDevice->setVertexBuffer( verts );
  1503. mDevice->setupGenericShaders();
  1504. mDevice->drawPrimitive( GFXLineList, 0, 3 );
  1505. }