meshRoad.cpp 66 KB

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