metalmap.cpp 22 KB

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