river.cpp 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545
  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 "environment/river.h"
  24. #include "console/consoleTypes.h"
  25. #include "console/engineAPI.h"
  26. #include "util/catmullRom.h"
  27. #include "math/util/quadTransforms.h"
  28. #include "scene/simPath.h"
  29. #include "scene/sceneRenderState.h"
  30. #include "scene/sceneManager.h"
  31. #include "materials/sceneData.h"
  32. #include "materials/baseMatInstance.h"
  33. #include "scene/sgUtil.h"
  34. #include "T3D/gameBase/gameConnection.h"
  35. #include "core/stream/bitStream.h"
  36. #include "gfx/gfxDrawUtil.h"
  37. #include "gfx/gfxTransformSaver.h"
  38. #include "gfx/primBuilder.h"
  39. #include "gfx/gfxDebugEvent.h"
  40. #include "gfx/gfxOcclusionQuery.h"
  41. #include "math/mathIO.h"
  42. #include "math/mathUtils.h"
  43. #include "math/util/frustum.h"
  44. #include "math/util/quadTransforms.h"
  45. #include "gui/3d/guiTSControl.h"
  46. #include "gfx/sim/debugDraw.h"
  47. #include "T3D/fx/particleEmitter.h"
  48. #include "scene/reflectionManager.h"
  49. #include "ts/tsShapeInstance.h"
  50. #include "postFx/postEffect.h"
  51. #include "math/util/matrixSet.h"
  52. #include "environment/nodeListManager.h"
  53. ConsoleDocClass( River,
  54. "@brief A water volume defined by a 3D spline.\n\n"
  55. "User may control width and depth per node and overall spline shape in three "
  56. "dimensions.\n\n"
  57. "%River supports dynamic planar reflections (fullReflect) like all WaterObject "
  58. "classes, but keep in mind it is not necessarily a planar surface. For best "
  59. "visual quality a %River should be less reflective the more it twists and "
  60. "bends. This caution only applies to %Rivers with fullReflect on.\n\n"
  61. "@see WaterObject for inherited functionality.\n\n"
  62. "@ingroup Water"
  63. );
  64. #define MIN_METERS_PER_SEGMENT 1.0f
  65. #define MIN_NODE_DEPTH 0.25f
  66. #define MAX_NODE_DEPTH 500.0f
  67. #define MIN_NODE_WIDTH 0.25f
  68. #define MAX_NODE_WIDTH 1000.0f
  69. #define NODE_RADIUS 15.0f
  70. static U32 gIdxArray[6][2][3] = {
  71. { { 0, 4, 5 }, { 0, 5, 1 }, }, // Top Face
  72. { { 2, 6, 4 }, { 2, 4, 0 }, }, // Left Face
  73. { { 1, 5, 7 }, { 1, 7, 3 }, }, // Right Face
  74. { { 2, 3, 7 }, { 2, 7, 6 }, }, // Bottom Face
  75. { { 0, 1, 3 }, { 0, 3, 2 }, }, // Front Face
  76. { { 4, 6, 7 }, { 4, 7, 5 }, }, // Back Face
  77. };
  78. struct RiverHitSegment
  79. {
  80. U32 idx;
  81. F32 t;
  82. };
  83. static S32 QSORT_CALLBACK compareHitSegments(const void* a,const void* b)
  84. {
  85. const RiverHitSegment *fa = (RiverHitSegment*)a;
  86. const RiverHitSegment *fb = (RiverHitSegment*)b;
  87. return mSign(fb->t - fa->t);
  88. }
  89. static Point3F sSegmentPointComparePoints[4];
  90. //-----------------------------------------------------------------------------
  91. // DecalRoadNodeList Struct
  92. //-----------------------------------------------------------------------------
  93. struct RiverNodeList : public NodeListManager::NodeList
  94. {
  95. Vector<Point3F> mPositions;
  96. Vector<F32> mWidths;
  97. Vector<F32> mDepths;
  98. Vector<VectorF> mNormals;
  99. RiverNodeList() { }
  100. virtual ~RiverNodeList() { }
  101. };
  102. //-----------------------------------------------------------------------------
  103. // RiverNodeEvent Class
  104. //-----------------------------------------------------------------------------
  105. class RiverNodeEvent : public NodeListEvent
  106. {
  107. typedef NodeListEvent Parent;
  108. public:
  109. Vector<Point3F> mPositions;
  110. Vector<F32> mWidths;
  111. Vector<F32> mDepths;
  112. Vector<VectorF> mNormals;
  113. public:
  114. RiverNodeEvent() { mNodeList = NULL; }
  115. virtual ~RiverNodeEvent() { }
  116. virtual void pack(NetConnection*, BitStream*);
  117. virtual void unpack(NetConnection*, BitStream*);
  118. virtual void copyIntoList(NodeListManager::NodeList* copyInto);
  119. virtual void padListToSize();
  120. DECLARE_CONOBJECT(RiverNodeEvent);
  121. };
  122. void RiverNodeEvent::pack(NetConnection* conn, BitStream* stream)
  123. {
  124. Parent::pack( conn, stream );
  125. stream->writeInt( mPositions.size(), 16 );
  126. for (U32 i=0; i<mPositions.size(); ++i)
  127. {
  128. mathWrite( *stream, mPositions[i] );
  129. stream->write( mWidths[i] );
  130. stream->write( mDepths[i] );
  131. mathWrite( *stream, mNormals[i] );
  132. }
  133. }
  134. void RiverNodeEvent::unpack(NetConnection* conn, BitStream* stream)
  135. {
  136. mNodeList = new RiverNodeList();
  137. Parent::unpack( conn, stream );
  138. U32 count = stream->readInt( 16 );
  139. Point3F pos;
  140. F32 width, depth;
  141. VectorF normal;
  142. RiverNodeList* list = static_cast<RiverNodeList*>(mNodeList);
  143. for (U32 i=0; i<count; ++i)
  144. {
  145. mathRead( *stream, &pos );
  146. stream->read( &width );
  147. stream->read( &depth );
  148. mathRead( *stream, &normal );
  149. list->mPositions.push_back( pos );
  150. list->mWidths.push_back( width );
  151. list->mDepths.push_back( depth );
  152. list->mNormals.push_back( normal );
  153. }
  154. list->mTotalValidNodes = count;
  155. // Do we have a complete list?
  156. if (list->mPositions.size() >= mTotalNodes)
  157. list->mListComplete = true;
  158. }
  159. void RiverNodeEvent::copyIntoList(NodeListManager::NodeList* copyInto)
  160. {
  161. RiverNodeList* prevList = dynamic_cast<RiverNodeList*>(copyInto);
  162. RiverNodeList* list = static_cast<RiverNodeList*>(mNodeList);
  163. // Merge our list with the old list.
  164. for (U32 i=mLocalListStart, index=0; i<mLocalListStart+list->mPositions.size(); ++i, ++index)
  165. {
  166. prevList->mPositions[i] = list->mPositions[index];
  167. prevList->mWidths[i] = list->mWidths[index];
  168. prevList->mDepths[i] = list->mDepths[index];
  169. prevList->mNormals[i] = list->mNormals[index];
  170. }
  171. }
  172. void RiverNodeEvent::padListToSize()
  173. {
  174. RiverNodeList* list = static_cast<RiverNodeList*>(mNodeList);
  175. U32 totalValidNodes = list->mTotalValidNodes;
  176. // Pad our list front?
  177. if (mLocalListStart)
  178. {
  179. RiverNodeList* newlist = new RiverNodeList();
  180. newlist->mPositions.increment(mLocalListStart);
  181. newlist->mWidths.increment(mLocalListStart);
  182. newlist->mDepths.increment(mLocalListStart);
  183. newlist->mNormals.increment(mLocalListStart);
  184. newlist->mPositions.merge(list->mPositions);
  185. newlist->mWidths.merge(list->mWidths);
  186. newlist->mDepths.merge(list->mDepths);
  187. newlist->mNormals.merge(list->mNormals);
  188. delete list;
  189. mNodeList = list = newlist;
  190. }
  191. // Pad our list end?
  192. if (list->mPositions.size() < mTotalNodes)
  193. {
  194. U32 delta = mTotalNodes - list->mPositions.size();
  195. list->mPositions.increment(delta);
  196. list->mWidths.increment(delta);
  197. list->mDepths.increment(delta);
  198. list->mNormals.increment(delta);
  199. }
  200. list->mTotalValidNodes = totalValidNodes;
  201. }
  202. IMPLEMENT_CO_NETEVENT_V1(RiverNodeEvent);
  203. ConsoleDocClass( RiverNodeEvent,
  204. "@brief Sends messages to the River Editor\n\n"
  205. "Editor use only.\n\n"
  206. "@internal"
  207. );
  208. //-----------------------------------------------------------------------------
  209. // RiverNodeListNotify Class
  210. //-----------------------------------------------------------------------------
  211. class RiverNodeListNotify : public NodeListNotify
  212. {
  213. typedef NodeListNotify Parent;
  214. protected:
  215. SimObjectPtr<River> mRiver;
  216. public:
  217. RiverNodeListNotify( River* river, U32 listId ) { mRiver = river; mListId = listId; }
  218. virtual ~RiverNodeListNotify() { mRiver = NULL; }
  219. virtual void sendNotification( NodeListManager::NodeList* list );
  220. };
  221. void RiverNodeListNotify::sendNotification( NodeListManager::NodeList* list )
  222. {
  223. if (mRiver.isValid())
  224. {
  225. // Build the road's nodes
  226. RiverNodeList* riverList = dynamic_cast<RiverNodeList*>( list );
  227. if (riverList)
  228. mRiver->buildNodesFromList( riverList );
  229. }
  230. }
  231. //------------------------------------------------------------------------------
  232. // Class: RiverSegment
  233. //------------------------------------------------------------------------------
  234. RiverSegment::RiverSegment()
  235. {
  236. mPlaneCount = 0;
  237. columns = 0;
  238. rows = 0;
  239. numVerts = 0;
  240. numTriangles = 0;
  241. startVert = 0;
  242. endVert = 0;
  243. startIndex = 0;
  244. endIndex = 0;
  245. slice0 = NULL;
  246. slice1 = NULL;
  247. }
  248. RiverSegment::RiverSegment( RiverSlice *rs0, RiverSlice *rs1 )
  249. {
  250. columns = 0;
  251. rows = 0;
  252. numVerts = 0;
  253. numTriangles = 0;
  254. startVert = 0;
  255. endVert = 0;
  256. startIndex = 0;
  257. endIndex = 0;
  258. slice0 = rs0;
  259. slice1 = rs1;
  260. // Calculate the planes for this segment
  261. // Will be used for intersection/buoyancy tests
  262. VectorF normal;
  263. mPlaneCount = 6;
  264. sSegmentPointCompareReference = getFaceCenter(6);
  265. // left
  266. mPlanes[0] = _getBestPlane( &slice1->p0, &slice1->pb0, &slice0->pb0, &slice0->p0 );
  267. // right
  268. mPlanes[1] = _getBestPlane( &slice0->pb2, &slice1->pb2, &slice1->p2, &slice0->p2 );
  269. // near
  270. mPlanes[2] = _getBestPlane( &slice0->pb0, &slice0->pb2, &slice0->p2, &slice0->p0 );
  271. // far
  272. mPlanes[3] = _getBestPlane( &slice1->pb2, &slice1->pb0, &slice1->p0, &slice1->p2 );
  273. // top
  274. mPlanes[4] = _getBestPlane( &slice0->p2, &slice1->p2, &slice1->p0, &slice0->p0 );
  275. // bottom
  276. mPlanes[5] = _getBestPlane( &slice0->pb2, &slice0->pb0, &slice1->pb0, &slice1->pb2 );
  277. // Calculate the bounding box(s)
  278. worldbounds.minExtents = worldbounds.maxExtents = rs0->p0;
  279. worldbounds.extend( rs0->p2 );
  280. worldbounds.extend( rs0->pb0 );
  281. worldbounds.extend( rs0->pb2 );
  282. worldbounds.extend( rs1->p0 );
  283. worldbounds.extend( rs1->p2 );
  284. worldbounds.extend( rs1->pb0 );
  285. worldbounds.extend( rs1->pb2 );
  286. /*
  287. // Calculate tetrahedrons (for collision and buoyancy testing)
  288. // This is 0 in the diagram.
  289. mCubePoints[0] = cornerPoint;
  290. mCubePoints[1] = cornerPoint + (VectorF( 1.0f, 0.0f, 0.0f ) * size );
  291. mCubePoints[2] = cornerPoint + (VectorF( 0.0f, 1.0f, 0.0f ) * size );
  292. mCubePoints[3] = cornerPoint + (VectorF( 1.0f, 1.0f, 0.0f ) * size );
  293. mCubePoints[4] = cornerPoint + (VectorF( 0.0f, 0.0f, 1.0f );
  294. mCubePoints[5] = cornerPoint + (VectorF( 1.0f, 0.0f, 1.0f );
  295. mCubePoints[6] = cornerPoint + (VectorF( 0.0f, 1.0f, 1.0f );
  296. mCubePoints[7] = cornerPoint + (VectorF( 1.0f, 1.0f, 1.0f );
  297. // Center tetra.
  298. mTetras[0].p0 = &mCubePoints[1];
  299. mTetras[0].p1 = &mCubePoints[2];
  300. mTetras[0].p2 = &mCubePoints[4];
  301. mTetras[0].p3 = &mCubePoints[7];
  302. mTetras[1].p0 = &mCubePoints[0]; // this is the tip
  303. mTetras[1].p1 = &mCubePoints[1];
  304. mTetras[1].p2 = &mCubePoints[2];
  305. mTetras[1].p3 = &mCubePoints[4];
  306. mTetras[2].p0 = &mCubePoints[3]; // tip
  307. mTetras[2].p1 = &mCubePoints[2];
  308. mTetras[2].p2 = &mCubePoints[1];
  309. mTetras[2].p3 = &mCubePoints[7];
  310. mTetras[3].p0 = &mCubePoints[6]; // tip
  311. mTetras[3].p1 = &mCubePoints[7];
  312. mTetras[3].p2 = &mCubePoints[4];
  313. mTetras[3].p3 = &mCubePoints[2];
  314. mTetras[4].p0 = &mCubePoints[5]; // tip
  315. mTetras[4].p1 = &mCubePoints[7];
  316. mTetras[4].p2 = &mCubePoints[4];
  317. mTetras[4].p3 = &mCubePoints[3];*/
  318. }
  319. void RiverSegment::set( RiverSlice *rs0, RiverSlice *rs1 )
  320. {
  321. columns = 0;
  322. rows = 0;
  323. numVerts = 0;
  324. numTriangles = 0;
  325. startVert = 0;
  326. endVert = 0;
  327. startIndex = 0;
  328. endIndex = 0;
  329. slice0 = rs0;
  330. slice1 = rs1;
  331. }
  332. static S32 QSORT_CALLBACK SegmentPointCompare(const void *aptr, const void *bptr)
  333. {
  334. const U32 a = *(const U32*)aptr;
  335. const U32 b = *(const U32*)bptr;
  336. F32 lenA = ( sSegmentPointCompareReference - sSegmentPointComparePoints[a] ).lenSquared();
  337. F32 lenB = ( sSegmentPointCompareReference - sSegmentPointComparePoints[b] ).lenSquared();
  338. return ( lenB - lenA );
  339. }
  340. PlaneF RiverSegment::_getBestPlane( const Point3F *p0, const Point3F *p1, const Point3F *p2, const Point3F *p3 )
  341. {
  342. sSegmentPointComparePoints[0] = *p0;
  343. sSegmentPointComparePoints[1] = *p1;
  344. sSegmentPointComparePoints[2] = *p2;
  345. sSegmentPointComparePoints[3] = *p3;
  346. Point3F points[4] = {
  347. *p0, *p1, *p2, *p3
  348. };
  349. U32 indices[4] = {
  350. 0,1,2,3
  351. };
  352. dQsort(indices, 4, sizeof(U32), SegmentPointCompare);
  353. // Collect the best three points (in correct winding order)
  354. // To generate the plane's normal
  355. Vector<Point3F> normalPnts;
  356. for ( U32 i = 0; i < 4; i++ )
  357. {
  358. if ( i == indices[3] )
  359. continue;
  360. normalPnts.push_back(points[i]);
  361. }
  362. PlaneF plane( normalPnts[0], normalPnts[1], normalPnts[2] );
  363. return plane;
  364. }
  365. Point3F RiverSegment::getFaceCenter( U32 faceIdx ) const
  366. {
  367. Point3F center(0,0,0);
  368. switch ( faceIdx )
  369. {
  370. case 0: // left
  371. center = slice1->p0 + slice0->p0 + slice0->pb0 + slice1->pb0;
  372. center *= 0.25f;
  373. break;
  374. case 1: // right
  375. center = slice0->p2 + slice1->p2 + slice1->pb2 + slice0->pb2;
  376. center *= 0.25f;
  377. break;
  378. case 2: // near
  379. center = slice0->p0 + slice0->p2 + slice0->pb2 + slice0->pb0;
  380. center *= 0.25f;
  381. break;
  382. case 3: // far
  383. center = slice1->pb0 + slice1->p0 + slice1->pb0 + slice1->pb2;
  384. center *= 0.25f;
  385. break;
  386. case 4: // top
  387. center = slice0->p0 + slice1->p0 + slice1->p2 + slice0->p2;
  388. center *= 0.25f;
  389. break;
  390. case 5: // bottom
  391. center = slice1->pb2 + slice1->pb0 + slice0->pb0 + slice0->pb2;
  392. center *= 0.25f;
  393. break;
  394. case 6: // segment center
  395. center = slice0->p0 + slice0->p2 + slice1->p0 + slice1->p2 + slice0->pb0 + slice0->pb2 + slice1->pb0 + slice1->pb2;
  396. center /= 8;
  397. break;
  398. }
  399. return center;
  400. }
  401. bool RiverSegment::intersectBox( const Box3F &bounds ) const
  402. {
  403. // This code copied from Frustum class.
  404. Point3F maxPoint;
  405. F32 maxDot;
  406. // Note the planes are ordered left, right, near,
  407. // far, top, bottom for getting early rejections
  408. // from the typical horizontal scene.
  409. for ( S32 i = 0; i < mPlaneCount; i++ )
  410. {
  411. // This is pretty much as optimal as you can
  412. // get for a plane vs AABB test...
  413. //
  414. // 4 comparisons
  415. // 3 multiplies
  416. // 2 adds
  417. // 1 negation
  418. //
  419. // It will early out as soon as it detects the
  420. // bounds is outside one of the planes.
  421. if ( mPlanes[i].x > 0 )
  422. maxPoint.x = bounds.maxExtents.x;
  423. else
  424. maxPoint.x = bounds.minExtents.x;
  425. if ( mPlanes[i].y > 0 )
  426. maxPoint.y = bounds.maxExtents.y;
  427. else
  428. maxPoint.y = bounds.minExtents.y;
  429. if ( mPlanes[i].z > 0 )
  430. maxPoint.z = bounds.maxExtents.z;
  431. else
  432. maxPoint.z = bounds.minExtents.z;
  433. maxDot = mDot( maxPoint, mPlanes[ i ] );
  434. if ( maxDot <= -mPlanes[ i ].d )
  435. return false;
  436. }
  437. return true;
  438. }
  439. bool RiverSegment::containsPoint( const Point3F &pnt ) const
  440. {
  441. // NOTE: this code from Frustum class.
  442. F32 maxDot;
  443. // Note the planes are ordered left, right, near,
  444. // far, top, bottom for getting early rejections
  445. // from the typical horizontal scene.
  446. for ( S32 i = 0; i < mPlaneCount; i++ )
  447. {
  448. const PlaneF &plane = mPlanes[ i ];
  449. // This is pretty much as optimal as you can
  450. // get for a plane vs point test...
  451. //
  452. // 1 comparison
  453. // 2 multiplies
  454. // 1 adds
  455. //
  456. // It will early out as soon as it detects the
  457. // point is outside one of the planes.
  458. maxDot = mDot( pnt, plane ) + plane.d;
  459. if ( maxDot < -0.1f )
  460. return false;
  461. }
  462. return true;
  463. }
  464. F32 RiverSegment::distanceToSurface(const Point3F &pnt) const
  465. {
  466. return mPlanes[4].distToPlane( pnt );
  467. }
  468. bool River::smEditorOpen = false;
  469. bool River::smWireframe = false;
  470. bool River::smShowWalls = false;
  471. bool River::smShowNodes = false;
  472. bool River::smShowSpline = true;
  473. bool River::smShowRiver = true;
  474. SimObjectPtr<SimSet> River::smServerRiverSet = NULL;
  475. IMPLEMENT_CO_NETOBJECT_V1(River);
  476. River::River()
  477. : mLowVertCount(0),
  478. mHighVertCount(0),
  479. mLowTriangleCount(0),
  480. mHighTriangleCount(0),
  481. mSegmentsPerBatch(10),
  482. mMetersPerSegment(10.0f),
  483. mDepthScale(1.0f),
  484. mFlowMagnitude(1.0f),
  485. mLodDistance( 50.0f ),
  486. mMaxDivisionSize(2.5f),
  487. mMinDivisionSize(0.25f),
  488. mColumnCount(5)
  489. {
  490. mNetFlags.set( Ghostable | ScopeAlways );
  491. mObjScale.set( 1, 1, 1 );
  492. mObjBox.minExtents.set( -0.5, -0.5, -0.5 );
  493. mObjBox.maxExtents.set( 0.5, 0.5, 0.5 );
  494. mReflectNormalUp = false;
  495. // We use the shader const miscParams.w to signify
  496. // that this object is a River.
  497. mMiscParamW = 1.0f;
  498. }
  499. River::~River()
  500. {
  501. }
  502. void River::initPersistFields()
  503. {
  504. docsURL;
  505. addGroup( "River" );
  506. addField( "SegmentLength", TypeF32, Offset( mMetersPerSegment, River ),
  507. "Divide the River lengthwise into segments of this length in meters. "
  508. "These geometric volumes are used for spacial queries like determining containment." );
  509. addField( "SubdivideLength", TypeF32, Offset( mMaxDivisionSize, River ),
  510. "For purposes of generating the renderable geometry River segments are further subdivided "
  511. "such that no quad is of greater width or length than this distance in meters." );
  512. addField( "FlowMagnitude", TypeF32, Offset( mFlowMagnitude, River ),
  513. "Magnitude of the force vector applied to dynamic objects within the River." );
  514. addField( "LowLODDistance", TypeF32, Offset( mLodDistance, River ),
  515. "Segments of the river at this distance in meters or greater will "
  516. "render as a single unsubdivided without undulation effects." );
  517. endGroup( "River" );
  518. addGroup( "Internal" );
  519. addProtectedField( "Node", TypeString, 0, &addNodeFromField, &emptyStringProtectedGetFn, "For internal use, do not modify." );
  520. endGroup( "Internal" );
  521. Parent::initPersistFields();
  522. }
  523. void River::consoleInit()
  524. {
  525. Parent::consoleInit();
  526. Con::addVariable( "$River::EditorOpen", TypeBool, &River::smEditorOpen, "For editor use.\n"
  527. "@ingroup Editors\n" );
  528. Con::addVariable( "$River::showWalls", TypeBool, &River::smShowWalls, "For editor use.\n"
  529. "@ingroup Editors\n" );
  530. Con::addVariable( "$River::showNodes", TypeBool, &River::smShowNodes, "For editor use.\n"
  531. "@ingroup Editors\n");
  532. Con::addVariable( "$River::showSpline", TypeBool, &River::smShowSpline, "For editor use.\n"
  533. "@ingroup Editors\n" );
  534. Con::addVariable( "$River::showRiver", TypeBool, &River::smShowRiver, "For editor use.\n"
  535. "@ingroup Editors\n" );
  536. Con::addVariable( "$River::showWireframe", TypeBool, &River::smWireframe, "For editor use.\n"
  537. "@ingroup Editors\n");
  538. }
  539. bool River::addNodeFromField( void *object, const char *index, const char *data )
  540. {
  541. River *pObj = static_cast<River*>(object);
  542. //if ( !pObj->isProperlyAdded() )
  543. //{
  544. F32 x,y,z,width,depth;
  545. VectorF normal;
  546. U32 result = dSscanf( data, "%f %f %f %f %f %f %f %f", &x, &y, &z, &width, &depth, &normal.x, &normal.y, &normal.z );
  547. if ( result == 8 )
  548. pObj->_addNode( Point3F(x,y,z), width, depth, normal );
  549. //}
  550. return false;
  551. }
  552. bool River::onAdd()
  553. {
  554. if ( !Parent::onAdd() )
  555. return false;
  556. // Reset the World Box.
  557. //setGlobalBounds();
  558. resetWorldBox();
  559. // Set the Render Transform.
  560. setRenderTransform(mObjToWorld);
  561. // Add to Scene.
  562. addToScene();
  563. if ( isServerObject() )
  564. getServerSet()->addObject( this );
  565. _regenerate();
  566. return true;
  567. }
  568. void River::onRemove()
  569. {
  570. removeFromScene();
  571. Parent::onRemove();
  572. }
  573. void River::inspectPostApply()
  574. {
  575. // Set Parent.
  576. Parent::inspectPostApply();
  577. if ( mMetersPerSegment < MIN_METERS_PER_SEGMENT )
  578. mMetersPerSegment = MIN_METERS_PER_SEGMENT;
  579. mMaxDivisionSize = getMax( mMaxDivisionSize, mMinDivisionSize );
  580. // Set fxPortal Mask.
  581. setMaskBits(RiverMask|RegenMask);
  582. }
  583. void River::onStaticModified( const char* slotName, const char*newValue )
  584. {
  585. Parent::onStaticModified( slotName, newValue );
  586. if ( dStricmp( slotName, "surfMaterial" ) == 0 )
  587. setMaskBits( MaterialMask );
  588. }
  589. SimSet* River::getServerSet()
  590. {
  591. if ( !smServerRiverSet )
  592. {
  593. smServerRiverSet = new SimSet();
  594. smServerRiverSet->registerObject( "ServerRiverSet" );
  595. Sim::getRootGroup()->addObject( smServerRiverSet );
  596. }
  597. return smServerRiverSet;
  598. }
  599. void River::writeFields( Stream &stream, U32 tabStop )
  600. {
  601. Parent::writeFields( stream, tabStop );
  602. // Now write all nodes
  603. stream.write(2, "\r\n");
  604. for ( U32 i = 0; i < mNodes.size(); i++ )
  605. {
  606. const RiverNode &node = mNodes[i];
  607. stream.writeTabs(tabStop);
  608. char buffer[1024];
  609. dMemset( buffer, 0, 1024 );
  610. dSprintf( buffer, 1024, "Node = \"%f %f %f %f %f %f %f %f\";", node.point.x, node.point.y, node.point.z,
  611. node.width,
  612. node.depth,
  613. node.normal.x, node.normal.y, node.normal.z );
  614. stream.writeLine( (const U8*)buffer );
  615. }
  616. }
  617. bool River::writeField( StringTableEntry fieldname, const char *value )
  618. {
  619. if ( fieldname == StringTable->insert("node") )
  620. return false;
  621. return Parent::writeField( fieldname, value );
  622. }
  623. void River::innerRender( SceneRenderState *state )
  624. {
  625. GFXDEBUGEVENT_SCOPE( River_innerRender, ColorI( 255, 0, 0 ) );
  626. PROFILE_SCOPE( River_innerRender );
  627. // Setup SceneData
  628. SceneData sgData;
  629. sgData.init( state );
  630. sgData.lights[0] = LIGHTMGR->getSpecialLight( LightManager::slSunLightType );
  631. sgData.backBuffTex = REFLECTMGR->getRefractTex();
  632. sgData.reflectTex = mPlaneReflector.reflectTex;
  633. sgData.wireframe |= smWireframe;
  634. const Point3F &camPosition = state->getCameraPosition();
  635. // set the material
  636. S32 matIdx = getMaterialIndex( camPosition );
  637. if ( !initMaterial( matIdx ) )
  638. return;
  639. BaseMatInstance *mat = mMatInstances[matIdx];
  640. WaterMatParams matParams = mMatParamHandles[matIdx];
  641. if ( !mat )
  642. return;
  643. // setup proj/world transform
  644. GFXTransformSaver saver;
  645. setShaderParams( state, mat, matParams );
  646. _makeRenderBatches( camPosition );
  647. if ( !River::smShowRiver )
  648. return;
  649. // If no material... we're done.
  650. if ( mLowLODBatches.empty() && mHighLODBatches.empty() )
  651. return;
  652. if ( !mHighLODBatches.empty() )
  653. _makeHighLODBuffers();
  654. mMatrixSet->restoreSceneViewProjection();
  655. mMatrixSet->setWorld( MatrixF::Identity );
  656. while( mat->setupPass( state, sgData ) )
  657. {
  658. mat->setSceneInfo(state, sgData);
  659. mat->setTransforms(*mMatrixSet, state);
  660. setCustomTextures( matIdx, mat->getCurPass(), matParams );
  661. GFX->setVertexBuffer( mVB_low );
  662. GFX->setPrimitiveBuffer( mPB_low );
  663. for ( U32 i = 0; i < mLowLODBatches.size(); i++ )
  664. {
  665. const RiverRenderBatch &batch = mLowLODBatches[i];
  666. U32 startVert = batch.startSegmentIdx * 2;
  667. U32 endVert = ( batch.endSegmentIdx + 1 ) * 2 + 1;
  668. U32 startIdx = batch.startSegmentIdx * 6;
  669. U32 endIdx = batch.endSegmentIdx * 6 + 5;
  670. U32 vertCount = ( endVert - startVert ) + 1;
  671. U32 idxCount = ( endIdx - startIdx ) + 1;
  672. U32 triangleCount = idxCount / 3;
  673. AssertFatal( startVert < mLowVertCount, "River, bad draw call!" );
  674. AssertFatal( startVert + vertCount <= mLowVertCount, "River, bad draw call!" );
  675. AssertFatal( triangleCount <= mLowTriangleCount, "River, bad draw call!" );
  676. GFX->drawIndexedPrimitive( GFXTriangleList, 0, startVert, vertCount, startIdx, triangleCount );
  677. }
  678. // Render all high detail batches.
  679. //
  680. // It is possible that the buffers could not be allocated because
  681. // the max number of verts/indices was exceeded. We don't want to
  682. // crash because that would be unhelpful for working in the editor.
  683. if ( mVB_high.isValid() && mPB_high.isValid() )
  684. {
  685. GFX->setVertexBuffer( mVB_high );
  686. GFX->setPrimitiveBuffer( mPB_high );
  687. for ( U32 i = 0; i < mHighLODBatches.size(); i++ )
  688. {
  689. const RiverRenderBatch &batch = mHighLODBatches[i];
  690. AssertFatal( batch.startVert < mHighVertCount, "River, bad draw call!" );
  691. AssertFatal( batch.startVert + batch.vertCount <= mHighVertCount, "River, bad draw call!" );
  692. AssertFatal( batch.triangleCount <= mHighTriangleCount, "River, bad draw call!" );
  693. AssertFatal( batch.startIndex < mHighTriangleCount * 3, "River, bad draw call!" );
  694. AssertFatal( batch.startIndex + batch.triangleCount * 3 <= mHighTriangleCount * 3, "River, bad draw call!" );
  695. GFX->drawIndexedPrimitive( GFXTriangleList,
  696. 0,
  697. 0,
  698. batch.vertCount,
  699. batch.startIndex,
  700. batch.triangleCount );
  701. }
  702. }
  703. } // while( mat->setupPass( sgData ) )
  704. }
  705. void River::updateUnderwaterEffect( SceneRenderState *state )
  706. {
  707. // Calculate mWaterPlane before calling updateUnderwaterEffect.
  708. Point3F dummy;
  709. _getWaterPlane( state->getCameraPosition(), mWaterFogData.plane, dummy );
  710. Parent::updateUnderwaterEffect( state );
  711. }
  712. void River::setShaderParams( SceneRenderState *state, BaseMatInstance* mat, const WaterMatParams& paramHandles )
  713. {
  714. // Set variables that will be assigned to shader consts within WaterCommon
  715. // before calling Parent::setShaderParams
  716. mUndulateMaxDist = mLodDistance;
  717. Parent::setShaderParams( state, mat, paramHandles );
  718. // Now set the rest of the shader consts that are either unique to this
  719. // class or that WaterObject leaves to us to handle...
  720. MaterialParameters* matParams = mat->getMaterialParameters();
  721. // set vertex shader constants
  722. //-----------------------------------
  723. matParams->setSafe(paramHandles.mGridElementSizeSC, 1.0f);
  724. if ( paramHandles.mModelMatSC->isValid() )
  725. matParams->set(paramHandles.mModelMatSC, MatrixF::Identity, GFXSCT_Float4x4);
  726. // set pixel shader constants
  727. //-----------------------------------
  728. LinearColorF c( mWaterFogData.color );
  729. matParams->setSafe(paramHandles.mBaseColorSC, c);
  730. // By default we need to show a true reflection is fullReflect is enabled and
  731. // we are above water.
  732. F32 reflect = mPlaneReflector.isEnabled() && !isUnderwater( state->getCameraPosition() );
  733. // If we were occluded the last frame a query was fetched ( not necessarily last frame )
  734. // and we weren't updated last frame... we don't have a valid texture to show
  735. // so use the cubemap / fake reflection color this frame.
  736. if ( mPlaneReflector.lastUpdateMs != REFLECTMGR->getLastUpdateMs() && mPlaneReflector.isOccluded() )
  737. reflect = false;
  738. Point4F reflectParams( mWaterPos.z, 0.0f, 1000.0f, !reflect );
  739. matParams->setSafe(paramHandles.mReflectParamsSC, reflectParams );
  740. matParams->setSafe(paramHandles.mReflectNormalSC, mPlaneReflector.refplane );
  741. }
  742. bool River::isUnderwater( const Point3F &pnt ) const
  743. {
  744. return containsPoint( pnt, NULL );
  745. }
  746. U32 River::packUpdate(NetConnection * con, U32 mask, BitStream * stream)
  747. {
  748. // Pack Parent.
  749. U32 retMask = Parent::packUpdate(con, mask, stream);
  750. if ( stream->writeFlag( mask & RiverMask ) )
  751. {
  752. // Write Object Transform.
  753. stream->writeAffineTransform(mObjToWorld);
  754. stream->write( mMetersPerSegment );
  755. stream->write( mSegmentsPerBatch );
  756. stream->write( mDepthScale );
  757. stream->write( mMaxDivisionSize );
  758. stream->write( mColumnCount );
  759. stream->write( mFlowMagnitude );
  760. stream->write( mLodDistance );
  761. }
  762. if ( stream->writeFlag( mask & NodeMask ) )
  763. {
  764. const U32 nodeByteSize = 32; // Based on sending all of a node's parameters
  765. // Test if we can fit all of our nodes within the current stream.
  766. // We make sure we leave 100 bytes still free in the stream for whatever
  767. // may follow us.
  768. S32 allowedBytes = stream->getWriteByteSize() - 100;
  769. if ( stream->writeFlag( (nodeByteSize * mNodes.size()) < allowedBytes ) )
  770. {
  771. // All nodes should fit, so send them out now.
  772. stream->writeInt( mNodes.size(), 16 );
  773. for ( U32 i = 0; i < mNodes.size(); i++ )
  774. {
  775. mathWrite( *stream, mNodes[i].point );
  776. stream->write( mNodes[i].width );
  777. stream->write( mNodes[i].depth );
  778. mathWrite( *stream, mNodes[i].normal );
  779. }
  780. }
  781. else
  782. {
  783. // There isn't enough space left in the stream for all of the
  784. // nodes. Batch them up into NetEvents.
  785. U32 id = gServerNodeListManager->nextListId();
  786. U32 count = 0;
  787. U32 index = 0;
  788. while (count < mNodes.size())
  789. {
  790. count += NodeListManager::smMaximumNodesPerEvent;
  791. if (count > mNodes.size())
  792. {
  793. count = mNodes.size();
  794. }
  795. RiverNodeEvent* event = new RiverNodeEvent();
  796. event->mId = id;
  797. event->mTotalNodes = mNodes.size();
  798. event->mLocalListStart = index;
  799. for (; index<count; ++index)
  800. {
  801. event->mPositions.push_back( mNodes[index].point );
  802. event->mWidths.push_back( mNodes[index].width );
  803. event->mDepths.push_back( mNodes[index].depth );
  804. event->mNormals.push_back( mNodes[index].normal );
  805. }
  806. con->postNetEvent( event );
  807. }
  808. stream->write( id );
  809. }
  810. }
  811. if( stream->writeFlag( mask & ( RiverMask | InitialUpdateMask ) ) )
  812. {
  813. // This is set to allow the user to modify the size of the water dynamically
  814. // in the editor
  815. mathWrite( *stream, mObjScale );
  816. stream->writeAffineTransform( mObjToWorld );
  817. }
  818. stream->writeFlag( mask & RegenMask );
  819. return retMask;
  820. }
  821. void River::unpackUpdate(NetConnection * con, BitStream * stream)
  822. {
  823. // Unpack Parent.
  824. Parent::unpackUpdate(con, stream);
  825. // RiverMask
  826. if(stream->readFlag())
  827. {
  828. MatrixF ObjectMatrix;
  829. stream->readAffineTransform(&ObjectMatrix);
  830. Parent::setTransform(ObjectMatrix);
  831. stream->read( &mMetersPerSegment );
  832. stream->read( &mSegmentsPerBatch );
  833. stream->read( &mDepthScale );
  834. stream->read( &mMaxDivisionSize );
  835. stream->read( &mColumnCount );
  836. stream->read( &mFlowMagnitude );
  837. stream->read( &mLodDistance );
  838. }
  839. // NodeMask
  840. if ( stream->readFlag() )
  841. {
  842. if (stream->readFlag())
  843. {
  844. // Nodes have been passed in this update
  845. U32 count = stream->readInt( 16 );
  846. mNodes.clear();
  847. Point3F pos;
  848. VectorF normal;
  849. F32 width,depth;
  850. for ( U32 i = 0; i < count; i++ )
  851. {
  852. mathRead( *stream, &pos );
  853. stream->read( &width );
  854. stream->read( &depth );
  855. mathRead( *stream, &normal );
  856. _addNode( pos, width, depth, normal );
  857. }
  858. }
  859. else
  860. {
  861. // Nodes will arrive as events
  862. U32 id;
  863. stream->read( &id );
  864. // Check if the road's nodes made it here before we did.
  865. NodeListManager::NodeList* list = NULL;
  866. if ( gClientNodeListManager->findListById( id, &list, true) )
  867. {
  868. // Work with the completed list
  869. RiverNodeList* riverList = dynamic_cast<RiverNodeList*>( list );
  870. if (riverList)
  871. buildNodesFromList( riverList );
  872. delete list;
  873. }
  874. else
  875. {
  876. // Nodes have not yet arrived, so register our interest in the list
  877. RiverNodeListNotify* notify = new RiverNodeListNotify( this, id );
  878. gClientNodeListManager->registerNotification( notify );
  879. }
  880. }
  881. }
  882. // RiverMask | InitialUpdateMask
  883. if( stream->readFlag() )
  884. {
  885. mathRead( *stream, &mObjScale );
  886. stream->readAffineTransform( &mObjToWorld );
  887. }
  888. // RegenMask
  889. if ( stream->readFlag() && isProperlyAdded() )
  890. regenerate();
  891. }
  892. void River::_getWaterPlane( const Point3F &camPos, PlaneF &outPlane, Point3F &outPos )
  893. {
  894. // Find the RiverSegment closest to the camera.
  895. F32 closestDist = F32_MAX;
  896. S32 closestSegment = 0;
  897. Point3F projPnt(0.0f, 0.0f, 0.0f);
  898. VectorF normal(0,0,0);
  899. for ( U32 i = 0; i < mSegments.size(); i++ )
  900. {
  901. const RiverSegment &segment = mSegments[i];
  902. const Point3F pos = MathUtils::mClosestPointOnSegment( segment.slice0->p1, segment.slice1->p1, camPos );
  903. F32 dist = ( camPos - pos ).len();
  904. if ( dist < closestDist )
  905. {
  906. closestDist = dist;
  907. closestSegment = i;
  908. projPnt = pos;
  909. }
  910. normal += segment.getSurfaceNormal();
  911. }
  912. if ( mReflectNormalUp )
  913. normal.set(0,0,1);
  914. else
  915. normal.normalizeSafe();
  916. outPos = projPnt;
  917. outPlane.set( projPnt, normal );
  918. }
  919. void River::setTransform( const MatrixF &mat )
  920. {
  921. for ( U32 i = 0; i < mNodes.size(); i++ )
  922. {
  923. mWorldToObj.mulP( mNodes[i].point );
  924. mat.mulP( mNodes[i].point );
  925. }
  926. /*
  927. // Get the amount of change in position.
  928. MatrixF oldMat = getTransform();
  929. Point3F oldPos = oldMat.getPosition();
  930. Point3F newPos = mat.getPosition();
  931. Point3F delta = newPos - oldPos;
  932. // Offset all nodes by that amount
  933. for ( U32 i = 0; i < mNodes.size(); i++ )
  934. {
  935. mNodes[i].point += delta;
  936. }
  937. // Assign the new position ( we ignore rotation )
  938. MatrixF newMat( oldMat );
  939. newMat.setPosition( newPos );
  940. */
  941. Parent::setTransform( mat );
  942. // Regenerate and update the client
  943. _regenerate();
  944. setMaskBits( NodeMask | RegenMask );
  945. }
  946. void River::setScale( const VectorF &scale )
  947. {
  948. // We ignore scale requests from the editor
  949. // right now.
  950. }
  951. bool River::castRay(const Point3F &s, const Point3F &e, RayInfo* info)
  952. {
  953. Point3F start = s;
  954. Point3F end = e;
  955. mObjToWorld.mulP(start);
  956. mObjToWorld.mulP(end);
  957. F32 out = 1.0f; // The output fraction/percentage along the line defined by s and e
  958. VectorF norm(0.0f, 0.0f, 0.0f); // The normal of the face intersected
  959. Vector<RiverHitSegment> hitSegments;
  960. for ( U32 i = 0; i < mSegments.size(); i++ )
  961. {
  962. const RiverSegment &segment = mSegments[i];
  963. F32 t;
  964. VectorF n;
  965. if ( segment.worldbounds.collideLine( start, end, &t, &n ) )
  966. {
  967. hitSegments.increment();
  968. hitSegments.last().t = t;
  969. hitSegments.last().idx = i;
  970. }
  971. }
  972. dQsort( hitSegments.address(), hitSegments.size(), sizeof(RiverHitSegment), compareHitSegments );
  973. U32 idx0, idx1, idx2;
  974. F32 t;
  975. for ( U32 i = 0; i < hitSegments.size(); i++ )
  976. {
  977. U32 segIdx = hitSegments[i].idx;
  978. const RiverSegment &segment = mSegments[segIdx];
  979. // Each segment has 6 faces
  980. for ( U32 j = 0; j < 6; j++ )
  981. {
  982. if ( j == 4 && segIdx != 0 )
  983. continue;
  984. if ( j == 5 && segIdx != mSegments.size() - 1 )
  985. continue;
  986. // Each face has 2 triangles
  987. for ( U32 k = 0; k < 2; k++ )
  988. {
  989. idx0 = gIdxArray[j][k][0];
  990. idx1 = gIdxArray[j][k][1];
  991. idx2 = gIdxArray[j][k][2];
  992. const Point3F &v0 = segment[idx0];
  993. const Point3F &v1 = segment[idx1];
  994. const Point3F &v2 = segment[idx2];
  995. if ( !MathUtils::mLineTriangleCollide( start, end,
  996. v2, v1, v0,
  997. NULL,
  998. &t ) )
  999. continue;
  1000. if ( t >= 0.0f && t < 1.0f && t < out )
  1001. {
  1002. out = t;
  1003. // optimize this, can be calculated easily within
  1004. // the collision test
  1005. norm = PlaneF( v0, v1, v2 );
  1006. }
  1007. }
  1008. }
  1009. if (out >= 0.0f && out < 1.0f)
  1010. break;
  1011. }
  1012. if (out >= 0.0f && out < 1.0f)
  1013. {
  1014. info->t = out;
  1015. info->normal = norm;
  1016. info->point.interpolate(start, end, out);
  1017. info->face = -1;
  1018. info->object = this;
  1019. return true;
  1020. }
  1021. return false;
  1022. }
  1023. bool River::collideBox(const Point3F &start, const Point3F &end, RayInfo* info)
  1024. {
  1025. return false;
  1026. }
  1027. bool River::buildPolyList( PolyListContext context, AbstractPolyList* polyList, const Box3F& box, const SphereF& sphere )
  1028. {
  1029. Vector<const RiverSegment*> hitSegments;
  1030. for ( U32 i = 0; i < mSegments.size(); i++ )
  1031. {
  1032. const RiverSegment &segment = mSegments[i];
  1033. if ( segment.worldbounds.isOverlapped( box ) )
  1034. {
  1035. hitSegments.push_back( &segment );
  1036. }
  1037. }
  1038. if ( !hitSegments.size() )
  1039. return false;
  1040. polyList->setObject( this );
  1041. polyList->setTransform( &MatrixF::Identity, Point3F( 1.0f, 1.0f, 1.0f ) );
  1042. for ( U32 i = 0; i < hitSegments.size(); i++ )
  1043. {
  1044. const RiverSegment* segment = hitSegments[i];
  1045. for ( U32 k = 0; k < 2; k++ )
  1046. {
  1047. // gIdxArray[0] gives us the top plane (see table definition).
  1048. U32 idx0 = gIdxArray[0][k][0];
  1049. U32 idx1 = gIdxArray[0][k][1];
  1050. U32 idx2 = gIdxArray[0][k][2];
  1051. const Point3F &v0 = (*segment)[idx0];
  1052. const Point3F &v1 = (*segment)[idx1];
  1053. const Point3F &v2 = (*segment)[idx2];
  1054. // Add vertices to poly list.
  1055. U32 i0 = polyList->addPoint(v0);
  1056. polyList->addPoint(v1);
  1057. polyList->addPoint(v2);
  1058. // Add plane between them.
  1059. polyList->begin(0, 0);
  1060. polyList->vertex(i0);
  1061. polyList->vertex(i0+1);
  1062. polyList->vertex(i0+2);
  1063. polyList->plane(i0, i0+1, i0+2);
  1064. polyList->end();
  1065. }
  1066. }
  1067. return true;
  1068. }
  1069. F32 River::getWaterCoverage( const Box3F &worldBox ) const
  1070. {
  1071. PROFILE_SCOPE( River_GetWaterCoverage );
  1072. if ( !mWorldBox.isOverlapped(worldBox) )
  1073. return 0.0f;
  1074. Point3F bottomPnt = worldBox.getCenter();
  1075. bottomPnt.z = worldBox.minExtents.z;
  1076. F32 farthest = 0.0f;
  1077. for ( U32 i = 0; i < mSegments.size(); i++ )
  1078. {
  1079. const RiverSegment &segment = mSegments[i];
  1080. if ( !segment.worldbounds.isOverlapped(worldBox) )
  1081. continue;
  1082. if ( !segment.intersectBox( worldBox ) )
  1083. continue;
  1084. F32 distance = segment.distanceToSurface( bottomPnt );
  1085. if ( distance > farthest )
  1086. farthest = distance;
  1087. }
  1088. F32 height = worldBox.maxExtents.z - worldBox.minExtents.z;
  1089. F32 distance = mClampF( farthest, 0.0f, height );
  1090. F32 coverage = distance / height;
  1091. return coverage;
  1092. }
  1093. F32 River::getSurfaceHeight( const Point2F &pos ) const
  1094. {
  1095. PROFILE_SCOPE( River_GetSurfaceHeight );
  1096. Point3F origin( pos.x, pos.y, mWorldBox.maxExtents.z );
  1097. Point3F direction(0,0,-1);
  1098. U32 nodeIdx;
  1099. Point3F collisionPnt;
  1100. if ( !collideRay( origin, direction, &nodeIdx, &collisionPnt ) )
  1101. return -1.0f;
  1102. return collisionPnt.z;
  1103. }
  1104. VectorF River::getFlow( const Point3F &pos ) const
  1105. {
  1106. PROFILE_SCOPE( River_GetFlow );
  1107. for ( U32 i = 0; i < mSegments.size(); i++ )
  1108. {
  1109. const RiverSegment &segment = mSegments[i];
  1110. if ( !segment.containsPoint(pos) )
  1111. continue;
  1112. VectorF flow = segment.slice0->p1 - segment.slice1->p1;
  1113. flow.normalize();
  1114. flow *= mFlowMagnitude;
  1115. return flow;
  1116. }
  1117. return VectorF::Zero;
  1118. }
  1119. void River::onReflectionInfoChanged()
  1120. {
  1121. /*
  1122. if ( isClientObject() && GFX->getPixelShaderVersion() >= 1.4 )
  1123. {
  1124. if ( mFullReflect )
  1125. REFLECTMGR->registerObject( this, ReflectDelegate( this, &River::updateReflection ), mReflectPriority, mReflectMaxRateMs, mReflectMaxDist );
  1126. else
  1127. {
  1128. REFLECTMGR->unregisterObject( this );
  1129. mReflectTex = NULL;
  1130. }
  1131. }
  1132. */
  1133. }
  1134. void River::_regenerate()
  1135. {
  1136. if ( mNodes.size() == 0 )
  1137. return;
  1138. const Point3F &nodePt = mNodes.first().point;
  1139. MatrixF mat( true );
  1140. mat.setPosition( nodePt );
  1141. Parent::setTransform( mat );
  1142. _generateSlices();
  1143. }
  1144. void River::_generateSlices()
  1145. {
  1146. if ( mNodes.size() < 2 )
  1147. return;
  1148. U32 nodeCount = mNodes.size();
  1149. RiverSplineNode *splineNodes = new RiverSplineNode[nodeCount];
  1150. for ( U32 i = 0; i < nodeCount; i++ )
  1151. {
  1152. const RiverNode &node = mNodes[i];
  1153. splineNodes[i].x = node.point.x;
  1154. splineNodes[i].y = node.point.y;
  1155. splineNodes[i].z = node.point.z;
  1156. splineNodes[i].width = node.width;
  1157. splineNodes[i].depth = node.depth;
  1158. splineNodes[i].normal = node.normal;
  1159. }
  1160. CatmullRom<RiverSplineNode> spline;
  1161. spline.initialize( nodeCount, splineNodes );
  1162. delete [] splineNodes;
  1163. mSlices.clear();
  1164. for ( U32 i = 1; i < nodeCount; i++ )
  1165. {
  1166. F32 t0 = spline.getTime( i-1 );
  1167. F32 t1 = spline.getTime( i );
  1168. F32 segLength = spline.arcLength( t0, t1 );
  1169. U32 numSegments = mCeil( segLength / mMetersPerSegment );
  1170. numSegments = getMax( numSegments, (U32)1 );
  1171. F32 tstep = ( t1 - t0 ) / numSegments;
  1172. //AssertFatal( numSegments > 0, "River::_generateSlices, got zero segments!" );
  1173. U32 startIdx = 0;
  1174. U32 endIdx = ( i == nodeCount - 1 ) ? numSegments + 1 : numSegments;
  1175. for ( U32 j = startIdx; j < endIdx; j++ )
  1176. {
  1177. F32 t = t0 + tstep * j; //spline.findParameterByDistance( 0.0f, i * segLen );
  1178. RiverSplineNode val = spline.evaluate(t);
  1179. RiverSlice slice;
  1180. slice.p1.set( val.x, val.y, val.z );
  1181. slice.uvec.set( 0,0,1 );
  1182. slice.width = val.width;
  1183. slice.depth = val.depth;
  1184. slice.parentNodeIdx = i-1;
  1185. slice.normal = val.normal;
  1186. slice.normal.normalize();
  1187. mSlices.push_back( slice );
  1188. }
  1189. }
  1190. //
  1191. // Calculate fvec and rvec for all slices
  1192. //
  1193. RiverSlice *pSlice = NULL;
  1194. RiverSlice *pNextSlice = NULL;
  1195. // Must do the first slice outside the loop
  1196. {
  1197. pSlice = &mSlices[0];
  1198. pNextSlice = &mSlices[1];
  1199. pSlice->fvec = pNextSlice->p1 - pSlice->p1;
  1200. pSlice->fvec.normalize();
  1201. pSlice->rvec = mCross( pSlice->fvec, pSlice->normal );
  1202. pSlice->rvec.normalize();
  1203. pSlice->uvec = mCross( pSlice->rvec, pSlice->fvec );
  1204. pSlice->uvec.normalize();
  1205. pSlice->rvec = mCross( pSlice->fvec, pSlice->uvec );
  1206. pSlice->rvec.normalize();
  1207. }
  1208. for ( U32 i = 1; i < mSlices.size() - 1; i++ )
  1209. {
  1210. pSlice = &mSlices[i];
  1211. pNextSlice = &mSlices[i+1];
  1212. pSlice->fvec = pNextSlice->p1 - pSlice->p1;
  1213. pSlice->fvec.normalize();
  1214. pSlice->rvec = mCross( pSlice->fvec, pSlice->normal );
  1215. pSlice->rvec.normalize();
  1216. pSlice->uvec = mCross( pSlice->rvec, pSlice->fvec );
  1217. pSlice->uvec.normalize();
  1218. pSlice->rvec = mCross( pSlice->fvec, pSlice->uvec );
  1219. pSlice->rvec.normalize();
  1220. }
  1221. // Must do the last slice outside the loop
  1222. {
  1223. RiverSlice *lastSlice = &mSlices[mSlices.size()-1];
  1224. RiverSlice *prevSlice = &mSlices[mSlices.size()-2];
  1225. lastSlice->fvec = prevSlice->fvec;
  1226. lastSlice->rvec = mCross( lastSlice->fvec, lastSlice->normal );
  1227. lastSlice->rvec.normalize();
  1228. lastSlice->uvec = mCross( lastSlice->rvec, lastSlice->fvec );
  1229. lastSlice->uvec.normalize();
  1230. lastSlice->rvec = mCross( lastSlice->fvec, lastSlice->uvec );
  1231. lastSlice->rvec.normalize();
  1232. }
  1233. //
  1234. // Calculate p0/p2/pb0/pb2 for all slices
  1235. //
  1236. for ( U32 i = 0; i < mSlices.size(); i++ )
  1237. {
  1238. RiverSlice *slice = &mSlices[i];
  1239. slice->p0 = slice->p1 - slice->rvec * slice->width * 0.5f;
  1240. slice->p2 = slice->p1 + slice->rvec * slice->width * 0.5f;
  1241. slice->pb0 = slice->p0 - slice->uvec * slice->depth;
  1242. slice->pb2 = slice->p2 - slice->uvec * slice->depth;
  1243. }
  1244. // Generate the object/world bounds
  1245. Box3F box;
  1246. for ( U32 i = 0; i < mSlices.size(); i++ )
  1247. {
  1248. const RiverSlice &slice = mSlices[i];
  1249. if ( i == 0 )
  1250. {
  1251. box.minExtents = slice.p0;
  1252. box.maxExtents = slice.p2;
  1253. box.extend( slice.pb0 );
  1254. box.extend( slice.pb2 );
  1255. }
  1256. else
  1257. {
  1258. box.extend( slice.p0 );
  1259. box.extend( slice.p2 );
  1260. box.extend( slice.pb0 );
  1261. box.extend( slice.pb2 );
  1262. }
  1263. }
  1264. mWorldBox = box;
  1265. //mObjBox.minExtents -= pos;
  1266. //mObjBox.maxExtents -= pos;
  1267. resetObjectBox();
  1268. // Make sure we are in the correct bins given our world box.
  1269. if( getSceneManager() != NULL )
  1270. getSceneManager()->notifyObjectDirty( this );
  1271. _generateSegments();
  1272. }
  1273. void River::_generateSegments()
  1274. {
  1275. mSegments.clear();
  1276. for ( U32 i = 0; i < mSlices.size() - 1; i++ )
  1277. {
  1278. RiverSegment seg( &mSlices[i], &mSlices[i+1] );
  1279. mSegments.push_back( seg );
  1280. }
  1281. /*
  1282. #ifdef TORQUE_DEBUG
  1283. for ( U32 i = 0; i < mSegments.size(); i++ )
  1284. {
  1285. const RiverSegment &segment = mSegments[i];
  1286. PlaneF normal0 = MathUtils::mTriangleNormal( segment.slice0->p0, segment.slice1->p0, segment.slice1->p2 );
  1287. PlaneF normal1 = MathUtils::mTriangleNormal( segment.slice0->p0, segment.slice1->p2, segment.slice0->p2 );
  1288. AssertFatal( true || normal0 != normal1, "River::generateSegments, segment is not coplanar!" );
  1289. }
  1290. #endif // TORQUE_DEBUG
  1291. */
  1292. // We have to go back and generate normals for each slice
  1293. // to be used in calculation of the reflect plane.
  1294. // The slice-normal we calculate are relative to the surface normal
  1295. // of the segments adjacent to the slice.
  1296. /*
  1297. if ( mSlices.size() >= 2 )
  1298. {
  1299. mSlices[0].normal = mSegments[0].getSurfaceNormal();
  1300. for ( U32 i = 1; i < mSlices.size() - 1; i++ )
  1301. {
  1302. mSlices[i].normal = ( mSegments[i-1].getSurfaceNormal() + mSegments[i].getSurfaceNormal() ) / 2;
  1303. }
  1304. mSlices.last().normal = mSegments.last().getSurfaceNormal();
  1305. }
  1306. */
  1307. _generateVerts();
  1308. }
  1309. void River::_generateVerts()
  1310. {
  1311. if ( isServerObject() )
  1312. return;
  1313. // These will depend on the level of subdivision per segment
  1314. // calculated below.
  1315. mHighVertCount = 0;
  1316. mHighTriangleCount = 0;
  1317. // Calculate the number of row/column subdivisions per each
  1318. // RiverSegment.
  1319. F32 greatestWidth = 0.1f;
  1320. for ( U32 i = 0; i < mNodes.size(); i++ )
  1321. {
  1322. RiverNode &node = mNodes[i];
  1323. if ( node.width > greatestWidth )
  1324. greatestWidth = node.width;
  1325. }
  1326. mColumnCount = mCeil( greatestWidth / mMaxDivisionSize );
  1327. for ( U32 i = 0; i < mSegments.size(); i++ )
  1328. {
  1329. RiverSegment &segment = mSegments[i];
  1330. const RiverSlice *slice = segment.slice0;
  1331. const RiverSlice *nextSlice = segment.slice1;
  1332. // Calculate the size of divisions in the forward direction ( p00 -> p01 )
  1333. F32 segLength = (nextSlice->p1 - slice->p1).len();
  1334. // A division count of one is actually NO subdivision,
  1335. // the segment corners are the only verts in this segment.
  1336. U32 numRows = 1;
  1337. if ( segLength > 0.0f )
  1338. numRows = mCeil( segLength / mMaxDivisionSize );
  1339. // The problem with calculating num columns per segment is
  1340. // two adjacent - high lod segments of different width can have
  1341. // verts that don't line up! So even though RiverSegment HAS a
  1342. // column data member we initialize all segments in the river to
  1343. // the same (River::mColumnCount)
  1344. // Calculate the size of divisions in the right direction ( p00 -> p10 )
  1345. // F32 segWidth = ( ( p11 - p01 ).len() + ( p10 - p00 ).len() ) * 0.5f;
  1346. // U32 numColumns = 5;
  1347. //F32 columnSize = segWidth / numColumns;
  1348. //while ( columnSize > mMaxDivisionSize )
  1349. //{
  1350. // numColumns++;
  1351. // columnSize = segWidth / numColumns;
  1352. //}
  1353. // Save the calculated numb of columns / rows for this segment.
  1354. segment.columns = mColumnCount;
  1355. segment.rows = numRows;
  1356. // Save the corresponding number of verts/prims
  1357. segment.numVerts = ( 1 + mColumnCount ) * ( 1 + numRows );
  1358. segment.numTriangles = mColumnCount * numRows * 2;
  1359. mHighVertCount += segment.numVerts;
  1360. mHighTriangleCount += segment.numTriangles;
  1361. }
  1362. // Number of low detail verts/prims.
  1363. mLowVertCount = mSlices.size() * 2;
  1364. mLowTriangleCount = mSegments.size() * 2;
  1365. // Allocate the low detail VertexBuffer,
  1366. // this will stay in memory and will never need to change.
  1367. mVB_low.set( GFX, mLowVertCount, GFXBufferTypeStatic );
  1368. GFXWaterVertex *lowVertPtr = mVB_low.lock();
  1369. U32 vertCounter = 0;
  1370. // The texCoord.y value start/end for a segment
  1371. // as we loop through them.
  1372. F32 textCoordV = 0;
  1373. //
  1374. // Fill the low-detail VertexBuffer
  1375. //
  1376. for ( U32 i = 0; i < mSlices.size(); i++ )
  1377. {
  1378. RiverSlice &slice = mSlices[i];
  1379. lowVertPtr->point = slice.p0;
  1380. lowVertPtr->normal = slice.normal;
  1381. lowVertPtr->undulateData.set( -slice.width*0.5f, textCoordV );
  1382. lowVertPtr->horizonFactor.set( 0, 0, 0, 0 );
  1383. lowVertPtr++;
  1384. vertCounter++;
  1385. lowVertPtr->point = slice.p2;
  1386. lowVertPtr->normal = slice.normal;
  1387. lowVertPtr->undulateData.set( slice.width*0.5f, textCoordV );
  1388. lowVertPtr->horizonFactor.set( 0, 0, 0, 0 );
  1389. lowVertPtr++;
  1390. vertCounter++;
  1391. // Save this so we can get it later.
  1392. slice.texCoordV = textCoordV;
  1393. if ( i < mSlices.size() - 1 )
  1394. {
  1395. // Increment the textCoordV for the next slice.
  1396. F32 segLen = ( mSlices[i+1].p1 - slice.p1 ).len();
  1397. textCoordV += segLen;
  1398. }
  1399. }
  1400. AssertFatal( vertCounter == mLowVertCount, "River, wrote incorrect number of verts in mBV_low!" );
  1401. // Unlock the low-detail VertexBuffer, we are done filling it.
  1402. mVB_low.unlock();
  1403. //
  1404. // Create the low-detail prim buffer(s)
  1405. //
  1406. mPB_low.set( GFX, mLowTriangleCount * 3, mLowTriangleCount, GFXBufferTypeStatic );
  1407. U16 *lowIdxBuff;
  1408. mPB_low.lock(&lowIdxBuff);
  1409. U32 curLowIdx = 0;
  1410. // Temporaries to hold indices for the corner points of a quad.
  1411. U32 p00, p01, p11, p10;
  1412. U32 offset = 0;
  1413. // Fill the low-detail PrimitiveBuffer
  1414. for ( U32 i = 0; i < mSegments.size(); i++ )
  1415. {
  1416. //const RiverSegment &segment = mSegments[i];
  1417. // Two triangles formed by the corner points of this segment
  1418. // into the the low detail primitive buffer.
  1419. p00 = offset;
  1420. p01 = p00 + 2;
  1421. p11 = p01 + 1;
  1422. p10 = p00 + 1;
  1423. // Upper-Left triangle
  1424. lowIdxBuff[curLowIdx] = p00;
  1425. curLowIdx++;
  1426. lowIdxBuff[curLowIdx] = p01;
  1427. curLowIdx++;
  1428. lowIdxBuff[curLowIdx] = p11;
  1429. curLowIdx++;
  1430. // Lower-Right Triangle
  1431. lowIdxBuff[curLowIdx] = p00;
  1432. curLowIdx++;
  1433. lowIdxBuff[curLowIdx] = p11;
  1434. curLowIdx++;
  1435. lowIdxBuff[curLowIdx] = p10;
  1436. curLowIdx++;
  1437. offset += 2;
  1438. }
  1439. AssertFatal( curLowIdx == mLowTriangleCount * 3, "River, wrote incorrect number of indices in mPB_low!" );
  1440. // Unlock the low-detail PrimitiveBuffer, we are done filling it.
  1441. mPB_low.unlock();
  1442. }
  1443. bool River::getClosestNode( const Point3F &pos, U32 &idx ) const
  1444. {
  1445. F32 closestDist = F32_MAX;
  1446. for ( U32 i = 0; i < mNodes.size(); i++ )
  1447. {
  1448. F32 dist = ( mNodes[i].point - pos ).len();
  1449. if ( dist < closestDist )
  1450. {
  1451. closestDist = dist;
  1452. idx = i;
  1453. }
  1454. }
  1455. return closestDist != F32_MAX;
  1456. }
  1457. bool River::containsPoint( const Point3F &worldPos, U32 *nodeIdx ) const
  1458. {
  1459. // If point isn't in the world box,
  1460. // it's definitely not in the River.
  1461. //if ( !getWorldBox().isContained( worldPos ) )
  1462. // return false;
  1463. // Look through all edges, does the polygon
  1464. // formed from adjacent edge's contain the worldPos?
  1465. for ( U32 i = 0; i < mSegments.size(); i++ )
  1466. {
  1467. const RiverSegment &segment = mSegments[i];
  1468. if ( segment.containsPoint( worldPos ) )
  1469. {
  1470. if ( nodeIdx )
  1471. *nodeIdx = i;
  1472. return true;
  1473. }
  1474. }
  1475. return false;
  1476. }
  1477. F32 River::distanceToSurface( const Point3F &pnt, U32 segmentIdx )
  1478. {
  1479. return mSegments[segmentIdx].distanceToSurface( pnt );
  1480. }
  1481. bool River::collideRay( const Point3F &origin, const Point3F &direction, U32 *nodeIdx, Point3F *collisionPnt ) const
  1482. {
  1483. Point3F p0 = origin;
  1484. Point3F p1 = origin + direction * 2000.0f;
  1485. // If the line segment does not collide with the river's world box,
  1486. // it definitely does not collide with any part of the river.
  1487. if ( !getWorldBox().collideLine( p0, p1 ) )
  1488. return false;
  1489. if ( mSlices.size() < 2 )
  1490. return false;
  1491. MathUtils::Quad quad;
  1492. MathUtils::Ray ray;
  1493. F32 t;
  1494. // Check each river segment (formed by a pair of slices) for collision
  1495. // with the line segment.
  1496. for ( U32 i = 0; i < mSlices.size() - 1; i++ )
  1497. {
  1498. const RiverSlice &slice0 = mSlices[i];
  1499. const RiverSlice &slice1 = mSlices[i+1];
  1500. // For simplicities sake we will only test for collision between the
  1501. // line segment and the Top face of the river segment.
  1502. // Clockwise starting with the leftmost/closest point.
  1503. quad.p00 = slice0.p0;
  1504. quad.p01 = slice1.p0;
  1505. quad.p11 = slice1.p2;
  1506. quad.p10 = slice0.p2;
  1507. ray.origin = origin;
  1508. ray.direction = direction;
  1509. // NOTE:
  1510. // mRayQuadCollide is designed for a "real" quad in which all four points
  1511. // are coplanar which is actually not the case here. The more twist
  1512. // and turn in-between two neighboring river slices the more incorrect
  1513. // this calculation will be.
  1514. if ( MathUtils::mRayQuadCollide( quad, ray, NULL, &t ) )
  1515. {
  1516. if ( nodeIdx )
  1517. *nodeIdx = slice0.parentNodeIdx;
  1518. if ( collisionPnt )
  1519. *collisionPnt = ray.origin + ray.direction * t;
  1520. return true;
  1521. }
  1522. }
  1523. return false;
  1524. }
  1525. Point3F River::getNodePosition( U32 idx ) const
  1526. {
  1527. if ( mNodes.size() - 1 < idx )
  1528. return Point3F();
  1529. return mNodes[idx].point;
  1530. }
  1531. void River::setNodePosition( U32 idx, const Point3F &pos )
  1532. {
  1533. if ( mNodes.size() - 1 < idx )
  1534. return;
  1535. mNodes[idx].point = pos;
  1536. regenerate();
  1537. setMaskBits( NodeMask | RegenMask );
  1538. }
  1539. U32 River::addNode( const Point3F &pos, const F32 &width, const F32 &depth, const VectorF &normal )
  1540. {
  1541. U32 idx = _addNode( pos, width, depth, normal );
  1542. regenerate();
  1543. setMaskBits( NodeMask | RegenMask );
  1544. return idx;
  1545. }
  1546. U32 River::insertNode(const Point3F &pos, const F32 &width, const F32 &depth, const VectorF &normal, const U32 &idx)
  1547. {
  1548. U32 ret = _insertNode( pos, width, depth, normal, idx );
  1549. regenerate();
  1550. setMaskBits( NodeMask | RegenMask );
  1551. return ret;
  1552. }
  1553. void River::setNode(const Point3F &pos, const F32 &width, const F32 &depth, const VectorF &normal, const U32 &idx)
  1554. {
  1555. if ( mNodes.size() - 1 < idx )
  1556. return;
  1557. RiverNode &node = mNodes[idx];
  1558. node.point = pos;
  1559. node.width = width;
  1560. node.depth = depth;
  1561. node.normal = normal;
  1562. regenerate();
  1563. setMaskBits( NodeMask | RegenMask );
  1564. }
  1565. void River::setNodeWidth( U32 idx, F32 meters )
  1566. {
  1567. meters = mClampF( meters, MIN_NODE_WIDTH, MAX_NODE_WIDTH );
  1568. if ( mNodes.size() - 1 < idx )
  1569. return;
  1570. mNodes[idx].width = meters;
  1571. _regenerate();
  1572. setMaskBits( RegenMask | NodeMask );
  1573. }
  1574. void River::setNodeHeight( U32 idx, F32 height )
  1575. {
  1576. if ( mNodes.size() - 1 < idx )
  1577. return;
  1578. mNodes[idx].point.z = height;
  1579. _regenerate();
  1580. setMaskBits( RegenMask | NodeMask );
  1581. }
  1582. F32 River::getNodeWidth( U32 idx ) const
  1583. {
  1584. if ( mNodes.size() - 1 < idx )
  1585. return -1.0f;
  1586. return mNodes[idx].width;
  1587. }
  1588. void River::setNodeDepth( U32 idx, F32 meters )
  1589. {
  1590. meters = mClampF( meters, MIN_NODE_DEPTH, MAX_NODE_DEPTH );
  1591. if ( mNodes.size() - 1 < idx )
  1592. return;
  1593. mNodes[idx].depth = meters;
  1594. _regenerate();
  1595. setMaskBits( RiverMask | RegenMask | NodeMask );
  1596. }
  1597. void River::setNodeNormal( U32 idx, const VectorF &normal )
  1598. {
  1599. if ( mNodes.size() - 1 < idx )
  1600. return;
  1601. mNodes[idx].normal = normal;
  1602. regenerate();
  1603. setMaskBits( NodeMask | RegenMask );
  1604. }
  1605. F32 River::getNodeDepth( U32 idx ) const
  1606. {
  1607. if ( mNodes.size() - 1 < idx )
  1608. return -1.0f;
  1609. return mNodes[idx].depth;
  1610. }
  1611. VectorF River::getNodeNormal( U32 idx ) const
  1612. {
  1613. if ( mNodes.size() - 1 < idx )
  1614. return VectorF::Zero;
  1615. return mNodes[idx].normal;
  1616. }
  1617. MatrixF River::getNodeTransform( U32 idx ) const
  1618. {
  1619. MatrixF mat(true);
  1620. if ( mNodes.size() - 1 < idx )
  1621. return mat;
  1622. bool hasNext = idx + 1 < mNodes.size();
  1623. bool hasPrev = (S32)idx - 1 >= 0;
  1624. const RiverNode &node = mNodes[idx];
  1625. VectorF fvec( 0, 1, 0 );
  1626. if ( hasNext )
  1627. {
  1628. fvec = mNodes[idx+1].point - node.point;
  1629. fvec.normalizeSafe();
  1630. }
  1631. else if ( hasPrev )
  1632. {
  1633. fvec = node.point - mNodes[idx-1].point;
  1634. fvec.normalizeSafe();
  1635. }
  1636. else
  1637. fvec = mPerp( node.normal );
  1638. if ( fvec.isZero() )
  1639. fvec = mPerp( node.normal );
  1640. F32 dot = mDot( fvec, node.normal );
  1641. if ( dot < -0.9f || dot > 0.9f )
  1642. fvec = mPerp( node.normal );
  1643. VectorF rvec = mCross( fvec, node.normal );
  1644. if ( rvec.isZero() )
  1645. rvec = mPerp( fvec );
  1646. rvec.normalize();
  1647. fvec = mCross( node.normal, rvec );
  1648. fvec.normalize();
  1649. mat.setColumn( 0, rvec );
  1650. mat.setColumn( 1, fvec );
  1651. mat.setColumn( 2, node.normal );
  1652. mat.setColumn( 3, node.point );
  1653. AssertFatal( m_matF_determinant( mat ) != 0.0f, "no inverse!");
  1654. return mat;
  1655. }
  1656. void River::deleteNode( U32 idx )
  1657. {
  1658. if ( mNodes.size() - 1 < idx )
  1659. return;
  1660. mNodes.erase(idx);
  1661. _regenerate();
  1662. setMaskBits( RegenMask | NodeMask );
  1663. }
  1664. void River::buildNodesFromList( RiverNodeList* list )
  1665. {
  1666. mNodes.clear();
  1667. for (U32 i=0; i<list->mPositions.size(); ++i)
  1668. {
  1669. _addNode( list->mPositions[i], list->mWidths[i], list->mDepths[i], list->mNormals[i] );
  1670. }
  1671. _regenerate();
  1672. }
  1673. void River::_makeRenderBatches( const Point3F &cameraPos )
  1674. {
  1675. // Loop through each segment to determine if it is either 1 [not visible], 2 [high LOD], 3 [low LOD]
  1676. mHighLODBatches.clear();
  1677. mLowLODBatches.clear();
  1678. // Keeps track of what we batch type we are currently collecting.
  1679. // -1 is uninitialized, 0 is low detail, 1 is high detail
  1680. S32 lastDetail = -1;
  1681. bool highDetail;
  1682. U32 startSegmentIdx = -1;
  1683. U32 endSegmentIdx = 0;
  1684. F32 lodDistSquared = mLodDistance * mLodDistance;
  1685. for ( U32 i = 0; i < mSegments.size(); i++ )
  1686. {
  1687. const RiverSegment &segment = mSegments[i];
  1688. const RiverSlice *slice = segment.slice0;
  1689. const RiverSlice *nextSlice = segment.slice1;
  1690. // TODO: add bounds BoxF to RiverSegment
  1691. const bool isVisible = true; //frustum.intersects( segment.bounds );
  1692. if ( isVisible )
  1693. {
  1694. F32 dist0 = MathUtils::mTriangleDistance( slice->p0, nextSlice->p0, nextSlice->p2, cameraPos );
  1695. F32 dist1 = MathUtils::mTriangleDistance( slice->p0, nextSlice->p2, slice->p2, cameraPos );
  1696. F32 dist = getMin( dist0, dist1 );
  1697. highDetail = ( dist < lodDistSquared );
  1698. if ( (highDetail && lastDetail == 0) ||
  1699. (!highDetail && lastDetail == 1) )
  1700. {
  1701. // We hit a segment with a different lod than the previous.
  1702. // Save what we have so far...
  1703. RiverRenderBatch batch;
  1704. batch.startSegmentIdx = startSegmentIdx;
  1705. batch.endSegmentIdx = endSegmentIdx;
  1706. if ( lastDetail == 0 )
  1707. {
  1708. mLowLODBatches.push_back( batch );
  1709. }
  1710. else
  1711. {
  1712. mHighLODBatches.push_back( batch );
  1713. }
  1714. // Reset the batching
  1715. startSegmentIdx = -1;
  1716. lastDetail = -1;
  1717. i--;
  1718. continue;
  1719. }
  1720. // If this is the start of a set of batches.
  1721. if ( startSegmentIdx == -1 )
  1722. {
  1723. endSegmentIdx = startSegmentIdx = i;
  1724. lastDetail = ( highDetail ) ? 1 : 0;
  1725. }
  1726. // Else we're extending the end batch index.
  1727. else
  1728. ++endSegmentIdx;
  1729. // If this isn't the last batch then continue.
  1730. if ( i < mSegments.size()-1 )
  1731. continue;
  1732. }
  1733. // If we still don't have a start batch skip.
  1734. if ( startSegmentIdx == -1 )
  1735. continue;
  1736. // Save what we have so far...
  1737. RiverRenderBatch batch;
  1738. batch.startSegmentIdx = startSegmentIdx;
  1739. batch.endSegmentIdx = endSegmentIdx;
  1740. if ( lastDetail == 0 )
  1741. {
  1742. mLowLODBatches.push_back( batch );
  1743. }
  1744. else
  1745. {
  1746. mHighLODBatches.push_back( batch );
  1747. }
  1748. // Reset the batching.
  1749. startSegmentIdx = -1;
  1750. lastDetail = -1;
  1751. }
  1752. }
  1753. void River::_makeHighLODBuffers()
  1754. {
  1755. PROFILE_SCOPE( River_makeHighLODBuffers );
  1756. // This is the number of verts/triangles for ALL high lod batches combined.
  1757. // eg. the size for the buffers.
  1758. U32 numVerts = 0;
  1759. U32 numTriangles = 0;
  1760. for ( U32 i = 0; i < mHighLODBatches.size(); i++ )
  1761. {
  1762. RiverRenderBatch &batch = mHighLODBatches[i];
  1763. for ( U32 j = batch.startSegmentIdx; j <= batch.endSegmentIdx; j++ )
  1764. {
  1765. const RiverSegment &segment = mSegments[j];
  1766. numTriangles += segment.numTriangles;
  1767. numVerts += segment.numVerts;
  1768. }
  1769. }
  1770. if ( numVerts > GFX_MAX_DYNAMIC_VERTS || numTriangles * 3 > GFX_MAX_DYNAMIC_INDICES )
  1771. {
  1772. mVB_high = NULL;
  1773. mPB_high = NULL;
  1774. return;
  1775. }
  1776. mHighTriangleCount = numTriangles;
  1777. mHighVertCount = numVerts;
  1778. mVB_high.set( GFX, numVerts, GFXBufferTypeVolatile );
  1779. GFXWaterVertex *vertPtr = mVB_high.lock();
  1780. U32 vertCounter = 0;
  1781. // NOTE: this will break if different segments have different number
  1782. // of columns, but that will also cause T-junction triangles so just don't
  1783. // do that.
  1784. // For each batch, loop through the segments contained by
  1785. // that batch, and add their verts to the buffer.
  1786. for ( U32 i = 0; i < mHighLODBatches.size(); i++ )
  1787. {
  1788. RiverRenderBatch &batch = mHighLODBatches[i];
  1789. batch.startVert = vertCounter;
  1790. batch.vertCount = 0;
  1791. VectorF lastNormal(0,0,1);
  1792. for ( U32 j = batch.startSegmentIdx; j <= batch.endSegmentIdx; j++ )
  1793. {
  1794. // Add the verts for this segment to the buffer.
  1795. RiverSegment &segment = mSegments[j];
  1796. BiSqrToQuad3D squareToQuad( segment.getP00(),
  1797. segment.getP10(),
  1798. segment.getP11(),
  1799. segment.getP01() );
  1800. // We are duplicating the last row of verts in a segment on
  1801. // the first row of the next segment. This could be optimized but
  1802. // shouldn't cause any problems.
  1803. VectorF normal = segment.getSurfaceNormal();
  1804. for ( U32 k = 0; k <= segment.rows; k++ )
  1805. {
  1806. VectorF vertNormal = ( k == 0 && j != batch.startSegmentIdx ) ? lastNormal : normal;
  1807. F32 rowLen = mLerp( segment.slice0->width, segment.slice1->width, (F32)k / (F32)segment.rows );
  1808. for ( U32 l = 0; l <= segment.columns; l++ )
  1809. {
  1810. // We are generating a "row" of verts along the forwardDivision
  1811. // Each l iteration is a step to the right along with row.
  1812. Point2F uv( (F32)l / (F32)segment.columns, (F32)k / (F32)segment.rows );
  1813. Point3F pnt = squareToQuad.transform( uv );
  1814. // Assign the Vert
  1815. vertPtr->point = pnt;
  1816. vertPtr->normal = vertNormal;
  1817. vertPtr->undulateData.x = ( uv.x - 0.5f ) * rowLen;
  1818. vertPtr->undulateData.y = ( segment.TexCoordEnd() - segment.TexCoordStart() ) * uv.y + segment.TexCoordStart();
  1819. vertPtr->horizonFactor.set( 0, 0, 0, 0 );
  1820. vertPtr++;
  1821. vertCounter++;
  1822. batch.vertCount++;
  1823. }
  1824. }
  1825. lastNormal = normal;
  1826. }
  1827. }
  1828. AssertFatal( vertCounter == mHighVertCount, "River, wrote incorrect number of verts in mVB_high" );
  1829. mVB_high.unlock();
  1830. //
  1831. // Do the high lod primitive buffer.
  1832. //
  1833. mPB_high.set( GFX, numTriangles * 3, numTriangles, GFXBufferTypeVolatile );
  1834. U16 *idxBuff;
  1835. mPB_high.lock(&idxBuff);
  1836. U32 curIdx = 0;
  1837. U32 batchOffset = 0;
  1838. // For each high lod batch, we must add indices to the buffer
  1839. // for each segment it contains ( and the count will depend on
  1840. // the division level columns/rows for each segment ).
  1841. // Temporaries for holding the indices of a quad
  1842. U32 p00, p01, p11, p10;
  1843. for ( U32 i = 0; i < mHighLODBatches.size(); i++ )
  1844. {
  1845. RiverRenderBatch &batch = mHighLODBatches[i];
  1846. batch.indexCount = 0;
  1847. batch.triangleCount = 0;
  1848. batch.startIndex = curIdx;
  1849. U32 temp = 0;
  1850. U32 segmentOffset = 0;
  1851. for ( U32 j = batch.startSegmentIdx; j <= batch.endSegmentIdx; j++ )
  1852. {
  1853. const RiverSegment &segment = mSegments[j];
  1854. // Loop through all divisions adding the indices to the
  1855. // high detail primitive buffer.
  1856. for ( U32 k = 0; k < segment.rows; k++ )
  1857. {
  1858. for ( U32 l = 0; l < segment.columns; l++ )
  1859. {
  1860. // The indices for this quad.
  1861. p00 = batchOffset + segmentOffset + l + k * ( segment.columns + 1 );
  1862. p01 = p00 + segment.columns + 1;
  1863. p11 = p01 + 1;
  1864. p10 = p00 + 1;
  1865. AssertFatal( p00 <= mHighTriangleCount * 3, "River, bad draw call!" );
  1866. AssertFatal( p01 <= mHighTriangleCount * 3, "River, bad draw call!" );
  1867. AssertFatal( p11 <= mHighTriangleCount * 3, "River, bad draw call!" );
  1868. AssertFatal( p10 <= mHighTriangleCount * 3, "River, bad draw call!" );
  1869. // Upper-Left triangle
  1870. idxBuff[curIdx] = p00;
  1871. curIdx++;
  1872. idxBuff[curIdx] = p01;
  1873. curIdx++;
  1874. idxBuff[curIdx] = p11;
  1875. curIdx++;
  1876. // Lower-Right Triangle
  1877. idxBuff[curIdx] = p00;
  1878. curIdx++;
  1879. idxBuff[curIdx] = p11;
  1880. curIdx++;
  1881. idxBuff[curIdx] = p10;
  1882. curIdx++;
  1883. batch.indexCount += 6;
  1884. batch.triangleCount += 2;
  1885. }
  1886. }
  1887. // Increment the sliceOffset by the number of verts
  1888. // used by this segment. So the next segment will index
  1889. // into new verts.
  1890. segmentOffset += ( segment.columns + 1 ) * ( segment.rows + 1 );
  1891. temp += ( segment.columns + 1 ) * ( segment.rows + 1 );
  1892. }
  1893. batchOffset += temp;
  1894. }
  1895. // Unlock the PrimitiveBuffer, we are done filling it.
  1896. mPB_high.unlock();
  1897. }
  1898. U32 River::_addNode( const Point3F &pos, const F32 &width, const F32 &depth, const VectorF &normal )
  1899. {
  1900. mNodes.increment();
  1901. RiverNode &node = mNodes.last();
  1902. node.point = pos;
  1903. node.width = width;
  1904. node.depth = depth;
  1905. node.normal = normal;
  1906. setMaskBits( NodeMask | RegenMask );
  1907. return mNodes.size() - 1;
  1908. }
  1909. U32 River::_insertNode( const Point3F &pos, const F32 &width, const F32 &depth, const VectorF &normal, const U32 &idx )
  1910. {
  1911. U32 ret;
  1912. RiverNode *node;
  1913. if ( idx == U32_MAX )
  1914. {
  1915. mNodes.increment();
  1916. node = &mNodes.last();
  1917. ret = mNodes.size() - 1;
  1918. }
  1919. else
  1920. {
  1921. mNodes.insert( idx );
  1922. node = &mNodes[idx];
  1923. ret = idx;
  1924. }
  1925. node->point = pos;
  1926. node->depth = depth;
  1927. node->width = width;
  1928. node->normal = normal;
  1929. return ret;
  1930. }
  1931. void River::setMetersPerSegment( F32 meters )
  1932. {
  1933. if ( meters < MIN_METERS_PER_SEGMENT )
  1934. {
  1935. Con::warnf( "River::setMetersPerSegment, specified meters (%g) is below the min meters (%g), NOT SET!", meters, MIN_METERS_PER_SEGMENT );
  1936. return;
  1937. }
  1938. mMetersPerSegment = meters;
  1939. _regenerate();
  1940. setMaskBits( RiverMask | RegenMask );
  1941. }
  1942. void River::setBatchSize( U32 size )
  1943. {
  1944. // Not functional
  1945. //mSegmentsPerBatch = size;
  1946. //_regenerate();
  1947. //setMaskBits( RiverMask | RegenMask );
  1948. }
  1949. void River::regenerate()
  1950. {
  1951. _regenerate();
  1952. setMaskBits( RegenMask );
  1953. }
  1954. void River::setMaxDivisionSize( F32 meters )
  1955. {
  1956. if ( meters < mMinDivisionSize )
  1957. mMaxDivisionSize = mMinDivisionSize;
  1958. else
  1959. mMaxDivisionSize = meters;
  1960. _regenerate();
  1961. setMaskBits( RiverMask | RegenMask );
  1962. }
  1963. //-------------------------------------------------------------------------
  1964. // Console Methods
  1965. //-------------------------------------------------------------------------
  1966. DefineEngineMethod( River, regenerate, void, (),,
  1967. "Intended as a helper to developers and editor scripts.\n"
  1968. "Force River to recreate its geometry."
  1969. )
  1970. {
  1971. object->regenerate();
  1972. }
  1973. DefineEngineMethod( River, setMetersPerSegment, void, ( F32 meters ),,
  1974. "Intended as a helper to developers and editor scripts.\n"
  1975. "@see SegmentLength field."
  1976. )
  1977. {
  1978. object->setMetersPerSegment( meters );
  1979. }
  1980. DefineEngineMethod( River, setBatchSize, void, ( F32 meters ),,
  1981. "Intended as a helper to developers and editor scripts.\n"
  1982. "BatchSize is not currently used."
  1983. )
  1984. {
  1985. object->setBatchSize( meters );
  1986. }
  1987. DefineEngineMethod( River, setNodeDepth, void, ( S32 idx, F32 meters ),,
  1988. "Intended as a helper to developers and editor scripts.\n"
  1989. "Sets the depth in meters of a particular node."
  1990. )
  1991. {
  1992. object->setNodeDepth( idx, meters );
  1993. }
  1994. DefineEngineMethod( River, setMaxDivisionSize, void, ( F32 meters ),,
  1995. "Intended as a helper to developers and editor scripts.\n"
  1996. "@see SubdivideLength field."
  1997. )
  1998. {
  1999. object->setMaxDivisionSize( meters );
  2000. }