vissample.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /*
  2. ** Command & Conquer Renegade(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /***********************************************************************************************
  19. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : WWPhys *
  23. * *
  24. * $Archive:: /Commando/Code/wwphys/vissample.cpp $*
  25. * *
  26. * Author:: Greg Hjelstrom *
  27. * *
  28. * $Modtime:: 7/16/00 1:19p $*
  29. * *
  30. * $Revision:: 9 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #include "vissample.h"
  36. #include "wwdebug.h"
  37. #include "chunkio.h"
  38. #include <memory.h>
  39. //////////////////////////////////////////////////////////////////////////////
  40. //
  41. // Constants
  42. //
  43. //////////////////////////////////////////////////////////////////////////////
  44. typedef struct
  45. {
  46. Matrix3D transform;
  47. float backface_fraction[6];
  48. VisStatusType status[6];
  49. int direction_bits;
  50. VisDirType current_direction;
  51. unsigned long reserved[4];
  52. } PERSISTENT_VIS_SAMPLE_INFO;
  53. VisSampleClass::VisSampleClass(void)
  54. : DirectionBits (0),
  55. ViewTransform (1),
  56. CurDirection (VIS_FORWARD),
  57. BitsChanged(0)
  58. {
  59. for (int i=0; i<VIS_DIRECTIONS; i++) {
  60. Status[i] = VIS_STATUS_NOT_TAKEN;
  61. BackfaceFraction[i] = 0.0f;
  62. }
  63. return ;
  64. }
  65. VisSampleClass::VisSampleClass(const Matrix3D & viewtm,VisDirBitsType direction_bits)
  66. {
  67. ViewTransform = viewtm;
  68. DirectionBits = direction_bits;
  69. CurDirection = VIS_FORWARD;
  70. BitsChanged = 0;
  71. for (int i=0; i<VIS_DIRECTIONS; i++) {
  72. Status[i] = VIS_STATUS_NOT_TAKEN;
  73. BackfaceFraction[i] = 0.0f;
  74. }
  75. }
  76. void VisSampleClass::Init_Error(void)
  77. {
  78. for (int i=0; i<VIS_DIRECTIONS; i++) {
  79. Status[i] = VIS_STATUS_NOT_TAKEN;
  80. BackfaceFraction[i] = 1.0f;
  81. }
  82. }
  83. bool VisSampleClass::Sample_Rejected(void) const
  84. {
  85. for (int i=0; i<VIS_DIRECTIONS; i++) {
  86. if ((Status[i] == VIS_STATUS_BACKFACE_OVERFLOW) || (Status[i] == VIS_STATUS_ERROR)) {
  87. return true;
  88. }
  89. }
  90. return false;
  91. }
  92. bool VisSampleClass::Sample_Useless(void) const
  93. {
  94. #if 1
  95. return Sample_Rejected();
  96. #else
  97. // This returns true if this sample is useless, we
  98. // obviously generated a point outside of where the player
  99. // should be allowed to be.
  100. for (int i=0; i<VIS_DIRECTIONS; i++) {
  101. if (BackfaceFraction[i] > 0.5f) {
  102. return true;
  103. }
  104. }
  105. return false;
  106. #endif
  107. }
  108. bool VisSampleClass::Direction_Enabled(VisDirType direction_index) const
  109. {
  110. WWASSERT(direction_index >= 0);
  111. WWASSERT(direction_index < VIS_DIRECTIONS);
  112. int mask = (1 << direction_index);
  113. return ((DirectionBits & mask) == mask);
  114. }
  115. Matrix3D VisSampleClass::Get_Camera_Transform(VisDirType direction_index) const
  116. {
  117. WWASSERT(direction_index >= 0);
  118. WWASSERT(direction_index < VIS_DIRECTIONS);
  119. Matrix3D view = ViewTransform; // vis forward
  120. switch(direction_index) {
  121. case VIS_FORWARD:
  122. break;
  123. case VIS_LEFT:
  124. view.Rotate_Y(DEG_TO_RADF(90.0f));
  125. break;
  126. case VIS_BACKWARDS:
  127. view.Rotate_Y(DEG_TO_RADF(180.0f));
  128. break;
  129. case VIS_RIGHT:
  130. view.Rotate_Y(DEG_TO_RADF(-90.0f));
  131. break;
  132. case VIS_UP:
  133. view.Rotate_X(DEG_TO_RADF(90.0f));
  134. break;
  135. case VIS_DOWN:
  136. view.Rotate_X(DEG_TO_RADF(-90.0f));
  137. break;
  138. }
  139. if (!(DirectionBits & VIS_DONT_RECENTER)) {
  140. view.Translate(Vector3(0,0,VIS_NEAR_CLIP));
  141. }
  142. return view;
  143. }
  144. int VisSampleClass::Sample_Status(VisDirType direction_index) const
  145. {
  146. WWASSERT(direction_index >= 0);
  147. WWASSERT(direction_index < VIS_DIRECTIONS);
  148. return Status[direction_index];
  149. }
  150. float VisSampleClass::Backface_Fraction(VisDirType direction_index) const
  151. {
  152. WWASSERT(direction_index >= 0);
  153. WWASSERT(direction_index < VIS_DIRECTIONS);
  154. return BackfaceFraction[direction_index];
  155. }
  156. void VisSampleClass::Set_Cur_Direction(VisDirType direction_index)
  157. {
  158. WWASSERT(direction_index >= 0);
  159. WWASSERT(direction_index < VIS_DIRECTIONS);
  160. CurDirection = direction_index;
  161. }
  162. void VisSampleClass::Set_Results(VisStatusType status,float fraction)
  163. {
  164. WWASSERT(CurDirection >= 0);
  165. WWASSERT(CurDirection < VIS_DIRECTIONS);
  166. Status[CurDirection] = status;
  167. BackfaceFraction[CurDirection] = fraction;
  168. }
  169. const char * VisSampleClass::Get_Cur_Direction_Name(void) const
  170. {
  171. WWASSERT(CurDirection >= 0);
  172. WWASSERT(CurDirection < VIS_DIRECTIONS);
  173. static char * _direction_names[] =
  174. {
  175. "F","L","B","R","U","D"
  176. };
  177. return _direction_names[CurDirection];
  178. }
  179. float VisSampleClass::Get_Biggest_Fraction(void) const
  180. {
  181. float biggest_fraction = BackfaceFraction[0];
  182. for (int direction = 1; direction < VIS_DIRECTIONS; direction ++) {
  183. biggest_fraction = max (biggest_fraction, BackfaceFraction[direction]);
  184. }
  185. return biggest_fraction;
  186. }
  187. int VisSampleClass::Get_Biggest_Fraction_Index(void) const
  188. {
  189. int index = 0;
  190. float biggest_fraction = BackfaceFraction[0];
  191. for (int direction = 1; direction < VIS_DIRECTIONS; direction ++) {
  192. if (BackfaceFraction[direction] > biggest_fraction) {
  193. index = direction;
  194. biggest_fraction = BackfaceFraction[direction];
  195. }
  196. }
  197. return index;
  198. }
  199. int VisSampleClass::Get_Cur_Direction(void) const
  200. {
  201. return CurDirection;
  202. }
  203. bool VisSampleClass::Save(ChunkSaveClass &chunk_save) const
  204. {
  205. bool retval = false;
  206. PERSISTENT_VIS_SAMPLE_INFO info;
  207. ::memset (&info, 0, sizeof (info));
  208. //
  209. // Copy the sample information into the object
  210. //
  211. info.transform = ViewTransform;
  212. info.direction_bits = DirectionBits;
  213. info.current_direction = CurDirection;
  214. for (int index = 0; index < VIS_DIRECTIONS; index ++) {
  215. info.status[index] = Status[index];
  216. info.backface_fraction[index] = BackfaceFraction[index];
  217. }
  218. //
  219. // Write the sample information to the chunk
  220. //
  221. retval = (chunk_save.Write (&info, sizeof (info)) == sizeof (info));
  222. return retval;
  223. }
  224. bool VisSampleClass::Load(ChunkLoadClass &chunk_load)
  225. {
  226. bool retval = false;
  227. //
  228. // Read the sample information from the chunk
  229. //
  230. PERSISTENT_VIS_SAMPLE_INFO info;
  231. retval = (chunk_load.Read (&info, sizeof (info)) == sizeof (info));
  232. if (retval) {
  233. //
  234. // Copy the sample information into the object
  235. //
  236. ViewTransform = info.transform;
  237. DirectionBits = info.direction_bits;
  238. CurDirection = info.current_direction;
  239. for (int index = 0; index < VIS_DIRECTIONS; index ++) {
  240. Status[index] = info.status[index];
  241. BackfaceFraction[index] = info.backface_fraction[index];
  242. }
  243. }
  244. return retval;
  245. }