ww3dformat.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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 : WW3D *
  23. * *
  24. * $Archive:: /Commando/Code/ww3d2/ww3dformat.cpp $*
  25. * *
  26. * Original Author:: Hector Yee *
  27. * *
  28. * $Author:: Jani_p $*
  29. * *
  30. * $Modtime:: 8/20/01 11:51a $*
  31. * *
  32. * $Revision:: 5 $*
  33. * *
  34. *---------------------------------------------------------------------------------------------*
  35. * Functions: *
  36. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  37. #include "ww3dformat.h"
  38. #include "vector4.h"
  39. #include "wwdebug.h"
  40. #include "targa.h"
  41. #include "dx8wrapper.h"
  42. #include "dx8caps.h"
  43. #include <d3d8.h>
  44. /*
  45. WW3D_FORMAT_UNKNOWN=0,
  46. WW3D_FORMAT_R8G8B8,
  47. WW3D_FORMAT_A8R8G8B8,
  48. WW3D_FORMAT_X8R8G8B8,
  49. WW3D_FORMAT_R5G6B5,
  50. WW3D_FORMAT_X1R5G5B5,
  51. WW3D_FORMAT_A1R5G5B5,
  52. WW3D_FORMAT_A4R4G4B4,
  53. WW3D_FORMAT_R3G3B2,
  54. WW3D_FORMAT_A8,
  55. WW3D_FORMAT_A8R3G3B2,
  56. WW3D_FORMAT_X4R4G4B4,
  57. WW3D_FORMAT_A8P8,
  58. WW3D_FORMAT_P8,
  59. WW3D_FORMAT_L8,
  60. WW3D_FORMAT_A8L8,
  61. WW3D_FORMAT_A4L4,
  62. WW3D_FORMAT_COUNT // Used only to determine number of surface formats
  63. */
  64. // extract the luminance from the RGB using the CIE 709 standard
  65. unsigned char RGB_to_CIEY(Vector4 color)
  66. {
  67. float lum=0.2126f*color.X + 0.7152f*color.Y + 0.0722f*color.Z;
  68. return (unsigned char) (255.0f*lum);
  69. }
  70. void Vector4_to_Color(unsigned int *outc,const Vector4 &inc,const WW3DFormat format)
  71. {
  72. // convert to ARGB 32-bit
  73. unsigned int color=DX8Wrapper::Convert_Color(inc);
  74. unsigned char *argb=(unsigned char*) &color;
  75. unsigned char r,g,b,a,lum;
  76. switch (format)
  77. {
  78. case WW3D_FORMAT_R8G8B8:
  79. case WW3D_FORMAT_A8R8G8B8:
  80. case WW3D_FORMAT_X8R8G8B8:
  81. *outc=color;
  82. break;
  83. case WW3D_FORMAT_R5G6B5:
  84. r=argb[1] >> 3;
  85. g=argb[2] >> 2;
  86. b=argb[3] >> 3;
  87. *outc=(r << 11) | (g<<5) | b;
  88. break;
  89. case WW3D_FORMAT_X1R5G5B5:
  90. case WW3D_FORMAT_A1R5G5B5:
  91. a=argb[0] >> 7;
  92. r=argb[1] >> 3;
  93. g=argb[2] >> 3;
  94. b=argb[3] >> 3;
  95. *outc=(a<<15) | (r<<10) | (g<<5) | b;
  96. break;
  97. case WW3D_FORMAT_A4R4G4B4:
  98. case WW3D_FORMAT_X4R4G4B4:
  99. a=argb[0] >> 4;
  100. r=argb[1] >> 4;
  101. g=argb[2] >> 4;
  102. b=argb[3] >> 4;
  103. *outc=(a<<12) | (r<<8) | (g<<4) | b;
  104. break;
  105. case WW3D_FORMAT_R3G3B2:
  106. case WW3D_FORMAT_A8R3G3B2:
  107. a=argb[0];
  108. r=argb[1] >> 5;
  109. g=argb[2] >> 5;
  110. b=argb[3] >> 6;
  111. *outc=(a<<8) | (r<<5) | (g<<2) | b;
  112. break;
  113. case WW3D_FORMAT_A8:
  114. *outc=argb[0];
  115. break;
  116. case WW3D_FORMAT_L8:
  117. lum=RGB_to_CIEY(inc);
  118. *outc=lum;
  119. break;
  120. case WW3D_FORMAT_A8L8:
  121. a=argb[0];
  122. lum=RGB_to_CIEY(inc);
  123. *outc=(a<<8) | lum;
  124. break;
  125. case WW3D_FORMAT_A4L4:
  126. a=argb[0] >> 4;
  127. lum=RGB_to_CIEY(inc);
  128. lum=lum>>4;
  129. *outc=(a<<4) | lum;
  130. break;
  131. default:
  132. WWASSERT(0);
  133. }
  134. }
  135. void Color_to_Vector4(Vector4* outc,const unsigned int inc,const WW3DFormat format)
  136. {
  137. WWASSERT(outc);
  138. unsigned char *argb=(unsigned char*) &inc;
  139. unsigned char a,r,g,b;
  140. a=r=g=b=0;
  141. switch (format)
  142. {
  143. case WW3D_FORMAT_R8G8B8:
  144. case WW3D_FORMAT_A8R8G8B8:
  145. case WW3D_FORMAT_X8R8G8B8:
  146. a=argb[0];
  147. r=argb[1];
  148. g=argb[2];
  149. b=argb[3];
  150. break;
  151. case WW3D_FORMAT_R5G6B5:
  152. r=argb[1]<<3;
  153. g=argb[2]<<2;
  154. b=argb[3]<<3;
  155. break;
  156. case WW3D_FORMAT_X1R5G5B5:
  157. case WW3D_FORMAT_A1R5G5B5:
  158. a=argb[0]<<7;
  159. r=argb[1]<<3;
  160. g=argb[2]<<3;
  161. b=argb[3]<<3;
  162. break;
  163. case WW3D_FORMAT_A4R4G4B4:
  164. a=argb[0]<<4;
  165. r=argb[1]<<4;
  166. g=argb[2]<<4;
  167. b=argb[3]<<4;
  168. break;
  169. case WW3D_FORMAT_R3G3B2:
  170. r=argb[1]<<5;
  171. g=argb[2]<<5;
  172. b=argb[3]<<6;
  173. break;
  174. case WW3D_FORMAT_A8:
  175. a=argb[0];
  176. break;
  177. case WW3D_FORMAT_A8R3G3B2:
  178. a=argb[0];
  179. r=argb[1]<<5;
  180. g=argb[2]<<5;
  181. b=argb[3]<<6;
  182. break;
  183. case WW3D_FORMAT_X4R4G4B4:
  184. r=argb[1]<<4;
  185. g=argb[2]<<4;
  186. b=argb[3]<<4;
  187. break;
  188. default:
  189. WWASSERT(0);
  190. }
  191. outc->X=r/255.0f;
  192. outc->Y=g/255.0f;
  193. outc->Z=b/255.0f;
  194. outc->W=a/255.0f;
  195. }
  196. // ----------------------------------------------------------------------------
  197. //
  198. // Utility function for determining WW3D format from TGA file header.
  199. //
  200. // ----------------------------------------------------------------------------
  201. void Get_WW3D_Format(WW3DFormat& dest_format,WW3DFormat& src_format,unsigned& src_bpp,const Targa& targa)
  202. {
  203. // Guess the format from the TGA Header bits:
  204. src_format = WW3D_FORMAT_UNKNOWN;
  205. src_bpp=0;
  206. switch (targa.Header.PixelDepth) {
  207. case 32: src_format = WW3D_FORMAT_A8R8G8B8; src_bpp=4; break;
  208. case 24: src_format = WW3D_FORMAT_R8G8B8; src_bpp=3; break;
  209. case 16: src_format = WW3D_FORMAT_A1R5G5B5; src_bpp=2; break;
  210. case 8:
  211. src_bpp=1;
  212. if (targa.Header.ColorMapType == 1) src_format = WW3D_FORMAT_P8;
  213. else if (targa.Header.ImageType == TGA_MONO) src_format = WW3D_FORMAT_L8;
  214. else src_format = WW3D_FORMAT_A8;
  215. break;
  216. default:
  217. WWDEBUG_SAY(("TextureClass: Targa has unsupported bitdepth(%i)\n",targa.Header.PixelDepth));
  218. WWASSERT(0);
  219. break;
  220. }
  221. dest_format=src_format;
  222. if ((dest_format==WW3D_FORMAT_P8) || (dest_format==WW3D_FORMAT_L8)) {
  223. dest_format=WW3D_FORMAT_X8R8G8B8;
  224. }
  225. dest_format=Get_Valid_Texture_Format(dest_format,false); // No compressed destination format if reading from targa...
  226. }
  227. // ----------------------------------------------------------------------------
  228. //
  229. // Utility function for determining valid WW3D format
  230. //
  231. // ----------------------------------------------------------------------------
  232. WW3DFormat Get_Valid_Texture_Format(WW3DFormat format, bool is_compression_allowed)
  233. {
  234. int w,h,bits;
  235. bool windowed;
  236. if (!DX8Caps::Support_DXTC() ||
  237. !is_compression_allowed ||
  238. WW3D::Get_Texture_Compression_Mode()==WW3D::TEXTURE_COMPRESSION_DISABLE) {
  239. switch (format) {
  240. case WW3D_FORMAT_DXT1: format=WW3D_FORMAT_R8G8B8; break;
  241. case WW3D_FORMAT_DXT2:
  242. case WW3D_FORMAT_DXT3:
  243. case WW3D_FORMAT_DXT4:
  244. case WW3D_FORMAT_DXT5: format=WW3D_FORMAT_A8R8G8B8; break;
  245. default: break;
  246. }
  247. }
  248. else {
  249. switch (format) {
  250. case WW3D_FORMAT_DXT1:
  251. // NVidia hack - switch to DXT2 is there is no DXT1 support (which is disabled on NVidia cards)
  252. if (!DX8Caps::Support_Texture_Format(WW3D_FORMAT_DXT1) && DX8Caps::Support_Texture_Format(WW3D_FORMAT_DXT2)) {
  253. format=WW3D_FORMAT_DXT2;
  254. }
  255. break;
  256. case WW3D_FORMAT_DXT2:
  257. case WW3D_FORMAT_DXT3:
  258. case WW3D_FORMAT_DXT4:
  259. case WW3D_FORMAT_DXT5:
  260. if (!DX8Caps::Support_Texture_Format(format)) format=WW3D_FORMAT_A8R8G8B8;
  261. break;
  262. }
  263. }
  264. if (format==WW3D_FORMAT_R8G8B8) {
  265. format=WW3D_FORMAT_X8R8G8B8;
  266. }
  267. WW3D::Get_Device_Resolution(w,h,bits,windowed);
  268. // if the device bitdepth is 16, don't allow 32 bit textures
  269. if (bits<=16) {
  270. switch (format) {
  271. case WW3D_FORMAT_A8R8G8B8: return WW3D_FORMAT_A4R4G4B4;
  272. case WW3D_FORMAT_X8R8G8B8:
  273. case WW3D_FORMAT_R8G8B8: return WW3D_FORMAT_R5G6B5;
  274. case WW3D_FORMAT_A4R4G4B4:
  275. case WW3D_FORMAT_A1R5G5B5:
  276. case WW3D_FORMAT_R5G6B5:
  277. case WW3D_FORMAT_L8:
  278. case WW3D_FORMAT_A8:
  279. case WW3D_FORMAT_P8:
  280. default:
  281. // Basically, anything goes here (just make sure the most common 32 bit formats are converted to 16 bit
  282. break;
  283. }
  284. }
  285. // Fallback if the hardware doesn't support the texture format
  286. if (!DX8Caps::Support_Texture_Format(format)) {
  287. format=WW3D_FORMAT_A8R8G8B8;
  288. if (!DX8Caps::Support_Texture_Format(format)) {
  289. format=WW3D_FORMAT_A4R4G4B4;
  290. if (!DX8Caps::Support_Texture_Format(format)) {
  291. // If still no luck, try non-alpha formats
  292. format=WW3D_FORMAT_A8R8G8B8;
  293. if (!DX8Caps::Support_Texture_Format(format)) {
  294. format=WW3D_FORMAT_R5G6B5;
  295. if (!DX8Caps::Support_Texture_Format(format)) {
  296. WWASSERT_PRINT(0,("No valid texture format found"));
  297. }
  298. }
  299. }
  300. }
  301. }
  302. return format;
  303. }
  304. unsigned Get_Bytes_Per_Pixel(WW3DFormat format)
  305. {
  306. switch (format) {
  307. case WW3D_FORMAT_X8R8G8B8:
  308. case WW3D_FORMAT_A8R8G8B8: return 4;
  309. case WW3D_FORMAT_R8G8B8: return 3;
  310. case WW3D_FORMAT_A4R4G4B4:
  311. case WW3D_FORMAT_A1R5G5B5:
  312. case WW3D_FORMAT_R5G6B5: return 2;
  313. case WW3D_FORMAT_L8:
  314. case WW3D_FORMAT_A8:
  315. case WW3D_FORMAT_P8: return 1;
  316. default: WWASSERT(0); break;
  317. }
  318. return 0;
  319. }