meshmdl.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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 : WW3D *
  23. * *
  24. * $Archive:: /Commando/Code/ww3d2/meshmdl.h $*
  25. * *
  26. * Author:: Greg Hjelstrom *
  27. * *
  28. * $Modtime:: 3/14/02 1:30p $*
  29. * *
  30. * $Revision:: 41 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #if defined(_MSC_VER)
  36. #pragma once
  37. #endif
  38. #ifndef MESHMDL_H
  39. #define MESHMDL_H
  40. #include "vector2.h"
  41. #include "vector3.h"
  42. #include "vector4.h"
  43. #include "vector3i.h"
  44. #include "sharebuf.h"
  45. #include "shader.h"
  46. #include "wwdebug.h"
  47. #include "vertmaterial.h"
  48. #include "bittype.h"
  49. #include "colmath.h"
  50. #include "simplevec.h"
  51. #include "wwstring.h"
  52. #include "rinfo.h"
  53. #include "meshgeometry.h"
  54. #include "meshmatdesc.h"
  55. #include "dx8list.h"
  56. class TextureClass;
  57. class RenderInfoClass;
  58. class SpecialRenderInfoClass;
  59. class MatBufferClass;
  60. class TexBufferClass;
  61. class AABoxClass;
  62. class OBBoxClass;
  63. class FrustumClass;
  64. class SphereClass;
  65. class AABTreeClass;
  66. class MaterialInfoClass;
  67. class MeshLoadContextClass;
  68. class MeshSaveContextClass;
  69. class ChunkLoadClass;
  70. class ChunkSaveClass;
  71. class MeshClass;
  72. class HTreeClass;
  73. class DecalGeneratorClass;
  74. class LightEnvironmentClass;
  75. class DX8MeshRendererClass;
  76. class DX8PolygonRendererAttachClass;
  77. class DX8SkinFVFCategoryContainer;
  78. class GapFillerClass;
  79. struct VertexFormatXYZNDUV2;
  80. /**
  81. ** MeshModelClass
  82. ** This class is a repository for all of the geometry information that defines the mesh.
  83. ** Its purpose is to allow separate instances of a mesh to share as much data as possible.
  84. **
  85. ** There are some tricky aspects to this class that may not be immediately obvious. The
  86. ** arrays of pointers to textures and vertex materials must be handled in a special way
  87. ** due to the fact that they are also ref-counted objects which should only be released
  88. ** when the last reference to the array is released (i.e. when no one is using the array
  89. ** any more...)
  90. **
  91. ** Copy/Add_Ref Rules:
  92. ** The purpose of this model was to share data between models whenever possible. To this
  93. ** end, some of the arrays of data are handled differently:
  94. **
  95. ** ALWAYS SHARED: These are *ALWAYS* Add_Ref'd and thus all point to the same array
  96. ** Poly - Connectivity of a mesh are always shared (cannot be changed at runtime)
  97. ** VertexShadeIdx - Shade indices of a mesh are always shared (cannot be changed at runtime)
  98. ** VertexInfluences - Vertex bone attachments are always shared (cannot be changed at runtime)
  99. **
  100. ** SHARED UNTIL SCALED, SKIN DEFORMED, or DAMAGED:
  101. ** Vertex - vertex positions must be copied if any are moved...
  102. ** VertexNorm - vertex normals cannot be shared if a vertex is moved
  103. ** PlaneEq - plane equations cannot be shared if a vertex is moved
  104. ** CullTree - culling tree becomes instance specific if a vertex moves (shouldn't even use this with skins...)
  105. **
  106. ** ALWAYS UNIQUE, BUT SHARE ARRAYS BETWEEN ALTERNATE MATERIAL REPRESENTATIONS (should we share some of these?)
  107. ** UV, DIG, DCG, SCG
  108. ** Texture, Shader, Material,
  109. ** TextureArray, MaterialArray, ShaderArray
  110. */
  111. class MeshModelClass : public MeshGeometryClass
  112. {
  113. public:
  114. MeshModelClass(void);
  115. MeshModelClass(const MeshModelClass & that);
  116. ~MeshModelClass(void);
  117. MeshModelClass & operator = (const MeshModelClass & that);
  118. void Reset(int polycount,int vertcount,int passcount);
  119. void Register_For_Rendering();
  120. void Shadow_Render(SpecialRenderInfoClass & rinfo,const Matrix3D & tm,const HTreeClass * htree);
  121. /////////////////////////////////////////////////////////////////////////////////////
  122. // Material interface, All of these functions call through to the current
  123. // material decription.
  124. /////////////////////////////////////////////////////////////////////////////////////
  125. void Set_Pass_Count(int passes) { CurMatDesc->Set_Pass_Count(passes); }
  126. int Get_Pass_Count(void) const { return CurMatDesc->Get_Pass_Count(); }
  127. const Vector2 * Get_UV_Array(int pass = 0, int stage = 0) { return CurMatDesc->Get_UV_Array(pass,stage); }
  128. int Get_UV_Array_Count(void) { return CurMatDesc->Get_UV_Array_Count(); }
  129. const Vector2 * Get_UV_Array_By_Index(int index) { return CurMatDesc->Get_UV_Array_By_Index(index, false); }
  130. unsigned * Get_DCG_Array(int pass) { return CurMatDesc->Get_DCG_Array(pass); }
  131. unsigned * Get_DIG_Array(int pass) { return CurMatDesc->Get_DIG_Array(pass); }
  132. VertexMaterialClass::ColorSourceType Get_DCG_Source(int pass) { return CurMatDesc->Get_DCG_Source(pass); }
  133. VertexMaterialClass::ColorSourceType Get_DIG_Source(int pass) { return CurMatDesc->Get_DIG_Source(pass); }
  134. unsigned * Get_Color_Array(int array_index,bool create = true) { return CurMatDesc->Get_Color_Array(array_index,create); }
  135. void Set_Single_Material(VertexMaterialClass * vmat,int pass=0) { CurMatDesc->Set_Single_Material(vmat,pass); }
  136. void Set_Single_Texture(TextureClass * tex,int pass=0,int stage=0) { CurMatDesc->Set_Single_Texture(tex,pass,stage); }
  137. void Set_Single_Shader(ShaderClass shader,int pass=0) { CurMatDesc->Set_Single_Shader(shader,pass); }
  138. // the "Get" functions add a reference before returning the pointer (if appropriate)
  139. VertexMaterialClass * Get_Single_Material(int pass=0) const { return CurMatDesc->Get_Single_Material(pass); }
  140. TextureClass * Get_Single_Texture(int pass=0,int stage=0) const { return CurMatDesc->Get_Single_Texture(pass,stage); }
  141. ShaderClass Get_Single_Shader(int pass=0) const { return CurMatDesc->Get_Single_Shader(pass); }
  142. // the "Peek" functions just return the pointer and it's the caller's responsibility to
  143. // maintain a reference to an object with a reference to the data
  144. VertexMaterialClass * Peek_Single_Material(int pass=0) const { return CurMatDesc->Peek_Single_Material(pass); }
  145. TextureClass * Peek_Single_Texture(int pass=0,int stage=0) const { return CurMatDesc->Peek_Single_Texture(pass,stage); }
  146. void Set_Material(int vidx,VertexMaterialClass * vmat,int pass=0) { CurMatDesc->Set_Material(vidx,vmat,pass); }
  147. void Set_Shader(int pidx,ShaderClass shader,int pass=0) { CurMatDesc->Set_Shader(pidx,shader,pass); }
  148. void Set_Texture(int pidx,TextureClass * tex,int pass=0,int stage=0) { CurMatDesc->Set_Texture(pidx,tex,pass,stage); }
  149. // Queries for determining whether this model has per-polygon arrays of Materials, Shaders, or Textures
  150. bool Has_Material_Array(int pass) const { return CurMatDesc->Has_Material_Array(pass); }
  151. bool Has_Shader_Array(int pass) const { return CurMatDesc->Has_Shader_Array(pass); }
  152. bool Has_Texture_Array(int pass,int stage) const { return CurMatDesc->Has_Texture_Array(pass,stage); }
  153. // "Get" functions for Materials, Textures, and Shaders when there are more than one (per-polygon/per-vertex)
  154. VertexMaterialClass * Get_Material(int vidx,int pass=0) const { return CurMatDesc->Get_Material(vidx,pass); }
  155. TextureClass * Get_Texture(int pidx,int pass=0,int stage=0) const { return CurMatDesc->Get_Texture(pidx,pass,stage); }
  156. ShaderClass Get_Shader(int pidx,int pass=0) const { return CurMatDesc->Get_Shader(pidx,pass); }
  157. // "Peek" functions for Materials and Textures when there are more than one (per-polygon/per-vertex)
  158. VertexMaterialClass * Peek_Material(int vidx,int pass=0) const { return CurMatDesc->Peek_Material(vidx,pass); }
  159. TextureClass * Peek_Texture(int pidx,int pass=0,int stage=0) const { return CurMatDesc->Peek_Texture(pidx,pass,stage); }
  160. void Replace_Texture(TextureClass* texture,TextureClass* new_texture);
  161. void Replace_VertexMaterial(VertexMaterialClass* vmat,VertexMaterialClass* new_vmat);
  162. /////////////////////////////////////////////////////////////////////////////////////
  163. // Modification interface. Call these functions to cause the model to ensure
  164. // that the specified array is unique to this instance. (I.e. if the specified
  165. // data is being shared, break the link!)
  166. /////////////////////////////////////////////////////////////////////////////////////
  167. void Make_Geometry_Unique();
  168. void Make_UV_Array_Unique(int pass=0,int stage=0);
  169. void Make_Color_Array_Unique(int array_index=0);
  170. // Load the w3d file format
  171. WW3DErrorType Load_W3D(ChunkLoadClass & cload);
  172. /////////////////////////////////////////////////////////////////////////////////////
  173. // Decal interface
  174. /////////////////////////////////////////////////////////////////////////////////////
  175. void Create_Decal(DecalGeneratorClass * generator, MeshClass * parent);
  176. void Delete_Decal(uint32 decal_id);
  177. /////////////////////////////////////////////////////////////////////////////////////
  178. // Alternate Material Description Interface
  179. // Some models will allow you to alternate between multiple material descriptions
  180. /////////////////////////////////////////////////////////////////////////////////////
  181. void Enable_Alternate_Material_Description(bool onoff);
  182. bool Is_Alternate_Material_Description_Enabled(void);
  183. // Determine whether any rendering feature used by this mesh requires vertex normals
  184. bool Needs_Vertex_Normals(void);
  185. void Init_For_NPatch_Rendering();
  186. const GapFillerClass* Get_Gap_Filler() const { return GapFiller; }
  187. protected:
  188. // MeshClass will set this for skins so that they can get the bone transforms
  189. void Set_HTree(const HTreeClass * htree);
  190. public: // Jani: I need to have an access to these for now...
  191. TexBufferClass * Get_Texture_Array(int pass,int stage,bool create = true)
  192. {
  193. return CurMatDesc->Get_Texture_Array(pass,stage,create);
  194. }
  195. MatBufferClass * Get_Material_Array(int pass,bool create = true)
  196. {
  197. return CurMatDesc->Get_Material_Array(pass,create);
  198. }
  199. ShaderClass * Get_Shader_Array(int pass,bool create = true)
  200. {
  201. return CurMatDesc->Get_Shader_Array(pass,create);
  202. }
  203. protected:
  204. int Register_Type();
  205. // functions to compute the deformed vertices of skins.
  206. // Destination pointers MUST point to arrays large enough to hold all vertices
  207. void get_deformed_vertices(Vector3 *dst_vert, Vector3 *dst_norm, const HTreeClass * htree);
  208. void get_deformed_vertices(Vector3 *dst_vert, const HTreeClass * htree);
  209. void get_deformed_screenspace_vertices(Vector4 *dst_vert,const RenderInfoClass & rinfo,const Matrix3D & mesh_tm,const HTreeClass * htree);
  210. void compose_deformed_vertex_buffer(
  211. VertexFormatXYZNDUV2* verts,
  212. const Vector2* uv0,
  213. const Vector2* uv1,
  214. const unsigned* diffuse,
  215. const HTreeClass * htree);
  216. // loading
  217. WW3DErrorType read_chunks(ChunkLoadClass & cload,MeshLoadContextClass * context);
  218. WW3DErrorType read_texcoords(ChunkLoadClass & cload,MeshLoadContextClass * context);
  219. WW3DErrorType read_materials(ChunkLoadClass & cload,MeshLoadContextClass * context);
  220. WW3DErrorType read_v2_materials(ChunkLoadClass & cload,MeshLoadContextClass * context);
  221. WW3DErrorType read_v3_materials(ChunkLoadClass & cload,MeshLoadContextClass * context);
  222. WW3DErrorType read_per_tri_materials(ChunkLoadClass & cload,MeshLoadContextClass * context);
  223. WW3DErrorType read_vertex_colors(ChunkLoadClass & cload,MeshLoadContextClass * context);
  224. WW3DErrorType read_material_info(ChunkLoadClass & cload,MeshLoadContextClass * context);
  225. WW3DErrorType read_shaders(ChunkLoadClass & cload,MeshLoadContextClass * context);
  226. WW3DErrorType read_vertex_materials(ChunkLoadClass & cload,MeshLoadContextClass * context);
  227. WW3DErrorType read_textures(ChunkLoadClass & cload,MeshLoadContextClass * context);
  228. WW3DErrorType read_material_pass(ChunkLoadClass & cload,MeshLoadContextClass * context);
  229. WW3DErrorType read_vertex_material_ids(ChunkLoadClass & cload,MeshLoadContextClass * context);
  230. WW3DErrorType read_shader_ids(ChunkLoadClass & cload,MeshLoadContextClass * context);
  231. WW3DErrorType read_scg(ChunkLoadClass & cload,MeshLoadContextClass * context);
  232. WW3DErrorType read_dig(ChunkLoadClass & cload,MeshLoadContextClass * context);
  233. WW3DErrorType read_dcg(ChunkLoadClass & cload,MeshLoadContextClass * context);
  234. WW3DErrorType read_texture_stage(ChunkLoadClass & cload,MeshLoadContextClass * context);
  235. WW3DErrorType read_texture_ids(ChunkLoadClass & cload,MeshLoadContextClass * context);
  236. WW3DErrorType read_stage_texcoords(ChunkLoadClass & cload,MeshLoadContextClass * context);
  237. WW3DErrorType read_per_face_texcoord_ids (ChunkLoadClass &cload, MeshLoadContextClass *context);
  238. WW3DErrorType read_prelit_material (ChunkLoadClass &cload, MeshLoadContextClass *context);
  239. // post-processing
  240. void post_process(void);
  241. void post_process_fog(void);
  242. unsigned int get_sort_flags(int pass) const;
  243. unsigned int get_sort_flags(void) const;
  244. void compute_static_sort_levels(void);
  245. // mat info support
  246. void install_materials(MeshLoadContextClass * loadinfo);
  247. void clone_materials(const MeshModelClass & srcmesh);
  248. void install_alternate_material_desc(MeshLoadContextClass * context);
  249. // Material Descriptions
  250. // DefMatDesc - the default material description, allocated in constructor, always present.
  251. // AlternateMatDes - an optional alternate material description, allocated at load time if needed
  252. // CurMatDesc - pointer to the currently active material description.
  253. MeshMatDescClass * DefMatDesc;
  254. MeshMatDescClass * AlternateMatDesc;
  255. MeshMatDescClass * CurMatDesc;
  256. // Collection of the unique materials in the mesh
  257. MaterialInfoClass * MatInfo;
  258. // Jani: Adding this here temporarily... must fine better place
  259. GapFillerClass * GapFiller;
  260. bool HasBeenInUse; // For debugging purposes!
  261. friend class MeshClass;
  262. friend class MeshDeformSetClass;
  263. friend class MeshDeformClass;
  264. friend class MeshLoadContextClass;
  265. friend class DX8SkinFVFCategoryContainer;
  266. friend class DX8MeshRendererClass;
  267. friend class DX8PolygonRendererClass;
  268. };
  269. /**
  270. ** GapFillerClass
  271. ** This class is used to generate gap-filling polygons for "N-Patched" meshes
  272. */
  273. class GapFillerClass
  274. {
  275. TriIndex* PolygonArray;
  276. unsigned PolygonCount;
  277. unsigned ArraySize;
  278. TextureClass** TextureArray[MeshMatDescClass::MAX_PASSES][MeshMatDescClass::MAX_TEX_STAGES];
  279. VertexMaterialClass** MaterialArray[MeshMatDescClass::MAX_PASSES];
  280. ShaderClass* ShaderArray[MeshMatDescClass::MAX_PASSES];
  281. MeshModelClass* mmc;
  282. GapFillerClass& operator = (const GapFillerClass & that) {}
  283. public:
  284. GapFillerClass(MeshModelClass* mmc);
  285. GapFillerClass(const GapFillerClass& that);
  286. ~GapFillerClass();
  287. WWINLINE const TriIndex* Get_Polygon_Array() const { return PolygonArray; }
  288. WWINLINE unsigned Get_Polygon_Count() const { return PolygonCount; }
  289. WWINLINE TextureClass** Get_Texture_Array(int pass, int stage) const { return TextureArray[pass][stage]; }
  290. WWINLINE VertexMaterialClass** Get_Material_Array(int pass) const { return MaterialArray[pass]; }
  291. WWINLINE ShaderClass* Get_Shader_Array(int pass) const { return ShaderArray[pass]; }
  292. void Add_Polygon(unsigned polygon_index,unsigned vidx1,unsigned vidx2, unsigned vidx3);
  293. void Shrink_Buffers();
  294. };
  295. #endif