W3DRoadBuffer.cpp 110 KB

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