decalManager.cpp 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792
  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->getMaterialDefinition()->getId() - (*pd2)->mDataBlock->getMaterialDefinition()->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 (i < maxmin && isLeft(points[minmin], points[maxmin], points[i]) >= 0)
  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 )
  855. {
  856. diff = delta - dinst->mDataBlock->lifeSpan;
  857. dinst->mVisibility = 1.0f - (F32)diff / (F32)dinst->mDataBlock->fadeTime;
  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 deferred 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->getMaterialDefinition();
  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. 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. 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. ri->translucentSort = !currentBatch->matInst->getMaterial()->isTranslucent();
  1127. // Ugly hack for ProjectedShadow!
  1128. if ( customTex )
  1129. ri->miscTex = customTex;
  1130. // The decal bin will contain render instances for both decals and decalRoad's.
  1131. // Dynamic decals render last, then editor decals and roads in priority order.
  1132. // DefaultKey is sorted in descending order.
  1133. ri->defaultKey = currentBatch->dynamic ? 0xFFFFFFFF : (U32)currentBatch->priority;
  1134. ri->defaultKey2 = 1;//(U32)lastDecal->mDataBlock;
  1135. renderPass->addInst( ri );
  1136. }
  1137. #ifdef TORQUE_GATHER_METRICS
  1138. Con::setIntVariable( "$Decal::Batches", batches.size() );
  1139. Con::setIntVariable( "$Decal::Buffers", mPBs.size() + mPBPool.size() );
  1140. Con::setIntVariable( "$Decal::DecalsRendered", mDecalQueue.size() );
  1141. #endif
  1142. if( smDebugRender && gEditingMission )
  1143. {
  1144. ObjectRenderInst* ri = state->getRenderPass()->allocInst< ObjectRenderInst >();
  1145. ri->renderDelegate.bind( this, &DecalManager::_renderDecalSpheres );
  1146. ri->type = RenderPassManager::RIT_Editor;
  1147. ri->defaultKey = 0;
  1148. ri->defaultKey2 = 0;
  1149. state->getRenderPass()->addInst( ri );
  1150. }
  1151. }
  1152. void DecalManager::_renderDecalSpheres( ObjectRenderInst* ri, SceneRenderState* state, BaseMatInstance* overrideMat )
  1153. {
  1154. if( !mData )
  1155. return;
  1156. const Vector<DecalSphere*> &grid = mData->getSphereList();
  1157. GFXDrawUtil *drawUtil = GFX->getDrawUtil();
  1158. ColorI sphereColor( 0, 255, 0, 45 );
  1159. GFXStateBlockDesc desc;
  1160. desc.setBlend( true );
  1161. desc.setZReadWrite( true, false );
  1162. desc.setCullMode( GFXCullNone );
  1163. for ( U32 i = 0; i < grid.size(); i++ )
  1164. {
  1165. DecalSphere *decalSphere = grid[i];
  1166. const SphereF &worldSphere = decalSphere->mWorldSphere;
  1167. if( state->getCullingFrustum().isCulled( worldSphere ) )
  1168. continue;
  1169. drawUtil->drawSphere( desc, worldSphere.radius, worldSphere.center, sphereColor );
  1170. }
  1171. }
  1172. bool DecalManager::_createDataFile()
  1173. {
  1174. AssertFatal( !mData, "DecalManager::tried to create duplicate data file?" );
  1175. // We need to construct a default file name
  1176. char fileName[1024];
  1177. fileName[0] = 0;
  1178. // See if we know our current mission name
  1179. char missionName[1024];
  1180. dStrcpy( missionName, Con::getVariable( "$Client::MissionFile" ), 1024 );
  1181. char *dot = dStrstr((const char*)missionName, ".mis");
  1182. if(dot)
  1183. *dot = '\0';
  1184. dSprintf( fileName, sizeof(fileName), "%s.mis.decals", missionName );
  1185. mDataFileName = StringTable->insert( fileName );
  1186. // If the file doesn't exist, create an empty file.
  1187. if( !Torque::FS::IsFile( fileName ) )
  1188. {
  1189. FileStream stream;
  1190. if( stream.open( mDataFileName, Torque::FS::File::Write ) )
  1191. {
  1192. DecalDataFile dataFile;
  1193. dataFile.write( stream );
  1194. }
  1195. }
  1196. mData = ResourceManager::get().load( mDataFileName );
  1197. return (bool)mData;
  1198. }
  1199. void DecalManager::saveDecals( const UTF8* fileName )
  1200. {
  1201. if( !mData )
  1202. return;
  1203. // Create the file.
  1204. FileStream stream;
  1205. if ( !stream.open( fileName, Torque::FS::File::Write ) )
  1206. {
  1207. Con::errorf( "DecalManager::saveDecals - Could not open '%s' for writing!", fileName );
  1208. return;
  1209. }
  1210. // Write the data.
  1211. if( !mData->write( stream ) )
  1212. {
  1213. Con::errorf( "DecalManager::saveDecals - Failed to write '%s'", fileName );
  1214. return;
  1215. }
  1216. mDirty = false;
  1217. }
  1218. bool DecalManager::loadDecals( const UTF8 *fileName )
  1219. {
  1220. if( mData )
  1221. clearData();
  1222. mData = ResourceManager::get().load( fileName );
  1223. mDirty = false;
  1224. return mData != NULL;
  1225. }
  1226. void DecalManager::clearData()
  1227. {
  1228. mClearDataSignal.trigger();
  1229. // Free all geometry buffers.
  1230. if( mData )
  1231. {
  1232. const Vector< DecalSphere* > grid = mData->getSphereList();
  1233. for( U32 i = 0; i < grid.size(); ++ i )
  1234. {
  1235. DecalSphere* sphere = grid[ i ];
  1236. for( U32 n = 0; n < sphere->mItems.size(); ++ n )
  1237. _freeBuffers( sphere->mItems[ n ] );
  1238. }
  1239. }
  1240. mData = NULL;
  1241. mDecalInstanceVec.clear();
  1242. _freePools();
  1243. }
  1244. bool DecalManager::onSceneAdd()
  1245. {
  1246. if( !Parent::onSceneAdd() )
  1247. return false;
  1248. SceneZoneSpaceManager::getZoningChangedSignal().notify( this, &DecalManager::_handleZoningChangedEvent );
  1249. return true;
  1250. }
  1251. void DecalManager::onSceneRemove()
  1252. {
  1253. SceneZoneSpaceManager::getZoningChangedSignal().remove( this, &DecalManager::_handleZoningChangedEvent );
  1254. Parent::onSceneRemove();
  1255. }
  1256. void DecalManager::_handleZoningChangedEvent( SceneZoneSpaceManager* zoneManager )
  1257. {
  1258. if( zoneManager != getSceneManager()->getZoneManager() || !getDecalDataFile() )
  1259. return;
  1260. // Clear the zoning state of all DecalSpheres in the data file.
  1261. const Vector< DecalSphere* > grid = getDecalDataFile()->getSphereList();
  1262. const U32 numSpheres = grid.size();
  1263. for( U32 i = 0; i < numSpheres; ++ i )
  1264. grid[ i ]->mZones.clear();
  1265. }
  1266. DefineEngineFunction( decalManagerSave, void, ( String decalSaveFile ), ( "" ),
  1267. "Saves the decals for the active mission in the entered filename.\n"
  1268. "@param decalSaveFile Filename to save the decals to.\n"
  1269. "@tsexample\n"
  1270. "// Set the filename to save the decals in. If no filename is set, then the\n"
  1271. "// decals will default to <activeMissionName>.mis.decals\n"
  1272. "%fileName = \"./missionDecals.mis.decals\";\n"
  1273. "// Inform the decal manager to save the decals for the active mission.\n"
  1274. "decalManagerSave( %fileName );\n"
  1275. "@endtsexample\n"
  1276. "@ingroup Decals" )
  1277. {
  1278. // If not given a file name, synthesize one.
  1279. if( decalSaveFile.isEmpty() )
  1280. {
  1281. String fileName = String::ToString( "%s.decals", Con::getVariable( "$Client::MissionFile" ) );
  1282. char fullName[ 4096 ];
  1283. Platform::makeFullPathName( fileName, fullName, sizeof( fullName ) );
  1284. decalSaveFile = String( fullName );
  1285. }
  1286. // Write the data.
  1287. gDecalManager->saveDecals( decalSaveFile );
  1288. }
  1289. DefineEngineFunction( decalManagerLoad, bool, ( const char* fileName ),,
  1290. "Clears existing decals and replaces them with decals loaded from the specified file.\n"
  1291. "@param fileName Filename to load the decals from.\n"
  1292. "@return True if the decal manager was able to load the requested file, "
  1293. "false if it could not.\n"
  1294. "@tsexample\n"
  1295. "// Set the filename to load the decals from.\n"
  1296. "%fileName = \"./missionDecals.mis.decals\";\n"
  1297. "// Inform the decal manager to load the decals from the entered filename.\n"
  1298. "decalManagerLoad( %fileName );\n"
  1299. "@endtsexample\n"
  1300. "@ingroup Decals" )
  1301. {
  1302. return gDecalManager->loadDecals( fileName );
  1303. }
  1304. DefineEngineFunction( decalManagerDirty, bool, (),,
  1305. "Returns whether the decal manager has unsaved modifications.\n"
  1306. "@return True if the decal manager has unsaved modifications, false if "
  1307. "everything has been saved.\n"
  1308. "@tsexample\n"
  1309. "// Ask the decal manager if it has unsaved modifications.\n"
  1310. "%hasUnsavedModifications = decalManagerDirty();\n"
  1311. "@endtsexample\n"
  1312. "@ingroup Decals" )
  1313. {
  1314. return gDecalManager->isDirty();
  1315. }
  1316. DefineEngineFunction( decalManagerClear, void, (),,
  1317. "Removes all decals currently loaded in the decal manager.\n"
  1318. "@tsexample\n"
  1319. "// Tell the decal manager to remove all existing decals.\n"
  1320. "decalManagerClear();\n"
  1321. "@endtsexample\n"
  1322. "@ingroup Decals" )
  1323. {
  1324. gDecalManager->clearData();
  1325. }
  1326. DefineEngineFunction( decalManagerAddDecal, S32,
  1327. ( Point3F position, Point3F normal, F32 rot, F32 scale, DecalData* decalData, bool isImmortal), ( false ),
  1328. "Adds a new decal to the decal manager.\n"
  1329. "@param position World position for the decal.\n"
  1330. "@param normal Decal normal vector (if the decal was a tire lying flat on a "
  1331. "surface, this is the vector pointing in the direction of the axle).\n"
  1332. "@param rot Angle (in radians) to rotate this decal around its normal vector.\n"
  1333. "@param scale Scale factor applied to the decal.\n"
  1334. "@param decalData DecalData datablock to use for the new decal.\n"
  1335. "@param isImmortal Whether or not this decal is immortal. If immortal, it "
  1336. "does not expire automatically and must be removed explicitly.\n"
  1337. "@return Returns the ID of the new Decal object or -1 on failure.\n"
  1338. "@tsexample\n"
  1339. "// Specify the decal position\n"
  1340. "%position = \"1.0 1.0 1.0\";\n\n"
  1341. "// Specify the up vector\n"
  1342. "%normal = \"0.0 0.0 1.0\";\n\n"
  1343. "// Add the new decal.\n"
  1344. "%decalObj = decalManagerAddDecal( %position, %normal, 0.5, 0.35, ScorchBigDecal, false );\n"
  1345. "@endtsexample\n"
  1346. "@ingroup Decals" )
  1347. {
  1348. if( !decalData )
  1349. {
  1350. Con::errorf( "decalManagerAddDecal - Invalid Decal DataBlock" );
  1351. return -1;
  1352. }
  1353. U8 flags = 0;
  1354. if( isImmortal )
  1355. flags |= PermanentDecal;
  1356. DecalInstance* inst = gDecalManager->addDecal( position, normal, rot, decalData, scale, -1, flags );
  1357. if( !inst )
  1358. {
  1359. Con::errorf( "decalManagerAddDecal - Unable to create decal instance." );
  1360. return -1;
  1361. }
  1362. // Add the decal to the instance vector.
  1363. inst->mId = gDecalManager->mDecalInstanceVec.size();
  1364. gDecalManager->mDecalInstanceVec.push_back( inst );
  1365. return inst->mId;
  1366. }
  1367. DefineEngineFunction( decalManagerRemoveDecal, bool, ( S32 decalID ),,
  1368. "Remove specified decal from the scene.\n"
  1369. "@param decalID ID of the decal to remove.\n"
  1370. "@return Returns true if successful, false if decal ID not found.\n"
  1371. "@tsexample\n"
  1372. "// Specify a decal ID to be removed\n"
  1373. "%decalID = 1;\n\n"
  1374. "// Tell the decal manager to remove the specified decal ID.\n"
  1375. "decalManagerRemoveDecal( %decalId )\n"
  1376. "@endtsexample\n"
  1377. "@ingroup Decals" )
  1378. {
  1379. DecalInstance *inst = gDecalManager->getDecal( decalID );
  1380. if( !inst )
  1381. return false;
  1382. gDecalManager->removeDecal(inst);
  1383. return true;
  1384. }
  1385. DefineEngineFunction( decalManagerEditDecal, bool, ( S32 decalID, Point3F pos, Point3F normal, F32 rotAroundNormal, F32 decalScale ),,
  1386. "Edit specified decal of the decal manager.\n"
  1387. "@param decalID ID of the decal to edit.\n"
  1388. "@param pos World position for the decal.\n"
  1389. "@param normal Decal normal vector (if the decal was a tire lying flat on a "
  1390. "surface, this is the vector pointing in the direction of the axle).\n"
  1391. "@param rotAroundNormal Angle (in radians) to rotate this decal around its normal vector.\n"
  1392. "@param decalScale Scale factor applied to the decal.\n"
  1393. "@return Returns true if successful, false if decalID not found.\n"
  1394. "" )
  1395. {
  1396. DecalInstance *decalInstance = gDecalManager->getDecal( decalID );
  1397. if( !decalInstance )
  1398. return false;
  1399. //Internally we need Point3F tangent instead of the user friendly F32 rotAroundNormal
  1400. MatrixF mat( true );
  1401. MathUtils::getMatrixFromUpVector( normal, &mat );
  1402. AngAxisF rot( normal, rotAroundNormal );
  1403. MatrixF rotmat;
  1404. rot.setMatrix( &rotmat );
  1405. mat.mul( rotmat );
  1406. Point3F tangent;
  1407. mat.getColumn( 1, &tangent );
  1408. //if everything is unchanged just do nothing and return "everything is ok"
  1409. if ( pos.equal(decalInstance->mPosition) &&
  1410. normal.equal(decalInstance->mNormal) &&
  1411. tangent.equal(decalInstance->mTangent) &&
  1412. mFabs( decalInstance->mSize - (decalInstance->mDataBlock->size * decalScale) ) < POINT_EPSILON )
  1413. return true;
  1414. decalInstance->mPosition = pos;
  1415. decalInstance->mNormal = normal;
  1416. decalInstance->mTangent = tangent;
  1417. decalInstance->mSize = decalInstance->mDataBlock->size * decalScale;
  1418. gDecalManager->clipDecal( decalInstance, NULL, NULL);
  1419. gDecalManager->notifyDecalModified( decalInstance );
  1420. return true;
  1421. }