transparency.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. #pragma warning(disable:4018) //amckern - 64bit - '<' Singed/Unsigned Mismatch
  2. //
  3. // Transparency Arrays for sparse and vismatrix methods
  4. //
  5. #include "qrad.h"
  6. #ifdef HLRAD_HULLU
  7. #define TRANS_LIST_GROWTH 64
  8. #define RAW_LIST_GROWTH 2048
  9. typedef struct {
  10. unsigned p1;
  11. unsigned p2;
  12. unsigned data_index;
  13. } transList_t;
  14. static vec3_t * s_trans_list = NULL;
  15. static unsigned int s_trans_count = 0;
  16. static unsigned int s_max_trans_count = 0;
  17. static transList_t* s_raw_list = NULL;
  18. static unsigned int s_raw_count = 0;
  19. static unsigned int s_max_raw_count = 0; // Current array maximum (used for reallocs)
  20. static transList_t* s_sorted_list = NULL; // Sorted first by p1 then p2
  21. static unsigned int s_sorted_count = 0;
  22. const vec3_t vec3_one = {1.0,1.0,1.0};
  23. //===============================================
  24. // AddTransparencyToRawArray
  25. //===============================================
  26. static unsigned AddTransparencyToDataList(const vec3_t trans)
  27. {
  28. //Check if this value is in list already
  29. for(int i = 0; i < s_trans_count; i++)
  30. {
  31. if( VectorCompare( trans, s_trans_list[i] ) )
  32. {
  33. return i;
  34. }
  35. }
  36. //realloc if needed
  37. while( s_trans_count >= s_max_trans_count )
  38. {
  39. unsigned int old_max_count = s_max_trans_count;
  40. s_max_trans_count += TRANS_LIST_GROWTH;
  41. s_trans_list = (vec3_t *)realloc( s_trans_list, sizeof(vec3_t) * s_max_trans_count );
  42. memset( &s_trans_list[old_max_count], 0, sizeof(vec3_t) * TRANS_LIST_GROWTH );
  43. if( old_max_count == 0 )
  44. {
  45. VectorFill(s_trans_list[0], 1.0);
  46. s_trans_count++;
  47. }
  48. }
  49. VectorCopy(trans, s_trans_list[s_trans_count]);
  50. return ( s_trans_count++ );
  51. }
  52. //===============================================
  53. // AddTransparencyToRawArray
  54. //===============================================
  55. void AddTransparencyToRawArray(const unsigned p1, const unsigned p2, const vec3_t trans)
  56. {
  57. //make thread safe
  58. ThreadLock();
  59. unsigned data_index = AddTransparencyToDataList(trans);
  60. //realloc if needed
  61. while( s_raw_count >= s_max_raw_count )
  62. {
  63. unsigned int old_max_count = s_max_raw_count;
  64. s_max_raw_count += RAW_LIST_GROWTH;
  65. s_raw_list = (transList_t *)realloc( s_raw_list, sizeof(transList_t) * s_max_raw_count );
  66. memset( &s_raw_list[old_max_count], 0, sizeof(transList_t) * RAW_LIST_GROWTH );
  67. }
  68. s_raw_list[s_raw_count].p1 = p1;
  69. s_raw_list[s_raw_count].p2 = p2;
  70. s_raw_list[s_raw_count].data_index = data_index;
  71. s_raw_count++;
  72. //unlock list
  73. ThreadUnlock();
  74. }
  75. //===============================================
  76. // SortList
  77. //===============================================
  78. static int CDECL SortList(const void *a, const void *b)
  79. {
  80. const transList_t* item1 = (transList_t *)a;
  81. const transList_t* item2 = (transList_t *)b;
  82. if( item1->p1 == item2->p1 )
  83. {
  84. return item1->p2 - item2->p2;
  85. }
  86. else
  87. {
  88. return item1->p1 - item2->p1;
  89. }
  90. }
  91. //===============================================
  92. // CreateFinalTransparencyArrays
  93. //===============================================
  94. void CreateFinalTransparencyArrays(const char *print_name)
  95. {
  96. if( s_raw_count == 0 )
  97. {
  98. s_raw_list = NULL;
  99. s_raw_count = s_max_raw_count = 0;
  100. return;
  101. }
  102. //double sized (faster find function for sorted list)
  103. s_sorted_count = s_raw_count * 2;
  104. s_sorted_list = (transList_t *)malloc( sizeof(transList_t) * s_sorted_count );
  105. //First half have p1>p2
  106. for( unsigned int i = 0; i < s_raw_count; i++ )
  107. {
  108. s_sorted_list[i].p1 = s_raw_list[i].p2;
  109. s_sorted_list[i].p2 = s_raw_list[i].p1;
  110. s_sorted_list[i].data_index = s_raw_list[i].data_index;
  111. }
  112. //Second half have p1<p2
  113. memcpy( &s_sorted_list[s_raw_count], s_raw_list, sizeof(transList_t) * s_raw_count );
  114. //free old array
  115. free( s_raw_list );
  116. s_raw_list = NULL;
  117. s_raw_count = s_max_raw_count = 0;
  118. //need to sorted for fast search function
  119. qsort( s_sorted_list, s_sorted_count, sizeof(transList_t), SortList );
  120. unsigned size = s_sorted_count * sizeof(transList_t) + s_max_trans_count * sizeof(vec3_t);
  121. if ( size > 1024 * 1024 )
  122. Log("%-20s: %5.1f megs \n", print_name, size / (1024 * 1024.0));
  123. else if ( size > 1024 )
  124. Log("%-20s: %5.1f kilos\n", print_name, size / 1024.0);
  125. else
  126. Log("%-20s: %5.1f bytes\n", print_name, size);
  127. #if 0
  128. int total_1 = 0;
  129. for(int i = 0; i < s_sorted_count; i++)
  130. {
  131. Log("a: %7i b: %7i di: %10i r: %3.1f g: %3.1f b: %3.1f\n",
  132. s_sorted_list[i].p1,
  133. s_sorted_list[i].p2,
  134. s_sorted_list[i].data_index,
  135. s_trans_list[s_sorted_list[i].data_index][0],
  136. s_trans_list[s_sorted_list[i].data_index][1],
  137. s_trans_list[s_sorted_list[i].data_index][2]
  138. );
  139. total_1++;
  140. }
  141. vec3_t rgb;
  142. int total_2 = 0;
  143. for(unsigned int next_index = 0, a = 0; a < g_num_patches; a++)
  144. {
  145. for(unsigned int b = 0; b < g_num_patches; b++)
  146. {
  147. GetTransparency(a, b, rgb, next_index);
  148. if(!VectorCompare(rgb,vec3_one))
  149. {
  150. Log("a: %7i b: %7i ni: %10i r: %3.1f g: %3.1f b: %3.1f\n",
  151. a,
  152. b,
  153. next_index,
  154. rgb[0],
  155. rgb[1],
  156. rgb[2]
  157. );
  158. total_2++;
  159. }
  160. }
  161. }
  162. Log("total1: %i\ntotal2: %i\n",total_1,total_2);
  163. #endif
  164. }
  165. //===============================================
  166. // FreeTransparencyArrays
  167. //===============================================
  168. void FreeTransparencyArrays( )
  169. {
  170. if (s_sorted_list) free(s_sorted_list);
  171. if (s_trans_list) free(s_trans_list);
  172. s_trans_list = NULL;
  173. s_sorted_list = NULL;
  174. s_max_trans_count = s_trans_count = s_sorted_count = 0;
  175. }
  176. //===============================================
  177. // GetTransparency -- find transparency from list. remembers last location
  178. //===============================================
  179. void GetTransparency(const unsigned p1, const unsigned p2, vec3_t &trans, unsigned int &next_index)
  180. {
  181. VectorFill( trans, 1.0 );
  182. for( unsigned i = next_index; i < s_sorted_count; i++ )
  183. {
  184. if ( s_sorted_list[i].p1 < p1 )
  185. {
  186. continue;
  187. }
  188. else if ( s_sorted_list[i].p1 == p1 )
  189. {
  190. if ( s_sorted_list[i].p2 < p2 )
  191. {
  192. continue;
  193. }
  194. else if ( s_sorted_list[i].p2 == p2 )
  195. {
  196. VectorCopy( s_trans_list[s_sorted_list[i].data_index], trans );
  197. next_index = i + 1;
  198. return;
  199. }
  200. else //if ( s_sorted_list[i].p2 > p2 )
  201. {
  202. next_index = i;
  203. return;
  204. }
  205. }
  206. else //if ( s_sorted_list[i].p1 > p1 )
  207. {
  208. next_index = i;
  209. return;
  210. }
  211. }
  212. next_index = s_sorted_count;
  213. }
  214. #endif /*HLRAD_HULLU*/