vertmaterial.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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:: /VSS_Sync/ww3d2/vertmaterial.h $*
  25. * *
  26. * Author:: Greg Hjelstrom *
  27. * *
  28. * $Modtime:: 8/29/01 7:29p $*
  29. * *
  30. * $Revision:: 26 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #if defined(_MSC_VER)
  36. #pragma once
  37. #endif
  38. #ifndef VERTMATERIAL_H
  39. #define VERTMATERIAL_H
  40. #include "always.h"
  41. #include "refcount.h"
  42. #include "vector3.h"
  43. #include "w3d_file.h"
  44. #include "meshbuild.h"
  45. #include "w3derr.h"
  46. #include "mapper.h"
  47. #include "wwstring.h"
  48. #include <string.h>
  49. class ChunkLoadClass;
  50. class ChunkSaveClass;
  51. struct _D3DMATERIAL8;
  52. /**
  53. ** VertexMaterialClass
  54. ** This is simply the typical W3D thin-wrapper around the surrender vertex material.
  55. ** The vertex material defines things like the lighting properties of a vertex.
  56. */
  57. class VertexMaterialClass : public RefCountClass
  58. {
  59. friend DX8Wrapper;
  60. public:
  61. /*
  62. ** Similar to the TextureClass, these enumerations are set up to be exactly the same as
  63. ** the surrender material enumerations. Nearly all functions in this class are inline
  64. ** so that it will go away since the only purpose of Material Class is to protect our
  65. ** application code from the ever-changing surrender...
  66. */
  67. enum MappingType {
  68. MAPPING_NONE = -1, // no mapping needed
  69. MAPPING_UV = W3DMAPPING_UV, // default, use the u-v values in the model
  70. MAPPING_ENVIRONMENT = W3DMAPPING_ENVIRONMENT, // use the environment mapper
  71. };
  72. enum FlagsType {
  73. DEPTH_CUE= 0, // enable depth cueing (default = false)
  74. DEPTH_CUE_TO_ALPHA,
  75. COPY_SPECULAR_TO_DIFFUSE,
  76. };
  77. enum ColorSourceType {
  78. MATERIAL = 0, // D3DMCS_MATERIAL - the color source should be taken from the material setting
  79. COLOR1, // D3DMCS_COLOR1 - the color should be taken from per-vertex color array 1 (aka D3DFVF_DIFFUSE)
  80. COLOR2, // D3DMCS_COLOR2 - the color should be taken from per-vertex color array 2 (aka D3DFVF_SPECULAR)
  81. };
  82. enum PresetType
  83. {
  84. PRELIT_DIFFUSE=0,
  85. PRELIT_NODIFFUSE,
  86. PRESET_COUNT
  87. };
  88. VertexMaterialClass(void);
  89. VertexMaterialClass(const VertexMaterialClass & src);
  90. ~VertexMaterialClass(void);
  91. VertexMaterialClass & operator = (const VertexMaterialClass &src);
  92. VertexMaterialClass * Clone(void) { VertexMaterialClass * mat = NEW_REF (VertexMaterialClass,()); *mat = *this; return mat;}
  93. /*
  94. ** Name Access
  95. */
  96. void Set_Name(const char * name)
  97. {
  98. Name = name;
  99. }
  100. const char * Get_Name(void) const
  101. {
  102. return Name;
  103. }
  104. /*
  105. ** Control over the material flags
  106. */
  107. void Set_Flag(FlagsType flag,bool onoff)
  108. {
  109. CRCDirty=true;
  110. if (onoff)
  111. Flags|=(1<<flag);
  112. else
  113. Flags&=~(1<<flag);
  114. }
  115. int Get_Flag(FlagsType flag)
  116. { return (Flags>>flag) & 0x1; }
  117. /*
  118. ** Basic material properties
  119. */
  120. float Get_Shininess(void) const;
  121. void Set_Shininess(float shin);
  122. float Get_Opacity(void) const;
  123. void Set_Opacity(float o);
  124. void Get_Ambient(Vector3 * set_color) const;
  125. void Set_Ambient(const Vector3 & color);
  126. void Set_Ambient(float r,float g,float b);
  127. void Get_Diffuse(Vector3 * set_color) const;
  128. void Set_Diffuse(const Vector3 & color);
  129. void Set_Diffuse(float r,float g,float b);
  130. void Get_Specular(Vector3 * set_color) const;
  131. void Set_Specular(const Vector3 & color);
  132. void Set_Specular(float r,float g,float b);
  133. void Get_Emissive(Vector3 * set_color) const;
  134. void Set_Emissive(const Vector3 & color);
  135. void Set_Emissive(float r,float g,float b);
  136. void Set_Lighting(bool lighting) { CRCDirty=true; UseLighting=lighting; };
  137. bool Get_Lighting() const { return UseLighting; };
  138. /*
  139. ** Color source control. Note that if you set one of the sources to be one of
  140. ** the arrays, then the setting in the material is ignored. (i.e. if you
  141. ** set the diffuse source to array0, then the diffuse color set into the
  142. ** vertex material is ignored.
  143. */
  144. void Set_Ambient_Color_Source(ColorSourceType src);
  145. ColorSourceType Get_Ambient_Color_Source(void);
  146. void Set_Emissive_Color_Source(ColorSourceType src);
  147. ColorSourceType Get_Emissive_Color_Source(void);
  148. void Set_Diffuse_Color_Source(ColorSourceType src);
  149. ColorSourceType Get_Diffuse_Color_Source(void);
  150. /*
  151. ** UV source control. The DX8 FVF can support up to 8 uv-arrays. The vertex
  152. ** material can/must be configured to index to the uv-arrays that you want to
  153. ** use for the two texture stages.
  154. */
  155. void Set_UV_Source(int stage,int array_index);
  156. int Get_UV_Source(int stage);
  157. /*
  158. ** Mapper control.
  159. */
  160. inline void Set_Mapper(TextureMapperClass *mapper,int stage=0);
  161. inline TextureMapperClass * Get_Mapper(int stage=0);
  162. inline TextureMapperClass * Peek_Mapper(int stage=0);
  163. inline void Reset_Mappers(void);
  164. /*
  165. ** Loading and saving to W3D files
  166. */
  167. WW3DErrorType Load_W3D(ChunkLoadClass & cload);
  168. WW3DErrorType Save_W3D(ChunkSaveClass & csave);
  169. void Init_From_Material3(const W3dMaterial3Struct & mat3);
  170. /*
  171. ** CRC, used by the loading code to build a list of the unique materials
  172. */
  173. inline unsigned long Get_CRC(void) const
  174. {
  175. if (CRCDirty) {
  176. CRC=Compute_CRC();
  177. CRCDirty=false;
  178. }
  179. return CRC;
  180. }
  181. /*
  182. ** Test whether this material is using any mappers which require vertex normals
  183. */
  184. bool Do_Mappers_Need_Normals(void) const;
  185. /*
  186. ** Test whether this material is using any mappers which are time-variant
  187. */
  188. bool Are_Mappers_Time_Variant(void) const;
  189. // Infrastructure to support presets
  190. static void Init();
  191. static void Shutdown();
  192. // TODO: Make Get_Preset const
  193. static VertexMaterialClass *Get_Preset(PresetType type);
  194. void Make_Unique();
  195. protected:
  196. // We're using the pointer instead of the actual structure
  197. // so we don't have to include the d3d header - HY
  198. _D3DMATERIAL8 * Material;
  199. unsigned int Flags;
  200. unsigned int AmbientColorSource;
  201. unsigned int EmissiveColorSource;
  202. unsigned int DiffuseColorSource;
  203. StringClass Name;
  204. TextureMapperClass * Mapper[MeshBuilderClass::MAX_STAGES];
  205. unsigned int UVSource[MeshBuilderClass::MAX_STAGES];
  206. bool UseLighting;
  207. unsigned int UniqueID;
  208. mutable unsigned long CRC;
  209. mutable bool CRCDirty;
  210. private:
  211. /*
  212. ** Apply the render states to D3D
  213. */
  214. void Apply(void) const;
  215. /*
  216. ** Apply the render states corresponding to a NULL vetex material to D3D
  217. */
  218. static void Apply_Null(void);
  219. unsigned long VertexMaterialClass::Compute_CRC(void) const;
  220. static VertexMaterialClass *Presets[PRESET_COUNT];
  221. };
  222. inline void VertexMaterialClass::Set_Mapper(TextureMapperClass *mapper, int stage)
  223. {
  224. CRCDirty=true;
  225. REF_PTR_SET(Mapper[stage],mapper);
  226. }
  227. inline TextureMapperClass * VertexMaterialClass::Get_Mapper(int stage)
  228. {
  229. if (Mapper[stage]) {
  230. Mapper[stage]->Add_Ref();
  231. }
  232. return Mapper[stage];
  233. }
  234. inline TextureMapperClass * VertexMaterialClass::Peek_Mapper(int stage)
  235. {
  236. return Mapper[stage];
  237. }
  238. inline void VertexMaterialClass::Reset_Mappers(void)
  239. {
  240. for (int stage = 0; stage < MeshBuilderClass::MAX_STAGES; stage++) {
  241. if (Mapper[stage]) {
  242. Mapper[stage]->Reset();
  243. }
  244. }
  245. }
  246. inline bool VertexMaterialClass::Do_Mappers_Need_Normals(void) const
  247. {
  248. for (int stage = 0; stage < MeshBuilderClass::MAX_STAGES; stage++) {
  249. if (Mapper[stage] && (Mapper[stage]->Needs_Normals())) return true;
  250. }
  251. return false;
  252. }
  253. inline bool VertexMaterialClass::Are_Mappers_Time_Variant(void) const
  254. {
  255. for (int stage = 0; stage < MeshBuilderClass::MAX_STAGES; stage++) {
  256. if (Mapper[stage] && (Mapper[stage]->Is_Time_Variant())) return true;
  257. }
  258. return false;
  259. }
  260. #endif //VERTMATERIAL_H