editableheightfield.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 : leveledit *
  23. * *
  24. * $Archive:: /Commando/Code/Tools/LevelEdit/editableheightfield.h $*
  25. * *
  26. * Author:: Patrick Smith *
  27. * *
  28. * $Modtime:: 3/07/02 1:55p $*
  29. * *
  30. * $Revision:: 4 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #if defined(_MSC_VER)
  36. #pragma once
  37. #endif
  38. #ifndef __EDITABLEHEIGHTFIELD_H
  39. #define __EDITABLEHEIGHTFIELD_H
  40. #include "rendobj.h"
  41. #include "vector.h"
  42. #include "shader.h"
  43. #include "coltest.h"
  44. #include "castres.h"
  45. #include "tri.h"
  46. //////////////////////////////////////////////////////////////////////
  47. // Forward declarations
  48. //////////////////////////////////////////////////////////////////////
  49. class VertexMaterialClass;
  50. class TerrainMaterialClass;
  51. class RenegadeTerrainPatchClass;
  52. class StaticPhysClass;
  53. //////////////////////////////////////////////////////////////////////
  54. //
  55. // EditableHeightfieldClass
  56. //
  57. //////////////////////////////////////////////////////////////////////
  58. class EditableHeightfieldClass
  59. {
  60. public:
  61. ///////////////////////////////////////////////////////////////////
  62. // Public constructors/destructors
  63. ///////////////////////////////////////////////////////////////////
  64. EditableHeightfieldClass (void);
  65. EditableHeightfieldClass (const EditableHeightfieldClass &src);
  66. virtual ~EditableHeightfieldClass (void);
  67. ///////////////////////////////////////////////////////////////////
  68. // Public operators
  69. ///////////////////////////////////////////////////////////////////
  70. const EditableHeightfieldClass & operator= (const EditableHeightfieldClass &src);
  71. ///////////////////////////////////////////////////////////////////
  72. // Public methods
  73. ///////////////////////////////////////////////////////////////////
  74. //
  75. // Initialization
  76. //
  77. void Create (float width, float height, float meters_per_point);
  78. void Create (const char *heightmap_filename, float width, float height, float meters_per_point, float scale);
  79. void Create (HBITMAP bitmap, float width, float height, float meters_per_point, float scale);
  80. //
  81. // Configuration
  82. //
  83. void Set_Dimensions (float width, float height, float meters_per_point);
  84. float Get_Width (void) const { return Width; }
  85. float Get_Height (void) const { return Height; }
  86. float Get_Density (void) const { return Density; }
  87. //
  88. // Height editing
  89. //
  90. void Deform_Heightfield (const Vector3 &center, float amount, float inner_radius, float outter_radius);
  91. void Smooth_Heightfield (const Vector3 &center, float amount, float inner_radius, float outter_radius);
  92. void Smooth_Foundation_Heightfield (const Vector3 &center, float amount, float inner_radius, float outter_radius);
  93. void Paint_Heightfield (const Vector3 &world_space_center, int texture_index, float amount, float inner_radius, float outter_radius);
  94. void Cutout_Heightfield (const Vector3 &world_space_center, float outter_radius, bool subtract);
  95. //
  96. // Save/load support
  97. //
  98. bool Save (ChunkSaveClass &csave);
  99. bool Load (ChunkLoadClass &cload);
  100. void Load_Variables (ChunkLoadClass &cload);
  101. void Load_Materials (ChunkLoadClass &cload);
  102. void On_Post_Load (void);
  103. void Assign_Unique_IDs (void);
  104. //
  105. // Material access
  106. //
  107. TerrainMaterialClass * Peek_Material (int index) { return MaterialList[index]; }
  108. void Set_Material (int index, TerrainMaterialClass *material);
  109. void On_Material_Changed (int index);
  110. protected:
  111. ///////////////////////////////////////////////////////////////////
  112. // Protected methods
  113. ///////////////////////////////////////////////////////////////////
  114. //
  115. // Index utility functions
  116. //
  117. int Grid_Index (int x, int y) { return (y * GridPointsX) + x; }
  118. int Get_Point_Index_X (float x_pos, bool clamp = true);
  119. int Get_Point_Index_Y (float y_pos, bool clamp = true);
  120. int Get_Quad_Index_X (float x_pos, bool clamp = true);
  121. int Get_Quad_Index_Y (float y_pos, bool clamp = true);
  122. bool Is_Valid_Quad (int x, int y);
  123. void Free_Grid (void);
  124. void Free_Patches (void);
  125. void Free_Material (void);
  126. void Initialize_Material (void);
  127. void Initialize (void);
  128. void Initialize_Grid (void);
  129. void Allocate_Grid (void);
  130. void Update_Normals (int min_x, int min_y, int max_x, int max_y);
  131. void Update_UVs (void);
  132. void Update_Texture_Quad_List (int min_x, int min_y, int max_x, int max_y);
  133. void Allocate_Patches (void);
  134. void Update_Patch_Pos_And_Normals (int min_x, int min_y, int max_x, int max_y);
  135. void Update_Patch_Texture_Quads (void);
  136. ///////////////////////////////////////////////////////////////////
  137. // Protected data types
  138. ///////////////////////////////////////////////////////////////////
  139. typedef enum
  140. {
  141. PASS_BASE = 0,
  142. PASS_ALPHA,
  143. PASS_COUNT
  144. } TEXTURE_PASS_TYPE;
  145. enum
  146. {
  147. MAX_TEXTURE_PASSES = 10,
  148. };
  149. enum
  150. {
  151. QF_NORMAL = 0,
  152. QF_HIDDEN = 1,
  153. } QUAD_FLAGS;
  154. ///////////////////////////////////////////////////////////////////
  155. // Protected member data
  156. ///////////////////////////////////////////////////////////////////
  157. Vector3 * Grid;
  158. Vector3 * GridNormals;
  159. Vector2 * GridUVs[MAX_TEXTURE_PASSES];
  160. float * TextureWeights[MAX_TEXTURE_PASSES];
  161. uint8 * QuadFlags;
  162. int GridPointsX;
  163. int GridPointsY;
  164. int GridPointCount;
  165. float Width;
  166. float Height;
  167. float Density;
  168. TerrainMaterialClass * MaterialList[MAX_TEXTURE_PASSES];
  169. RenegadeTerrainPatchClass ** PatchGrid;
  170. StaticPhysClass ** PhysObjGrid;
  171. int PatchesX;
  172. int PatchesY;
  173. int PatchGridPointsX;
  174. int PatchGridPointsY;
  175. float PatchWidth;
  176. float PatchHeight;
  177. };
  178. //////////////////////////////////////////////////////////////////////
  179. // Get_Point_Index_X
  180. //////////////////////////////////////////////////////////////////////
  181. WWINLINE int
  182. EditableHeightfieldClass::Get_Point_Index_X (float x_pos, bool clamp)
  183. {
  184. int point_x = WWMath::Float_To_Int_Floor (x_pos / Density);
  185. //
  186. // Clamp the index to the range of existing points
  187. //
  188. if (clamp) {
  189. point_x = WWMath::Clamp_Int (point_x, 0, GridPointsX - 1);
  190. }
  191. return point_x;
  192. }
  193. //////////////////////////////////////////////////////////////////////
  194. // Get_Point_Index_Y
  195. //////////////////////////////////////////////////////////////////////
  196. WWINLINE int
  197. EditableHeightfieldClass::Get_Point_Index_Y (float y_pos, bool clamp)
  198. {
  199. int point_y = WWMath::Float_To_Int_Floor (y_pos / Density);
  200. //
  201. // Clamp the index to the range of existing point cells
  202. //
  203. if (clamp) {
  204. point_y = WWMath::Clamp_Int (point_y, 0, GridPointsY - 1);
  205. }
  206. return point_y;
  207. }
  208. //////////////////////////////////////////////////////////////////////
  209. // Get_Quad_Index_X
  210. //////////////////////////////////////////////////////////////////////
  211. WWINLINE int
  212. EditableHeightfieldClass::Get_Quad_Index_X (float x_pos, bool clamp)
  213. {
  214. int quad_x = WWMath::Float_To_Int_Floor (x_pos / Density);
  215. //
  216. // Clamp the index to the range of existing quad cells
  217. //
  218. if (clamp) {
  219. quad_x = WWMath::Clamp_Int (quad_x, 0, GridPointsX - 2);
  220. }
  221. return quad_x;
  222. }
  223. //////////////////////////////////////////////////////////////////////
  224. // Get_Quad_Index_Y
  225. //////////////////////////////////////////////////////////////////////
  226. WWINLINE int
  227. EditableHeightfieldClass::Get_Quad_Index_Y (float y_pos, bool clamp)
  228. {
  229. int quad_y = WWMath::Float_To_Int_Floor (y_pos / Density);
  230. //
  231. // Clamp the index to the range of existing quad cells
  232. //
  233. if (clamp) {
  234. quad_y = WWMath::Clamp_Int (quad_y, 0, GridPointsY - 2);
  235. }
  236. return quad_y;
  237. }
  238. //////////////////////////////////////////////////////////////////////
  239. // Is_Valid_Quad
  240. //////////////////////////////////////////////////////////////////////
  241. WWINLINE bool
  242. EditableHeightfieldClass::Is_Valid_Quad (int x, int y)
  243. {
  244. bool retval = false;
  245. //
  246. // Simply check to see if the coordinates are inside the patch
  247. //
  248. if ( x >= 0 && x < (GridPointsX - 1) &&
  249. y >= 0 && y < (GridPointsY - 1))
  250. {
  251. retval = true;
  252. }
  253. return retval;
  254. }
  255. #endif //__EDITABLEHEIGHTFIELD_H