gfxDrawUtil.cpp 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869
  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 uvec = tipPnt - basePnt;
  1191. F32 height = uvec.len();
  1192. uvec.normalize();
  1193. MatrixF mat( true );
  1194. MathUtils::getMatrixFromUpVector( uvec, &mat );
  1195. mat.setPosition(basePnt);
  1196. Point3F scale( baseRadius, baseRadius, height );
  1197. mat.scale(scale);
  1198. GFXTransformSaver saver;
  1199. mDevice->pushWorldMatrix();
  1200. mDevice->multWorld(mat);
  1201. S32 numPoints = sizeof(circlePoints)/sizeof(Point2F);
  1202. GFXVertexBufferHandle<GFXVertexPCT> verts(mDevice, numPoints * 3 + 2, GFXBufferTypeVolatile);
  1203. verts.lock();
  1204. F32 sign = -1.f;
  1205. S32 indexDown = 0; //counting down from numPoints
  1206. S32 indexUp = 0; //counting up from 0
  1207. S32 index = 0; //circlePoints index for cap
  1208. for (S32 i = 0; i < numPoints + 1; i++)
  1209. {
  1210. //Top cap
  1211. if (i != numPoints)
  1212. {
  1213. if (sign < 0)
  1214. index = indexDown;
  1215. else
  1216. index = indexUp;
  1217. verts[i].point = Point3F(circlePoints[index].x, circlePoints[index].y, 0);
  1218. verts[i].color = color;
  1219. if (sign < 0)
  1220. indexUp += 1;
  1221. else
  1222. indexDown = numPoints - indexUp;
  1223. // invert sign
  1224. sign *= -1.0f;
  1225. }
  1226. //cone
  1227. S32 imod = i % numPoints;
  1228. S32 vertindex = 2 * i + numPoints;
  1229. verts[vertindex].point = Point3F(circlePoints[imod].x, circlePoints[imod].y, 0);
  1230. verts[vertindex].color = color;
  1231. verts[vertindex + 1].point = Point3F(0.0f, 0.0f, 1.0f);
  1232. verts[vertindex + 1].color = color;
  1233. }
  1234. verts.unlock();
  1235. mDevice->setStateBlockByDesc( desc );
  1236. mDevice->setVertexBuffer( verts );
  1237. mDevice->setupGenericShaders();
  1238. mDevice->drawPrimitive(GFXTriangleStrip, 0, numPoints - 2);
  1239. mDevice->drawPrimitive(GFXTriangleStrip, numPoints, numPoints * 2);
  1240. mDevice->popWorldMatrix();
  1241. }
  1242. void GFXDrawUtil::drawCylinder( const GFXStateBlockDesc &desc, const Point3F &basePnt, const Point3F &tipPnt, F32 radius, const ColorI &color )
  1243. {
  1244. VectorF uvec = tipPnt - basePnt;
  1245. F32 height = uvec.len();
  1246. uvec.normalize();
  1247. MatrixF mat( true );
  1248. MathUtils::getMatrixFromUpVector( uvec, &mat );
  1249. mat.setPosition(basePnt);
  1250. Point3F scale( radius, radius, height * 2 );
  1251. mat.scale(scale);
  1252. GFXTransformSaver saver;
  1253. mDevice->pushWorldMatrix();
  1254. mDevice->multWorld(mat);
  1255. S32 numPoints = sizeof(circlePoints) / sizeof(Point2F);
  1256. GFXVertexBufferHandle<GFXVertexPCT> verts(mDevice, numPoints *4 + 2, GFXBufferTypeVolatile);
  1257. verts.lock();
  1258. F32 sign = -1.f;
  1259. S32 indexDown = 0; //counting down from numPoints
  1260. S32 indexUp = 0; //counting up from 0
  1261. S32 index = 0; //circlePoints index for caps
  1262. for (S32 i = 0; i < numPoints + 1; i++)
  1263. {
  1264. //Top/Bottom cap
  1265. if (i != numPoints)
  1266. {
  1267. if (sign < 0)
  1268. index = indexDown;
  1269. else
  1270. index = indexUp;
  1271. verts[i].point = Point3F(circlePoints[index].x, circlePoints[index].y, 0);
  1272. verts[i].color = color;
  1273. verts[i + numPoints].point = Point3F(circlePoints[index].x, circlePoints[index].y, 0.5f);
  1274. verts[i + numPoints].color = color;
  1275. if (sign < 0)
  1276. indexUp += 1;
  1277. else
  1278. indexDown = numPoints - indexUp;
  1279. // invert sign
  1280. sign *= -1.0f;
  1281. }
  1282. //cylinder
  1283. S32 imod = i % numPoints;
  1284. S32 vertindex = 2 * i + (numPoints * 2);
  1285. verts[vertindex].point = Point3F(circlePoints[imod].x, circlePoints[imod].y, 0);
  1286. verts[vertindex].color = color;
  1287. verts[vertindex + 1].point = Point3F(circlePoints[imod].x, circlePoints[imod].y, 0.5f);
  1288. verts[vertindex + 1].color = color;
  1289. }
  1290. verts.unlock();
  1291. mDevice->setStateBlockByDesc( desc );
  1292. mDevice->setVertexBuffer( verts );
  1293. mDevice->setupGenericShaders();
  1294. mDevice->drawPrimitive( GFXTriangleStrip, 0, numPoints-2 );
  1295. mDevice->drawPrimitive( GFXTriangleStrip, numPoints, numPoints - 2);
  1296. mDevice->drawPrimitive( GFXTriangleStrip, numPoints*2, numPoints * 2);
  1297. mDevice->popWorldMatrix();
  1298. }
  1299. void GFXDrawUtil::drawArrow( const GFXStateBlockDesc &desc, const Point3F &start, const Point3F &end, const ColorI &color, F32 baseRad )
  1300. {
  1301. GFXTransformSaver saver;
  1302. // Direction and length of the arrow.
  1303. VectorF dir = end - start;
  1304. F32 len = dir.len();
  1305. dir.normalize();
  1306. len *= 0.2f;
  1307. // Base of the cone will be a distance back from the end of the arrow
  1308. // proportional to the total distance of the arrow... 0.3f looks about right.
  1309. Point3F coneBase = end - dir * len * 0.3f;
  1310. // Calculate the radius of the cone given that we want the cone to have
  1311. // an angle of 25 degrees (just because it looks good).
  1312. F32 coneLen = (baseRad != 0.0f) ? baseRad * 4.0 :( end - coneBase ).len();
  1313. F32 coneDiameter = (baseRad != 0.0f) ? baseRad*4.0f : mTan( mDegToRad(25.0f) ) * coneLen;
  1314. // Draw the cone on at the arrow's tip.
  1315. drawCone( desc, coneBase, end, coneDiameter / 2.0f, color );
  1316. // Get the difference in length from
  1317. // the start of the cone to the end
  1318. // of the cylinder so we can put the
  1319. // end of the cylinder right against where
  1320. // the cone starts.
  1321. Point3F coneDiff = end - coneBase;
  1322. // Draw the cylinder.
  1323. F32 stickRadius = (baseRad != 0.0f) ? baseRad : len * 0.025f;
  1324. drawCylinder( desc, start, end - coneDiff, stickRadius, color );
  1325. }
  1326. void GFXDrawUtil::drawFrustum( const Frustum &f, const ColorI &color )
  1327. {
  1328. const Point3F *points = f.getPoints();
  1329. // Draw near and far planes.
  1330. for (U32 offset = 0; offset < 8; offset+=4)
  1331. {
  1332. drawLine(points[offset+0], points[offset+1], color);
  1333. drawLine(points[offset+2], points[offset+3], color);
  1334. drawLine(points[offset+0], points[offset+2], color);
  1335. drawLine(points[offset+1], points[offset+3], color);
  1336. }
  1337. // connect the near and far planes
  1338. drawLine(points[Frustum::NearTopLeft], points[Frustum::FarTopLeft], color);
  1339. drawLine(points[Frustum::NearTopRight], points[Frustum::FarTopRight], color);
  1340. drawLine(points[Frustum::NearBottomLeft], points[Frustum::FarBottomLeft], color);
  1341. drawLine(points[Frustum::NearBottomRight], points[Frustum::FarBottomRight], color);
  1342. }
  1343. void GFXDrawUtil::drawSolidPlane( const GFXStateBlockDesc &desc, const Point3F &pos, const Point2F &size, const ColorI &color )
  1344. {
  1345. GFXVertexBufferHandle<GFXVertexPCT> verts(mDevice, 4, GFXBufferTypeVolatile);
  1346. verts.lock();
  1347. verts[0].point = pos + Point3F( -size.x / 2.0f, -size.y / 2.0f, 0 );
  1348. verts[0].color = color;
  1349. verts[1].point = pos + Point3F( -size.x / 2.0f, size.y / 2.0f, 0 );
  1350. verts[1].color = color;
  1351. verts[2].point = pos + Point3F( size.x / 2.0f, size.y / 2.0f, 0 );
  1352. verts[2].color = color;
  1353. verts[3].point = pos + Point3F( size.x / 2.0f, -size.y / 2.0f, 0 );
  1354. verts[3].color = color;
  1355. verts.unlock();
  1356. mDevice->setStateBlockByDesc( desc );
  1357. mDevice->setVertexBuffer( verts );
  1358. mDevice->setupGenericShaders();
  1359. mDevice->drawPrimitive( GFXTriangleStrip, 0, 2 );
  1360. }
  1361. void GFXDrawUtil::drawPlaneGrid( const GFXStateBlockDesc &desc, const Point3F &pos, const Point2F &size, const Point2F &step, const ColorI &color, Plane plane )
  1362. {
  1363. // Note that when calculating the number of steps, we +0.5 to round up,
  1364. // and +1 for the last line (ie. 4 steps needs 5 lines to be rendered)
  1365. U32 uSteps = 0;
  1366. if( step.x > 0 )
  1367. uSteps = size.x / step.x + 0.5 + 1;
  1368. U32 vSteps = 0;
  1369. if( step.y > 0 )
  1370. vSteps = size.y / step.y + 0.5 + 1;
  1371. if( uSteps <= 1 || vSteps <= 1 )
  1372. return;
  1373. const U32 numVertices = uSteps * 2 + vSteps * 2;
  1374. const U32 numLines = uSteps + vSteps;
  1375. Point3F origin;
  1376. switch( plane )
  1377. {
  1378. case PlaneXY:
  1379. origin = Point3F( pos.x - ( size.x / 2.0f ), pos.y - ( size.y / 2.0f ), pos.z );
  1380. break;
  1381. case PlaneXZ:
  1382. origin = Point3F( pos.x - ( size.x / 2.0f ), pos.y, pos.z - ( size.y / 2.0f ) );
  1383. break;
  1384. case PlaneYZ:
  1385. origin = Point3F( pos.x, pos.y - ( size.x / 2.0f ), pos.z - ( size.y / 2.0f ) );
  1386. break;
  1387. }
  1388. GFXVertexBufferHandle<GFXVertexPCT> verts( mDevice, numVertices, GFXBufferTypeVolatile );
  1389. verts.lock();
  1390. U32 vertCount = 0;
  1391. if( plane == PlaneXY || plane == PlaneXZ )
  1392. {
  1393. F32 start = mFloor( origin.x / step.x + 0.5f ) * step.x;
  1394. for ( U32 i = 0; i < uSteps; i++ )
  1395. {
  1396. verts[vertCount].point = Point3F( start + step.x * i, origin.y, origin.z );
  1397. verts[vertCount].color = color;
  1398. ++vertCount;
  1399. if( plane == PlaneXY )
  1400. verts[vertCount].point = Point3F( start + step.x * i, origin.y + size.y, origin.z );
  1401. else
  1402. verts[vertCount].point = Point3F( start + step.x * i, origin.y, origin.z + size.y );
  1403. verts[vertCount].color = color;
  1404. ++vertCount;
  1405. }
  1406. }
  1407. if( plane == PlaneXY || plane == PlaneYZ )
  1408. {
  1409. U32 num;
  1410. F32 stp;
  1411. if( plane == PlaneXY )
  1412. {
  1413. num = vSteps;
  1414. stp = step.y;
  1415. }
  1416. else
  1417. {
  1418. num = uSteps;
  1419. stp = step.x;
  1420. }
  1421. F32 start = mFloor( origin.y / stp + 0.5f ) * stp;
  1422. for ( U32 i = 0; i < num; i++ )
  1423. {
  1424. verts[vertCount].point = Point3F( origin.x, start + stp * i, origin.z );
  1425. verts[vertCount].color = color;
  1426. ++vertCount;
  1427. if( plane == PlaneXY )
  1428. verts[vertCount].point = Point3F( origin.x + size.x, start + stp * i, origin.z );
  1429. else
  1430. verts[vertCount].point = Point3F( origin.x, start + stp * i, origin.z + size.x );
  1431. verts[vertCount].color = color;
  1432. ++vertCount;
  1433. }
  1434. }
  1435. if( plane == PlaneXZ || plane == PlaneYZ )
  1436. {
  1437. F32 start = mFloor( origin.z / step.y + 0.5f ) * step.y;
  1438. for ( U32 i = 0; i < vSteps; i++ )
  1439. {
  1440. verts[vertCount].point = Point3F( origin.x, origin.y, start + step.y * i );
  1441. verts[vertCount].color = color;
  1442. ++vertCount;
  1443. if( plane == PlaneXZ )
  1444. verts[vertCount].point = Point3F( origin.x + size.x, origin.y, start + step.y * i );
  1445. else
  1446. verts[vertCount].point = Point3F( origin.x, origin.y + size.x, start + step.y * i );
  1447. verts[vertCount].color = color;
  1448. ++vertCount;
  1449. }
  1450. }
  1451. verts.unlock();
  1452. mDevice->setStateBlockByDesc( desc );
  1453. mDevice->setVertexBuffer( verts );
  1454. mDevice->setupGenericShaders();
  1455. mDevice->drawPrimitive( GFXLineList, 0, numLines );
  1456. }
  1457. void GFXDrawUtil::drawTransform( const GFXStateBlockDesc &desc, const MatrixF &mat, const Point3F *scale, const ColorI colors[3] )
  1458. {
  1459. GFXTransformSaver saver;
  1460. GFX->multWorld( mat );
  1461. GFXVertexBufferHandle<GFXVertexPCT> verts( mDevice, 6, GFXBufferTypeVolatile );
  1462. verts.lock();
  1463. const static ColorI defColors[3] =
  1464. {
  1465. ColorI::RED,
  1466. ColorI::GREEN,
  1467. ColorI::BLUE
  1468. };
  1469. const ColorI *colArray = ( colors != NULL ) ? colors : defColors;
  1470. verts[0].point = Point3F::Zero;
  1471. verts[0].color = colArray[0];
  1472. verts[1].point = Point3F( 1, 0, 0 );
  1473. verts[1].color = colArray[0];
  1474. verts[2].point = Point3F::Zero;
  1475. verts[2].color = colArray[1];
  1476. verts[3].point = Point3F( 0, 1, 0 );
  1477. verts[3].color = colArray[1];
  1478. verts[4].point = Point3F::Zero;
  1479. verts[4].color = colArray[2];
  1480. verts[5].point = Point3F( 0, 0, 1 );
  1481. verts[5].color = colArray[2];
  1482. if ( scale )
  1483. {
  1484. verts[1].point *= *scale;
  1485. verts[3].point *= *scale;
  1486. verts[5].point *= *scale;
  1487. }
  1488. verts.unlock();
  1489. mDevice->setStateBlockByDesc( desc );
  1490. mDevice->setVertexBuffer( verts );
  1491. mDevice->setupGenericShaders();
  1492. mDevice->drawPrimitive( GFXLineList, 0, 3 );
  1493. }