interior.cpp 85 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "interior/interior.h"
  24. #include "scene/sceneRenderState.h"
  25. #include "scene/sceneManager.h"
  26. #include "gfx/bitmap/gBitmap.h"
  27. #include "math/mMatrix.h"
  28. #include "math/mRect.h"
  29. #include "core/bitVector.h"
  30. #include "core/frameAllocator.h"
  31. #include "scene/sgUtil.h"
  32. #include "platform/profiler.h"
  33. #include "gfx/gfxDevice.h"
  34. #include "gfx/gfxTextureHandle.h"
  35. #include "materials/materialList.h"
  36. #include "materials/matInstance.h"
  37. #include "materials/materialManager.h"
  38. #include "renderInstance/renderPassManager.h"
  39. #include "materials/processedMaterial.h"
  40. #include "materials/materialFeatureTypes.h"
  41. U32 Interior::smRenderMode = 0;
  42. bool Interior::smFocusedDebug = false;
  43. bool Interior::smUseVertexLighting = false;
  44. bool Interior::smLightingCastRays = false;
  45. bool Interior::smLightingBuildPolyList = false;
  46. // These are setup by setupActivePolyList
  47. U16* sgActivePolyList = NULL;
  48. U32 sgActivePolyListSize = 0;
  49. U16* sgEnvironPolyList = NULL;
  50. U32 sgEnvironPolyListSize = 0;
  51. U16* sgFogPolyList = NULL;
  52. U32 sgFogPolyListSize = 0;
  53. bool sgFogActive = false;
  54. // Always the same size as the mPoints array
  55. Point2F* sgFogTexCoords = NULL;
  56. class PlaneRange
  57. {
  58. public:
  59. U32 start;
  60. U32 count;
  61. };
  62. namespace {
  63. struct PortalRenderInfo
  64. {
  65. bool render;
  66. F64 frustum[4];
  67. RectI viewport;
  68. };
  69. //-------------------------------------- Rendering state variables.
  70. Point3F sgCamPoint;
  71. F64 sgStoredFrustum[6];
  72. RectI sgStoredViewport;
  73. Vector<PortalRenderInfo> sgZoneRenderInfo(__FILE__, __LINE__);
  74. // Takes OS coords to clip space...
  75. MatrixF sgWSToOSMatrix;
  76. MatrixF sgProjMatrix;
  77. PlaneF sgOSPlaneFar;
  78. PlaneF sgOSPlaneXMin;
  79. PlaneF sgOSPlaneXMax;
  80. PlaneF sgOSPlaneYMin;
  81. PlaneF sgOSPlaneYMax;
  82. struct ZoneRect {
  83. RectD rect;
  84. bool active;
  85. };
  86. Vector<ZoneRect> sgZoneRects(__FILE__, __LINE__);
  87. //-------------------------------------- Little utility functions
  88. RectD outlineRects(const Vector<RectD>& rects)
  89. {
  90. F64 minx = 1e10;
  91. F64 maxx = -1e10;
  92. F64 miny = 1e10;
  93. F64 maxy = -1e10;
  94. for (S32 i = 0; i < rects.size(); i++)
  95. {
  96. if (rects[i].point.x < minx)
  97. minx = rects[i].point.x;
  98. if (rects[i].point.y < miny)
  99. miny = rects[i].point.y;
  100. if (rects[i].point.x + rects[i].extent.x > maxx)
  101. maxx = rects[i].point.x + rects[i].extent.x;
  102. if (rects[i].point.y + rects[i].extent.y > maxy)
  103. maxy = rects[i].point.y + rects[i].extent.y;
  104. }
  105. return RectD(minx, miny, maxx - minx, maxy - miny);
  106. }
  107. void insertZoneRects(ZoneRect& rZoneRect, const RectD* rects, const U32 numRects)
  108. {
  109. F64 minx = 1e10;
  110. F64 maxx = -1e10;
  111. F64 miny = 1e10;
  112. F64 maxy = -1e10;
  113. for (U32 i = 0; i < numRects; i++) {
  114. if (rects[i].point.x < minx)
  115. minx = rects[i].point.x;
  116. if (rects[i].point.y < miny)
  117. miny = rects[i].point.y;
  118. if (rects[i].point.x + rects[i].extent.x > maxx)
  119. maxx = rects[i].point.x + rects[i].extent.x;
  120. if (rects[i].point.y + rects[i].extent.y > maxy)
  121. maxy = rects[i].point.y + rects[i].extent.y;
  122. }
  123. if (rZoneRect.active == false && numRects != 0) {
  124. rZoneRect.rect = RectD(minx, miny, maxx - minx, maxy - miny);
  125. rZoneRect.active = true;
  126. } else {
  127. if (rZoneRect.rect.point.x < minx)
  128. minx = rZoneRect.rect.point.x;
  129. if (rZoneRect.rect.point.y < miny)
  130. miny = rZoneRect.rect.point.y;
  131. if (rZoneRect.rect.point.x + rZoneRect.rect.extent.x > maxx)
  132. maxx = rZoneRect.rect.point.x + rZoneRect.rect.extent.x;
  133. if (rZoneRect.rect.point.y + rZoneRect.rect.extent.y > maxy)
  134. maxy = rZoneRect.rect.point.y + rZoneRect.rect.extent.y;
  135. rZoneRect.rect = RectD(minx, miny, maxx - minx, maxy - miny);
  136. }
  137. }
  138. void fixupViewport(PortalRenderInfo& rInfo)
  139. {
  140. F64 widthV = rInfo.frustum[1] - rInfo.frustum[0];
  141. F64 heightV = rInfo.frustum[3] - rInfo.frustum[2];
  142. F64 fx0 = (rInfo.frustum[0] - sgStoredFrustum[0]) / (sgStoredFrustum[1] - sgStoredFrustum[0]);
  143. F64 fx1 = (sgStoredFrustum[1] - rInfo.frustum[1]) / (sgStoredFrustum[1] - sgStoredFrustum[0]);
  144. F64 dV0 = F64(sgStoredViewport.point.x) + fx0 * F64(sgStoredViewport.extent.x);
  145. F64 dV1 = F64(sgStoredViewport.point.x +
  146. sgStoredViewport.extent.x) - fx1 * F64(sgStoredViewport.extent.x);
  147. F64 fdV0 = getMax(mFloorD(dV0), F64(sgStoredViewport.point.x));
  148. F64 cdV1 = getMin(mCeilD(dV1), F64(sgStoredViewport.point.x + sgStoredViewport.extent.x));
  149. // If the width is 1 pixel, we need to widen it up a bit...
  150. if ((cdV1 - fdV0) <= 1.0)
  151. cdV1 = fdV0 + 1;
  152. AssertFatal((fdV0 >= sgStoredViewport.point.x &&
  153. cdV1 <= sgStoredViewport.point.x + sgStoredViewport.extent.x),
  154. "Out of bounds viewport bounds");
  155. F64 new0 = rInfo.frustum[0] - ((dV0 - fdV0) * (widthV / F64(sgStoredViewport.extent.x)));
  156. F64 new1 = rInfo.frustum[1] + ((cdV1 - dV1) * (widthV / F64(sgStoredViewport.extent.x)));
  157. rInfo.frustum[0] = new0;
  158. rInfo.frustum[1] = new1;
  159. rInfo.viewport.point.x = S32(fdV0);
  160. rInfo.viewport.extent.x = S32(cdV1) - rInfo.viewport.point.x;
  161. F64 fy0 = (sgStoredFrustum[3] - rInfo.frustum[3]) / (sgStoredFrustum[3] - sgStoredFrustum[2]);
  162. F64 fy1 = (rInfo.frustum[2] - sgStoredFrustum[2]) / (sgStoredFrustum[3] - sgStoredFrustum[2]);
  163. dV0 = F64(sgStoredViewport.point.y) + fy0 * F64(sgStoredViewport.extent.y);
  164. dV1 = F64(sgStoredViewport.point.y +
  165. sgStoredViewport.extent.y) - fy1 * F64(sgStoredViewport.extent.y);
  166. fdV0 = getMax(mFloorD(dV0), F64(sgStoredViewport.point.y));
  167. cdV1 = getMin(mCeilD(dV1), F64(sgStoredViewport.point.y + sgStoredViewport.extent.y));
  168. // If the width is 1 pixel, we need to widen it up a bit...
  169. if ((cdV1 - fdV0) <= 1.0)
  170. cdV1 = fdV0 + 1;
  171. // GFX2_RENDER_MERGE
  172. // Need to fix this properly but for now *HACK*
  173. #ifndef TORQUE_OS_MAC
  174. AssertFatal((fdV0 >= sgStoredViewport.point.y &&
  175. cdV1 <= sgStoredViewport.point.y + sgStoredViewport.extent.y),
  176. "Out of bounds viewport bounds");
  177. #endif
  178. new0 = rInfo.frustum[2] - ((cdV1 - dV1) * (heightV / F64(sgStoredViewport.extent.y)));
  179. new1 = rInfo.frustum[3] + ((dV0 - fdV0) * (heightV / F64(sgStoredViewport.extent.y)));
  180. rInfo.frustum[2] = new0;
  181. rInfo.frustum[3] = new1;
  182. rInfo.viewport.point.y = S32(fdV0);
  183. rInfo.viewport.extent.y = S32(cdV1) - rInfo.viewport.point.y;
  184. }
  185. RectD convertToRectD(const F64 inResult[4])
  186. {
  187. F64 minx = ((inResult[0] + 1.0f) / 2.0f) * (sgStoredFrustum[1] - sgStoredFrustum[0]) + sgStoredFrustum[0];
  188. F64 maxx = ((inResult[2] + 1.0f) / 2.0f) * (sgStoredFrustum[1] - sgStoredFrustum[0]) + sgStoredFrustum[0];
  189. F64 miny = ((inResult[1] + 1.0f) / 2.0f) * (sgStoredFrustum[3] - sgStoredFrustum[2]) + sgStoredFrustum[2];
  190. F64 maxy = ((inResult[3] + 1.0f) / 2.0f) * (sgStoredFrustum[3] - sgStoredFrustum[2]) + sgStoredFrustum[2];
  191. return RectD(minx, miny, (maxx - minx), (maxy - miny));
  192. }
  193. void convertToFrustum(PortalRenderInfo& zrInfo, const RectD& finalRect)
  194. {
  195. zrInfo.frustum[0] = finalRect.point.x; // left
  196. zrInfo.frustum[1] = finalRect.point.x + finalRect.extent.x; // right
  197. zrInfo.frustum[2] = finalRect.point.y; // bottom
  198. zrInfo.frustum[3] = finalRect.point.y + finalRect.extent.y; // top
  199. fixupViewport(zrInfo);
  200. }
  201. } // namespace {}
  202. //------------------------------------------------------------------------------
  203. //-------------------------------------- IMPLEMENTATION
  204. //
  205. Interior::Interior()
  206. {
  207. mMaterialList = NULL;
  208. mHasTranslucentMaterials = false;
  209. mLMHandle = LM_HANDLE(-1);
  210. // By default, no alarm state, no animated light states
  211. mHasAlarmState = false;
  212. mNumLightStateEntries = 0;
  213. mNumTriggerableLights = 0;
  214. mPreppedForRender = false;;
  215. mSearchTag = 0;
  216. mLightMapBorderSize = 0;
  217. #ifndef TORQUE_SHIPPING
  218. mDebugShader = NULL;
  219. mDebugShaderModelViewSC = NULL;
  220. mDebugShaderShadeColorSC = NULL;
  221. #endif
  222. // Bind our vectors
  223. VECTOR_SET_ASSOCIATION(mPlanes);
  224. VECTOR_SET_ASSOCIATION(mPoints);
  225. VECTOR_SET_ASSOCIATION(mBSPNodes);
  226. VECTOR_SET_ASSOCIATION(mBSPSolidLeaves);
  227. VECTOR_SET_ASSOCIATION(mWindings);
  228. VECTOR_SET_ASSOCIATION(mTexGenEQs);
  229. VECTOR_SET_ASSOCIATION(mLMTexGenEQs);
  230. VECTOR_SET_ASSOCIATION(mWindingIndices);
  231. VECTOR_SET_ASSOCIATION(mSurfaces);
  232. VECTOR_SET_ASSOCIATION(mNullSurfaces);
  233. VECTOR_SET_ASSOCIATION(mSolidLeafSurfaces);
  234. VECTOR_SET_ASSOCIATION(mZones);
  235. VECTOR_SET_ASSOCIATION(mZonePlanes);
  236. VECTOR_SET_ASSOCIATION(mZoneSurfaces);
  237. VECTOR_SET_ASSOCIATION(mZonePortalList);
  238. VECTOR_SET_ASSOCIATION(mPortals);
  239. //VECTOR_SET_ASSOCIATION(mSubObjects);
  240. VECTOR_SET_ASSOCIATION(mLightmaps);
  241. VECTOR_SET_ASSOCIATION(mLightmapKeep);
  242. VECTOR_SET_ASSOCIATION(mNormalLMapIndices);
  243. VECTOR_SET_ASSOCIATION(mAlarmLMapIndices);
  244. VECTOR_SET_ASSOCIATION(mAnimatedLights);
  245. VECTOR_SET_ASSOCIATION(mLightStates);
  246. VECTOR_SET_ASSOCIATION(mStateData);
  247. VECTOR_SET_ASSOCIATION(mStateDataBuffer);
  248. VECTOR_SET_ASSOCIATION(mNameBuffer);
  249. VECTOR_SET_ASSOCIATION(mConvexHulls);
  250. VECTOR_SET_ASSOCIATION(mConvexHullEmitStrings);
  251. VECTOR_SET_ASSOCIATION(mHullIndices);
  252. VECTOR_SET_ASSOCIATION(mHullEmitStringIndices);
  253. VECTOR_SET_ASSOCIATION(mHullSurfaceIndices);
  254. VECTOR_SET_ASSOCIATION(mHullPlaneIndices);
  255. VECTOR_SET_ASSOCIATION(mPolyListPlanes);
  256. VECTOR_SET_ASSOCIATION(mPolyListPoints);
  257. VECTOR_SET_ASSOCIATION(mPolyListStrings);
  258. VECTOR_SET_ASSOCIATION(mCoordBinIndices);
  259. VECTOR_SET_ASSOCIATION(mVehicleConvexHulls);
  260. VECTOR_SET_ASSOCIATION(mVehicleConvexHullEmitStrings);
  261. VECTOR_SET_ASSOCIATION(mVehicleHullIndices);
  262. VECTOR_SET_ASSOCIATION(mVehicleHullEmitStringIndices);
  263. VECTOR_SET_ASSOCIATION(mVehicleHullSurfaceIndices);
  264. VECTOR_SET_ASSOCIATION(mVehicleHullPlaneIndices);
  265. VECTOR_SET_ASSOCIATION(mVehiclePolyListPlanes);
  266. VECTOR_SET_ASSOCIATION(mVehiclePolyListPoints);
  267. VECTOR_SET_ASSOCIATION(mVehiclePolyListStrings);
  268. VECTOR_SET_ASSOCIATION(mVehiclePoints);
  269. VECTOR_SET_ASSOCIATION(mVehicleNullSurfaces);
  270. VECTOR_SET_ASSOCIATION(mVehiclePlanes);
  271. }
  272. Interior::~Interior()
  273. {
  274. U32 i;
  275. delete mMaterialList;
  276. mMaterialList = NULL;
  277. if(mLMHandle != LM_HANDLE(-1))
  278. gInteriorLMManager.removeInterior(mLMHandle);
  279. for(i = 0; i < mLightmaps.size(); i++)
  280. {
  281. delete mLightmaps[i];
  282. mLightmaps[i] = NULL;
  283. }
  284. for(i = 0; i < mMatInstCleanupList.size(); i++)
  285. {
  286. delete mMatInstCleanupList[i];
  287. mMatInstCleanupList[i] = NULL;
  288. }
  289. for(S32 i=0; i<mStaticMeshes.size(); i++)
  290. delete mStaticMeshes[i];
  291. }
  292. //--------------------------------------------------------------------------
  293. bool Interior::prepForRendering(const char* path)
  294. {
  295. if(mPreppedForRender == true)
  296. return true;
  297. // Before we load the material list we temporarily remove
  298. // some special texture names so that we don't get bogus
  299. // texture load warnings in the console.
  300. const Vector<String> &matNames = mMaterialList->getMaterialNameList();
  301. Vector<String> originalNames = matNames;
  302. for (U32 i = 0; i < matNames.size(); i++)
  303. {
  304. if (matNames[i].equal("NULL", String::NoCase) ||
  305. matNames[i].equal("ORIGIN", String::NoCase) ||
  306. matNames[i].equal("TRIGGER", String::NoCase) ||
  307. matNames[i].equal("FORCEFIELD", String::NoCase) ||
  308. matNames[i].equal("EMITTER", String::NoCase) )
  309. {
  310. mMaterialList->setMaterialName(i, String());
  311. }
  312. }
  313. String relPath = Platform::makeRelativePathName(path, Platform::getCurrentDirectory());
  314. // Load the material list
  315. mMaterialList->setTextureLookupPath(relPath);
  316. mMaterialList->mapMaterials();
  317. // Grab all the static meshes and load any textures that didn't originate
  318. // from inside the DIF.
  319. for(S32 i=0; i<mStaticMeshes.size(); i++)
  320. mStaticMeshes[i]->materialList->setTextureLookupPath(relPath);
  321. // Now restore the material names since someone later may
  322. // count on the special texture names being present.
  323. for (U32 i = 0; i < originalNames.size(); i++)
  324. mMaterialList->setMaterialName(i, originalNames[i]);
  325. fillSurfaceTexMats();
  326. createZoneVBs();
  327. cloneMatInstances();
  328. createReflectPlanes();
  329. initMatInstances();
  330. // lightmap manager steals the lightmaps here...
  331. gInteriorLMManager.addInterior(mLMHandle, mLightmaps.size(), this);
  332. AssertFatal(!mLightmaps.size(), "Failed to process lightmaps");
  333. for(U32 i=0; i<mStaticMeshes.size(); i++)
  334. mStaticMeshes[i]->prepForRendering(relPath);
  335. GFXStateBlockDesc sh;
  336. #ifndef TORQUE_SHIPPING
  337. // First create a default state block with
  338. // texturing turned off
  339. mInteriorDebugNoneSB = GFX->createStateBlock(sh);
  340. // Create a state block for portal rendering that
  341. // doesn't have backface culling enabled
  342. sh.cullDefined = true;
  343. sh.cullMode = GFXCullNone;
  344. mInteriorDebugPortalSB = GFX->createStateBlock(sh);
  345. // Reset our cull mode to the default
  346. sh.cullMode = GFXCullCCW;
  347. #endif
  348. // Next turn on the first texture channel
  349. sh.samplersDefined = true;
  350. sh.samplers[0].textureColorOp = GFXTOPModulate;
  351. #ifndef TORQUE_SHIPPING
  352. mInteriorDebugTextureSB = GFX->createStateBlock(sh);
  353. sh.samplers[1].textureColorOp = GFXTOPModulate;
  354. mInteriorDebugTwoTextureSB = GFX->createStateBlock(sh);
  355. #endif
  356. // Lastly create a standard rendering state block
  357. sh.samplers[2].textureColorOp = GFXTOPModulate;
  358. sh.samplers[0].magFilter = GFXTextureFilterLinear;
  359. sh.samplers[0].minFilter = GFXTextureFilterLinear;
  360. mInteriorSB = GFX->createStateBlock(sh);
  361. mPreppedForRender = true;
  362. return true;
  363. }
  364. void Interior::setupAveTexGenLength()
  365. {
  366. /*
  367. F32 len = 0;
  368. for (U32 i = 0; i < mSurfaces.size(); i++)
  369. {
  370. // We're going to assume that most textures don't have separate scales for
  371. // x and y...
  372. F32 lenx = mTexGenEQs[mSurfaces[i].texGenIndex].planeX.len();
  373. len += F32((*mMaterialList)[mSurfaces[i].textureIndex].getWidth()) * lenx;
  374. }
  375. len /= F32(mSurfaces.size());
  376. mAveTexGenLength = len;
  377. */
  378. }
  379. //--------------------------------------------------------------------------
  380. bool Interior::traverseZones(SceneCullingState* state,
  381. const Frustum& frustum,
  382. S32 containingZone,
  383. S32 baseZone,
  384. U32 zoneOffset,
  385. const MatrixF& OSToWS,
  386. const Point3F& objScale,
  387. const bool dontRestrictOutside,
  388. const bool flipClipPlanes,
  389. Frustum& outFrustum)
  390. {
  391. // Store off the viewport and frustum
  392. sgStoredViewport = state->getCameraState().getViewport();
  393. if( dontRestrictOutside )
  394. {
  395. sgStoredFrustum[0] = state->getFrustum().getNearLeft();
  396. sgStoredFrustum[1] = state->getFrustum().getNearRight();
  397. sgStoredFrustum[2] = state->getFrustum().getNearBottom();
  398. sgStoredFrustum[3] = state->getFrustum().getNearTop();
  399. sgStoredFrustum[4] = state->getFrustum().getNearDist();
  400. sgStoredFrustum[5] = state->getFrustum().getFarDist();
  401. }
  402. else
  403. {
  404. sgStoredFrustum[0] = frustum.getNearLeft();
  405. sgStoredFrustum[1] = frustum.getNearRight();
  406. sgStoredFrustum[2] = frustum.getNearBottom();
  407. sgStoredFrustum[3] = frustum.getNearTop();
  408. sgStoredFrustum[4] = frustum.getNearDist();
  409. sgStoredFrustum[5] = frustum.getFarDist();
  410. }
  411. sgProjMatrix = state->getCameraState().getProjectionMatrix();
  412. MatrixF finalModelView = state->getCameraState().getWorldViewMatrix();
  413. finalModelView.mul(OSToWS);
  414. finalModelView.scale(Point3F(objScale.x, objScale.y, objScale.z));
  415. sgProjMatrix.mul(finalModelView);
  416. finalModelView.inverse();
  417. finalModelView.mulP(Point3F(0, 0, 0), &sgCamPoint);
  418. sgWSToOSMatrix = finalModelView;
  419. // do the zone traversal
  420. sgZoneRenderInfo.setSize(mZones.size());
  421. zoneTraversal(baseZone, flipClipPlanes);
  422. // Copy out the information for all zones but the outside zone.
  423. for(U32 i = 1; i < mZones.size(); i++)
  424. {
  425. AssertFatal(zoneOffset != 0xFFFFFFFF, "Error, this should never happen!");
  426. U32 globalIndex = i + zoneOffset - 1;
  427. if( sgZoneRenderInfo[ i ].render )
  428. {
  429. state->addCullingVolumeToZone(
  430. globalIndex,
  431. SceneCullingVolume::Includer,
  432. Frustum(
  433. frustum.isOrtho(),
  434. sgZoneRenderInfo[ i ].frustum[ 0 ],
  435. sgZoneRenderInfo[ i ].frustum[ 1 ],
  436. sgZoneRenderInfo[ i ].frustum[ 3 ],
  437. sgZoneRenderInfo[ i ].frustum[ 2 ],
  438. frustum.getNearDist(),
  439. frustum.getFarDist(),
  440. frustum.getTransform()
  441. )
  442. );
  443. }
  444. }
  445. destroyZoneRectVectors();
  446. // If zone 0 is rendered, then we return true...
  447. bool continueOut = sgZoneRenderInfo[ 0 ].render;
  448. if( continueOut )
  449. outFrustum = Frustum(
  450. frustum.isOrtho(),
  451. sgZoneRenderInfo[ 0 ].frustum[ 0 ],
  452. sgZoneRenderInfo[ 0 ].frustum[ 1 ],
  453. sgZoneRenderInfo[ 0 ].frustum[ 3 ],
  454. sgZoneRenderInfo[ 0 ].frustum[ 2 ],
  455. frustum.getNearDist(),
  456. frustum.getFarDist(),
  457. frustum.getTransform()
  458. );
  459. return sgZoneRenderInfo[0].render;
  460. }
  461. //------------------------------------------------------------------------------
  462. S32 Interior::getZoneForPoint(const Point3F& rPoint) const
  463. {
  464. const IBSPNode* pNode = &mBSPNodes[0];
  465. while (true) {
  466. F32 dist = getPlane(pNode->planeIndex).distToPlane(rPoint);
  467. if (planeIsFlipped(pNode->planeIndex))
  468. dist = -dist;
  469. U32 traverseIndex;
  470. if (dist >= 0)
  471. traverseIndex = pNode->frontIndex;
  472. else
  473. traverseIndex = pNode->backIndex;
  474. if (isBSPLeafIndex(traverseIndex)) {
  475. if (isBSPSolidLeaf(traverseIndex)) {
  476. return -1;
  477. } else {
  478. U16 zone = getBSPEmptyLeafZone(traverseIndex);
  479. if (zone == 0x0FFF)
  480. return -1;
  481. else
  482. return zone;
  483. }
  484. }
  485. pNode = &mBSPNodes[traverseIndex];
  486. }
  487. }
  488. //--------------------------------------------------------------------------
  489. static void itrClipToPlane(Point3F* points, U32& rNumPoints, const PlaneF& rPlane)
  490. {
  491. S32 start = -1;
  492. for(U32 i = 0; i < rNumPoints; i++)
  493. {
  494. if(rPlane.whichSide(points[i]) == PlaneF::Front)
  495. {
  496. start = i;
  497. break;
  498. }
  499. }
  500. // Nothing was in front of the plane...
  501. if(start == -1)
  502. {
  503. rNumPoints = 0;
  504. return;
  505. }
  506. static Point3F finalPoints[128];
  507. U32 numFinalPoints = 0;
  508. U32 baseStart = start;
  509. U32 end = (start + 1) % rNumPoints;
  510. while(end != baseStart)
  511. {
  512. const Point3F& rStartPoint = points[start];
  513. const Point3F& rEndPoint = points[end];
  514. PlaneF::Side fSide = rPlane.whichSide(rStartPoint);
  515. PlaneF::Side eSide = rPlane.whichSide(rEndPoint);
  516. S32 code = fSide * 3 + eSide;
  517. switch(code)
  518. {
  519. case 4: // f f
  520. case 3: // f o
  521. case 1: // o f
  522. case 0: // o o
  523. // No Clipping required
  524. finalPoints[numFinalPoints++] = points[start];
  525. start = end;
  526. end = (end + 1) % rNumPoints;
  527. break;
  528. case 2: // f b
  529. {
  530. // In this case, we emit the front point, Insert the intersection,
  531. // and advancing to point to first point that is in front or on...
  532. //
  533. finalPoints[numFinalPoints++] = points[start];
  534. Point3F vector = rEndPoint - rStartPoint;
  535. F32 t = -(rPlane.distToPlane(rStartPoint) / mDot(rPlane, vector));
  536. Point3F intersection = rStartPoint + (vector * t);
  537. finalPoints[numFinalPoints++] = intersection;
  538. U32 endSeek = (end + 1) % rNumPoints;
  539. while(rPlane.whichSide(points[endSeek]) == PlaneF::Back)
  540. endSeek = (endSeek + 1) % rNumPoints;
  541. end = endSeek;
  542. start = (end + (rNumPoints - 1)) % rNumPoints;
  543. const Point3F& rNewStartPoint = points[start];
  544. const Point3F& rNewEndPoint = points[end];
  545. vector = rNewEndPoint - rNewStartPoint;
  546. t = -(rPlane.distToPlane(rNewStartPoint) / mDot(rPlane, vector));
  547. intersection = rNewStartPoint + (vector * t);
  548. points[start] = intersection;
  549. }
  550. break;
  551. case -1: // o b
  552. {
  553. // In this case, we emit the front point, and advance to point to first
  554. // point that is in front or on...
  555. //
  556. finalPoints[numFinalPoints++] = points[start];
  557. U32 endSeek = (end + 1) % rNumPoints;
  558. while(rPlane.whichSide(points[endSeek]) == PlaneF::Back)
  559. endSeek = (endSeek + 1) % rNumPoints;
  560. end = endSeek;
  561. start = (end + (rNumPoints - 1)) % rNumPoints;
  562. const Point3F& rNewStartPoint = points[start];
  563. const Point3F& rNewEndPoint = points[end];
  564. Point3F vector = rNewEndPoint - rNewStartPoint;
  565. F32 t = -(rPlane.distToPlane(rNewStartPoint) / mDot(rPlane, vector));
  566. Point3F intersection = rNewStartPoint + (vector * t);
  567. points[start] = intersection;
  568. }
  569. break;
  570. case -2: // b f
  571. case -3: // b o
  572. case -4: // b b
  573. // In the algorithm used here, this should never happen...
  574. AssertISV(false, "CSGPlane::clipWindingToPlaneFront: error in polygon clipper");
  575. break;
  576. default:
  577. AssertFatal(false, "CSGPlane::clipWindingToPlaneFront: bad outcode");
  578. break;
  579. }
  580. }
  581. // Emit the last point.
  582. finalPoints[numFinalPoints++] = points[start];
  583. AssertFatal(numFinalPoints >= 3, avar("Error, this shouldn't happen! Invalid winding in itrClipToPlane: %d", numFinalPoints));
  584. // Copy the new rWinding, and we're set!
  585. //
  586. dMemcpy(points, finalPoints, numFinalPoints * sizeof(Point3F));
  587. rNumPoints = numFinalPoints;
  588. AssertISV(rNumPoints <= 128, "Increase maxWindingPoints. Talk to DMoore");
  589. }
  590. bool Interior::projectClipAndBoundFan(U32 fanIndex, F64* pResult)
  591. {
  592. const TriFan& rFan = mWindingIndices[fanIndex];
  593. static Point3F windingPoints[128];
  594. U32 numPoints = rFan.windingCount;
  595. U32 i;
  596. for(i = 0; i < numPoints; i++)
  597. windingPoints[i] = mPoints[mWindings[rFan.windingStart + i]].point;
  598. itrClipToPlane(windingPoints, numPoints, sgOSPlaneFar);
  599. if(numPoints != 0)
  600. itrClipToPlane(windingPoints, numPoints, sgOSPlaneXMin);
  601. if(numPoints != 0)
  602. itrClipToPlane(windingPoints, numPoints, sgOSPlaneXMax);
  603. if(numPoints != 0)
  604. itrClipToPlane(windingPoints, numPoints, sgOSPlaneYMin);
  605. if(numPoints != 0)
  606. itrClipToPlane(windingPoints, numPoints, sgOSPlaneYMax);
  607. if(numPoints == 0)
  608. {
  609. pResult[0] =
  610. pResult[1] =
  611. pResult[2] =
  612. pResult[3] = 0.0f;
  613. return false;
  614. }
  615. F32 minX = 1e10;
  616. F32 maxX = -1e10;
  617. F32 minY = 1e10;
  618. F32 maxY = -1e10;
  619. static Point4F projPoints[128];
  620. for(i = 0; i < numPoints; i++)
  621. {
  622. projPoints[i].set(windingPoints[i].x, windingPoints[i].y, windingPoints[i].z, 1.0);
  623. sgProjMatrix.mul(projPoints[i]);
  624. AssertFatal(projPoints[i].w != 0.0, "Error, that's bad!");
  625. projPoints[i].x /= projPoints[i].w;
  626. projPoints[i].y /= projPoints[i].w;
  627. if(projPoints[i].x < minX)
  628. minX = projPoints[i].x;
  629. if(projPoints[i].x > maxX)
  630. maxX = projPoints[i].x;
  631. if(projPoints[i].y < minY)
  632. minY = projPoints[i].y;
  633. if(projPoints[i].y > maxY)
  634. maxY = projPoints[i].y;
  635. }
  636. if(minX < -1.0f) minX = -1.0f;
  637. if(minY < -1.0f) minY = -1.0f;
  638. if(maxX > 1.0f) maxX = 1.0f;
  639. if(maxY > 1.0f) maxY = 1.0f;
  640. pResult[0] = minX;
  641. pResult[1] = minY;
  642. pResult[2] = maxX;
  643. pResult[3] = maxY;
  644. return true;
  645. }
  646. void Interior::createZoneRectVectors()
  647. {
  648. sgZoneRects.setSize(mZones.size());
  649. for(U32 i = 0; i < mZones.size(); i++)
  650. sgZoneRects[i].active = false;
  651. }
  652. void Interior::destroyZoneRectVectors()
  653. {
  654. }
  655. void Interior::traverseZone(const RectD* inputRects, const U32 numInputRects, U32 currZone, Vector<U32>& zoneStack)
  656. {
  657. PROFILE_START(InteriorTraverseZone);
  658. // First, we push onto our rect list all the inputRects...
  659. insertZoneRects(sgZoneRects[currZone], inputRects, numInputRects);
  660. // A portal is a valid traversal if the camera point is on the
  661. // same side of it's plane as the zone. It must then pass the
  662. // clip/project test.
  663. U32 i;
  664. const Zone& rZone = mZones[currZone];
  665. for(i = rZone.portalStart; i < U32(rZone.portalStart + rZone.portalCount); i++)
  666. {
  667. const Portal& rPortal = mPortals[mZonePortalList[i]];
  668. AssertFatal(U32(rPortal.zoneFront) == currZone || U32(rPortal.zoneBack) == currZone,
  669. "Portal doesn't reference this zone?");
  670. S32 camSide = getPlane(rPortal.planeIndex).whichSide(sgCamPoint);
  671. if(planeIsFlipped(rPortal.planeIndex))
  672. camSide = -camSide;
  673. S32 zoneSide = (U32(rPortal.zoneFront) == currZone) ? 1 : -1;
  674. U16 otherZone = (U32(rPortal.zoneFront) == currZone) ? rPortal.zoneBack : rPortal.zoneFront;
  675. // Make sure this isn't a free floating portal...
  676. if(otherZone == currZone)
  677. continue;
  678. // Make sure we haven't encountered this zone already in this traversal
  679. bool onStack = false;
  680. for(U32 i = 0; i < zoneStack.size(); i++)
  681. {
  682. if(otherZone == zoneStack[i])
  683. {
  684. onStack = true;
  685. break;
  686. }
  687. }
  688. if(onStack == true)
  689. continue;
  690. if(camSide == zoneSide)
  691. {
  692. // Can traverse. Note: special case PlaneF::On
  693. // here to prevent possible w == 0 problems and infinite recursion
  694. // Vector<RectD> newRects;
  695. // VECTOR_SET_ASSOCIATION(newRects);
  696. // We're abusing the heck out of the allocator here.
  697. U32 waterMark = FrameAllocator::getWaterMark();
  698. RectD* newRects = (RectD*)FrameAllocator::alloc(1);
  699. U32 numNewRects = 0;
  700. for(S32 j = 0; j < rPortal.triFanCount; j++)
  701. {
  702. F64 result[4];
  703. if(projectClipAndBoundFan(rPortal.triFanStart + j, result))
  704. {
  705. // Have a good rect from this.
  706. RectD possible = convertToRectD(result);
  707. for(U32 k = 0; k < numInputRects; k++)
  708. {
  709. RectD copy = possible;
  710. if(copy.intersect(inputRects[k]))
  711. newRects[numNewRects++] = copy;
  712. }
  713. }
  714. }
  715. if(numNewRects != 0)
  716. {
  717. FrameAllocator::alloc((sizeof(RectD) * numNewRects) - 1);
  718. U32 prevStackSize = zoneStack.size();
  719. zoneStack.push_back(currZone);
  720. traverseZone(newRects, numNewRects, otherZone, zoneStack);
  721. zoneStack.pop_back();
  722. AssertFatal(zoneStack.size() == prevStackSize, "Error, stack size changed!");
  723. }
  724. FrameAllocator::setWaterMark(waterMark);
  725. }
  726. else if(camSide == PlaneF::On)
  727. {
  728. U32 waterMark = FrameAllocator::getWaterMark();
  729. RectD* newRects = (RectD*)FrameAllocator::alloc(numInputRects * sizeof(RectD));
  730. dMemcpy(newRects, inputRects, sizeof(RectD) * numInputRects);
  731. U32 prevStackSize = zoneStack.size();
  732. zoneStack.push_back(currZone);
  733. traverseZone(newRects, numInputRects, otherZone, zoneStack);
  734. zoneStack.pop_back();
  735. AssertFatal(zoneStack.size() == prevStackSize, "Error, stack size changed!");
  736. FrameAllocator::setWaterMark(waterMark);
  737. }
  738. }
  739. PROFILE_END();
  740. }
  741. void Interior::zoneTraversal(S32 baseZone, const bool flipClip)
  742. {
  743. PROFILE_START(InteriorZoneTraversal);
  744. // If we're in solid, render everything...
  745. if(baseZone == -1)
  746. {
  747. for(U32 i = 0; i < mZones.size(); i++)
  748. {
  749. sgZoneRenderInfo[i].render = true;
  750. sgZoneRenderInfo[i].frustum[0] = sgStoredFrustum[0];
  751. sgZoneRenderInfo[i].frustum[1] = sgStoredFrustum[1];
  752. sgZoneRenderInfo[i].frustum[2] = sgStoredFrustum[2];
  753. sgZoneRenderInfo[i].frustum[3] = sgStoredFrustum[3];
  754. sgZoneRenderInfo[i].viewport = sgStoredViewport;
  755. }
  756. PROFILE_END();
  757. return;
  758. }
  759. // Otherwise, we're going to have to do some work...
  760. createZoneRectVectors();
  761. U32 i;
  762. for(i = 0; i < mZones.size(); i++)
  763. sgZoneRenderInfo[i].render = false;
  764. // Create the object space clipping planes...
  765. sgComputeOSFrustumPlanes(sgStoredFrustum,
  766. sgWSToOSMatrix,
  767. sgCamPoint,
  768. sgOSPlaneFar,
  769. sgOSPlaneXMin,
  770. sgOSPlaneXMax,
  771. sgOSPlaneYMin,
  772. sgOSPlaneYMax);
  773. if(flipClip == true)
  774. {
  775. sgOSPlaneXMin.neg();
  776. sgOSPlaneXMax.neg();
  777. sgOSPlaneYMin.neg();
  778. sgOSPlaneYMax.neg();
  779. }
  780. // First, the current zone gets the full clipRect, and marked as rendering...
  781. static const F64 fullResult[4] = { -1, -1, 1, 1};
  782. static Vector<U32> zoneStack;
  783. zoneStack.clear();
  784. VECTOR_SET_ASSOCIATION(zoneStack);
  785. RectD baseRect = convertToRectD(fullResult);
  786. traverseZone(&baseRect, 1, baseZone, zoneStack);
  787. for(i = 0; i < mZones.size(); i++)
  788. {
  789. if(sgZoneRects[i].active == true)
  790. {
  791. sgZoneRenderInfo[i].render = true;
  792. convertToFrustum(sgZoneRenderInfo[i], sgZoneRects[i].rect);
  793. }
  794. }
  795. PROFILE_END();
  796. }
  797. void mergeSurfaceVectors(const U16* from0,
  798. const U32 size0,
  799. const U16* from1,
  800. const U32 size1,
  801. U16* output,
  802. U32* outputSize)
  803. {
  804. U32 pos0 = 0;
  805. U32 pos1 = 0;
  806. U32 outputCount = 0;
  807. while(pos0 < size0 && pos1 < size1)
  808. {
  809. if(from0[pos0] < from1[pos1])
  810. {
  811. output[outputCount++] = from0[pos0++];
  812. }
  813. else if(from0[pos0] == from1[pos1])
  814. {
  815. // Equal, output one, and inc both counts
  816. output[outputCount++] = from0[pos0++];
  817. pos1++;
  818. }
  819. else
  820. {
  821. output[outputCount++] = from1[pos1++];
  822. }
  823. }
  824. AssertFatal(pos0 == size0 || pos1 == size1, "Error, one of these must have reached the end!");
  825. // Copy the dregs...
  826. if(pos0 != size0)
  827. {
  828. dMemcpy(&output[outputCount], &from0[pos0], sizeof(U16) * (size0 - pos0));
  829. outputCount += size0 - pos0;
  830. }
  831. else if(pos1 != size1)
  832. {
  833. dMemcpy(&output[outputCount], &from1[pos1], sizeof(U16) * (size1 - pos1));
  834. outputCount += size1 - pos1;
  835. }
  836. *outputSize = outputCount;
  837. }
  838. // Remove any collision hulls, interval trees, etc...
  839. //
  840. void Interior::purgeLODData()
  841. {
  842. mConvexHulls.clear();
  843. mHullIndices.clear();
  844. mHullEmitStringIndices.clear();
  845. mHullSurfaceIndices.clear();
  846. mCoordBinIndices.clear();
  847. mConvexHullEmitStrings.clear();
  848. for(U32 i = 0; i < NumCoordBins * NumCoordBins; i++)
  849. {
  850. mCoordBins[i].binStart = 0;
  851. mCoordBins[i].binCount = 0;
  852. }
  853. }
  854. // Build an OptimizedPolyList that represents this Interior's mesh
  855. void Interior::buildExportPolyList(OptimizedPolyList& polys, MatrixF* mat, Point3F* scale)
  856. {
  857. MatrixF saveMat;
  858. Point3F saveScale;
  859. polys.getTransform(&saveMat, &saveScale);
  860. if (mat)
  861. {
  862. if (scale)
  863. polys.setTransform(mat, *scale);
  864. else
  865. polys.setTransform(mat, Point3F(1.0f, 1.0f, 1.0f));
  866. }
  867. // Create one TSMesh per zone
  868. for (U32 i = 0; i < mZones.size(); i++)
  869. {
  870. const Interior::Zone& zone = mZones[i];
  871. // Gather some data
  872. for (U32 j = 0; j < zone.surfaceCount; j++)
  873. {
  874. U32 sdx = mZoneSurfaces[zone.surfaceStart + j];
  875. const Interior::Surface& surface = mSurfaces[sdx];
  876. // Snag the MaterialInstance
  877. BaseMatInstance *matInst = mMaterialList->getMaterialInst( surface.textureIndex );
  878. // Start a poly
  879. polys.begin(matInst, j, OptimizedPolyList::TriangleStrip);
  880. // Set its plane
  881. PlaneF plane = getFlippedPlane(surface.planeIndex);
  882. polys.plane(plane);
  883. // Get its texGen so that we can calculate uvs
  884. Interior::TexGenPlanes texGens = mTexGenEQs[surface.texGenIndex];
  885. texGens.planeY.invert();
  886. // Loop through and add the verts and uvs
  887. for (U32 k = 0; k < surface.windingCount; k++)
  888. {
  889. // Get our point
  890. U32 vdx = mWindings[surface.windingStart + k];
  891. const Point3F& pt = mPoints[vdx].point;
  892. // Get our uv
  893. Point2F uv;
  894. uv.x = texGens.planeX.distToPlane(pt);
  895. uv.y = texGens.planeY.distToPlane(pt);
  896. Point3F normal = getPointNormal(sdx, k);
  897. polys.vertex(pt, normal, uv);
  898. }
  899. polys.end();
  900. }
  901. }
  902. polys.setTransform(&saveMat, saveScale);
  903. }
  904. struct TempProcSurface
  905. {
  906. U32 numPoints;
  907. U32 pointIndices[32];
  908. U16 planeIndex;
  909. U8 mask;
  910. };
  911. struct PlaneGrouping
  912. {
  913. U32 numPlanes;
  914. U16 planeIndices[32];
  915. U8 mask;
  916. };
  917. //--------------------------------------------------------------------------
  918. void Interior::processHullPolyLists()
  919. {
  920. Vector<U16> planeIndices(256, __FILE__, __LINE__);
  921. Vector<U32> pointIndices(256, __FILE__, __LINE__);
  922. Vector<U8> pointMasks(256, __FILE__, __LINE__);
  923. Vector<U8> planeMasks(256, __FILE__, __LINE__);
  924. Vector<TempProcSurface> tempSurfaces(128, __FILE__, __LINE__);
  925. Vector<PlaneGrouping> planeGroups(32, __FILE__, __LINE__);
  926. // Reserve space in the vectors
  927. {
  928. mPolyListStrings.setSize(0);
  929. mPolyListStrings.reserve(128 << 10);
  930. mPolyListPoints.setSize(0);
  931. mPolyListPoints.reserve(32 << 10);
  932. mPolyListPlanes.setSize(0);
  933. mPolyListPlanes.reserve(16 << 10);
  934. }
  935. for(U32 i = 0; i < mConvexHulls.size(); i++)
  936. {
  937. U32 j, k, l, m;
  938. ConvexHull& rHull = mConvexHulls[i];
  939. planeIndices.setSize(0);
  940. pointIndices.setSize(0);
  941. tempSurfaces.setSize(0);
  942. // Extract all the surfaces from this hull into our temporary processing format
  943. {
  944. for(j = 0; j < rHull.surfaceCount; j++)
  945. {
  946. tempSurfaces.increment();
  947. TempProcSurface& temp = tempSurfaces.last();
  948. U32 surfaceIndex = mHullSurfaceIndices[j + rHull.surfaceStart];
  949. if(isNullSurfaceIndex(surfaceIndex))
  950. {
  951. const NullSurface& rSurface = mNullSurfaces[getNullSurfaceIndex(surfaceIndex)];
  952. temp.planeIndex = rSurface.planeIndex;
  953. temp.numPoints = rSurface.windingCount;
  954. for(k = 0; k < rSurface.windingCount; k++)
  955. temp.pointIndices[k] = mWindings[rSurface.windingStart + k];
  956. }
  957. else
  958. {
  959. const Surface& rSurface = mSurfaces[surfaceIndex];
  960. temp.planeIndex = rSurface.planeIndex;
  961. collisionFanFromSurface(rSurface, temp.pointIndices, &temp.numPoints);
  962. }
  963. }
  964. }
  965. // First order of business: extract all unique planes and points from
  966. // the list of surfaces...
  967. {
  968. for(j = 0; j < tempSurfaces.size(); j++)
  969. {
  970. const TempProcSurface& rSurface = tempSurfaces[j];
  971. bool found = false;
  972. for(k = 0; k < planeIndices.size() && !found; k++)
  973. {
  974. if(rSurface.planeIndex == planeIndices[k])
  975. found = true;
  976. }
  977. if(!found)
  978. planeIndices.push_back(rSurface.planeIndex);
  979. for(k = 0; k < rSurface.numPoints; k++)
  980. {
  981. found = false;
  982. for(l = 0; l < pointIndices.size(); l++)
  983. {
  984. if(pointIndices[l] == rSurface.pointIndices[k])
  985. found = true;
  986. }
  987. if(!found)
  988. pointIndices.push_back(rSurface.pointIndices[k]);
  989. }
  990. }
  991. }
  992. // Now that we have all the unique points and planes, remap the surfaces in
  993. // terms of the offsets into the unique point list...
  994. {
  995. for(j = 0; j < tempSurfaces.size(); j++)
  996. {
  997. TempProcSurface& rSurface = tempSurfaces[j];
  998. // Points
  999. for(k = 0; k < rSurface.numPoints; k++)
  1000. {
  1001. bool found = false;
  1002. for(l = 0; l < pointIndices.size(); l++)
  1003. {
  1004. if(pointIndices[l] == rSurface.pointIndices[k])
  1005. {
  1006. rSurface.pointIndices[k] = l;
  1007. found = true;
  1008. break;
  1009. }
  1010. }
  1011. AssertISV(found, "Error remapping point indices in interior collision processing");
  1012. }
  1013. }
  1014. }
  1015. // Ok, at this point, we have a list of unique points, unique planes, and the
  1016. // surfaces all remapped in those terms. We need to check our error conditions
  1017. // that will make sure that we can properly encode this hull:
  1018. {
  1019. AssertISV(planeIndices.size() < 256, "Error, > 256 planes on an interior hull");
  1020. AssertISV(pointIndices.size() < 63356, "Error, > 65536 points on an interior hull");
  1021. AssertISV(tempSurfaces.size() < 256, "Error, > 256 surfaces on an interior hull");
  1022. }
  1023. // Now we group the planes together, and merge the closest groups until we're left
  1024. // with <= 8 groups
  1025. {
  1026. planeGroups.setSize(planeIndices.size());
  1027. for(j = 0; j < planeIndices.size(); j++)
  1028. {
  1029. planeGroups[j].numPlanes = 1;
  1030. planeGroups[j].planeIndices[0] = planeIndices[j];
  1031. }
  1032. while(planeGroups.size() > 8)
  1033. {
  1034. // Find the two closest groups. If mdp(i, j) is the value of the
  1035. // largest pairwise dot product that can be computed from the vectors
  1036. // of group i, and group j, then the closest group pair is the one
  1037. // with the smallest value of mdp.
  1038. F32 currmin = 2;
  1039. S32 firstGroup = -1;
  1040. S32 secondGroup = -1;
  1041. for(j = 0; j < planeGroups.size(); j++)
  1042. {
  1043. PlaneGrouping& first = planeGroups[j];
  1044. for(k = j + 1; k < planeGroups.size(); k++)
  1045. {
  1046. PlaneGrouping& second = planeGroups[k];
  1047. F32 max = -2;
  1048. for(l = 0; l < first.numPlanes; l++)
  1049. {
  1050. for(m = 0; m < second.numPlanes; m++)
  1051. {
  1052. Point3F firstNormal = getPlane(first.planeIndices[l]);
  1053. if(planeIsFlipped(first.planeIndices[l]))
  1054. firstNormal.neg();
  1055. Point3F secondNormal = getPlane(second.planeIndices[m]);
  1056. if(planeIsFlipped(second.planeIndices[m]))
  1057. secondNormal.neg();
  1058. F32 dot = mDot(firstNormal, secondNormal);
  1059. if(dot > max)
  1060. max = dot;
  1061. }
  1062. }
  1063. if(max < currmin)
  1064. {
  1065. currmin = max;
  1066. firstGroup = j;
  1067. secondGroup = k;
  1068. }
  1069. }
  1070. }
  1071. AssertFatal(firstGroup != -1 && secondGroup != -1, "Error, unable to find a suitable pairing?");
  1072. // Merge first and second
  1073. PlaneGrouping& to = planeGroups[firstGroup];
  1074. PlaneGrouping& from = planeGroups[secondGroup];
  1075. while(from.numPlanes != 0)
  1076. {
  1077. to.planeIndices[to.numPlanes++] = from.planeIndices[from.numPlanes - 1];
  1078. from.numPlanes--;
  1079. }
  1080. // And remove the merged group
  1081. planeGroups.erase(secondGroup);
  1082. }
  1083. AssertFatal(planeGroups.size() <= 8, "Error, too many plane groupings!");
  1084. // Assign a mask to each of the plane groupings
  1085. for(j = 0; j < planeGroups.size(); j++)
  1086. planeGroups[j].mask = (1 << j);
  1087. }
  1088. // Now, assign the mask to each of the temp polys
  1089. {
  1090. for(j = 0; j < tempSurfaces.size(); j++)
  1091. {
  1092. bool assigned = false;
  1093. for(k = 0; k < planeGroups.size() && !assigned; k++)
  1094. {
  1095. for(l = 0; l < planeGroups[k].numPlanes; l++)
  1096. {
  1097. if(planeGroups[k].planeIndices[l] == tempSurfaces[j].planeIndex)
  1098. {
  1099. tempSurfaces[j].mask = planeGroups[k].mask;
  1100. assigned = true;
  1101. break;
  1102. }
  1103. }
  1104. }
  1105. AssertFatal(assigned, "Error, missed a plane somewhere in the hull poly list!");
  1106. }
  1107. }
  1108. // Copy the appropriate group mask to the plane masks
  1109. {
  1110. planeMasks.setSize(planeIndices.size());
  1111. dMemset(planeMasks.address(), 0, planeMasks.size() * sizeof(U8));
  1112. for(j = 0; j < planeIndices.size(); j++)
  1113. {
  1114. bool found = false;
  1115. for(k = 0; k < planeGroups.size() && !found; k++)
  1116. {
  1117. for(l = 0; l < planeGroups[k].numPlanes; l++)
  1118. {
  1119. if(planeGroups[k].planeIndices[l] == planeIndices[j])
  1120. {
  1121. planeMasks[j] = planeGroups[k].mask;
  1122. found = true;
  1123. break;
  1124. }
  1125. }
  1126. }
  1127. AssertFatal(planeMasks[j] != 0, "Error, missing mask for plane!");
  1128. }
  1129. }
  1130. // And whip through the points, constructing the total mask for that point
  1131. {
  1132. pointMasks.setSize(pointIndices.size());
  1133. dMemset(pointMasks.address(), 0, pointMasks.size() * sizeof(U8));
  1134. for(j = 0; j < pointIndices.size(); j++)
  1135. {
  1136. for(k = 0; k < tempSurfaces.size(); k++)
  1137. {
  1138. for(l = 0; l < tempSurfaces[k].numPoints; l++)
  1139. {
  1140. if(tempSurfaces[k].pointIndices[l] == j)
  1141. {
  1142. pointMasks[j] |= tempSurfaces[k].mask;
  1143. break;
  1144. }
  1145. }
  1146. }
  1147. AssertFatal(pointMasks[j] != 0, "Error, point must exist in at least one surface!");
  1148. }
  1149. }
  1150. // Create the emit strings, and we're done!
  1151. {
  1152. // Set the range of planes
  1153. rHull.polyListPlaneStart = mPolyListPlanes.size();
  1154. mPolyListPlanes.setSize(rHull.polyListPlaneStart + planeIndices.size());
  1155. for(j = 0; j < planeIndices.size(); j++)
  1156. mPolyListPlanes[j + rHull.polyListPlaneStart] = planeIndices[j];
  1157. // Set the range of points
  1158. rHull.polyListPointStart = mPolyListPoints.size();
  1159. mPolyListPoints.setSize(rHull.polyListPointStart + pointIndices.size());
  1160. for(j = 0; j < pointIndices.size(); j++)
  1161. mPolyListPoints[j + rHull.polyListPointStart] = pointIndices[j];
  1162. // Now the emit string. The emit string goes like: (all fields are bytes)
  1163. // NumPlanes (PLMask) * NumPlanes
  1164. // NumPointsHi NumPointsLo (PtMask) * NumPoints
  1165. // NumSurfaces
  1166. // (NumPoints SurfaceMask PlOffset (PtOffsetHi PtOffsetLo) * NumPoints) * NumSurfaces
  1167. //
  1168. U32 stringLen = 1 + planeIndices.size();
  1169. stringLen += 2 + pointIndices.size();
  1170. stringLen += 1;
  1171. for(j = 0; j < tempSurfaces.size(); j++)
  1172. stringLen += 1 + 1 + 1 + (tempSurfaces[j].numPoints * 2);
  1173. rHull.polyListStringStart = mPolyListStrings.size();
  1174. mPolyListStrings.setSize(rHull.polyListStringStart + stringLen);
  1175. U8* pString = &mPolyListStrings[rHull.polyListStringStart];
  1176. U32 currPos = 0;
  1177. // Planes
  1178. pString[currPos++] = planeIndices.size();
  1179. for(j = 0; j < planeIndices.size(); j++)
  1180. pString[currPos++] = planeMasks[j];
  1181. // Points
  1182. pString[currPos++] = (pointIndices.size() >> 8) & 0xFF;
  1183. pString[currPos++] = (pointIndices.size() >> 0) & 0xFF;
  1184. for(j = 0; j < pointIndices.size(); j++)
  1185. pString[currPos++] = pointMasks[j];
  1186. // Surfaces
  1187. pString[currPos++] = tempSurfaces.size();
  1188. for(j = 0; j < tempSurfaces.size(); j++)
  1189. {
  1190. pString[currPos++] = tempSurfaces[j].numPoints;
  1191. pString[currPos++] = tempSurfaces[j].mask;
  1192. bool found = false;
  1193. for(k = 0; k < planeIndices.size(); k++)
  1194. {
  1195. if(planeIndices[k] == tempSurfaces[j].planeIndex)
  1196. {
  1197. pString[currPos++] = k;
  1198. found = true;
  1199. break;
  1200. }
  1201. }
  1202. AssertFatal(found, "Error, missing planeindex!");
  1203. for(k = 0; k < tempSurfaces[j].numPoints; k++)
  1204. {
  1205. pString[currPos++] = (tempSurfaces[j].pointIndices[k] >> 8) & 0xFF;
  1206. pString[currPos++] = (tempSurfaces[j].pointIndices[k] >> 0) & 0xFF;
  1207. }
  1208. }
  1209. AssertFatal(currPos == stringLen, "Error, mismatched string length!");
  1210. }
  1211. } // for (i = 0; i < mConvexHulls.size(); i++)
  1212. // Compact the used vectors
  1213. {
  1214. mPolyListStrings.compact();
  1215. mPolyListPoints.compact();
  1216. mPolyListPlanes.compact();
  1217. }
  1218. }
  1219. //--------------------------------------------------------------------------
  1220. void Interior::processVehicleHullPolyLists()
  1221. {
  1222. Vector<U16> planeIndices(256, __FILE__, __LINE__);
  1223. Vector<U32> pointIndices(256, __FILE__, __LINE__);
  1224. Vector<U8> pointMasks(256, __FILE__, __LINE__);
  1225. Vector<U8> planeMasks(256, __FILE__, __LINE__);
  1226. Vector<TempProcSurface> tempSurfaces(128, __FILE__, __LINE__);
  1227. Vector<PlaneGrouping> planeGroups(32, __FILE__, __LINE__);
  1228. // Reserve space in the vectors
  1229. {
  1230. mVehiclePolyListStrings.setSize(0);
  1231. mVehiclePolyListStrings.reserve(128 << 10);
  1232. mVehiclePolyListPoints.setSize(0);
  1233. mVehiclePolyListPoints.reserve(32 << 10);
  1234. mVehiclePolyListPlanes.setSize(0);
  1235. mVehiclePolyListPlanes.reserve(16 << 10);
  1236. }
  1237. for(U32 i = 0; i < mVehicleConvexHulls.size(); i++)
  1238. {
  1239. U32 j, k, l, m;
  1240. ConvexHull& rHull = mVehicleConvexHulls[i];
  1241. planeIndices.setSize(0);
  1242. pointIndices.setSize(0);
  1243. tempSurfaces.setSize(0);
  1244. // Extract all the surfaces from this hull into our temporary processing format
  1245. {
  1246. for(j = 0; j < rHull.surfaceCount; j++)
  1247. {
  1248. tempSurfaces.increment();
  1249. TempProcSurface& temp = tempSurfaces.last();
  1250. U32 surfaceIndex = mVehicleHullSurfaceIndices[j + rHull.surfaceStart];
  1251. const NullSurface& rSurface = mVehicleNullSurfaces[getVehicleNullSurfaceIndex(surfaceIndex)];
  1252. temp.planeIndex = rSurface.planeIndex;
  1253. temp.numPoints = rSurface.windingCount;
  1254. for(k = 0; k < rSurface.windingCount; k++)
  1255. temp.pointIndices[k] = mVehicleWindings[rSurface.windingStart + k];
  1256. }
  1257. }
  1258. // First order of business: extract all unique planes and points from
  1259. // the list of surfaces...
  1260. {
  1261. for(j = 0; j < tempSurfaces.size(); j++)
  1262. {
  1263. const TempProcSurface& rSurface = tempSurfaces[j];
  1264. bool found = false;
  1265. for(k = 0; k < planeIndices.size() && !found; k++)
  1266. {
  1267. if(rSurface.planeIndex == planeIndices[k])
  1268. found = true;
  1269. }
  1270. if(!found)
  1271. planeIndices.push_back(rSurface.planeIndex);
  1272. for(k = 0; k < rSurface.numPoints; k++)
  1273. {
  1274. found = false;
  1275. for(l = 0; l < pointIndices.size(); l++)
  1276. {
  1277. if(pointIndices[l] == rSurface.pointIndices[k])
  1278. found = true;
  1279. }
  1280. if(!found)
  1281. pointIndices.push_back(rSurface.pointIndices[k]);
  1282. }
  1283. }
  1284. }
  1285. // Now that we have all the unique points and planes, remap the surfaces in
  1286. // terms of the offsets into the unique point list...
  1287. {
  1288. for(j = 0; j < tempSurfaces.size(); j++)
  1289. {
  1290. TempProcSurface& rSurface = tempSurfaces[j];
  1291. // Points
  1292. for(k = 0; k < rSurface.numPoints; k++)
  1293. {
  1294. bool found = false;
  1295. for(l = 0; l < pointIndices.size(); l++)
  1296. {
  1297. if(pointIndices[l] == rSurface.pointIndices[k])
  1298. {
  1299. rSurface.pointIndices[k] = l;
  1300. found = true;
  1301. break;
  1302. }
  1303. }
  1304. AssertISV(found, "Error remapping point indices in interior collision processing");
  1305. }
  1306. }
  1307. }
  1308. // Ok, at this point, we have a list of unique points, unique planes, and the
  1309. // surfaces all remapped in those terms. We need to check our error conditions
  1310. // that will make sure that we can properly encode this hull:
  1311. {
  1312. AssertISV(planeIndices.size() < 256, "Error, > 256 planes on an interior hull");
  1313. AssertISV(pointIndices.size() < 63356, "Error, > 65536 points on an interior hull");
  1314. AssertISV(tempSurfaces.size() < 256, "Error, > 256 surfaces on an interior hull");
  1315. }
  1316. // Now we group the planes together, and merge the closest groups until we're left
  1317. // with <= 8 groups
  1318. {
  1319. planeGroups.setSize(planeIndices.size());
  1320. for(j = 0; j < planeIndices.size(); j++)
  1321. {
  1322. planeGroups[j].numPlanes = 1;
  1323. planeGroups[j].planeIndices[0] = planeIndices[j];
  1324. }
  1325. while(planeGroups.size() > 8)
  1326. {
  1327. // Find the two closest groups. If mdp(i, j) is the value of the
  1328. // largest pairwise dot product that can be computed from the vectors
  1329. // of group i, and group j, then the closest group pair is the one
  1330. // with the smallest value of mdp.
  1331. F32 currmin = 2;
  1332. S32 firstGroup = -1;
  1333. S32 secondGroup = -1;
  1334. for(j = 0; j < planeGroups.size(); j++)
  1335. {
  1336. PlaneGrouping& first = planeGroups[j];
  1337. for(k = j + 1; k < planeGroups.size(); k++)
  1338. {
  1339. PlaneGrouping& second = planeGroups[k];
  1340. F32 max = -2;
  1341. for(l = 0; l < first.numPlanes; l++)
  1342. {
  1343. for(m = 0; m < second.numPlanes; m++)
  1344. {
  1345. Point3F firstNormal = mVehiclePlanes[first.planeIndices[l]];
  1346. Point3F secondNormal = mVehiclePlanes[second.planeIndices[m]];
  1347. F32 dot = mDot(firstNormal, secondNormal);
  1348. if(dot > max)
  1349. max = dot;
  1350. }
  1351. }
  1352. if(max < currmin)
  1353. {
  1354. currmin = max;
  1355. firstGroup = j;
  1356. secondGroup = k;
  1357. }
  1358. }
  1359. }
  1360. AssertFatal(firstGroup != -1 && secondGroup != -1, "Error, unable to find a suitable pairing?");
  1361. // Merge first and second
  1362. PlaneGrouping& to = planeGroups[firstGroup];
  1363. PlaneGrouping& from = planeGroups[secondGroup];
  1364. while(from.numPlanes != 0)
  1365. {
  1366. to.planeIndices[to.numPlanes++] = from.planeIndices[from.numPlanes - 1];
  1367. from.numPlanes--;
  1368. }
  1369. // And remove the merged group
  1370. planeGroups.erase(secondGroup);
  1371. }
  1372. AssertFatal(planeGroups.size() <= 8, "Error, too many plane groupings!");
  1373. // Assign a mask to each of the plane groupings
  1374. for(j = 0; j < planeGroups.size(); j++)
  1375. planeGroups[j].mask = (1 << j);
  1376. }
  1377. // Now, assign the mask to each of the temp polys
  1378. {
  1379. for(j = 0; j < tempSurfaces.size(); j++)
  1380. {
  1381. bool assigned = false;
  1382. for(k = 0; k < planeGroups.size() && !assigned; k++)
  1383. {
  1384. for(l = 0; l < planeGroups[k].numPlanes; l++)
  1385. {
  1386. if(planeGroups[k].planeIndices[l] == tempSurfaces[j].planeIndex)
  1387. {
  1388. tempSurfaces[j].mask = planeGroups[k].mask;
  1389. assigned = true;
  1390. break;
  1391. }
  1392. }
  1393. }
  1394. AssertFatal(assigned, "Error, missed a plane somewhere in the hull poly list!");
  1395. }
  1396. }
  1397. // Copy the appropriate group mask to the plane masks
  1398. {
  1399. planeMasks.setSize(planeIndices.size());
  1400. dMemset(planeMasks.address(), 0, planeMasks.size() * sizeof(U8));
  1401. for(j = 0; j < planeIndices.size(); j++)
  1402. {
  1403. bool found = false;
  1404. for(k = 0; k < planeGroups.size() && !found; k++)
  1405. {
  1406. for(l = 0; l < planeGroups[k].numPlanes; l++)
  1407. {
  1408. if(planeGroups[k].planeIndices[l] == planeIndices[j])
  1409. {
  1410. planeMasks[j] = planeGroups[k].mask;
  1411. found = true;
  1412. break;
  1413. }
  1414. }
  1415. }
  1416. AssertFatal(planeMasks[j] != 0, "Error, missing mask for plane!");
  1417. }
  1418. }
  1419. // And whip through the points, constructing the total mask for that point
  1420. {
  1421. pointMasks.setSize(pointIndices.size());
  1422. dMemset(pointMasks.address(), 0, pointMasks.size() * sizeof(U8));
  1423. for(j = 0; j < pointIndices.size(); j++)
  1424. {
  1425. for(k = 0; k < tempSurfaces.size(); k++)
  1426. {
  1427. for(l = 0; l < tempSurfaces[k].numPoints; l++)
  1428. {
  1429. if(tempSurfaces[k].pointIndices[l] == j)
  1430. {
  1431. pointMasks[j] |= tempSurfaces[k].mask;
  1432. break;
  1433. }
  1434. }
  1435. }
  1436. AssertFatal(pointMasks[j] != 0, "Error, point must exist in at least one surface!");
  1437. }
  1438. }
  1439. // Create the emit strings, and we're done!
  1440. {
  1441. // Set the range of planes
  1442. rHull.polyListPlaneStart = mVehiclePolyListPlanes.size();
  1443. mVehiclePolyListPlanes.setSize(rHull.polyListPlaneStart + planeIndices.size());
  1444. for(j = 0; j < planeIndices.size(); j++)
  1445. mVehiclePolyListPlanes[j + rHull.polyListPlaneStart] = planeIndices[j];
  1446. // Set the range of points
  1447. rHull.polyListPointStart = mVehiclePolyListPoints.size();
  1448. mVehiclePolyListPoints.setSize(rHull.polyListPointStart + pointIndices.size());
  1449. for(j = 0; j < pointIndices.size(); j++)
  1450. mVehiclePolyListPoints[j + rHull.polyListPointStart] = pointIndices[j];
  1451. // Now the emit string. The emit string goes like: (all fields are bytes)
  1452. // NumPlanes (PLMask) * NumPlanes
  1453. // NumPointsHi NumPointsLo (PtMask) * NumPoints
  1454. // NumSurfaces
  1455. // (NumPoints SurfaceMask PlOffset (PtOffsetHi PtOffsetLo) * NumPoints) * NumSurfaces
  1456. //
  1457. U32 stringLen = 1 + planeIndices.size();
  1458. stringLen += 2 + pointIndices.size();
  1459. stringLen += 1;
  1460. for(j = 0; j < tempSurfaces.size(); j++)
  1461. stringLen += 1 + 1 + 1 + (tempSurfaces[j].numPoints * 2);
  1462. rHull.polyListStringStart = mVehiclePolyListStrings.size();
  1463. mVehiclePolyListStrings.setSize(rHull.polyListStringStart + stringLen);
  1464. U8* pString = &mVehiclePolyListStrings[rHull.polyListStringStart];
  1465. U32 currPos = 0;
  1466. // Planes
  1467. pString[currPos++] = planeIndices.size();
  1468. for(j = 0; j < planeIndices.size(); j++)
  1469. pString[currPos++] = planeMasks[j];
  1470. // Points
  1471. pString[currPos++] = (pointIndices.size() >> 8) & 0xFF;
  1472. pString[currPos++] = (pointIndices.size() >> 0) & 0xFF;
  1473. for(j = 0; j < pointIndices.size(); j++)
  1474. pString[currPos++] = pointMasks[j];
  1475. // Surfaces
  1476. pString[currPos++] = tempSurfaces.size();
  1477. for(j = 0; j < tempSurfaces.size(); j++)
  1478. {
  1479. pString[currPos++] = tempSurfaces[j].numPoints;
  1480. pString[currPos++] = tempSurfaces[j].mask;
  1481. bool found = false;
  1482. for(k = 0; k < planeIndices.size(); k++)
  1483. {
  1484. if(planeIndices[k] == tempSurfaces[j].planeIndex)
  1485. {
  1486. pString[currPos++] = k;
  1487. found = true;
  1488. break;
  1489. }
  1490. }
  1491. AssertFatal(found, "Error, missing planeindex!");
  1492. for(k = 0; k < tempSurfaces[j].numPoints; k++)
  1493. {
  1494. pString[currPos++] = (tempSurfaces[j].pointIndices[k] >> 8) & 0xFF;
  1495. pString[currPos++] = (tempSurfaces[j].pointIndices[k] >> 0) & 0xFF;
  1496. }
  1497. }
  1498. AssertFatal(currPos == stringLen, "Error, mismatched string length!");
  1499. }
  1500. } // for (i = 0; i < mConvexHulls.size(); i++)
  1501. // Compact the used vectors
  1502. {
  1503. mVehiclePolyListStrings.compact();
  1504. mVehiclePolyListPoints.compact();
  1505. mVehiclePolyListPlanes.compact();
  1506. }
  1507. }
  1508. //--------------------------------------------------------------------------
  1509. void ZoneVisDeterminer::runFromState(SceneRenderState* state, U32 offset, U32 parentZone)
  1510. {
  1511. mMode = FromState;
  1512. mState = state;
  1513. mZoneRangeOffset = offset;
  1514. mParentZone = parentZone;
  1515. }
  1516. void ZoneVisDeterminer::runFromRects(SceneRenderState* state, U32 offset, U32 parentZone)
  1517. {
  1518. mMode = FromRects;
  1519. mState = state;
  1520. mZoneRangeOffset = offset;
  1521. mParentZone = parentZone;
  1522. }
  1523. bool ZoneVisDeterminer::isZoneVisible(const U32 zone) const
  1524. {
  1525. if(zone == 0)
  1526. return mState->getCullingState().getZoneState(mParentZone).isZoneVisible();
  1527. if(mMode == FromState)
  1528. {
  1529. return mState->getCullingState().getZoneState(zone + mZoneRangeOffset - 1).isZoneVisible();
  1530. }
  1531. else
  1532. {
  1533. return sgZoneRenderInfo[zone].render;
  1534. }
  1535. }
  1536. //--------------------------------------------------------------------------
  1537. // storeSurfaceVerts -
  1538. // Need to store the verts for every surface because the uv mapping changes
  1539. // per vertex per surface.
  1540. //--------------------------------------------------------------------------
  1541. void Interior::storeSurfVerts( Vector<U16> &masterIndexList,
  1542. Vector<VertexBufferTempIndex> &tempIndexList,
  1543. Vector<GFXVertexPNTTB> &verts,
  1544. U32 numIndices,
  1545. Surface &surface,
  1546. U32 surfaceIndex )
  1547. {
  1548. U32 startIndex = tempIndexList.size() - numIndices;
  1549. U32 startVert = verts.size();
  1550. Vector<U32> vertMap;
  1551. for( U32 i=0; i<numIndices; i++ )
  1552. {
  1553. // check if vertex is already stored for this surface
  1554. bool alreadyStored = false;
  1555. for( U32 j=0; j<i; j++ )
  1556. {
  1557. if( tempIndexList[startIndex+i].index == tempIndexList[startIndex+j].index )
  1558. {
  1559. alreadyStored = true;
  1560. break;
  1561. }
  1562. }
  1563. if( alreadyStored )
  1564. {
  1565. for( U32 a=0; a<vertMap.size(); a++ )
  1566. {
  1567. // find which vertex is indexed
  1568. if( vertMap[a] == tempIndexList[startIndex+i].index )
  1569. {
  1570. // store the index
  1571. masterIndexList.push_back( startVert + a );
  1572. break;
  1573. }
  1574. }
  1575. }
  1576. else
  1577. {
  1578. // store the vertex
  1579. GFXVertexPNTTB vert;
  1580. VertexBufferTempIndex &ind = tempIndexList[startIndex+i];
  1581. vert.point = mPoints[ind.index].point;
  1582. vert.normal = ind.normal;
  1583. fillVertex( vert, surface, surfaceIndex );
  1584. verts.push_back( vert );
  1585. // store the index
  1586. masterIndexList.push_back( verts.size() - 1 );
  1587. // maintain mapping of old indices to new indices
  1588. vertMap.push_back( ind.index );
  1589. }
  1590. }
  1591. }
  1592. //--------------------------------------------------------------------------
  1593. // storeRenderNode
  1594. //--------------------------------------------------------------------------
  1595. void Interior::storeRenderNode( RenderNode &node,
  1596. ZoneRNList &RNList,
  1597. Vector<GFXPrimitive> &primInfoList,
  1598. Vector<U16> &indexList,
  1599. Vector<GFXVertexPNTTB> &verts,
  1600. U32 &startIndex,
  1601. U32 &startVert )
  1602. {
  1603. GFXPrimitive pnfo;
  1604. if( !node.matInst )
  1605. {
  1606. String name = mMaterialList->getMaterialName( node.baseTexIndex );
  1607. if (!name.equal("NULL", String::NoCase) &&
  1608. !name.equal("ORIGIN", String::NoCase) &&
  1609. !name.equal("TRIGGER", String::NoCase) &&
  1610. !name.equal("FORCEFIELD", String::NoCase) &&
  1611. !name.equal("EMITTER", String::NoCase) )
  1612. {
  1613. Con::errorf( "material unmapped: %s", name.c_str() );
  1614. }
  1615. node.matInst = MATMGR->getWarningMatInstance();
  1616. }
  1617. // find min index
  1618. pnfo.minIndex = U32(-1);
  1619. for( U32 i=startIndex; i<indexList.size(); i++ )
  1620. {
  1621. if( indexList[i] < pnfo.minIndex )
  1622. {
  1623. pnfo.minIndex = indexList[i];
  1624. }
  1625. }
  1626. pnfo.numPrimitives = (indexList.size() - startIndex) / 3;
  1627. pnfo.startIndex = startIndex;
  1628. pnfo.numVertices = verts.size() - startVert;
  1629. pnfo.type = GFXTriangleList;
  1630. startIndex = indexList.size();
  1631. startVert = verts.size();
  1632. if( pnfo.numPrimitives > 0 )
  1633. {
  1634. primInfoList.push_back( pnfo );
  1635. node.primInfoIndex = primInfoList.size() - 1;
  1636. RNList.renderNodeList.push_back( node );
  1637. }
  1638. }
  1639. //--------------------------------------------------------------------------
  1640. // fill vertex
  1641. //--------------------------------------------------------------------------
  1642. void Interior::fillVertex( GFXVertexPNTTB &vert, Surface &surface, U32 surfaceIndex )
  1643. {
  1644. TexGenPlanes texPlanes = mTexGenEQs[surface.texGenIndex];
  1645. vert.texCoord.x = texPlanes.planeX.x * vert.point.x +
  1646. texPlanes.planeX.y * vert.point.y +
  1647. texPlanes.planeX.z * vert.point.z +
  1648. texPlanes.planeX.d;
  1649. vert.texCoord.y = texPlanes.planeY.x * vert.point.x +
  1650. texPlanes.planeY.y * vert.point.y +
  1651. texPlanes.planeY.z * vert.point.z +
  1652. texPlanes.planeY.d;
  1653. texPlanes = mLMTexGenEQs[surfaceIndex];
  1654. vert.texCoord2.x = texPlanes.planeX.x * vert.point.x +
  1655. texPlanes.planeX.y * vert.point.y +
  1656. texPlanes.planeX.z * vert.point.z +
  1657. texPlanes.planeX.d;
  1658. vert.texCoord2.y = texPlanes.planeY.x * vert.point.x +
  1659. texPlanes.planeY.y * vert.point.y +
  1660. texPlanes.planeY.z * vert.point.z +
  1661. texPlanes.planeY.d;
  1662. // vert normal and N already set
  1663. vert.T = surface.T - vert.normal * mDot(vert.normal, surface.T);
  1664. vert.T.normalize();
  1665. mCross(vert.normal, vert.T, &vert.B);
  1666. vert.B *= (mDot(vert.B, surface.B) < 0.0F) ? -1.0F : 1.0F;
  1667. }
  1668. //--------------------------------------------------------------------------
  1669. // Create vertex (and index) buffers for each zone
  1670. //--------------------------------------------------------------------------
  1671. void Interior::createZoneVBs()
  1672. {
  1673. if( mVertBuff )
  1674. {
  1675. return;
  1676. }
  1677. // create one big-ass vertex buffer to contain all verts
  1678. // drawIndexedPrimitive() calls can render subsets of the big-ass buffer
  1679. Vector<GFXVertexPNTTB> verts;
  1680. Vector<U16> indices;
  1681. U32 startIndex = 0;
  1682. U32 startVert = 0;
  1683. Vector<GFXPrimitive> primInfoList;
  1684. // fill index list first, then fill verts
  1685. for( U32 i=0; i<mZones.size(); i++ )
  1686. {
  1687. ZoneRNList RNList;
  1688. RenderNode node;
  1689. U16 curTexIndex = 0;
  1690. U8 curLightMapIndex = U8(-1);
  1691. Vector<VertexBufferTempIndex> tempIndices;
  1692. tempIndices.setSize(0);
  1693. for( U32 j=0; j<mZones[i].surfaceCount; j++ )
  1694. {
  1695. U32 surfaceIndex = mZoneSurfaces[mZones[i].surfaceStart + j];
  1696. Surface& surface = mSurfaces[ surfaceIndex ];
  1697. U32 *surfIndices = &mWindings[surface.windingStart];
  1698. //surface.VBIndexStart = indices.size();
  1699. //surface.primIndex = primInfoList.size();
  1700. BaseMatInstance *matInst = mMaterialList->getMaterialInst( surface.textureIndex );
  1701. Material* pMat = dynamic_cast<Material*>(matInst->getMaterial());
  1702. if( pMat && pMat->mPlanarReflection ) continue;
  1703. node.exterior = surface.surfaceFlags & SurfaceOutsideVisible;
  1704. // fill in node info on first time through
  1705. if( j==0 )
  1706. {
  1707. node.baseTexIndex = surface.textureIndex;
  1708. node.matInst = matInst;
  1709. curTexIndex = node.baseTexIndex;
  1710. node.lightMapIndex = mNormalLMapIndices[surfaceIndex];
  1711. curLightMapIndex = node.lightMapIndex;
  1712. }
  1713. // check for material change
  1714. if( surface.textureIndex != curTexIndex ||
  1715. mNormalLMapIndices[surfaceIndex] != curLightMapIndex )
  1716. {
  1717. storeRenderNode( node, RNList, primInfoList, indices, verts, startIndex, startVert );
  1718. tempIndices.setSize( 0 );
  1719. // set new material info
  1720. U16 baseTex = surface.textureIndex;
  1721. U8 lmIndex = mNormalLMapIndices[surfaceIndex];
  1722. if( baseTex != curTexIndex )
  1723. {
  1724. node.baseTexIndex = baseTex;
  1725. node.matInst = mMaterialList->getMaterialInst( baseTex );
  1726. }
  1727. else
  1728. {
  1729. node.baseTexIndex = NULL;
  1730. }
  1731. node.lightMapIndex = lmIndex;
  1732. curTexIndex = baseTex;
  1733. curLightMapIndex = lmIndex;
  1734. }
  1735. // NOTE, can put this in storeSurfVerts()
  1736. U32 tempStartIndex = tempIndices.size();
  1737. U32 nPrim = 0;
  1738. U32 last = 2;
  1739. while(last < surface.windingCount)
  1740. {
  1741. // First
  1742. tempIndices.push_back( VertexBufferTempIndex(surfIndices[last-2], getPointNormal(surfaceIndex, last-2)) );
  1743. tempIndices.push_back( VertexBufferTempIndex(surfIndices[last-1], getPointNormal(surfaceIndex, last-1)) );
  1744. tempIndices.push_back( VertexBufferTempIndex(surfIndices[last-0], getPointNormal(surfaceIndex, last)) );
  1745. last++;
  1746. nPrim++;
  1747. if(last == surface.windingCount)
  1748. break;
  1749. // Second
  1750. tempIndices.push_back( VertexBufferTempIndex(surfIndices[last-1], getPointNormal(surfaceIndex, last-1)) );
  1751. tempIndices.push_back( VertexBufferTempIndex(surfIndices[last-2], getPointNormal(surfaceIndex, last-2)) );
  1752. tempIndices.push_back( VertexBufferTempIndex(surfIndices[last-0], getPointNormal(surfaceIndex, last)) );
  1753. last++;
  1754. nPrim++;
  1755. }
  1756. U32 dStartVert = verts.size();
  1757. GFXPrimitive* p = &surface.surfaceInfo;
  1758. p->startIndex = indices.size(); //tempStartIndex;
  1759. // Normal render info
  1760. storeSurfVerts( indices, tempIndices, verts, tempIndices.size() - tempStartIndex,
  1761. surface, surfaceIndex );
  1762. // Debug render info
  1763. p->type = GFXTriangleList;
  1764. p->numVertices = verts.size() - dStartVert;
  1765. p->numPrimitives = nPrim;
  1766. p->minIndex = indices[p->startIndex];
  1767. for (U32 i = p->startIndex; i < p->startIndex + nPrim * 3; i++) {
  1768. if (indices[i] < p->minIndex) {
  1769. p->minIndex = indices[i];
  1770. }
  1771. }
  1772. }
  1773. // store remaining index list
  1774. storeRenderNode( node, RNList, primInfoList, indices, verts, startIndex, startVert );
  1775. mZoneRNList.push_back( RNList );
  1776. }
  1777. // It is possible that we have no zones or have no surfaces in our zones (static meshes only)
  1778. if (verts.size() == 0)
  1779. return;
  1780. // create vertex buffer
  1781. mVertBuff.set(GFX, verts.size(), GFXBufferTypeStatic);
  1782. GFXVertexPNTTB *vbVerts = mVertBuff.lock();
  1783. dMemcpy( vbVerts, verts.address(), verts.size() * sizeof( GFXVertexPNTTB ) );
  1784. mVertBuff.unlock();
  1785. // create primitive buffer
  1786. U16 *ibIndices;
  1787. GFXPrimitive *piInput;
  1788. mPrimBuff.set(GFX, indices.size(), primInfoList.size(), GFXBufferTypeStatic);
  1789. mPrimBuff.lock(&ibIndices, &piInput);
  1790. dMemcpy( ibIndices, indices.address(), indices.size() * sizeof(U16) );
  1791. dMemcpy( piInput, primInfoList.address(), primInfoList.size() * sizeof(GFXPrimitive) );
  1792. mPrimBuff.unlock();
  1793. }
  1794. #define SMALL_FLOAT (1e-12)
  1795. //--------------------------------------------------------------------------
  1796. // Get the texture space matrice for a point on a surface
  1797. //--------------------------------------------------------------------------
  1798. void Interior::getTexMat(U32 surfaceIndex, U32 pointOffset, Point3F& T, Point3F& N, Point3F& B)
  1799. {
  1800. Surface& surface = mSurfaces[surfaceIndex];
  1801. if (mFileVersion >= 11)
  1802. {
  1803. // There is a one-to-one mapping of mWindings and mTexMatIndices
  1804. U32 texMatIndex = mTexMatIndices[surface.windingStart + pointOffset];
  1805. TexMatrix& texMat = mTexMatrices[texMatIndex];
  1806. T = mNormals[texMat.T];
  1807. N = mNormals[texMat.N];
  1808. B = mNormals[texMat.B];
  1809. }
  1810. else
  1811. {
  1812. T = surface.T - surface.N * mDot(surface.N, surface.T);
  1813. N = surface.N;
  1814. mCross(surface.N, T, &B);
  1815. B *= (mDot(B, surface.B) < 0.0F) ? -1.0f : 1.0f;
  1816. }
  1817. return;
  1818. }
  1819. //--------------------------------------------------------------------------
  1820. // Fill in texture space matrices for each surface
  1821. //--------------------------------------------------------------------------
  1822. void Interior::fillSurfaceTexMats()
  1823. {
  1824. for( U32 i=0; i<mSurfaces.size(); i++ )
  1825. {
  1826. Surface &surface = mSurfaces[i];
  1827. const PlaneF & plane = getPlane(surface.planeIndex);
  1828. Point3F planeNorm = plane;
  1829. if( planeIsFlipped( surface.planeIndex ) )
  1830. {
  1831. planeNorm = -planeNorm;
  1832. }
  1833. GFXVertexPNTTB pts[3];
  1834. pts[0].point = mPoints[mWindings[surface.windingStart + 1]].point;
  1835. pts[1].point = mPoints[mWindings[surface.windingStart + 0]].point;
  1836. pts[2].point = mPoints[mWindings[surface.windingStart + 2]].point;
  1837. TexGenPlanes texPlanes = mTexGenEQs[surface.texGenIndex];
  1838. for( U32 j=0; j<3; j++ )
  1839. {
  1840. pts[j].texCoord.x = texPlanes.planeX.x * pts[j].point.x +
  1841. texPlanes.planeX.y * pts[j].point.y +
  1842. texPlanes.planeX.z * pts[j].point.z +
  1843. texPlanes.planeX.d;
  1844. pts[j].texCoord.y = texPlanes.planeY.x * pts[j].point.x +
  1845. texPlanes.planeY.y * pts[j].point.y +
  1846. texPlanes.planeY.z * pts[j].point.z +
  1847. texPlanes.planeY.d;
  1848. }
  1849. Point3F edge1, edge2;
  1850. Point3F cp;
  1851. Point3F S,T,SxT;
  1852. // x, s, t
  1853. edge1.set( pts[1].point.x - pts[0].point.x, pts[1].texCoord.x - pts[0].texCoord.x, pts[1].texCoord.y - pts[0].texCoord.y );
  1854. edge2.set( pts[2].point.x - pts[0].point.x, pts[2].texCoord.x - pts[0].texCoord.x, pts[2].texCoord.y - pts[0].texCoord.y );
  1855. mCross( edge1, edge2, &cp );
  1856. if( fabs(cp.x) > SMALL_FLOAT )
  1857. {
  1858. S.x = -cp.y / cp.x;
  1859. T.x = -cp.z / cp.x;
  1860. }
  1861. edge1.set( pts[1].point.y - pts[0].point.y, pts[1].texCoord.x - pts[0].texCoord.x, pts[1].texCoord.y - pts[0].texCoord.y );
  1862. edge2.set( pts[2].point.y - pts[0].point.y, pts[2].texCoord.x - pts[0].texCoord.x, pts[2].texCoord.y - pts[0].texCoord.y );
  1863. mCross( edge1, edge2, &cp );
  1864. if( fabs(cp.x) > SMALL_FLOAT )
  1865. {
  1866. S.y = -cp.y / cp.x;
  1867. T.y = -cp.z / cp.x;
  1868. }
  1869. edge1.set( pts[1].point.z - pts[0].point.z, pts[1].texCoord.x - pts[0].texCoord.x, pts[1].texCoord.y - pts[0].texCoord.y );
  1870. edge2.set( pts[2].point.z - pts[0].point.z, pts[2].texCoord.x - pts[0].texCoord.x, pts[2].texCoord.y - pts[0].texCoord.y );
  1871. mCross( edge1, edge2, &cp );
  1872. if( fabs(cp.x) > SMALL_FLOAT )
  1873. {
  1874. S.z = -cp.y / cp.x;
  1875. T.z = -cp.z / cp.x;
  1876. }
  1877. S.normalizeSafe();
  1878. T.normalizeSafe();
  1879. mCross( S, T, &SxT );
  1880. if( mDot( SxT, planeNorm ) < 0.0 )
  1881. {
  1882. SxT = -SxT;
  1883. }
  1884. surface.T = S;
  1885. surface.B = T;
  1886. surface.N = SxT;
  1887. surface.normal = planeNorm;
  1888. }
  1889. }
  1890. //--------------------------------------------------------------------------
  1891. // Clone material instances - if a texture (material) exists on both the
  1892. // inside and outside of an interior, it needs to create two material
  1893. // instances - one for the inside, and one for the outside. The reason is
  1894. // that the light direction maps only exist on the inside of the interior.
  1895. //--------------------------------------------------------------------------
  1896. void Interior::cloneMatInstances()
  1897. {
  1898. Vector< BaseMatInstance *> outsideMats;
  1899. Vector< BaseMatInstance *> insideMats;
  1900. // store pointers to mat lists
  1901. for( U32 i=0; i<getNumZones(); i++ )
  1902. {
  1903. for( U32 j=0; j<mZoneRNList[i].renderNodeList.size(); j++ )
  1904. {
  1905. RenderNode &node = mZoneRNList[i].renderNodeList[j];
  1906. if( !node.matInst ) continue;
  1907. if( node.exterior )
  1908. {
  1909. // insert only if it's not already there
  1910. U32 k;
  1911. for( k=0; k<outsideMats.size(); k++ )
  1912. {
  1913. if( node.matInst == outsideMats[k] ) break;
  1914. }
  1915. if( k == outsideMats.size() )
  1916. {
  1917. outsideMats.push_back( node.matInst );
  1918. }
  1919. }
  1920. else
  1921. {
  1922. // insert only if it's not already there
  1923. U32 k;
  1924. for( k=0; k<insideMats.size(); k++ )
  1925. {
  1926. if( node.matInst == insideMats[k] ) break;
  1927. }
  1928. if( k == insideMats.size() )
  1929. {
  1930. insideMats.push_back( node.matInst );
  1931. }
  1932. }
  1933. }
  1934. }
  1935. // for all materials that exist both inside and outside,
  1936. // clone them so they can have separate material instances
  1937. for( U32 i=0; i<outsideMats.size(); i++ )
  1938. {
  1939. for( U32 j=0; j<insideMats.size(); j++ )
  1940. {
  1941. if( outsideMats[i] == insideMats[j] )
  1942. {
  1943. // GFX2_RENDER_MERGE
  1944. Material *mat = dynamic_cast<Material*>(outsideMats[i]->getMaterial());
  1945. if (mat)
  1946. {
  1947. BaseMatInstance *newMat = mat->createMatInstance();
  1948. mMatInstCleanupList.push_back( newMat );
  1949. // go through and find the inside version and replace it
  1950. // with the new one.
  1951. for( U32 k=0; k<getNumZones(); k++ )
  1952. {
  1953. for( U32 l=0; l<mZoneRNList[k].renderNodeList.size(); l++ )
  1954. {
  1955. RenderNode &node = mZoneRNList[k].renderNodeList[l];
  1956. if( !node.exterior )
  1957. {
  1958. if( node.matInst == outsideMats[i] )
  1959. {
  1960. node.matInst = newMat;
  1961. }
  1962. }
  1963. }
  1964. }
  1965. }
  1966. }
  1967. }
  1968. }
  1969. }
  1970. //--------------------------------------------------------------------------
  1971. // Intialize material instances
  1972. //--------------------------------------------------------------------------
  1973. void Interior::initMatInstances()
  1974. {
  1975. mHasTranslucentMaterials = false;
  1976. for( U32 i=0; i<getNumZones(); i++ )
  1977. {
  1978. for( U32 j=0; j<mZoneRNList[i].renderNodeList.size(); j++ )
  1979. {
  1980. RenderNode &node = mZoneRNList[i].renderNodeList[j];
  1981. BaseMatInstance *mat = node.matInst;
  1982. if( mat )
  1983. {
  1984. // Note: We disabled this as it was keeping the prepass lighting
  1985. // from being applied to interiors... this fixes that.
  1986. //fd.features[MFT_RTLighting] = false;
  1987. // If this node has a lightmap index,
  1988. // we need to bind the override features delegate
  1989. // in order to ensure the MFT_LightMap feature
  1990. // is added for the material.
  1991. if ( node.lightMapIndex != (U8)-1 )
  1992. mat->getFeaturesDelegate().bind( &Interior::_enableLightMapFeature );
  1993. mat->init( MATMGR->getDefaultFeatures(), getGFXVertexFormat<GFXVertexPNTTB>());
  1994. // We need to know if we have non-zwrite translucent materials
  1995. // so that we can make the extra renderimage pass for them.
  1996. Material* pMat = dynamic_cast<Material*>(mat->getMaterial());
  1997. if ( pMat )
  1998. mHasTranslucentMaterials |= pMat->mTranslucent && !pMat->mTranslucentZWrite;
  1999. }
  2000. }
  2001. for( U32 j=0; j<mZoneReflectRNList[i].reflectList.size(); j++ )
  2002. {
  2003. ReflectRenderNode &node = mZoneReflectRNList[i].reflectList[j];
  2004. BaseMatInstance *mat = node.matInst;
  2005. if( mat )
  2006. {
  2007. // Note: We disabled this as it was keeping the prepass lighting
  2008. // from being applied to interiors... this fixes that.
  2009. //fd.features[MFT_RTLighting] = false;
  2010. mat->init( MATMGR->getDefaultFeatures(), getGFXVertexFormat<GFXVertexPNTTB>());
  2011. // We need to know if we have non-zwrite translucent materials
  2012. // so that we can make the extra renderimage pass for them.
  2013. Material* pMat = dynamic_cast<Material*>(mat->getMaterial());
  2014. if ( pMat )
  2015. mHasTranslucentMaterials |= pMat->mTranslucent && !pMat->mTranslucentZWrite;
  2016. }
  2017. }
  2018. }
  2019. }
  2020. void Interior::_enableLightMapFeature( ProcessedMaterial *mat,
  2021. U32 stageNum,
  2022. MaterialFeatureData &fd,
  2023. const FeatureSet &features )
  2024. {
  2025. if ( mat->getMaterial() )
  2026. {
  2027. fd.features.addFeature( MFT_LightMap );
  2028. fd.features.removeFeature( MFT_ToneMap );
  2029. }
  2030. }
  2031. //--------------------------------------------------------------------------
  2032. // Create the reflect plane list and the nodes of geometry necessary to
  2033. // render the reflective surfaces.
  2034. //--------------------------------------------------------------------------
  2035. void Interior::createReflectPlanes()
  2036. {
  2037. Vector<GFXVertexPNTTB> verts;
  2038. Vector<GFXPrimitive> primInfoList;
  2039. Vector<U16> indices;
  2040. U32 startIndex = 0;
  2041. U32 startVert = 0;
  2042. for( U32 i=0; i<mZones.size(); i++ )
  2043. {
  2044. ZoneReflectRNList reflectRNList;
  2045. // for each zone:
  2046. // go through list of surfaces, searching for reflection
  2047. for( U32 j=0; j<mZones[i].surfaceCount; j++ )
  2048. {
  2049. U32 surfaceIndex = mZoneSurfaces[mZones[i].surfaceStart + j];
  2050. Surface& surface = mSurfaces[ surfaceIndex ];
  2051. BaseMatInstance *matInst = mMaterialList->getMaterialInst( surface.textureIndex );
  2052. Material* pMat = dynamic_cast<Material*>(matInst->getMaterial());
  2053. if( !pMat || !pMat->mPlanarReflection ) continue;
  2054. U32 *surfIndices = &mWindings[surface.windingStart];
  2055. // create / fill in GFXPrimitve, verts, indices
  2056. // going to need a new render node
  2057. ReflectRenderNode node;
  2058. node.exterior = surface.surfaceFlags & SurfaceOutsideVisible;
  2059. node.matInst = mMaterialList->getMaterialInst( surface.textureIndex );
  2060. node.lightMapIndex = mNormalLMapIndices[surfaceIndex];
  2061. PlaneF plane;
  2062. plane = getPlane( surface.planeIndex );
  2063. if( planeIsFlipped( surface.planeIndex ) )
  2064. {
  2065. plane.x = -plane.x;
  2066. plane.y = -plane.y;
  2067. plane.z = -plane.z;
  2068. plane.d = -plane.d;
  2069. }
  2070. // check if coplanar with existing reflect plane
  2071. //--------------------------------------------------
  2072. S32 rPlaneIdx = -1;
  2073. for( U32 a=0; a<mReflectPlanes.size(); a++ )
  2074. {
  2075. if( fabs( mDot( plane, mReflectPlanes[a] ) ) > 0.999 )
  2076. {
  2077. if( fabs( plane.d - mReflectPlanes[a].d ) < 0.001 )
  2078. {
  2079. rPlaneIdx = a;
  2080. break;
  2081. }
  2082. }
  2083. }
  2084. PlaneF refPlane;
  2085. refPlane = plane;
  2086. if( rPlaneIdx < 0 )
  2087. {
  2088. mReflectPlanes.push_back( refPlane );
  2089. node.reflectPlaneIndex = mReflectPlanes.size() - 1;
  2090. }
  2091. else
  2092. {
  2093. node.reflectPlaneIndex = rPlaneIdx;
  2094. }
  2095. // store the indices for the surface
  2096. //--------------------------------------------------
  2097. Vector<VertexBufferTempIndex> tempIndices;
  2098. tempIndices.setSize( 0 );
  2099. U32 tempStartIndex = tempIndices.size();
  2100. U32 last = 2;
  2101. while(last < surface.windingCount)
  2102. {
  2103. // First
  2104. tempIndices.push_back( VertexBufferTempIndex(surfIndices[last-2], getPointNormal(surfaceIndex, last-2)) );
  2105. tempIndices.push_back( VertexBufferTempIndex(surfIndices[last-1], getPointNormal(surfaceIndex, last-1)) );
  2106. tempIndices.push_back( VertexBufferTempIndex(surfIndices[last-0], getPointNormal(surfaceIndex, last)) );
  2107. last++;
  2108. if(last == surface.windingCount)
  2109. break;
  2110. // Second
  2111. tempIndices.push_back( VertexBufferTempIndex(surfIndices[last-1], getPointNormal(surfaceIndex, last-1)) );
  2112. tempIndices.push_back( VertexBufferTempIndex(surfIndices[last-2], getPointNormal(surfaceIndex, last-2)) );
  2113. tempIndices.push_back( VertexBufferTempIndex(surfIndices[last-0], getPointNormal(surfaceIndex, last)) );
  2114. last++;
  2115. }
  2116. storeSurfVerts( indices, tempIndices, verts, tempIndices.size() - tempStartIndex,
  2117. surface, surfaceIndex );
  2118. // store render node and GFXPrimitive
  2119. // each node is a different reflective surface
  2120. // ---------------------------------------------------
  2121. // find min index
  2122. GFXPrimitive pnfo;
  2123. pnfo.minIndex = U32(-1);
  2124. for( U32 k=startIndex; k<indices.size(); k++ )
  2125. {
  2126. if( indices[k] < pnfo.minIndex )
  2127. {
  2128. pnfo.minIndex = indices[k];
  2129. }
  2130. }
  2131. pnfo.numPrimitives = (indices.size() - startIndex) / 3;
  2132. pnfo.startIndex = startIndex;
  2133. pnfo.numVertices = verts.size() - startVert;
  2134. pnfo.type = GFXTriangleList;
  2135. startIndex = indices.size();
  2136. startVert = verts.size();
  2137. primInfoList.push_back( pnfo );
  2138. node.primInfoIndex = primInfoList.size() - 1;
  2139. reflectRNList.reflectList.push_back( node );
  2140. }
  2141. mZoneReflectRNList.push_back( reflectRNList );
  2142. }
  2143. if( mReflectPlanes.size() )
  2144. {
  2145. // copy verts to buffer
  2146. mReflectVertBuff.set(GFX, verts.size(), GFXBufferTypeStatic);
  2147. GFXVertexPNTTB *vbVerts = mReflectVertBuff.lock();
  2148. dMemcpy( vbVerts, verts.address(), verts.size() * sizeof( GFXVertexPNTTB ) );
  2149. mReflectVertBuff.unlock();
  2150. // create primitive buffer
  2151. U16 *ibIndices;
  2152. GFXPrimitive *piInput;
  2153. mReflectPrimBuff.set(GFX, indices.size(), primInfoList.size(), GFXBufferTypeStatic);
  2154. mReflectPrimBuff.lock(&ibIndices, &piInput);
  2155. dMemcpy( ibIndices, indices.address(), indices.size() * sizeof(U16) );
  2156. dMemcpy( piInput, primInfoList.address(), primInfoList.size() * sizeof(GFXPrimitive) );
  2157. mReflectPrimBuff.unlock();
  2158. }
  2159. }
  2160. void Interior::buildSurfaceZones()
  2161. {
  2162. surfaceZones.clear();
  2163. surfaceZones.setSize(mSurfaces.size());
  2164. for(U32 i=0; i<getSurfaceCount(); i++)
  2165. {
  2166. surfaceZones[i] = -1;
  2167. }
  2168. for(U32 z=0; z<mZones.size(); z++)
  2169. {
  2170. Interior::Zone &zone = mZones[z];
  2171. zone.zoneId = z;
  2172. for(U32 s=zone.surfaceStart; s<(zone.surfaceStart + zone.surfaceCount); s++)
  2173. {
  2174. surfaceZones[mZoneSurfaces[s]] = zone.zoneId - 1;
  2175. }
  2176. }
  2177. }
  2178. const String& Interior::getTargetName( S32 mapToNameIndex ) const
  2179. {
  2180. S32 targetCount = mMaterialList->getMaterialNameList().size();
  2181. if(mapToNameIndex < 0 || mapToNameIndex >= targetCount)
  2182. return String::EmptyString;
  2183. return mMaterialList->getMaterialNameList()[mapToNameIndex];
  2184. }
  2185. S32 Interior::getTargetCount() const
  2186. {
  2187. if(!this)
  2188. return -1;
  2189. return mMaterialList->getMaterialNameList().size();
  2190. }