W3DRoadBuffer.cpp 107 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306
  1. /*
  2. ** Command & Conquer Generals(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. ////////////////////////////////////////////////////////////////////////////////
  19. // //
  20. // (c) 2001-2003 Electronic Arts Inc. //
  21. // //
  22. ////////////////////////////////////////////////////////////////////////////////
  23. // FILE: W3DRoadBuffer.cpp ////////////////////////////////////////////////
  24. //-----------------------------------------------------------------------------
  25. //
  26. // Westwood Studios Pacific.
  27. //
  28. // Confidential Information
  29. // Copyright (C) 2001 - All Rights Reserved
  30. //
  31. //-----------------------------------------------------------------------------
  32. //
  33. // Project: RTS3
  34. //
  35. // File name: W3DRoadBuffer.cpp
  36. //
  37. // Created: John Ahlquist, May 2001
  38. //
  39. // Desc: Draw buffer to handle all the roads in a scene.
  40. //
  41. //-----------------------------------------------------------------------------
  42. //-----------------------------------------------------------------------------
  43. // Includes
  44. //-----------------------------------------------------------------------------
  45. #include "W3DDevice/GameClient/W3DRoadBuffer.h"
  46. #include <stdio.h>
  47. #include <string.h>
  48. #include <assetmgr.h>
  49. #include <texture.h>
  50. #include "common/GlobalData.h"
  51. #include "common/RandomValue.h"
  52. //#include "Common/GameFileSystem.h"
  53. #include "Common/FileSystem.h" // for LOAD_TEST_ASSETS
  54. #include "GameClient/TerrainRoads.h"
  55. #include "W3DDevice/GameClient/TerrainTex.h"
  56. #include "W3DDevice/GameClient/HeightMap.h"
  57. #include "W3DDevice/GameClient/W3DAssetManager.h"
  58. #include "W3DDevice/GameClient/W3DDynamicLight.h"
  59. #include "W3DDevice/GameClient/WorldHeightMap.h"
  60. #include "W3DDevice/GameClient/W3DShaderManager.h"
  61. #include "WW3D2/Camera.h"
  62. #include "WW3D2/DX8Wrapper.h"
  63. #include "WW3D2/DX8Renderer.h"
  64. #include "WW3D2/Mesh.h"
  65. #include "WW3D2/MeshMdl.h"
  66. static const Real TEE_WIDTH_ADJUSTMENT = 1.03f;
  67. #ifdef _INTERNAL
  68. // for occasional debugging...
  69. //#pragma optimize("", off)
  70. //#pragma MESSAGE("************************************** WARNING, optimization disabled for debugging purposes")
  71. #endif
  72. //-----------------------------------------------------------------------------
  73. // Private Data
  74. //-----------------------------------------------------------------------------
  75. // A W3D shader that does alpha, texturing, tests zbuffer, doesn't update zbuffer.
  76. #define SC_ALPHA_DETAIL ( SHADE_CNST(ShaderClass::PASS_LEQUAL, ShaderClass::DEPTH_WRITE_DISABLE, ShaderClass::COLOR_WRITE_ENABLE, ShaderClass::SRCBLEND_SRC_ALPHA, \
  77. ShaderClass::DSTBLEND_ONE_MINUS_SRC_ALPHA, ShaderClass::FOG_DISABLE, ShaderClass::GRADIENT_MODULATE, ShaderClass::SECONDARY_GRADIENT_DISABLE, ShaderClass::TEXTURING_ENABLE, \
  78. ShaderClass::ALPHATEST_DISABLE, ShaderClass::CULL_MODE_ENABLE, \
  79. ShaderClass::DETAILCOLOR_SCALE, ShaderClass::DETAILALPHA_DISABLE) )
  80. static ShaderClass detailAlphaShader(SC_ALPHA_DETAIL);
  81. #define SC_ALPHA_MIRROR ( SHADE_CNST(ShaderClass::PASS_LEQUAL, ShaderClass::DEPTH_WRITE_DISABLE, ShaderClass::COLOR_WRITE_ENABLE, ShaderClass::SRCBLEND_ONE, \
  82. ShaderClass::DSTBLEND_ZERO, ShaderClass::FOG_DISABLE, ShaderClass::GRADIENT_MODULATE, ShaderClass::SECONDARY_GRADIENT_DISABLE, ShaderClass::TEXTURING_ENABLE, \
  83. ShaderClass::ALPHATEST_DISABLE, ShaderClass::CULL_MODE_DISABLE, \
  84. ShaderClass::DETAILCOLOR_DISABLE, ShaderClass::DETAILALPHA_DISABLE) )
  85. static ShaderClass detailShader(SC_ALPHA_MIRROR);
  86. // The radius of the center of the road is 1.5 - outer radius 2.0, inner radius 1.0.
  87. static const Real CORNER_RADIUS = 1.5f;
  88. // The radius of the center of the road is 0.5 - outer radius 1.0, inner radius 0.0.
  89. static const Real TIGHT_CORNER_RADIUS = 0.5f;
  90. /*
  91. // ShaderClass::PASS_ALWAYS,
  92. #define SC_ALPHA_2D ( SHADE_CNST(PASS_ALWAYS, DEPTH_WRITE_DISABLE, COLOR_WRITE_ENABLE, \
  93. SRCBLEND_SRC_ALPHA, DSTBLEND_ONE_MINUS_SRC_ALPHA, FOG_DISABLE, GRADIENT_DISABLE, \
  94. SECONDARY_GRADIENT_DISABLE, TEXTURING_ENABLE, DETAILCOLOR_DISABLE, DETAILALPHA_DISABLE, \
  95. ALPHATEST_DISABLE, CULL_MODE_ENABLE, DETAILCOLOR_DISABLE, DE AILALPHA_DISABLE) )
  96. ShaderClass ShaderClass::_PresetAlpha2DShader(SC_ALPHA_2D);
  97. */
  98. /** Calculate the sign of the cross product. If the tails of the vectors are both placed
  99. at 0,0, then the cross product can be interpreted as -1 means v2 is to the right of v1,
  100. 1 means v2 is to the left of v1, and 0 means v2 is parallel to v1. */
  101. static Int xpSign(const Vector2 &v1, const Vector2 &v2) {
  102. Real xpdct = v1.X*v2.Y - v1.Y*v2.X;
  103. if (xpdct<0) return -1;
  104. if (xpdct>0) return 1;
  105. return 0;
  106. }
  107. //-----------------------------------------------------------------------------
  108. // Private Class
  109. //-----------------------------------------------------------------------------
  110. //=============================================================================
  111. // RoadType constructor
  112. //=============================================================================
  113. /** Nulls index & vertex data. */
  114. //=============================================================================
  115. RoadType::RoadType(void):
  116. m_roadTexture(NULL),
  117. m_vertexRoad(NULL),
  118. m_indexRoad(NULL),
  119. m_stackingOrder(0),
  120. m_uniqueID(-1)
  121. {
  122. }
  123. //=============================================================================
  124. // RoadType destructor
  125. //=============================================================================
  126. /** Frees index & vertex data. */
  127. //=============================================================================
  128. RoadType::~RoadType(void)
  129. {
  130. REF_PTR_RELEASE(m_roadTexture);
  131. REF_PTR_RELEASE(m_vertexRoad);
  132. REF_PTR_RELEASE(m_indexRoad);
  133. }
  134. //=============================================================================
  135. // RoadType applyTexture
  136. //=============================================================================
  137. /** Sets the W3D texture. */
  138. //=============================================================================
  139. void RoadType::applyTexture(void)
  140. {
  141. W3DShaderManager::setTexture(0,m_roadTexture);
  142. DX8Wrapper::Set_Index_Buffer(m_indexRoad,0);
  143. DX8Wrapper::Set_Vertex_Buffer(m_vertexRoad);
  144. }
  145. //=============================================================================
  146. // RoadType loadTexture
  147. //=============================================================================
  148. /** Sets the W3D texture. */
  149. //=============================================================================
  150. void RoadType::loadTexture(AsciiString path, Int ID)
  151. {
  152. /// @todo - delay loading textures and only load textures referenced by map.
  153. WW3DAssetManager *pMgr = W3DAssetManager::Get_Instance();
  154. m_roadTexture = pMgr->Get_Texture(path.str(), TextureClass::MIP_LEVELS_3);
  155. m_roadTexture->Set_Mip_Mapping( TextureClass::FILTER_TYPE_BEST );
  156. m_roadTexture->Set_U_Addr_Mode(TextureClass::TEXTURE_ADDRESS_REPEAT);
  157. m_roadTexture->Set_V_Addr_Mode(TextureClass::TEXTURE_ADDRESS_REPEAT);
  158. m_vertexRoad=NEW_REF(DX8VertexBufferClass,(DX8_FVF_XYZDUV1,TheGlobalData->m_maxRoadVertex+4,DX8VertexBufferClass::USAGE_DYNAMIC));
  159. m_indexRoad=NEW_REF(DX8IndexBufferClass,(TheGlobalData->m_maxRoadIndex+4, DX8IndexBufferClass::USAGE_DYNAMIC));
  160. m_numRoadVertices=0;
  161. m_numRoadIndices=0;
  162. #ifdef LOAD_TEST_ASSETS
  163. m_texturePath = path;
  164. #endif
  165. m_uniqueID = ID;
  166. }
  167. #ifdef LOAD_TEST_ASSETS
  168. //=============================================================================
  169. // RoadType loadTexture
  170. //=============================================================================
  171. /** Sets the W3D texture. */
  172. //=============================================================================
  173. void RoadType::loadTestTexture(void)
  174. {
  175. if (m_isAutoLoaded && m_uniqueID>0 && !m_texturePath.isEmpty()) {
  176. /// @todo - delay loading textures and only load textures referenced by map.
  177. m_roadTexture = NEW_REF(TextureClass, (m_texturePath.str(), m_texturePath.str(), TextureClass::MIP_LEVELS_3));
  178. m_roadTexture->Set_Mip_Mapping( TextureClass::FILTER_TYPE_BEST );
  179. m_roadTexture->Set_U_Addr_Mode(TextureClass::TEXTURE_ADDRESS_REPEAT);
  180. m_roadTexture->Set_V_Addr_Mode(TextureClass::TEXTURE_ADDRESS_REPEAT);
  181. }
  182. }
  183. #endif
  184. //=============================================================================
  185. // RoadSegment constructor
  186. //=============================================================================
  187. /** Nulls index & vertex data. */
  188. //=============================================================================
  189. RoadSegment::RoadSegment(void)
  190. {
  191. m_numVertex = 0;
  192. m_vb = NULL;
  193. m_numIndex = 0;
  194. m_ib = NULL;
  195. }
  196. //=============================================================================
  197. // RoadSegment destructor
  198. //=============================================================================
  199. /** Frees index & vertex data. */
  200. //=============================================================================
  201. RoadSegment::~RoadSegment(void)
  202. {
  203. m_numVertex = 0;
  204. if (m_vb) {
  205. delete m_vb;
  206. }
  207. m_vb= NULL;
  208. m_numIndex = 0;
  209. if (m_ib) {
  210. delete m_ib;
  211. }
  212. m_ib = NULL;
  213. }
  214. //=============================================================================
  215. // RoadSegment::SetVertexBuffer
  216. //=============================================================================
  217. /** Allocates & sets the vertex entries. */
  218. //=============================================================================
  219. void RoadSegment::SetVertexBuffer(VertexFormatXYZDUV1 *vb, Int numVertex)
  220. {
  221. if (m_vb) {
  222. delete m_vb;
  223. m_vb = NULL;
  224. m_numVertex = 0;
  225. }
  226. Vector3 verts[MAX_SEG_VERTEX];
  227. if (numVertex<1 || numVertex > MAX_SEG_VERTEX) return;
  228. m_vb = NEW VertexFormatXYZDUV1[numVertex]; // pool[]ify
  229. if (!m_vb) return;
  230. m_numVertex = numVertex;
  231. memcpy(m_vb, vb, numVertex*sizeof(VertexFormatXYZDUV1));
  232. Int i;
  233. for (i=0; i<numVertex; i++) {
  234. verts[i].X = m_vb[i].x;
  235. verts[i].Y = m_vb[i].y;
  236. verts[i].Z = m_vb[i].z;
  237. }
  238. SphereClass bounds(verts, numVertex);
  239. m_bounds = bounds;
  240. }
  241. //=============================================================================
  242. // RoadSegment::SetIndexBuffer
  243. //=============================================================================
  244. /** Allocates & sets the index entries. */
  245. //=============================================================================
  246. void RoadSegment::SetIndexBuffer(UnsignedShort *ib, Int numIndex)
  247. {
  248. if (m_ib) {
  249. delete m_ib;
  250. m_ib = NULL;
  251. m_numIndex = 0;
  252. }
  253. if (numIndex < 1 || numIndex > MAX_SEG_INDEX) return;
  254. m_ib = NEW UnsignedShort[numIndex];
  255. if (!m_ib) return;
  256. m_numIndex = numIndex;
  257. memcpy(m_ib, ib, numIndex*sizeof(UnsignedShort));
  258. }
  259. //=============================================================================
  260. // RoadSegment::GetVertices
  261. //=============================================================================
  262. /** Copies vertex entries into destination_vb. */
  263. //=============================================================================
  264. Int RoadSegment::GetVertices(VertexFormatXYZDUV1 *destination_vb, Int numToCopy)
  265. {
  266. if (m_vb == NULL || numToCopy<1) return (0);
  267. if (numToCopy > m_numVertex) return(0);
  268. memcpy(destination_vb, m_vb, numToCopy*sizeof(VertexFormatXYZDUV1));
  269. return(numToCopy);
  270. }
  271. //=============================================================================
  272. // RoadSegment::GetIndices
  273. //=============================================================================
  274. /** Copies index entries into destination_ib. */
  275. //=============================================================================
  276. Int RoadSegment::GetIndices(UnsignedShort *destination_ib, Int numToCopy, Int offset)
  277. {
  278. if (m_ib == NULL || numToCopy<1) return (0);
  279. if (numToCopy > m_numIndex) return(0);
  280. Int i;
  281. for (i=0; i<numToCopy; i++) {
  282. destination_ib[i] = m_ib[i]+offset;
  283. }
  284. return(numToCopy);
  285. }
  286. //=============================================================================
  287. // RoadSegment::updateSegLighting
  288. //=============================================================================
  289. /** Updates the diffuse lighting in the vertex buffer. */
  290. //=============================================================================
  291. void RoadSegment::updateSegLighting(void)
  292. {
  293. Int i;
  294. for (i=0; i<m_numVertex; i++) {
  295. Int x = m_vb[i].x/MAP_XY_FACTOR+0.5;
  296. Int y = m_vb[i].y/MAP_XY_FACTOR+0.5;
  297. x += TheTerrainRenderObject->getMap()->getBorderSize();
  298. y += TheTerrainRenderObject->getMap()->getBorderSize();
  299. m_vb[i].diffuse = (255<<24)|TheTerrainRenderObject->getStaticDiffuse(x, y);
  300. }
  301. }
  302. //-----------------------------------------------------------------------------
  303. // Private Methods
  304. //-----------------------------------------------------------------------------
  305. //=============================================================================
  306. // W3DRoadBuffer::loadTee
  307. //=============================================================================
  308. /** Loads a tee into the vertex buffer for a road join. */
  309. //=============================================================================
  310. void W3DRoadBuffer::loadTee(RoadSegment *pRoad, Vector2 loc1,
  311. Vector2 loc2, Bool is4Way, Real scale)
  312. {
  313. Vector2 roadVector(loc2.X-loc1.X, loc2.Y-loc1.Y);
  314. Vector2 roadNormal(-roadVector.Y, roadVector.X);
  315. // Real roadLen = scale;
  316. if (abs(roadVector.X) < MIN_ROAD_SEGMENT && abs(roadVector.Y) < MIN_ROAD_SEGMENT) {
  317. roadVector.Set(1.0f, 0.0f);
  318. roadNormal.Set(0.0f, 1.0f);
  319. } else {
  320. roadVector.Normalize();
  321. roadNormal.Normalize();
  322. }
  323. Real uOffset = (425.0f/512.0f);
  324. Real vOffset = (255.0f/512.0f);
  325. if (is4Way) {
  326. uOffset = (425.0f/512.0f);
  327. vOffset = (425.0f/512.0f);
  328. }
  329. Real teeFactor = scale*TEE_WIDTH_ADJUSTMENT/2.0f;
  330. Real left = pRoad->m_widthInTexture*scale/2.0f;
  331. loadFloatSection(pRoad, loc1, loc2-loc1, teeFactor, left, teeFactor, uOffset, vOffset, scale);
  332. }
  333. //=============================================================================
  334. // W3DRoadBuffer::loadAlphaJoin
  335. //=============================================================================
  336. /** Loads an alpha join into the vertex buffer for a road join. */
  337. //=============================================================================
  338. void W3DRoadBuffer::loadAlphaJoin(RoadSegment *pRoad, Vector2 loc1,
  339. Vector2 loc2,Real scale)
  340. {
  341. Vector2 roadVector(loc2.X-loc1.X, loc2.Y-loc1.Y);
  342. Vector2 roadNormal(-roadVector.Y, roadVector.X);
  343. // Real roadLen = scale;
  344. if (abs(roadVector.X) < MIN_ROAD_SEGMENT && abs(roadVector.Y) < MIN_ROAD_SEGMENT) {
  345. roadVector.Set(1.0f, 0.0f);
  346. roadNormal.Set(0.0f, 1.0f);
  347. } else {
  348. roadVector.Normalize();
  349. roadNormal.Normalize();
  350. }
  351. Real uOffset = (106.0f/512.0f);
  352. Real vOffset = (425.0f/512.0f);
  353. Real roadwidth = scale;
  354. Real uScale = pRoad->m_widthInTexture;
  355. roadVector *= roadwidth*48/128; // we just want 48 pixels.
  356. roadNormal *= uScale*(1+8.0f/128); // we want 8 extra pixels.
  357. Vector2 corners[NUM_CORNERS];
  358. corners[topLeft] = loc1 + roadNormal*0.5f - roadVector*0.65f ;
  359. corners[bottomLeft] = corners[topLeft] - roadNormal;
  360. corners[bottomRight] = corners[bottomLeft] + roadVector;
  361. corners[topRight] = corners[topLeft] + roadVector;
  362. loadFloat4PtSection(pRoad, loc1, roadNormal, roadVector, corners, uOffset, vOffset, scale, uScale);
  363. }
  364. //=============================================================================
  365. // W3DRoadBuffer::loadY
  366. //=============================================================================
  367. /** Loads a Y into the vertex buffer for a road join. */
  368. //=============================================================================
  369. void W3DRoadBuffer::loadY(RoadSegment *pRoad, Vector2 loc1,
  370. Vector2 loc2,Real scale)
  371. {
  372. Vector2 roadVector(loc2.X-loc1.X, loc2.Y-loc1.Y);
  373. Vector2 roadNormal(-roadVector.Y, roadVector.X);
  374. // Real roadLen = scale;
  375. if (abs(roadVector.X) < MIN_ROAD_SEGMENT && abs(roadVector.Y) < MIN_ROAD_SEGMENT) {
  376. roadVector.Set(1.0f, 0.0f);
  377. roadNormal.Set(0.0f, 1.0f);
  378. } else {
  379. roadVector.Normalize();
  380. roadNormal.Normalize();
  381. }
  382. Real uOffset = (255.0f/512.0f);
  383. Real vOffset = (226.0f/512.0f);
  384. Real roadwidth = scale;
  385. roadVector *= roadwidth;
  386. roadNormal *= roadwidth;
  387. Vector2 corners[NUM_CORNERS];
  388. roadVector *= 1.59f;
  389. corners[topLeft] = loc1 + roadNormal*0.29f - roadVector*0.5f ;
  390. corners[bottomLeft] = corners[topLeft] - roadNormal*1.08f;
  391. corners[bottomRight] = corners[bottomLeft] + roadVector;
  392. corners[topRight] = corners[topLeft] + roadVector;
  393. loadFloat4PtSection(pRoad, loc1, roadNormal, roadVector, corners, uOffset, vOffset, scale, scale);
  394. }
  395. //=============================================================================
  396. // W3DRoadBuffer::loadH
  397. //=============================================================================
  398. /** Loads a h shaped tee into the vertex buffer for a road join. */
  399. //=============================================================================
  400. void W3DRoadBuffer::loadH(RoadSegment *pRoad, Vector2 loc1,
  401. Vector2 loc2, Bool flip, Real scale)
  402. {
  403. Vector2 roadVector(loc2.X-loc1.X, loc2.Y-loc1.Y);
  404. Vector2 roadNormal(-roadVector.Y, roadVector.X);
  405. // Real roadLen = scale;
  406. if (abs(roadVector.X) < MIN_ROAD_SEGMENT && abs(roadVector.Y) < MIN_ROAD_SEGMENT) {
  407. roadVector.Set(1.0f, 0.0f);
  408. roadNormal.Set(0.0f, 1.0f);
  409. } else {
  410. roadVector.Normalize();
  411. roadNormal.Normalize();
  412. }
  413. Real uOffset = (202.0f/512.0f);
  414. Real vOffset = (364.0f/512.0f);
  415. Real roadwidth = scale;
  416. roadVector *= roadwidth;
  417. roadNormal *= roadwidth;
  418. Vector2 corners[NUM_CORNERS];
  419. roadNormal *= 1.35f;
  420. if (flip) {
  421. corners[bottomLeft] = loc1 - roadNormal*0.20f - roadVector*pRoad->m_widthInTexture/2;
  422. } else {
  423. corners[bottomLeft] = loc1 - roadNormal*0.8f - roadVector*pRoad->m_widthInTexture/2;
  424. }
  425. Vector2 width = roadVector*pRoad->m_widthInTexture/2;
  426. width = width + roadVector * 1.2f;
  427. corners[bottomRight] = corners[bottomLeft] + width;
  428. corners[topRight] = corners[bottomRight] + roadNormal;
  429. corners[topLeft] = corners[bottomLeft] + roadNormal;
  430. if (flip) roadNormal = -roadNormal;
  431. loadFloat4PtSection(pRoad, loc1, roadNormal, roadVector, corners, uOffset, vOffset, scale, scale);
  432. }
  433. //=============================================================================
  434. // W3DRoadBuffer::loadFloatSection
  435. //=============================================================================
  436. /** Loads a section of road using a mesh that floats a little above the
  437. terrain. */
  438. //=============================================================================
  439. void W3DRoadBuffer::loadFloatSection(RoadSegment *pRoad, Vector2 loc,
  440. Vector2 roadVector, Real halfHeight, Real left, Real right,
  441. Real uOffset, Real vOffset, Real scale)
  442. {
  443. if (m_map==NULL) {
  444. return;
  445. }
  446. Vector2 roadNormal(-roadVector.Y, roadVector.X);
  447. roadVector.Normalize();
  448. roadVector *= right;
  449. roadNormal.Normalize();
  450. if (halfHeight<0) halfHeight = -halfHeight;
  451. roadNormal *= halfHeight;
  452. Vector2 roadLeft = roadVector;
  453. roadLeft.Normalize();
  454. roadLeft *= left;
  455. roadVector += roadLeft;
  456. Vector2 leftCenter = loc;
  457. leftCenter -= roadLeft;
  458. Vector2 corners[NUM_CORNERS];
  459. corners[bottomLeft] = leftCenter - roadNormal;
  460. corners[bottomRight] = corners[bottomLeft];
  461. corners[bottomRight] += roadVector;
  462. corners[topRight] = corners[bottomRight];
  463. corners[topRight] += 2*roadNormal;
  464. corners[topLeft] = corners[bottomLeft];
  465. corners[topLeft] += 2*roadNormal;
  466. loadFloat4PtSection(pRoad, loc, roadNormal, roadVector, corners, uOffset, vOffset, scale, scale);
  467. }
  468. //=============================================================================
  469. // W3DRoadBuffer::loadFloat4PtSection
  470. //=============================================================================
  471. /** Loads a section of road using a mesh that floats a little above the
  472. terrain. The road is loaded into the quadrilateral defined by the
  473. 4 corners points. loc specifies the point where u==uOffset && v==vOffset, and
  474. the road vector gives the direction of the road, and the road normal is perpendicular
  475. to the road normal. */
  476. //=============================================================================
  477. void W3DRoadBuffer::loadFloat4PtSection(RoadSegment *pRoad, Vector2 loc,
  478. Vector2 roadNormal, Vector2 roadVector,
  479. Vector2 *cornersP,
  480. Real uOffset, Real vOffset, Real uScale, Real vScale)
  481. {
  482. const Real FLOAT_AMOUNT = MAP_HEIGHT_SCALE/8;
  483. const Real MAX_ERROR = MAP_HEIGHT_SCALE*1.1f;
  484. UnsignedShort ib[MAX_SEG_INDEX];
  485. VertexFormatXYZDUV1 vb[MAX_SEG_VERTEX];
  486. Int numRoadVertices = 0;
  487. Int numRoadIndices = 0;
  488. TRoadSegInfo info;
  489. info.loc = loc;
  490. info.roadNormal = roadNormal;
  491. info.roadVector = roadVector;
  492. info.corners[bottomLeft] = cornersP[bottomLeft];
  493. info.corners[bottomRight] = cornersP[bottomRight];
  494. info.corners[topLeft] = cornersP[topLeft];
  495. info.corners[topRight] = cornersP[topRight];
  496. info.uOffset = uOffset;
  497. info.vOffset = vOffset;
  498. info.scale = uScale;
  499. pRoad->SetRoadSegInfo(&info);
  500. Real roadLen = roadVector.Length();
  501. Real halfHeight = roadNormal.Length();
  502. roadNormal.Normalize();
  503. roadVector.Normalize();
  504. Vector2 curVector;
  505. Int uCount = (roadLen/MAP_XY_FACTOR)+1;
  506. if (uCount<2) uCount = 2;
  507. Int vCount = (2*halfHeight/MAP_XY_FACTOR)+1;
  508. if (vCount<2) vCount = 2;
  509. const int maxRows = 100;
  510. typedef struct {
  511. Bool collapsed;
  512. Bool deleted;
  513. Vector3 vtx[maxRows];
  514. Int diffuseRed;
  515. Bool lightGradient;
  516. Int vertexIndex[maxRows];
  517. Real uIndex;
  518. } TColumn;
  519. const Int DIFFUSE_LIMIT = 25; // if more than that, we tesselate :) jba.
  520. if (vCount>maxRows) vCount = maxRows;
  521. TColumn prevColumn, curColumn, nextColumn;
  522. prevColumn.deleted = true;
  523. curColumn.deleted = true;
  524. Int i, j, k;
  525. Vector2 v2 = cornersP[bottomLeft];
  526. Vector3 origin(v2.X, v2.Y, 0);
  527. v2 = cornersP[bottomRight] - cornersP[bottomLeft];
  528. Vector3 uVector1(v2.X, v2.Y, 0);
  529. v2 = cornersP[topRight] - cornersP[topLeft];
  530. Vector3 uVector2(v2.X, v2.Y, 0);
  531. v2 = cornersP[topLeft];
  532. Vector3 origin2(v2.X, v2.Y, 0);
  533. v2 = cornersP[topLeft] - cornersP[bottomLeft];
  534. Vector3 vVector1(v2.X, v2.Y, 0);
  535. v2 = cornersP[topRight] - cornersP[bottomRight];
  536. Vector3 vVector2(v2.X, v2.Y, 0);
  537. uVector2 += (vVector1 - vVector2);
  538. for (i=0; i<=uCount; i++) {
  539. Real iFactor = ((Real)i / (uCount-1));
  540. Real iBarFactor = 1.0f-iFactor;
  541. if (i<uCount) {
  542. nextColumn.collapsed = false;
  543. nextColumn.deleted = false;
  544. nextColumn.lightGradient = false;
  545. nextColumn.uIndex = i;
  546. Real minHeight=m_map->getMaxHeightValue()*MAP_HEIGHT_SCALE;
  547. Real maxHeight = m_map->getMinHeightValue()*MAP_HEIGHT_SCALE;
  548. for (j=0; j<vCount; j++) {
  549. Real jFactor = ((Real)j / (vCount-1));
  550. Real jBarFactor = 1.0f-jFactor;
  551. nextColumn.vtx[j] = origin + (uVector1 * jBarFactor * iFactor) + (uVector2 * jFactor * iFactor) +
  552. (vVector1 * iBarFactor * jFactor) + (vVector2 * iFactor * jFactor) ;
  553. Real z = TheTerrainRenderObject->getMaxCellHeight(nextColumn.vtx[j].X, nextColumn.vtx[j].Y);
  554. if (z<minHeight) minHeight = z;
  555. if (z>maxHeight) maxHeight = z;
  556. nextColumn.vertexIndex[j] = -1;
  557. nextColumn.vtx[j].Z = z;
  558. Int diffuse = 0;
  559. if (j==0) {
  560. nextColumn.diffuseRed = (diffuse & 0x00ff);
  561. } else {
  562. Int red = diffuse&0x00ff;
  563. if (abs(red-nextColumn.diffuseRed) > DIFFUSE_LIMIT) {
  564. nextColumn.lightGradient = true;
  565. }
  566. }
  567. }
  568. if (true) { // !nextColumn.lightGradient) {
  569. nextColumn.collapsed = true;
  570. nextColumn.vtx[0].Z = maxHeight;
  571. nextColumn.vtx[1] = nextColumn.vtx[vCount-1];
  572. nextColumn.vtx[1].Z = maxHeight;
  573. } else {
  574. for (j=0; j<vCount; j++) {
  575. nextColumn.vtx[j].Z = maxHeight;
  576. }
  577. }
  578. if (i<2) {
  579. curColumn = nextColumn;
  580. } else {
  581. if (prevColumn.collapsed && curColumn.collapsed && nextColumn.collapsed) {
  582. Bool okToDelete = false;
  583. Real theZ = prevColumn.vtx[0].Z * (curColumn.uIndex-prevColumn.uIndex) +
  584. nextColumn.vtx[0].Z * (nextColumn.uIndex-curColumn.uIndex);
  585. theZ /= nextColumn.uIndex-prevColumn.uIndex;
  586. if (theZ >= curColumn.vtx[0].Z && theZ < curColumn.vtx[0].Z + MAX_ERROR) {
  587. theZ = prevColumn.vtx[1].Z * (curColumn.uIndex-prevColumn.uIndex) +
  588. nextColumn.vtx[1].Z * (nextColumn.uIndex-curColumn.uIndex);
  589. theZ /= nextColumn.uIndex-prevColumn.uIndex;
  590. if (theZ >= curColumn.vtx[1].Z && theZ < curColumn.vtx[1].Z + MAX_ERROR) {
  591. okToDelete = true;
  592. }
  593. }
  594. if (okToDelete) {
  595. curColumn.deleted = true;
  596. }
  597. }
  598. }
  599. }
  600. if (!curColumn.deleted && i!=1) {
  601. // Write out the vertices.
  602. for (j=0; j<vCount; j++) {
  603. Real U, V;
  604. if (numRoadVertices >= MAX_SEG_INDEX) {
  605. break;
  606. }
  607. curVector.Set(curColumn.vtx[j].X - loc.X, curColumn.vtx[j].Y - loc.Y);
  608. V = Vector2::Dot_Product(roadNormal, curVector);
  609. U = Vector2::Dot_Product(roadVector, curVector);
  610. Int diffuse = 0;
  611. #ifdef _DEBUG
  612. //diffuse &= 0xFFFF00FF; // strip out green.
  613. #endif
  614. vb[numRoadVertices].u1 = uOffset+U/(uScale*4);
  615. vb[numRoadVertices].v1 = vOffset-V/(vScale*4); // Road is 1/16 texture height.
  616. vb[numRoadVertices].x = curColumn.vtx[j].X;
  617. vb[numRoadVertices].y = curColumn.vtx[j].Y;
  618. vb[numRoadVertices].z = curColumn.vtx[j].Z+FLOAT_AMOUNT;
  619. vb[numRoadVertices].diffuse = diffuse;
  620. curColumn.vertexIndex[j] = numRoadVertices;
  621. numRoadVertices++;
  622. if (j==1 && curColumn.collapsed) {
  623. break;
  624. }
  625. }
  626. if (numRoadVertices >= MAX_SEG_INDEX) {
  627. break;
  628. }
  629. if (i>1) {
  630. // Write out the triangles.
  631. j = 0;
  632. k = 0;
  633. while (j<vCount-1 && k<vCount-1) {
  634. if (numRoadIndices >= MAX_SEG_INDEX) {
  635. break;
  636. }
  637. UnsignedShort *curIb = ib+numRoadIndices;
  638. if (k==0 || !prevColumn.collapsed) {
  639. *curIb++ = prevColumn.vertexIndex[j+1];
  640. *curIb++ = prevColumn.vertexIndex[j];
  641. *curIb++ = curColumn.vertexIndex[k];
  642. numRoadIndices+=3;
  643. }
  644. if (j==0 || !curColumn.collapsed) {
  645. Int offset = 1;
  646. if (curColumn.collapsed && !prevColumn.collapsed) {
  647. offset = vCount-1;
  648. }
  649. *curIb++ = prevColumn.vertexIndex[j+offset];
  650. *curIb++ = curColumn.vertexIndex[k];
  651. *curIb++ = curColumn.vertexIndex[k+1];
  652. numRoadIndices+=3;
  653. }
  654. if (prevColumn.collapsed && curColumn.collapsed) {
  655. break;
  656. }
  657. if (!prevColumn.collapsed) {
  658. j++;
  659. }
  660. if (!curColumn.collapsed) {
  661. k++;
  662. }
  663. }
  664. prevColumn = curColumn;
  665. } else if (i==0) {
  666. prevColumn = curColumn;
  667. }
  668. if (numRoadIndices >= MAX_SEG_INDEX) {
  669. break;
  670. }
  671. }
  672. curColumn = nextColumn;
  673. }
  674. pRoad->SetVertexBuffer(vb, numRoadVertices);
  675. pRoad->SetIndexBuffer(ib, numRoadIndices);
  676. }
  677. //=============================================================================
  678. // W3DRoadBuffer::loadLit4PtSection
  679. //=============================================================================
  680. /** Loads a section of road using a mesh that floats a little above the
  681. terrain. The road is loaded into the quadrilateral defined by the
  682. 4 corners points. loc specifies the point where u==uOffset && v==vOffset, and
  683. the road vector gives the direction of the road, and the road normal is perpendicular
  684. to the road normal. */
  685. //=============================================================================
  686. void W3DRoadBuffer::loadLit4PtSection(RoadSegment *pRoad, UnsignedShort *ib, VertexFormatXYZDUV1 *vb, RefRenderObjListIterator *pDynamicLightsIterator)
  687. {
  688. const Real FLOAT_AMOUNT = MAP_HEIGHT_SCALE/8;
  689. const Real MAX_ERROR = MAP_HEIGHT_SCALE*1.1f;
  690. if (pRoad->m_uniqueID != m_curUniqueID) {
  691. return;
  692. }
  693. // Throw out segs out of view.
  694. if (pRoad->m_pt1.loc.X + pRoad->m_scale/2 < this->m_minX &&
  695. pRoad->m_pt2.loc.X + pRoad->m_scale/2 < this->m_minX) {
  696. return;
  697. }
  698. if (pRoad->m_pt1.loc.X - pRoad->m_scale/2 > this->m_maxX &&
  699. pRoad->m_pt2.loc.X - pRoad->m_scale/2 > this->m_maxX) {
  700. return;
  701. }
  702. // Throw out segs out of view.
  703. if (pRoad->m_pt1.loc.Y + pRoad->m_scale/2 < this->m_minY &&
  704. pRoad->m_pt2.loc.Y + pRoad->m_scale/2 < this->m_minY) {
  705. return;
  706. }
  707. if (pRoad->m_pt1.loc.Y - pRoad->m_scale/2 > this->m_maxY &&
  708. pRoad->m_pt2.loc.Y - pRoad->m_scale/2 > this->m_maxY) {
  709. return;
  710. }
  711. Int numLights = 0;
  712. const Int maxLights = 8;
  713. LightClass *lights[maxLights];
  714. for (pDynamicLightsIterator->First(); !pDynamicLightsIterator->Is_Done(); pDynamicLightsIterator->Next()) {
  715. LightClass *pLight = (LightClass*)pDynamicLightsIterator->Peek_Obj();
  716. SphereClass bounds = pLight->Get_Bounding_Sphere();
  717. if (Spheres_Intersect(pRoad->getBounds(), bounds)) {
  718. lights[numLights] = pLight;
  719. numLights++;
  720. if (numLights == maxLights) break;
  721. }
  722. }
  723. if (numLights == 0) return;
  724. TRoadSegInfo info;
  725. pRoad->GetRoadSegInfo(&info);
  726. Real roadLen = info.roadVector.Length();
  727. Real halfHeight = info.roadNormal.Length();
  728. info.roadNormal.Normalize();
  729. info.roadVector.Normalize();
  730. Vector2 curVector;
  731. Int uCount = (roadLen/MAP_XY_FACTOR)+1;
  732. Int vCount = (2*halfHeight/MAP_XY_FACTOR)+1;
  733. const int maxRows = 100;
  734. typedef struct {
  735. Bool collapsed;
  736. Bool deleted;
  737. Vector3 vtx[maxRows];
  738. Int diffuseRed;
  739. Bool lightGradient;
  740. Int vertexIndex[maxRows];
  741. Real uIndex;
  742. } TColumn;
  743. // const Int DIFFUSE_LIMIT = 25; // if more than that, we tesselate :) jba.
  744. if (vCount>maxRows) vCount = maxRows;
  745. TColumn prevColumn, curColumn, nextColumn;
  746. prevColumn.deleted = true;
  747. curColumn.deleted = true;
  748. Int i, j, k;
  749. Vector2 v2 = info.corners[bottomLeft];
  750. Vector3 origin(v2.X, v2.Y, 0);
  751. v2 = info.corners[bottomRight] - info.corners[bottomLeft];
  752. Vector3 uVector1(v2.X, v2.Y, 0);
  753. v2 = info.corners[topRight] - info.corners[topLeft];
  754. Vector3 uVector2(v2.X, v2.Y, 0);
  755. v2 = info.corners[topLeft];
  756. Vector3 origin2(v2.X, v2.Y, 0);
  757. v2 = info.corners[topLeft] - info.corners[bottomLeft];
  758. Vector3 vVector1(v2.X, v2.Y, 0);
  759. v2 = info.corners[topRight] - info.corners[bottomRight];
  760. Vector3 vVector2(v2.X, v2.Y, 0);
  761. uVector2 += (vVector1 - vVector2);
  762. for (i=0; i<=uCount; i++) {
  763. Real iFactor = ((Real)i / (uCount-1));
  764. Real iBarFactor = 1.0f-iFactor;
  765. if (i<uCount) {
  766. nextColumn.collapsed = false;
  767. nextColumn.deleted = false;
  768. nextColumn.lightGradient = false;
  769. nextColumn.uIndex = i;
  770. Real minHeight=m_map->getMaxHeightValue()*MAP_HEIGHT_SCALE;
  771. Real maxHeight = m_map->getMinHeightValue()*MAP_HEIGHT_SCALE;
  772. for (j=0; j<vCount; j++) {
  773. Real jFactor = ((Real)j / (vCount-1));
  774. Real jBarFactor = 1.0f-jFactor;
  775. nextColumn.vtx[j] = origin + (uVector1 * jBarFactor * iFactor) + (uVector2 * jFactor * iFactor) +
  776. (vVector1 * iBarFactor * jFactor) + (vVector2 * iFactor * jFactor) ;
  777. Real z = TheTerrainRenderObject->getMaxCellHeight(nextColumn.vtx[j].X, nextColumn.vtx[j].Y);
  778. if (z<minHeight) minHeight = z;
  779. if (z>maxHeight) maxHeight = z;
  780. nextColumn.vertexIndex[j] = -1;
  781. nextColumn.vtx[j].Z = z;
  782. Int k;
  783. for (k=0; k<numLights; k++) {
  784. Vector3 offset = nextColumn.vtx[j] - lights[k]->Get_Position();
  785. Real range = lights[k]->Get_Attenuation_Range();
  786. // for culling, expand one cell radius.
  787. range += MAP_XY_FACTOR;
  788. if (offset.Length2() < range*range) {
  789. nextColumn.lightGradient = true;
  790. }
  791. }
  792. }
  793. if (!nextColumn.lightGradient) {
  794. nextColumn.collapsed = true;
  795. nextColumn.vtx[0].Z = maxHeight;
  796. nextColumn.vtx[1] = nextColumn.vtx[vCount-1];
  797. nextColumn.vtx[1].Z = maxHeight;
  798. } else {
  799. for (j=0; j<vCount; j++) {
  800. nextColumn.vtx[j].Z = maxHeight;
  801. }
  802. }
  803. if (i<2) {
  804. curColumn = nextColumn;
  805. } else {
  806. if (prevColumn.collapsed && curColumn.collapsed && nextColumn.collapsed) {
  807. Bool okToDelete = false;
  808. Real theZ = prevColumn.vtx[0].Z * (curColumn.uIndex-prevColumn.uIndex) +
  809. nextColumn.vtx[0].Z * (nextColumn.uIndex-curColumn.uIndex);
  810. theZ /= nextColumn.uIndex-prevColumn.uIndex;
  811. if (theZ >= curColumn.vtx[0].Z && theZ < curColumn.vtx[0].Z + MAX_ERROR) {
  812. theZ = prevColumn.vtx[1].Z * (curColumn.uIndex-prevColumn.uIndex) +
  813. nextColumn.vtx[1].Z * (nextColumn.uIndex-curColumn.uIndex);
  814. theZ /= nextColumn.uIndex-prevColumn.uIndex;
  815. if (theZ >= curColumn.vtx[1].Z && theZ < curColumn.vtx[1].Z + MAX_ERROR) {
  816. okToDelete = true;
  817. }
  818. }
  819. if (okToDelete) {
  820. curColumn.deleted = true;
  821. }
  822. }
  823. }
  824. }
  825. if (!curColumn.deleted && i!=1) {
  826. // Write out the vertices.
  827. for (j=0; j<vCount; j++) {
  828. Real U, V;
  829. if (m_curNumRoadVertices >= m_maxRoadVertex) {
  830. break;
  831. }
  832. curVector.Set(curColumn.vtx[j].X - info.loc.X, curColumn.vtx[j].Y - info.loc.Y);
  833. V = Vector2::Dot_Product(info.roadNormal, curVector);
  834. U = Vector2::Dot_Product(info.roadVector, curVector);
  835. Int diffuse = (255<<24)|TheTerrainRenderObject->getStaticDiffuse(curColumn.vtx[j].X/MAP_XY_FACTOR+0.5, curColumn.vtx[j].Y/MAP_XY_FACTOR+0.5);
  836. Real shadeR, shadeG, shadeB;
  837. shadeB = (diffuse & 0xFF)/255.0;
  838. shadeG = ((diffuse>>8) & 0xFF)/255.0;
  839. shadeR = ((diffuse>>16) & 0xFF)/255.0;
  840. Int k;
  841. for (k=0; k<numLights; k++) {
  842. Real factor;
  843. if (lights[k]->Get_Type() == LightClass::POINT) {
  844. Vector3 lightLoc = lights[k]->Get_Position();
  845. Vector3 vtx = curColumn.vtx[j];
  846. Vector3 offset = vtx - lightLoc;
  847. double range, midRange;
  848. lights[k]->Get_Far_Attenuation_Range(midRange, range);
  849. if (vtx.X < lightLoc.X-range) continue;
  850. if (vtx.X > lightLoc.X+range) continue;
  851. if (vtx.Y < lightLoc.Y-range) continue;
  852. if (vtx.Y > lightLoc.Y+range) continue;
  853. Real dist = offset.Length();
  854. if (dist >= range) continue;
  855. if (midRange < 0.1) continue;
  856. #if 1
  857. factor = 1.0f - (dist - midRange) / (range - midRange);
  858. #else
  859. // f = 1.0 / (atten0 + d*atten1 + d*d/atten2);
  860. if (fabs(range-midRange)<1e-5) {
  861. // if the attenuation range is too small assume uniform with cutoff
  862. factor = 1.0;
  863. } else {
  864. factor = 1.0f/(0.1+dist/midRange + 5.0f*dist*dist/(range*range));
  865. }
  866. #endif
  867. factor = WWMath::Clamp(factor,0.0f,1.0f);
  868. Real shade = 0.5f;
  869. shade *= factor;
  870. Vector3 diffuse;
  871. lights[k]->Get_Diffuse(&diffuse);
  872. Vector3 ambient;
  873. lights[k]->Get_Ambient(&ambient);
  874. if (shade > 1.0) shade = 1.0;
  875. if(shade < 0.0f) shade = 0.0f;
  876. shadeR += shade*diffuse.X;
  877. shadeG += shade*diffuse.Y;
  878. shadeB += shade*diffuse.Z;
  879. shadeR += factor*ambient.X;
  880. shadeG += factor*ambient.Y;
  881. shadeB += factor*ambient.Z;
  882. }
  883. }
  884. if (shadeR > 1.0) shadeR = 1.0;
  885. if(shadeR < 0.0f) shadeR = 0.0f;
  886. if (shadeG > 1.0) shadeG = 1.0;
  887. if(shadeG < 0.0f) shadeG = 0.0f;
  888. if (shadeB > 1.0) shadeB = 1.0;
  889. if(shadeB < 0.0f) shadeB = 0.0f;
  890. shadeR*=255;
  891. shadeG*=255;
  892. shadeB*=255;
  893. diffuse=REAL_TO_INT(shadeB) | (REAL_TO_INT(shadeG) << 8) | (REAL_TO_INT(shadeR) << 16) | ((int)255 << 24);
  894. #ifdef _DEBUG
  895. //diffuse &= 0xFFFF00FF; // strip out green.
  896. #endif
  897. vb[m_curNumRoadVertices].u1 = info.uOffset+U/(info.scale*4);
  898. vb[m_curNumRoadVertices].v1 = info.vOffset-V/(info.scale*4); // Road is 1/16 texture height.
  899. vb[m_curNumRoadVertices].x = curColumn.vtx[j].X;
  900. vb[m_curNumRoadVertices].y = curColumn.vtx[j].Y;
  901. vb[m_curNumRoadVertices].z = curColumn.vtx[j].Z+FLOAT_AMOUNT;
  902. vb[m_curNumRoadVertices].diffuse = diffuse;
  903. curColumn.vertexIndex[j] = m_curNumRoadVertices;
  904. m_curNumRoadVertices++;
  905. if (j==1 && curColumn.collapsed) {
  906. break;
  907. }
  908. }
  909. if (m_curNumRoadVertices >= MAX_SEG_INDEX) {
  910. break;
  911. }
  912. if (i>1 && (!prevColumn.collapsed || !curColumn.collapsed)) {
  913. // Write out the triangles.
  914. j = 0;
  915. k = 0;
  916. while (j<vCount-1 && k<vCount-1) {
  917. if (m_curNumRoadIndices >= m_maxRoadIndex) {
  918. break;
  919. }
  920. UnsignedShort *curIb = ib+m_curNumRoadIndices;
  921. if (k==0 || !prevColumn.collapsed) {
  922. *curIb++ = prevColumn.vertexIndex[j+1];
  923. *curIb++ = prevColumn.vertexIndex[j];
  924. *curIb++ = curColumn.vertexIndex[k];
  925. m_curNumRoadIndices+=3;
  926. }
  927. if (j==0 || !curColumn.collapsed) {
  928. Int offset = 1;
  929. if (curColumn.collapsed && !prevColumn.collapsed) {
  930. offset = vCount-1;
  931. }
  932. *curIb++ = prevColumn.vertexIndex[j+offset];
  933. *curIb++ = curColumn.vertexIndex[k];
  934. *curIb++ = curColumn.vertexIndex[k+1];
  935. m_curNumRoadIndices+=3;
  936. }
  937. if (prevColumn.collapsed && curColumn.collapsed) {
  938. break;
  939. }
  940. if (!prevColumn.collapsed) {
  941. j++;
  942. }
  943. if (!curColumn.collapsed) {
  944. k++;
  945. }
  946. }
  947. prevColumn = curColumn;
  948. } else if (i==0) {
  949. prevColumn = curColumn;
  950. }
  951. if (m_curNumRoadIndices >= MAX_SEG_INDEX) {
  952. break;
  953. }
  954. }
  955. curColumn = nextColumn;
  956. }
  957. }
  958. //=============================================================================
  959. // W3DRoadBuffer::loadCurve
  960. //=============================================================================
  961. /** Loads a curve segment into the vertex buffer for a road end cap or join. */
  962. //=============================================================================
  963. void W3DRoadBuffer::loadCurve(RoadSegment *pRoad, Vector2 loc1, Vector2 loc2, Real scale)
  964. {
  965. Real uOffset = (4.0f/512.0f);
  966. Real vOffset = (255.0f/512.0f); // CORNER_RADIUS radius 1.5 curve.
  967. if (pRoad->m_curveRadius == TIGHT_CORNER_RADIUS) {
  968. vOffset = (425.0f/512.0f);
  969. }
  970. Vector2 roadVector(loc2.X-loc1.X, loc2.Y-loc1.Y);
  971. Vector2 roadNormal(-roadVector.Y, roadVector.X);
  972. Real roadLen = scale;
  973. Real curveHeight = pRoad->m_widthInTexture*scale/2.0f;
  974. roadVector.Normalize();
  975. roadVector *= roadLen;
  976. roadNormal.Normalize();
  977. roadNormal *= abs(curveHeight);
  978. Vector2 corners[NUM_CORNERS];
  979. corners[bottomLeft] = loc1 - roadNormal;
  980. corners[bottomRight] = corners[bottomLeft];
  981. corners[bottomRight] += roadVector;
  982. corners[topRight] = corners[bottomRight];
  983. corners[topRight] += 2*roadNormal;
  984. corners[topLeft] = corners[bottomLeft];
  985. corners[topLeft] += 2*roadNormal;
  986. // Tweak a little
  987. if (pRoad->m_curveRadius == TIGHT_CORNER_RADIUS) {
  988. corners[bottomLeft] = loc1 - roadNormal;
  989. corners[bottomRight] = corners[bottomLeft];
  990. corners[bottomRight] += roadVector*0.5f;
  991. corners[topRight] = corners[bottomRight];
  992. corners[topRight] += 2*roadNormal;
  993. corners[topLeft] = corners[bottomLeft];
  994. corners[topLeft] += 2*roadNormal;
  995. corners[bottomRight] += roadVector*0.1f;
  996. corners[bottomRight] += roadNormal*0.2f;
  997. corners[bottomLeft] -= roadNormal*0.1f;
  998. corners[bottomLeft] -= roadVector*0.02f;
  999. corners[topLeft] -= roadVector*0.02f;
  1000. corners[topRight] -= roadVector*0.4f;
  1001. corners[topRight] += roadNormal*0.2f;
  1002. } else {
  1003. corners[bottomLeft] = loc1 - roadNormal;
  1004. corners[bottomRight] = corners[bottomLeft];
  1005. corners[bottomRight] += roadVector;
  1006. corners[topRight] = corners[bottomRight];
  1007. corners[topRight] += 2*roadNormal;
  1008. corners[topLeft] = corners[bottomLeft];
  1009. corners[topLeft] += 2*roadNormal;
  1010. corners[bottomRight] += roadVector*0.1f;
  1011. corners[bottomRight] += roadNormal*0.4f;
  1012. corners[bottomLeft] -= roadNormal*0.2f;
  1013. corners[bottomLeft] -= roadVector*0.02f;
  1014. corners[topLeft] -= roadVector*0.02f;
  1015. corners[topRight] -= roadVector*0.4f;
  1016. corners[topRight] += roadNormal*0.4f;
  1017. }
  1018. loadFloat4PtSection(pRoad, loc1, roadNormal, roadVector, corners, uOffset, vOffset, scale, scale);
  1019. }
  1020. //=============================================================================
  1021. // W3DRoadBuffer::preloadRoadSegment
  1022. //=============================================================================
  1023. /** Loads a road segment into the vertex buffer for drawing. */
  1024. //=============================================================================
  1025. void W3DRoadBuffer::preloadRoadSegment(RoadSegment *pRoad)
  1026. {
  1027. Vector2 roadVector = pRoad->m_pt2.loc-pRoad->m_pt1.loc;
  1028. Vector2 roadNormal(-roadVector.Y, roadVector.X);
  1029. // Real roadLen = roadVector.Length();
  1030. Real uOffset = 0.0f;
  1031. Real vOffset = (85.0f/512.0f);
  1032. Real roadHeight = pRoad->m_widthInTexture*pRoad->m_scale/2.0f;
  1033. roadNormal.Normalize();
  1034. roadNormal *= roadHeight;
  1035. Vector2 corners[NUM_CORNERS];
  1036. corners[bottomLeft] = pRoad->m_pt1.bottom;
  1037. corners[topLeft] = pRoad->m_pt1.top;
  1038. corners[bottomRight] = pRoad->m_pt2.bottom;
  1039. corners[topRight] = pRoad->m_pt2.top;
  1040. loadFloat4PtSection(pRoad, pRoad->m_pt1.loc, roadNormal, roadVector, corners, uOffset, vOffset,
  1041. pRoad->m_scale, pRoad->m_scale);
  1042. }
  1043. //=============================================================================
  1044. // W3DRoadBuffer::preloadRoadsInVertexAndIndexBuffers
  1045. //=============================================================================
  1046. /** Loads the roads into the vertex buffer for drawing. */
  1047. //=============================================================================
  1048. void W3DRoadBuffer::preloadRoadsInVertexAndIndexBuffers()
  1049. {
  1050. if (!m_initialized) {
  1051. return;
  1052. }
  1053. Int curRoad;
  1054. // Do road segments.
  1055. for (curRoad=0; curRoad<m_numRoads; curRoad++) {
  1056. if (m_roads[curRoad].m_type == SEGMENT) {
  1057. preloadRoadSegment(&m_roads[curRoad]);
  1058. }
  1059. }
  1060. // Do curves.
  1061. for (curRoad=0; curRoad<m_numRoads; curRoad++) {
  1062. if (m_roads[curRoad].m_type == CURVE) {
  1063. loadCurve(&m_roads[curRoad], m_roads[curRoad].m_pt1.loc, m_roads[curRoad].m_pt2.loc, m_roads[curRoad].m_scale);
  1064. }
  1065. }
  1066. // Do tees.
  1067. for (curRoad=0; curRoad<m_numRoads; curRoad++) {
  1068. if (m_roads[curRoad].m_type == THREE_WAY_Y) {
  1069. loadY(&m_roads[curRoad], m_roads[curRoad].m_pt1.loc, m_roads[curRoad].m_pt2.loc,
  1070. m_roads[curRoad].m_scale);
  1071. }
  1072. }
  1073. // Do tees.
  1074. for (curRoad=0; curRoad<m_numRoads; curRoad++) {
  1075. if (m_roads[curRoad].m_type == THREE_WAY_H || m_roads[curRoad].m_type == THREE_WAY_H_FLIP) {
  1076. loadH(&m_roads[curRoad], m_roads[curRoad].m_pt1.loc, m_roads[curRoad].m_pt2.loc,
  1077. m_roads[curRoad].m_type == THREE_WAY_H_FLIP, m_roads[curRoad].m_scale);
  1078. }
  1079. }
  1080. // Do tees.
  1081. for (curRoad=0; curRoad<m_numRoads; curRoad++) {
  1082. if (m_roads[curRoad].m_type == TEE || m_roads[curRoad].m_type == FOUR_WAY) {
  1083. loadTee(&m_roads[curRoad], m_roads[curRoad].m_pt1.loc, m_roads[curRoad].m_pt2.loc,
  1084. (m_roads[curRoad].m_type == FOUR_WAY), m_roads[curRoad].m_scale);
  1085. }
  1086. }
  1087. // Do joins.
  1088. for (curRoad=0; curRoad<m_numRoads; curRoad++) {
  1089. if (m_roads[curRoad].m_type == ALPHA_JOIN) {
  1090. loadAlphaJoin(&m_roads[curRoad], m_roads[curRoad].m_pt1.loc, m_roads[curRoad].m_pt2.loc,
  1091. m_roads[curRoad].m_scale);
  1092. }
  1093. }
  1094. updateLighting();
  1095. }
  1096. //=============================================================================
  1097. // W3DRoadBuffer::loadRoadsInVertexAndIndexBuffers
  1098. //=============================================================================
  1099. /** Loads the roads into the vertex buffer for drawing. */
  1100. //=============================================================================
  1101. void W3DRoadBuffer::loadRoadsInVertexAndIndexBuffers()
  1102. {
  1103. if ( !m_initialized) {
  1104. return;
  1105. }
  1106. m_curNumRoadVertices = 0;
  1107. m_curNumRoadIndices = 0;
  1108. VertexFormatXYZDUV1 *vb;
  1109. UnsignedShort *ib;
  1110. // Lock the buffers.
  1111. if (m_roadTypes[m_curRoadType].getIB() == NULL) {
  1112. this->m_roadTypes[m_curRoadType].setNumVertices(0);
  1113. this->m_roadTypes[m_curRoadType].setNumIndices(0);
  1114. return;
  1115. }
  1116. DX8IndexBufferClass::WriteLockClass lockIdxBuffer(m_roadTypes[m_curRoadType].getIB());
  1117. DX8VertexBufferClass::WriteLockClass lockVtxBuffer(m_roadTypes[m_curRoadType].getVB());
  1118. vb=(VertexFormatXYZDUV1*)lockVtxBuffer.Get_Vertex_Array();
  1119. ib = lockIdxBuffer.Get_Index_Array();
  1120. // Add to the index buffer & vertex buffer.
  1121. Int curRoad;
  1122. // Do road segments.
  1123. TCorner corner;
  1124. for (corner = SEGMENT; corner < NUM_JOINS; corner = (TCorner)(corner+1)) {
  1125. for (curRoad=0; curRoad<m_numRoads; curRoad++) {
  1126. if (m_roads[curRoad].m_type == corner) {
  1127. loadRoadSegment(ib, vb, &m_roads[curRoad]);
  1128. }
  1129. }
  1130. }
  1131. this->m_roadTypes[m_curRoadType].setNumVertices(m_curNumRoadVertices);
  1132. this->m_roadTypes[m_curRoadType].setNumIndices(m_curNumRoadIndices);
  1133. }
  1134. //=============================================================================
  1135. // W3DRoadBuffer::loadLitRoadsInVertexAndIndexBuffers
  1136. //=============================================================================
  1137. /** Loads the roads into the vertex buffer for drawing. */
  1138. //=============================================================================
  1139. void W3DRoadBuffer::loadLitRoadsInVertexAndIndexBuffers(RefRenderObjListIterator *pDynamicLightsIterator)
  1140. {
  1141. if ( !m_initialized) {
  1142. return;
  1143. }
  1144. m_curNumRoadVertices = 0;
  1145. m_curNumRoadIndices = 0;
  1146. VertexFormatXYZDUV1 *vb;
  1147. UnsignedShort *ib;
  1148. // Lock the buffers.
  1149. DX8IndexBufferClass::WriteLockClass lockIdxBuffer(m_roadTypes[m_curRoadType].getIB());
  1150. DX8VertexBufferClass::WriteLockClass lockVtxBuffer(m_roadTypes[m_curRoadType].getVB());
  1151. vb=(VertexFormatXYZDUV1*)lockVtxBuffer.Get_Vertex_Array();
  1152. ib = lockIdxBuffer.Get_Index_Array();
  1153. // Add to the index buffer & vertex buffer.
  1154. Int curRoad;
  1155. if (true) {
  1156. // Do road segments.
  1157. TCorner corner;
  1158. for (corner = SEGMENT; corner < NUM_JOINS; corner = (TCorner)(corner+1)) {
  1159. for (curRoad=0; curRoad<m_numRoads; curRoad++) {
  1160. if (m_roads[curRoad].m_type == corner) {
  1161. loadLit4PtSection(&m_roads[curRoad], ib, vb, pDynamicLightsIterator);
  1162. }
  1163. }
  1164. }
  1165. }
  1166. this->m_roadTypes[m_curRoadType].setNumVertices(m_curNumRoadVertices);
  1167. this->m_roadTypes[m_curRoadType].setNumIndices(m_curNumRoadIndices);
  1168. }
  1169. //=============================================================================
  1170. // W3DRoadBuffer::loadRoadSegment
  1171. //=============================================================================
  1172. /** Loads a road segment into the vertex buffer for drawing. */
  1173. //=============================================================================
  1174. void W3DRoadBuffer::loadRoadSegment(UnsignedShort *ib, VertexFormatXYZDUV1 *vb, RoadSegment *pRoad)
  1175. {
  1176. if (pRoad->m_uniqueID != m_curUniqueID) {
  1177. return;
  1178. }
  1179. // Throw out segs out of view.
  1180. if (pRoad->m_pt1.loc.X + pRoad->m_scale/2 < this->m_minX &&
  1181. pRoad->m_pt2.loc.X + pRoad->m_scale/2 < this->m_minX) {
  1182. return;
  1183. }
  1184. if (pRoad->m_pt1.loc.X - pRoad->m_scale/2 > this->m_maxX &&
  1185. pRoad->m_pt2.loc.X - pRoad->m_scale/2 > this->m_maxX) {
  1186. return;
  1187. }
  1188. // Throw out segs out of view.
  1189. if (pRoad->m_pt1.loc.Y + pRoad->m_scale/2 < this->m_minY &&
  1190. pRoad->m_pt2.loc.Y + pRoad->m_scale/2 < this->m_minY) {
  1191. return;
  1192. }
  1193. if (pRoad->m_pt1.loc.Y - pRoad->m_scale/2 > this->m_maxY &&
  1194. pRoad->m_pt2.loc.Y - pRoad->m_scale/2 > this->m_maxY) {
  1195. return;
  1196. }
  1197. Int curVertex = m_curNumRoadVertices;
  1198. if (curVertex+pRoad->GetNumVertex() >= m_maxRoadVertex) return;
  1199. if (m_curNumRoadIndices+pRoad->GetNumIndex() >= m_maxRoadIndex) return;
  1200. m_curNumRoadVertices += pRoad->GetVertices(vb+curVertex, pRoad->GetNumVertex());
  1201. m_curNumRoadIndices += pRoad->GetIndices(ib+m_curNumRoadIndices, pRoad->GetNumIndex(), curVertex);
  1202. }
  1203. //=============================================================================
  1204. // W3DRoadBuffer::moveRoadSegTo
  1205. //=============================================================================
  1206. /** Moves a road segment. */
  1207. //=============================================================================
  1208. void W3DRoadBuffer::moveRoadSegTo(Int fromNdx, Int toNdx)
  1209. {
  1210. if (fromNdx<0 || fromNdx>=m_numRoads || toNdx<0 || toNdx>=m_numRoads) {
  1211. #ifdef _DEBUG
  1212. DEBUG_LOG(("bad moveRoadSegTo\n"));
  1213. #endif
  1214. return;
  1215. }
  1216. if (fromNdx == toNdx) return;
  1217. RoadSegment cur = m_roads[fromNdx];
  1218. Int i;
  1219. if (fromNdx<toNdx) {
  1220. for (i=fromNdx; i<toNdx; i++) {
  1221. m_roads[i] = m_roads[i+1];
  1222. }
  1223. } else {
  1224. for (i=fromNdx; i>toNdx; i--) {
  1225. m_roads[i] = m_roads[i-1];
  1226. }
  1227. }
  1228. m_roads[toNdx] = cur;
  1229. }
  1230. //=============================================================================
  1231. // W3DRoadBuffer::checkLinkBefore
  1232. //=============================================================================
  1233. /** Checks to see if any segments need to link before this one. */
  1234. //=============================================================================
  1235. void W3DRoadBuffer::checkLinkBefore(Int ndx)
  1236. {
  1237. // Check for things whose loc == our loc2.
  1238. if (m_roads[ndx].m_pt2.count != 1) {
  1239. return;
  1240. }
  1241. Vector2 loc2 = m_roads[ndx].m_pt2.loc;
  1242. #ifdef _DEBUG
  1243. DEBUG_ASSERTLOG(m_roads[ndx].m_pt1.loc == m_roads[ndx+1].m_pt2.loc, ("Bad link\n"));
  1244. if (ndx>0) {
  1245. DEBUG_ASSERTLOG(m_roads[ndx].m_pt2.loc != m_roads[ndx-1].m_pt1.loc, ("Bad Link\n"));
  1246. }
  1247. #endif
  1248. Int endOfCurSeg = ndx+1;
  1249. while (endOfCurSeg<m_numRoads-1) {
  1250. if (m_roads[endOfCurSeg].m_pt1.loc != m_roads[endOfCurSeg+1].m_pt2.loc) {
  1251. break;
  1252. }
  1253. endOfCurSeg++;
  1254. }
  1255. Int checkNdx = endOfCurSeg+1;
  1256. while (checkNdx < m_numRoads) {
  1257. if (m_roads[checkNdx].m_pt1.loc == loc2) {
  1258. #ifdef _DEBUG
  1259. DEBUG_ASSERTLOG(m_roads[checkNdx].m_pt1.count==1, ("Bad count\n"));
  1260. #endif
  1261. moveRoadSegTo(checkNdx, ndx);
  1262. loc2 = m_roads[ndx].m_pt2.loc;
  1263. if (m_roads[ndx].m_pt2.count != 1) return;
  1264. endOfCurSeg++;
  1265. } else if (m_roads[checkNdx].m_pt2.loc == loc2) {
  1266. #ifdef _DEBUG
  1267. if (m_roads[checkNdx].m_pt2.count!=1) {
  1268. ::OutputDebugString("fooey.\n");
  1269. }
  1270. DEBUG_ASSERTLOG(m_roads[checkNdx].m_pt2.count==1, ("Bad count\n"));
  1271. #endif
  1272. flipTheRoad(&m_roads[checkNdx]);
  1273. moveRoadSegTo(checkNdx, ndx);
  1274. loc2 = m_roads[ndx].m_pt2.loc;
  1275. if (m_roads[ndx].m_pt2.count != 1) return;
  1276. endOfCurSeg++;
  1277. } else {
  1278. checkNdx++;
  1279. }
  1280. if (checkNdx <= endOfCurSeg) {
  1281. checkNdx = endOfCurSeg+1;
  1282. }
  1283. }
  1284. }
  1285. //=============================================================================
  1286. // W3DRoadBuffer::checkLinkAfter
  1287. //=============================================================================
  1288. /** Checks to see if any segments need to link after this one. */
  1289. //=============================================================================
  1290. void W3DRoadBuffer::checkLinkAfter(Int ndx)
  1291. {
  1292. // Check for things whose loc == our loc1.
  1293. if (m_roads[ndx].m_pt1.count != 1) {
  1294. return;
  1295. }
  1296. if (ndx >= m_numRoads-1) {
  1297. return;
  1298. }
  1299. Vector2 loc1 = m_roads[ndx].m_pt1.loc;
  1300. #ifdef _DEBUG
  1301. DEBUG_ASSERTLOG(m_roads[ndx].m_pt2.loc == m_roads[ndx-1].m_pt1.loc, ("Bad link\n"));
  1302. #endif
  1303. Int checkNdx = ndx+1;
  1304. while (checkNdx < m_numRoads && ndx < m_numRoads-1) {
  1305. if (m_roads[checkNdx].m_pt2.loc == loc1) {
  1306. #ifdef _DEBUG
  1307. DEBUG_ASSERTLOG(m_roads[checkNdx].m_pt2.count==1, ("Bad count\n"));
  1308. #endif
  1309. ndx++;
  1310. moveRoadSegTo(checkNdx, ndx);
  1311. loc1 = m_roads[ndx].m_pt1.loc;
  1312. if (m_roads[ndx].m_pt1.count != 1) return;
  1313. } else if (m_roads[checkNdx].m_pt1.loc == loc1) {
  1314. #ifdef _DEBUG
  1315. DEBUG_ASSERTLOG(m_roads[checkNdx].m_pt1.count==1, ("Wrong m_pt1.count.\n"));
  1316. if ( m_roads[checkNdx].m_pt1.count!=1) {
  1317. ::OutputDebugString("Wrong m_pt1.count.\n");
  1318. }
  1319. #endif
  1320. flipTheRoad(&m_roads[checkNdx]);
  1321. ndx++;
  1322. moveRoadSegTo(checkNdx, ndx);
  1323. loc1 = m_roads[ndx].m_pt1.loc;
  1324. if (m_roads[ndx].m_pt1.count != 1) return;
  1325. } else {
  1326. checkNdx++;
  1327. }
  1328. }
  1329. }
  1330. static Bool warnSegments = true;
  1331. #define CHECK_SEGMENTS {if (m_numRoads >= m_maxRoadSegments) { if (warnSegments) DEBUG_LOG(("****** Too many road segments. Need to increase ini values. See john a.\n")); warnSegments = false; return;}}
  1332. //=============================================================================
  1333. // W3DRoadBuffer::addMapObject
  1334. //=============================================================================
  1335. /** Loads the roads from the map objects. */
  1336. //=============================================================================
  1337. void W3DRoadBuffer::addMapObject(RoadSegment *pRoad, Bool updateTheCounts)
  1338. {
  1339. RoadSegment cur = *pRoad;
  1340. Vector2 loc1, loc2;
  1341. loc1 = cur.m_pt1.loc;
  1342. loc2 = cur.m_pt2.loc;
  1343. Vector2 roadVector(loc2.X-loc1.X, loc2.Y-loc1.Y);
  1344. Vector2 roadNormal(-roadVector.Y, roadVector.X);
  1345. roadNormal.Normalize();
  1346. roadNormal *= (cur.m_scale*cur.m_widthInTexture/2.0f);
  1347. cur.m_pt1.top = loc1+roadNormal;
  1348. cur.m_pt1.bottom = loc1 - roadNormal;
  1349. cur.m_pt2.top = loc2+roadNormal;
  1350. cur.m_pt2.bottom = loc2 - roadNormal;
  1351. if (updateTheCounts) {
  1352. updateCounts(&cur);
  1353. }
  1354. CHECK_SEGMENTS;
  1355. Int i;
  1356. Bool flip = false;
  1357. Bool addBefore = false;
  1358. Bool addAfter = false;
  1359. Bool bothMatch = false;
  1360. for (i=0; i<m_numRoads; i++) {
  1361. bothMatch = false;
  1362. if ( (m_roads[i].m_pt1.loc==loc1 && m_roads[i].m_pt2.loc==loc2) ||
  1363. (m_roads[i].m_pt1.loc==loc2 && m_roads[i].m_pt2.loc==loc1) ) {
  1364. bothMatch = true; // identical segment, so discard.
  1365. break;
  1366. }
  1367. if (cur.m_pt1.count==1) {
  1368. if ((m_roads[i].m_pt1.loc == loc1)) {
  1369. flip = true;
  1370. addAfter = true;
  1371. }
  1372. if ((m_roads[i].m_pt2.loc ==loc1)) {
  1373. flip = false;
  1374. addBefore = true;
  1375. }
  1376. }
  1377. if (cur.m_pt2.count==1) {
  1378. if ((m_roads[i].m_pt1.loc == loc2)) {
  1379. flip = false;
  1380. addAfter = true;
  1381. }
  1382. if ((m_roads[i].m_pt2.loc == loc2)) {
  1383. flip = true;
  1384. addBefore = true;
  1385. }
  1386. }
  1387. if (addBefore || addAfter) {
  1388. break;
  1389. }
  1390. }
  1391. if (bothMatch) {
  1392. return;
  1393. }
  1394. Int addIndex = i;
  1395. if (addAfter) {
  1396. addIndex++;
  1397. }
  1398. if (addIndex < m_numRoads) {
  1399. for (i=m_numRoads; i>addIndex; i--) {
  1400. m_roads[i] = m_roads[i-1];
  1401. }
  1402. }
  1403. m_roads[addIndex] = cur;
  1404. if (flip) {
  1405. flipTheRoad(&m_roads[addIndex]);
  1406. }
  1407. m_numRoads++;
  1408. if (addBefore) {
  1409. checkLinkBefore(addIndex);
  1410. } else if (addAfter) {
  1411. checkLinkAfter(addIndex);
  1412. }
  1413. }
  1414. //=============================================================================
  1415. // W3DRoadBuffer::addMapObjects
  1416. //=============================================================================
  1417. /** Loads the roads from the map objects. */
  1418. //=============================================================================
  1419. void W3DRoadBuffer::addMapObjects()
  1420. {
  1421. MapObject *pMapObj;
  1422. MapObject *pMapObj2;
  1423. for (pMapObj = MapObject::getFirstMapObject(); pMapObj; pMapObj = pMapObj->getNext()) {
  1424. if (m_numRoads >= m_maxRoadSegments) {
  1425. break;
  1426. }
  1427. if (pMapObj->getFlag(FLAG_ROAD_POINT1)) {
  1428. pMapObj2 = pMapObj->getNext();
  1429. #ifdef _DEBUG
  1430. DEBUG_ASSERTLOG(pMapObj2 && pMapObj2->getFlag(FLAG_ROAD_POINT2), ("Bad Flag\n"));
  1431. #endif
  1432. if (pMapObj2==NULL) break;
  1433. if (!pMapObj2->getFlag(FLAG_ROAD_POINT2)) continue;
  1434. Vector2 loc1, loc2;
  1435. loc1.Set(pMapObj->getLocation()->x, pMapObj->getLocation()->y);
  1436. loc2.Set(pMapObj2->getLocation()->x, pMapObj2->getLocation()->y);
  1437. if (loc1.X==loc2.X && loc1.Y==loc2.Y) {
  1438. loc2.X += 0.25;
  1439. }
  1440. RoadSegment curRoad;
  1441. curRoad.m_scale = DEFAULT_ROAD_SCALE;
  1442. curRoad.m_widthInTexture = 1.0f;
  1443. curRoad.m_uniqueID = 1;
  1444. Bool found = false;
  1445. TerrainRoadType *road = TheTerrainRoads->findRoad( pMapObj->getName() );
  1446. if( road )
  1447. {
  1448. curRoad.m_widthInTexture = road->getRoadWidthInTexture();
  1449. curRoad.m_scale = road->getRoadWidth();
  1450. curRoad.m_uniqueID = road->getID();
  1451. found = TRUE;
  1452. } // end if
  1453. #ifdef LOAD_TEST_ASSETS
  1454. const Real DEFAULT_SCALE = 30;
  1455. if (!found) {
  1456. Int i;
  1457. for (i=0; i<m_maxRoadTypes; i++) {
  1458. if (pMapObj->getName() == m_roadTypes[i].getPath()) {
  1459. curRoad.m_scale = DEFAULT_SCALE;
  1460. curRoad.m_uniqueID = m_roadTypes[i].getUniqueID();
  1461. found = true;
  1462. }
  1463. }
  1464. }
  1465. if (!found && m_curOpenRoad<m_maxRoadTypes) {
  1466. m_maxUID++;
  1467. curRoad.m_scale = DEFAULT_SCALE;
  1468. curRoad.m_uniqueID = m_maxUID;
  1469. m_roadTypes[m_curOpenRoad].loadTexture(pMapObj->getName(), m_maxUID);
  1470. m_curOpenRoad++;
  1471. }
  1472. #endif
  1473. curRoad.m_pt1.loc = loc1;
  1474. curRoad.m_pt1.isAngled = pMapObj->getFlag(FLAG_ROAD_CORNER_ANGLED);
  1475. curRoad.m_pt1.isJoin = pMapObj->getFlag(FLAG_ROAD_JOIN);
  1476. curRoad.m_pt2.loc =loc2;
  1477. curRoad.m_pt2.isAngled = pMapObj2->getFlag(FLAG_ROAD_CORNER_ANGLED);
  1478. curRoad.m_pt2.isJoin = pMapObj2->getFlag(FLAG_ROAD_JOIN);
  1479. curRoad.m_type = SEGMENT;
  1480. curRoad.m_curveRadius = pMapObj->getFlag(FLAG_ROAD_CORNER_TIGHT)?TIGHT_CORNER_RADIUS:CORNER_RADIUS;
  1481. addMapObject(&curRoad, true);
  1482. pMapObj = pMapObj2;
  1483. }
  1484. }
  1485. Int curCount = m_numRoads;
  1486. Int i;
  1487. m_numRoads = 0;
  1488. for (i=0; i<curCount; i++) {
  1489. RoadSegment curRoad = m_roads[i];
  1490. addMapObject(&curRoad, false);
  1491. }
  1492. }
  1493. //=============================================================================
  1494. // W3DRoadBuffer::updateCounts
  1495. //=============================================================================
  1496. /** Updates the count and last fields. */
  1497. //=============================================================================
  1498. void W3DRoadBuffer::updateCounts(RoadSegment *pRoad)
  1499. {
  1500. pRoad->m_pt1.last = true;
  1501. pRoad->m_pt2.last = true;
  1502. pRoad->m_pt1.multi = false;
  1503. pRoad->m_pt2.multi = false;
  1504. pRoad->m_pt1.count = 0;
  1505. pRoad->m_pt2.count = 0;
  1506. Vector2 loc1, loc2;
  1507. loc1 = pRoad->m_pt1.loc;
  1508. loc2 = pRoad->m_pt2.loc;
  1509. Int i;
  1510. for (i=0; i<m_numRoads; i++) {
  1511. // Only connect same type of road.
  1512. if (m_roads[i].m_uniqueID != pRoad->m_uniqueID) {
  1513. continue;
  1514. }
  1515. if ((m_roads[i].m_pt1.loc == loc1)) {
  1516. m_roads[i].m_pt1.count++;
  1517. pRoad->m_pt1.count++;
  1518. }
  1519. if ((m_roads[i].m_pt1.loc == loc2)) {
  1520. m_roads[i].m_pt1.count++;
  1521. pRoad->m_pt2.count++;
  1522. }
  1523. if ((m_roads[i].m_pt2.loc ==loc1)) {
  1524. m_roads[i].m_pt2.count++;
  1525. pRoad->m_pt1.count++;
  1526. }
  1527. if ((m_roads[i].m_pt2.loc == loc2)) {
  1528. m_roads[i].m_pt2.count++;
  1529. pRoad->m_pt2.count++;
  1530. }
  1531. m_roads[i].m_pt1.multi = m_roads[i].m_pt1.count > 1;
  1532. m_roads[i].m_pt2.multi = m_roads[i].m_pt2.count > 1;
  1533. }
  1534. pRoad->m_pt1.multi = pRoad->m_pt1.count > 1;
  1535. pRoad->m_pt2.multi = pRoad->m_pt2.count > 1;
  1536. }
  1537. //=============================================================================
  1538. // W3DRoadBuffer::updateCountsAndFlags
  1539. //=============================================================================
  1540. /** Updates the count and last fields. */
  1541. //=============================================================================
  1542. void W3DRoadBuffer::updateCountsAndFlags()
  1543. {
  1544. Int i, j;
  1545. for (i=0; i<m_numRoads; i++) {
  1546. m_roads[i].m_pt1.last = true;
  1547. m_roads[i].m_pt2.last = true;
  1548. m_roads[i].m_pt1.count = 0;
  1549. m_roads[i].m_pt2.count = 0;
  1550. }
  1551. for (j=m_numRoads-1; j>0; j--) {
  1552. Vector2 loc1, loc2;
  1553. loc1 = m_roads[j].m_pt1.loc;
  1554. loc2 = m_roads[j].m_pt2.loc;
  1555. for (i=0; i<j; i++) {
  1556. if (m_roads[i].m_uniqueID != m_roads[j].m_uniqueID) {
  1557. continue;
  1558. }
  1559. if ((m_roads[i].m_pt1.loc == loc1)) {
  1560. m_roads[i].m_pt1.last = false;
  1561. m_roads[i].m_pt1.count++;
  1562. m_roads[j].m_pt1.count++;
  1563. }
  1564. if ((m_roads[i].m_pt1.loc == loc2)) {
  1565. m_roads[i].m_pt1.last = false;
  1566. m_roads[i].m_pt1.count++;
  1567. m_roads[j].m_pt2.count++;
  1568. }
  1569. if ((m_roads[i].m_pt2.loc ==loc1)) {
  1570. m_roads[i].m_pt2.last = false;
  1571. m_roads[i].m_pt2.count++;
  1572. m_roads[j].m_pt1.count++;
  1573. }
  1574. if ((m_roads[i].m_pt2.loc == loc2)) {
  1575. m_roads[i].m_pt2.last = false;
  1576. m_roads[i].m_pt2.count++;
  1577. m_roads[j].m_pt2.count++;
  1578. }
  1579. }
  1580. }
  1581. }
  1582. //=============================================================================
  1583. // W3DRoadBuffer::insertTee
  1584. //=============================================================================
  1585. /** Inserts a Tee intersection. */
  1586. //=============================================================================
  1587. void W3DRoadBuffer::insertTee(Vector2 loc, Int index1, Real scale)
  1588. {
  1589. if (insertY(loc,index1, scale)) {
  1590. return;
  1591. }
  1592. // pr1-3 point to the points on the segments that form the tee.
  1593. // They are the points on the segments that are != loc.
  1594. TRoadPt *pr1=NULL;
  1595. TRoadPt *pr2=NULL;
  1596. TRoadPt *pr3=NULL;
  1597. // pc1-3 point to the center points of the segments. These are the
  1598. // points that are at loc.
  1599. TRoadPt *pc1=NULL;
  1600. TRoadPt *pc2=NULL;
  1601. TRoadPt *pc3=NULL;
  1602. if (m_roads[index1].m_pt1.loc == loc) {
  1603. pr1 = &m_roads[index1].m_pt2;
  1604. pc1 = &m_roads[index1].m_pt1;
  1605. } else {
  1606. pr1 = &m_roads[index1].m_pt1;
  1607. pc1 = &m_roads[index1].m_pt2;
  1608. }
  1609. Int i;
  1610. Int index2=0;
  1611. Int index3=0;
  1612. for (i = index1+1; i<m_numRoads; i++) {
  1613. if (m_roads[i].m_pt1.loc == loc) {
  1614. m_roads[i].m_pt1.count = -2;
  1615. if (pr2==NULL) {
  1616. pr2 = &m_roads[i].m_pt2;
  1617. pc2 = &m_roads[i].m_pt1;
  1618. index2 = i;
  1619. } else {
  1620. pr3 = &m_roads[i].m_pt2;
  1621. pc3 = &m_roads[i].m_pt1;
  1622. index3 = i;
  1623. }
  1624. }
  1625. if (m_roads[i].m_pt2.loc == loc) {
  1626. m_roads[i].m_pt2.count = -2;
  1627. if (pr2==NULL) {
  1628. pr2 = &m_roads[i].m_pt1;
  1629. pc2 = &m_roads[i].m_pt2;
  1630. index2 = i;
  1631. } else {
  1632. pr3 = &m_roads[i].m_pt1;
  1633. pc3 = &m_roads[i].m_pt2;
  1634. index3 = i;
  1635. }
  1636. }
  1637. }
  1638. if (pr2 == NULL || pr3 == NULL) {
  1639. return;
  1640. }
  1641. Vector2 v1 = pr1->loc - loc;
  1642. v1.Normalize();
  1643. Vector2 v2 = pr2->loc - loc;
  1644. v2.Normalize();
  1645. Vector2 v3 = pr3->loc - loc;
  1646. v3.Normalize();
  1647. Real dot12 = v1.Dot_Product(v1, v2);
  1648. Real dot13 = v1.Dot_Product(v1, v3);
  1649. Real dot32 = v1.Dot_Product(v3, v2);
  1650. // The greatest negative dot product is the pair that is heading most opposite each other.
  1651. Bool do12 = false;
  1652. Bool do13 = false;
  1653. Bool do32 = false;
  1654. if (dot12<dot13) {
  1655. if (dot12<dot32) {
  1656. do12 = true;
  1657. } else {
  1658. do32 = true;
  1659. }
  1660. } else {
  1661. if (dot13<dot32) {
  1662. do13 = true;
  1663. } else {
  1664. do32 = true;
  1665. }
  1666. }
  1667. Vector2 upVector;
  1668. Vector2 decider;
  1669. if (do12) {
  1670. upVector = v2-v1;
  1671. decider = v3;
  1672. }
  1673. if (do13) {
  1674. upVector = v3-v1;
  1675. decider = v2;
  1676. }
  1677. if (do32) {
  1678. upVector = v2-v3;
  1679. decider = v1;
  1680. }
  1681. upVector.Normalize();
  1682. // Check to see if the Tee side is slanted.
  1683. const Real cos60 = 0.5f;
  1684. Real dot = fabs(Vector2::Dot_Product(upVector, decider));
  1685. if (dot > cos60) {
  1686. // The arm of the tee is slanted, so do a slant tee.
  1687. Real angle = (PI/2); // 90 degrees.
  1688. Real xpdct = Vector3::Cross_Product_Z(Vector3(upVector.X,upVector.Y,0), Vector3(decider.X, decider.Y,0));
  1689. Bool mirror = false;
  1690. if (xpdct<0) {
  1691. angle = -angle;
  1692. mirror = true;
  1693. }
  1694. upVector.Normalize();
  1695. upVector *= 0.5*scale; // we are offseting one half road width.
  1696. Vector2 teeVector(upVector);
  1697. teeVector.Rotate(angle);
  1698. Bool flip;
  1699. if (do12) {
  1700. flip = xpSign(teeVector, v3) == 1;
  1701. offsetH(pc1, pc2, pc3, loc, upVector, teeVector, flip, mirror, m_roads[index1].m_widthInTexture);
  1702. }
  1703. if (do13) {
  1704. flip = xpSign(teeVector, v2) == 1;
  1705. offsetH(pc1, pc3, pc2, loc, upVector, teeVector, flip, mirror, m_roads[index1].m_widthInTexture);
  1706. }
  1707. if (do32) {
  1708. flip = xpSign(teeVector, v1) == 1;
  1709. offsetH(pc3, pc2, pc1, loc, upVector, teeVector, flip, mirror, m_roads[index1].m_widthInTexture);
  1710. }
  1711. pc1->last = true;
  1712. pc1->count = 0;
  1713. pc2->last = true;
  1714. pc2->count = 0;
  1715. pc3->last = true;
  1716. pc3->count = 0;
  1717. CHECK_SEGMENTS;
  1718. m_roads[m_numRoads].m_pt1.loc.Set(loc);
  1719. m_roads[m_numRoads].m_pt2.loc.Set(loc+teeVector);
  1720. m_roads[m_numRoads].m_pt1.last = true; // if not, that one will clear flag in prior loop.
  1721. m_roads[m_numRoads].m_pt2.last = true; // if not, that one will clear flag in prior loop.
  1722. m_roads[m_numRoads].m_scale = m_roads[index1].m_scale;
  1723. m_roads[m_numRoads].m_widthInTexture = m_roads[index1].m_widthInTexture;
  1724. m_roads[m_numRoads].m_pt1.count = -3;
  1725. m_roads[m_numRoads].m_type = flip?THREE_WAY_H_FLIP:THREE_WAY_H;
  1726. m_roads[m_numRoads].m_uniqueID = m_roads[index1].m_uniqueID;
  1727. m_numRoads++;
  1728. } else {
  1729. // Do a not slanted tee.
  1730. Real angle = (PI/2); // 90 degrees.
  1731. Real xpdct = Vector3::Cross_Product_Z(Vector3(upVector.X,upVector.Y,0), Vector3(decider.X, decider.Y,0));
  1732. if (xpdct<0) angle = -angle;
  1733. upVector.Normalize();
  1734. upVector *= 0.5*scale; // we are offseting one half road width.
  1735. Vector2 teeVector(upVector);
  1736. teeVector.Rotate(angle);
  1737. if (do12) {
  1738. offset3Way(pc1, pc2, pc3, loc, upVector, teeVector, m_roads[index1].m_widthInTexture);
  1739. }
  1740. if (do13) {
  1741. offset3Way(pc1, pc3, pc2, loc, upVector, teeVector, m_roads[index1].m_widthInTexture);
  1742. }
  1743. if (do32) {
  1744. offset3Way(pc3, pc2, pc1, loc, upVector, teeVector, m_roads[index1].m_widthInTexture);
  1745. }
  1746. pc1->last = true;
  1747. pc1->count = 0;
  1748. pc2->last = true;
  1749. pc2->count = 0;
  1750. pc3->last = true;
  1751. pc3->count = 0;
  1752. CHECK_SEGMENTS;
  1753. m_roads[m_numRoads].m_pt1.loc.Set(loc);
  1754. m_roads[m_numRoads].m_pt2.loc.Set(loc+teeVector);
  1755. m_roads[m_numRoads].m_pt1.last = true; // if not, that one will clear flag in prior loop.
  1756. m_roads[m_numRoads].m_pt2.last = true; // if not, that one will clear flag in prior loop.
  1757. m_roads[m_numRoads].m_scale = m_roads[index1].m_scale;
  1758. m_roads[m_numRoads].m_widthInTexture = m_roads[index1].m_widthInTexture;
  1759. m_roads[m_numRoads].m_pt1.count = -3;
  1760. m_roads[m_numRoads].m_type = TEE;
  1761. m_roads[m_numRoads].m_uniqueID = m_roads[index1].m_uniqueID;
  1762. m_numRoads++;
  1763. }
  1764. }
  1765. //=============================================================================
  1766. // W3DRoadBuffer::insertY
  1767. //=============================================================================
  1768. /** Inserts a Y intersection if the corner meets "Y" criteria. */
  1769. //=============================================================================
  1770. Bool W3DRoadBuffer::insertY(Vector2 loc, Int index1, Real scale)
  1771. {
  1772. // pr1-3 point to the points on the segments that form the tee.
  1773. // They are the points on the segments that are != loc.
  1774. TRoadPt *pr1=NULL;
  1775. TRoadPt *pr2=NULL;
  1776. TRoadPt *pr3=NULL;
  1777. // pc1-3 point to the center points of the segments. These are the
  1778. // points that are at loc.
  1779. TRoadPt *pc1=NULL;
  1780. TRoadPt *pc2=NULL;
  1781. TRoadPt *pc3=NULL;
  1782. if (m_roads[index1].m_pt1.loc == loc) {
  1783. pr1 = &m_roads[index1].m_pt2;
  1784. pc1 = &m_roads[index1].m_pt1;
  1785. } else {
  1786. pr1 = &m_roads[index1].m_pt1;
  1787. pc1 = &m_roads[index1].m_pt2;
  1788. }
  1789. Int i;
  1790. Int index2=0;
  1791. Int index3=0;
  1792. for (i = index1+1; i<m_numRoads; i++) {
  1793. if (m_roads[i].m_pt1.loc == loc) {
  1794. m_roads[i].m_pt1.count = -2;
  1795. if (pr2==NULL) {
  1796. pr2 = &m_roads[i].m_pt2;
  1797. pc2 = &m_roads[i].m_pt1;
  1798. index2 = i;
  1799. } else {
  1800. pr3 = &m_roads[i].m_pt2;
  1801. pc3 = &m_roads[i].m_pt1;
  1802. index3 = i;
  1803. }
  1804. }
  1805. if (m_roads[i].m_pt2.loc == loc) {
  1806. m_roads[i].m_pt2.count = -2;
  1807. if (pr2==NULL) {
  1808. pr2 = &m_roads[i].m_pt1;
  1809. pc2 = &m_roads[i].m_pt2;
  1810. index2 = i;
  1811. } else {
  1812. pr3 = &m_roads[i].m_pt1;
  1813. pc3 = &m_roads[i].m_pt2;
  1814. index3 = i;
  1815. }
  1816. }
  1817. }
  1818. if (pr2 == NULL || pr3 == NULL) {
  1819. return false;
  1820. }
  1821. Vector2 v1 = pr1->loc - loc;
  1822. v1.Normalize();
  1823. Vector2 v2 = pr2->loc - loc;
  1824. v2.Normalize();
  1825. Vector2 v3 = pr3->loc - loc;
  1826. v3.Normalize();
  1827. Bool do12 = false;
  1828. Bool do13 = false;
  1829. Bool do32 = false;
  1830. Real dot12 = v1.Dot_Product(v1, v2);
  1831. Real dot13 = v1.Dot_Product(v1, v3);
  1832. Real dot32 = v1.Dot_Product(v3, v2);
  1833. Real score12 = 2.0f;
  1834. Real score13 = 2.0f;
  1835. Real score32 = 2.0f;
  1836. // const Real cos60 = 0.5f;
  1837. const Real cos30 = 0.866f;
  1838. const Real cos45 = 0.707f;
  1839. if (dot12 < (-cos30)) return false; // Too close to a straigh line, do a straight side tee.
  1840. if (dot13 < (-cos30)) return false; // Too close to a straigh line, do a straight side tee.
  1841. if (dot32 < (-cos30)) return false; // Too close to a straigh line, to a straight side tee.
  1842. Int s1 = 0;
  1843. Int s2 = xpSign(v1, v2);
  1844. Int s3 = xpSign(v1, v3);
  1845. if (s2!=s3 && (s2+s3==0)) {
  1846. Vector2 v1_90(-v1.Y, v1.X);
  1847. if (xpSign(v1_90, v2) == 1 && xpSign(v1_90, v3) == 1) {
  1848. // s2 & s3 could be Y legs.
  1849. do32 = true;
  1850. score32 = fabs(dot12+cos45) + fabs(dot13+cos45);
  1851. }
  1852. }
  1853. s1 = xpSign(v3, v1);
  1854. s2 = xpSign(v3, v2);
  1855. if (s2!=s1 && (s2+s1==0)) {
  1856. // s2 & s3 could be Y legs.
  1857. Vector2 v3_90(-v3.Y, v3.X);
  1858. if (xpSign(v3_90, v2) == 1 && xpSign(v3_90, v1) == 1) {
  1859. do12 = true;
  1860. score12 = fabs(dot13+cos45) + fabs(dot32+cos45);
  1861. }
  1862. }
  1863. s1 = xpSign(v2, v1);
  1864. s3 = xpSign(v2, v3);
  1865. if (s3!=s1 && (s3+s1==0)) {
  1866. // s2 & s3 could be Y legs.
  1867. Vector2 v2_90(-v2.Y, v2.X);
  1868. if (xpSign(v2_90, v3) == 1 && xpSign(v2_90, v1) == 1) {
  1869. do13 = true;
  1870. score13 = fabs(dot12+cos45) + fabs(dot32+cos45);
  1871. }
  1872. }
  1873. if (score12<score13) {
  1874. do13 = false;
  1875. if (score12<score32) {
  1876. do32 = false;
  1877. } else {
  1878. do12 = false;
  1879. }
  1880. } else {
  1881. do12 = false;
  1882. if (score13<score32) {
  1883. do32 = false;
  1884. } else {
  1885. do13 = false;
  1886. }
  1887. }
  1888. Vector2 upVector;
  1889. if (do12) {
  1890. upVector = v3;
  1891. } else if (do13) {
  1892. upVector = v2;
  1893. } else if (do32) {
  1894. upVector = v1;
  1895. } else {
  1896. return false;
  1897. }
  1898. Real angle = -(PI/2); // -90 degrees.
  1899. upVector.Normalize();
  1900. upVector *= 0.5*scale; // we are offseting one half road width.
  1901. Vector2 teeVector(upVector);
  1902. teeVector.Rotate(angle);
  1903. // Offset the ends of the road to meet the stumps of the y.
  1904. if (do12) {
  1905. if (xpSign(v3, v1) == -1) {
  1906. offsetY(pc1, pc2, pc3, loc, upVector, m_roads[index1].m_widthInTexture);
  1907. } else {
  1908. offsetY(pc2, pc1, pc3, loc, upVector, m_roads[index1].m_widthInTexture);
  1909. }
  1910. }
  1911. if (do13) {
  1912. if (xpSign(v2, v1) == -1) {
  1913. offsetY(pc1, pc3, pc2, loc, upVector, m_roads[index1].m_widthInTexture);
  1914. } else {
  1915. offsetY(pc3, pc1, pc2, loc, upVector, m_roads[index1].m_widthInTexture);
  1916. }
  1917. }
  1918. if (do32) {
  1919. if (xpSign(v1, v3) == -1) {
  1920. offsetY(pc3, pc2, pc1, loc, upVector, m_roads[index1].m_widthInTexture);
  1921. } else {
  1922. offsetY(pc2, pc3, pc1, loc, upVector, m_roads[index1].m_widthInTexture);
  1923. }
  1924. }
  1925. pc1->last = true;
  1926. pc1->count = 0;
  1927. pc2->last = true;
  1928. pc2->count = 0;
  1929. pc3->last = true;
  1930. pc3->count = 0;
  1931. if (m_numRoads >= m_maxRoadSegments) return false;
  1932. m_roads[m_numRoads].m_pt1.loc.Set(loc);
  1933. m_roads[m_numRoads].m_pt2.loc.Set(loc+teeVector);
  1934. m_roads[m_numRoads].m_pt1.last = true; // if not, that one will clear flag in prior loop.
  1935. m_roads[m_numRoads].m_pt2.last = true; // if not, that one will clear flag in prior loop.
  1936. m_roads[m_numRoads].m_scale = m_roads[index1].m_scale;
  1937. m_roads[m_numRoads].m_widthInTexture = m_roads[index1].m_widthInTexture;
  1938. m_roads[m_numRoads].m_pt1.count = -3;
  1939. m_roads[m_numRoads].m_type = THREE_WAY_Y;
  1940. m_roads[m_numRoads].m_uniqueID = m_roads[index1].m_uniqueID;
  1941. m_numRoads++;
  1942. return true;
  1943. }
  1944. //=============================================================================
  1945. // W3DRoadBuffer::offset3Way
  1946. //=============================================================================
  1947. /** Offsets the points coming into a 3 way intersection so that they move to
  1948. the join points of the 3 way intersection. */
  1949. //=============================================================================
  1950. void W3DRoadBuffer::offset3Way(TRoadPt *pc1, TRoadPt *pc2, TRoadPt *pc3, Vector2 loc, Vector2 upVector,
  1951. Vector2 teeVector, Real widthInTexture)
  1952. {
  1953. pc1->loc = loc - upVector;
  1954. pc2->loc = loc + upVector;
  1955. pc3->loc = loc + teeVector;
  1956. // Adjust the top & bottoms, so they merge smoothly into the tee.
  1957. // Make sure the t vector goes right with respect to the up vector.
  1958. Real xpdct = Vector3::Cross_Product_Z(Vector3(upVector.X,upVector.Y,0), Vector3(teeVector.X, teeVector.Y,0));
  1959. Vector2 rightTee = teeVector;
  1960. if (xpdct<0) {
  1961. rightTee.X = -teeVector.X;
  1962. rightTee.Y = -teeVector.Y;
  1963. }
  1964. rightTee *= (widthInTexture);
  1965. xpdct = Vector3::Cross_Product_Z(Vector3(upVector.X, upVector.Y, 0), Vector3(pc1->top.X-pc1->loc.X, pc1->top.Y-pc1->loc.Y,0));
  1966. if (xpdct>0) {
  1967. pc1->bottom = pc1->loc - rightTee;
  1968. pc1->top = pc1->loc + rightTee;
  1969. } else {
  1970. pc1->bottom = pc1->loc + rightTee;
  1971. pc1->top = pc1->loc - rightTee;
  1972. }
  1973. xpdct = Vector3::Cross_Product_Z(Vector3(upVector.X, upVector.Y, 0), Vector3(pc2->top.X-pc2->loc.X, pc2->top.Y-pc2->loc.Y,0));
  1974. if (xpdct>0) {
  1975. pc2->bottom = pc2->loc - rightTee;
  1976. pc2->top = pc2->loc + rightTee;
  1977. } else {
  1978. pc2->bottom = pc2->loc + rightTee;
  1979. pc2->top = pc2->loc - rightTee;
  1980. }
  1981. upVector *= (widthInTexture);
  1982. xpdct = Vector3::Cross_Product_Z(Vector3(rightTee.X, rightTee.Y, 0), Vector3(pc3->top.X-pc3->loc.X, pc3->top.Y-pc3->loc.Y,0));
  1983. if (xpdct<0) {
  1984. pc3->bottom = pc3->loc - upVector;
  1985. pc3->top = pc3->loc + upVector;
  1986. } else {
  1987. pc3->bottom = pc3->loc + upVector;
  1988. pc3->top = pc3->loc - upVector;
  1989. }
  1990. }
  1991. //=============================================================================
  1992. // W3DRoadBuffer::offsetH
  1993. //=============================================================================
  1994. /** Offsets the points coming into a 3 way intersection so that they move to
  1995. the join points of the 3 way intersection. */
  1996. //=============================================================================
  1997. void W3DRoadBuffer::offsetH(TRoadPt *pc1, TRoadPt *pc2, TRoadPt *pc3, Vector2 loc,
  1998. Vector2 upVector, Vector2 teeVector,
  1999. Bool flip, Bool mirror, Real widthInTexture)
  2000. {
  2001. if (flip != mirror) {
  2002. pc1->loc = loc - upVector*2.05f;
  2003. pc2->loc = loc + upVector*.46f;
  2004. } else {
  2005. pc1->loc = loc - upVector*.46f;
  2006. pc2->loc = loc + upVector*2.05f;
  2007. }
  2008. //pc3->loc = loc + teeVector;
  2009. // Adjust the top & bottoms, so they merge smoothly into the tee.
  2010. // Make sure the t vector goes right with respect to the up vector.
  2011. Real xpdct = Vector3::Cross_Product_Z(Vector3(upVector.X,upVector.Y,0), Vector3(teeVector.X, teeVector.Y,0));
  2012. Vector2 rightTee = teeVector;
  2013. if (xpdct<0) {
  2014. rightTee.X = -teeVector.X;
  2015. rightTee.Y = -teeVector.Y;
  2016. }
  2017. rightTee *= (widthInTexture);
  2018. xpdct = Vector3::Cross_Product_Z(Vector3(upVector.X, upVector.Y, 0), Vector3(pc1->top.X-pc1->loc.X, pc1->top.Y-pc1->loc.Y,0));
  2019. if (xpdct>0) {
  2020. pc1->bottom = pc1->loc - rightTee;
  2021. pc1->top = pc1->loc + rightTee;
  2022. } else {
  2023. pc1->bottom = pc1->loc + rightTee;
  2024. pc1->top = pc1->loc - rightTee;
  2025. }
  2026. xpdct = Vector3::Cross_Product_Z(Vector3(upVector.X, upVector.Y, 0), Vector3(pc2->top.X-pc2->loc.X, pc2->top.Y-pc2->loc.Y,0));
  2027. if (xpdct>0) {
  2028. pc2->bottom = pc2->loc - rightTee;
  2029. pc2->top = pc2->loc + rightTee;
  2030. } else {
  2031. pc2->bottom = pc2->loc + rightTee;
  2032. pc2->top = pc2->loc - rightTee;
  2033. }
  2034. Vector2 arm = teeVector;
  2035. if (flip) {
  2036. arm.Rotate(PI/4);
  2037. } else {
  2038. arm.Rotate(-PI/4);
  2039. }
  2040. Vector2 armNormal(-arm.Y, arm.X);
  2041. armNormal *= widthInTexture;
  2042. pc3->loc += arm*2.10f;
  2043. xpdct = xpSign(arm, pc3->top-loc);
  2044. if (xpdct==1) {
  2045. pc3->top = pc3->loc + armNormal;
  2046. pc3->bottom = pc3->loc - armNormal;
  2047. } else {
  2048. pc3->top = pc3->loc - armNormal;
  2049. pc3->bottom = pc3->loc + armNormal;
  2050. }
  2051. }
  2052. //=============================================================================
  2053. // W3DRoadBuffer::offsetY
  2054. //=============================================================================
  2055. /** Offsets the points coming into a 3 way intersection so that they move to
  2056. the join points of the 3 way intersection. */
  2057. //=============================================================================
  2058. void W3DRoadBuffer::offsetY(TRoadPt *pc1, TRoadPt *pc2, TRoadPt *pc3, Vector2 loc, Vector2 upVector,
  2059. Real widthInTexture)
  2060. {
  2061. pc3->loc += upVector*0.55f;
  2062. pc3->top += upVector*0.55f;
  2063. pc3->bottom += upVector*0.55f;
  2064. // Adjust the bottom, so they merge smoothly into the tee.
  2065. // Make sure the t vector goes right with respect to the up vector.
  2066. Vector2 arm = upVector;
  2067. arm.Rotate(3*PI/4);
  2068. Vector2 armNormal(-arm.Y, arm.X);
  2069. armNormal *= widthInTexture;
  2070. pc2->loc += arm*1.1f;
  2071. Int xpdct = xpSign(arm, pc2->top-loc);
  2072. if (xpdct==1) {
  2073. pc2->top = pc2->loc + armNormal;
  2074. pc2->bottom = pc2->loc - armNormal;
  2075. } else {
  2076. pc2->top = pc2->loc - armNormal;
  2077. pc2->bottom = pc2->loc + armNormal;
  2078. }
  2079. arm = upVector;
  2080. arm.Rotate(-3*PI/4);
  2081. armNormal.Set(-arm.Y, arm.X);
  2082. armNormal *= widthInTexture;
  2083. pc1->loc += arm*1.1f;
  2084. xpdct = xpSign(arm, pc1->top-loc);
  2085. if (xpdct==1) {
  2086. pc1->top = pc1->loc + armNormal;
  2087. pc1->bottom = pc1->loc - armNormal;
  2088. } else {
  2089. pc1->top = pc1->loc - armNormal;
  2090. pc1->bottom = pc1->loc + armNormal;
  2091. }
  2092. }
  2093. //=============================================================================
  2094. // W3DRoadBuffer::offset4Way
  2095. //=============================================================================
  2096. /** Offsets the points coming into a 4 way intersection so that they move to
  2097. the join points of the 4 way intersection. */
  2098. //=============================================================================
  2099. void W3DRoadBuffer::offset4Way(TRoadPt *pc1, TRoadPt *pc2, TRoadPt *pc3, TRoadPt *pr3,
  2100. TRoadPt *pc4, Vector2 loc, Vector2 alignVector, Real widthInTexture)
  2101. {
  2102. pc1->loc = loc - alignVector;
  2103. pc2->loc = loc + alignVector;
  2104. Vector2 v3 = pr3->loc - loc;
  2105. Real angle = (PI/2); // 90 degrees.
  2106. Real xpdct = Vector3::Cross_Product_Z(Vector3(alignVector.X,alignVector.Y,0), Vector3(v3.X, v3.Y,0));
  2107. if (xpdct<0) angle = -angle;
  2108. Vector2 teeVector(alignVector);
  2109. teeVector.Rotate(angle);
  2110. pc3->loc = loc + teeVector;
  2111. pc4->loc = loc - teeVector;
  2112. Vector2 realTee(alignVector);
  2113. realTee.Rotate(PI/2);
  2114. realTee *= widthInTexture;
  2115. // Fix up the top & bottom points.
  2116. Vector3 align3(alignVector.X,alignVector.Y,0);
  2117. xpdct = Vector3::Cross_Product_Z(align3, Vector3(pc1->top.X-pc1->loc.X, pc1->top.Y-pc1->loc.Y,0));
  2118. if (xpdct>0) {
  2119. pc1->bottom = pc1->loc - realTee;
  2120. pc1->top = pc1->loc + realTee;
  2121. } else {
  2122. pc1->bottom = pc1->loc + realTee;
  2123. pc1->top = pc1->loc - realTee;
  2124. }
  2125. xpdct = Vector3::Cross_Product_Z(align3, Vector3(pc2->top.X-pc2->loc.X, pc2->top.Y-pc2->loc.Y,0));
  2126. if (xpdct>0) {
  2127. pc2->bottom = pc2->loc - realTee;
  2128. pc2->top = pc2->loc + realTee;
  2129. } else {
  2130. pc2->bottom = pc2->loc + realTee;
  2131. pc2->top = pc2->loc - realTee;
  2132. }
  2133. alignVector *= widthInTexture;
  2134. Vector3 tee3(realTee.X,realTee.Y,0);
  2135. xpdct = Vector3::Cross_Product_Z(tee3, Vector3(pc3->top.X-pc3->loc.X, pc3->top.Y-pc3->loc.Y,0));
  2136. if (xpdct<0) {
  2137. pc3->bottom = pc3->loc - alignVector;
  2138. pc3->top = pc3->loc + alignVector;
  2139. } else {
  2140. pc3->bottom = pc3->loc + alignVector;
  2141. pc3->top = pc3->loc - alignVector;
  2142. }
  2143. xpdct = Vector3::Cross_Product_Z(tee3, Vector3(pc4->top.X-pc4->loc.X, pc4->top.Y-pc4->loc.Y,0));
  2144. if (xpdct<0) {
  2145. pc4->bottom = pc4->loc - alignVector;
  2146. pc4->top = pc4->loc + alignVector;
  2147. } else {
  2148. pc4->bottom = pc4->loc + alignVector;
  2149. pc4->top = pc4->loc - alignVector;
  2150. }
  2151. pc1->last = true;
  2152. pc1->count = 0;
  2153. pc2->last = true;
  2154. pc2->count = 0;
  2155. pc3->last = true;
  2156. pc3->count = 0;
  2157. pc4->last = true;
  2158. pc4->count = 0;
  2159. }
  2160. //=============================================================================
  2161. // W3DRoadBuffer::insert4Way
  2162. //=============================================================================
  2163. /** Inserts a 4 way intersection. */
  2164. //=============================================================================
  2165. void W3DRoadBuffer::insert4Way(Vector2 loc, Int index1, Real scale)
  2166. {
  2167. // pr1-4 point to the points on the segments that form the tee.
  2168. // They are the points on the segments that are != loc.
  2169. TRoadPt *pr1=NULL;
  2170. TRoadPt *pr2=NULL;
  2171. TRoadPt *pr3=NULL;
  2172. TRoadPt *pr4=NULL;
  2173. // pc1-4 point to the center points of the segments. These are the
  2174. // points that are at loc.
  2175. TRoadPt *pc1=NULL;
  2176. TRoadPt *pc2=NULL;
  2177. TRoadPt *pc3=NULL;
  2178. TRoadPt *pc4=NULL;
  2179. if (m_roads[index1].m_pt1.loc == loc) {
  2180. pr1 = &m_roads[index1].m_pt2;
  2181. pc1 = &m_roads[index1].m_pt1;
  2182. } else {
  2183. pr1 = &m_roads[index1].m_pt1;
  2184. pc1 = &m_roads[index1].m_pt2;
  2185. }
  2186. Int i;
  2187. for (i = index1+1; i<m_numRoads; i++) {
  2188. if (m_roads[i].m_pt1.loc == loc) {
  2189. m_roads[i].m_pt1.count = -2;
  2190. if (pr2==NULL) {
  2191. pr2 = &m_roads[i].m_pt2;
  2192. pc2 = &m_roads[i].m_pt1;
  2193. } else if (pr3==NULL) {
  2194. pr3 = &m_roads[i].m_pt2;
  2195. pc3 = &m_roads[i].m_pt1;
  2196. } else {
  2197. pr4 = &m_roads[i].m_pt2;
  2198. pc4 = &m_roads[i].m_pt1;
  2199. }
  2200. }
  2201. if (m_roads[i].m_pt2.loc == loc) {
  2202. m_roads[i].m_pt2.count = -2;
  2203. if (pr2==NULL) {
  2204. pr2 = &m_roads[i].m_pt1;
  2205. pc2 = &m_roads[i].m_pt2;
  2206. } else if (pr3==NULL) {
  2207. pr3 = &m_roads[i].m_pt1;
  2208. pc3 = &m_roads[i].m_pt2;
  2209. } else {
  2210. pr4 = &m_roads[i].m_pt1;
  2211. pc4 = &m_roads[i].m_pt2;
  2212. }
  2213. }
  2214. }
  2215. if (pr2 == NULL || pr3 == NULL || pr4==NULL) {
  2216. return;
  2217. }
  2218. Vector2 v1 = pr1->loc - loc;
  2219. v1.Normalize();
  2220. Vector2 v2 = pr2->loc - loc;
  2221. v2.Normalize();
  2222. Vector2 v3 = pr3->loc - loc;
  2223. v3.Normalize();
  2224. Vector2 v4 = pr4->loc - loc;
  2225. v4.Normalize();
  2226. Real dot12 = v1.Dot_Product(v1, v2);
  2227. Real dot13 = v1.Dot_Product(v1, v3);
  2228. Real dot14 = v1.Dot_Product(v1, v4);
  2229. Real dot23 = v1.Dot_Product(v2, v3);
  2230. Real dot24 = v1.Dot_Product(v2, v4);
  2231. Real dot34 = v1.Dot_Product(v3, v4);
  2232. // The most negative dot product is the pair that is heading most opposite each other.
  2233. Int curPair = 12;
  2234. Real curDot = dot12;
  2235. if (dot13<curDot) {
  2236. curPair = 13;
  2237. curDot = dot13;
  2238. }
  2239. if (dot14<curDot) {
  2240. curPair = 14;
  2241. curDot = dot14;
  2242. }
  2243. if (dot23<curDot) {
  2244. curPair = 23;
  2245. curDot = dot23;
  2246. }
  2247. if (dot24<curDot) {
  2248. curPair = 24;
  2249. curDot = dot24;
  2250. }
  2251. if (dot34<curDot) {
  2252. curPair = 34;
  2253. curDot = dot34;
  2254. }
  2255. Bool do12 = (curPair==12); // Do segment 1 to 2
  2256. Bool do13 = (curPair==13); // Do segment 1 to 3 etc.
  2257. Bool do14 = (curPair==14);
  2258. Bool do23 = (curPair==23);
  2259. Bool do24 = (curPair==24);
  2260. Bool do34 = (curPair==34);
  2261. Vector2 alignVector;
  2262. if (do12) {
  2263. alignVector = v2-v1;
  2264. }
  2265. if (do13) {
  2266. alignVector = v3-v1;
  2267. }
  2268. if (do14) {
  2269. alignVector = v4-v1;
  2270. }
  2271. if (do23) {
  2272. alignVector = v3-v2;
  2273. }
  2274. if (do24) {
  2275. alignVector = v4-v2;
  2276. }
  2277. if (do34) {
  2278. alignVector = v4-v3;
  2279. }
  2280. alignVector.Normalize();
  2281. alignVector *= 0.5*scale; // we are offseting one half road width.
  2282. if (do12) {
  2283. offset4Way(pc1, pc2, pc3, pr3, pc4, loc, alignVector, m_roads[index1].m_widthInTexture);
  2284. }
  2285. if (do13) {
  2286. offset4Way(pc1, pc3, pc2, pr2, pc4, loc, alignVector, m_roads[index1].m_widthInTexture);
  2287. }
  2288. if (do14) {
  2289. offset4Way(pc1, pc4, pc3, pr3, pc2, loc, alignVector, m_roads[index1].m_widthInTexture);
  2290. }
  2291. if (do23) {
  2292. offset4Way(pc2, pc3, pc1, pr1, pc4, loc, alignVector, m_roads[index1].m_widthInTexture);
  2293. }
  2294. if (do24) {
  2295. offset4Way(pc2, pc4, pc1, pr1, pc3, loc, alignVector, m_roads[index1].m_widthInTexture);
  2296. }
  2297. if (do34) {
  2298. offset4Way(pc3, pc4, pc1, pr1, pc2, loc, alignVector, m_roads[index1].m_widthInTexture);
  2299. }
  2300. if (alignVector.X<0) {
  2301. // Since the 4 way intersection is symmetrical, make it go right (pos x)
  2302. alignVector.X = -alignVector.X;
  2303. alignVector.Y = -alignVector.Y;
  2304. }
  2305. CHECK_SEGMENTS;
  2306. m_roads[m_numRoads].m_pt1.loc.Set(loc);
  2307. m_roads[m_numRoads].m_pt2.loc.Set(loc+alignVector);
  2308. m_roads[m_numRoads].m_pt1.last = true; // if not, that one will clear flag in prior loop.
  2309. m_roads[m_numRoads].m_pt2.last = true; // if not, that one will clear flag in prior loop.
  2310. m_roads[m_numRoads].m_scale = m_roads[index1].m_scale;
  2311. m_roads[m_numRoads].m_widthInTexture = TEE_WIDTH_ADJUSTMENT; // It is symmetrical.
  2312. m_roads[m_numRoads].m_pt1.count = -4;
  2313. m_roads[m_numRoads].m_type = FOUR_WAY;
  2314. m_roads[m_numRoads].m_uniqueID = m_roads[index1].m_uniqueID;
  2315. m_numRoads++;
  2316. }
  2317. //=============================================================================
  2318. // W3DRoadBuffer::insertTeeIntersections
  2319. //=============================================================================
  2320. /** Inserts Tee intersections at 3 way intersections. */
  2321. //=============================================================================
  2322. void W3DRoadBuffer::insertTeeIntersections(void)
  2323. {
  2324. // Insert the tees.
  2325. Int numRoadSegments = m_numRoads;
  2326. Int i;
  2327. // Int segmentStartIndex = -1;
  2328. for (i=0; i<numRoadSegments; i++) {
  2329. if (m_roads[i].m_type != SEGMENT) {
  2330. continue;
  2331. }
  2332. if (m_roads[i].m_pt1.count==2) {
  2333. insertTee(m_roads[i].m_pt1.loc, i, m_roads[i].m_scale);
  2334. }
  2335. if (m_roads[i].m_pt2.count==2) {
  2336. insertTee(m_roads[i].m_pt2.loc, i, m_roads[i].m_scale);
  2337. }
  2338. if (m_roads[i].m_pt1.count==3) {
  2339. insert4Way(m_roads[i].m_pt1.loc, i, m_roads[i].m_scale);
  2340. }
  2341. if (m_roads[i].m_pt2.count==3) {
  2342. insert4Way(m_roads[i].m_pt2.loc, i, m_roads[i].m_scale);
  2343. }
  2344. }
  2345. }
  2346. //=============================================================================
  2347. // W3DRoadBuffer::insertCurveSegments
  2348. //=============================================================================
  2349. /** Inserts curved segments along connected segments. */
  2350. //=============================================================================
  2351. void W3DRoadBuffer::insertCurveSegments(void)
  2352. {
  2353. // Insert the curve segments.
  2354. Int numRoadSegments = m_numRoads;
  2355. Int i;
  2356. Int segmentStartIndex = -1;
  2357. for (i=0; i<numRoadSegments; i++) {
  2358. if (i<numRoadSegments-1 && m_roads[i].m_pt1.loc == m_roads[i+1].m_pt2.loc) {
  2359. if (m_roads[i+1].m_pt2.count==1 && m_roads[i].m_pt1.count==1) {
  2360. insertCurveSegmentAt(i, i+1);
  2361. if (segmentStartIndex < 0) {
  2362. segmentStartIndex = i;
  2363. }
  2364. }
  2365. } else if (segmentStartIndex>=0) {
  2366. if (m_roads[i].m_pt1.loc == m_roads[segmentStartIndex].m_pt2.loc) {
  2367. if (m_roads[segmentStartIndex].m_pt2.count==1 && m_roads[i].m_pt1.count==1) {
  2368. insertCurveSegmentAt(i, segmentStartIndex);
  2369. }
  2370. }
  2371. segmentStartIndex = -1;
  2372. }
  2373. }
  2374. }
  2375. //=============================================================================
  2376. // W3DRoadBuffer::findCrossTypeJoinVector
  2377. //=============================================================================
  2378. /** Finds a road segment of different type && sets the join vector. */
  2379. //=============================================================================
  2380. Int W3DRoadBuffer::findCrossTypeJoinVector(Vector2 loc, Vector2 *joinVector, Int uniqueID)
  2381. {
  2382. Vector2 newVector = *joinVector;
  2383. // Insert the curve segments.
  2384. Int numRoadSegments = m_numRoads;
  2385. Int i;
  2386. for (i=0; i<numRoadSegments; i++) {
  2387. // we only want different road types.
  2388. if (m_roads[i].m_uniqueID == uniqueID) continue;
  2389. // Only join to straight line segments.
  2390. if (m_roads[i].m_type != SEGMENT) continue;
  2391. Vector2 loc1, loc2;
  2392. loc1 = m_roads[i].m_pt1.loc;
  2393. loc2 = m_roads[i].m_pt2.loc;
  2394. Region2D bounds;
  2395. bounds.lo.x = loc1.X;
  2396. bounds.lo.y = loc1.Y;
  2397. bounds.hi = bounds.lo;
  2398. if (bounds.lo.x > loc2.X) bounds.lo.x = loc2.X;
  2399. if (bounds.lo.y > loc2.Y) bounds.lo.y = loc2.Y;
  2400. if (bounds.hi.x < loc2.X) bounds.hi.x = loc2.X;
  2401. if (bounds.hi.y < loc2.Y) bounds.hi.y = loc2.Y;
  2402. bounds.lo.x -= m_roads[i].m_scale/2;
  2403. bounds.lo.y -= m_roads[i].m_scale/2;
  2404. bounds.hi.x += m_roads[i].m_scale/2;
  2405. bounds.hi.y += m_roads[i].m_scale/2;
  2406. if (loc.X >= bounds.lo.x && loc.Y >= bounds.lo.y && loc.X <= bounds.hi.x && loc.Y <= bounds.hi.y) {
  2407. Vector3 v1 = Vector3(loc1.X, loc1.Y, 0);
  2408. Vector3 v2 = Vector3(loc2.X, loc2.Y, 0);
  2409. LineSegClass roadLine(v1,v2);
  2410. Vector3 vLoc(loc.X, loc.Y, 0);
  2411. Vector3 ptOnLine = roadLine.Find_Point_Closest_To(vLoc);
  2412. Real dist = Vector3::Distance(ptOnLine, vLoc);
  2413. if (dist < m_roads[i].m_scale*0.55f) {
  2414. Vector2 roadVec = loc2-loc1;
  2415. if (xpSign(roadVec, *joinVector) == 1) {
  2416. roadVec.Rotate(PI/2);
  2417. } else {
  2418. roadVec.Rotate(-PI/2);
  2419. }
  2420. newVector = roadVec;
  2421. *joinVector = newVector;
  2422. return m_roads[i].m_uniqueID;
  2423. }
  2424. }
  2425. }
  2426. return 0;
  2427. }
  2428. //=============================================================================
  2429. // W3DRoadBuffer::adjustStacking
  2430. //=============================================================================
  2431. /** Adjusts the stacking order. */
  2432. //=============================================================================
  2433. void W3DRoadBuffer::adjustStacking(Int topUniqueID, Int bottomUniqueID)
  2434. {
  2435. Int i, j;
  2436. for (i=0; i<m_maxRoadTypes; i++) {
  2437. if (m_roadTypes[i].getUniqueID() == topUniqueID) break;
  2438. }
  2439. DEBUG_ASSERTLOG(i<m_maxRoadTypes, ("***** Wrong unique id- john a should fix.\n"));
  2440. if (i>=m_maxRoadTypes) return;
  2441. for (j=0; j<m_maxRoadTypes; j++) {
  2442. if (m_roadTypes[j].getUniqueID() == bottomUniqueID) break;
  2443. }
  2444. DEBUG_ASSERTLOG(j<m_maxRoadTypes, ("***** Wrong unique id- john a should fix.\n"));
  2445. if (j>=m_maxRoadTypes) return;
  2446. if (m_roadTypes[i].getStacking() > m_roadTypes[j].getStacking()) {
  2447. return; // It's already on top.
  2448. }
  2449. Int newStacking = m_roadTypes[j].getStacking();
  2450. for (j=0; j<m_maxRoadTypes; j++) {
  2451. if (m_roadTypes[j].getStacking()>newStacking) {
  2452. m_roadTypes[j].setStacking(m_roadTypes[j].getStacking()+1);
  2453. }
  2454. }
  2455. m_roadTypes[i].setStacking(newStacking+1);
  2456. }
  2457. //=============================================================================
  2458. // W3DRoadBuffer::insertCrossTypeJoins
  2459. //=============================================================================
  2460. /** Inserts alpha blend type joins at open ends. */
  2461. //=============================================================================
  2462. void W3DRoadBuffer::insertCrossTypeJoins(void)
  2463. {
  2464. // Insert the curve segments.
  2465. Int numRoadSegments = m_numRoads;
  2466. Int i;
  2467. for (i=0; i<numRoadSegments; i++) {
  2468. Vector2 loc1, loc2;
  2469. Bool isPt1 = false;
  2470. if ((m_roads[i].m_pt2.count==0 && m_roads[i].m_pt2.isJoin)) {
  2471. loc1 = m_roads[i].m_pt2.loc;
  2472. loc2 = m_roads[i].m_pt1.loc;
  2473. } else if ((m_roads[i].m_pt1.count==0 && m_roads[i].m_pt1.isJoin)) {
  2474. loc1 = m_roads[i].m_pt1.loc;
  2475. loc2 = m_roads[i].m_pt2.loc;
  2476. isPt1 = true;
  2477. } else {
  2478. continue;
  2479. }
  2480. Vector2 joinVector(1, 0);
  2481. Vector2 roadVector(loc2.X-loc1.X, loc2.Y-loc1.Y);
  2482. roadVector.Normalize();
  2483. joinVector = roadVector;
  2484. Int otherID = findCrossTypeJoinVector(loc1, &joinVector, m_roads[i].m_uniqueID);
  2485. if (!otherID ) {
  2486. joinVector*=100;
  2487. }
  2488. Vector2 roadNormal(-roadVector.Y, roadVector.X);
  2489. Vector2 joinNormal(-joinVector.Y, joinVector.X);
  2490. Vector2 p1 = loc1 + roadNormal * m_roads[i].m_scale * m_roads[i].m_widthInTexture / 2;
  2491. Vector2 p2 = loc2 + roadNormal * m_roads[i].m_scale * m_roads[i].m_widthInTexture / 2;
  2492. Vector3 v1 = Vector3(p1.X, p1.Y, 0);
  2493. Vector3 v2 = Vector3(p2.X, p2.Y, 0);
  2494. LineSegClass roadLine(v1,v2);
  2495. Vector3 vLoc1(loc1.X, loc1.Y, 0);
  2496. v1 = Vector3(joinNormal.X, joinNormal.Y, 0)+vLoc1;
  2497. LineSegClass joinLine(vLoc1,v1);
  2498. Vector3 pInt1, pInt2;
  2499. //Vector3 pInt3, pInt4;
  2500. Real nu; // not used.
  2501. Vector2 top = m_roads[i].m_pt1.top;
  2502. if (joinLine.Find_Intersection(roadLine, &pInt1, &nu, &pInt2, &nu) ) {
  2503. if (isPt1) {
  2504. m_roads[i].m_pt1.top.Set(pInt1.X, pInt1.Y);
  2505. top = m_roads[i].m_pt1.top;
  2506. } else {
  2507. m_roads[i].m_pt2.bottom.Set(pInt1.X, pInt1.Y);
  2508. top = m_roads[i].m_pt2.bottom;
  2509. }
  2510. }
  2511. p1 = loc1 - roadNormal * m_roads[i].m_scale * m_roads[i].m_widthInTexture / 2;
  2512. p2 = loc2 - roadNormal * m_roads[i].m_scale * m_roads[i].m_widthInTexture / 2;
  2513. v1.Set(p1.X, p1.Y, 0);
  2514. v2.Set(p2.X, p2.Y, 0);
  2515. roadLine.Set(v1,v2);
  2516. Vector2 bottom = m_roads[i].m_pt1.bottom;
  2517. if (joinLine.Find_Intersection(roadLine, &pInt1, &nu, &pInt2, &nu) ) {
  2518. if (isPt1) {
  2519. m_roads[i].m_pt1.bottom.Set(pInt1.X, pInt1.Y);
  2520. bottom = m_roads[i].m_pt1.bottom;
  2521. } else {
  2522. m_roads[i].m_pt2.top.Set(pInt1.X, pInt1.Y);
  2523. bottom = m_roads[i].m_pt2.top;
  2524. }
  2525. }
  2526. bottom = bottom-top;
  2527. Real scaleAdjustment = bottom.Length() / (m_roads[i].m_scale * m_roads[i].m_widthInTexture);
  2528. if (otherID) {
  2529. adjustStacking(m_roads[i].m_uniqueID, otherID);
  2530. }
  2531. CHECK_SEGMENTS;
  2532. m_roads[m_numRoads].m_pt1.loc.Set(loc1);
  2533. m_roads[m_numRoads].m_pt2.loc.Set(loc1+joinVector);
  2534. m_roads[m_numRoads].m_pt1.last = true; // if not, that one will clear flag in prior loop.
  2535. m_roads[m_numRoads].m_pt2.last = true; // if not, that one will clear flag in prior loop.
  2536. m_roads[m_numRoads].m_scale = m_roads[i].m_scale;
  2537. m_roads[m_numRoads].m_widthInTexture = m_roads[i].m_scale * scaleAdjustment;
  2538. m_roads[m_numRoads].m_pt1.count = 0;
  2539. m_roads[m_numRoads].m_type = ALPHA_JOIN;
  2540. m_roads[m_numRoads].m_uniqueID = m_roads[i].m_uniqueID;
  2541. m_numRoads++;
  2542. }
  2543. }
  2544. //=============================================================================
  2545. // W3DRoadBuffer::miter
  2546. //=============================================================================
  2547. /** Adjusts the end points to create a smooth miter join between road segments. */
  2548. //=============================================================================
  2549. void W3DRoadBuffer::miter(Int ndx1, Int ndx2)
  2550. {
  2551. // adjust a mitered join. jba.
  2552. Vector3 p1 = Vector3(m_roads[ndx1].m_pt1.top.X, m_roads[ndx1].m_pt1.top.Y, 0);
  2553. Vector3 p2 = Vector3(m_roads[ndx1].m_pt2.top.X, m_roads[ndx1].m_pt2.top.Y, 0);
  2554. LineSegClass offsetLine1(p1, p2);
  2555. p1 = Vector3(m_roads[ndx2].m_pt1.top.X, m_roads[ndx2].m_pt1.top.Y, 0);
  2556. p2 = Vector3(m_roads[ndx2].m_pt2.top.X, m_roads[ndx2].m_pt2.top.Y, 0);
  2557. LineSegClass offsetLine2(p1,p2);
  2558. Vector3 pInt1, pInt2;
  2559. Real nu; // not used.
  2560. if (offsetLine1.Find_Intersection(offsetLine2, &pInt1, &nu, &pInt2, &nu) ) {
  2561. m_roads[ndx2].m_pt2.top.X = pInt1.X;
  2562. m_roads[ndx2].m_pt2.top.Y = pInt1.Y;
  2563. m_roads[ndx1].m_pt1.top.X = pInt1.X;
  2564. m_roads[ndx1].m_pt1.top.Y = pInt1.Y;
  2565. }
  2566. p1 = Vector3(m_roads[ndx1].m_pt1.bottom.X, m_roads[ndx1].m_pt1.bottom.Y, 0);
  2567. p2 = Vector3(m_roads[ndx1].m_pt2.bottom.X, m_roads[ndx1].m_pt2.bottom.Y, 0);
  2568. offsetLine1=LineSegClass (p1,p2);
  2569. p1 = Vector3(m_roads[ndx2].m_pt1.bottom.X, m_roads[ndx2].m_pt1.bottom.Y, 0);
  2570. p2 = Vector3(m_roads[ndx2].m_pt2.bottom.X, m_roads[ndx2].m_pt2.bottom.Y, 0);
  2571. offsetLine2 = LineSegClass(p1,p2);
  2572. if (offsetLine1.Find_Intersection(offsetLine2, &pInt1, &nu, &pInt2, &nu) ) {
  2573. m_roads[ndx2].m_pt2.bottom.X = pInt1.X;
  2574. m_roads[ndx2].m_pt2.bottom.Y = pInt1.Y;
  2575. m_roads[ndx1].m_pt1.bottom.X = pInt1.X;
  2576. m_roads[ndx1].m_pt1.bottom.Y = pInt1.Y;
  2577. }
  2578. }
  2579. //=============================================================================
  2580. // W3DRoadBuffer::insertCurveSegmentAt
  2581. //=============================================================================
  2582. /** Insertes curves at the corner of 2 segments. */
  2583. //=============================================================================
  2584. void W3DRoadBuffer::insertCurveSegmentAt(Int ndx1, Int ndx2)
  2585. {
  2586. const Real DOT_LIMIT = 0.5f; // If the dot product of the new line is less than this, abort.
  2587. Real radius = m_roads[ndx1].m_curveRadius*m_roads[ndx1].m_scale;
  2588. Vector2 originalPt = m_roads[ndx1].m_pt1.loc;
  2589. // we got a segment.
  2590. LineSegClass line1(Vector3(m_roads[ndx1].m_pt1.loc.X, m_roads[ndx1].m_pt1.loc.Y, 0), Vector3(m_roads[ndx1].m_pt2.loc.X, m_roads[ndx1].m_pt2.loc.Y, 0));
  2591. LineSegClass line2(Vector3(m_roads[ndx2].m_pt1.loc.X, m_roads[ndx2].m_pt1.loc.Y, 0), Vector3(m_roads[ndx2].m_pt2.loc.X, m_roads[ndx2].m_pt2.loc.Y, 0));
  2592. Vector2 *pr1, *pr2, *pr3, *pr4;
  2593. if (m_roads[ndx1].m_uniqueID != m_roads[ndx2].m_uniqueID) {
  2594. return;
  2595. }
  2596. Real curSin = Vector3::Dot_Product(line1.Get_Dir(), line2.Get_Dir());
  2597. Real xpdct = Vector3::Cross_Product_Z(line1.Get_Dir(), line2.Get_Dir());
  2598. Bool turnRight;
  2599. if (xpdct > 0) {
  2600. pr1 = &m_roads[ndx1].m_pt1.loc;
  2601. pr2 = &m_roads[ndx1].m_pt2.loc;
  2602. pr3 = &m_roads[ndx2].m_pt1.loc;
  2603. pr4 = &m_roads[ndx2].m_pt2.loc;
  2604. turnRight = true;
  2605. } else {
  2606. pr4 = &m_roads[ndx1].m_pt1.loc;
  2607. pr3 = &m_roads[ndx1].m_pt2.loc;
  2608. pr2 = &m_roads[ndx2].m_pt1.loc;
  2609. pr1 = &m_roads[ndx2].m_pt2.loc;
  2610. turnRight = false;
  2611. line1.Set(Vector3(pr1->X, pr1->Y, 0), Vector3(pr2->X, pr2->Y, 0));
  2612. line2.Set(Vector3(pr3->X, pr3->Y, 0), Vector3(pr4->X, pr4->Y, 0));
  2613. }
  2614. Real angle = WWMath::Acos(curSin);
  2615. Real count = angle / (PI/6.0f); // number of 30 degree steps.
  2616. if (count<0.9 || m_roads[ndx1].m_pt1.isAngled) {
  2617. miter(ndx1, ndx2);
  2618. return;
  2619. }
  2620. Vector3 offset1(radius*line1.Get_Dir());
  2621. Vector3 offset2(radius*line2.Get_Dir());
  2622. offset1.Rotate_Z(-PI/2);
  2623. offset2.Rotate_Z(-PI/2);
  2624. Vector3 p1 = Vector3(pr1->X, pr1->Y, 0)+offset1;
  2625. Vector3 p2 = Vector3(pr2->X, pr2->Y, 0)+offset1;
  2626. LineSegClass offsetLine1(p1,p2);
  2627. p1 = Vector3(pr3->X, pr3->Y, 0)+offset2;
  2628. p2 = Vector3(pr4->X, pr4->Y, 0)+offset2;
  2629. LineSegClass offsetLine2(p1,p2);
  2630. Vector3 pInt1, pInt2;
  2631. Vector3 pInt3, pInt4;
  2632. Real nu; // not used.
  2633. if (offsetLine1.Find_Intersection(offsetLine2, &pInt1, &nu, &pInt2, &nu) ) {
  2634. m_roads[ndx2].m_pt2.last = true;
  2635. LineSegClass cross1(pInt1, pInt1-offset2);
  2636. LineSegClass cross2(pInt1, pInt1-offset1);
  2637. cross1.Find_Intersection(line2, &pInt1, &nu, &pInt2, &nu);
  2638. cross2.Find_Intersection(line1, &pInt3, &nu, &pInt4, &nu);
  2639. // Make sure the lines didn't clip out of existence.
  2640. Real theDot = Vector3::Dot_Product(line2.Get_Dir(), pInt1-Vector3(pr3->X, pr3->Y, 0));
  2641. if (theDot < DOT_LIMIT) {
  2642. *pr1 = originalPt;
  2643. *pr4 = originalPt;
  2644. miter(ndx1, ndx2);
  2645. return;
  2646. }
  2647. theDot = Vector3::Dot_Product(line1.Get_Dir(), Vector3(pr2->X, pr2->Y, 0)-pInt3);
  2648. if (theDot < DOT_LIMIT) {
  2649. *pr1 = originalPt;
  2650. *pr4 = originalPt;
  2651. miter(ndx1, ndx2);
  2652. return;
  2653. }
  2654. *pr4 = Vector2(pInt1.X, pInt1.Y);
  2655. Real angle = -PI/6.0f; // -30 degrees.
  2656. Vector2 pt2 = *pr4;
  2657. Vector2 pt1 = *pr3;
  2658. Vector2 direction(pt1.X-pt2.X, pt1.Y-pt2.Y);
  2659. // offset = normal of the vector from pt1 to pt2.
  2660. Vector2 centerOfCurve(-direction.Y, direction.X);
  2661. centerOfCurve.Normalize();
  2662. centerOfCurve *= m_roads[ndx1].m_curveRadius*m_roads[ndx1].m_scale;
  2663. centerOfCurve += pt2;
  2664. rotateAbout(&pt2, centerOfCurve, angle);
  2665. direction.Rotate(angle);
  2666. pt1 = pt2+direction;
  2667. m_roads[m_numRoads].m_pt1.loc=pt2;
  2668. m_roads[m_numRoads].m_pt2.loc=pt1;
  2669. CHECK_SEGMENTS;
  2670. m_roads[m_numRoads].m_pt1.last = true; // if not, that one will clear flag in prior loop.
  2671. m_roads[m_numRoads].m_pt2.last = true; // if not, that one will clear flag in prior loop.
  2672. m_roads[m_numRoads].m_scale = m_roads[ndx1].m_scale;
  2673. m_roads[m_numRoads].m_widthInTexture = m_roads[ndx1].m_widthInTexture;
  2674. m_roads[m_numRoads].m_type = CURVE;
  2675. m_roads[m_numRoads].m_curveRadius = m_roads[ndx1].m_curveRadius;
  2676. m_roads[m_numRoads].m_uniqueID = m_roads[ndx1].m_uniqueID;
  2677. m_numRoads++;
  2678. if (count > 2.0) {
  2679. Int i;
  2680. for (i=2; i<count; i++) {
  2681. // offset = normal of the vector from pt1 to pt2.
  2682. direction.Rotate(angle);
  2683. rotateAbout(&pt2, centerOfCurve, angle);
  2684. pt1 = pt2+direction;
  2685. CHECK_SEGMENTS;
  2686. m_roads[m_numRoads].m_pt1.loc.Set(pt2);
  2687. m_roads[m_numRoads].m_pt2.loc.Set(pt1);
  2688. m_roads[m_numRoads].m_pt1.last = true; // if not, that one will clear flag in prior loop.
  2689. m_roads[m_numRoads].m_pt2.last = true; // if not, that one will clear flag in prior loop.
  2690. m_roads[m_numRoads].m_scale = m_roads[ndx1].m_scale;
  2691. m_roads[m_numRoads].m_widthInTexture = m_roads[ndx1].m_widthInTexture;
  2692. m_roads[m_numRoads].m_type = CURVE;
  2693. m_roads[m_numRoads].m_curveRadius = m_roads[ndx1].m_curveRadius;
  2694. m_roads[m_numRoads].m_uniqueID = m_roads[ndx1].m_uniqueID;
  2695. m_numRoads++;
  2696. }
  2697. }
  2698. *pr1 = Vector2(pInt3.X, pInt3.Y);
  2699. m_roads[ndx1].m_pt1.last = true;
  2700. if (count > 1.0) {
  2701. pt2 = *pr1;
  2702. pt1 = *pr1+*pr1-*pr2;
  2703. direction.Set(pt1.X-pt2.X, pt1.Y-pt2.Y);
  2704. pt1 = pt2+direction;
  2705. CHECK_SEGMENTS;
  2706. m_roads[m_numRoads].m_pt1.loc.Set(pt2);
  2707. m_roads[m_numRoads].m_pt2.loc.Set(pt1);
  2708. m_roads[m_numRoads].m_pt1.last = true; // if not, that one will clear flag in prior loop.
  2709. m_roads[m_numRoads].m_pt2.last = true; // if not, that one will clear flag in prior loop.
  2710. m_roads[m_numRoads].m_scale = m_roads[ndx1].m_scale;
  2711. m_roads[m_numRoads].m_widthInTexture = m_roads[ndx1].m_widthInTexture;
  2712. m_roads[m_numRoads].m_type = CURVE;
  2713. m_roads[m_numRoads].m_curveRadius = m_roads[ndx1].m_curveRadius;
  2714. m_roads[m_numRoads].m_uniqueID = m_roads[ndx1].m_uniqueID;
  2715. m_numRoads++;
  2716. }
  2717. // Recalculate top & bottom.
  2718. Vector2 roadVector = m_roads[ndx1].m_pt2.loc - m_roads[ndx1].m_pt1.loc;
  2719. Vector2 roadNormal(-roadVector.Y, roadVector.X);
  2720. roadNormal.Normalize();
  2721. roadNormal *= (m_roads[ndx1].m_scale*m_roads[ndx1].m_widthInTexture/2.0f);
  2722. m_roads[ndx1].m_pt1.top = m_roads[ndx1].m_pt1.loc+roadNormal;
  2723. m_roads[ndx1].m_pt1.bottom = m_roads[ndx1].m_pt1.loc - roadNormal;
  2724. roadVector = m_roads[ndx2].m_pt2.loc - m_roads[ndx2].m_pt1.loc;
  2725. roadNormal = Vector2(-roadVector.Y, roadVector.X);
  2726. roadNormal.Normalize();
  2727. roadNormal *= (m_roads[ndx2].m_scale*m_roads[ndx2].m_widthInTexture/2.0f);
  2728. m_roads[ndx2].m_pt2.top = m_roads[ndx2].m_pt2.loc+roadNormal;
  2729. m_roads[ndx2].m_pt2.bottom = m_roads[ndx2].m_pt2.loc - roadNormal;
  2730. }
  2731. }
  2732. //=============================================================================
  2733. // W3DRoadBuffer::rotateAbout
  2734. //=============================================================================
  2735. /** Rotates ptP about center. */
  2736. //=============================================================================
  2737. void W3DRoadBuffer::rotateAbout(Vector2 *ptP, Vector2 center, Real angle)
  2738. {
  2739. Vector2 offset;
  2740. offset.X = ptP->X - center.X;
  2741. offset.Y = ptP->Y - center.Y;
  2742. Vector2 orgOffset = offset;
  2743. offset.Rotate(angle);
  2744. offset.Y -= orgOffset.Y;
  2745. offset.X -= orgOffset.X;
  2746. *ptP += offset;
  2747. }
  2748. //-----------------------------------------------------------------------------
  2749. // Public Functions
  2750. //-----------------------------------------------------------------------------
  2751. //=============================================================================
  2752. // W3DRoadBuffer::~W3DRoadBuffer
  2753. //=============================================================================
  2754. /** Destructor. Releases w3d assets. */
  2755. //=============================================================================
  2756. W3DRoadBuffer::~W3DRoadBuffer(void)
  2757. {
  2758. freeRoadBuffers();
  2759. REF_PTR_RELEASE(m_map);
  2760. }
  2761. //=============================================================================
  2762. // W3DRoadBuffer::W3DRoadBuffer
  2763. //=============================================================================
  2764. /** Constructor. */
  2765. //=============================================================================
  2766. W3DRoadBuffer::W3DRoadBuffer(void) :
  2767. m_roads(NULL),
  2768. m_numRoads(0),
  2769. m_initialized(false),
  2770. m_map(NULL),
  2771. #ifdef LOAD_TEST_ASSETS
  2772. m_maxUID(0),
  2773. #endif // LOAD_TEST_ASSETS
  2774. m_lightsIterator(NULL),
  2775. m_maxRoadSegments(500),
  2776. m_maxRoadTypes(8),
  2777. m_maxRoadVertex(1000),
  2778. m_maxRoadIndex(2000),
  2779. m_curRoadType(0)
  2780. {
  2781. allocateRoadBuffers();
  2782. }
  2783. //=============================================================================
  2784. // W3DRoadBuffer::freeRoadBuffers
  2785. //=============================================================================
  2786. /** Frees the index and vertex buffers. */
  2787. //=============================================================================
  2788. void W3DRoadBuffer::freeRoadBuffers(void)
  2789. {
  2790. if (m_roads) {
  2791. delete[] m_roads;
  2792. m_roads = NULL;
  2793. }
  2794. if (m_roadTypes) {
  2795. delete[] m_roadTypes;
  2796. m_roadTypes = NULL;
  2797. }
  2798. }
  2799. //=============================================================================
  2800. // W3DRoadBuffer::allocateRoadBuffers
  2801. //=============================================================================
  2802. /** Allocates the index and vertex buffers. */
  2803. //=============================================================================
  2804. void W3DRoadBuffer::allocateRoadBuffers(void)
  2805. {
  2806. Int i = 0;
  2807. // save data for max limits
  2808. m_maxRoadSegments = TheGlobalData->m_maxRoadSegments;
  2809. m_maxRoadVertex = TheGlobalData->m_maxRoadVertex;
  2810. m_maxRoadIndex = TheGlobalData->m_maxRoadIndex;
  2811. m_maxRoadTypes = TheGlobalData->m_maxRoadTypes;
  2812. #ifdef LOAD_TEST_ASSETS
  2813. m_maxRoadTypes+=4;
  2814. #endif
  2815. m_curNumRoadVertices=0;
  2816. m_curNumRoadIndices=0;
  2817. m_roads = MSGNEW("RoadBuffer") RoadSegment[m_maxRoadSegments];
  2818. m_roadTypes = MSGNEW("RoadBuffer") RoadType[m_maxRoadTypes];
  2819. // load roads from INI
  2820. TerrainRoadType *road;
  2821. i = 0;
  2822. for( road = TheTerrainRoads->firstRoad(); road; road = TheTerrainRoads->nextRoad( road ) )
  2823. {
  2824. // get a path to the texture file
  2825. if( i < m_maxRoadTypes )
  2826. {
  2827. Int id = road->getID();
  2828. m_roadTypes[ i++ ].loadTexture( road->getTexture(), id );
  2829. #ifdef LOAD_TEST_ASSETS
  2830. if( m_maxUID < id )
  2831. m_maxUID = id;
  2832. #endif // LOAD_TEST_ASSETS
  2833. } // end if
  2834. } // end for road
  2835. #ifdef LOAD_TEST_ASSETS
  2836. while (i<m_maxRoadTypes && m_roadTypes[i].isAutoLoaded()) {
  2837. m_roadTypes[i++].loadTestTexture();
  2838. }
  2839. m_curOpenRoad = i;
  2840. #endif
  2841. m_initialized = true;
  2842. }
  2843. //=============================================================================
  2844. // W3DRoadBuffer::clearAllRoads
  2845. //=============================================================================
  2846. /** Removes all roads. */
  2847. //=============================================================================
  2848. void W3DRoadBuffer::clearAllRoads(void)
  2849. {
  2850. Int i;
  2851. if (m_roads)
  2852. for (i=0; i<m_numRoads; i++) {
  2853. m_roads[i].SetIndexBuffer(NULL,0);
  2854. m_roads[i].SetVertexBuffer(NULL,0);
  2855. }
  2856. m_numRoads = 0;
  2857. if (m_roadTypes)
  2858. for (i=0; i<m_maxRoadTypes; i++) {
  2859. m_roadTypes[i].setStacking(0); // Reset stacking orders
  2860. }
  2861. }
  2862. //=============================================================================
  2863. // W3DRoadBuffer::setMap
  2864. //=============================================================================
  2865. /** Sets the height map. */
  2866. //=============================================================================
  2867. void W3DRoadBuffer::setMap(WorldHeightMap *pMap)
  2868. {
  2869. REF_PTR_SET(m_map, pMap);
  2870. }
  2871. //=============================================================================
  2872. // W3DRoadBuffer::loadRoads
  2873. //=============================================================================
  2874. /** Loads the roads from the map objects. */
  2875. //=============================================================================
  2876. void W3DRoadBuffer::loadRoads()
  2877. {
  2878. if (!m_initialized) {
  2879. return;
  2880. }
  2881. // Free any existing road segments.
  2882. clearAllRoads();
  2883. //Int ticks = ::GetTickCount();
  2884. addMapObjects();
  2885. updateCountsAndFlags();
  2886. insertTeeIntersections();
  2887. insertCurveSegments();
  2888. insertCrossTypeJoins();
  2889. preloadRoadsInVertexAndIndexBuffers();
  2890. //ticks = ::GetTickCount() - ticks;
  2891. //char buf[256];
  2892. //sprintf(buf, "%d road segs, %d milisec.\n", m_numRoads, ticks);
  2893. //::OutputDebugString(buf);
  2894. }
  2895. //=============================================================================
  2896. // W3DRoadBuffer::updateLighting
  2897. //=============================================================================
  2898. /** Draws the roads. Uses terrain bounds to cull. */
  2899. //=============================================================================
  2900. void W3DRoadBuffer::updateLighting(void)
  2901. {
  2902. Int curRoad;
  2903. // Do road segments.
  2904. for (curRoad=0; curRoad<m_numRoads; curRoad++) {
  2905. m_roads[curRoad].updateSegLighting();
  2906. }
  2907. }
  2908. //=============================================================================
  2909. // W3DRoadBuffer::drawRoads
  2910. //=============================================================================
  2911. /** Draws the roads. */
  2912. //=============================================================================
  2913. void W3DRoadBuffer::drawRoads(CameraClass * camera, TextureClass *cloudTexture, TextureClass *noiseTexture, Bool wireframe,
  2914. Int minX, Int maxX, Int minY, Int maxY, RefRenderObjListIterator *pDynamicLightsIterator)
  2915. {
  2916. m_minX = minX*MAP_XY_FACTOR;
  2917. m_maxX = maxX*MAP_XY_FACTOR;
  2918. m_minY = minY*MAP_XY_FACTOR;
  2919. m_maxY = maxY*MAP_XY_FACTOR;
  2920. Int i;
  2921. Int maxStacking = 0;
  2922. for (i=0; i<m_maxRoadTypes; i++) {
  2923. if (m_roadTypes[i].getStacking() > maxStacking) {
  2924. maxStacking = m_roadTypes[i].getStacking();
  2925. }
  2926. }
  2927. Int stacking;
  2928. W3DShaderManager::ShaderTypes st=W3DShaderManager::ST_ROAD_BASE; //set default shader
  2929. if (cloudTexture)
  2930. { st=W3DShaderManager::ST_ROAD_BASE_NOISE1;
  2931. if (noiseTexture)
  2932. st=W3DShaderManager::ST_ROAD_BASE_NOISE12;
  2933. }
  2934. else
  2935. if (noiseTexture)
  2936. st=W3DShaderManager::ST_ROAD_BASE_NOISE2;
  2937. Int devicePasses = 1; //assume regular rendering
  2938. //Find number of passes required to render current shader
  2939. devicePasses=W3DShaderManager::getShaderPasses(st);
  2940. W3DShaderManager::setTexture(1,cloudTexture); //cloud
  2941. W3DShaderManager::setTexture(2,noiseTexture); //noise/lightmap
  2942. for (stacking=0; stacking <= maxStacking; stacking++) {
  2943. for (i=0; i<m_maxRoadTypes; i++) {
  2944. if (stacking != m_roadTypes[i].getStacking()) {
  2945. continue;
  2946. }
  2947. m_curUniqueID = m_roadTypes[i].getUniqueID();
  2948. m_curRoadType = i;
  2949. loadRoadsInVertexAndIndexBuffers();
  2950. if (m_roadTypes[i].getNumIndices() == 0) continue;
  2951. if (wireframe) {
  2952. m_roadTypes[i].applyTexture();
  2953. DX8Wrapper::Set_Texture(0,NULL);
  2954. DX8Wrapper::Set_Shader(detailShader); // shows clipping.
  2955. } else {
  2956. m_roadTypes[i].applyTexture();
  2957. }
  2958. #ifdef _DEBUG
  2959. //DX8Wrapper::Set_Shader(detailShader); // shows clipping.
  2960. #endif
  2961. for (Int pass=0; pass < devicePasses; pass++)
  2962. {
  2963. if (!wireframe)
  2964. W3DShaderManager::setShader(st, pass);
  2965. //Draw all this road type.
  2966. DX8Wrapper::Draw_Triangles( 0, m_roadTypes[i].getNumIndices()/3, 0, m_roadTypes[i].getNumVertices());
  2967. }
  2968. if (!wireframe) //shader was applied at least once?
  2969. W3DShaderManager::resetShader(st);
  2970. }
  2971. }
  2972. #if 0
  2973. // Need to use a separate set of index & vertex buffers for this. jba.
  2974. DX8Wrapper::Set_Index_Buffer(NULL,0);
  2975. DX8Wrapper::Set_Vertex_Buffer(NULL);
  2976. if (pDynamicLightsIterator) {
  2977. for (i=0; i<m_maxRoadTypes; i++) {
  2978. m_curRoadType = i;
  2979. m_curUniqueID = m_roadTypes[i].getUniqueID();
  2980. if (m_curUniqueID < 0 || m_curUniqueID >= m_maxRoadTypes) continue;
  2981. loadLitRoadsInVertexAndIndexBuffers(pDynamicLightsIterator);
  2982. if (this->m_curNumRoadIndices == 0) continue;
  2983. if (wireframe) {
  2984. DX8Wrapper::Set_Texture(0,NULL);
  2985. } else {
  2986. m_roadTypes[i].applyTexture();
  2987. if (cloudTexture) {
  2988. DX8Wrapper::Set_Texture(1,cloudTexture);
  2989. }
  2990. }
  2991. DX8Wrapper::Set_Shader(detailAlphaShader);
  2992. //Draw all the roads.
  2993. DX8Wrapper::Draw_Triangles( 0, m_curNumRoadIndices/3, 0, m_curNumRoadVertices);
  2994. }
  2995. }
  2996. #endif
  2997. m_curRoadType = 0;
  2998. }