Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

675 lines
20 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $Workfile: $
  6. // $Date: $
  7. // $NoKeywords: $
  8. //=============================================================================//
  9. #include "disp_vbsp.h"
  10. #include "tier0/dbg.h"
  11. #include "vbsp.h"
  12. #include "mstristrip.h"
  13. #include "writebsp.h"
  14. #include "pacifier.h"
  15. #include "disp_ivp.h"
  16. #include "builddisp.h"
  17. #include "mathlib/vector.h"
  18. // map displacement info -- runs parallel to the dispinfos struct
  19. int nummapdispinfo = 0;
  20. mapdispinfo_t mapdispinfo[MAX_MAP_DISPINFO];
  21. CUtlVector<CCoreDispInfo*> g_CoreDispInfos;
  22. //-----------------------------------------------------------------------------
  23. // Computes the bounds for a disp info
  24. //-----------------------------------------------------------------------------
  25. void ComputeDispInfoBounds( int dispinfo, Vector& mins, Vector& maxs )
  26. {
  27. CDispBox box;
  28. // Get a CCoreDispInfo. All we need is the triangles and lightmap texture coordinates.
  29. mapdispinfo_t *pMapDisp = &mapdispinfo[dispinfo];
  30. CCoreDispInfo coreDispInfo;
  31. DispMapToCoreDispInfo( pMapDisp, &coreDispInfo, NULL, NULL );
  32. GetDispBox( &coreDispInfo, box );
  33. mins = box.m_Min;
  34. maxs = box.m_Max;
  35. }
  36. // Gets the barycentric coordinates of the position on the triangle where the lightmap
  37. // coordinates are equal to lmCoords. This always generates the coordinates but it
  38. // returns false if the point containing them does not lie inside the triangle.
  39. bool GetBarycentricCoordsFromLightmapCoords( Vector2D tri[3], Vector2D const &lmCoords, float bcCoords[3] )
  40. {
  41. GetBarycentricCoords2D( tri[0], tri[1], tri[2], lmCoords, bcCoords );
  42. return
  43. (bcCoords[0] >= 0.0f && bcCoords[0] <= 1.0f) &&
  44. (bcCoords[1] >= 0.0f && bcCoords[1] <= 1.0f) &&
  45. (bcCoords[2] >= 0.0f && bcCoords[2] <= 1.0f);
  46. }
  47. bool FindTriIndexMapByUV( CCoreDispInfo *pCoreDisp, Vector2D const &lmCoords,
  48. int &iTriangle, float flBarycentric[3] )
  49. {
  50. const CPowerInfo *pPowerInfo = GetPowerInfo( pCoreDisp->GetPower() );
  51. // Search all the triangles..
  52. int nTriCount= pCoreDisp->GetTriCount();
  53. for ( int iTri = 0; iTri < nTriCount; ++iTri )
  54. {
  55. unsigned short iVerts[3];
  56. // pCoreDisp->GetTriIndices( iTri, iVerts[0], iVerts[1], iVerts[2] );
  57. CTriInfo *pTri = &pPowerInfo->m_pTriInfos[iTri];
  58. iVerts[0] = pTri->m_Indices[0];
  59. iVerts[1] = pTri->m_Indices[1];
  60. iVerts[2] = pTri->m_Indices[2];
  61. // Get this triangle's UVs.
  62. Vector2D vecUV[3];
  63. for ( int iCoord = 0; iCoord < 3; ++iCoord )
  64. {
  65. pCoreDisp->GetLuxelCoord( 0, iVerts[iCoord], vecUV[iCoord] );
  66. }
  67. // See if the passed-in UVs are in this triangle's UVs.
  68. if( GetBarycentricCoordsFromLightmapCoords( vecUV, lmCoords, flBarycentric ) )
  69. {
  70. iTriangle = iTri;
  71. return true;
  72. }
  73. }
  74. return false;
  75. }
  76. void CalculateLightmapSamplePositions( CCoreDispInfo *pCoreDispInfo, const dface_t *pFace, CUtlVector<unsigned char> &out )
  77. {
  78. int width = pFace->m_LightmapTextureSizeInLuxels[0] + 1;
  79. int height = pFace->m_LightmapTextureSizeInLuxels[1] + 1;
  80. // For each lightmap sample, find the triangle it sits in.
  81. Vector2D lmCoords;
  82. for( int y=0; y < height; y++ )
  83. {
  84. lmCoords.y = y + 0.5f;
  85. for( int x=0; x < width; x++ )
  86. {
  87. lmCoords.x = x + 0.5f;
  88. float flBarycentric[3];
  89. int iTri;
  90. if( FindTriIndexMapByUV( pCoreDispInfo, lmCoords, iTri, flBarycentric ) )
  91. {
  92. if( iTri < 255 )
  93. {
  94. out.AddToTail( iTri );
  95. }
  96. else
  97. {
  98. out.AddToTail( 255 );
  99. out.AddToTail( iTri - 255 );
  100. }
  101. out.AddToTail( (unsigned char)( flBarycentric[0] * 255.9f ) );
  102. out.AddToTail( (unsigned char)( flBarycentric[1] * 255.9f ) );
  103. out.AddToTail( (unsigned char)( flBarycentric[2] * 255.9f ) );
  104. }
  105. else
  106. {
  107. out.AddToTail( 0 );
  108. out.AddToTail( 0 );
  109. out.AddToTail( 0 );
  110. out.AddToTail( 0 );
  111. }
  112. }
  113. }
  114. }
  115. //-----------------------------------------------------------------------------
  116. //-----------------------------------------------------------------------------
  117. int GetDispInfoEntityNum( mapdispinfo_t *pDisp )
  118. {
  119. return pDisp->entitynum;
  120. }
  121. // Setup a CCoreDispInfo given a mapdispinfo_t.
  122. // If pFace is non-NULL, then lightmap texture coordinates will be generated.
  123. void DispMapToCoreDispInfo( mapdispinfo_t *pMapDisp, CCoreDispInfo *pCoreDispInfo, dface_t *pFace, int *pSwappedTexInfos )
  124. {
  125. winding_t *pWinding = pMapDisp->face.originalface->winding;
  126. Assert( pWinding->numpoints == 4 );
  127. //
  128. // set initial surface data
  129. //
  130. CCoreDispSurface *pSurf = pCoreDispInfo->GetSurface();
  131. texinfo_t *pTexInfo = &texinfo[ pMapDisp->face.texinfo ];
  132. Assert( pTexInfo != NULL );
  133. // init material contents
  134. pMapDisp->contents = pMapDisp->face.contents;
  135. if (!(pMapDisp->contents & (ALL_VISIBLE_CONTENTS | CONTENTS_PLAYERCLIP|CONTENTS_MONSTERCLIP) ) )
  136. {
  137. pMapDisp->contents |= CONTENTS_SOLID;
  138. }
  139. pSurf->SetContents( pMapDisp->contents );
  140. // Calculate the lightmap coordinates.
  141. Vector2D tCoords[4] = {Vector2D(0,0),Vector2D(0,1),Vector2D(1,0),Vector2D(1,1)};
  142. if( pFace )
  143. {
  144. Assert( pFace->numedges == 4 );
  145. Vector pt[4];
  146. for( int i=0; i < 4; i++ )
  147. pt[i] = pWinding->p[i];
  148. int zeroOffset[2] = {0,0};
  149. CalcTextureCoordsAtPoints(
  150. pTexInfo->textureVecsTexelsPerWorldUnits,
  151. zeroOffset,
  152. pt,
  153. 4,
  154. tCoords );
  155. }
  156. //
  157. // set face point data ...
  158. //
  159. pSurf->SetPointCount( 4 );
  160. for( int i = 0; i < 4; i++ )
  161. {
  162. // position
  163. pSurf->SetPoint( i, pWinding->p[i] );
  164. pSurf->SetTexCoord( i, tCoords[i] );
  165. }
  166. // reset surface given start info
  167. pSurf->SetPointStart( pMapDisp->startPosition );
  168. pSurf->FindSurfPointStartIndex();
  169. pSurf->AdjustSurfPointData();
  170. // Set the luxel coordinates on the base displacement surface.
  171. Vector vecTmp( pTexInfo->lightmapVecsLuxelsPerWorldUnits[0][0],
  172. pTexInfo->lightmapVecsLuxelsPerWorldUnits[0][1],
  173. pTexInfo->lightmapVecsLuxelsPerWorldUnits[0][2] );
  174. int nLuxelsPerWorldUnit = static_cast<int>( 1.0f / VectorLength( vecTmp ) );
  175. Vector vecU( pTexInfo->lightmapVecsLuxelsPerWorldUnits[0][0],
  176. pTexInfo->lightmapVecsLuxelsPerWorldUnits[0][1],
  177. pTexInfo->lightmapVecsLuxelsPerWorldUnits[0][2] );
  178. Vector vecV( pTexInfo->lightmapVecsLuxelsPerWorldUnits[1][0],
  179. pTexInfo->lightmapVecsLuxelsPerWorldUnits[1][1],
  180. pTexInfo->lightmapVecsLuxelsPerWorldUnits[1][2] );
  181. bool bSwap = pSurf->CalcLuxelCoords( nLuxelsPerWorldUnit, false, vecU, vecV );
  182. // Set the face m_LightmapExtents
  183. if ( pFace )
  184. {
  185. pFace->m_LightmapTextureSizeInLuxels[0] = pSurf->GetLuxelU();
  186. pFace->m_LightmapTextureSizeInLuxels[1] = pSurf->GetLuxelV();
  187. if ( bSwap )
  188. {
  189. if ( pSwappedTexInfos[ pMapDisp->face.texinfo ] < 0 )
  190. {
  191. // Create a new texinfo to hold the swapped data.
  192. // We must do this because other surfaces may want the non-swapped data
  193. // This fixes a lighting bug in d2_prison_08 where many non-displacement surfaces
  194. // were pitch black, in addition to bugs in other maps I bet.
  195. // NOTE: Copy here because adding a texinfo could realloc.
  196. texinfo_t temp = *pTexInfo;
  197. memcpy( temp.lightmapVecsLuxelsPerWorldUnits[0], pTexInfo->lightmapVecsLuxelsPerWorldUnits[1], 4 * sizeof(float) );
  198. memcpy( temp.lightmapVecsLuxelsPerWorldUnits[1], pTexInfo->lightmapVecsLuxelsPerWorldUnits[0], 4 * sizeof(float) );
  199. temp.lightmapVecsLuxelsPerWorldUnits[1][0] *= -1.0f;
  200. temp.lightmapVecsLuxelsPerWorldUnits[1][1] *= -1.0f;
  201. temp.lightmapVecsLuxelsPerWorldUnits[1][2] *= -1.0f;
  202. temp.lightmapVecsLuxelsPerWorldUnits[1][3] *= -1.0f;
  203. pSwappedTexInfos[ pMapDisp->face.texinfo ] = texinfo.AddToTail( temp );
  204. }
  205. pMapDisp->face.texinfo = pSwappedTexInfos[ pMapDisp->face.texinfo ];
  206. }
  207. // NOTE: This is here to help future-proof code, since there are codepaths where
  208. // pTexInfo can be made invalid (texinfo.AddToTail above).
  209. pTexInfo = NULL;
  210. }
  211. // Setup the displacement vectors and offsets.
  212. int size = ( ( ( 1 << pMapDisp->power ) + 1 ) * ( ( 1 << pMapDisp->power ) + 1 ) );
  213. Vector vectorDisps[2048];
  214. float dispDists[2048];
  215. Assert( size < sizeof(vectorDisps)/sizeof(vectorDisps[0]) );
  216. for( int j = 0; j < size; j++ )
  217. {
  218. Vector v;
  219. float dist;
  220. VectorScale( pMapDisp->vectorDisps[j], pMapDisp->dispDists[j], v );
  221. VectorAdd( v, pMapDisp->vectorOffsets[j], v );
  222. dist = VectorLength( v );
  223. VectorNormalize( v );
  224. vectorDisps[j] = v;
  225. dispDists[j] = dist;
  226. }
  227. // Use CCoreDispInfo to setup the actual vertex positions.
  228. pCoreDispInfo->InitDispInfo( pMapDisp->power, pMapDisp->minTess, pMapDisp->smoothingAngle,
  229. pMapDisp->alphaValues, vectorDisps, dispDists );
  230. pCoreDispInfo->Create();
  231. }
  232. //-----------------------------------------------------------------------------
  233. //-----------------------------------------------------------------------------
  234. void EmitInitialDispInfos( void )
  235. {
  236. int i;
  237. mapdispinfo_t *pMapDisp;
  238. ddispinfo_t *pDisp;
  239. Vector v;
  240. // Calculate the total number of verts.
  241. int nTotalVerts = 0;
  242. int nTotalTris = 0;
  243. for ( i=0; i < nummapdispinfo; i++ )
  244. {
  245. nTotalVerts += NUM_DISP_POWER_VERTS( mapdispinfo[i].power );
  246. nTotalTris += NUM_DISP_POWER_TRIS( mapdispinfo[i].power );
  247. }
  248. // Clear the output arrays..
  249. g_dispinfo.Purge();
  250. g_dispinfo.SetSize( nummapdispinfo );
  251. g_DispVerts.SetSize( nTotalVerts );
  252. g_DispTris.SetSize( nTotalTris );
  253. int iCurVert = 0;
  254. int iCurTri = 0;
  255. for( i = 0; i < nummapdispinfo; i++ )
  256. {
  257. pDisp = &g_dispinfo[i];
  258. pMapDisp = &mapdispinfo[i];
  259. CDispVert *pOutVerts = &g_DispVerts[iCurVert];
  260. CDispTri *pOutTris = &g_DispTris[iCurTri];
  261. // Setup the vert pointers.
  262. pDisp->m_iDispVertStart = iCurVert;
  263. pDisp->m_iDispTriStart = iCurTri;
  264. iCurVert += NUM_DISP_POWER_VERTS( pMapDisp->power );
  265. iCurTri += NUM_DISP_POWER_TRIS( pMapDisp->power );
  266. //
  267. // save power, minimum tesselation, and smoothing angle
  268. //
  269. pDisp->power = pMapDisp->power;
  270. // If the high bit is set - this is FLAGS!
  271. pDisp->minTess = pMapDisp->flags;
  272. pDisp->minTess |= 0x80000000;
  273. // pDisp->minTess = pMapDisp->minTess;
  274. pDisp->smoothingAngle = pMapDisp->smoothingAngle;
  275. pDisp->m_iMapFace = (unsigned short)-2;
  276. // get surface contents
  277. pDisp->contents = pMapDisp->face.contents;
  278. pDisp->startPosition = pMapDisp->startPosition;
  279. //
  280. // add up the vectorOffsets and displacements, save alphas (per vertex)
  281. //
  282. int size = ( ( ( 1 << pDisp->power ) + 1 ) * ( ( 1 << pDisp->power ) + 1 ) );
  283. for( int j = 0; j < size; j++ )
  284. {
  285. VectorScale( pMapDisp->vectorDisps[j], pMapDisp->dispDists[j], v );
  286. VectorAdd( v, pMapDisp->vectorOffsets[j], v );
  287. float dist = VectorLength( v );
  288. VectorNormalize( v );
  289. VectorCopy( v, pOutVerts[j].m_vVector );
  290. pOutVerts[j].m_flDist = dist;
  291. pOutVerts[j].m_flAlpha = pMapDisp->alphaValues[j];
  292. }
  293. int nTriCount = ( (1 << (pDisp->power)) * (1 << (pDisp->power)) * 2 );
  294. for ( int iTri = 0; iTri< nTriCount; ++iTri )
  295. {
  296. pOutTris[iTri].m_uiTags = pMapDisp->triTags[iTri];
  297. }
  298. //===================================================================
  299. //===================================================================
  300. // save the index for face data reference
  301. pMapDisp->face.dispinfo = i;
  302. }
  303. }
  304. void ExportCoreDispNeighborData( const CCoreDispInfo *pIn, ddispinfo_t *pOut )
  305. {
  306. for ( int i=0; i < 4; i++ )
  307. {
  308. pOut->m_EdgeNeighbors[i] = *pIn->GetEdgeNeighbor( i );
  309. pOut->m_CornerNeighbors[i] = *pIn->GetCornerNeighbors( i );
  310. }
  311. }
  312. void ExportNeighborData( CCoreDispInfo **ppListBase, ddispinfo_t *pBSPDispInfos, int listSize )
  313. {
  314. FindNeighboringDispSurfs( ppListBase, listSize );
  315. // Export the neighbor data.
  316. for ( int i=0; i < nummapdispinfo; i++ )
  317. {
  318. ExportCoreDispNeighborData( g_CoreDispInfos[i], &pBSPDispInfos[i] );
  319. }
  320. }
  321. void ExportCoreDispAllowedVertList( const CCoreDispInfo *pIn, ddispinfo_t *pOut )
  322. {
  323. ErrorIfNot(
  324. pIn->GetAllowedVerts().GetNumDWords() == sizeof( pOut->m_AllowedVerts ) / 4,
  325. ("ExportCoreDispAllowedVertList: size mismatch")
  326. );
  327. for ( int i=0; i < pIn->GetAllowedVerts().GetNumDWords(); i++ )
  328. pOut->m_AllowedVerts[i] = pIn->GetAllowedVerts().GetDWord( i );
  329. }
  330. void ExportAllowedVertLists( CCoreDispInfo **ppListBase, ddispinfo_t *pBSPDispInfos, int listSize )
  331. {
  332. SetupAllowedVerts( ppListBase, listSize );
  333. for ( int i=0; i < listSize; i++ )
  334. {
  335. ExportCoreDispAllowedVertList( ppListBase[i], &pBSPDispInfos[i] );
  336. }
  337. }
  338. bool FindEnclosingTri(
  339. const Vector2D &vert,
  340. CUtlVector<Vector2D> &vertCoords,
  341. CUtlVector<unsigned short> &indices,
  342. int *pStartVert,
  343. float bcCoords[3] )
  344. {
  345. for ( int i=0; i < indices.Count(); i += 3 )
  346. {
  347. GetBarycentricCoords2D(
  348. vertCoords[indices[i+0]],
  349. vertCoords[indices[i+1]],
  350. vertCoords[indices[i+2]],
  351. vert,
  352. bcCoords );
  353. if ( bcCoords[0] >= 0 && bcCoords[0] <= 1 &&
  354. bcCoords[1] >= 0 && bcCoords[1] <= 1 &&
  355. bcCoords[2] >= 0 && bcCoords[2] <= 1 )
  356. {
  357. *pStartVert = i;
  358. return true;
  359. }
  360. }
  361. return false;
  362. }
  363. void SnapRemainingVertsToSurface( CCoreDispInfo *pCoreDisp, ddispinfo_t *pDispInfo )
  364. {
  365. // First, tesselate the displacement.
  366. CUtlVector<unsigned short> indices;
  367. CVBSPTesselateHelper helper;
  368. helper.m_pIndices = &indices;
  369. helper.m_pActiveVerts = pCoreDisp->GetAllowedVerts().Base();
  370. helper.m_pPowerInfo = pCoreDisp->GetPowerInfo();
  371. ::TesselateDisplacement( &helper );
  372. // Figure out which verts are actually referenced in the tesselation.
  373. CUtlVector<bool> vertsTouched;
  374. vertsTouched.SetSize( pCoreDisp->GetSize() );
  375. memset( vertsTouched.Base(), 0, sizeof( bool ) * vertsTouched.Count() );
  376. for ( int i=0; i < indices.Count(); i++ )
  377. vertsTouched[ indices[i] ] = true;
  378. // Generate 2D floating point coordinates for each vertex. We use these to generate
  379. // barycentric coordinates, and the scale doesn't matter.
  380. CUtlVector<Vector2D> vertCoords;
  381. vertCoords.SetSize( pCoreDisp->GetSize() );
  382. for ( int y=0; y < pCoreDisp->GetHeight(); y++ )
  383. {
  384. for ( int x=0; x < pCoreDisp->GetWidth(); x++ )
  385. vertCoords[y*pCoreDisp->GetWidth()+x].Init( x, y );
  386. }
  387. // Now, for each vert not touched, snap its position to the main surface.
  388. for ( int y=0; y < pCoreDisp->GetHeight(); y++ )
  389. {
  390. for ( int x=0; x < pCoreDisp->GetWidth(); x++ )
  391. {
  392. int index = y * pCoreDisp->GetWidth() + x;
  393. if ( !( vertsTouched[index] ) )
  394. {
  395. float bcCoords[3];
  396. int iStartVert = -1;
  397. if ( FindEnclosingTri( vertCoords[index], vertCoords, indices, &iStartVert, bcCoords ) )
  398. {
  399. const Vector &A = pCoreDisp->GetVert( indices[iStartVert+0] );
  400. const Vector &B = pCoreDisp->GetVert( indices[iStartVert+1] );
  401. const Vector &C = pCoreDisp->GetVert( indices[iStartVert+2] );
  402. Vector vNewPos = A*bcCoords[0] + B*bcCoords[1] + C*bcCoords[2];
  403. // This is kind of cheesy, but it gets the job done. Since the CDispVerts store the
  404. // verts relative to some other offset, we'll just offset their position instead
  405. // of setting it directly.
  406. Vector vOffset = vNewPos - pCoreDisp->GetVert( index );
  407. // Modify the mapfile vert.
  408. CDispVert *pVert = &g_DispVerts[pDispInfo->m_iDispVertStart + index];
  409. pVert->m_vVector = (pVert->m_vVector * pVert->m_flDist) + vOffset;
  410. pVert->m_flDist = 1;
  411. // Modify the CCoreDispInfo vert (although it probably won't be used later).
  412. pCoreDisp->SetVert( index, vNewPos );
  413. }
  414. else
  415. {
  416. // This shouldn't happen because it would mean that the triangulation that
  417. // disp_tesselation.h produced was missing a chunk of the space that the
  418. // displacement covers.
  419. // It also could indicate a floating-point epsilon error.. check to see if
  420. // FindEnclosingTri finds a triangle that -almost- encloses the vert.
  421. Assert( false );
  422. }
  423. }
  424. }
  425. }
  426. }
  427. void SnapRemainingVertsToSurface( CCoreDispInfo **ppListBase, ddispinfo_t *pBSPDispInfos, int listSize )
  428. {
  429. //g_pPad = ScratchPad3D_Create();
  430. for ( int i=0; i < listSize; i++ )
  431. {
  432. SnapRemainingVertsToSurface( ppListBase[i], &pBSPDispInfos[i] );
  433. }
  434. }
  435. void EmitDispLMAlphaAndNeighbors()
  436. {
  437. int i;
  438. Msg( "Finding displacement neighbors...\n" );
  439. // Build the CCoreDispInfos.
  440. CUtlVector<dface_t*> faces;
  441. // Create the core dispinfos and init them for use as CDispUtilsHelpers.
  442. for ( int iDisp = 0; iDisp < nummapdispinfo; ++iDisp )
  443. {
  444. CCoreDispInfo *pDisp = new CCoreDispInfo;
  445. if ( !pDisp )
  446. {
  447. g_CoreDispInfos.Purge();
  448. return;
  449. }
  450. int nIndex = g_CoreDispInfos.AddToTail();
  451. pDisp->SetListIndex( nIndex );
  452. g_CoreDispInfos[nIndex] = pDisp;
  453. }
  454. for ( i=0; i < nummapdispinfo; i++ )
  455. {
  456. g_CoreDispInfos[i]->SetDispUtilsHelperInfo( g_CoreDispInfos.Base(), nummapdispinfo );
  457. }
  458. faces.SetSize( nummapdispinfo );
  459. int nMemSize = texinfo.Count() * sizeof(int);
  460. int *pSwappedTexInfos = (int*)stackalloc( nMemSize );
  461. memset( pSwappedTexInfos, 0xFF, nMemSize );
  462. for( i = 0; i < numfaces; i++ )
  463. {
  464. dface_t *pFace = &dfaces[i];
  465. if( pFace->dispinfo == -1 )
  466. continue;
  467. mapdispinfo_t *pMapDisp = &mapdispinfo[pFace->dispinfo];
  468. // Set the displacement's face index.
  469. ddispinfo_t *pDisp = &g_dispinfo[pFace->dispinfo];
  470. pDisp->m_iMapFace = i;
  471. // Get a CCoreDispInfo. All we need is the triangles and lightmap texture coordinates.
  472. CCoreDispInfo *pCoreDispInfo = g_CoreDispInfos[pFace->dispinfo];
  473. DispMapToCoreDispInfo( pMapDisp, pCoreDispInfo, pFace, pSwappedTexInfos );
  474. faces[pFace->dispinfo] = pFace;
  475. }
  476. stackfree( pSwappedTexInfos );
  477. // Generate and export neighbor data.
  478. ExportNeighborData( g_CoreDispInfos.Base(), g_dispinfo.Base(), nummapdispinfo );
  479. // Generate and export the active vert lists.
  480. ExportAllowedVertLists( g_CoreDispInfos.Base(), g_dispinfo.Base(), nummapdispinfo );
  481. // Now that we know which vertices are actually going to be around, snap the ones that won't
  482. // be around onto the slightly-reduced mesh. This is so the engine's ray test code and
  483. // overlay code works right.
  484. SnapRemainingVertsToSurface( g_CoreDispInfos.Base(), g_dispinfo.Base(), nummapdispinfo );
  485. Msg( "Finding lightmap sample positions...\n" );
  486. for ( i=0; i < nummapdispinfo; i++ )
  487. {
  488. dface_t *pFace = faces[i];
  489. ddispinfo_t *pDisp = &g_dispinfo[pFace->dispinfo];
  490. CCoreDispInfo *pCoreDispInfo = g_CoreDispInfos[i];
  491. pDisp->m_iLightmapSamplePositionStart = g_DispLightmapSamplePositions.Count();
  492. CalculateLightmapSamplePositions( pCoreDispInfo, pFace, g_DispLightmapSamplePositions );
  493. }
  494. StartPacifier( "Displacement Alpha : ");
  495. // Build lightmap alphas.
  496. int dispCount = 0; // How many we've processed.
  497. for( i = 0; i < nummapdispinfo; i++ )
  498. {
  499. dface_t *pFace = faces[i];
  500. Assert( pFace->dispinfo == i );
  501. ddispinfo_t *pDisp = &g_dispinfo[pFace->dispinfo];
  502. // Allocate space for the alpha values.
  503. pDisp->m_iLightmapAlphaStart = 0; // not used anymore
  504. ++dispCount;
  505. }
  506. EndPacifier();
  507. }
  508. //-----------------------------------------------------------------------------
  509. //-----------------------------------------------------------------------------
  510. void DispGetFaceInfo( mapbrush_t *pBrush )
  511. {
  512. int i;
  513. side_t *pSide;
  514. // we don't support displacement on entities at the moment!!
  515. if( pBrush->entitynum != 0 )
  516. {
  517. char* pszEntityName = ValueForKey( &g_LoadingMap->entities[pBrush->entitynum], "classname" );
  518. Error( "Error: displacement found on a(n) %s entity - not supported (entity %d, brush %d)\n", pszEntityName, pBrush->entitynum, pBrush->brushnum );
  519. }
  520. for( i = 0; i < pBrush->numsides; i++ )
  521. {
  522. pSide = &pBrush->original_sides[i];
  523. if( pSide->pMapDisp )
  524. {
  525. // error checking!!
  526. if( pSide->winding->numpoints != 4 )
  527. Error( "Trying to create a non-quad displacement! (entity %d, brush %d)\n", pBrush->entitynum, pBrush->brushnum );
  528. pSide->pMapDisp->face.originalface = pSide;
  529. pSide->pMapDisp->face.texinfo = pSide->texinfo;
  530. pSide->pMapDisp->face.dispinfo = -1;
  531. pSide->pMapDisp->face.planenum = pSide->planenum;
  532. pSide->pMapDisp->face.numpoints = pSide->winding->numpoints;
  533. pSide->pMapDisp->face.w = CopyWinding( pSide->winding );
  534. pSide->pMapDisp->face.contents = pBrush->contents;
  535. pSide->pMapDisp->face.merged = FALSE;
  536. pSide->pMapDisp->face.split[0] = FALSE;
  537. pSide->pMapDisp->face.split[1] = FALSE;
  538. pSide->pMapDisp->entitynum = pBrush->entitynum;
  539. pSide->pMapDisp->brushSideID = pSide->id;
  540. }
  541. }
  542. }
  543. //-----------------------------------------------------------------------------
  544. //-----------------------------------------------------------------------------
  545. bool HasDispInfo( mapbrush_t *pBrush )
  546. {
  547. int i;
  548. side_t *pSide;
  549. for( i = 0; i < pBrush->numsides; i++ )
  550. {
  551. pSide = &pBrush->original_sides[i];
  552. if( pSide->pMapDisp )
  553. return true;
  554. }
  555. return false;
  556. }