river.cpp 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544
  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. addGroup( "River" );
  505. addField( "SegmentLength", TypeF32, Offset( mMetersPerSegment, River ),
  506. "Divide the River lengthwise into segments of this length in meters. "
  507. "These geometric volumes are used for spacial queries like determining containment." );
  508. addField( "SubdivideLength", TypeF32, Offset( mMaxDivisionSize, River ),
  509. "For purposes of generating the renderable geometry River segments are further subdivided "
  510. "such that no quad is of greater width or length than this distance in meters." );
  511. addField( "FlowMagnitude", TypeF32, Offset( mFlowMagnitude, River ),
  512. "Magnitude of the force vector applied to dynamic objects within the River." );
  513. addField( "LowLODDistance", TypeF32, Offset( mLodDistance, River ),
  514. "Segments of the river at this distance in meters or greater will "
  515. "render as a single unsubdivided without undulation effects." );
  516. endGroup( "River" );
  517. addGroup( "Internal" );
  518. addProtectedField( "Node", TypeString, 0, &addNodeFromField, &emptyStringProtectedGetFn, "For internal use, do not modify." );
  519. endGroup( "Internal" );
  520. Parent::initPersistFields();
  521. }
  522. void River::consoleInit()
  523. {
  524. Parent::consoleInit();
  525. Con::addVariable( "$River::EditorOpen", TypeBool, &River::smEditorOpen, "For editor use.\n"
  526. "@ingroup Editors\n" );
  527. Con::addVariable( "$River::showWalls", TypeBool, &River::smShowWalls, "For editor use.\n"
  528. "@ingroup Editors\n" );
  529. Con::addVariable( "$River::showNodes", TypeBool, &River::smShowNodes, "For editor use.\n"
  530. "@ingroup Editors\n");
  531. Con::addVariable( "$River::showSpline", TypeBool, &River::smShowSpline, "For editor use.\n"
  532. "@ingroup Editors\n" );
  533. Con::addVariable( "$River::showRiver", TypeBool, &River::smShowRiver, "For editor use.\n"
  534. "@ingroup Editors\n" );
  535. Con::addVariable( "$River::showWireframe", TypeBool, &River::smWireframe, "For editor use.\n"
  536. "@ingroup Editors\n");
  537. }
  538. bool River::addNodeFromField( void *object, const char *index, const char *data )
  539. {
  540. River *pObj = static_cast<River*>(object);
  541. //if ( !pObj->isProperlyAdded() )
  542. //{
  543. F32 x,y,z,width,depth;
  544. VectorF normal;
  545. U32 result = dSscanf( data, "%f %f %f %f %f %f %f %f", &x, &y, &z, &width, &depth, &normal.x, &normal.y, &normal.z );
  546. if ( result == 8 )
  547. pObj->_addNode( Point3F(x,y,z), width, depth, normal );
  548. //}
  549. return false;
  550. }
  551. bool River::onAdd()
  552. {
  553. if ( !Parent::onAdd() )
  554. return false;
  555. // Reset the World Box.
  556. //setGlobalBounds();
  557. resetWorldBox();
  558. // Set the Render Transform.
  559. setRenderTransform(mObjToWorld);
  560. // Add to Scene.
  561. addToScene();
  562. if ( isServerObject() )
  563. getServerSet()->addObject( this );
  564. _regenerate();
  565. return true;
  566. }
  567. void River::onRemove()
  568. {
  569. removeFromScene();
  570. Parent::onRemove();
  571. }
  572. void River::inspectPostApply()
  573. {
  574. // Set Parent.
  575. Parent::inspectPostApply();
  576. if ( mMetersPerSegment < MIN_METERS_PER_SEGMENT )
  577. mMetersPerSegment = MIN_METERS_PER_SEGMENT;
  578. mMaxDivisionSize = getMax( mMaxDivisionSize, mMinDivisionSize );
  579. // Set fxPortal Mask.
  580. setMaskBits(RiverMask|RegenMask);
  581. }
  582. void River::onStaticModified( const char* slotName, const char*newValue )
  583. {
  584. Parent::onStaticModified( slotName, newValue );
  585. if ( dStricmp( slotName, "surfMaterial" ) == 0 )
  586. setMaskBits( MaterialMask );
  587. }
  588. SimSet* River::getServerSet()
  589. {
  590. if ( !smServerRiverSet )
  591. {
  592. smServerRiverSet = new SimSet();
  593. smServerRiverSet->registerObject( "ServerRiverSet" );
  594. Sim::getRootGroup()->addObject( smServerRiverSet );
  595. }
  596. return smServerRiverSet;
  597. }
  598. void River::writeFields( Stream &stream, U32 tabStop )
  599. {
  600. Parent::writeFields( stream, tabStop );
  601. // Now write all nodes
  602. stream.write(2, "\r\n");
  603. for ( U32 i = 0; i < mNodes.size(); i++ )
  604. {
  605. const RiverNode &node = mNodes[i];
  606. stream.writeTabs(tabStop);
  607. char buffer[1024];
  608. dMemset( buffer, 0, 1024 );
  609. dSprintf( buffer, 1024, "Node = \"%f %f %f %f %f %f %f %f\";", node.point.x, node.point.y, node.point.z,
  610. node.width,
  611. node.depth,
  612. node.normal.x, node.normal.y, node.normal.z );
  613. stream.writeLine( (const U8*)buffer );
  614. }
  615. }
  616. bool River::writeField( StringTableEntry fieldname, const char *value )
  617. {
  618. if ( fieldname == StringTable->insert("node") )
  619. return false;
  620. return Parent::writeField( fieldname, value );
  621. }
  622. void River::innerRender( SceneRenderState *state )
  623. {
  624. GFXDEBUGEVENT_SCOPE( River_innerRender, ColorI( 255, 0, 0 ) );
  625. PROFILE_SCOPE( River_innerRender );
  626. // Setup SceneData
  627. SceneData sgData;
  628. sgData.init( state );
  629. sgData.lights[0] = LIGHTMGR->getSpecialLight( LightManager::slSunLightType );
  630. sgData.backBuffTex = REFLECTMGR->getRefractTex();
  631. sgData.reflectTex = mPlaneReflector.reflectTex;
  632. sgData.wireframe |= smWireframe;
  633. const Point3F &camPosition = state->getCameraPosition();
  634. // set the material
  635. S32 matIdx = getMaterialIndex( camPosition );
  636. if ( !initMaterial( matIdx ) )
  637. return;
  638. BaseMatInstance *mat = mMatInstances[matIdx];
  639. WaterMatParams matParams = mMatParamHandles[matIdx];
  640. if ( !mat )
  641. return;
  642. // setup proj/world transform
  643. GFXTransformSaver saver;
  644. setShaderParams( state, mat, matParams );
  645. _makeRenderBatches( camPosition );
  646. if ( !River::smShowRiver )
  647. return;
  648. // If no material... we're done.
  649. if ( mLowLODBatches.empty() && mHighLODBatches.empty() )
  650. return;
  651. if ( !mHighLODBatches.empty() )
  652. _makeHighLODBuffers();
  653. mMatrixSet->restoreSceneViewProjection();
  654. mMatrixSet->setWorld( MatrixF::Identity );
  655. while( mat->setupPass( state, sgData ) )
  656. {
  657. mat->setSceneInfo(state, sgData);
  658. mat->setTransforms(*mMatrixSet, state);
  659. setCustomTextures( matIdx, mat->getCurPass(), matParams );
  660. GFX->setVertexBuffer( mVB_low );
  661. GFX->setPrimitiveBuffer( mPB_low );
  662. for ( U32 i = 0; i < mLowLODBatches.size(); i++ )
  663. {
  664. const RiverRenderBatch &batch = mLowLODBatches[i];
  665. U32 startVert = batch.startSegmentIdx * 2;
  666. U32 endVert = ( batch.endSegmentIdx + 1 ) * 2 + 1;
  667. U32 startIdx = batch.startSegmentIdx * 6;
  668. U32 endIdx = batch.endSegmentIdx * 6 + 5;
  669. U32 vertCount = ( endVert - startVert ) + 1;
  670. U32 idxCount = ( endIdx - startIdx ) + 1;
  671. U32 triangleCount = idxCount / 3;
  672. AssertFatal( startVert < mLowVertCount, "River, bad draw call!" );
  673. AssertFatal( startVert + vertCount <= mLowVertCount, "River, bad draw call!" );
  674. AssertFatal( triangleCount <= mLowTriangleCount, "River, bad draw call!" );
  675. GFX->drawIndexedPrimitive( GFXTriangleList, 0, startVert, vertCount, startIdx, triangleCount );
  676. }
  677. // Render all high detail batches.
  678. //
  679. // It is possible that the buffers could not be allocated because
  680. // the max number of verts/indices was exceeded. We don't want to
  681. // crash because that would be unhelpful for working in the editor.
  682. if ( mVB_high.isValid() && mPB_high.isValid() )
  683. {
  684. GFX->setVertexBuffer( mVB_high );
  685. GFX->setPrimitiveBuffer( mPB_high );
  686. for ( U32 i = 0; i < mHighLODBatches.size(); i++ )
  687. {
  688. const RiverRenderBatch &batch = mHighLODBatches[i];
  689. AssertFatal( batch.startVert < mHighVertCount, "River, bad draw call!" );
  690. AssertFatal( batch.startVert + batch.vertCount <= mHighVertCount, "River, bad draw call!" );
  691. AssertFatal( batch.triangleCount <= mHighTriangleCount, "River, bad draw call!" );
  692. AssertFatal( batch.startIndex < mHighTriangleCount * 3, "River, bad draw call!" );
  693. AssertFatal( batch.startIndex + batch.triangleCount * 3 <= mHighTriangleCount * 3, "River, bad draw call!" );
  694. GFX->drawIndexedPrimitive( GFXTriangleList,
  695. 0,
  696. 0,
  697. batch.vertCount,
  698. batch.startIndex,
  699. batch.triangleCount );
  700. }
  701. }
  702. } // while( mat->setupPass( sgData ) )
  703. }
  704. void River::updateUnderwaterEffect( SceneRenderState *state )
  705. {
  706. // Calculate mWaterPlane before calling updateUnderwaterEffect.
  707. Point3F dummy;
  708. _getWaterPlane( state->getCameraPosition(), mWaterFogData.plane, dummy );
  709. Parent::updateUnderwaterEffect( state );
  710. }
  711. void River::setShaderParams( SceneRenderState *state, BaseMatInstance* mat, const WaterMatParams& paramHandles )
  712. {
  713. // Set variables that will be assigned to shader consts within WaterCommon
  714. // before calling Parent::setShaderParams
  715. mUndulateMaxDist = mLodDistance;
  716. Parent::setShaderParams( state, mat, paramHandles );
  717. // Now set the rest of the shader consts that are either unique to this
  718. // class or that WaterObject leaves to us to handle...
  719. MaterialParameters* matParams = mat->getMaterialParameters();
  720. // set vertex shader constants
  721. //-----------------------------------
  722. matParams->setSafe(paramHandles.mGridElementSizeSC, 1.0f);
  723. if ( paramHandles.mModelMatSC->isValid() )
  724. matParams->set(paramHandles.mModelMatSC, MatrixF::Identity, GFXSCT_Float4x4);
  725. // set pixel shader constants
  726. //-----------------------------------
  727. LinearColorF c( mWaterFogData.color );
  728. matParams->setSafe(paramHandles.mBaseColorSC, c);
  729. // By default we need to show a true reflection is fullReflect is enabled and
  730. // we are above water.
  731. F32 reflect = mPlaneReflector.isEnabled() && !isUnderwater( state->getCameraPosition() );
  732. // If we were occluded the last frame a query was fetched ( not necessarily last frame )
  733. // and we weren't updated last frame... we don't have a valid texture to show
  734. // so use the cubemap / fake reflection color this frame.
  735. if ( mPlaneReflector.lastUpdateMs != REFLECTMGR->getLastUpdateMs() && mPlaneReflector.isOccluded() )
  736. reflect = false;
  737. Point4F reflectParams( mWaterPos.z, 0.0f, 1000.0f, !reflect );
  738. matParams->setSafe(paramHandles.mReflectParamsSC, reflectParams );
  739. matParams->setSafe(paramHandles.mReflectNormalSC, mPlaneReflector.refplane );
  740. }
  741. bool River::isUnderwater( const Point3F &pnt ) const
  742. {
  743. return containsPoint( pnt, NULL );
  744. }
  745. U32 River::packUpdate(NetConnection * con, U32 mask, BitStream * stream)
  746. {
  747. // Pack Parent.
  748. U32 retMask = Parent::packUpdate(con, mask, stream);
  749. if ( stream->writeFlag( mask & RiverMask ) )
  750. {
  751. // Write Object Transform.
  752. stream->writeAffineTransform(mObjToWorld);
  753. stream->write( mMetersPerSegment );
  754. stream->write( mSegmentsPerBatch );
  755. stream->write( mDepthScale );
  756. stream->write( mMaxDivisionSize );
  757. stream->write( mColumnCount );
  758. stream->write( mFlowMagnitude );
  759. stream->write( mLodDistance );
  760. }
  761. if ( stream->writeFlag( mask & NodeMask ) )
  762. {
  763. const U32 nodeByteSize = 32; // Based on sending all of a node's parameters
  764. // Test if we can fit all of our nodes within the current stream.
  765. // We make sure we leave 100 bytes still free in the stream for whatever
  766. // may follow us.
  767. S32 allowedBytes = stream->getWriteByteSize() - 100;
  768. if ( stream->writeFlag( (nodeByteSize * mNodes.size()) < allowedBytes ) )
  769. {
  770. // All nodes should fit, so send them out now.
  771. stream->writeInt( mNodes.size(), 16 );
  772. for ( U32 i = 0; i < mNodes.size(); i++ )
  773. {
  774. mathWrite( *stream, mNodes[i].point );
  775. stream->write( mNodes[i].width );
  776. stream->write( mNodes[i].depth );
  777. mathWrite( *stream, mNodes[i].normal );
  778. }
  779. }
  780. else
  781. {
  782. // There isn't enough space left in the stream for all of the
  783. // nodes. Batch them up into NetEvents.
  784. U32 id = gServerNodeListManager->nextListId();
  785. U32 count = 0;
  786. U32 index = 0;
  787. while (count < mNodes.size())
  788. {
  789. count += NodeListManager::smMaximumNodesPerEvent;
  790. if (count > mNodes.size())
  791. {
  792. count = mNodes.size();
  793. }
  794. RiverNodeEvent* event = new RiverNodeEvent();
  795. event->mId = id;
  796. event->mTotalNodes = mNodes.size();
  797. event->mLocalListStart = index;
  798. for (; index<count; ++index)
  799. {
  800. event->mPositions.push_back( mNodes[index].point );
  801. event->mWidths.push_back( mNodes[index].width );
  802. event->mDepths.push_back( mNodes[index].depth );
  803. event->mNormals.push_back( mNodes[index].normal );
  804. }
  805. con->postNetEvent( event );
  806. }
  807. stream->write( id );
  808. }
  809. }
  810. if( stream->writeFlag( mask & ( RiverMask | InitialUpdateMask ) ) )
  811. {
  812. // This is set to allow the user to modify the size of the water dynamically
  813. // in the editor
  814. mathWrite( *stream, mObjScale );
  815. stream->writeAffineTransform( mObjToWorld );
  816. }
  817. stream->writeFlag( mask & RegenMask );
  818. return retMask;
  819. }
  820. void River::unpackUpdate(NetConnection * con, BitStream * stream)
  821. {
  822. // Unpack Parent.
  823. Parent::unpackUpdate(con, stream);
  824. // RiverMask
  825. if(stream->readFlag())
  826. {
  827. MatrixF ObjectMatrix;
  828. stream->readAffineTransform(&ObjectMatrix);
  829. Parent::setTransform(ObjectMatrix);
  830. stream->read( &mMetersPerSegment );
  831. stream->read( &mSegmentsPerBatch );
  832. stream->read( &mDepthScale );
  833. stream->read( &mMaxDivisionSize );
  834. stream->read( &mColumnCount );
  835. stream->read( &mFlowMagnitude );
  836. stream->read( &mLodDistance );
  837. }
  838. // NodeMask
  839. if ( stream->readFlag() )
  840. {
  841. if (stream->readFlag())
  842. {
  843. // Nodes have been passed in this update
  844. U32 count = stream->readInt( 16 );
  845. mNodes.clear();
  846. Point3F pos;
  847. VectorF normal;
  848. F32 width,depth;
  849. for ( U32 i = 0; i < count; i++ )
  850. {
  851. mathRead( *stream, &pos );
  852. stream->read( &width );
  853. stream->read( &depth );
  854. mathRead( *stream, &normal );
  855. _addNode( pos, width, depth, normal );
  856. }
  857. }
  858. else
  859. {
  860. // Nodes will arrive as events
  861. U32 id;
  862. stream->read( &id );
  863. // Check if the road's nodes made it here before we did.
  864. NodeListManager::NodeList* list = NULL;
  865. if ( gClientNodeListManager->findListById( id, &list, true) )
  866. {
  867. // Work with the completed list
  868. RiverNodeList* riverList = dynamic_cast<RiverNodeList*>( list );
  869. if (riverList)
  870. buildNodesFromList( riverList );
  871. delete list;
  872. }
  873. else
  874. {
  875. // Nodes have not yet arrived, so register our interest in the list
  876. RiverNodeListNotify* notify = new RiverNodeListNotify( this, id );
  877. gClientNodeListManager->registerNotification( notify );
  878. }
  879. }
  880. }
  881. // RiverMask | InitialUpdateMask
  882. if( stream->readFlag() )
  883. {
  884. mathRead( *stream, &mObjScale );
  885. stream->readAffineTransform( &mObjToWorld );
  886. }
  887. // RegenMask
  888. if ( stream->readFlag() && isProperlyAdded() )
  889. regenerate();
  890. }
  891. void River::_getWaterPlane( const Point3F &camPos, PlaneF &outPlane, Point3F &outPos )
  892. {
  893. // Find the RiverSegment closest to the camera.
  894. F32 closestDist = F32_MAX;
  895. S32 closestSegment = 0;
  896. Point3F projPnt(0.0f, 0.0f, 0.0f);
  897. VectorF normal(0,0,0);
  898. for ( U32 i = 0; i < mSegments.size(); i++ )
  899. {
  900. const RiverSegment &segment = mSegments[i];
  901. const Point3F pos = MathUtils::mClosestPointOnSegment( segment.slice0->p1, segment.slice1->p1, camPos );
  902. F32 dist = ( camPos - pos ).len();
  903. if ( dist < closestDist )
  904. {
  905. closestDist = dist;
  906. closestSegment = i;
  907. projPnt = pos;
  908. }
  909. normal += segment.getSurfaceNormal();
  910. }
  911. if ( mReflectNormalUp )
  912. normal.set(0,0,1);
  913. else
  914. normal.normalizeSafe();
  915. outPos = projPnt;
  916. outPlane.set( projPnt, normal );
  917. }
  918. void River::setTransform( const MatrixF &mat )
  919. {
  920. for ( U32 i = 0; i < mNodes.size(); i++ )
  921. {
  922. mWorldToObj.mulP( mNodes[i].point );
  923. mat.mulP( mNodes[i].point );
  924. }
  925. /*
  926. // Get the amount of change in position.
  927. MatrixF oldMat = getTransform();
  928. Point3F oldPos = oldMat.getPosition();
  929. Point3F newPos = mat.getPosition();
  930. Point3F delta = newPos - oldPos;
  931. // Offset all nodes by that amount
  932. for ( U32 i = 0; i < mNodes.size(); i++ )
  933. {
  934. mNodes[i].point += delta;
  935. }
  936. // Assign the new position ( we ignore rotation )
  937. MatrixF newMat( oldMat );
  938. newMat.setPosition( newPos );
  939. */
  940. Parent::setTransform( mat );
  941. // Regenerate and update the client
  942. _regenerate();
  943. setMaskBits( NodeMask | RegenMask );
  944. }
  945. void River::setScale( const VectorF &scale )
  946. {
  947. // We ignore scale requests from the editor
  948. // right now.
  949. }
  950. bool River::castRay(const Point3F &s, const Point3F &e, RayInfo* info)
  951. {
  952. Point3F start = s;
  953. Point3F end = e;
  954. mObjToWorld.mulP(start);
  955. mObjToWorld.mulP(end);
  956. F32 out = 1.0f; // The output fraction/percentage along the line defined by s and e
  957. VectorF norm(0.0f, 0.0f, 0.0f); // The normal of the face intersected
  958. Vector<RiverHitSegment> hitSegments;
  959. for ( U32 i = 0; i < mSegments.size(); i++ )
  960. {
  961. const RiverSegment &segment = mSegments[i];
  962. F32 t;
  963. VectorF n;
  964. if ( segment.worldbounds.collideLine( start, end, &t, &n ) )
  965. {
  966. hitSegments.increment();
  967. hitSegments.last().t = t;
  968. hitSegments.last().idx = i;
  969. }
  970. }
  971. dQsort( hitSegments.address(), hitSegments.size(), sizeof(RiverHitSegment), compareHitSegments );
  972. U32 idx0, idx1, idx2;
  973. F32 t;
  974. for ( U32 i = 0; i < hitSegments.size(); i++ )
  975. {
  976. U32 segIdx = hitSegments[i].idx;
  977. const RiverSegment &segment = mSegments[segIdx];
  978. // Each segment has 6 faces
  979. for ( U32 j = 0; j < 6; j++ )
  980. {
  981. if ( j == 4 && segIdx != 0 )
  982. continue;
  983. if ( j == 5 && segIdx != mSegments.size() - 1 )
  984. continue;
  985. // Each face has 2 triangles
  986. for ( U32 k = 0; k < 2; k++ )
  987. {
  988. idx0 = gIdxArray[j][k][0];
  989. idx1 = gIdxArray[j][k][1];
  990. idx2 = gIdxArray[j][k][2];
  991. const Point3F &v0 = segment[idx0];
  992. const Point3F &v1 = segment[idx1];
  993. const Point3F &v2 = segment[idx2];
  994. if ( !MathUtils::mLineTriangleCollide( start, end,
  995. v2, v1, v0,
  996. NULL,
  997. &t ) )
  998. continue;
  999. if ( t >= 0.0f && t < 1.0f && t < out )
  1000. {
  1001. out = t;
  1002. // optimize this, can be calculated easily within
  1003. // the collision test
  1004. norm = PlaneF( v0, v1, v2 );
  1005. }
  1006. }
  1007. }
  1008. if (out >= 0.0f && out < 1.0f)
  1009. break;
  1010. }
  1011. if (out >= 0.0f && out < 1.0f)
  1012. {
  1013. info->t = out;
  1014. info->normal = norm;
  1015. info->point.interpolate(start, end, out);
  1016. info->face = -1;
  1017. info->object = this;
  1018. return true;
  1019. }
  1020. return false;
  1021. }
  1022. bool River::collideBox(const Point3F &start, const Point3F &end, RayInfo* info)
  1023. {
  1024. return false;
  1025. }
  1026. bool River::buildPolyList( PolyListContext context, AbstractPolyList* polyList, const Box3F& box, const SphereF& sphere )
  1027. {
  1028. Vector<const RiverSegment*> hitSegments;
  1029. for ( U32 i = 0; i < mSegments.size(); i++ )
  1030. {
  1031. const RiverSegment &segment = mSegments[i];
  1032. if ( segment.worldbounds.isOverlapped( box ) )
  1033. {
  1034. hitSegments.push_back( &segment );
  1035. }
  1036. }
  1037. if ( !hitSegments.size() )
  1038. return false;
  1039. polyList->setObject( this );
  1040. polyList->setTransform( &MatrixF::Identity, Point3F( 1.0f, 1.0f, 1.0f ) );
  1041. for ( U32 i = 0; i < hitSegments.size(); i++ )
  1042. {
  1043. const RiverSegment* segment = hitSegments[i];
  1044. for ( U32 k = 0; k < 2; k++ )
  1045. {
  1046. // gIdxArray[0] gives us the top plane (see table definition).
  1047. U32 idx0 = gIdxArray[0][k][0];
  1048. U32 idx1 = gIdxArray[0][k][1];
  1049. U32 idx2 = gIdxArray[0][k][2];
  1050. const Point3F &v0 = (*segment)[idx0];
  1051. const Point3F &v1 = (*segment)[idx1];
  1052. const Point3F &v2 = (*segment)[idx2];
  1053. // Add vertices to poly list.
  1054. U32 i0 = polyList->addPoint(v0);
  1055. polyList->addPoint(v1);
  1056. polyList->addPoint(v2);
  1057. // Add plane between them.
  1058. polyList->begin(0, 0);
  1059. polyList->vertex(i0);
  1060. polyList->vertex(i0+1);
  1061. polyList->vertex(i0+2);
  1062. polyList->plane(i0, i0+1, i0+2);
  1063. polyList->end();
  1064. }
  1065. }
  1066. return true;
  1067. }
  1068. F32 River::getWaterCoverage( const Box3F &worldBox ) const
  1069. {
  1070. PROFILE_SCOPE( River_GetWaterCoverage );
  1071. if ( !mWorldBox.isOverlapped(worldBox) )
  1072. return 0.0f;
  1073. Point3F bottomPnt = worldBox.getCenter();
  1074. bottomPnt.z = worldBox.minExtents.z;
  1075. F32 farthest = 0.0f;
  1076. for ( U32 i = 0; i < mSegments.size(); i++ )
  1077. {
  1078. const RiverSegment &segment = mSegments[i];
  1079. if ( !segment.worldbounds.isOverlapped(worldBox) )
  1080. continue;
  1081. if ( !segment.intersectBox( worldBox ) )
  1082. continue;
  1083. F32 distance = segment.distanceToSurface( bottomPnt );
  1084. if ( distance > farthest )
  1085. farthest = distance;
  1086. }
  1087. F32 height = worldBox.maxExtents.z - worldBox.minExtents.z;
  1088. F32 distance = mClampF( farthest, 0.0f, height );
  1089. F32 coverage = distance / height;
  1090. return coverage;
  1091. }
  1092. F32 River::getSurfaceHeight( const Point2F &pos ) const
  1093. {
  1094. PROFILE_SCOPE( River_GetSurfaceHeight );
  1095. Point3F origin( pos.x, pos.y, mWorldBox.maxExtents.z );
  1096. Point3F direction(0,0,-1);
  1097. U32 nodeIdx;
  1098. Point3F collisionPnt;
  1099. if ( !collideRay( origin, direction, &nodeIdx, &collisionPnt ) )
  1100. return -1.0f;
  1101. return collisionPnt.z;
  1102. }
  1103. VectorF River::getFlow( const Point3F &pos ) const
  1104. {
  1105. PROFILE_SCOPE( River_GetFlow );
  1106. for ( U32 i = 0; i < mSegments.size(); i++ )
  1107. {
  1108. const RiverSegment &segment = mSegments[i];
  1109. if ( !segment.containsPoint(pos) )
  1110. continue;
  1111. VectorF flow = segment.slice0->p1 - segment.slice1->p1;
  1112. flow.normalize();
  1113. flow *= mFlowMagnitude;
  1114. return flow;
  1115. }
  1116. return VectorF::Zero;
  1117. }
  1118. void River::onReflectionInfoChanged()
  1119. {
  1120. /*
  1121. if ( isClientObject() && GFX->getPixelShaderVersion() >= 1.4 )
  1122. {
  1123. if ( mFullReflect )
  1124. REFLECTMGR->registerObject( this, ReflectDelegate( this, &River::updateReflection ), mReflectPriority, mReflectMaxRateMs, mReflectMaxDist );
  1125. else
  1126. {
  1127. REFLECTMGR->unregisterObject( this );
  1128. mReflectTex = NULL;
  1129. }
  1130. }
  1131. */
  1132. }
  1133. void River::_regenerate()
  1134. {
  1135. if ( mNodes.size() == 0 )
  1136. return;
  1137. const Point3F &nodePt = mNodes.first().point;
  1138. MatrixF mat( true );
  1139. mat.setPosition( nodePt );
  1140. Parent::setTransform( mat );
  1141. _generateSlices();
  1142. }
  1143. void River::_generateSlices()
  1144. {
  1145. if ( mNodes.size() < 2 )
  1146. return;
  1147. U32 nodeCount = mNodes.size();
  1148. RiverSplineNode *splineNodes = new RiverSplineNode[nodeCount];
  1149. for ( U32 i = 0; i < nodeCount; i++ )
  1150. {
  1151. const RiverNode &node = mNodes[i];
  1152. splineNodes[i].x = node.point.x;
  1153. splineNodes[i].y = node.point.y;
  1154. splineNodes[i].z = node.point.z;
  1155. splineNodes[i].width = node.width;
  1156. splineNodes[i].depth = node.depth;
  1157. splineNodes[i].normal = node.normal;
  1158. }
  1159. CatmullRom<RiverSplineNode> spline;
  1160. spline.initialize( nodeCount, splineNodes );
  1161. delete [] splineNodes;
  1162. mSlices.clear();
  1163. for ( U32 i = 1; i < nodeCount; i++ )
  1164. {
  1165. F32 t0 = spline.getTime( i-1 );
  1166. F32 t1 = spline.getTime( i );
  1167. F32 segLength = spline.arcLength( t0, t1 );
  1168. U32 numSegments = mCeil( segLength / mMetersPerSegment );
  1169. numSegments = getMax( numSegments, (U32)1 );
  1170. F32 tstep = ( t1 - t0 ) / numSegments;
  1171. //AssertFatal( numSegments > 0, "River::_generateSlices, got zero segments!" );
  1172. U32 startIdx = 0;
  1173. U32 endIdx = ( i == nodeCount - 1 ) ? numSegments + 1 : numSegments;
  1174. for ( U32 j = startIdx; j < endIdx; j++ )
  1175. {
  1176. F32 t = t0 + tstep * j; //spline.findParameterByDistance( 0.0f, i * segLen );
  1177. RiverSplineNode val = spline.evaluate(t);
  1178. RiverSlice slice;
  1179. slice.p1.set( val.x, val.y, val.z );
  1180. slice.uvec.set( 0,0,1 );
  1181. slice.width = val.width;
  1182. slice.depth = val.depth;
  1183. slice.parentNodeIdx = i-1;
  1184. slice.normal = val.normal;
  1185. slice.normal.normalize();
  1186. mSlices.push_back( slice );
  1187. }
  1188. }
  1189. //
  1190. // Calculate fvec and rvec for all slices
  1191. //
  1192. RiverSlice *pSlice = NULL;
  1193. RiverSlice *pNextSlice = NULL;
  1194. // Must do the first slice outside the loop
  1195. {
  1196. pSlice = &mSlices[0];
  1197. pNextSlice = &mSlices[1];
  1198. pSlice->fvec = pNextSlice->p1 - pSlice->p1;
  1199. pSlice->fvec.normalize();
  1200. pSlice->rvec = mCross( pSlice->fvec, pSlice->normal );
  1201. pSlice->rvec.normalize();
  1202. pSlice->uvec = mCross( pSlice->rvec, pSlice->fvec );
  1203. pSlice->uvec.normalize();
  1204. pSlice->rvec = mCross( pSlice->fvec, pSlice->uvec );
  1205. pSlice->rvec.normalize();
  1206. }
  1207. for ( U32 i = 1; i < mSlices.size() - 1; i++ )
  1208. {
  1209. pSlice = &mSlices[i];
  1210. pNextSlice = &mSlices[i+1];
  1211. pSlice->fvec = pNextSlice->p1 - pSlice->p1;
  1212. pSlice->fvec.normalize();
  1213. pSlice->rvec = mCross( pSlice->fvec, pSlice->normal );
  1214. pSlice->rvec.normalize();
  1215. pSlice->uvec = mCross( pSlice->rvec, pSlice->fvec );
  1216. pSlice->uvec.normalize();
  1217. pSlice->rvec = mCross( pSlice->fvec, pSlice->uvec );
  1218. pSlice->rvec.normalize();
  1219. }
  1220. // Must do the last slice outside the loop
  1221. {
  1222. RiverSlice *lastSlice = &mSlices[mSlices.size()-1];
  1223. RiverSlice *prevSlice = &mSlices[mSlices.size()-2];
  1224. lastSlice->fvec = prevSlice->fvec;
  1225. lastSlice->rvec = mCross( lastSlice->fvec, lastSlice->normal );
  1226. lastSlice->rvec.normalize();
  1227. lastSlice->uvec = mCross( lastSlice->rvec, lastSlice->fvec );
  1228. lastSlice->uvec.normalize();
  1229. lastSlice->rvec = mCross( lastSlice->fvec, lastSlice->uvec );
  1230. lastSlice->rvec.normalize();
  1231. }
  1232. //
  1233. // Calculate p0/p2/pb0/pb2 for all slices
  1234. //
  1235. for ( U32 i = 0; i < mSlices.size(); i++ )
  1236. {
  1237. RiverSlice *slice = &mSlices[i];
  1238. slice->p0 = slice->p1 - slice->rvec * slice->width * 0.5f;
  1239. slice->p2 = slice->p1 + slice->rvec * slice->width * 0.5f;
  1240. slice->pb0 = slice->p0 - slice->uvec * slice->depth;
  1241. slice->pb2 = slice->p2 - slice->uvec * slice->depth;
  1242. }
  1243. // Generate the object/world bounds
  1244. Box3F box;
  1245. for ( U32 i = 0; i < mSlices.size(); i++ )
  1246. {
  1247. const RiverSlice &slice = mSlices[i];
  1248. if ( i == 0 )
  1249. {
  1250. box.minExtents = slice.p0;
  1251. box.maxExtents = slice.p2;
  1252. box.extend( slice.pb0 );
  1253. box.extend( slice.pb2 );
  1254. }
  1255. else
  1256. {
  1257. box.extend( slice.p0 );
  1258. box.extend( slice.p2 );
  1259. box.extend( slice.pb0 );
  1260. box.extend( slice.pb2 );
  1261. }
  1262. }
  1263. mWorldBox = box;
  1264. //mObjBox.minExtents -= pos;
  1265. //mObjBox.maxExtents -= pos;
  1266. resetObjectBox();
  1267. // Make sure we are in the correct bins given our world box.
  1268. if( getSceneManager() != NULL )
  1269. getSceneManager()->notifyObjectDirty( this );
  1270. _generateSegments();
  1271. }
  1272. void River::_generateSegments()
  1273. {
  1274. mSegments.clear();
  1275. for ( U32 i = 0; i < mSlices.size() - 1; i++ )
  1276. {
  1277. RiverSegment seg( &mSlices[i], &mSlices[i+1] );
  1278. mSegments.push_back( seg );
  1279. }
  1280. /*
  1281. #ifdef TORQUE_DEBUG
  1282. for ( U32 i = 0; i < mSegments.size(); i++ )
  1283. {
  1284. const RiverSegment &segment = mSegments[i];
  1285. PlaneF normal0 = MathUtils::mTriangleNormal( segment.slice0->p0, segment.slice1->p0, segment.slice1->p2 );
  1286. PlaneF normal1 = MathUtils::mTriangleNormal( segment.slice0->p0, segment.slice1->p2, segment.slice0->p2 );
  1287. AssertFatal( true || normal0 != normal1, "River::generateSegments, segment is not coplanar!" );
  1288. }
  1289. #endif // TORQUE_DEBUG
  1290. */
  1291. // We have to go back and generate normals for each slice
  1292. // to be used in calculation of the reflect plane.
  1293. // The slice-normal we calculate are relative to the surface normal
  1294. // of the segments adjacent to the slice.
  1295. /*
  1296. if ( mSlices.size() >= 2 )
  1297. {
  1298. mSlices[0].normal = mSegments[0].getSurfaceNormal();
  1299. for ( U32 i = 1; i < mSlices.size() - 1; i++ )
  1300. {
  1301. mSlices[i].normal = ( mSegments[i-1].getSurfaceNormal() + mSegments[i].getSurfaceNormal() ) / 2;
  1302. }
  1303. mSlices.last().normal = mSegments.last().getSurfaceNormal();
  1304. }
  1305. */
  1306. _generateVerts();
  1307. }
  1308. void River::_generateVerts()
  1309. {
  1310. if ( isServerObject() )
  1311. return;
  1312. // These will depend on the level of subdivision per segment
  1313. // calculated below.
  1314. mHighVertCount = 0;
  1315. mHighTriangleCount = 0;
  1316. // Calculate the number of row/column subdivisions per each
  1317. // RiverSegment.
  1318. F32 greatestWidth = 0.1f;
  1319. for ( U32 i = 0; i < mNodes.size(); i++ )
  1320. {
  1321. RiverNode &node = mNodes[i];
  1322. if ( node.width > greatestWidth )
  1323. greatestWidth = node.width;
  1324. }
  1325. mColumnCount = mCeil( greatestWidth / mMaxDivisionSize );
  1326. for ( U32 i = 0; i < mSegments.size(); i++ )
  1327. {
  1328. RiverSegment &segment = mSegments[i];
  1329. const RiverSlice *slice = segment.slice0;
  1330. const RiverSlice *nextSlice = segment.slice1;
  1331. // Calculate the size of divisions in the forward direction ( p00 -> p01 )
  1332. F32 segLength = (nextSlice->p1 - slice->p1).len();
  1333. // A division count of one is actually NO subdivision,
  1334. // the segment corners are the only verts in this segment.
  1335. U32 numRows = 1;
  1336. if ( segLength > 0.0f )
  1337. numRows = mCeil( segLength / mMaxDivisionSize );
  1338. // The problem with calculating num columns per segment is
  1339. // two adjacent - high lod segments of different width can have
  1340. // verts that don't line up! So even though RiverSegment HAS a
  1341. // column data member we initialize all segments in the river to
  1342. // the same (River::mColumnCount)
  1343. // Calculate the size of divisions in the right direction ( p00 -> p10 )
  1344. // F32 segWidth = ( ( p11 - p01 ).len() + ( p10 - p00 ).len() ) * 0.5f;
  1345. // U32 numColumns = 5;
  1346. //F32 columnSize = segWidth / numColumns;
  1347. //while ( columnSize > mMaxDivisionSize )
  1348. //{
  1349. // numColumns++;
  1350. // columnSize = segWidth / numColumns;
  1351. //}
  1352. // Save the calculated numb of columns / rows for this segment.
  1353. segment.columns = mColumnCount;
  1354. segment.rows = numRows;
  1355. // Save the corresponding number of verts/prims
  1356. segment.numVerts = ( 1 + mColumnCount ) * ( 1 + numRows );
  1357. segment.numTriangles = mColumnCount * numRows * 2;
  1358. mHighVertCount += segment.numVerts;
  1359. mHighTriangleCount += segment.numTriangles;
  1360. }
  1361. // Number of low detail verts/prims.
  1362. mLowVertCount = mSlices.size() * 2;
  1363. mLowTriangleCount = mSegments.size() * 2;
  1364. // Allocate the low detail VertexBuffer,
  1365. // this will stay in memory and will never need to change.
  1366. mVB_low.set( GFX, mLowVertCount, GFXBufferTypeStatic );
  1367. GFXWaterVertex *lowVertPtr = mVB_low.lock();
  1368. U32 vertCounter = 0;
  1369. // The texCoord.y value start/end for a segment
  1370. // as we loop through them.
  1371. F32 textCoordV = 0;
  1372. //
  1373. // Fill the low-detail VertexBuffer
  1374. //
  1375. for ( U32 i = 0; i < mSlices.size(); i++ )
  1376. {
  1377. RiverSlice &slice = mSlices[i];
  1378. lowVertPtr->point = slice.p0;
  1379. lowVertPtr->normal = slice.normal;
  1380. lowVertPtr->undulateData.set( -slice.width*0.5f, textCoordV );
  1381. lowVertPtr->horizonFactor.set( 0, 0, 0, 0 );
  1382. lowVertPtr++;
  1383. vertCounter++;
  1384. lowVertPtr->point = slice.p2;
  1385. lowVertPtr->normal = slice.normal;
  1386. lowVertPtr->undulateData.set( slice.width*0.5f, textCoordV );
  1387. lowVertPtr->horizonFactor.set( 0, 0, 0, 0 );
  1388. lowVertPtr++;
  1389. vertCounter++;
  1390. // Save this so we can get it later.
  1391. slice.texCoordV = textCoordV;
  1392. if ( i < mSlices.size() - 1 )
  1393. {
  1394. // Increment the textCoordV for the next slice.
  1395. F32 segLen = ( mSlices[i+1].p1 - slice.p1 ).len();
  1396. textCoordV += segLen;
  1397. }
  1398. }
  1399. AssertFatal( vertCounter == mLowVertCount, "River, wrote incorrect number of verts in mBV_low!" );
  1400. // Unlock the low-detail VertexBuffer, we are done filling it.
  1401. mVB_low.unlock();
  1402. //
  1403. // Create the low-detail prim buffer(s)
  1404. //
  1405. mPB_low.set( GFX, mLowTriangleCount * 3, mLowTriangleCount, GFXBufferTypeStatic );
  1406. U16 *lowIdxBuff;
  1407. mPB_low.lock(&lowIdxBuff);
  1408. U32 curLowIdx = 0;
  1409. // Temporaries to hold indices for the corner points of a quad.
  1410. U32 p00, p01, p11, p10;
  1411. U32 offset = 0;
  1412. // Fill the low-detail PrimitiveBuffer
  1413. for ( U32 i = 0; i < mSegments.size(); i++ )
  1414. {
  1415. //const RiverSegment &segment = mSegments[i];
  1416. // Two triangles formed by the corner points of this segment
  1417. // into the the low detail primitive buffer.
  1418. p00 = offset;
  1419. p01 = p00 + 2;
  1420. p11 = p01 + 1;
  1421. p10 = p00 + 1;
  1422. // Upper-Left triangle
  1423. lowIdxBuff[curLowIdx] = p00;
  1424. curLowIdx++;
  1425. lowIdxBuff[curLowIdx] = p01;
  1426. curLowIdx++;
  1427. lowIdxBuff[curLowIdx] = p11;
  1428. curLowIdx++;
  1429. // Lower-Right Triangle
  1430. lowIdxBuff[curLowIdx] = p00;
  1431. curLowIdx++;
  1432. lowIdxBuff[curLowIdx] = p11;
  1433. curLowIdx++;
  1434. lowIdxBuff[curLowIdx] = p10;
  1435. curLowIdx++;
  1436. offset += 2;
  1437. }
  1438. AssertFatal( curLowIdx == mLowTriangleCount * 3, "River, wrote incorrect number of indices in mPB_low!" );
  1439. // Unlock the low-detail PrimitiveBuffer, we are done filling it.
  1440. mPB_low.unlock();
  1441. }
  1442. bool River::getClosestNode( const Point3F &pos, U32 &idx ) const
  1443. {
  1444. F32 closestDist = F32_MAX;
  1445. for ( U32 i = 0; i < mNodes.size(); i++ )
  1446. {
  1447. F32 dist = ( mNodes[i].point - pos ).len();
  1448. if ( dist < closestDist )
  1449. {
  1450. closestDist = dist;
  1451. idx = i;
  1452. }
  1453. }
  1454. return closestDist != F32_MAX;
  1455. }
  1456. bool River::containsPoint( const Point3F &worldPos, U32 *nodeIdx ) const
  1457. {
  1458. // If point isn't in the world box,
  1459. // it's definitely not in the River.
  1460. //if ( !getWorldBox().isContained( worldPos ) )
  1461. // return false;
  1462. // Look through all edges, does the polygon
  1463. // formed from adjacent edge's contain the worldPos?
  1464. for ( U32 i = 0; i < mSegments.size(); i++ )
  1465. {
  1466. const RiverSegment &segment = mSegments[i];
  1467. if ( segment.containsPoint( worldPos ) )
  1468. {
  1469. if ( nodeIdx )
  1470. *nodeIdx = i;
  1471. return true;
  1472. }
  1473. }
  1474. return false;
  1475. }
  1476. F32 River::distanceToSurface( const Point3F &pnt, U32 segmentIdx )
  1477. {
  1478. return mSegments[segmentIdx].distanceToSurface( pnt );
  1479. }
  1480. bool River::collideRay( const Point3F &origin, const Point3F &direction, U32 *nodeIdx, Point3F *collisionPnt ) const
  1481. {
  1482. Point3F p0 = origin;
  1483. Point3F p1 = origin + direction * 2000.0f;
  1484. // If the line segment does not collide with the river's world box,
  1485. // it definitely does not collide with any part of the river.
  1486. if ( !getWorldBox().collideLine( p0, p1 ) )
  1487. return false;
  1488. if ( mSlices.size() < 2 )
  1489. return false;
  1490. MathUtils::Quad quad;
  1491. MathUtils::Ray ray;
  1492. F32 t;
  1493. // Check each river segment (formed by a pair of slices) for collision
  1494. // with the line segment.
  1495. for ( U32 i = 0; i < mSlices.size() - 1; i++ )
  1496. {
  1497. const RiverSlice &slice0 = mSlices[i];
  1498. const RiverSlice &slice1 = mSlices[i+1];
  1499. // For simplicities sake we will only test for collision between the
  1500. // line segment and the Top face of the river segment.
  1501. // Clockwise starting with the leftmost/closest point.
  1502. quad.p00 = slice0.p0;
  1503. quad.p01 = slice1.p0;
  1504. quad.p11 = slice1.p2;
  1505. quad.p10 = slice0.p2;
  1506. ray.origin = origin;
  1507. ray.direction = direction;
  1508. // NOTE:
  1509. // mRayQuadCollide is designed for a "real" quad in which all four points
  1510. // are coplanar which is actually not the case here. The more twist
  1511. // and turn in-between two neighboring river slices the more incorrect
  1512. // this calculation will be.
  1513. if ( MathUtils::mRayQuadCollide( quad, ray, NULL, &t ) )
  1514. {
  1515. if ( nodeIdx )
  1516. *nodeIdx = slice0.parentNodeIdx;
  1517. if ( collisionPnt )
  1518. *collisionPnt = ray.origin + ray.direction * t;
  1519. return true;
  1520. }
  1521. }
  1522. return false;
  1523. }
  1524. Point3F River::getNodePosition( U32 idx ) const
  1525. {
  1526. if ( mNodes.size() - 1 < idx )
  1527. return Point3F();
  1528. return mNodes[idx].point;
  1529. }
  1530. void River::setNodePosition( U32 idx, const Point3F &pos )
  1531. {
  1532. if ( mNodes.size() - 1 < idx )
  1533. return;
  1534. mNodes[idx].point = pos;
  1535. regenerate();
  1536. setMaskBits( NodeMask | RegenMask );
  1537. }
  1538. U32 River::addNode( const Point3F &pos, const F32 &width, const F32 &depth, const VectorF &normal )
  1539. {
  1540. U32 idx = _addNode( pos, width, depth, normal );
  1541. regenerate();
  1542. setMaskBits( NodeMask | RegenMask );
  1543. return idx;
  1544. }
  1545. U32 River::insertNode(const Point3F &pos, const F32 &width, const F32 &depth, const VectorF &normal, const U32 &idx)
  1546. {
  1547. U32 ret = _insertNode( pos, width, depth, normal, idx );
  1548. regenerate();
  1549. setMaskBits( NodeMask | RegenMask );
  1550. return ret;
  1551. }
  1552. void River::setNode(const Point3F &pos, const F32 &width, const F32 &depth, const VectorF &normal, const U32 &idx)
  1553. {
  1554. if ( mNodes.size() - 1 < idx )
  1555. return;
  1556. RiverNode &node = mNodes[idx];
  1557. node.point = pos;
  1558. node.width = width;
  1559. node.depth = depth;
  1560. node.normal = normal;
  1561. regenerate();
  1562. setMaskBits( NodeMask | RegenMask );
  1563. }
  1564. void River::setNodeWidth( U32 idx, F32 meters )
  1565. {
  1566. meters = mClampF( meters, MIN_NODE_WIDTH, MAX_NODE_WIDTH );
  1567. if ( mNodes.size() - 1 < idx )
  1568. return;
  1569. mNodes[idx].width = meters;
  1570. _regenerate();
  1571. setMaskBits( RegenMask | NodeMask );
  1572. }
  1573. void River::setNodeHeight( U32 idx, F32 height )
  1574. {
  1575. if ( mNodes.size() - 1 < idx )
  1576. return;
  1577. mNodes[idx].point.z = height;
  1578. _regenerate();
  1579. setMaskBits( RegenMask | NodeMask );
  1580. }
  1581. F32 River::getNodeWidth( U32 idx ) const
  1582. {
  1583. if ( mNodes.size() - 1 < idx )
  1584. return -1.0f;
  1585. return mNodes[idx].width;
  1586. }
  1587. void River::setNodeDepth( U32 idx, F32 meters )
  1588. {
  1589. meters = mClampF( meters, MIN_NODE_DEPTH, MAX_NODE_DEPTH );
  1590. if ( mNodes.size() - 1 < idx )
  1591. return;
  1592. mNodes[idx].depth = meters;
  1593. _regenerate();
  1594. setMaskBits( RiverMask | RegenMask | NodeMask );
  1595. }
  1596. void River::setNodeNormal( U32 idx, const VectorF &normal )
  1597. {
  1598. if ( mNodes.size() - 1 < idx )
  1599. return;
  1600. mNodes[idx].normal = normal;
  1601. regenerate();
  1602. setMaskBits( NodeMask | RegenMask );
  1603. }
  1604. F32 River::getNodeDepth( U32 idx ) const
  1605. {
  1606. if ( mNodes.size() - 1 < idx )
  1607. return -1.0f;
  1608. return mNodes[idx].depth;
  1609. }
  1610. VectorF River::getNodeNormal( U32 idx ) const
  1611. {
  1612. if ( mNodes.size() - 1 < idx )
  1613. return VectorF::Zero;
  1614. return mNodes[idx].normal;
  1615. }
  1616. MatrixF River::getNodeTransform( U32 idx ) const
  1617. {
  1618. MatrixF mat(true);
  1619. if ( mNodes.size() - 1 < idx )
  1620. return mat;
  1621. bool hasNext = idx + 1 < mNodes.size();
  1622. bool hasPrev = (S32)idx - 1 >= 0;
  1623. const RiverNode &node = mNodes[idx];
  1624. VectorF fvec( 0, 1, 0 );
  1625. if ( hasNext )
  1626. {
  1627. fvec = mNodes[idx+1].point - node.point;
  1628. fvec.normalizeSafe();
  1629. }
  1630. else if ( hasPrev )
  1631. {
  1632. fvec = node.point - mNodes[idx-1].point;
  1633. fvec.normalizeSafe();
  1634. }
  1635. else
  1636. fvec = mPerp( node.normal );
  1637. if ( fvec.isZero() )
  1638. fvec = mPerp( node.normal );
  1639. F32 dot = mDot( fvec, node.normal );
  1640. if ( dot < -0.9f || dot > 0.9f )
  1641. fvec = mPerp( node.normal );
  1642. VectorF rvec = mCross( fvec, node.normal );
  1643. if ( rvec.isZero() )
  1644. rvec = mPerp( fvec );
  1645. rvec.normalize();
  1646. fvec = mCross( node.normal, rvec );
  1647. fvec.normalize();
  1648. mat.setColumn( 0, rvec );
  1649. mat.setColumn( 1, fvec );
  1650. mat.setColumn( 2, node.normal );
  1651. mat.setColumn( 3, node.point );
  1652. AssertFatal( m_matF_determinant( mat ) != 0.0f, "no inverse!");
  1653. return mat;
  1654. }
  1655. void River::deleteNode( U32 idx )
  1656. {
  1657. if ( mNodes.size() - 1 < idx )
  1658. return;
  1659. mNodes.erase(idx);
  1660. _regenerate();
  1661. setMaskBits( RegenMask | NodeMask );
  1662. }
  1663. void River::buildNodesFromList( RiverNodeList* list )
  1664. {
  1665. mNodes.clear();
  1666. for (U32 i=0; i<list->mPositions.size(); ++i)
  1667. {
  1668. _addNode( list->mPositions[i], list->mWidths[i], list->mDepths[i], list->mNormals[i] );
  1669. }
  1670. _regenerate();
  1671. }
  1672. void River::_makeRenderBatches( const Point3F &cameraPos )
  1673. {
  1674. // Loop through each segment to determine if it is either 1 [not visible], 2 [high LOD], 3 [low LOD]
  1675. mHighLODBatches.clear();
  1676. mLowLODBatches.clear();
  1677. // Keeps track of what we batch type we are currently collecting.
  1678. // -1 is uninitialized, 0 is low detail, 1 is high detail
  1679. S32 lastDetail = -1;
  1680. bool highDetail;
  1681. U32 startSegmentIdx = -1;
  1682. U32 endSegmentIdx = 0;
  1683. F32 lodDistSquared = mLodDistance * mLodDistance;
  1684. for ( U32 i = 0; i < mSegments.size(); i++ )
  1685. {
  1686. const RiverSegment &segment = mSegments[i];
  1687. const RiverSlice *slice = segment.slice0;
  1688. const RiverSlice *nextSlice = segment.slice1;
  1689. // TODO: add bounds BoxF to RiverSegment
  1690. const bool isVisible = true; //frustum.intersects( segment.bounds );
  1691. if ( isVisible )
  1692. {
  1693. F32 dist0 = MathUtils::mTriangleDistance( slice->p0, nextSlice->p0, nextSlice->p2, cameraPos );
  1694. F32 dist1 = MathUtils::mTriangleDistance( slice->p0, nextSlice->p2, slice->p2, cameraPos );
  1695. F32 dist = getMin( dist0, dist1 );
  1696. highDetail = ( dist < lodDistSquared );
  1697. if ( (highDetail && lastDetail == 0) ||
  1698. (!highDetail && lastDetail == 1) )
  1699. {
  1700. // We hit a segment with a different lod than the previous.
  1701. // Save what we have so far...
  1702. RiverRenderBatch batch;
  1703. batch.startSegmentIdx = startSegmentIdx;
  1704. batch.endSegmentIdx = endSegmentIdx;
  1705. if ( lastDetail == 0 )
  1706. {
  1707. mLowLODBatches.push_back( batch );
  1708. }
  1709. else
  1710. {
  1711. mHighLODBatches.push_back( batch );
  1712. }
  1713. // Reset the batching
  1714. startSegmentIdx = -1;
  1715. lastDetail = -1;
  1716. i--;
  1717. continue;
  1718. }
  1719. // If this is the start of a set of batches.
  1720. if ( startSegmentIdx == -1 )
  1721. {
  1722. endSegmentIdx = startSegmentIdx = i;
  1723. lastDetail = ( highDetail ) ? 1 : 0;
  1724. }
  1725. // Else we're extending the end batch index.
  1726. else
  1727. ++endSegmentIdx;
  1728. // If this isn't the last batch then continue.
  1729. if ( i < mSegments.size()-1 )
  1730. continue;
  1731. }
  1732. // If we still don't have a start batch skip.
  1733. if ( startSegmentIdx == -1 )
  1734. continue;
  1735. // Save what we have so far...
  1736. RiverRenderBatch batch;
  1737. batch.startSegmentIdx = startSegmentIdx;
  1738. batch.endSegmentIdx = endSegmentIdx;
  1739. if ( lastDetail == 0 )
  1740. {
  1741. mLowLODBatches.push_back( batch );
  1742. }
  1743. else
  1744. {
  1745. mHighLODBatches.push_back( batch );
  1746. }
  1747. // Reset the batching.
  1748. startSegmentIdx = -1;
  1749. lastDetail = -1;
  1750. }
  1751. }
  1752. void River::_makeHighLODBuffers()
  1753. {
  1754. PROFILE_SCOPE( River_makeHighLODBuffers );
  1755. // This is the number of verts/triangles for ALL high lod batches combined.
  1756. // eg. the size for the buffers.
  1757. U32 numVerts = 0;
  1758. U32 numTriangles = 0;
  1759. for ( U32 i = 0; i < mHighLODBatches.size(); i++ )
  1760. {
  1761. RiverRenderBatch &batch = mHighLODBatches[i];
  1762. for ( U32 j = batch.startSegmentIdx; j <= batch.endSegmentIdx; j++ )
  1763. {
  1764. const RiverSegment &segment = mSegments[j];
  1765. numTriangles += segment.numTriangles;
  1766. numVerts += segment.numVerts;
  1767. }
  1768. }
  1769. if ( numVerts > GFX_MAX_DYNAMIC_VERTS || numTriangles * 3 > GFX_MAX_DYNAMIC_INDICES )
  1770. {
  1771. mVB_high = NULL;
  1772. mPB_high = NULL;
  1773. return;
  1774. }
  1775. mHighTriangleCount = numTriangles;
  1776. mHighVertCount = numVerts;
  1777. mVB_high.set( GFX, numVerts, GFXBufferTypeVolatile );
  1778. GFXWaterVertex *vertPtr = mVB_high.lock();
  1779. U32 vertCounter = 0;
  1780. // NOTE: this will break if different segments have different number
  1781. // of columns, but that will also cause T-junction triangles so just don't
  1782. // do that.
  1783. // For each batch, loop through the segments contained by
  1784. // that batch, and add their verts to the buffer.
  1785. for ( U32 i = 0; i < mHighLODBatches.size(); i++ )
  1786. {
  1787. RiverRenderBatch &batch = mHighLODBatches[i];
  1788. batch.startVert = vertCounter;
  1789. batch.vertCount = 0;
  1790. VectorF lastNormal(0,0,1);
  1791. for ( U32 j = batch.startSegmentIdx; j <= batch.endSegmentIdx; j++ )
  1792. {
  1793. // Add the verts for this segment to the buffer.
  1794. RiverSegment &segment = mSegments[j];
  1795. BiSqrToQuad3D squareToQuad( segment.getP00(),
  1796. segment.getP10(),
  1797. segment.getP11(),
  1798. segment.getP01() );
  1799. // We are duplicating the last row of verts in a segment on
  1800. // the first row of the next segment. This could be optimized but
  1801. // shouldn't cause any problems.
  1802. VectorF normal = segment.getSurfaceNormal();
  1803. for ( U32 k = 0; k <= segment.rows; k++ )
  1804. {
  1805. VectorF vertNormal = ( k == 0 && j != batch.startSegmentIdx ) ? lastNormal : normal;
  1806. F32 rowLen = mLerp( segment.slice0->width, segment.slice1->width, (F32)k / (F32)segment.rows );
  1807. for ( U32 l = 0; l <= segment.columns; l++ )
  1808. {
  1809. // We are generating a "row" of verts along the forwardDivision
  1810. // Each l iteration is a step to the right along with row.
  1811. Point2F uv( (F32)l / (F32)segment.columns, (F32)k / (F32)segment.rows );
  1812. Point3F pnt = squareToQuad.transform( uv );
  1813. // Assign the Vert
  1814. vertPtr->point = pnt;
  1815. vertPtr->normal = vertNormal;
  1816. vertPtr->undulateData.x = ( uv.x - 0.5f ) * rowLen;
  1817. vertPtr->undulateData.y = ( segment.TexCoordEnd() - segment.TexCoordStart() ) * uv.y + segment.TexCoordStart();
  1818. vertPtr->horizonFactor.set( 0, 0, 0, 0 );
  1819. vertPtr++;
  1820. vertCounter++;
  1821. batch.vertCount++;
  1822. }
  1823. }
  1824. lastNormal = normal;
  1825. }
  1826. }
  1827. AssertFatal( vertCounter == mHighVertCount, "River, wrote incorrect number of verts in mVB_high" );
  1828. mVB_high.unlock();
  1829. //
  1830. // Do the high lod primitive buffer.
  1831. //
  1832. mPB_high.set( GFX, numTriangles * 3, numTriangles, GFXBufferTypeVolatile );
  1833. U16 *idxBuff;
  1834. mPB_high.lock(&idxBuff);
  1835. U32 curIdx = 0;
  1836. U32 batchOffset = 0;
  1837. // For each high lod batch, we must add indices to the buffer
  1838. // for each segment it contains ( and the count will depend on
  1839. // the division level columns/rows for each segment ).
  1840. // Temporaries for holding the indices of a quad
  1841. U32 p00, p01, p11, p10;
  1842. for ( U32 i = 0; i < mHighLODBatches.size(); i++ )
  1843. {
  1844. RiverRenderBatch &batch = mHighLODBatches[i];
  1845. batch.indexCount = 0;
  1846. batch.triangleCount = 0;
  1847. batch.startIndex = curIdx;
  1848. U32 temp = 0;
  1849. U32 segmentOffset = 0;
  1850. for ( U32 j = batch.startSegmentIdx; j <= batch.endSegmentIdx; j++ )
  1851. {
  1852. const RiverSegment &segment = mSegments[j];
  1853. // Loop through all divisions adding the indices to the
  1854. // high detail primitive buffer.
  1855. for ( U32 k = 0; k < segment.rows; k++ )
  1856. {
  1857. for ( U32 l = 0; l < segment.columns; l++ )
  1858. {
  1859. // The indices for this quad.
  1860. p00 = batchOffset + segmentOffset + l + k * ( segment.columns + 1 );
  1861. p01 = p00 + segment.columns + 1;
  1862. p11 = p01 + 1;
  1863. p10 = p00 + 1;
  1864. AssertFatal( p00 <= mHighTriangleCount * 3, "River, bad draw call!" );
  1865. AssertFatal( p01 <= mHighTriangleCount * 3, "River, bad draw call!" );
  1866. AssertFatal( p11 <= mHighTriangleCount * 3, "River, bad draw call!" );
  1867. AssertFatal( p10 <= mHighTriangleCount * 3, "River, bad draw call!" );
  1868. // Upper-Left triangle
  1869. idxBuff[curIdx] = p00;
  1870. curIdx++;
  1871. idxBuff[curIdx] = p01;
  1872. curIdx++;
  1873. idxBuff[curIdx] = p11;
  1874. curIdx++;
  1875. // Lower-Right Triangle
  1876. idxBuff[curIdx] = p00;
  1877. curIdx++;
  1878. idxBuff[curIdx] = p11;
  1879. curIdx++;
  1880. idxBuff[curIdx] = p10;
  1881. curIdx++;
  1882. batch.indexCount += 6;
  1883. batch.triangleCount += 2;
  1884. }
  1885. }
  1886. // Increment the sliceOffset by the number of verts
  1887. // used by this segment. So the next segment will index
  1888. // into new verts.
  1889. segmentOffset += ( segment.columns + 1 ) * ( segment.rows + 1 );
  1890. temp += ( segment.columns + 1 ) * ( segment.rows + 1 );
  1891. }
  1892. batchOffset += temp;
  1893. }
  1894. // Unlock the PrimitiveBuffer, we are done filling it.
  1895. mPB_high.unlock();
  1896. }
  1897. U32 River::_addNode( const Point3F &pos, const F32 &width, const F32 &depth, const VectorF &normal )
  1898. {
  1899. mNodes.increment();
  1900. RiverNode &node = mNodes.last();
  1901. node.point = pos;
  1902. node.width = width;
  1903. node.depth = depth;
  1904. node.normal = normal;
  1905. setMaskBits( NodeMask | RegenMask );
  1906. return mNodes.size() - 1;
  1907. }
  1908. U32 River::_insertNode( const Point3F &pos, const F32 &width, const F32 &depth, const VectorF &normal, const U32 &idx )
  1909. {
  1910. U32 ret;
  1911. RiverNode *node;
  1912. if ( idx == U32_MAX )
  1913. {
  1914. mNodes.increment();
  1915. node = &mNodes.last();
  1916. ret = mNodes.size() - 1;
  1917. }
  1918. else
  1919. {
  1920. mNodes.insert( idx );
  1921. node = &mNodes[idx];
  1922. ret = idx;
  1923. }
  1924. node->point = pos;
  1925. node->depth = depth;
  1926. node->width = width;
  1927. node->normal = normal;
  1928. return ret;
  1929. }
  1930. void River::setMetersPerSegment( F32 meters )
  1931. {
  1932. if ( meters < MIN_METERS_PER_SEGMENT )
  1933. {
  1934. Con::warnf( "River::setMetersPerSegment, specified meters (%g) is below the min meters (%g), NOT SET!", meters, MIN_METERS_PER_SEGMENT );
  1935. return;
  1936. }
  1937. mMetersPerSegment = meters;
  1938. _regenerate();
  1939. setMaskBits( RiverMask | RegenMask );
  1940. }
  1941. void River::setBatchSize( U32 size )
  1942. {
  1943. // Not functional
  1944. //mSegmentsPerBatch = size;
  1945. //_regenerate();
  1946. //setMaskBits( RiverMask | RegenMask );
  1947. }
  1948. void River::regenerate()
  1949. {
  1950. _regenerate();
  1951. setMaskBits( RegenMask );
  1952. }
  1953. void River::setMaxDivisionSize( F32 meters )
  1954. {
  1955. if ( meters < mMinDivisionSize )
  1956. mMaxDivisionSize = mMinDivisionSize;
  1957. else
  1958. mMaxDivisionSize = meters;
  1959. _regenerate();
  1960. setMaskBits( RiverMask | RegenMask );
  1961. }
  1962. //-------------------------------------------------------------------------
  1963. // Console Methods
  1964. //-------------------------------------------------------------------------
  1965. DefineEngineMethod( River, regenerate, void, (),,
  1966. "Intended as a helper to developers and editor scripts.\n"
  1967. "Force River to recreate its geometry."
  1968. )
  1969. {
  1970. object->regenerate();
  1971. }
  1972. DefineEngineMethod( River, setMetersPerSegment, void, ( F32 meters ),,
  1973. "Intended as a helper to developers and editor scripts.\n"
  1974. "@see SegmentLength field."
  1975. )
  1976. {
  1977. object->setMetersPerSegment( meters );
  1978. }
  1979. DefineEngineMethod( River, setBatchSize, void, ( F32 meters ),,
  1980. "Intended as a helper to developers and editor scripts.\n"
  1981. "BatchSize is not currently used."
  1982. )
  1983. {
  1984. object->setBatchSize( meters );
  1985. }
  1986. DefineEngineMethod( River, setNodeDepth, void, ( S32 idx, F32 meters ),,
  1987. "Intended as a helper to developers and editor scripts.\n"
  1988. "Sets the depth in meters of a particular node."
  1989. )
  1990. {
  1991. object->setNodeDepth( idx, meters );
  1992. }
  1993. DefineEngineMethod( River, setMaxDivisionSize, void, ( F32 meters ),,
  1994. "Intended as a helper to developers and editor scripts.\n"
  1995. "@see SubdivideLength field."
  1996. )
  1997. {
  1998. object->setMaxDivisionSize( meters );
  1999. }