metalmap.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /*
  2. ** Command & Conquer Generals(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 : G *
  23. * *
  24. * $Archive:: /Commando/Code/ww3d2/metalmap.cpp $*
  25. * *
  26. * $Author:: Hector_y $*
  27. * *
  28. * $Modtime:: 6/27/01 4:59p $*
  29. * *
  30. * $Revision:: 3 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * MMMC::MetalMapManagerClass -- Create metal map manager according to given metal parameters*
  35. * MMMC::MetalMapManagerClass -- Create metal map manager from INI *
  36. * MMMC::~MetalMapManagerClass -- MetalMapManagerClass destructor *
  37. * MMMC::Get_Metal_Map -- Get the texture for a metal map by id number *
  38. * MMMC::Metal_Map_Count -- Get the number of metal maps in the manager *
  39. * MMMC::Update_Lighting -- Update the lighting parameters used for generating the maps *
  40. * MMMC::Update_Textures -- Update metal map textures (call once/frame before rendering) *
  41. * MMMC::initialize_normal_table -- Utility function to initialize the normal table *
  42. * MMMC::initialize_metal_params -- Utility function (shared CTor code) *
  43. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  44. #include "metalmap.h"
  45. #include "texture.h"
  46. #include "ww3dformat.h"
  47. #include <vp.h>
  48. #include <ini.h>
  49. #include <point.h>
  50. #include <stdio.h>
  51. #include <hashtemplate.h>
  52. #include <wwstring.h>
  53. /*
  54. ** Class static members:
  55. */
  56. Vector3 * MetalMapManagerClass::_NormalTable = 0;
  57. /***********************************************************************************************
  58. * MMMC::MetalMapManagerClass -- Create metal map manager from INI *
  59. * *
  60. * *
  61. * INPUT: *
  62. * *
  63. * OUTPUT: *
  64. * *
  65. * WARNINGS: *
  66. * *
  67. * HISTORY: *
  68. * 11/23/1999 NH : Created. *
  69. *=============================================================================================*/
  70. MetalMapManagerClass::MetalMapManagerClass(INIClass &ini) :
  71. MapCount(0),
  72. Textures(0),
  73. MetalParameters(0),
  74. CurrentAmbient(0.0f, 0.0f, 0.0f),
  75. CurrentMainLightColor(0.0f, 0.0f, 0.0f),
  76. CurrentMainLightDir(1.0f, 0.0f, 0.0f),
  77. CurrentCameraDir(1.0f,0.0f,0.0f)
  78. {
  79. // If the static normal table has not been initialized yet, initialize it
  80. if (!_NormalTable) {
  81. initialize_normal_table();
  82. }
  83. // Determine how many metals are in this file
  84. char section[255];
  85. for (int lp = 0; ; lp++) {
  86. sprintf(section, "Metal%02d", lp);
  87. if (!ini.Find_Section(section)) {
  88. break; // NAK - Mar 8, 2000: changed to a break to fix off by one error in lp
  89. }
  90. }
  91. if (lp > 0) {
  92. // Create metal params structs and fill from INI:
  93. MetalParams *metal_params = W3DNEWARRAY MetalParams[lp];
  94. TPoint3D<float> white_tpoint(255.0f, 255.0f, 255.0f);
  95. Vector3 white(1.0f, 1.0f, 1.0f);
  96. Vector3 black(0.0f, 0.0f, 0.0f);
  97. for (int i = 0; i < lp; i++) {
  98. sprintf(section, "Metal%02d", i);
  99. static const float cf = 0.003921568627451f; // 1 / 255
  100. TPoint3D<float> color;
  101. color = ini.Get_Point(section, "AmbientColor", white_tpoint);
  102. metal_params[i].AmbientColor.Set(color.X * cf, color.Y * cf, color.Z * cf);
  103. metal_params[i].AmbientColor.Update_Min(white);
  104. metal_params[i].AmbientColor.Update_Max(black);
  105. color = ini.Get_Point(section, "DiffuseColor", white_tpoint);
  106. metal_params[i].DiffuseColor.Set(color.X * cf, color.Y * cf, color.Z * cf);
  107. metal_params[i].AmbientColor.Update_Min(white);
  108. metal_params[i].AmbientColor.Update_Max(black);
  109. color = ini.Get_Point(section, "SpecularColor", white_tpoint);
  110. metal_params[i].SpecularColor.Set(color.X * cf, color.Y * cf, color.Z * cf);
  111. metal_params[i].AmbientColor.Update_Min(white);
  112. metal_params[i].AmbientColor.Update_Max(black);
  113. float shininess = ini.Get_Float(section, "Shininess", 0.0f);
  114. metal_params[i].Shininess = WWMath::Clamp(shininess, 0.0f, 127.0f);
  115. }
  116. initialize_metal_params(lp, metal_params);
  117. delete [] metal_params;
  118. } else {
  119. assert(0);
  120. }
  121. for (int i = 0; i < lp; i++) {
  122. // Create texture. NOTE: we should add code here to ensure we use a native texture format
  123. Textures[i]=NEW_REF(TextureClass,(METALMAP_SIZE,METALMAP_SIZE,WW3D_FORMAT_A8R8G8B8,TextureClass::MIP_LEVELS_1));
  124. Textures[i]->Set_U_Addr_Mode(TextureClass::TEXTURE_ADDRESS_CLAMP);
  125. Textures[i]->Set_V_Addr_Mode(TextureClass::TEXTURE_ADDRESS_CLAMP);
  126. StringClass tex_name;
  127. tex_name.Format("!m%02d.tga", i);
  128. Textures[i]->Set_Texture_Name(tex_name);
  129. }
  130. }
  131. /***********************************************************************************************
  132. * MMMC::~MetalMapManagerClass -- MetalMapManagerClass destructor *
  133. * *
  134. * *
  135. * INPUT: *
  136. * *
  137. * OUTPUT: *
  138. * *
  139. * WARNINGS: *
  140. * *
  141. * HISTORY: *
  142. * 11/19/1999 NH : Created. *
  143. *=============================================================================================*/
  144. MetalMapManagerClass::~MetalMapManagerClass(void)
  145. {
  146. if (Textures) {
  147. for (int i = 0; i < MapCount; i++) {
  148. REF_PTR_RELEASE(Textures[i]);
  149. }
  150. delete [] Textures;
  151. Textures = 0;
  152. }
  153. if (MetalParameters) {
  154. delete [] MetalParameters;
  155. MetalParameters = 0;
  156. }
  157. }
  158. /***********************************************************************************************
  159. * MMMC::Get_Metal_Map -- Get the texture for a metal map by id number *
  160. * *
  161. * *
  162. * INPUT: *
  163. * *
  164. * OUTPUT: *
  165. * *
  166. * WARNINGS: *
  167. * *
  168. * HISTORY: *
  169. * 11/19/1999 NH : Created. *
  170. *=============================================================================================*/
  171. TextureClass * MetalMapManagerClass::Get_Metal_Map(int id)
  172. {
  173. if (id < 0 || id >= MapCount) return 0;
  174. Textures[id]->Add_Ref();
  175. return Textures[id];
  176. }
  177. /***********************************************************************************************
  178. * MMMC::Metal_Map_Count -- Get the number of metal maps in the manager *
  179. * *
  180. * *
  181. * INPUT: *
  182. * *
  183. * OUTPUT: *
  184. * *
  185. * WARNINGS: *
  186. * *
  187. * HISTORY: *
  188. * 11/19/1999 NH : Created. *
  189. *=============================================================================================*/
  190. int MetalMapManagerClass::Metal_Map_Count(void)
  191. {
  192. return MapCount;
  193. }
  194. /***********************************************************************************************
  195. * MMMC::Update_Lighting -- Update the lighting parameters used for generating the maps *
  196. * *
  197. * *
  198. * INPUT: *
  199. * *
  200. * OUTPUT: *
  201. * *
  202. * WARNINGS: *
  203. * *
  204. * HISTORY: *
  205. * 11/19/1999 NH : Created. *
  206. *=============================================================================================*/
  207. void MetalMapManagerClass::Update_Lighting(const Vector3& ambient, const Vector3& main_light_color,
  208. const Vector3& main_light_dir, const Vector3& camera_dir)
  209. {
  210. CurrentAmbient = ambient;
  211. CurrentMainLightColor = main_light_color;
  212. CurrentMainLightDir = main_light_dir;
  213. CurrentCameraDir= camera_dir;
  214. }
  215. /***********************************************************************************************
  216. * MMMC::Update_Textures -- Update metal map textures (call once/frame before rendering) *
  217. * *
  218. * *
  219. * INPUT: *
  220. * *
  221. * OUTPUT: *
  222. * *
  223. * WARNINGS: *
  224. * *
  225. * HISTORY: *
  226. * 11/19/1999 NH : Created. *
  227. *=============================================================================================*/
  228. void MetalMapManagerClass::Update_Textures(void)
  229. {
  230. // Currently the lighting is done using a simple Phong (actually Blinn) model.
  231. Vector3 &l = CurrentMainLightDir;
  232. Vector3 &v = CurrentCameraDir;
  233. // Calculate halfway vector
  234. Vector3 h = l+v;
  235. h.Normalize();
  236. // NOTE: when our lighting equation gets more complicated we might want to do some testing to
  237. // detect zero components...
  238. // Calculate quantities which are the same for all metal maps
  239. float n_dot_l[METALMAP_SIZE_2];
  240. float n_dot_h[METALMAP_SIZE_2];
  241. VectorProcessorClass::DotProduct(n_dot_l,l,_NormalTable,METALMAP_SIZE_2);
  242. VectorProcessorClass::ClampMin(n_dot_l, n_dot_l, 0.0f, METALMAP_SIZE_2);
  243. VectorProcessorClass::DotProduct(n_dot_h,h,_NormalTable, METALMAP_SIZE_2);
  244. VectorProcessorClass::ClampMin(n_dot_h, n_dot_h, 0.0f, METALMAP_SIZE_2);
  245. // Loop over each metal map and update it
  246. for (int i = 0; i < MapCount; i++) {
  247. MetalParams &cur_params = MetalParameters[i];
  248. // If shinyness > 1, apply it to specular value array
  249. float *specular = 0;
  250. float temp_specular[METALMAP_SIZE_2];
  251. float shinyness = cur_params.Shininess;
  252. if (shinyness > 1.0f) {
  253. VectorProcessorClass::Power(temp_specular, n_dot_h, shinyness, METALMAP_SIZE_2);
  254. specular = &(temp_specular[0]);
  255. } else {
  256. specular = &(n_dot_h[0]);
  257. }
  258. // Generate metal map row by row
  259. Vector3 specular_color(cur_params.SpecularColor.X * CurrentMainLightColor.X,
  260. cur_params.SpecularColor.Y * CurrentMainLightColor.Y,
  261. cur_params.SpecularColor.Z * CurrentMainLightColor.Z);
  262. Vector3 diffuse_color(cur_params.DiffuseColor.X * CurrentMainLightColor.X,
  263. cur_params.DiffuseColor.Y * CurrentMainLightColor.Y,
  264. cur_params.DiffuseColor.Z * CurrentMainLightColor.Z);
  265. Vector3 ambient_color(cur_params.AmbientColor.X * CurrentAmbient.X,
  266. cur_params.AmbientColor.Y * CurrentAmbient.Y,
  267. cur_params.AmbientColor.Z * CurrentAmbient.Z);
  268. Vector3 white(1.0f, 1.0f, 1.0f);
  269. SurfaceClass * metal_map_surface = Textures[i]->Get_Surface_Level(0);
  270. int pitch;
  271. unsigned char *map=(unsigned char *) metal_map_surface->Lock(&pitch);
  272. int idx=0;
  273. for (int y = 0; y < METALMAP_SIZE; y++) {
  274. for (int x = 0; x < METALMAP_SIZE; x++) {
  275. Vector3 result = ambient_color + (diffuse_color * n_dot_l[idx]) + (specular_color * specular[idx]);
  276. result.Update_Min(white); // Clamp to white
  277. map[4*x] = (unsigned char)floor(result.Z * 255.99f); // B
  278. map[4*x+1] = (unsigned char)floor(result.Y * 255.99f); // G
  279. map[4*x+2] = (unsigned char)floor(result.X * 255.99f); // R
  280. map[4*x+3] = 0xFF; // A
  281. idx++;
  282. }
  283. map+=pitch;
  284. }
  285. metal_map_surface->Unlock();
  286. REF_PTR_RELEASE(metal_map_surface);
  287. } // for i
  288. }
  289. /***********************************************************************************************
  290. * MMMC::initialize_normal_table -- Utility function to initialize the normal table *
  291. * *
  292. * *
  293. * INPUT: *
  294. * *
  295. * OUTPUT: *
  296. * *
  297. * WARNINGS: *
  298. * *
  299. * HISTORY: *
  300. * 11/19/1999 NH : Created. *
  301. *=============================================================================================*/
  302. void MetalMapManagerClass::initialize_normal_table(void)
  303. {
  304. // NOTE: changing the actual static _NormalTable member must be the last thing this function
  305. // does to avoid synchronization errors.
  306. static Vector3 _normal_table[METALMAP_SIZE_2];
  307. // Calculate vectors (area outside sphere should be filled with a radial fill of the vectors at
  308. // the sphere's edge to avoid aliasing artifacts)
  309. float step = 2.0f / (float)METALMAP_SIZE;
  310. int idx = 0;
  311. for (int y = 0; y < METALMAP_SIZE; y++) {
  312. for (int x = 0; x < METALMAP_SIZE; x++) {
  313. Vector3 &normal = _normal_table[idx];
  314. // Set vector to point to surface of unit sphere
  315. normal.Set((step * (float)x) - 1.0f, (step * (float)y) - 1.0f, 0.0f);
  316. float z2 = 1 - ((normal.X * normal.X) + (normal.Y * normal.Y));
  317. z2 = MAX(z2, 0.0f); // If outside the sphere, treat as if on its edge
  318. normal.Z = sqrt(z2);
  319. normal.Normalize(); // Needed for "outside sphere" case and for safety's sake
  320. idx++;
  321. }
  322. }
  323. _NormalTable = &(_normal_table[0]);
  324. }
  325. /***********************************************************************************************
  326. * MMMC::initialize_metal_params -- Utility function (shared CTor code) *
  327. * *
  328. * *
  329. * INPUT: *
  330. * *
  331. * OUTPUT: *
  332. * *
  333. * WARNINGS: *
  334. * *
  335. * HISTORY: *
  336. * 11/23/1999 NH : Created. *
  337. *=============================================================================================*/
  338. void MetalMapManagerClass::initialize_metal_params(int map_count, MetalParams *metal_params)
  339. {
  340. MapCount = map_count;
  341. if (MapCount > 0) {
  342. Textures = W3DNEWARRAY TextureClass *[MapCount];
  343. MetalParameters = W3DNEWARRAY MetalParams[MapCount];
  344. for (int i = 0; i < MapCount; i++) {
  345. // Copy metal parameters (assert if invalid)
  346. MetalParameters[i] = metal_params[i];
  347. assert(MetalParameters[i].AmbientColor.X >= 0.0f && MetalParameters[i].AmbientColor.X <= 1.0f);
  348. assert(MetalParameters[i].AmbientColor.Y >= 0.0f && MetalParameters[i].AmbientColor.Y <= 1.0f);
  349. assert(MetalParameters[i].AmbientColor.Z >= 0.0f && MetalParameters[i].AmbientColor.Z <= 1.0f);
  350. assert(MetalParameters[i].DiffuseColor.X >= 0.0f && MetalParameters[i].DiffuseColor.X <= 1.0f);
  351. assert(MetalParameters[i].DiffuseColor.Y >= 0.0f && MetalParameters[i].DiffuseColor.Y <= 1.0f);
  352. assert(MetalParameters[i].DiffuseColor.Z >= 0.0f && MetalParameters[i].DiffuseColor.Z <= 1.0f);
  353. assert(MetalParameters[i].SpecularColor.X >= 0.0f && MetalParameters[i].SpecularColor.X <= 1.0f);
  354. assert(MetalParameters[i].SpecularColor.Y >= 0.0f && MetalParameters[i].SpecularColor.Y <= 1.0f);
  355. assert(MetalParameters[i].SpecularColor.Z >= 0.0f && MetalParameters[i].SpecularColor.Z <= 1.0f);
  356. assert(MetalParameters[i].Shininess > 0.0f);
  357. }
  358. }
  359. }