Counter Strike : Global Offensive Source Code
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.

230 lines
7.6 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. #include "vrad.h"
  9. #include "lightmap.h"
  10. #define SAMPLEHASH_NUM_BUCKETS 65536
  11. #define SAMPLEHASH_GROW_SIZE 0
  12. #define SAMPLEHASH_INIT_SIZE 0
  13. int samplesAdded = 0;
  14. int patchSamplesAdded = 0;
  15. static unsigned short g_PatchIterationKey = 0;
  16. //-----------------------------------------------------------------------------
  17. //-----------------------------------------------------------------------------
  18. bool SampleData_CompareFunc( SampleData_t const &src1, SampleData_t const &src2 )
  19. {
  20. return ( ( src1.x == src2.x ) &&
  21. ( src1.y == src2.y ) &&
  22. ( src1.z == src2.z ) );
  23. }
  24. //-----------------------------------------------------------------------------
  25. //-----------------------------------------------------------------------------
  26. unsigned int SampleData_KeyFunc( SampleData_t const &src )
  27. {
  28. return ( src.x + src.y + src.z );
  29. }
  30. CUtlHash<SampleData_t> g_SampleHashTable( SAMPLEHASH_NUM_BUCKETS,
  31. SAMPLEHASH_GROW_SIZE,
  32. SAMPLEHASH_INIT_SIZE,
  33. SampleData_CompareFunc, SampleData_KeyFunc );
  34. //-----------------------------------------------------------------------------
  35. //-----------------------------------------------------------------------------
  36. UtlHashHandle_t SampleData_Find( sample_t *pSample )
  37. {
  38. SampleData_t sampleData;
  39. sampleData.x = ( int )( pSample->pos.x / SAMPLEHASH_VOXEL_SIZE ) * 100;
  40. sampleData.y = ( int )( pSample->pos.y / SAMPLEHASH_VOXEL_SIZE ) * 10;
  41. sampleData.z = ( int )( pSample->pos.z / SAMPLEHASH_VOXEL_SIZE );
  42. return g_SampleHashTable.Find( sampleData );
  43. }
  44. //-----------------------------------------------------------------------------
  45. //-----------------------------------------------------------------------------
  46. UtlHashHandle_t SampleData_InsertIntoHashTable( sample_t *pSample, SampleHandle_t sampleHandle )
  47. {
  48. SampleData_t sampleData;
  49. sampleData.x = ( int )( pSample->pos.x / SAMPLEHASH_VOXEL_SIZE ) * 100;
  50. sampleData.y = ( int )( pSample->pos.y / SAMPLEHASH_VOXEL_SIZE ) * 10;
  51. sampleData.z = ( int )( pSample->pos.z / SAMPLEHASH_VOXEL_SIZE );
  52. UtlHashHandle_t handle = g_SampleHashTable.AllocEntryFromKey( sampleData );
  53. SampleData_t *pSampleData = &g_SampleHashTable.Element( handle );
  54. pSampleData->x = sampleData.x;
  55. pSampleData->y = sampleData.y;
  56. pSampleData->z = sampleData.z;
  57. pSampleData->m_Samples.AddToTail( sampleHandle );
  58. samplesAdded++;
  59. return handle;
  60. }
  61. //-----------------------------------------------------------------------------
  62. //-----------------------------------------------------------------------------
  63. UtlHashHandle_t SampleData_AddSample( sample_t *pSample, SampleHandle_t sampleHandle )
  64. {
  65. // find the key -- if it doesn't exist add new sample data to the
  66. // hash table
  67. UtlHashHandle_t handle = SampleData_Find( pSample );
  68. if( handle == g_SampleHashTable.InvalidHandle() )
  69. {
  70. handle = SampleData_InsertIntoHashTable( pSample, sampleHandle );
  71. }
  72. else
  73. {
  74. SampleData_t *pSampleData = &g_SampleHashTable.Element( handle );
  75. pSampleData->m_Samples.AddToTail( sampleHandle );
  76. samplesAdded++;
  77. }
  78. return handle;
  79. }
  80. //-----------------------------------------------------------------------------
  81. //-----------------------------------------------------------------------------
  82. void SampleData_Log( void )
  83. {
  84. if( g_bLogHashData )
  85. {
  86. g_SampleHashTable.Log( "samplehash.txt" );
  87. }
  88. }
  89. //=============================================================================
  90. //=============================================================================
  91. //
  92. // PatchSample Functions
  93. //
  94. //=============================================================================
  95. //=============================================================================
  96. //-----------------------------------------------------------------------------
  97. //-----------------------------------------------------------------------------
  98. bool PatchSampleData_CompareFunc( PatchSampleData_t const &src1, PatchSampleData_t const &src2 )
  99. {
  100. return ( ( src1.x == src2.x ) &&
  101. ( src1.y == src2.y ) &&
  102. ( src1.z == src2.z ) );
  103. }
  104. //-----------------------------------------------------------------------------
  105. //-----------------------------------------------------------------------------
  106. unsigned int PatchSampleData_KeyFunc( PatchSampleData_t const &src )
  107. {
  108. return ( src.x + src.y + src.z );
  109. }
  110. CUtlHash<PatchSampleData_t> g_PatchSampleHashTable( SAMPLEHASH_NUM_BUCKETS,
  111. SAMPLEHASH_GROW_SIZE,
  112. SAMPLEHASH_INIT_SIZE,
  113. PatchSampleData_CompareFunc, PatchSampleData_KeyFunc );
  114. void GetPatchSampleHashXYZ( const Vector &vOrigin, int &x, int &y, int &z )
  115. {
  116. x = ( int )( vOrigin.x / SAMPLEHASH_VOXEL_SIZE );
  117. y = ( int )( vOrigin.y / SAMPLEHASH_VOXEL_SIZE );
  118. z = ( int )( vOrigin.z / SAMPLEHASH_VOXEL_SIZE );
  119. }
  120. unsigned short IncrementPatchIterationKey()
  121. {
  122. if ( g_PatchIterationKey == 0xFFFF )
  123. {
  124. g_PatchIterationKey = 1;
  125. for ( int i=0; i < g_Patches.Count(); i++ )
  126. g_Patches[i].m_IterationKey = 0;
  127. }
  128. else
  129. {
  130. g_PatchIterationKey++;
  131. }
  132. return g_PatchIterationKey;
  133. }
  134. //-----------------------------------------------------------------------------
  135. //-----------------------------------------------------------------------------
  136. void PatchSampleData_AddSample( CPatch *pPatch, int ndxPatch )
  137. {
  138. int patchSampleMins[3], patchSampleMaxs[3];
  139. #if defined( SAMPLEHASH_USE_AREA_PATCHES )
  140. GetPatchSampleHashXYZ( pPatch->mins, patchSampleMins[0], patchSampleMins[1], patchSampleMins[2] );
  141. GetPatchSampleHashXYZ( pPatch->maxs, patchSampleMaxs[0], patchSampleMaxs[1], patchSampleMaxs[2] );
  142. #else
  143. // If not using area patches, just use the patch's origin to add it to the voxels.
  144. GetPatchSampleHashXYZ( pPatch->origin, patchSampleMins[0], patchSampleMins[1], patchSampleMins[2] );
  145. memcpy( patchSampleMaxs, patchSampleMins, sizeof( patchSampleMaxs ) );
  146. #endif
  147. // Make sure mins are smaller than maxs so we don't iterate for 4 bil.
  148. Assert( patchSampleMins[0] <= patchSampleMaxs[0] && patchSampleMins[1] <= patchSampleMaxs[1] && patchSampleMins[2] <= patchSampleMaxs[2] );
  149. patchSampleMins[0] = min( patchSampleMins[0], patchSampleMaxs[0] );
  150. patchSampleMins[1] = min( patchSampleMins[1], patchSampleMaxs[1] );
  151. patchSampleMins[2] = min( patchSampleMins[2], patchSampleMaxs[2] );
  152. int iterateCoords[3];
  153. for ( iterateCoords[0]=patchSampleMins[0]; iterateCoords[0] <= patchSampleMaxs[0]; iterateCoords[0]++ )
  154. {
  155. for ( iterateCoords[1]=patchSampleMins[1]; iterateCoords[1] <= patchSampleMaxs[1]; iterateCoords[1]++ )
  156. {
  157. for ( iterateCoords[2]=patchSampleMins[2]; iterateCoords[2] <= patchSampleMaxs[2]; iterateCoords[2]++ )
  158. {
  159. // find the key -- if it doesn't exist add new sample data to the
  160. // hash table
  161. PatchSampleData_t iteratePatch;
  162. iteratePatch.x = iterateCoords[0] * 100;
  163. iteratePatch.y = iterateCoords[1] * 10;
  164. iteratePatch.z = iterateCoords[2];
  165. UtlHashHandle_t handle = g_PatchSampleHashTable.Find( iteratePatch );
  166. if( handle == g_PatchSampleHashTable.InvalidHandle() )
  167. {
  168. UtlHashHandle_t handle = g_PatchSampleHashTable.AllocEntryFromKey( iteratePatch );
  169. PatchSampleData_t *pPatchData = &g_PatchSampleHashTable.Element( handle );
  170. pPatchData->x = iteratePatch.x;
  171. pPatchData->y = iteratePatch.y;
  172. pPatchData->z = iteratePatch.z;
  173. pPatchData->m_ndxPatches.AddToTail( ndxPatch );
  174. patchSamplesAdded++;
  175. }
  176. else
  177. {
  178. PatchSampleData_t *pPatchData = &g_PatchSampleHashTable.Element( handle );
  179. pPatchData->m_ndxPatches.AddToTail( ndxPatch );
  180. patchSamplesAdded++;
  181. }
  182. }
  183. }
  184. }
  185. }