meshRoad.cpp 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483
  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. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  23. // Arcane-FX for MIT Licensed Open Source version of Torque 3D from GarageGames
  24. // Copyright (C) 2015 Faust Logic, Inc.
  25. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  26. #include "platform/platform.h"
  27. #include "environment/meshRoad.h"
  28. #include "console/consoleTypes.h"
  29. #include "console/engineAPI.h"
  30. #include "util/catmullRom.h"
  31. #include "math/util/quadTransforms.h"
  32. #include "scene/simPath.h"
  33. #include "scene/sceneRenderState.h"
  34. #include "scene/sceneManager.h"
  35. #include "scene/sgUtil.h"
  36. #include "renderInstance/renderPassManager.h"
  37. #include "T3D/gameBase/gameConnection.h"
  38. #include "core/stream/bitStream.h"
  39. #include "gfx/gfxDrawUtil.h"
  40. #include "gfx/gfxTransformSaver.h"
  41. #include "gfx/primBuilder.h"
  42. #include "gfx/gfxDebugEvent.h"
  43. #include "materials/materialManager.h"
  44. #include "math/mathIO.h"
  45. #include "math/mathUtils.h"
  46. #include "math/util/frustum.h"
  47. #include "gui/3d/guiTSControl.h"
  48. #include "materials/shaderData.h"
  49. #include "gfx/sim/gfxStateBlockData.h"
  50. #include "gfx/sim/debugDraw.h"
  51. #include "collision/concretePolyList.h"
  52. #include "T3D/physics/physicsPlugin.h"
  53. #include "T3D/physics/physicsBody.h"
  54. #include "T3D/physics/physicsCollision.h"
  55. #include "environment/nodeListManager.h"
  56. #include "afx/ce/afxZodiacMgr.h"
  57. #define MIN_METERS_PER_SEGMENT 1.0f
  58. #define MIN_NODE_DEPTH 0.25f
  59. #define MAX_NODE_DEPTH 50.0f
  60. #define MIN_NODE_WIDTH 0.25f
  61. #define MAX_NODE_WIDTH 50.0f
  62. static U32 gIdxArray[6][2][3] = {
  63. { { 0, 4, 5 }, { 0, 5, 1 }, }, // Top Face
  64. { { 2, 6, 4 }, { 2, 4, 0 }, }, // Left Face
  65. { { 1, 5, 7 }, { 1, 7, 3 }, }, // Right Face
  66. { { 2, 3, 7 }, { 2, 7, 6 }, }, // Bottom Face
  67. { { 0, 1, 3 }, { 0, 3, 2 }, }, // Front Face
  68. { { 4, 6, 7 }, { 4, 7, 5 }, }, // Back Face
  69. };
  70. static S32 QSORT_CALLBACK compareHitSegments(const void* a,const void* b)
  71. {
  72. const MeshRoadHitSegment *fa = (MeshRoadHitSegment*)a;
  73. const MeshRoadHitSegment *fb = (MeshRoadHitSegment*)b;
  74. return mSign(fb->t - fa->t);
  75. }
  76. //-----------------------------------------------------------------------------
  77. // MeshRoadNodeList Struct
  78. //-----------------------------------------------------------------------------
  79. struct MeshRoadNodeList : public NodeListManager::NodeList
  80. {
  81. Vector<Point3F> mPositions;
  82. Vector<F32> mWidths;
  83. Vector<F32> mDepths;
  84. Vector<VectorF> mNormals;
  85. MeshRoadNodeList() { }
  86. virtual ~MeshRoadNodeList() { }
  87. };
  88. //-----------------------------------------------------------------------------
  89. // MeshRoadNodeEvent Class
  90. //-----------------------------------------------------------------------------
  91. class MeshRoadNodeEvent : public NodeListEvent
  92. {
  93. typedef NodeListEvent Parent;
  94. public:
  95. Vector<Point3F> mPositions;
  96. Vector<F32> mWidths;
  97. Vector<F32> mDepths;
  98. Vector<VectorF> mNormals;
  99. public:
  100. MeshRoadNodeEvent() { mNodeList = NULL; }
  101. virtual ~MeshRoadNodeEvent() { }
  102. virtual void pack(NetConnection*, BitStream*);
  103. virtual void unpack(NetConnection*, BitStream*);
  104. virtual void copyIntoList(NodeListManager::NodeList* copyInto);
  105. virtual void padListToSize();
  106. DECLARE_CONOBJECT(MeshRoadNodeEvent);
  107. };
  108. void MeshRoadNodeEvent::pack(NetConnection* conn, BitStream* stream)
  109. {
  110. Parent::pack( conn, stream );
  111. stream->writeInt( mPositions.size(), 16 );
  112. for (U32 i=0; i<mPositions.size(); ++i)
  113. {
  114. mathWrite( *stream, mPositions[i] );
  115. stream->write( mWidths[i] );
  116. stream->write( mDepths[i] );
  117. mathWrite( *stream, mNormals[i] );
  118. }
  119. }
  120. void MeshRoadNodeEvent::unpack(NetConnection* conn, BitStream* stream)
  121. {
  122. mNodeList = new MeshRoadNodeList();
  123. Parent::unpack( conn, stream );
  124. U32 count = stream->readInt( 16 );
  125. Point3F pos;
  126. F32 width, depth;
  127. VectorF normal;
  128. MeshRoadNodeList* list = static_cast<MeshRoadNodeList*>(mNodeList);
  129. for (U32 i=0; i<count; ++i)
  130. {
  131. mathRead( *stream, &pos );
  132. stream->read( &width );
  133. stream->read( &depth );
  134. mathRead( *stream, &normal );
  135. list->mPositions.push_back( pos );
  136. list->mWidths.push_back( width );
  137. list->mDepths.push_back( depth );
  138. list->mNormals.push_back( normal );
  139. }
  140. list->mTotalValidNodes = count;
  141. // Do we have a complete list?
  142. if (list->mPositions.size() >= mTotalNodes)
  143. list->mListComplete = true;
  144. }
  145. void MeshRoadNodeEvent::copyIntoList(NodeListManager::NodeList* copyInto)
  146. {
  147. MeshRoadNodeList* prevList = dynamic_cast<MeshRoadNodeList*>(copyInto);
  148. MeshRoadNodeList* list = static_cast<MeshRoadNodeList*>(mNodeList);
  149. // Merge our list with the old list.
  150. for (U32 i=mLocalListStart, index=0; i<mLocalListStart+list->mPositions.size(); ++i, ++index)
  151. {
  152. prevList->mPositions[i] = list->mPositions[index];
  153. prevList->mWidths[i] = list->mWidths[index];
  154. prevList->mDepths[i] = list->mDepths[index];
  155. prevList->mNormals[i] = list->mNormals[index];
  156. }
  157. }
  158. void MeshRoadNodeEvent::padListToSize()
  159. {
  160. MeshRoadNodeList* list = static_cast<MeshRoadNodeList*>(mNodeList);
  161. U32 totalValidNodes = list->mTotalValidNodes;
  162. // Pad our list front?
  163. if (mLocalListStart)
  164. {
  165. MeshRoadNodeList* newlist = new MeshRoadNodeList();
  166. newlist->mPositions.increment(mLocalListStart);
  167. newlist->mWidths.increment(mLocalListStart);
  168. newlist->mDepths.increment(mLocalListStart);
  169. newlist->mNormals.increment(mLocalListStart);
  170. newlist->mPositions.merge(list->mPositions);
  171. newlist->mWidths.merge(list->mWidths);
  172. newlist->mDepths.merge(list->mDepths);
  173. newlist->mNormals.merge(list->mNormals);
  174. delete list;
  175. mNodeList = list = newlist;
  176. }
  177. // Pad our list end?
  178. if (list->mPositions.size() < mTotalNodes)
  179. {
  180. U32 delta = mTotalNodes - list->mPositions.size();
  181. list->mPositions.increment(delta);
  182. list->mWidths.increment(delta);
  183. list->mDepths.increment(delta);
  184. list->mNormals.increment(delta);
  185. }
  186. list->mTotalValidNodes = totalValidNodes;
  187. }
  188. IMPLEMENT_CO_NETEVENT_V1(MeshRoadNodeEvent);
  189. ConsoleDocClass( MeshRoadNodeEvent,
  190. "@brief Sends messages to the Mesh Road Editor\n\n"
  191. "Editor use only.\n\n"
  192. "@internal"
  193. );
  194. //-----------------------------------------------------------------------------
  195. // MeshRoadNodeListNotify Class
  196. //-----------------------------------------------------------------------------
  197. class MeshRoadNodeListNotify : public NodeListNotify
  198. {
  199. typedef NodeListNotify Parent;
  200. protected:
  201. SimObjectPtr<MeshRoad> mRoad;
  202. public:
  203. MeshRoadNodeListNotify( MeshRoad* road, U32 listId ) { mRoad = road; mListId = listId; }
  204. virtual ~MeshRoadNodeListNotify() { mRoad = NULL; }
  205. virtual void sendNotification( NodeListManager::NodeList* list );
  206. };
  207. void MeshRoadNodeListNotify::sendNotification( NodeListManager::NodeList* list )
  208. {
  209. if (mRoad.isValid())
  210. {
  211. // Build the road's nodes
  212. MeshRoadNodeList* roadList = dynamic_cast<MeshRoadNodeList*>( list );
  213. if (roadList)
  214. mRoad->buildNodesFromList( roadList );
  215. }
  216. }
  217. //------------------------------------------------------------------------------
  218. // MeshRoadConvex Class
  219. //------------------------------------------------------------------------------
  220. const MatrixF& MeshRoadConvex::getTransform() const
  221. {
  222. return MatrixF::Identity; //mObject->getTransform();
  223. }
  224. Box3F MeshRoadConvex::getBoundingBox() const
  225. {
  226. return box;
  227. }
  228. Box3F MeshRoadConvex::getBoundingBox(const MatrixF& mat, const Point3F& scale) const
  229. {
  230. Box3F newBox = box;
  231. newBox.minExtents.convolve(scale);
  232. newBox.maxExtents.convolve(scale);
  233. mat.mul(newBox);
  234. return newBox;
  235. }
  236. Point3F MeshRoadConvex::support(const VectorF& vec) const
  237. {
  238. F32 bestDot = mDot( verts[0], vec );
  239. const Point3F *bestP = &verts[0];
  240. for(S32 i=1; i<4; i++)
  241. {
  242. F32 newD = mDot(verts[i], vec);
  243. if(newD > bestDot)
  244. {
  245. bestDot = newD;
  246. bestP = &verts[i];
  247. }
  248. }
  249. return *bestP;
  250. }
  251. void MeshRoadConvex::getFeatures(const MatrixF& mat, const VectorF& n, ConvexFeature* cf)
  252. {
  253. cf->material = 0;
  254. cf->object = mObject;
  255. // For a tetrahedron this is pretty easy... first
  256. // convert everything into world space.
  257. Point3F tverts[4];
  258. mat.mulP(verts[0], &tverts[0]);
  259. mat.mulP(verts[1], &tverts[1]);
  260. mat.mulP(verts[2], &tverts[2]);
  261. mat.mulP(verts[3], &tverts[3]);
  262. // Points...
  263. S32 firstVert = cf->mVertexList.size();
  264. cf->mVertexList.increment(); cf->mVertexList.last() = tverts[0];
  265. cf->mVertexList.increment(); cf->mVertexList.last() = tverts[1];
  266. cf->mVertexList.increment(); cf->mVertexList.last() = tverts[2];
  267. cf->mVertexList.increment(); cf->mVertexList.last() = tverts[3];
  268. // Edges...
  269. cf->mEdgeList.increment();
  270. cf->mEdgeList.last().vertex[0] = firstVert+0;
  271. cf->mEdgeList.last().vertex[1] = firstVert+1;
  272. cf->mEdgeList.increment();
  273. cf->mEdgeList.last().vertex[0] = firstVert+1;
  274. cf->mEdgeList.last().vertex[1] = firstVert+2;
  275. cf->mEdgeList.increment();
  276. cf->mEdgeList.last().vertex[0] = firstVert+2;
  277. cf->mEdgeList.last().vertex[1] = firstVert+0;
  278. cf->mEdgeList.increment();
  279. cf->mEdgeList.last().vertex[0] = firstVert+3;
  280. cf->mEdgeList.last().vertex[1] = firstVert+0;
  281. cf->mEdgeList.increment();
  282. cf->mEdgeList.last().vertex[0] = firstVert+3;
  283. cf->mEdgeList.last().vertex[1] = firstVert+1;
  284. cf->mEdgeList.increment();
  285. cf->mEdgeList.last().vertex[0] = firstVert+3;
  286. cf->mEdgeList.last().vertex[1] = firstVert+2;
  287. // Triangles...
  288. cf->mFaceList.increment();
  289. cf->mFaceList.last().normal = PlaneF(tverts[2], tverts[1], tverts[0]);
  290. cf->mFaceList.last().vertex[0] = firstVert+2;
  291. cf->mFaceList.last().vertex[1] = firstVert+1;
  292. cf->mFaceList.last().vertex[2] = firstVert+0;
  293. cf->mFaceList.increment();
  294. cf->mFaceList.last().normal = PlaneF(tverts[1], tverts[0], tverts[3]);
  295. cf->mFaceList.last().vertex[0] = firstVert+1;
  296. cf->mFaceList.last().vertex[1] = firstVert+0;
  297. cf->mFaceList.last().vertex[2] = firstVert+3;
  298. cf->mFaceList.increment();
  299. cf->mFaceList.last().normal = PlaneF(tverts[2], tverts[1], tverts[3]);
  300. cf->mFaceList.last().vertex[0] = firstVert+2;
  301. cf->mFaceList.last().vertex[1] = firstVert+1;
  302. cf->mFaceList.last().vertex[2] = firstVert+3;
  303. cf->mFaceList.increment();
  304. cf->mFaceList.last().normal = PlaneF(tverts[0], tverts[2], tverts[3]);
  305. cf->mFaceList.last().vertex[0] = firstVert+0;
  306. cf->mFaceList.last().vertex[1] = firstVert+2;
  307. cf->mFaceList.last().vertex[2] = firstVert+3;
  308. }
  309. void MeshRoadConvex::getPolyList( AbstractPolyList* list )
  310. {
  311. // Transform the list into object space and set the pointer to the object
  312. //MatrixF i( mObject->getTransform() );
  313. //Point3F iS( mObject->getScale() );
  314. //list->setTransform(&i, iS);
  315. list->setTransform( &MatrixF::Identity, Point3F::One );
  316. list->setObject(mObject);
  317. // Points...
  318. S32 base = list->addPoint(verts[1]);
  319. list->addPoint(verts[2]);
  320. list->addPoint(verts[0]);
  321. list->addPoint(verts[3]);
  322. // Planes...
  323. list->begin(0,0);
  324. list->vertex(base + 2);
  325. list->vertex(base + 1);
  326. list->vertex(base + 0);
  327. list->plane(base + 2, base + 1, base + 0);
  328. list->end();
  329. list->begin(0,0);
  330. list->vertex(base + 2);
  331. list->vertex(base + 1);
  332. list->vertex(base + 3);
  333. list->plane(base + 2, base + 1, base + 3);
  334. list->end();
  335. list->begin(0,0);
  336. list->vertex(base + 3);
  337. list->vertex(base + 1);
  338. list->vertex(base + 0);
  339. list->plane(base + 3, base + 1, base + 0);
  340. list->end();
  341. list->begin(0,0);
  342. list->vertex(base + 2);
  343. list->vertex(base + 3);
  344. list->vertex(base + 0);
  345. list->plane(base + 2, base + 3, base + 0);
  346. list->end();
  347. }
  348. //------------------------------------------------------------------------------
  349. // MeshRoadSegment Class
  350. //------------------------------------------------------------------------------
  351. MeshRoadSegment::MeshRoadSegment()
  352. {
  353. mPlaneCount = 0;
  354. columns = 0;
  355. rows = 0;
  356. numVerts = 0;
  357. numTriangles = 0;
  358. startVert = 0;
  359. endVert = 0;
  360. startIndex = 0;
  361. endIndex = 0;
  362. slice0 = NULL;
  363. slice1 = NULL;
  364. }
  365. MeshRoadSegment::MeshRoadSegment( MeshRoadSlice *rs0, MeshRoadSlice *rs1, const MatrixF &roadMat )
  366. {
  367. columns = 0;
  368. rows = 0;
  369. numVerts = 0;
  370. numTriangles = 0;
  371. startVert = 0;
  372. endVert = 0;
  373. startIndex = 0;
  374. endIndex = 0;
  375. slice0 = rs0;
  376. slice1 = rs1;
  377. // Calculate the bounding box(s)
  378. worldbounds.minExtents = worldbounds.maxExtents = rs0->p0;
  379. worldbounds.extend( rs0->p2 );
  380. worldbounds.extend( rs0->pb0 );
  381. worldbounds.extend( rs0->pb2 );
  382. worldbounds.extend( rs1->p0 );
  383. worldbounds.extend( rs1->p2 );
  384. worldbounds.extend( rs1->pb0 );
  385. worldbounds.extend( rs1->pb2 );
  386. objectbounds = worldbounds;
  387. roadMat.mul( objectbounds );
  388. // Calculate the planes for this segment
  389. // Will be used for intersection/buoyancy tests
  390. mPlaneCount = 6;
  391. mPlanes[0].set( slice0->pb0, slice0->p0, slice1->p0 ); // left
  392. mPlanes[1].set( slice1->pb2, slice1->p2, slice0->p2 ); // right
  393. mPlanes[2].set( slice0->pb2, slice0->p2, slice0->p0 ); // near
  394. mPlanes[3].set( slice1->p0, slice1->p2, slice1->pb2 ); // far
  395. mPlanes[4].set( slice1->p2, slice1->p0, slice0->p0 ); // top
  396. mPlanes[5].set( slice0->pb0, slice1->pb0, slice1->pb2 ); // bottom
  397. }
  398. void MeshRoadSegment::set( MeshRoadSlice *rs0, MeshRoadSlice *rs1 )
  399. {
  400. columns = 0;
  401. rows = 0;
  402. numVerts = 0;
  403. numTriangles = 0;
  404. startVert = 0;
  405. endVert = 0;
  406. startIndex = 0;
  407. endIndex = 0;
  408. slice0 = rs0;
  409. slice1 = rs1;
  410. }
  411. bool MeshRoadSegment::intersectBox( const Box3F &bounds ) const
  412. {
  413. // This code copied from Frustum class.
  414. Point3F maxPoint;
  415. F32 maxDot;
  416. // Note the planes are ordered left, right, near,
  417. // far, top, bottom for getting early rejections
  418. // from the typical horizontal scene.
  419. for ( S32 i = 0; i < mPlaneCount; i++ )
  420. {
  421. // This is pretty much as optimal as you can
  422. // get for a plane vs AABB test...
  423. //
  424. // 4 comparisons
  425. // 3 multiplies
  426. // 2 adds
  427. // 1 negation
  428. //
  429. // It will early out as soon as it detects the
  430. // bounds is outside one of the planes.
  431. if ( mPlanes[i].x > 0 )
  432. maxPoint.x = bounds.maxExtents.x;
  433. else
  434. maxPoint.x = bounds.minExtents.x;
  435. if ( mPlanes[i].y > 0 )
  436. maxPoint.y = bounds.maxExtents.y;
  437. else
  438. maxPoint.y = bounds.minExtents.y;
  439. if ( mPlanes[i].z > 0 )
  440. maxPoint.z = bounds.maxExtents.z;
  441. else
  442. maxPoint.z = bounds.minExtents.z;
  443. maxDot = mDot( maxPoint, mPlanes[ i ] );
  444. if ( maxDot <= -mPlanes[ i ].d )
  445. return false;
  446. }
  447. return true;
  448. }
  449. bool MeshRoadSegment::containsPoint( const Point3F &pnt ) const
  450. {
  451. // This code from Frustum class.
  452. F32 maxDot;
  453. // Note the planes are ordered left, right, near,
  454. // far, top, bottom for getting early rejections
  455. // from the typical horizontal scene.
  456. for ( S32 i = 0; i < mPlaneCount; i++ )
  457. {
  458. const PlaneF &plane = mPlanes[ i ];
  459. // This is pretty much as optimal as you can
  460. // get for a plane vs point test...
  461. //
  462. // 1 comparison
  463. // 2 multiplies
  464. // 1 adds
  465. //
  466. // It will early out as soon as it detects the
  467. // point is outside one of the planes.
  468. maxDot = mDot( pnt, plane ) + plane.d;
  469. if ( maxDot < 0.0f )
  470. return false;
  471. }
  472. return true;
  473. }
  474. F32 MeshRoadSegment::distanceToSurface(const Point3F &pnt) const
  475. {
  476. return mPlanes[4].distToPlane( pnt );
  477. }
  478. //------------------------------------------------------------------------------
  479. // MeshRoad Class
  480. //------------------------------------------------------------------------------
  481. ConsoleDocClass( MeshRoad,
  482. "@brief A strip of rectangular mesh segments defined by a 3D spline "
  483. "for prototyping road-shaped objects in your scene.\n\n"
  484. "User may control width and depth per node, overall spline shape in three "
  485. "dimensions, and seperate Materials for rendering the top, bottom, and side surfaces.\n\n"
  486. "MeshRoad is not capable of handling intersections, branches, curbs, or other "
  487. "desirable features in a final 'road' asset and is therefore intended for "
  488. "prototyping and experimentation.\n\n"
  489. "Materials assigned to MeshRoad should tile vertically.\n\n"
  490. "@ingroup Terrain"
  491. );
  492. bool MeshRoad::smEditorOpen = false;
  493. bool MeshRoad::smShowBatches = false;
  494. bool MeshRoad::smShowSpline = true;
  495. bool MeshRoad::smShowRoad = true;
  496. bool MeshRoad::smWireframe = true;
  497. SimObjectPtr<SimSet> MeshRoad::smServerMeshRoadSet = NULL;
  498. GFXStateBlockRef MeshRoad::smWireframeSB;
  499. IMPLEMENT_CO_NETOBJECT_V1(MeshRoad);
  500. MeshRoad::MeshRoad()
  501. : mTextureLength( 5.0f ),
  502. mBreakAngle( 3.0f ),
  503. mWidthSubdivisions( 0 ),
  504. mPhysicsRep( NULL )
  505. {
  506. mConvexList = new Convex;
  507. // Setup NetObject.
  508. mTypeMask |= StaticObjectType | StaticShapeObjectType;
  509. mNetFlags.set(Ghostable);
  510. mMatInst[Top] = NULL;
  511. mMatInst[Bottom] = NULL;
  512. mMatInst[Side] = NULL;
  513. mTypeMask |= TerrainLikeObjectType;
  514. }
  515. MeshRoad::~MeshRoad()
  516. {
  517. delete mConvexList;
  518. mConvexList = NULL;
  519. }
  520. void MeshRoad::initPersistFields()
  521. {
  522. addGroup( "MeshRoad" );
  523. addField( "topMaterial", TypeMaterialName, Offset( mMaterialName[Top], MeshRoad ),
  524. "Material for the upper surface of the road." );
  525. addField( "bottomMaterial", TypeMaterialName, Offset( mMaterialName[Bottom], MeshRoad ),
  526. "Material for the bottom surface of the road." );
  527. addField( "sideMaterial", TypeMaterialName, Offset( mMaterialName[Side], MeshRoad ),
  528. "Material for the left, right, front, and back surfaces of the road." );
  529. addField( "textureLength", TypeF32, Offset( mTextureLength, MeshRoad ),
  530. "The length in meters of textures mapped to the MeshRoad." );
  531. addField( "breakAngle", TypeF32, Offset( mBreakAngle, MeshRoad ),
  532. "Angle in degrees - MeshRoad will subdivide the spline if its curve is greater than this threshold." );
  533. addField( "widthSubdivisions", TypeS32, Offset( mWidthSubdivisions, MeshRoad ),
  534. "Subdivide segments widthwise this many times when generating vertices." );
  535. endGroup( "MeshRoad" );
  536. addGroup( "Internal" );
  537. addProtectedField( "Node", TypeString, NULL, &addNodeFromField, &emptyStringProtectedGetFn,
  538. "Do not modify, for internal use." );
  539. endGroup( "Internal" );
  540. Parent::initPersistFields();
  541. }
  542. void MeshRoad::consoleInit()
  543. {
  544. Parent::consoleInit();
  545. Con::addVariable( "$MeshRoad::EditorOpen", TypeBool, &MeshRoad::smEditorOpen, "True if the MeshRoad editor is open, otherwise false.\n"
  546. "@ingroup Editors\n");
  547. Con::addVariable( "$MeshRoad::wireframe", TypeBool, &MeshRoad::smWireframe, "If true, will render the wireframe of the road.\n"
  548. "@ingroup Editors\n");
  549. Con::addVariable( "$MeshRoad::showBatches", TypeBool, &MeshRoad::smShowBatches, "Determines if the debug rendering of the batches cubes is displayed or not.\n"
  550. "@ingroup Editors\n");
  551. Con::addVariable( "$MeshRoad::showSpline", TypeBool, &MeshRoad::smShowSpline, "If true, the spline on which the curvature of this road is based will be rendered.\n"
  552. "@ingroup Editors\n");
  553. Con::addVariable( "$MeshRoad::showRoad", TypeBool, &MeshRoad::smShowRoad, "If true, the road will be rendered. When in the editor, roads are always rendered regardless of this flag.\n"
  554. "@ingroup Editors\n");
  555. }
  556. bool MeshRoad::addNodeFromField( void *object, const char *index, const char *data )
  557. {
  558. MeshRoad *pObj = static_cast<MeshRoad*>(object);
  559. //if ( !pObj->isProperlyAdded() )
  560. //{
  561. F32 width, depth;
  562. Point3F pos, normal;
  563. U32 result = dSscanf( data, "%g %g %g %g %g %g %g %g", &pos.x, &pos.y, &pos.z, &width, &depth, &normal.x, &normal.y, &normal.z );
  564. if ( result == 8 )
  565. pObj->_addNode( pos, width, depth, normal );
  566. //}
  567. return false;
  568. }
  569. bool MeshRoad::onAdd()
  570. {
  571. if ( !Parent::onAdd() )
  572. return false;
  573. // Reset the World Box.
  574. //setGlobalBounds();
  575. resetWorldBox();
  576. // Set the Render Transform.
  577. setRenderTransform(mObjToWorld);
  578. // Add to ServerMeshRoadSet
  579. if ( isServerObject() )
  580. {
  581. getServerSet()->addObject( this );
  582. }
  583. if ( isClientObject() )
  584. _initMaterial();
  585. // Generate the Vert/Index buffers and everything else.
  586. _regenerate();
  587. // Add to Scene.
  588. addToScene();
  589. return true;
  590. }
  591. void MeshRoad::onRemove()
  592. {
  593. SAFE_DELETE( mPhysicsRep );
  594. mConvexList->nukeList();
  595. for ( U32 i = 0; i < SurfaceCount; i++ )
  596. {
  597. SAFE_DELETE( mMatInst[i] );
  598. }
  599. removeFromScene();
  600. Parent::onRemove();
  601. }
  602. void MeshRoad::inspectPostApply()
  603. {
  604. // Set Parent.
  605. Parent::inspectPostApply();
  606. //if ( mMetersPerSegment < MIN_METERS_PER_SEGMENT )
  607. // mMetersPerSegment = MIN_METERS_PER_SEGMENT;
  608. setMaskBits(MeshRoadMask);
  609. }
  610. void MeshRoad::onStaticModified( const char* slotName, const char*newValue )
  611. {
  612. Parent::onStaticModified( slotName, newValue );
  613. if ( dStricmp( slotName, "breakAngle" ) == 0 )
  614. {
  615. setMaskBits( RegenMask );
  616. }
  617. }
  618. void MeshRoad::writeFields( Stream &stream, U32 tabStop )
  619. {
  620. Parent::writeFields( stream, tabStop );
  621. // Now write all nodes
  622. stream.write(2, "\r\n");
  623. for ( U32 i = 0; i < mNodes.size(); i++ )
  624. {
  625. const MeshRoadNode &node = mNodes[i];
  626. stream.writeTabs(tabStop);
  627. char buffer[1024];
  628. dMemset( buffer, 0, 1024 );
  629. dSprintf( buffer, 1024, "Node = \"%g %g %g %g %g %g %g %g\";", node.point.x, node.point.y, node.point.z, node.width, node.depth, node.normal.x, node.normal.y, node.normal.z );
  630. stream.writeLine( (const U8*)buffer );
  631. }
  632. }
  633. bool MeshRoad::writeField( StringTableEntry fieldname, const char *value )
  634. {
  635. if ( fieldname == StringTable->insert("Node") )
  636. return false;
  637. return Parent::writeField( fieldname, value );
  638. }
  639. void MeshRoad::onEditorEnable()
  640. {
  641. }
  642. void MeshRoad::onEditorDisable()
  643. {
  644. }
  645. SimSet* MeshRoad::getServerSet()
  646. {
  647. if ( !smServerMeshRoadSet )
  648. {
  649. smServerMeshRoadSet = new SimSet();
  650. smServerMeshRoadSet->registerObject( "ServerMeshRoadSet" );
  651. Sim::getRootGroup()->addObject( smServerMeshRoadSet );
  652. }
  653. return smServerMeshRoadSet;
  654. }
  655. void MeshRoad::prepRenderImage( SceneRenderState* state )
  656. {
  657. if ( mNodes.size() <= 1 )
  658. return;
  659. RenderPassManager *renderPass = state->getRenderPass();
  660. // Normal Road RenderInstance
  661. // Always rendered when the editor is not open
  662. // otherwise obey the smShowRoad flag
  663. if ( smShowRoad || !smEditorOpen )
  664. {
  665. afxZodiacMgr::renderMeshRoadZodiacs(state, this);
  666. MeshRenderInst coreRI;
  667. coreRI.clear();
  668. coreRI.objectToWorld = &MatrixF::Identity;
  669. coreRI.worldToCamera = renderPass->allocSharedXform(RenderPassManager::View);
  670. coreRI.projection = renderPass->allocSharedXform(RenderPassManager::Projection);
  671. coreRI.type = RenderPassManager::RIT_Mesh;
  672. BaseMatInstance *matInst;
  673. for ( U32 i = 0; i < SurfaceCount; i++ )
  674. {
  675. matInst = state->getOverrideMaterial( mMatInst[i] );
  676. if ( !matInst )
  677. continue;
  678. // Get the lights if we haven't already.
  679. if ( matInst->isForwardLit() && !coreRI.lights[0] )
  680. {
  681. LightQuery query;
  682. query.init( getWorldSphere() );
  683. query.getLights( coreRI.lights, 8 );
  684. }
  685. MeshRenderInst *ri = renderPass->allocInst<MeshRenderInst>();
  686. *ri = coreRI;
  687. // Currently rendering whole road, fix to cull and batch
  688. // per segment.
  689. // Set the correct material for rendering.
  690. ri->matInst = matInst;
  691. ri->vertBuff = &mVB[i];
  692. ri->primBuff = &mPB[i];
  693. ri->prim = renderPass->allocPrim();
  694. ri->prim->type = GFXTriangleList;
  695. ri->prim->minIndex = 0;
  696. ri->prim->startIndex = 0;
  697. ri->prim->numPrimitives = mTriangleCount[i];
  698. ri->prim->startVertex = 0;
  699. ri->prim->numVertices = mVertCount[i];
  700. // We sort by the material then vertex buffer.
  701. ri->defaultKey = matInst->getStateHint();
  702. ri->defaultKey2 = (uintptr_t)ri->vertBuff; // Not 64bit safe!
  703. renderPass->addInst( ri );
  704. }
  705. }
  706. // Debug RenderInstance
  707. // Only when editor is open.
  708. if ( smEditorOpen )
  709. {
  710. ObjectRenderInst *ri = state->getRenderPass()->allocInst<ObjectRenderInst>();
  711. ri->renderDelegate.bind( this, &MeshRoad::_debugRender );
  712. ri->type = RenderPassManager::RIT_Editor;
  713. state->getRenderPass()->addInst( ri );
  714. }
  715. }
  716. void MeshRoad::_initMaterial()
  717. {
  718. for ( U32 i = 0; i < SurfaceCount; i++ )
  719. {
  720. if ( mMatInst[i] )
  721. SAFE_DELETE( mMatInst[i] );
  722. if ( mMaterial[i] )
  723. mMatInst[i] = mMaterial[i]->createMatInstance();
  724. else
  725. mMatInst[i] = MATMGR->createMatInstance( "WarningMaterial" );
  726. mMatInst[i]->init( MATMGR->getDefaultFeatures(), getGFXVertexFormat<GFXVertexPNTT>() );
  727. }
  728. }
  729. void MeshRoad::_debugRender( ObjectRenderInst *ri, SceneRenderState *state, BaseMatInstance* )
  730. {
  731. //MeshRoadConvex convex;
  732. //buildConvex( Box3F(true), convex );
  733. //convex.render();
  734. //GFXDrawUtil *drawer = GFX->getDrawUtil();
  735. //GFX->setStateBlock( smStateBlock );
  736. return;
  737. /*
  738. U32 convexCount = mDebugConvex.size();
  739. PrimBuild::begin( GFXTriangleList, convexCount * 12 );
  740. PrimBuild::color4i( 0, 0, 255, 155 );
  741. for ( U32 i = 0; i < convexCount; i++ )
  742. {
  743. MeshRoadConvex *convex = mDebugConvex[i];
  744. Point3F a = convex->verts[0];
  745. Point3F b = convex->verts[1];
  746. Point3F c = convex->verts[2];
  747. Point3F p = convex->verts[3];
  748. //mObjToWorld.mulP(a);
  749. //mObjToWorld.mulP(b);
  750. //mObjToWorld.mulP(c);
  751. //mObjToWorld.mulP(p);
  752. PrimBuild::vertex3fv( c );
  753. PrimBuild::vertex3fv( b );
  754. PrimBuild::vertex3fv( a );
  755. PrimBuild::vertex3fv( b );
  756. PrimBuild::vertex3fv( a );
  757. PrimBuild::vertex3fv( p );
  758. PrimBuild::vertex3fv( c );
  759. PrimBuild::vertex3fv( b );
  760. PrimBuild::vertex3fv( p );
  761. PrimBuild::vertex3fv( a );
  762. PrimBuild::vertex3fv( c );
  763. PrimBuild::vertex3fv( p );
  764. }
  765. PrimBuild::end();
  766. for ( U32 i = 0; i < mSegments.size(); i++ )
  767. {
  768. ///GFX->getDrawUtil()->drawWireBox( mSegments[i].worldbounds, ColorI(255,0,0,255) );
  769. }
  770. GFX->enterDebugEvent( ColorI( 255, 0, 0 ), "DecalRoad_debugRender" );
  771. GFXTransformSaver saver;
  772. GFX->setStateBlock( smStateBlock );
  773. Point3F size(1,1,1);
  774. ColorI color( 255, 0, 0, 255 );
  775. if ( smShowBatches )
  776. {
  777. for ( U32 i = 0; i < mBatches.size(); i++ )
  778. {
  779. const Box3F &box = mBatches[i].bounds;
  780. Point3F center;
  781. box.getCenter( &center );
  782. GFX->getDrawUtil()->drawWireCube( ( box.maxExtents - box.minExtents ) * 0.5f, center, ColorI(255,100,100,255) );
  783. }
  784. }
  785. GFX->leaveDebugEvent();
  786. */
  787. }
  788. U32 MeshRoad::packUpdate(NetConnection * con, U32 mask, BitStream * stream)
  789. {
  790. U32 retMask = Parent::packUpdate(con, mask, stream);
  791. if ( stream->writeFlag( mask & MeshRoadMask ) )
  792. {
  793. // Write Object Transform.
  794. stream->writeAffineTransform( mObjToWorld );
  795. // Write Materials
  796. stream->write( mMaterialName[0] );
  797. stream->write( mMaterialName[1] );
  798. stream->write( mMaterialName[2] );
  799. stream->write( mTextureLength );
  800. stream->write( mBreakAngle );
  801. stream->write( mWidthSubdivisions );
  802. }
  803. if ( stream->writeFlag( mask & NodeMask ) )
  804. {
  805. const U32 nodeByteSize = 32; // Based on sending all of a node's parameters
  806. // Test if we can fit all of our nodes within the current stream.
  807. // We make sure we leave 100 bytes still free in the stream for whatever
  808. // may follow us.
  809. S32 allowedBytes = stream->getWriteByteSize() - 100;
  810. if ( stream->writeFlag( (nodeByteSize * mNodes.size()) < allowedBytes ) )
  811. {
  812. // All nodes should fit, so send them out now.
  813. stream->writeInt( mNodes.size(), 16 );
  814. for ( U32 i = 0; i < mNodes.size(); i++ )
  815. {
  816. mathWrite( *stream, mNodes[i].point );
  817. stream->write( mNodes[i].width );
  818. stream->write( mNodes[i].depth );
  819. mathWrite( *stream, mNodes[i].normal );
  820. }
  821. }
  822. else
  823. {
  824. // There isn't enough space left in the stream for all of the
  825. // nodes. Batch them up into NetEvents.
  826. U32 id = gServerNodeListManager->nextListId();
  827. U32 count = 0;
  828. U32 index = 0;
  829. while (count < mNodes.size())
  830. {
  831. count += NodeListManager::smMaximumNodesPerEvent;
  832. if (count > mNodes.size())
  833. {
  834. count = mNodes.size();
  835. }
  836. MeshRoadNodeEvent* event = new MeshRoadNodeEvent();
  837. event->mId = id;
  838. event->mTotalNodes = mNodes.size();
  839. event->mLocalListStart = index;
  840. for (; index<count; ++index)
  841. {
  842. event->mPositions.push_back( mNodes[index].point );
  843. event->mWidths.push_back( mNodes[index].width );
  844. event->mDepths.push_back( mNodes[index].depth );
  845. event->mNormals.push_back( mNodes[index].normal );
  846. }
  847. con->postNetEvent( event );
  848. }
  849. stream->write( id );
  850. }
  851. }
  852. stream->writeFlag( mask & RegenMask );
  853. // Were done ...
  854. return retMask;
  855. }
  856. void MeshRoad::unpackUpdate(NetConnection * con, BitStream * stream)
  857. {
  858. // Unpack Parent.
  859. Parent::unpackUpdate(con, stream);
  860. // MeshRoadMask
  861. if(stream->readFlag())
  862. {
  863. MatrixF ObjectMatrix;
  864. stream->readAffineTransform(&ObjectMatrix);
  865. Parent::setTransform(ObjectMatrix);
  866. // Read Materials...
  867. Material *pMat = NULL;
  868. for ( U32 i = 0; i < SurfaceCount; i++ )
  869. {
  870. stream->read( &mMaterialName[i] );
  871. if ( !Sim::findObject( mMaterialName[i], pMat ) )
  872. Con::printf( "DecalRoad::unpackUpdate, failed to find Material of name %s", mMaterialName[i].c_str() );
  873. else
  874. mMaterial[i] = pMat;
  875. }
  876. if ( isProperlyAdded() )
  877. _initMaterial();
  878. stream->read( &mTextureLength );
  879. stream->read( &mBreakAngle );
  880. stream->read( &mWidthSubdivisions );
  881. }
  882. // NodeMask
  883. if ( stream->readFlag() )
  884. {
  885. if (stream->readFlag())
  886. {
  887. // Nodes have been passed in this update
  888. U32 count = stream->readInt( 16 );
  889. mNodes.clear();
  890. Point3F pos, normal;
  891. F32 width, depth;
  892. for ( U32 i = 0; i < count; i++ )
  893. {
  894. mathRead( *stream, &pos );
  895. stream->read( &width );
  896. stream->read( &depth );
  897. mathRead( *stream, &normal );
  898. _addNode( pos, width, depth, normal );
  899. }
  900. }
  901. else
  902. {
  903. // Nodes will arrive as events
  904. U32 id;
  905. stream->read( &id );
  906. // Check if the road's nodes made it here before we did.
  907. NodeListManager::NodeList* list = NULL;
  908. if ( gClientNodeListManager->findListById( id, &list, true) )
  909. {
  910. // Work with the completed list
  911. MeshRoadNodeList* roadList = dynamic_cast<MeshRoadNodeList*>( list );
  912. if (roadList)
  913. buildNodesFromList( roadList );
  914. delete list;
  915. }
  916. else
  917. {
  918. // Nodes have not yet arrived, so register our interest in the list
  919. MeshRoadNodeListNotify* notify = new MeshRoadNodeListNotify( this, id );
  920. gClientNodeListManager->registerNotification( notify );
  921. }
  922. }
  923. }
  924. if ( stream->readFlag() && isProperlyAdded() )
  925. _regenerate();
  926. }
  927. void MeshRoad::setTransform( const MatrixF &mat )
  928. {
  929. for ( U32 i = 0; i < mNodes.size(); i++ )
  930. {
  931. mWorldToObj.mulP( mNodes[i].point );
  932. mat.mulP( mNodes[i].point );
  933. }
  934. Parent::setTransform( mat );
  935. if ( mPhysicsRep )
  936. mPhysicsRep->setTransform( mat );
  937. // Regenerate and update the client
  938. _regenerate();
  939. setMaskBits( NodeMask | RegenMask );
  940. }
  941. void MeshRoad::setScale( const VectorF &scale )
  942. {
  943. // We ignore scale requests from the editor
  944. // right now.
  945. //Parent::setScale( scale );
  946. }
  947. void MeshRoad::buildConvex(const Box3F& box, Convex* convex)
  948. {
  949. if ( mSlices.size() < 2 )
  950. return;
  951. mConvexList->collectGarbage();
  952. mDebugConvex.clear();
  953. Box3F realBox = box;
  954. mWorldToObj.mul(realBox);
  955. realBox.minExtents.convolveInverse(mObjScale);
  956. realBox.maxExtents.convolveInverse(mObjScale);
  957. if (realBox.isOverlapped(getObjBox()) == false)
  958. return;
  959. U32 segmentCount = mSegments.size();
  960. // Create convex(s) for each segment
  961. for ( U32 i = 0; i < segmentCount; i++ )
  962. {
  963. const MeshRoadSegment &segment = mSegments[i];
  964. // Is this segment overlapped?
  965. if ( !segment.getWorldBounds().isOverlapped( box ) )
  966. continue;
  967. // Each segment has 6 faces
  968. for ( U32 j = 0; j < 6; j++ )
  969. {
  970. // Only first segment has front face
  971. if ( j == 4 && i != 0 )
  972. continue;
  973. // Only last segment has back face
  974. if ( j == 5 && i != segmentCount-1 )
  975. continue;
  976. // Each face has 2 convex(s)
  977. for ( U32 k = 0; k < 2; k++ )
  978. {
  979. // See if this convex exists in the working set already...
  980. Convex* cc = 0;
  981. CollisionWorkingList& wl = convex->getWorkingList();
  982. for ( CollisionWorkingList* itr = wl.wLink.mNext; itr != &wl; itr = itr->wLink.mNext )
  983. {
  984. if ( itr->mConvex->getType() == MeshRoadConvexType )
  985. {
  986. MeshRoadConvex *pConvex = static_cast<MeshRoadConvex*>(itr->mConvex);
  987. if ( pConvex->pRoad == this &&
  988. pConvex->segmentId == i &&
  989. pConvex->faceId == j &&
  990. pConvex->triangleId == k )
  991. {
  992. cc = itr->mConvex;
  993. break;
  994. }
  995. }
  996. }
  997. if (cc)
  998. continue;
  999. // Get the triangle...
  1000. U32 idx0 = gIdxArray[j][k][0];
  1001. U32 idx1 = gIdxArray[j][k][1];
  1002. U32 idx2 = gIdxArray[j][k][2];
  1003. Point3F a = segment[idx0];
  1004. Point3F b = segment[idx1];
  1005. Point3F c = segment[idx2];
  1006. // Transform the result into object space!
  1007. //mWorldToObj.mulP( a );
  1008. //mWorldToObj.mulP( b );
  1009. //mWorldToObj.mulP( c );
  1010. PlaneF p( c, b, a );
  1011. Point3F peak = ((a + b + c) / 3.0f) + (p * 0.15f);
  1012. // Set up the convex...
  1013. MeshRoadConvex *cp = new MeshRoadConvex();
  1014. mConvexList->registerObject( cp );
  1015. convex->addToWorkingList( cp );
  1016. cp->mObject = this;
  1017. cp->pRoad = this;
  1018. cp->segmentId = i;
  1019. cp->faceId = j;
  1020. cp->triangleId = k;
  1021. cp->normal = p;
  1022. cp->verts[0] = c;
  1023. cp->verts[1] = b;
  1024. cp->verts[2] = a;
  1025. cp->verts[3] = peak;
  1026. // Update the bounding box.
  1027. Box3F &bounds = cp->box;
  1028. bounds.minExtents.set( F32_MAX, F32_MAX, F32_MAX );
  1029. bounds.maxExtents.set( -F32_MAX, -F32_MAX, -F32_MAX );
  1030. bounds.minExtents.setMin( a );
  1031. bounds.minExtents.setMin( b );
  1032. bounds.minExtents.setMin( c );
  1033. bounds.minExtents.setMin( peak );
  1034. bounds.maxExtents.setMax( a );
  1035. bounds.maxExtents.setMax( b );
  1036. bounds.maxExtents.setMax( c );
  1037. bounds.maxExtents.setMax( peak );
  1038. mDebugConvex.push_back(cp);
  1039. }
  1040. }
  1041. }
  1042. }
  1043. bool MeshRoad::buildPolyList( PolyListContext, AbstractPolyList* polyList, const Box3F &box, const SphereF & )
  1044. {
  1045. if ( mSlices.size() < 2 )
  1046. return false;
  1047. polyList->setTransform( &MatrixF::Identity, Point3F::One );
  1048. polyList->setObject(this);
  1049. // JCF: optimize this to not always add everything.
  1050. return buildSegmentPolyList( polyList, 0, mSegments.size() - 1, true, true );
  1051. }
  1052. bool MeshRoad::buildSegmentPolyList( AbstractPolyList* polyList, U32 startSegIdx, U32 endSegIdx, bool capFront, bool capEnd )
  1053. {
  1054. if ( mSlices.size() < 2 )
  1055. return false;
  1056. // Add verts
  1057. for ( U32 i = startSegIdx; i <= endSegIdx; i++ )
  1058. {
  1059. const MeshRoadSegment &seg = mSegments[i];
  1060. if ( i == startSegIdx )
  1061. {
  1062. polyList->addPoint( seg.slice0->p0 );
  1063. polyList->addPoint( seg.slice0->p2 );
  1064. polyList->addPoint( seg.slice0->pb0 );
  1065. polyList->addPoint( seg.slice0->pb2 );
  1066. }
  1067. polyList->addPoint( seg.slice1->p0 );
  1068. polyList->addPoint( seg.slice1->p2 );
  1069. polyList->addPoint( seg.slice1->pb0 );
  1070. polyList->addPoint( seg.slice1->pb2 );
  1071. }
  1072. // Temporaries to hold indices for the corner points of a quad.
  1073. S32 p00, p01, p11, p10;
  1074. S32 pb00, pb01, pb11, pb10;
  1075. U32 offset = 0;
  1076. DebugDrawer *ddraw = NULL;//DebugDrawer::get();
  1077. ClippedPolyList *cpolyList = dynamic_cast<ClippedPolyList*>(polyList);
  1078. MatrixF mat;
  1079. Point3F scale;
  1080. if ( cpolyList )
  1081. cpolyList->getTransform( &mat, &scale );
  1082. for ( U32 i = startSegIdx; i <= endSegIdx; i++ )
  1083. {
  1084. p00 = offset;
  1085. p10 = offset + 1;
  1086. pb00 = offset + 2;
  1087. pb10 = offset + 3;
  1088. p01 = offset + 4;
  1089. p11 = offset + 5;
  1090. pb01 = offset + 6;
  1091. pb11 = offset + 7;
  1092. // Top Face
  1093. polyList->begin( 0,0 );
  1094. polyList->vertex( p00 );
  1095. polyList->vertex( p01 );
  1096. polyList->vertex( p11 );
  1097. polyList->plane( p00, p01, p11 );
  1098. polyList->end();
  1099. if ( ddraw && cpolyList )
  1100. {
  1101. Point3F v0 = cpolyList->mVertexList[p00].point;
  1102. mat.mulP( v0 );
  1103. Point3F v1 = cpolyList->mVertexList[p01].point;
  1104. mat.mulP( v1 );
  1105. Point3F v2 = cpolyList->mVertexList[p11].point;
  1106. mat.mulP( v2 );
  1107. ddraw->drawTri( v0, v1, v2 );
  1108. ddraw->setLastZTest( false );
  1109. ddraw->setLastTTL( 0 );
  1110. }
  1111. polyList->begin( 0,0 );
  1112. polyList->vertex( p00 );
  1113. polyList->vertex( p11 );
  1114. polyList->vertex( p10 );
  1115. polyList->plane( p00, p11, p10 );
  1116. polyList->end();
  1117. if ( ddraw && cpolyList )
  1118. {
  1119. ddraw->drawTri( cpolyList->mVertexList[p00].point, cpolyList->mVertexList[p11].point, cpolyList->mVertexList[p10].point );
  1120. ddraw->setLastTTL( 0 );
  1121. }
  1122. if (buildPolyList_TopSurfaceOnly)
  1123. {
  1124. offset += 4;
  1125. continue;
  1126. }
  1127. // Left Face
  1128. polyList->begin( 0,0 );
  1129. polyList->vertex( pb00 );
  1130. polyList->vertex( pb01 );
  1131. polyList->vertex( p01 );
  1132. polyList->plane( pb00, pb01, p01 );
  1133. polyList->end();
  1134. polyList->begin( 0,0 );
  1135. polyList->vertex( pb00 );
  1136. polyList->vertex( p01 );
  1137. polyList->vertex( p00 );
  1138. polyList->plane( pb00, p01, p00 );
  1139. polyList->end();
  1140. // Right Face
  1141. polyList->begin( 0,0 );
  1142. polyList->vertex( p10 );
  1143. polyList->vertex( p11 );
  1144. polyList->vertex( pb11 );
  1145. polyList->plane( p10, p11, pb11 );
  1146. polyList->end();
  1147. polyList->begin( 0,0 );
  1148. polyList->vertex( p10 );
  1149. polyList->vertex( pb11 );
  1150. polyList->vertex( pb10 );
  1151. polyList->plane( p10, pb11, pb10 );
  1152. polyList->end();
  1153. // Bottom Face
  1154. polyList->begin( 0,0 );
  1155. polyList->vertex( pb00 );
  1156. polyList->vertex( pb10 );
  1157. polyList->vertex( pb11 );
  1158. polyList->plane( pb00, pb10, pb11 );
  1159. polyList->end();
  1160. polyList->begin( 0,0 );
  1161. polyList->vertex( pb00 );
  1162. polyList->vertex( pb11 );
  1163. polyList->vertex( pb01 );
  1164. polyList->plane( pb00, pb11, pb01 );
  1165. polyList->end();
  1166. // Front Face
  1167. if ( i == startSegIdx && capFront )
  1168. {
  1169. polyList->begin( 0,0 );
  1170. polyList->vertex( p00 );
  1171. polyList->vertex( p10 );
  1172. polyList->vertex( pb10 );
  1173. polyList->plane( p00, p10, pb10 );
  1174. polyList->end();
  1175. polyList->begin( 0,0 );
  1176. polyList->vertex( p00 );
  1177. polyList->vertex( pb10 );
  1178. polyList->vertex( pb00 );
  1179. polyList->plane( p00, pb10, pb00 );
  1180. polyList->end();
  1181. }
  1182. // Back Face
  1183. if ( i == endSegIdx && capEnd )
  1184. {
  1185. polyList->begin( 0,0 );
  1186. polyList->vertex( p01 );
  1187. polyList->vertex( pb01 );
  1188. polyList->vertex( pb11 );
  1189. polyList->plane( p01, pb01, pb11 );
  1190. polyList->end();
  1191. polyList->begin( 0,0 );
  1192. polyList->vertex( p01 );
  1193. polyList->vertex( pb11 );
  1194. polyList->vertex( p11 );
  1195. polyList->plane( p01, pb11, p11 );
  1196. polyList->end();
  1197. }
  1198. offset += 4;
  1199. }
  1200. return true;
  1201. }
  1202. bool MeshRoad::castRay( const Point3F &s, const Point3F &e, RayInfo *info )
  1203. {
  1204. Point3F start = s;
  1205. Point3F end = e;
  1206. mObjToWorld.mulP(start);
  1207. mObjToWorld.mulP(end);
  1208. F32 out = 1.0f; // The output fraction/percentage along the line defined by s and e
  1209. VectorF norm(0.0f, 0.0f, 0.0f); // The normal of the face intersected
  1210. Vector<MeshRoadHitSegment> hitSegments;
  1211. for ( U32 i = 0; i < mSegments.size(); i++ )
  1212. {
  1213. const MeshRoadSegment &segment = mSegments[i];
  1214. F32 t;
  1215. VectorF n;
  1216. if ( segment.getWorldBounds().collideLine( start, end, &t, &n ) )
  1217. {
  1218. hitSegments.increment();
  1219. hitSegments.last().t = t;
  1220. hitSegments.last().idx = i;
  1221. }
  1222. }
  1223. dQsort( hitSegments.address(), hitSegments.size(), sizeof(MeshRoadHitSegment), compareHitSegments );
  1224. U32 idx0, idx1, idx2;
  1225. F32 t;
  1226. for ( U32 i = 0; i < hitSegments.size(); i++ )
  1227. {
  1228. U32 segIdx = hitSegments[i].idx;
  1229. const MeshRoadSegment &segment = mSegments[segIdx];
  1230. // Each segment has 6 faces
  1231. for ( U32 j = 0; j < 6; j++ )
  1232. {
  1233. if ( j == 4 && segIdx != 0 )
  1234. continue;
  1235. if ( j == 5 && segIdx != mSegments.size() - 1 )
  1236. continue;
  1237. // Each face has 2 triangles
  1238. for ( U32 k = 0; k < 2; k++ )
  1239. {
  1240. idx0 = gIdxArray[j][k][0];
  1241. idx1 = gIdxArray[j][k][1];
  1242. idx2 = gIdxArray[j][k][2];
  1243. const Point3F &v0 = segment[idx0];
  1244. const Point3F &v1 = segment[idx1];
  1245. const Point3F &v2 = segment[idx2];
  1246. if ( !MathUtils::mLineTriangleCollide( start, end,
  1247. v2, v1, v0,
  1248. NULL,
  1249. &t ) )
  1250. continue;
  1251. if ( t >= 0.0f && t < 1.0f && t < out )
  1252. {
  1253. out = t;
  1254. norm = PlaneF( v0, v1, v2 );
  1255. }
  1256. }
  1257. }
  1258. if (out >= 0.0f && out < 1.0f)
  1259. break;
  1260. }
  1261. if (out >= 0.0f && out < 1.0f)
  1262. {
  1263. info->t = out;
  1264. info->normal = norm;
  1265. info->point.interpolate(start, end, out);
  1266. info->face = -1;
  1267. info->object = this;
  1268. info->material = this->mMatInst[0];
  1269. return true;
  1270. }
  1271. return false;
  1272. }
  1273. bool MeshRoad::collideBox(const Point3F &start, const Point3F &end, RayInfo* info)
  1274. {
  1275. Con::warnf( "MeshRoad::collideBox() - not yet implemented!" );
  1276. return Parent::collideBox( start, end, info );
  1277. }
  1278. void MeshRoad::_regenerate()
  1279. {
  1280. if ( mNodes.size() == 0 )
  1281. return;
  1282. const Point3F &nodePt = mNodes.first().point;
  1283. MatrixF mat( true );
  1284. mat.setPosition( nodePt );
  1285. Parent::setTransform( mat );
  1286. _generateSlices();
  1287. // Make sure we are in the correct bins given our world box.
  1288. if( getSceneManager() != NULL )
  1289. getSceneManager()->notifyObjectDirty( this );
  1290. }
  1291. void MeshRoad::_generateSlices()
  1292. {
  1293. if ( mNodes.size() < 2 )
  1294. return;
  1295. // Create the spline, initialized with the MeshRoadNode(s)
  1296. U32 nodeCount = mNodes.size();
  1297. MeshRoadSplineNode *splineNodes = new MeshRoadSplineNode[nodeCount];
  1298. for ( U32 i = 0; i < nodeCount; i++ )
  1299. {
  1300. MeshRoadSplineNode &splineNode = splineNodes[i];
  1301. const MeshRoadNode &node = mNodes[i];
  1302. splineNode.x = node.point.x;
  1303. splineNode.y = node.point.y;
  1304. splineNode.z = node.point.z;
  1305. splineNode.width = node.width;
  1306. splineNode.depth = node.depth;
  1307. splineNode.normal = node.normal;
  1308. }
  1309. CatmullRom<MeshRoadSplineNode> spline;
  1310. spline.initialize( nodeCount, splineNodes );
  1311. delete [] splineNodes;
  1312. mSlices.clear();
  1313. VectorF lastBreakVector(0,0,0);
  1314. MeshRoadSlice slice;
  1315. MeshRoadSplineNode lastBreakNode;
  1316. lastBreakNode = spline.evaluate(0.0f);
  1317. for ( U32 i = 1; i < mNodes.size(); i++ )
  1318. {
  1319. F32 t1 = spline.getTime(i);
  1320. F32 t0 = spline.getTime(i-1);
  1321. F32 segLength = spline.arcLength( t0, t1 );
  1322. U32 numSegments = mCeil( segLength / MIN_METERS_PER_SEGMENT );
  1323. numSegments = getMax( numSegments, (U32)1 );
  1324. F32 tstep = ( t1 - t0 ) / numSegments;
  1325. U32 startIdx = 0;
  1326. U32 endIdx = ( i == nodeCount - 1 ) ? numSegments + 1 : numSegments;
  1327. for ( U32 j = startIdx; j < endIdx; j++ )
  1328. {
  1329. F32 t = t0 + tstep * j;
  1330. MeshRoadSplineNode splineNode = spline.evaluate(t);
  1331. VectorF toNodeVec = splineNode.getPosition() - lastBreakNode.getPosition();
  1332. toNodeVec.normalizeSafe();
  1333. if ( lastBreakVector.isZero() )
  1334. lastBreakVector = toNodeVec;
  1335. F32 angle = mRadToDeg( mAcos( mDot( toNodeVec, lastBreakVector ) ) );
  1336. if ( j == startIdx ||
  1337. ( j == endIdx - 1 && i == mNodes.size() - 1 ) ||
  1338. angle > mBreakAngle )
  1339. {
  1340. // Push back a spline node
  1341. slice.p1.set( splineNode.x, splineNode.y, splineNode.z );
  1342. slice.width = splineNode.width;
  1343. slice.depth = splineNode.depth;
  1344. slice.normal = splineNode.normal;
  1345. slice.normal.normalize();
  1346. slice.parentNodeIdx = i-1;
  1347. slice.t = t;
  1348. mSlices.push_back( slice );
  1349. lastBreakVector = splineNode.getPosition() - lastBreakNode.getPosition();
  1350. lastBreakVector.normalizeSafe();
  1351. lastBreakNode = splineNode;
  1352. }
  1353. }
  1354. }
  1355. //
  1356. // Calculate uvec, fvec, and rvec for all slices
  1357. //
  1358. MatrixF mat(true);
  1359. for ( U32 i = 0; i < mSlices.size(); i++ )
  1360. {
  1361. calcSliceTransform( i, mat );
  1362. mat.getColumn( 0, &mSlices[i].rvec );
  1363. mat.getColumn( 1, &mSlices[i].fvec );
  1364. mat.getColumn( 2, &mSlices[i].uvec );
  1365. }
  1366. //
  1367. // Calculate p0/p2/pb0/pb2 for all slices
  1368. //
  1369. for ( U32 i = 0; i < mSlices.size(); i++ )
  1370. {
  1371. MeshRoadSlice *slice = &mSlices[i];
  1372. slice->p0 = slice->p1 - slice->rvec * slice->width * 0.5f;
  1373. slice->p2 = slice->p1 + slice->rvec * slice->width * 0.5f;
  1374. slice->pb0 = slice->p0 - slice->uvec * slice->depth;
  1375. slice->pb2 = slice->p2 - slice->uvec * slice->depth;
  1376. }
  1377. // Generate the object/world bounds
  1378. Box3F box;
  1379. for ( U32 i = 0; i < mSlices.size(); i++ )
  1380. {
  1381. const MeshRoadSlice &slice = mSlices[i];
  1382. if ( i == 0 )
  1383. {
  1384. box.minExtents = slice.p0;
  1385. box.maxExtents = slice.p2;
  1386. box.extend( slice.pb0 );
  1387. box.extend( slice.pb2 );
  1388. }
  1389. else
  1390. {
  1391. box.extend( slice.p0 );
  1392. box.extend( slice.p2 );
  1393. box.extend( slice.pb0 );
  1394. box.extend( slice.pb2 );
  1395. }
  1396. }
  1397. mWorldBox = box;
  1398. resetObjectBox();
  1399. _generateSegments();
  1400. }
  1401. void MeshRoad::_generateSegments()
  1402. {
  1403. SAFE_DELETE( mPhysicsRep );
  1404. mSegments.clear();
  1405. for ( U32 i = 0; i < mSlices.size() - 1; i++ )
  1406. {
  1407. MeshRoadSegment seg( &mSlices[i], &mSlices[i+1], getWorldTransform() );
  1408. mSegments.push_back( seg );
  1409. }
  1410. if ( isClientObject() )
  1411. _generateVerts();
  1412. if ( PHYSICSMGR )
  1413. {
  1414. ConcretePolyList polylist;
  1415. if ( buildPolyList( PLC_Collision, &polylist, getWorldBox(), getWorldSphere() ) )
  1416. {
  1417. polylist.triangulate();
  1418. PhysicsCollision *colShape = PHYSICSMGR->createCollision();
  1419. colShape->addTriangleMesh( polylist.mVertexList.address(),
  1420. polylist.mVertexList.size(),
  1421. polylist.mIndexList.address(),
  1422. polylist.mIndexList.size() / 3,
  1423. MatrixF::Identity );
  1424. PhysicsWorld *world = PHYSICSMGR->getWorld( isServerObject() ? "server" : "client" );
  1425. mPhysicsRep = PHYSICSMGR->createBody();
  1426. mPhysicsRep->init( colShape, 0, 0, this, world );
  1427. }
  1428. }
  1429. }
  1430. void MeshRoad::_generateVerts()
  1431. {
  1432. const U32 widthDivisions = getMax( 0, mWidthSubdivisions );
  1433. const F32 divisionStep = 1.0f / (F32)( widthDivisions + 1 );
  1434. const U32 sliceCount = mSlices.size();
  1435. const U32 segmentCount = mSegments.size();
  1436. mVertCount[Top] = ( 2 + widthDivisions ) * sliceCount;
  1437. mTriangleCount[Top] = segmentCount * 2 * ( widthDivisions + 1 );
  1438. mVertCount[Bottom] = sliceCount * 2;
  1439. mTriangleCount[Bottom] = segmentCount * 2;
  1440. mVertCount[Side] = sliceCount * 4;
  1441. mTriangleCount[Side] = segmentCount * 4 + 4;
  1442. // Calculate TexCoords for Slices
  1443. F32 texCoordV = 0.0f;
  1444. mSlices[0].texCoordV = 0.0f;
  1445. for ( U32 i = 1; i < sliceCount; i++ )
  1446. {
  1447. MeshRoadSlice &slice = mSlices[i];
  1448. MeshRoadSlice &prevSlice = mSlices[i-1];
  1449. // Increment the textCoordV for the next slice.
  1450. F32 len = ( slice.p1 - prevSlice.p1 ).len();
  1451. texCoordV += len / mTextureLength;
  1452. slice.texCoordV = texCoordV;
  1453. }
  1454. // Make Vertex Buffers
  1455. GFXVertexPNTT *pVert = NULL;
  1456. U32 vertCounter = 0;
  1457. // Top Buffers...
  1458. mVB[Top].set( GFX, mVertCount[Top], GFXBufferTypeStatic );
  1459. pVert = mVB[Top].lock();
  1460. vertCounter = 0;
  1461. for ( U32 i = 0; i < sliceCount; i++ )
  1462. {
  1463. MeshRoadSlice &slice = mSlices[i];
  1464. pVert->point = slice.p0;
  1465. pVert->normal = slice.uvec;
  1466. pVert->tangent = slice.fvec;
  1467. pVert->texCoord.set(1,slice.texCoordV);
  1468. pVert++;
  1469. vertCounter++;
  1470. for ( U32 j = 0; j < widthDivisions; j++ )
  1471. {
  1472. const F32 t = divisionStep * (F32)( j + 1 );
  1473. pVert->point.interpolate( slice.p0, slice.p2, t );
  1474. pVert->normal = slice.uvec;
  1475. pVert->tangent = slice.fvec;
  1476. pVert->texCoord.set( 1.0f - t, slice.texCoordV );
  1477. pVert++;
  1478. vertCounter++;
  1479. }
  1480. pVert->point = slice.p2;
  1481. pVert->normal = slice.uvec;
  1482. pVert->tangent = slice.fvec;
  1483. pVert->texCoord.set( 0, slice.texCoordV );
  1484. pVert++;
  1485. vertCounter++;
  1486. }
  1487. AssertFatal( vertCounter == mVertCount[Top], "MeshRoad, wrote incorrect number of verts in mVB[Top]!" );
  1488. mVB[Top].unlock();
  1489. // Bottom Buffer...
  1490. mVB[Bottom].set( GFX, mVertCount[Bottom], GFXBufferTypeStatic );
  1491. pVert = mVB[Bottom].lock();
  1492. vertCounter = 0;
  1493. for ( U32 i = 0; i < sliceCount; i++ )
  1494. {
  1495. MeshRoadSlice &slice = mSlices[i];
  1496. pVert->point = slice.pb2;
  1497. pVert->normal = -slice.uvec;
  1498. pVert->tangent = slice.fvec;
  1499. pVert->texCoord.set(0,slice.texCoordV);
  1500. pVert++;
  1501. vertCounter++;
  1502. pVert->point = slice.pb0;
  1503. pVert->normal = -slice.uvec;
  1504. pVert->tangent = slice.fvec;
  1505. pVert->texCoord.set(1,slice.texCoordV);
  1506. pVert++;
  1507. vertCounter++;
  1508. }
  1509. AssertFatal( vertCounter == mVertCount[Bottom], "MeshRoad, wrote incorrect number of verts in mVB[Bottom]!" );
  1510. mVB[Bottom].unlock();
  1511. // Side Buffers...
  1512. mVB[Side].set( GFX, mVertCount[Side], GFXBufferTypeStatic );
  1513. pVert = mVB[Side].lock();
  1514. vertCounter = 0;
  1515. for ( U32 i = 0; i < sliceCount; i++ )
  1516. {
  1517. MeshRoadSlice &slice = mSlices[i];
  1518. pVert->point = slice.p0;
  1519. pVert->normal = -slice.rvec;
  1520. pVert->tangent = slice.fvec;
  1521. pVert->texCoord.set(1,slice.texCoordV);
  1522. pVert++;
  1523. vertCounter++;
  1524. pVert->point = slice.p2;
  1525. pVert->normal = slice.rvec;
  1526. pVert->tangent = slice.fvec;
  1527. pVert->texCoord.set(1,slice.texCoordV);
  1528. pVert++;
  1529. vertCounter++;
  1530. pVert->point = slice.pb0;
  1531. pVert->normal = -slice.rvec;
  1532. pVert->tangent = slice.fvec;
  1533. pVert->texCoord.set(0,slice.texCoordV);
  1534. pVert++;
  1535. vertCounter++;
  1536. pVert->point = slice.pb2;
  1537. pVert->normal = slice.rvec;
  1538. pVert->tangent = slice.fvec;
  1539. pVert->texCoord.set(0,slice.texCoordV);
  1540. pVert++;
  1541. vertCounter++;
  1542. }
  1543. AssertFatal( vertCounter == mVertCount[Side], "MeshRoad, wrote incorrect number of verts in mVB[Side]!" );
  1544. mVB[Side].unlock();
  1545. // Make Primitive Buffers
  1546. U32 p00, p01, p11, p10;
  1547. U32 pb00, pb01, pb11, pb10;
  1548. U32 offset = 0;
  1549. U16 *pIdx = NULL;
  1550. U32 curIdx = 0;
  1551. // Top Primitive Buffer
  1552. mPB[Top].set( GFX, mTriangleCount[Top] * 3, mTriangleCount[Top], GFXBufferTypeStatic );
  1553. mPB[Top].lock(&pIdx);
  1554. curIdx = 0;
  1555. offset = 0;
  1556. const U32 rowStride = 2 + widthDivisions;
  1557. for ( U32 i = 0; i < mSegments.size(); i++ )
  1558. {
  1559. for ( U32 j = 0; j < widthDivisions + 1; j++ )
  1560. {
  1561. p00 = offset;
  1562. p10 = offset + 1;
  1563. p01 = offset + rowStride;
  1564. p11 = offset + rowStride + 1;
  1565. pIdx[curIdx] = p00;
  1566. curIdx++;
  1567. pIdx[curIdx] = p01;
  1568. curIdx++;
  1569. pIdx[curIdx] = p11;
  1570. curIdx++;
  1571. pIdx[curIdx] = p00;
  1572. curIdx++;
  1573. pIdx[curIdx] = p11;
  1574. curIdx++;
  1575. pIdx[curIdx] = p10;
  1576. curIdx++;
  1577. offset += 1;
  1578. }
  1579. offset += 1;
  1580. }
  1581. AssertFatal( curIdx == mTriangleCount[Top] * 3, "MeshRoad, wrote incorrect number of indices in mPB[Top]!" );
  1582. mPB[Top].unlock();
  1583. // Bottom Primitive Buffer
  1584. mPB[Bottom].set( GFX, mTriangleCount[Bottom] * 3, mTriangleCount[Bottom], GFXBufferTypeStatic );
  1585. mPB[Bottom].lock(&pIdx);
  1586. curIdx = 0;
  1587. offset = 0;
  1588. for ( U32 i = 0; i < mSegments.size(); i++ )
  1589. {
  1590. p00 = offset;
  1591. p10 = offset + 1;
  1592. p01 = offset + 2;
  1593. p11 = offset + 3;
  1594. pIdx[curIdx] = p00;
  1595. curIdx++;
  1596. pIdx[curIdx] = p01;
  1597. curIdx++;
  1598. pIdx[curIdx] = p11;
  1599. curIdx++;
  1600. pIdx[curIdx] = p00;
  1601. curIdx++;
  1602. pIdx[curIdx] = p11;
  1603. curIdx++;
  1604. pIdx[curIdx] = p10;
  1605. curIdx++;
  1606. offset += 2;
  1607. }
  1608. AssertFatal( curIdx == mTriangleCount[Bottom] * 3, "MeshRoad, wrote incorrect number of indices in mPB[Bottom]!" );
  1609. mPB[Bottom].unlock();
  1610. // Side Primitive Buffer
  1611. mPB[Side].set( GFX, mTriangleCount[Side] * 3, mTriangleCount[Side], GFXBufferTypeStatic );
  1612. mPB[Side].lock(&pIdx);
  1613. curIdx = 0;
  1614. offset = 0;
  1615. for ( U32 i = 0; i < mSegments.size(); i++ )
  1616. {
  1617. p00 = offset;
  1618. p10 = offset + 1;
  1619. pb00 = offset + 2;
  1620. pb10 = offset + 3;
  1621. p01 = offset + 4;
  1622. p11 = offset + 5;
  1623. pb01 = offset + 6;
  1624. pb11 = offset + 7;
  1625. // Left Side
  1626. pIdx[curIdx] = pb00;
  1627. curIdx++;
  1628. pIdx[curIdx] = pb01;
  1629. curIdx++;
  1630. pIdx[curIdx] = p01;
  1631. curIdx++;
  1632. pIdx[curIdx] = pb00;
  1633. curIdx++;
  1634. pIdx[curIdx] = p01;
  1635. curIdx++;
  1636. pIdx[curIdx] = p00;
  1637. curIdx++;
  1638. // Right Side
  1639. pIdx[curIdx] = p10;
  1640. curIdx++;
  1641. pIdx[curIdx] = p11;
  1642. curIdx++;
  1643. pIdx[curIdx] = pb11;
  1644. curIdx++;
  1645. pIdx[curIdx] = p10;
  1646. curIdx++;
  1647. pIdx[curIdx] = pb11;
  1648. curIdx++;
  1649. pIdx[curIdx] = pb10;
  1650. curIdx++;
  1651. offset += 4;
  1652. }
  1653. // Cap the front and back ends
  1654. pIdx[curIdx++] = 0;
  1655. pIdx[curIdx++] = 1;
  1656. pIdx[curIdx++] = 3;
  1657. pIdx[curIdx++] = 0;
  1658. pIdx[curIdx++] = 3;
  1659. pIdx[curIdx++] = 2;
  1660. pIdx[curIdx++] = offset + 0;
  1661. pIdx[curIdx++] = offset + 3;
  1662. pIdx[curIdx++] = offset + 1;
  1663. pIdx[curIdx++] = offset + 0;
  1664. pIdx[curIdx++] = offset + 2;
  1665. pIdx[curIdx++] = offset + 3;
  1666. AssertFatal( curIdx == mTriangleCount[Side] * 3, "MeshRoad, wrote incorrect number of indices in mPB[Side]!" );
  1667. mPB[Side].unlock();
  1668. }
  1669. const MeshRoadNode& MeshRoad::getNode( U32 idx )
  1670. {
  1671. return mNodes[idx];
  1672. }
  1673. VectorF MeshRoad::getNodeNormal( U32 idx )
  1674. {
  1675. if ( mNodes.size() - 1 < idx )
  1676. return VectorF::Zero;
  1677. return mNodes[idx].normal;
  1678. }
  1679. void MeshRoad::setNodeNormal( U32 idx, const VectorF &normal )
  1680. {
  1681. if ( mNodes.size() - 1 < idx )
  1682. return;
  1683. mNodes[idx].normal = normal;
  1684. regenerate();
  1685. setMaskBits( NodeMask | RegenMask );
  1686. }
  1687. Point3F MeshRoad::getNodePosition( U32 idx )
  1688. {
  1689. if ( mNodes.size() - 1 < idx )
  1690. return Point3F::Zero;
  1691. return mNodes[idx].point;
  1692. }
  1693. void MeshRoad::setNodePosition( U32 idx, const Point3F &pos )
  1694. {
  1695. if ( mNodes.size() - 1 < idx )
  1696. return;
  1697. mNodes[idx].point = pos;
  1698. regenerate();
  1699. setMaskBits( NodeMask | RegenMask );
  1700. }
  1701. U32 MeshRoad::addNode( const Point3F &pos, const F32 &width, const F32 &depth, const VectorF &normal )
  1702. {
  1703. U32 idx = _addNode( pos, width, depth, normal );
  1704. regenerate();
  1705. setMaskBits( NodeMask | RegenMask );
  1706. return idx;
  1707. }
  1708. void MeshRoad::buildNodesFromList( MeshRoadNodeList* list )
  1709. {
  1710. mNodes.clear();
  1711. for (U32 i=0; i<list->mPositions.size(); ++i)
  1712. {
  1713. _addNode( list->mPositions[i], list->mWidths[i], list->mDepths[i], list->mNormals[i] );
  1714. }
  1715. _regenerate();
  1716. }
  1717. U32 MeshRoad::insertNode( const Point3F &pos, const F32 &width, const F32 &depth, const VectorF &normal, const U32 &idx )
  1718. {
  1719. U32 ret = _insertNode( pos, width, depth, normal, idx );
  1720. regenerate();
  1721. setMaskBits( NodeMask | RegenMask );
  1722. return ret;
  1723. }
  1724. void MeshRoad::setNode( const Point3F &pos, const F32 &width, const F32 &depth, const VectorF &normal, const U32 &idx )
  1725. {
  1726. if ( mNodes.size() - 1 < idx )
  1727. return;
  1728. MeshRoadNode &node = mNodes[idx];
  1729. node.point = pos;
  1730. node.width = width;
  1731. node.depth = depth;
  1732. node.normal = normal;
  1733. regenerate();
  1734. setMaskBits( NodeMask | RegenMask );
  1735. }
  1736. void MeshRoad::setNodeWidth( U32 idx, F32 meters )
  1737. {
  1738. meters = mClampF( meters, MIN_NODE_WIDTH, MAX_NODE_WIDTH );
  1739. if ( mNodes.size() - 1 < idx )
  1740. return;
  1741. mNodes[idx].width = meters;
  1742. _regenerate();
  1743. setMaskBits( RegenMask | NodeMask );
  1744. }
  1745. F32 MeshRoad::getNodeWidth( U32 idx )
  1746. {
  1747. if ( mNodes.size() - 1 < idx )
  1748. return -1.0f;
  1749. return mNodes[idx].width;
  1750. }
  1751. void MeshRoad::setNodeDepth( U32 idx, F32 meters )
  1752. {
  1753. meters = mClampF( meters, MIN_NODE_DEPTH, MAX_NODE_DEPTH );
  1754. if ( mNodes.size() - 1 < idx )
  1755. return;
  1756. mNodes[idx].depth = meters;
  1757. _regenerate();
  1758. setMaskBits( MeshRoadMask | RegenMask | NodeMask );
  1759. }
  1760. F32 MeshRoad::getNodeDepth( U32 idx )
  1761. {
  1762. if ( mNodes.size() - 1 < idx )
  1763. return -1.0f;
  1764. return mNodes[idx].depth;
  1765. }
  1766. MatrixF MeshRoad::getNodeTransform( U32 idx )
  1767. {
  1768. MatrixF mat(true);
  1769. if ( mNodes.size() - 1 < idx )
  1770. return mat;
  1771. bool hasNext = idx + 1 < mNodes.size();
  1772. bool hasPrev = (S32)idx - 1 > 0;
  1773. const MeshRoadNode &node = mNodes[idx];
  1774. VectorF fvec( 0, 1, 0 );
  1775. if ( hasNext )
  1776. {
  1777. fvec = mNodes[idx+1].point - node.point;
  1778. fvec.normalizeSafe();
  1779. }
  1780. else if ( hasPrev )
  1781. {
  1782. fvec = node.point - mNodes[idx-1].point;
  1783. fvec.normalizeSafe();
  1784. }
  1785. else
  1786. fvec = mPerp( node.normal );
  1787. if ( fvec.isZero() )
  1788. fvec = mPerp( node.normal );
  1789. F32 dot = mDot( fvec, node.normal );
  1790. if ( dot < -0.9f || dot > 0.9f )
  1791. fvec = mPerp( node.normal );
  1792. VectorF rvec = mCross( fvec, node.normal );
  1793. if ( rvec.isZero() )
  1794. rvec = mPerp( fvec );
  1795. rvec.normalize();
  1796. fvec = mCross( node.normal, rvec );
  1797. fvec.normalize();
  1798. mat.setColumn( 0, rvec );
  1799. mat.setColumn( 1, fvec );
  1800. mat.setColumn( 2, node.normal );
  1801. mat.setColumn( 3, node.point );
  1802. AssertFatal( m_matF_determinant( mat ) != 0.0f, "no inverse!");
  1803. return mat;
  1804. }
  1805. void MeshRoad::calcSliceTransform( U32 idx, MatrixF &mat )
  1806. {
  1807. if ( mSlices.size() - 1 < idx )
  1808. return;
  1809. bool hasNext = idx + 1 < mSlices.size();
  1810. bool hasPrev = (S32)idx - 1 >= 0;
  1811. const MeshRoadSlice &slice = mSlices[idx];
  1812. VectorF fvec( 0, 1, 0 );
  1813. if ( hasNext )
  1814. {
  1815. fvec = mSlices[idx+1].p1 - slice.p1;
  1816. fvec.normalizeSafe();
  1817. }
  1818. else if ( hasPrev )
  1819. {
  1820. fvec = slice.p1 - mSlices[idx-1].p1;
  1821. fvec.normalizeSafe();
  1822. }
  1823. else
  1824. fvec = mPerp( slice.normal );
  1825. if ( fvec.isZero() )
  1826. fvec = mPerp( slice.normal );
  1827. F32 dot = mDot( fvec, slice.normal );
  1828. if ( dot < -0.9f || dot > 0.9f )
  1829. fvec = mPerp( slice.normal );
  1830. VectorF rvec = mCross( fvec, slice.normal );
  1831. if ( rvec.isZero() )
  1832. rvec = mPerp( fvec );
  1833. rvec.normalize();
  1834. fvec = mCross( slice.normal, rvec );
  1835. fvec.normalize();
  1836. mat.setColumn( 0, rvec );
  1837. mat.setColumn( 1, fvec );
  1838. mat.setColumn( 2, slice.normal );
  1839. mat.setColumn( 3, slice.p1 );
  1840. AssertFatal( m_matF_determinant( mat ) != 0.0f, "no inverse!");
  1841. }
  1842. F32 MeshRoad::getRoadLength() const
  1843. {
  1844. F32 length = 0.0f;
  1845. for ( U32 i = 0; i < mSegments.size(); i++ )
  1846. {
  1847. length += mSegments[i].length();
  1848. }
  1849. return length;
  1850. }
  1851. void MeshRoad::deleteNode( U32 idx )
  1852. {
  1853. if ( mNodes.size() - 1 < idx )
  1854. return;
  1855. mNodes.erase(idx);
  1856. _regenerate();
  1857. setMaskBits( RegenMask | NodeMask );
  1858. }
  1859. U32 MeshRoad::_addNode( const Point3F &pos, const F32 &width, const F32 &depth, const VectorF &normal )
  1860. {
  1861. mNodes.increment();
  1862. MeshRoadNode &node = mNodes.last();
  1863. node.point = pos;
  1864. node.width = width;
  1865. node.depth = depth;
  1866. node.normal = normal;
  1867. setMaskBits( NodeMask | RegenMask );
  1868. return mNodes.size() - 1;
  1869. }
  1870. U32 MeshRoad::_insertNode( const Point3F &pos, const F32 &width, const F32 &depth, const VectorF &normal, const U32 &idx )
  1871. {
  1872. U32 ret;
  1873. MeshRoadNode *node;
  1874. if ( idx == U32_MAX )
  1875. {
  1876. mNodes.increment();
  1877. node = &mNodes.last();
  1878. ret = mNodes.size() - 1;
  1879. }
  1880. else
  1881. {
  1882. mNodes.insert( idx );
  1883. node = &mNodes[idx];
  1884. ret = idx;
  1885. }
  1886. node->point = pos;
  1887. node->depth = depth;
  1888. node->width = width;
  1889. node->normal = normal;
  1890. return ret;
  1891. }
  1892. bool MeshRoad::collideRay( const Point3F &origin, const Point3F &direction, U32 *nodeIdx, Point3F *collisionPnt )
  1893. {
  1894. Point3F p0 = origin;
  1895. Point3F p1 = origin + direction * 2000.0f;
  1896. // If the line segment does not collide with the MeshRoad's world box,
  1897. // it definitely does not collide with any part of the river.
  1898. if ( !getWorldBox().collideLine( p0, p1 ) )
  1899. return false;
  1900. if ( mSlices.size() < 2 )
  1901. return false;
  1902. MathUtils::Quad quad;
  1903. MathUtils::Ray ray;
  1904. F32 t;
  1905. // Check each road segment (formed by a pair of slices) for collision
  1906. // with the line segment.
  1907. for ( U32 i = 0; i < mSlices.size() - 1; i++ )
  1908. {
  1909. const MeshRoadSlice &slice0 = mSlices[i];
  1910. const MeshRoadSlice &slice1 = mSlices[i+1];
  1911. // For simplicities sake we will only test for collision between the
  1912. // line segment and the Top face of the river segment.
  1913. // Clockwise starting with the leftmost/closest point.
  1914. quad.p00 = slice0.p0;
  1915. quad.p01 = slice1.p0;
  1916. quad.p11 = slice1.p2;
  1917. quad.p10 = slice0.p2;
  1918. ray.origin = origin;
  1919. ray.direction = direction;
  1920. if ( MathUtils::mRayQuadCollide( quad, ray, NULL, &t ) )
  1921. {
  1922. if ( nodeIdx )
  1923. *nodeIdx = slice0.parentNodeIdx;
  1924. if ( collisionPnt )
  1925. *collisionPnt = ray.origin + ray.direction * t;
  1926. return true;
  1927. }
  1928. }
  1929. return false;
  1930. }
  1931. void MeshRoad::regenerate()
  1932. {
  1933. _regenerate();
  1934. setMaskBits( RegenMask );
  1935. }
  1936. //-------------------------------------------------------------------------
  1937. // Console Methods
  1938. //-------------------------------------------------------------------------
  1939. DefineEngineMethod( MeshRoad, setNodeDepth, void, ( S32 idx, F32 meters ),,
  1940. "Intended as a helper to developers and editor scripts.\n"
  1941. "Sets the depth in meters of a particular node."
  1942. )
  1943. {
  1944. object->setNodeDepth( idx, meters );
  1945. }
  1946. DefineEngineMethod( MeshRoad, regenerate, void, (),,
  1947. "Intended as a helper to developers and editor scripts.\n"
  1948. "Force MeshRoad to recreate its geometry."
  1949. )
  1950. {
  1951. object->regenerate();
  1952. }
  1953. DefineEngineMethod( MeshRoad, postApply, void, (),,
  1954. "Intended as a helper to developers and editor scripts.\n"
  1955. "Force trigger an inspectPostApply. This will transmit "
  1956. "material and other fields ( not including nodes ) to client objects."
  1957. )
  1958. {
  1959. object->inspectPostApply();
  1960. }
  1961. bool MeshRoad::buildPolyList_TopSurfaceOnly = false;
  1962. bool MeshRoad::buildTopPolyList(PolyListContext plc, AbstractPolyList* polyList)
  1963. {
  1964. static Box3F box_prox; static SphereF ball_prox;
  1965. buildPolyList_TopSurfaceOnly = true;
  1966. bool result = buildPolyList(plc, polyList, box_prox, ball_prox);
  1967. buildPolyList_TopSurfaceOnly = false;
  1968. return result;
  1969. }