decalManager.cpp 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746
  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 "T3D/decal/decalManager.h"
  24. #include "scene/sceneManager.h"
  25. #include "scene/sceneRenderState.h"
  26. #include "ts/tsShapeInstance.h"
  27. #include "console/console.h"
  28. #include "console/dynamicTypes.h"
  29. #include "gfx/primBuilder.h"
  30. #include "console/consoleTypes.h"
  31. #include "platform/profiler.h"
  32. #include "gfx/gfxTransformSaver.h"
  33. #include "lighting/lightManager.h"
  34. #include "lighting/lightInfo.h"
  35. #include "gfx/gfxDrawUtil.h"
  36. #include "gfx/sim/gfxStateBlockData.h"
  37. #include "materials/shaderData.h"
  38. #include "materials/matInstance.h"
  39. #include "renderInstance/renderPassManager.h"
  40. #include "core/resourceManager.h"
  41. #include "core/stream/fileStream.h"
  42. #include "gfx/gfxDebugEvent.h"
  43. #include "math/util/quadTransforms.h"
  44. #include "math/mathUtils.h"
  45. #include "core/volume.h"
  46. #include "core/module.h"
  47. #include "T3D/decal/decalData.h"
  48. #include "console/engineAPI.h"
  49. extern bool gEditingMission;
  50. MODULE_BEGIN( DecalManager )
  51. MODULE_INIT_AFTER( Scene )
  52. MODULE_SHUTDOWN_BEFORE( Scene )
  53. MODULE_INIT
  54. {
  55. gDecalManager = new DecalManager;
  56. gClientSceneGraph->addObjectToScene( gDecalManager );
  57. }
  58. MODULE_SHUTDOWN
  59. {
  60. gClientSceneGraph->removeObjectFromScene( gDecalManager );
  61. SAFE_DELETE( gDecalManager );
  62. }
  63. MODULE_END;
  64. /// A bias applied to the nearPlane for Decal and DecalRoad rendering.
  65. /// Is set by by LevelInfo.
  66. F32 gDecalBias = 0.0015f;
  67. bool DecalManager::smDecalsOn = true;
  68. bool DecalManager::smDebugRender = false;
  69. F32 DecalManager::smDecalLifeTimeScale = 1.0f;
  70. bool DecalManager::smPoolBuffers = true;
  71. const U32 DecalManager::smMaxVerts = 6000;
  72. const U32 DecalManager::smMaxIndices = 10000;
  73. DecalManager *gDecalManager = NULL;
  74. IMPLEMENT_CONOBJECT(DecalManager);
  75. ConsoleDoc(
  76. "@defgroup Decals\n"
  77. "@brief Decals are non-SimObject derived objects that are stored and loaded "
  78. "separately from the normal mission file.\n\n"
  79. "The DecalManager handles all aspects of decal management including loading, "
  80. "creation, saving, and automatically deleting decals that have exceeded their "
  81. "lifeSpan.\n\n"
  82. "The static decals associated with a mission are normally loaded immediately "
  83. "after the mission itself has loaded as shown below.\n"
  84. "@tsexample\n"
  85. "// Load the static mission decals.\n"
  86. "decalManagerLoad( %missionName @ \".decals\" );\n"
  87. "@endtsexample\n"
  88. "@ingroup FX\n"
  89. );
  90. ConsoleDocClass( DecalManager,
  91. "@brief The object that manages all of the decals in the active mission.\n\n"
  92. "@see Decals\n"
  93. "@ingroup Decals\n"
  94. "@ingroup FX\n"
  95. );
  96. namespace {
  97. S32 QSORT_CALLBACK cmpDecalInstance(const void* p1, const void* p2)
  98. {
  99. const DecalInstance** pd1 = (const DecalInstance**)p1;
  100. const DecalInstance** pd2 = (const DecalInstance**)p2;
  101. return int(((char *)(*pd1)->mDataBlock) - ((char *)(*pd2)->mDataBlock));
  102. }
  103. S32 QSORT_CALLBACK cmpPointsXY( const void *p1, const void *p2 )
  104. {
  105. const Point3F *pnt1 = (const Point3F*)p1;
  106. const Point3F *pnt2 = (const Point3F*)p2;
  107. if ( pnt1->x < pnt2->x )
  108. return -1;
  109. else if ( pnt1->x > pnt2->x )
  110. return 1;
  111. else if ( pnt1->y < pnt2->y )
  112. return -1;
  113. else if ( pnt1->y > pnt2->y )
  114. return 1;
  115. else
  116. return 0;
  117. }
  118. S32 QSORT_CALLBACK cmpQuadPointTheta( const void *p1, const void *p2 )
  119. {
  120. const Point4F *pnt1 = (const Point4F*)p1;
  121. const Point4F *pnt2 = (const Point4F*)p2;
  122. if ( mFabs( pnt1->w ) > mFabs( pnt2->w ) )
  123. return 1;
  124. else if ( mFabs( pnt1->w ) < mFabs( pnt2->w ) )
  125. return -1;
  126. else
  127. return 0;
  128. }
  129. static Point3F gSortPoint;
  130. S32 QSORT_CALLBACK cmpDecalDistance( const void *p1, const void *p2 )
  131. {
  132. const DecalInstance** pd1 = (const DecalInstance**)p1;
  133. const DecalInstance** pd2 = (const DecalInstance**)p2;
  134. F32 dist1 = ( (*pd1)->mPosition - gSortPoint ).lenSquared();
  135. F32 dist2 = ( (*pd2)->mPosition - gSortPoint ).lenSquared();
  136. return mSign( dist1 - dist2 );
  137. }
  138. S32 QSORT_CALLBACK cmpDecalRenderOrder( const void *p1, const void *p2 )
  139. {
  140. const DecalInstance** pd1 = (const DecalInstance**)p1;
  141. const DecalInstance** pd2 = (const DecalInstance**)p2;
  142. if ( ( (*pd2)->mFlags & SaveDecal ) && !( (*pd1)->mFlags & SaveDecal ) )
  143. return -1;
  144. else if ( !( (*pd2)->mFlags & SaveDecal ) && ( (*pd1)->mFlags & SaveDecal ) )
  145. return 1;
  146. else
  147. {
  148. S32 priority = (*pd1)->getRenderPriority() - (*pd2)->getRenderPriority();
  149. if ( priority != 0 )
  150. return priority;
  151. if ( (*pd2)->mFlags & SaveDecal )
  152. {
  153. S32 id = ( (*pd1)->mDataBlock->getMaterial()->getId() - (*pd2)->mDataBlock->getMaterial()->getId() );
  154. if ( id != 0 )
  155. return id;
  156. return (*pd1)->mCreateTime - (*pd2)->mCreateTime;
  157. }
  158. else
  159. return (*pd1)->mCreateTime - (*pd2)->mCreateTime;
  160. }
  161. }
  162. } // namespace {}
  163. // These numbers should be tweaked to get as many dynamically placed decals
  164. // as possible to allocate buffer arrays with the FreeListChunker.
  165. enum
  166. {
  167. SIZE_CLASS_0 = 256,
  168. SIZE_CLASS_1 = 512,
  169. SIZE_CLASS_2 = 1024,
  170. NUM_SIZE_CLASSES = 3
  171. };
  172. //-------------------------------------------------------------------------
  173. // DecalManager
  174. //-------------------------------------------------------------------------
  175. DecalManager::DecalManager()
  176. {
  177. #ifdef DECALMANAGER_DEBUG
  178. VECTOR_SET_ASSOCIATION( mDebugPlanes );
  179. #endif
  180. setGlobalBounds();
  181. mDataFileName = NULL;
  182. mTypeMask |= EnvironmentObjectType;
  183. mDirty = false;
  184. mChunkers[0] = new FreeListChunkerUntyped( SIZE_CLASS_0 * sizeof( U8 ) );
  185. mChunkers[1] = new FreeListChunkerUntyped( SIZE_CLASS_1 * sizeof( U8 ) );
  186. mChunkers[2] = new FreeListChunkerUntyped( SIZE_CLASS_2 * sizeof( U8 ) );
  187. GFXDevice::getDeviceEventSignal().notify(this, &DecalManager::_handleGFXEvent);
  188. }
  189. DecalManager::~DecalManager()
  190. {
  191. GFXDevice::getDeviceEventSignal().remove(this, &DecalManager::_handleGFXEvent);
  192. clearData();
  193. for( U32 i = 0; i < NUM_SIZE_CLASSES; ++ i )
  194. delete mChunkers[ i ];
  195. }
  196. void DecalManager::consoleInit()
  197. {
  198. Con::addVariable( "$pref::Decals::enabled", TypeBool, &smDecalsOn,
  199. "Controls whether decals are rendered.\n"
  200. "@ingroup Decals" );
  201. Con::addVariable( "$pref::Decals::lifeTimeScale", TypeF32, &smDecalLifeTimeScale,
  202. "@brief Lifetime that decals will last after being created in the world.\n"
  203. "Deprecated. Use DecalData::lifeSpan instead.\n"
  204. "@ingroup Decals" );
  205. Con::addVariable( "$Decals::poolBuffers", TypeBool, &smPoolBuffers,
  206. "If true, will merge all PrimitiveBuffers and VertexBuffers into a pair "
  207. "of pools before clearing them at the end of a frame.\n"
  208. "If false, will just clear them at the end of a frame.\n"
  209. "@ingroup Decals" );
  210. Con::addVariable( "$Decals::debugRender", TypeBool, &smDebugRender,
  211. "If true, the decal spheres will be visualized when in the editor.\n\n"
  212. "@ingroup Decals" );
  213. Con::addVariable( "$Decals::sphereDistanceTolerance", TypeF32, &DecalSphere::smDistanceTolerance,
  214. "The distance at which the decal system will start breaking up decal "
  215. "spheres when adding new decals.\n\n"
  216. "@ingroup Decals" );
  217. Con::addVariable( "$Decals::sphereRadiusTolerance", TypeF32, &DecalSphere::smRadiusTolerance,
  218. "The radius beyond which the decal system will start breaking up decal "
  219. "spheres when adding new decals.\n\n"
  220. "@ingroup Decals" );
  221. }
  222. bool DecalManager::_handleGFXEvent(GFXDevice::GFXDeviceEventType event)
  223. {
  224. switch(event)
  225. {
  226. case GFXDevice::deEndOfFrame:
  227. // Return PrimitiveBuffers and VertexBuffers used this frame to the pool.
  228. if ( smPoolBuffers )
  229. {
  230. mPBPool.merge( mPBs );
  231. mPBs.clear();
  232. mVBPool.merge( mVBs );
  233. mVBs.clear();
  234. }
  235. else
  236. {
  237. _freePools();
  238. }
  239. break;
  240. default: ;
  241. }
  242. return true;
  243. }
  244. bool DecalManager::clipDecal( DecalInstance *decal, Vector<Point3F> *edgeVerts, const Point2F *clipDepth )
  245. {
  246. PROFILE_SCOPE( DecalManager_clipDecal );
  247. // Free old verts and indices.
  248. _freeBuffers( decal );
  249. F32 halfSize = decal->mSize * 0.5f;
  250. // Ugly hack for ProjectedShadow!
  251. F32 halfSizeZ = clipDepth ? clipDepth->x : halfSize;
  252. F32 negHalfSize = clipDepth ? clipDepth->y : halfSize;
  253. Point3F decalHalfSize( halfSize, halfSize, halfSize );
  254. Point3F decalHalfSizeZ( halfSizeZ, halfSizeZ, halfSizeZ );
  255. MatrixF projMat( true );
  256. decal->getWorldMatrix( &projMat );
  257. const VectorF &crossVec = decal->mNormal;
  258. const Point3F &decalPos = decal->mPosition;
  259. VectorF newFwd, newRight;
  260. projMat.getColumn( 0, &newRight );
  261. projMat.getColumn( 1, &newFwd );
  262. VectorF objRight( 1.0f, 0, 0 );
  263. VectorF objFwd( 0, 1.0f, 0 );
  264. VectorF objUp( 0, 0, 1.0f );
  265. // See above re: decalHalfSizeZ hack.
  266. mClipper.clear();
  267. mClipper.mPlaneList.setSize(6);
  268. mClipper.mPlaneList[0].set( ( decalPos + ( -newRight * halfSize ) ), -newRight );
  269. mClipper.mPlaneList[1].set( ( decalPos + ( -newFwd * halfSize ) ), -newFwd );
  270. mClipper.mPlaneList[2].set( ( decalPos + ( -crossVec * decalHalfSizeZ ) ), -crossVec );
  271. mClipper.mPlaneList[3].set( ( decalPos + ( newRight * halfSize ) ), newRight );
  272. mClipper.mPlaneList[4].set( ( decalPos + ( newFwd * halfSize ) ), newFwd );
  273. mClipper.mPlaneList[5].set( ( decalPos + ( crossVec * negHalfSize ) ), crossVec );
  274. mClipper.mNormal = decal->mNormal;
  275. const DecalData *decalData = decal->mDataBlock;
  276. mClipper.mNormalTolCosineRadians = mCos( mDegToRad( decalData->clippingAngle ) );
  277. Box3F box( -decalHalfSizeZ, decalHalfSizeZ );
  278. projMat.mul( box );
  279. PROFILE_START( DecalManager_clipDecal_buildPolyList );
  280. getContainer()->buildPolyList( PLC_Decal, box, decalData->clippingMasks, &mClipper );
  281. PROFILE_END();
  282. mClipper.cullUnusedVerts();
  283. mClipper.triangulate();
  284. const U32 numVerts = mClipper.mVertexList.size();
  285. const U32 numIndices = mClipper.mIndexList.size();
  286. if ( !numVerts || !numIndices )
  287. return false;
  288. // Fail if either of the buffer metrics exceeds our limits
  289. // on dynamic geometry buffers.
  290. if ( numVerts > smMaxVerts ||
  291. numIndices > smMaxIndices )
  292. return false;
  293. if ( !decalData->skipVertexNormals )
  294. mClipper.generateNormals();
  295. #ifdef DECALMANAGER_DEBUG
  296. mDebugPlanes.clear();
  297. mDebugPlanes.merge( mClipper.mPlaneList );
  298. #endif
  299. decal->mVertCount = numVerts;
  300. decal->mIndxCount = numIndices;
  301. Vector<Point3F> tmpPoints;
  302. tmpPoints.push_back(( objFwd * decalHalfSize ) + ( objRight * decalHalfSize ));
  303. tmpPoints.push_back(( objFwd * decalHalfSize ) + ( -objRight * decalHalfSize ));
  304. tmpPoints.push_back(( -objFwd * decalHalfSize ) + ( -objRight * decalHalfSize ));
  305. Point3F lowerLeft(( -objFwd * decalHalfSize ) + ( objRight * decalHalfSize ));
  306. projMat.inverse();
  307. _generateWindingOrder( lowerLeft, &tmpPoints );
  308. BiQuadToSqr quadToSquare( Point2F( lowerLeft.x, lowerLeft.y ),
  309. Point2F( tmpPoints[0].x, tmpPoints[0].y ),
  310. Point2F( tmpPoints[1].x, tmpPoints[1].y ),
  311. Point2F( tmpPoints[2].x, tmpPoints[2].y ) );
  312. Point2F uv( 0, 0 );
  313. Point3F vecX(0.0f, 0.0f, 0.0f);
  314. // Allocate memory for vert and index arrays
  315. _allocBuffers( decal );
  316. // Mark this so that the color will be assigned on these verts the next
  317. // time it renders, since we just threw away the previous verts.
  318. decal->mLastAlpha = -1;
  319. Point3F vertPoint( 0, 0, 0 );
  320. for ( U32 i = 0; i < mClipper.mVertexList.size(); i++ )
  321. {
  322. const ClippedPolyList::Vertex &vert = mClipper.mVertexList[i];
  323. vertPoint = vert.point;
  324. // Transform this point to
  325. // object space to look up the
  326. // UV coordinate for this vertex.
  327. projMat.mulP( vertPoint );
  328. // Clamp the point to be within the quad.
  329. vertPoint.x = mClampF( vertPoint.x, -decalHalfSize.x, decalHalfSize.x );
  330. vertPoint.y = mClampF( vertPoint.y, -decalHalfSize.y, decalHalfSize.y );
  331. // Get our UV.
  332. uv = quadToSquare.transform( Point2F( vertPoint.x, vertPoint.y ) );
  333. const RectF &rect = decal->mDataBlock->texRect[decal->mTextureRectIdx];
  334. uv *= rect.extent;
  335. uv += rect.point;
  336. // Set the world space vertex position.
  337. decal->mVerts[i].point = vert.point;
  338. decal->mVerts[i].texCoord.set( uv.x, uv.y );
  339. if ( mClipper.mNormalList.empty() )
  340. continue;
  341. decal->mVerts[i].normal = mClipper.mNormalList[i];
  342. decal->mVerts[i].normal.normalize();
  343. if( mFabs( decal->mVerts[i].normal.z ) > 0.8f )
  344. mCross( decal->mVerts[i].normal, Point3F( 1.0f, 0.0f, 0.0f ), &vecX );
  345. else if ( mFabs( decal->mVerts[i].normal.x ) > 0.8f )
  346. mCross( decal->mVerts[i].normal, Point3F( 0.0f, 1.0f, 0.0f ), &vecX );
  347. else if ( mFabs( decal->mVerts[i].normal.y ) > 0.8f )
  348. mCross( decal->mVerts[i].normal, Point3F( 0.0f, 0.0f, 1.0f ), &vecX );
  349. decal->mVerts[i].tangent = mCross( decal->mVerts[i].normal, vecX );
  350. }
  351. U32 curIdx = 0;
  352. for ( U32 j = 0; j < mClipper.mPolyList.size(); j++ )
  353. {
  354. // Write indices for each Poly
  355. ClippedPolyList::Poly *poly = &mClipper.mPolyList[j];
  356. AssertFatal( poly->vertexCount == 3, "Got non-triangle poly!" );
  357. decal->mIndices[curIdx] = mClipper.mIndexList[poly->vertexStart];
  358. curIdx++;
  359. decal->mIndices[curIdx] = mClipper.mIndexList[poly->vertexStart + 1];
  360. curIdx++;
  361. decal->mIndices[curIdx] = mClipper.mIndexList[poly->vertexStart + 2];
  362. curIdx++;
  363. }
  364. if ( !edgeVerts )
  365. return true;
  366. Point3F tmpHullPt( 0, 0, 0 );
  367. Vector<Point3F> tmpHullPts;
  368. for ( U32 i = 0; i < mClipper.mVertexList.size(); i++ )
  369. {
  370. const ClippedPolyList::Vertex &vert = mClipper.mVertexList[i];
  371. tmpHullPt = vert.point;
  372. projMat.mulP( tmpHullPt );
  373. tmpHullPts.push_back( tmpHullPt );
  374. }
  375. edgeVerts->clear();
  376. U32 verts = _generateConvexHull( tmpHullPts, edgeVerts );
  377. edgeVerts->setSize( verts );
  378. projMat.inverse();
  379. for ( U32 i = 0; i < edgeVerts->size(); i++ )
  380. projMat.mulP( (*edgeVerts)[i] );
  381. return true;
  382. }
  383. DecalInstance* DecalManager::addDecal( const Point3F &pos,
  384. const Point3F &normal,
  385. F32 rotAroundNormal,
  386. DecalData *decalData,
  387. F32 decalScale,
  388. S32 decalTexIndex,
  389. U8 flags )
  390. {
  391. MatrixF mat( true );
  392. MathUtils::getMatrixFromUpVector( normal, &mat );
  393. AngAxisF rot( normal, rotAroundNormal );
  394. MatrixF rotmat;
  395. rot.setMatrix( &rotmat );
  396. mat.mul( rotmat );
  397. Point3F tangent;
  398. mat.getColumn( 1, &tangent );
  399. return addDecal( pos, normal, tangent, decalData, decalScale, decalTexIndex, flags );
  400. }
  401. DecalInstance* DecalManager::addDecal( const Point3F& pos,
  402. const Point3F& normal,
  403. const Point3F& tangent,
  404. DecalData* decalData,
  405. F32 decalScale,
  406. S32 decalTexIndex,
  407. U8 flags )
  408. {
  409. if ( !mData && !_createDataFile() )
  410. return NULL;
  411. // only dirty the manager if this decal should be saved
  412. if ( flags & SaveDecal )
  413. mDirty = true;
  414. return mData->addDecal( pos, normal, tangent, decalData, decalScale, decalTexIndex, flags );
  415. }
  416. void DecalManager::removeDecal( DecalInstance *inst )
  417. {
  418. // If this is a decal we save then we need
  419. // to set the dirty flag.
  420. if ( inst->mFlags & SaveDecal )
  421. mDirty = true;
  422. // Remove the decal from the instance vector.
  423. if( inst->mId != -1 && inst->mId < mDecalInstanceVec.size() )
  424. mDecalInstanceVec[ inst->mId ] = NULL;
  425. // Release its geometry (if it has any).
  426. _freeBuffers( inst );
  427. // Remove it from the decal file.
  428. if ( mData )
  429. mData->removeDecal( inst );
  430. }
  431. DecalInstance* DecalManager::getDecal( S32 id )
  432. {
  433. if( id < 0 || id >= mDecalInstanceVec.size() )
  434. return NULL;
  435. return mDecalInstanceVec[id];
  436. }
  437. void DecalManager::notifyDecalModified( DecalInstance *inst )
  438. {
  439. // If this is a decal we save then we need
  440. // to set the dirty flag.
  441. if ( inst->mFlags & SaveDecal )
  442. mDirty = true;
  443. if ( mData )
  444. mData->notifyDecalModified( inst );
  445. }
  446. DecalInstance* DecalManager::getClosestDecal( const Point3F &pos )
  447. {
  448. if ( !mData )
  449. return NULL;
  450. const Vector<DecalSphere*> &grid = mData->getSphereList();
  451. DecalInstance *inst = NULL;
  452. SphereF worldPickSphere( pos, 0.5f );
  453. SphereF worldInstSphere( Point3F( 0, 0, 0 ), 1.0f );
  454. Vector<DecalInstance*> collectedInsts;
  455. for ( U32 i = 0; i < grid.size(); i++ )
  456. {
  457. DecalSphere *decalSphere = grid[i];
  458. const SphereF &worldSphere = decalSphere->mWorldSphere;
  459. if ( !worldSphere.isIntersecting( worldPickSphere ) &&
  460. !worldSphere.isContained( pos ) )
  461. continue;
  462. const Vector<DecalInstance*> &items = decalSphere->mItems;
  463. for ( U32 n = 0; n < items.size(); n++ )
  464. {
  465. inst = items[n];
  466. if ( !inst )
  467. continue;
  468. worldInstSphere.center = inst->mPosition;
  469. worldInstSphere.radius = inst->mSize;
  470. if ( !worldInstSphere.isContained( inst->mPosition ) )
  471. continue;
  472. collectedInsts.push_back( inst );
  473. }
  474. }
  475. F32 closestDistance = F32_MAX;
  476. F32 currentDist = 0;
  477. U32 closestIndex = 0;
  478. for ( U32 i = 0; i < collectedInsts.size(); i++ )
  479. {
  480. inst = collectedInsts[i];
  481. currentDist = (inst->mPosition - pos).len();
  482. if ( currentDist < closestDistance )
  483. {
  484. closestIndex = i;
  485. closestDistance = currentDist;
  486. worldInstSphere.center = inst->mPosition;
  487. worldInstSphere.radius = inst->mSize;
  488. }
  489. }
  490. if ( !collectedInsts.empty() &&
  491. collectedInsts[closestIndex] &&
  492. closestDistance < 1.0f ||
  493. worldInstSphere.isContained( pos ) )
  494. return collectedInsts[closestIndex];
  495. else
  496. return NULL;
  497. }
  498. DecalInstance* DecalManager::raycast( const Point3F &start, const Point3F &end, bool savedDecalsOnly )
  499. {
  500. if ( !mData )
  501. return NULL;
  502. const Vector<DecalSphere*> &grid = mData->getSphereList();
  503. DecalInstance *inst = NULL;
  504. SphereF worldSphere( Point3F( 0, 0, 0 ), 1.0f );
  505. Vector<DecalInstance*> hitDecals;
  506. for ( U32 i = 0; i < grid.size(); i++ )
  507. {
  508. DecalSphere *decalSphere = grid[i];
  509. if ( !decalSphere->mWorldSphere.intersectsRay( start, end ) )
  510. continue;
  511. const Vector<DecalInstance*> &items = decalSphere->mItems;
  512. for ( U32 n = 0; n < items.size(); n++ )
  513. {
  514. inst = items[n];
  515. if ( !inst )
  516. continue;
  517. if ( savedDecalsOnly && !(inst->mFlags & SaveDecal) )
  518. continue;
  519. worldSphere.center = inst->mPosition;
  520. worldSphere.radius = inst->mSize;
  521. if ( !worldSphere.intersectsRay( start, end ) )
  522. continue;
  523. RayInfo ri;
  524. bool containsPoint = false;
  525. if ( gServerContainer.castRayRendered( start, end, STATIC_COLLISION_TYPEMASK, &ri ) )
  526. {
  527. Point2F poly[4];
  528. poly[0].set( inst->mPosition.x - (inst->mSize / 2), inst->mPosition.y + (inst->mSize / 2));
  529. poly[1].set( inst->mPosition.x - (inst->mSize / 2), inst->mPosition.y - (inst->mSize / 2));
  530. poly[2].set( inst->mPosition.x + (inst->mSize / 2), inst->mPosition.y - (inst->mSize / 2));
  531. poly[3].set( inst->mPosition.x + (inst->mSize / 2), inst->mPosition.y + (inst->mSize / 2));
  532. if ( MathUtils::pointInPolygon( poly, 4, Point2F(ri.point.x, ri.point.y) ) )
  533. containsPoint = true;
  534. }
  535. if( !containsPoint )
  536. continue;
  537. hitDecals.push_back( inst );
  538. }
  539. }
  540. if ( hitDecals.empty() )
  541. return NULL;
  542. gSortPoint = start;
  543. dQsort( hitDecals.address(), hitDecals.size(), sizeof(DecalInstance*), cmpDecalDistance );
  544. return hitDecals[0];
  545. }
  546. U32 DecalManager::_generateConvexHull( const Vector<Point3F> &points, Vector<Point3F> *outPoints )
  547. {
  548. PROFILE_SCOPE( DecalManager_generateConvexHull );
  549. // chainHull_2D(): Andrew's monotone chain 2D convex hull algorithm
  550. // Input: P[] = an array of 2D points
  551. // presorted by increasing x- and y-coordinates
  552. // n = the number of points in P[]
  553. // Output: H[] = an array of the convex hull vertices (max is n)
  554. // Return: the number of points in H[]
  555. //int
  556. if ( points.size() < 3 )
  557. {
  558. outPoints->merge( points );
  559. return outPoints->size();
  560. }
  561. // Sort our input points.
  562. dQsort( points.address(), points.size(), sizeof( Point3F ), cmpPointsXY );
  563. U32 n = points.size();
  564. Vector<Point3F> tmpPoints;
  565. tmpPoints.setSize( n );
  566. // the output array H[] will be used as the stack
  567. S32 bot=0, top=(-1); // indices for bottom and top of the stack
  568. S32 i; // array scan index
  569. S32 toptmp = 0;
  570. // Get the indices of points with min x-coord and min|max y-coord
  571. S32 minmin = 0, minmax;
  572. F32 xmin = points[0].x;
  573. for ( i = 1; i < n; i++ )
  574. if (points[i].x != xmin)
  575. break;
  576. minmax = i - 1;
  577. if ( minmax == n - 1 )
  578. {
  579. // degenerate case: all x-coords == xmin
  580. toptmp = top + 1;
  581. if ( toptmp < n )
  582. tmpPoints[++top] = points[minmin];
  583. if ( points[minmax].y != points[minmin].y ) // a nontrivial segment
  584. {
  585. toptmp = top + 1;
  586. if ( toptmp < n )
  587. tmpPoints[++top] = points[minmax];
  588. }
  589. toptmp = top + 1;
  590. if ( toptmp < n )
  591. tmpPoints[++top] = points[minmin]; // add polygon endpoint
  592. return top+1;
  593. }
  594. // Get the indices of points with max x-coord and min|max y-coord
  595. S32 maxmin, maxmax = n-1;
  596. F32 xmax = points[n-1].x;
  597. for ( i = n - 2; i >= 0; i-- )
  598. if ( points[i].x != xmax )
  599. break;
  600. maxmin = i + 1;
  601. // Compute the lower hull on the stack H
  602. toptmp = top + 1;
  603. if ( toptmp < n )
  604. tmpPoints[++top] = points[minmin]; // push minmin point onto stack
  605. i = minmax;
  606. while ( ++i <= maxmin )
  607. {
  608. // the lower line joins P[minmin] with P[maxmin]
  609. if ( isLeft( points[minmin], points[maxmin], points[i]) >= 0 && i < maxmin )
  610. continue; // ignore P[i] above or on the lower line
  611. while (top > 0) // there are at least 2 points on the stack
  612. {
  613. // test if P[i] is left of the line at the stack top
  614. if ( isLeft( tmpPoints[top-1], tmpPoints[top], points[i]) > 0)
  615. break; // P[i] is a new hull vertex
  616. else
  617. top--; // pop top point off stack
  618. }
  619. toptmp = top + 1;
  620. if ( toptmp < n )
  621. tmpPoints[++top] = points[i]; // push P[i] onto stack
  622. }
  623. // Next, compute the upper hull on the stack H above the bottom hull
  624. if (maxmax != maxmin) // if distinct xmax points
  625. {
  626. toptmp = top + 1;
  627. if ( toptmp < n )
  628. tmpPoints[++top] = points[maxmax]; // push maxmax point onto stack
  629. }
  630. bot = top; // the bottom point of the upper hull stack
  631. i = maxmin;
  632. while (--i >= minmax)
  633. {
  634. // the upper line joins P[maxmax] with P[minmax]
  635. if ( isLeft( points[maxmax], points[minmax], points[i] ) >= 0 && i > minmax )
  636. continue; // ignore P[i] below or on the upper line
  637. while ( top > bot ) // at least 2 points on the upper stack
  638. {
  639. // test if P[i] is left of the line at the stack top
  640. if ( isLeft( tmpPoints[top-1], tmpPoints[top], points[i] ) > 0 )
  641. break; // P[i] is a new hull vertex
  642. else
  643. top--; // pop top point off stack
  644. }
  645. toptmp = top + 1;
  646. if ( toptmp < n )
  647. tmpPoints[++top] = points[i]; // push P[i] onto stack
  648. }
  649. if (minmax != minmin)
  650. {
  651. toptmp = top + 1;
  652. if ( toptmp < n )
  653. tmpPoints[++top] = points[minmin]; // push joining endpoint onto stack
  654. }
  655. outPoints->merge( tmpPoints );
  656. return top + 1;
  657. }
  658. void DecalManager::_generateWindingOrder( const Point3F &cornerPoint, Vector<Point3F> *sortPoints )
  659. {
  660. // This block of code is used to find
  661. // the winding order for the points in our quad.
  662. // First, choose an arbitrary corner point.
  663. // We'll use the "lowerRight" point.
  664. Point3F relPoint( 0, 0, 0 );
  665. // See comment below about radius.
  666. //F32 radius = 0;
  667. F32 theta = 0;
  668. Vector<Point4F> tmpPoints;
  669. for ( U32 i = 0; i < (*sortPoints).size(); i++ )
  670. {
  671. const Point3F &pnt = (*sortPoints)[i];
  672. relPoint = cornerPoint - pnt;
  673. // Get the radius (r^2 = x^2 + y^2).
  674. // This is commented because for a quad
  675. // you typically can't have the same values
  676. // for theta, which is the caveat which would
  677. // require sorting by the radius.
  678. //radius = mSqrt( (relPoint.x * relPoint.x) + (relPoint.y * relPoint.y) );
  679. // Get the theta value for the
  680. // interval -PI, PI.
  681. // This algorithm for determining the
  682. // theta value is defined by
  683. // | arctan( y / x ) if x > 0
  684. // | arctan( y / x ) if x < 0 and y >= 0
  685. // theta = | arctan( y / x ) if x < 0 and y < 0
  686. // | PI / 2 if x = 0 and y > 0
  687. // | -( PI / 2 ) if x = 0 and y < 0
  688. if ( relPoint.x > 0.0f )
  689. theta = mAtan2( relPoint.y, relPoint.x );
  690. else if ( relPoint.x < 0.0f )
  691. {
  692. if ( relPoint.y >= 0.0f )
  693. theta = mAtan2( relPoint.y, relPoint.x ) + M_PI_F;
  694. else if ( relPoint.y < 0.0f )
  695. theta = mAtan2( relPoint.y, relPoint.x ) - M_PI_F;
  696. }
  697. else if ( relPoint.x == 0.0f )
  698. {
  699. if ( relPoint.y > 0.0f )
  700. theta = M_PI_F / 2.0f;
  701. else if ( relPoint.y < 0.0f )
  702. theta = -(M_PI_F / 2.0f);
  703. }
  704. tmpPoints.push_back( Point4F( pnt.x, pnt.y, pnt.z, theta ) );
  705. }
  706. dQsort( tmpPoints.address(), tmpPoints.size(), sizeof( Point4F ), cmpQuadPointTheta );
  707. for ( U32 i = 0; i < tmpPoints.size(); i++ )
  708. {
  709. const Point4F &tmpPoint = tmpPoints[i];
  710. (*sortPoints)[i].set( tmpPoint.x, tmpPoint.y, tmpPoint.z );
  711. }
  712. }
  713. void DecalManager::_allocBuffers( DecalInstance *inst )
  714. {
  715. const S32 sizeClass = _getSizeClass( inst );
  716. void* data;
  717. if ( sizeClass == -1 )
  718. data = dMalloc( sizeof( DecalVertex ) * inst->mVertCount + sizeof( U16 ) * inst->mIndxCount );
  719. else
  720. data = mChunkers[sizeClass]->alloc();
  721. inst->mVerts = reinterpret_cast< DecalVertex* >( data );
  722. data = (U8*)data + sizeof( DecalVertex ) * inst->mVertCount;
  723. inst->mIndices = reinterpret_cast< U16* >( data );
  724. }
  725. void DecalManager::_freeBuffers( DecalInstance *inst )
  726. {
  727. if ( inst->mVerts != NULL )
  728. {
  729. const S32 sizeClass = _getSizeClass( inst );
  730. if ( sizeClass == -1 )
  731. dFree( inst->mVerts );
  732. else
  733. {
  734. // Use FreeListChunker
  735. mChunkers[sizeClass]->free( inst->mVerts );
  736. }
  737. inst->mVerts = NULL;
  738. inst->mVertCount = 0;
  739. inst->mIndices = NULL;
  740. inst->mIndxCount = 0;
  741. }
  742. }
  743. void DecalManager::_freePools()
  744. {
  745. while ( !mVBPool.empty() )
  746. {
  747. delete mVBPool.last();
  748. mVBPool.pop_back();
  749. }
  750. while ( !mVBs.empty() )
  751. {
  752. delete mVBs.last();
  753. mVBs.pop_back();
  754. }
  755. while ( !mPBPool.empty() )
  756. {
  757. delete mPBPool.last();
  758. mPBPool.pop_back();
  759. }
  760. while ( !mPBs.empty() )
  761. {
  762. delete mPBs.last();
  763. mPBs.pop_back();
  764. }
  765. }
  766. S32 DecalManager::_getSizeClass( DecalInstance *inst ) const
  767. {
  768. U32 bytes = inst->mVertCount * sizeof( DecalVertex ) + inst->mIndxCount * sizeof ( U16 );
  769. if ( bytes <= SIZE_CLASS_0 )
  770. return 0;
  771. if ( bytes <= SIZE_CLASS_1 )
  772. return 1;
  773. if ( bytes <= SIZE_CLASS_2 )
  774. return 2;
  775. // Size is outside of the largest chunker.
  776. return -1;
  777. }
  778. void DecalManager::prepRenderImage( SceneRenderState* state )
  779. {
  780. PROFILE_SCOPE( DecalManager_RenderDecals );
  781. if ( !smDecalsOn || !mData )
  782. return;
  783. // Decals only render in the diffuse pass!
  784. // We technically could render them into reflections but prefer to save
  785. // the performance. This would also break the DecalInstance::mLastAlpha
  786. // optimization.
  787. if ( !state->isDiffusePass() )
  788. return;
  789. PROFILE_START( DecalManager_RenderDecals_SphereTreeCull );
  790. const Frustum& rootFrustum = state->getCameraFrustum();
  791. // Populate vector of decal instances to be rendered with all
  792. // decals from visible decal spheres.
  793. SceneManager* sceneManager = state->getSceneManager();
  794. SceneZoneSpaceManager* zoneManager = sceneManager->getZoneManager();
  795. AssertFatal( zoneManager, "DecalManager::prepRenderImage - No zone manager!" );
  796. const Vector<DecalSphere*> &grid = mData->getSphereList();
  797. const bool haveOnlyOutdoorZone = ( zoneManager->getNumActiveZones() == 1 );
  798. mDecalQueue.clear();
  799. for ( U32 i = 0; i < grid.size(); i++ )
  800. {
  801. DecalSphere* decalSphere = grid[i];
  802. const SphereF& worldSphere = decalSphere->mWorldSphere;
  803. // See if this decal sphere can be culled.
  804. const SceneCullingState& cullingState = state->getCullingState();
  805. if( haveOnlyOutdoorZone )
  806. {
  807. U32 outdoorZone = SceneZoneSpaceManager::RootZoneId;
  808. if( cullingState.isCulled( worldSphere, &outdoorZone, 1 ) )
  809. continue;
  810. }
  811. else
  812. {
  813. // Update the zoning state of the sphere, if we need to.
  814. if( decalSphere->mZones.size() == 0 )
  815. decalSphere->updateZoning( zoneManager );
  816. // Skip the sphere if it is not visible in any of its zones.
  817. if( cullingState.isCulled( worldSphere, decalSphere->mZones.address(), decalSphere->mZones.size() ) )
  818. continue;
  819. }
  820. // TODO: If each sphere stored its largest decal instance we
  821. // could do an LOD step on it here and skip adding any of the
  822. // decals in the sphere.
  823. mDecalQueue.merge( decalSphere->mItems );
  824. }
  825. PROFILE_END();
  826. PROFILE_START( DecalManager_RenderDecals_Update );
  827. const U32 &curSimTime = Sim::getCurrentTime();
  828. F32 pixelSize;
  829. U32 delta, diff;
  830. DecalInstance *dinst;
  831. DecalData *ddata;
  832. // Loop through DecalQueue once for preRendering work.
  833. // 1. Update DecalInstance fade (over time)
  834. // 2. Clip geometry if flagged to do so.
  835. // 3. Calculate lod - if decal is far enough away it will not render.
  836. for ( U32 i = 0; i < mDecalQueue.size(); i++ )
  837. {
  838. dinst = mDecalQueue[i];
  839. ddata = dinst->mDataBlock;
  840. // LOD calculation...
  841. pixelSize = dinst->calcPixelSize( state->getViewport().extent.y, state->getCameraPosition(), state->getWorldToScreenScale().y );
  842. if ( pixelSize != F32_MAX && pixelSize < ddata->fadeEndPixelSize )
  843. {
  844. mDecalQueue.erase_fast( i );
  845. i--;
  846. continue;
  847. }
  848. // We will try to render this decal... so do any
  849. // final adjustments to it before rendering.
  850. // Update fade and delete expired.
  851. if ( !( dinst->mFlags & PermanentDecal || dinst->mFlags & CustomDecal ) )
  852. {
  853. delta = ( curSimTime - dinst->mCreateTime );
  854. if (delta > dinst->mDataBlock->lifeSpan * smDecalLifeTimeScale)
  855. {
  856. diff = delta - (dinst->mDataBlock->lifeSpan * smDecalLifeTimeScale);
  857. dinst->mVisibility = 1.0f - (F32)diff / ((F32)dinst->mDataBlock->fadeTime * smDecalLifeTimeScale);
  858. if ( dinst->mVisibility <= 0.0f )
  859. {
  860. mDecalQueue.erase_fast( i );
  861. removeDecal( dinst );
  862. i--;
  863. continue;
  864. }
  865. }
  866. }
  867. // Build clipped geometry for this decal if needed.
  868. if ( dinst->mFlags & ClipDecal && !( dinst->mFlags & CustomDecal ) )
  869. {
  870. // Turn off the flag so we don't continually try to clip
  871. // if it fails.
  872. dinst->mFlags = dinst->mFlags & ~ClipDecal;
  873. if ( !(dinst->mFlags & CustomDecal) && !clipDecal( dinst ) )
  874. {
  875. // Clipping failed to get any geometry...
  876. // Remove it from the render queue.
  877. mDecalQueue.erase_fast( i );
  878. i--;
  879. // If the decal is one placed at run-time (not the editor)
  880. // then we should also permanently delete the decal instance.
  881. if ( !(dinst->mFlags & SaveDecal) )
  882. {
  883. removeDecal( dinst );
  884. }
  885. // If this is a decal placed by the editor it will be
  886. // flagged to attempt clipping again the next time it is
  887. // modified. For now we just skip rendering it.
  888. continue;
  889. }
  890. }
  891. // If we get here and the decal still does not have any geometry
  892. // skip rendering it. It must be an editor placed decal that failed
  893. // to clip any geometry but has not yet been flagged to try again.
  894. if ( !dinst->mVerts || dinst->mVertCount == 0 || dinst->mIndxCount == 0 )
  895. {
  896. mDecalQueue.erase_fast( i );
  897. i--;
  898. continue;
  899. }
  900. // Calculate the alpha value for this decal and apply it to the verts.
  901. {
  902. PROFILE_START( DecalManager_RenderDecals_Update_SetAlpha );
  903. F32 alpha = 1.0f;
  904. // Only necessary for decals which fade over time or distance.
  905. if ( !( dinst->mFlags & PermanentDecal ) || dinst->mDataBlock->fadeStartPixelSize >= 0.0f )
  906. {
  907. if ( pixelSize < ddata->fadeStartPixelSize )
  908. {
  909. const F32 range = ddata->fadeStartPixelSize - ddata->fadeEndPixelSize;
  910. alpha = 1.0f - mClampF( ( ddata->fadeStartPixelSize - pixelSize ) / range, 0.0f, 1.0f );
  911. }
  912. alpha *= dinst->mVisibility;
  913. }
  914. // If the alpha value has not changed since last render avoid
  915. // looping through all the verts!
  916. if ( alpha != dinst->mLastAlpha )
  917. {
  918. // calculate the swizzles color once, outside the loop.
  919. GFXVertexColor color;
  920. color.set( 255, 255, 255, (U8)(alpha * 255.0f) );
  921. for ( U32 v = 0; v < dinst->mVertCount; v++ )
  922. dinst->mVerts[v].color = color;
  923. dinst->mLastAlpha = alpha;
  924. }
  925. PROFILE_END();
  926. }
  927. }
  928. PROFILE_END();
  929. if ( mDecalQueue.empty() )
  930. return;
  931. // Sort queued decals...
  932. // 1. Editor decals - in render priority order first, creation time second, and material third.
  933. // 2. Dynamic decals - in render priority order first and creation time second.
  934. //
  935. // With the constraint that decals with different render priority cannot
  936. // be rendered together in the same draw call.
  937. PROFILE_START( DecalManager_RenderDecals_Sort );
  938. dQsort( mDecalQueue.address(), mDecalQueue.size(), sizeof(DecalInstance*), cmpDecalRenderOrder );
  939. PROFILE_END();
  940. PROFILE_SCOPE( DecalManager_RenderDecals_RenderBatch );
  941. RenderPassManager *renderPass = state->getRenderPass();
  942. // Base render instance we use for convenience.
  943. // Data shared by all instances we allocate below can be copied
  944. // from the base instance at the same time.
  945. MeshRenderInst baseRenderInst;
  946. baseRenderInst.clear();
  947. MatrixF *tempMat = renderPass->allocUniqueXform( MatrixF( true ) );
  948. MathUtils::getZBiasProjectionMatrix( gDecalBias, rootFrustum, tempMat );
  949. baseRenderInst.projection = tempMat;
  950. baseRenderInst.objectToWorld = &MatrixF::Identity;
  951. baseRenderInst.worldToCamera = renderPass->allocSharedXform(RenderPassManager::View);
  952. baseRenderInst.type = RenderPassManager::RIT_Decal;
  953. // Make it the sort distance the max distance so that
  954. // it renders after all the other opaque geometry in
  955. // the prepass bin.
  956. baseRenderInst.sortDistSq = F32_MAX;
  957. Vector<DecalBatch> batches;
  958. DecalBatch *currentBatch = NULL;
  959. // Loop through DecalQueue collecting them into render batches.
  960. for ( U32 i = 0; i < mDecalQueue.size(); i++ )
  961. {
  962. DecalInstance *decal = mDecalQueue[i];
  963. DecalData *data = decal->mDataBlock;
  964. Material *mat = data->getMaterial();
  965. if ( currentBatch == NULL )
  966. {
  967. // Start a new batch, beginning with this decal.
  968. batches.increment();
  969. currentBatch = &batches.last();
  970. currentBatch->startDecal = i;
  971. currentBatch->decalCount = 1;
  972. // Shrink and warning: preventing a potential crash.
  973. currentBatch->iCount =
  974. (decal->mIndxCount > smMaxIndices) ? smMaxIndices : decal->mIndxCount;
  975. currentBatch->vCount =
  976. (decal->mVertCount > smMaxVerts) ? smMaxVerts : decal->mVertCount;
  977. #ifdef TORQUE_DEBUG
  978. // we didn't mean send a spam to the console
  979. static U32 countMsgIndx = 0;
  980. if ( (decal->mIndxCount > smMaxIndices) && ((countMsgIndx++ % 1024) == 0) ) {
  981. Con::warnf(
  982. "DecalManager::prepRenderImage() - Shrinked indices of decal."
  983. " Lost %u.", (decal->mIndxCount - smMaxIndices)
  984. );
  985. }
  986. static U32 countMsgVert = 0;
  987. if ( (decal->mVertCount > smMaxVerts) && ((countMsgVert++ % 1024) == 0) ) {
  988. Con::warnf(
  989. "DecalManager::prepRenderImage() - Shrinked vertices of decal."
  990. " Lost %u.", (decal->mVertCount - smMaxVerts)
  991. );
  992. }
  993. #endif
  994. currentBatch->mat = mat;
  995. currentBatch->matInst = decal->mDataBlock->getMaterialInstance();
  996. currentBatch->priority = decal->getRenderPriority();
  997. currentBatch->dynamic = !(decal->mFlags & SaveDecal);
  998. continue;
  999. }
  1000. if ( currentBatch->iCount + decal->mIndxCount >= smMaxIndices ||
  1001. currentBatch->vCount + decal->mVertCount >= smMaxVerts ||
  1002. currentBatch->mat != mat ||
  1003. currentBatch->priority != decal->getRenderPriority() ||
  1004. decal->mCustomTex )
  1005. {
  1006. // End batch.
  1007. currentBatch = NULL;
  1008. i--;
  1009. continue;
  1010. }
  1011. // Add on to current batch.
  1012. currentBatch->decalCount++;
  1013. currentBatch->iCount += decal->mIndxCount;
  1014. currentBatch->vCount += decal->mVertCount;
  1015. }
  1016. // Make sure our primitive and vertex buffer handle storage is
  1017. // big enough to take all batches. Doing this now avoids reallocation
  1018. // later on which would invalidate all the pointers we already had
  1019. // passed into render instances.
  1020. //mPBs.reserve( batches.size() );
  1021. //mVBs.reserve( batches.size() );
  1022. // System memory array of verts and indices so we can fill them incrementally
  1023. // and then memcpy to the graphics device buffers in one call.
  1024. static DecalVertex vertData[smMaxVerts];
  1025. static U16 indexData[smMaxIndices];
  1026. // Loop through batches allocating buffers and submitting render instances.
  1027. for ( U32 i = 0; i < batches.size(); i++ )
  1028. {
  1029. DecalBatch &currentBatch = batches[i];
  1030. // Copy data into the system memory arrays, from all decals in this batch...
  1031. DecalVertex *vpPtr = vertData;
  1032. U16 *pbPtr = indexData;
  1033. U32 lastDecal = currentBatch.startDecal + currentBatch.decalCount;
  1034. U32 voffset = 0;
  1035. U32 ioffset = 0;
  1036. // This is an ugly hack for ProjectedShadow!
  1037. GFXTextureObject *customTex = NULL;
  1038. for ( U32 j = currentBatch.startDecal; j < lastDecal; j++ )
  1039. {
  1040. DecalInstance *dinst = mDecalQueue[j];
  1041. const U32 indxCount =
  1042. (dinst->mIndxCount > currentBatch.iCount) ?
  1043. currentBatch.iCount : dinst->mIndxCount;
  1044. for ( U32 k = 0; k < indxCount; k++ )
  1045. {
  1046. *( pbPtr + ioffset + k ) = dinst->mIndices[k] + voffset;
  1047. }
  1048. ioffset += indxCount;
  1049. const U32 vertCount =
  1050. (dinst->mVertCount > currentBatch.vCount) ?
  1051. currentBatch.vCount : dinst->mVertCount;
  1052. dMemcpy( vpPtr + voffset, dinst->mVerts, sizeof( DecalVertex ) * vertCount );
  1053. voffset += vertCount;
  1054. // Ugly hack for ProjectedShadow!
  1055. if ( (dinst->mFlags & CustomDecal) && dinst->mCustomTex != NULL )
  1056. customTex = *dinst->mCustomTex;
  1057. }
  1058. AssertFatal( ioffset == currentBatch.iCount, "bad" );
  1059. AssertFatal( voffset == currentBatch.vCount, "bad" );
  1060. // Get handles to video memory buffers we will be filling...
  1061. GFXVertexBufferHandle<DecalVertex> *vb = NULL;
  1062. if ( mVBPool.empty() )
  1063. {
  1064. // If the Pool is empty allocate a new one.
  1065. vb = new GFXVertexBufferHandle<DecalVertex>;
  1066. vb->set( GFX, smMaxVerts, GFXBufferTypeDynamic );
  1067. }
  1068. else
  1069. {
  1070. // Otherwise grab from the pool.
  1071. vb = mVBPool.last();
  1072. mVBPool.pop_back();
  1073. }
  1074. // Push into our vector of 'in use' buffers.
  1075. mVBs.push_back( vb );
  1076. // Ready to start filling.
  1077. vpPtr = vb->lock();
  1078. // Same deal as above...
  1079. GFXPrimitiveBufferHandle *pb = NULL;
  1080. if ( mPBPool.empty() )
  1081. {
  1082. pb = new GFXPrimitiveBufferHandle;
  1083. pb->set( GFX, smMaxIndices, 0, GFXBufferTypeDynamic );
  1084. }
  1085. else
  1086. {
  1087. pb = mPBPool.last();
  1088. mPBPool.pop_back();
  1089. }
  1090. mPBs.push_back( pb );
  1091. pb->lock( &pbPtr );
  1092. // Memcpy from system to video memory.
  1093. const U32 vpCount = sizeof( DecalVertex ) * currentBatch.vCount;
  1094. dMemcpy( vpPtr, vertData, vpCount );
  1095. const U32 pbCount = sizeof( U16 ) * currentBatch.iCount;
  1096. dMemcpy( pbPtr, indexData, pbCount );
  1097. pb->unlock();
  1098. vb->unlock();
  1099. // DecalManager must hold handles to these buffers so they remain valid,
  1100. // we don't actually use them elsewhere.
  1101. //mPBs.push_back( pb );
  1102. //mVBs.push_back( vb );
  1103. // Get the best lights for the current camera position
  1104. // if the materail is forward lit and we haven't got them yet.
  1105. if ( currentBatch.matInst->isForwardLit() && !baseRenderInst.lights[0] )
  1106. {
  1107. LightQuery query;
  1108. query.init( rootFrustum.getPosition(),
  1109. rootFrustum.getTransform().getForwardVector(),
  1110. rootFrustum.getFarDist() );
  1111. query.getLights( baseRenderInst.lights, 8 );
  1112. }
  1113. // Submit render inst...
  1114. MeshRenderInst *ri = renderPass->allocInst<MeshRenderInst>();
  1115. *ri = baseRenderInst;
  1116. ri->primBuff = pb;
  1117. ri->vertBuff = vb;
  1118. ri->matInst = currentBatch.matInst;
  1119. ri->prim = renderPass->allocPrim();
  1120. ri->prim->type = GFXTriangleList;
  1121. ri->prim->minIndex = 0;
  1122. ri->prim->startIndex = 0;
  1123. ri->prim->numPrimitives = currentBatch.iCount / 3;
  1124. ri->prim->startVertex = 0;
  1125. ri->prim->numVertices = currentBatch.vCount;
  1126. // Ugly hack for ProjectedShadow!
  1127. if ( customTex )
  1128. ri->miscTex = customTex;
  1129. // The decal bin will contain render instances for both decals and decalRoad's.
  1130. // Dynamic decals render last, then editor decals and roads in priority order.
  1131. // DefaultKey is sorted in descending order.
  1132. ri->defaultKey = currentBatch.dynamic ? 0xFFFFFFFF : (U32)currentBatch.priority;
  1133. ri->defaultKey2 = 1;//(U32)lastDecal->mDataBlock;
  1134. renderPass->addInst( ri );
  1135. }
  1136. #ifdef TORQUE_GATHER_METRICS
  1137. Con::setIntVariable( "$Decal::Batches", batches.size() );
  1138. Con::setIntVariable( "$Decal::Buffers", mPBs.size() + mPBPool.size() );
  1139. Con::setIntVariable( "$Decal::DecalsRendered", mDecalQueue.size() );
  1140. #endif
  1141. if( smDebugRender && gEditingMission )
  1142. {
  1143. ObjectRenderInst* ri = state->getRenderPass()->allocInst< ObjectRenderInst >();
  1144. ri->renderDelegate.bind( this, &DecalManager::_renderDecalSpheres );
  1145. ri->type = RenderPassManager::RIT_Editor;
  1146. ri->defaultKey = 0;
  1147. ri->defaultKey2 = 0;
  1148. state->getRenderPass()->addInst( ri );
  1149. }
  1150. }
  1151. void DecalManager::_renderDecalSpheres( ObjectRenderInst* ri, SceneRenderState* state, BaseMatInstance* overrideMat )
  1152. {
  1153. if( !mData )
  1154. return;
  1155. const Vector<DecalSphere*> &grid = mData->getSphereList();
  1156. GFXDrawUtil *drawUtil = GFX->getDrawUtil();
  1157. ColorI sphereColor( 0, 255, 0, 45 );
  1158. GFXStateBlockDesc desc;
  1159. desc.setBlend( true );
  1160. desc.setZReadWrite( true, false );
  1161. desc.setCullMode( GFXCullNone );
  1162. for ( U32 i = 0; i < grid.size(); i++ )
  1163. {
  1164. DecalSphere *decalSphere = grid[i];
  1165. const SphereF &worldSphere = decalSphere->mWorldSphere;
  1166. if( state->getCullingFrustum().isCulled( worldSphere ) )
  1167. continue;
  1168. drawUtil->drawSphere( desc, worldSphere.radius, worldSphere.center, sphereColor );
  1169. }
  1170. }
  1171. bool DecalManager::_createDataFile()
  1172. {
  1173. AssertFatal( !mData, "DecalManager::tried to create duplicate data file?" );
  1174. // We need to construct a default file name
  1175. char fileName[1024];
  1176. fileName[0] = 0;
  1177. // See if we know our current mission name
  1178. char missionName[1024];
  1179. dStrcpy( missionName, Con::getVariable( "$Client::MissionFile" ) );
  1180. char *dot = dStrstr((const char*)missionName, ".mis");
  1181. if(dot)
  1182. *dot = '\0';
  1183. dSprintf( fileName, sizeof(fileName), "%s.mis.decals", missionName );
  1184. mDataFileName = StringTable->insert( fileName );
  1185. // If the file doesn't exist, create an empty file.
  1186. if( !Torque::FS::IsFile( fileName ) )
  1187. {
  1188. FileStream stream;
  1189. if( stream.open( mDataFileName, Torque::FS::File::Write ) )
  1190. {
  1191. DecalDataFile dataFile;
  1192. dataFile.write( stream );
  1193. }
  1194. }
  1195. mData = ResourceManager::get().load( mDataFileName );
  1196. return (bool)mData;
  1197. }
  1198. void DecalManager::saveDecals( const UTF8* fileName )
  1199. {
  1200. if( !mData )
  1201. return;
  1202. // Create the file.
  1203. FileStream stream;
  1204. if ( !stream.open( fileName, Torque::FS::File::Write ) )
  1205. {
  1206. Con::errorf( "DecalManager::saveDecals - Could not open '%s' for writing!", fileName );
  1207. return;
  1208. }
  1209. // Write the data.
  1210. if( !mData->write( stream ) )
  1211. {
  1212. Con::errorf( "DecalManager::saveDecals - Failed to write '%s'", fileName );
  1213. return;
  1214. }
  1215. mDirty = false;
  1216. }
  1217. bool DecalManager::loadDecals( const UTF8 *fileName )
  1218. {
  1219. if( mData )
  1220. clearData();
  1221. mData = ResourceManager::get().load( fileName );
  1222. mDirty = false;
  1223. return mData != NULL;
  1224. }
  1225. void DecalManager::clearData()
  1226. {
  1227. mClearDataSignal.trigger();
  1228. // Free all geometry buffers.
  1229. if( mData )
  1230. {
  1231. const Vector< DecalSphere* > grid = mData->getSphereList();
  1232. for( U32 i = 0; i < grid.size(); ++ i )
  1233. {
  1234. DecalSphere* sphere = grid[ i ];
  1235. for( U32 n = 0; n < sphere->mItems.size(); ++ n )
  1236. _freeBuffers( sphere->mItems[ n ] );
  1237. }
  1238. }
  1239. mData = NULL;
  1240. mDecalInstanceVec.clear();
  1241. _freePools();
  1242. }
  1243. bool DecalManager::onSceneAdd()
  1244. {
  1245. if( !Parent::onSceneAdd() )
  1246. return false;
  1247. SceneZoneSpaceManager::getZoningChangedSignal().notify( this, &DecalManager::_handleZoningChangedEvent );
  1248. return true;
  1249. }
  1250. void DecalManager::onSceneRemove()
  1251. {
  1252. SceneZoneSpaceManager::getZoningChangedSignal().remove( this, &DecalManager::_handleZoningChangedEvent );
  1253. Parent::onSceneRemove();
  1254. }
  1255. void DecalManager::_handleZoningChangedEvent( SceneZoneSpaceManager* zoneManager )
  1256. {
  1257. if( zoneManager != getSceneManager()->getZoneManager() || !getDecalDataFile() )
  1258. return;
  1259. // Clear the zoning state of all DecalSpheres in the data file.
  1260. const Vector< DecalSphere* > grid = getDecalDataFile()->getSphereList();
  1261. const U32 numSpheres = grid.size();
  1262. for( U32 i = 0; i < numSpheres; ++ i )
  1263. grid[ i ]->mZones.clear();
  1264. }
  1265. DefineEngineFunction( decalManagerSave, void, ( String decalSaveFile ), ( "" ),
  1266. "Saves the decals for the active mission in the entered filename.\n"
  1267. "@param decalSaveFile Filename to save the decals to.\n"
  1268. "@tsexample\n"
  1269. "// Set the filename to save the decals in. If no filename is set, then the\n"
  1270. "// decals will default to <activeMissionName>.mis.decals\n"
  1271. "%fileName = \"./missionDecals.mis.decals\";\n"
  1272. "// Inform the decal manager to save the decals for the active mission.\n"
  1273. "decalManagerSave( %fileName );\n"
  1274. "@endtsexample\n"
  1275. "@ingroup Decals" )
  1276. {
  1277. // If not given a file name, synthesize one.
  1278. if( decalSaveFile.isEmpty() )
  1279. {
  1280. String fileName = String::ToString( "%s.decals", Con::getVariable( "$Client::MissionFile" ) );
  1281. char fullName[ 4096 ];
  1282. Platform::makeFullPathName( fileName, fullName, sizeof( fullName ) );
  1283. decalSaveFile = String( fullName );
  1284. }
  1285. // Write the data.
  1286. gDecalManager->saveDecals( decalSaveFile );
  1287. }
  1288. DefineEngineFunction( decalManagerLoad, bool, ( const char* fileName ),,
  1289. "Clears existing decals and replaces them with decals loaded from the specified file.\n"
  1290. "@param fileName Filename to load the decals from.\n"
  1291. "@return True if the decal manager was able to load the requested file, "
  1292. "false if it could not.\n"
  1293. "@tsexample\n"
  1294. "// Set the filename to load the decals from.\n"
  1295. "%fileName = \"./missionDecals.mis.decals\";\n"
  1296. "// Inform the decal manager to load the decals from the entered filename.\n"
  1297. "decalManagerLoad( %fileName );\n"
  1298. "@endtsexample\n"
  1299. "@ingroup Decals" )
  1300. {
  1301. return gDecalManager->loadDecals( fileName );
  1302. }
  1303. DefineEngineFunction( decalManagerDirty, bool, (),,
  1304. "Returns whether the decal manager has unsaved modifications.\n"
  1305. "@return True if the decal manager has unsaved modifications, false if "
  1306. "everything has been saved.\n"
  1307. "@tsexample\n"
  1308. "// Ask the decal manager if it has unsaved modifications.\n"
  1309. "%hasUnsavedModifications = decalManagerDirty();\n"
  1310. "@endtsexample\n"
  1311. "@ingroup Decals" )
  1312. {
  1313. return gDecalManager->isDirty();
  1314. }
  1315. DefineEngineFunction( decalManagerClear, void, (),,
  1316. "Removes all decals currently loaded in the decal manager.\n"
  1317. "@tsexample\n"
  1318. "// Tell the decal manager to remove all existing decals.\n"
  1319. "decalManagerClear();\n"
  1320. "@endtsexample\n"
  1321. "@ingroup Decals" )
  1322. {
  1323. gDecalManager->clearData();
  1324. }
  1325. DefineEngineFunction( decalManagerAddDecal, S32,
  1326. ( Point3F position, Point3F normal, F32 rot, F32 scale, DecalData* decalData, bool isImmortal), ( false ),
  1327. "Adds a new decal to the decal manager.\n"
  1328. "@param position World position for the decal.\n"
  1329. "@param normal Decal normal vector (if the decal was a tire lying flat on a "
  1330. "surface, this is the vector pointing in the direction of the axle).\n"
  1331. "@param rot Angle (in radians) to rotate this decal around its normal vector.\n"
  1332. "@param scale Scale factor applied to the decal.\n"
  1333. "@param decalData DecalData datablock to use for the new decal.\n"
  1334. "@param isImmortal Whether or not this decal is immortal. If immortal, it "
  1335. "does not expire automatically and must be removed explicitly.\n"
  1336. "@return Returns the ID of the new Decal object or -1 on failure.\n"
  1337. "@tsexample\n"
  1338. "// Specify the decal position\n"
  1339. "%position = \"1.0 1.0 1.0\";\n\n"
  1340. "// Specify the up vector\n"
  1341. "%normal = \"0.0 0.0 1.0\";\n\n"
  1342. "// Add the new decal.\n"
  1343. "%decalObj = decalManagerAddDecal( %position, %normal, 0.5, 0.35, ScorchBigDecal, false );\n"
  1344. "@endtsexample\n"
  1345. "@ingroup Decals" )
  1346. {
  1347. if( !decalData )
  1348. {
  1349. Con::errorf( "decalManagerAddDecal - Invalid Decal DataBlock" );
  1350. return -1;
  1351. }
  1352. U8 flags = 0;
  1353. if( isImmortal )
  1354. flags |= PermanentDecal;
  1355. DecalInstance* inst = gDecalManager->addDecal( position, normal, rot, decalData, scale, -1, flags );
  1356. if( !inst )
  1357. {
  1358. Con::errorf( "decalManagerAddDecal - Unable to create decal instance." );
  1359. return -1;
  1360. }
  1361. // Add the decal to the instance vector.
  1362. inst->mId = gDecalManager->mDecalInstanceVec.size();
  1363. gDecalManager->mDecalInstanceVec.push_back( inst );
  1364. return inst->mId;
  1365. }
  1366. DefineEngineFunction( decalManagerRemoveDecal, bool, ( S32 decalID ),,
  1367. "Remove specified decal from the scene.\n"
  1368. "@param decalID ID of the decal to remove.\n"
  1369. "@return Returns true if successful, false if decal ID not found.\n"
  1370. "@tsexample\n"
  1371. "// Specify a decal ID to be removed\n"
  1372. "%decalID = 1;\n\n"
  1373. "// Tell the decal manager to remove the specified decal ID.\n"
  1374. "decalManagerRemoveDecal( %decalId )\n"
  1375. "@endtsexample\n"
  1376. "@ingroup Decals" )
  1377. {
  1378. DecalInstance *inst = gDecalManager->getDecal( decalID );
  1379. if( !inst )
  1380. return false;
  1381. gDecalManager->removeDecal(inst);
  1382. return true;
  1383. }