main.cpp 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111
  1. #include <cassert>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cstring>
  5. #include <direct.h>
  6. #include <io.h>
  7. #include <windows.h>
  8. #include "bsp.h"
  9. #include "pbsp.h"
  10. #include "../qcommon/sparc.h"
  11. #define SWAP16(x) \
  12. big_endian ? \
  13. ((short)( ( ((x) & 0xff00) >> 8 ) + ( ((x) & 0xff) << 8 ) )) \
  14. : \
  15. x;
  16. #define SWAP32(x) \
  17. big_endian ? \
  18. ((int)( ( ((x) & 0xff000000) >> 24) \
  19. + ( ((x) & 0x00ff0000) >> 8 ) \
  20. + ( ((x) & 0x0000ff00) << 8 ) \
  21. + ( ((x) & 0x000000ff) << 24 ) )) \
  22. : \
  23. x;
  24. static float SWAPF(float x, bool big_endian)
  25. {
  26. int temp = SWAP32(*(int*)&x);
  27. return *(float*)&temp;
  28. }
  29. #define CHECKED_READ(BUFFER, SIZE, OFFSET) \
  30. if (fseek(in, OFFSET, SEEK_SET) < 0 || \
  31. !fread(BUFFER, SIZE, 1, in)) \
  32. { \
  33. fprintf(stderr, "Error reading BSP.\n"); \
  34. assert(false); \
  35. exit(-1); \
  36. }
  37. #define CHECKED_WRITE(NAME, BUFFER, SIZE) \
  38. { \
  39. char fullname[256]; \
  40. sprintf(fullname, "%s\\%s.%s", \
  41. path, NAME, big_endian ? "mbe" : "mle"); \
  42. FILE* out = fopen(fullname, "wb"); \
  43. if (!out) \
  44. { \
  45. fprintf(stderr, "Error opening %s.\n", fullname); \
  46. assert(false); \
  47. exit(-1); \
  48. } \
  49. if (!fwrite(BUFFER, SIZE, 1, out)) \
  50. { \
  51. fprintf(stderr, "Error writing %s.\n", fullname); \
  52. assert(false); \
  53. exit(-1); \
  54. } \
  55. fclose(out); \
  56. }
  57. static void convert_fogs(const lump_t& lump, FILE* in, bool big_endian, const char* path)
  58. {
  59. int num = lump.filelen / sizeof(pdfog_t);
  60. if (num == 0)
  61. {
  62. return;
  63. }
  64. pdfog_t* fogs = new pdfog_t[num];
  65. CHECKED_READ(fogs, lump.filelen, lump.fileofs);
  66. for (int i = 0; i < num; ++i)
  67. {
  68. fogs[i].brushNum = SWAP32(fogs[i].brushNum);
  69. fogs[i].visibleSide = SWAP32(fogs[i].visibleSide);
  70. }
  71. CHECKED_WRITE("fogs", fogs, lump.filelen);
  72. delete [] fogs;
  73. }
  74. static void convert_brushes(const lump_t& lump, FILE* in, bool big_endian, const char* path)
  75. {
  76. int num = lump.filelen / sizeof(dbrush_t);
  77. dbrush_t* in_brushes = new dbrush_t[num];
  78. CHECKED_READ(in_brushes, lump.filelen, lump.fileofs);
  79. pdbrush_t* out_brushes = new pdbrush_t[num];
  80. for (int i = 0; i < num; ++i)
  81. {
  82. assert(in_brushes[i].numSides >= 0 && in_brushes[i].numSides < 256);
  83. assert(in_brushes[i].shaderNum >= 0 && in_brushes[i].shaderNum < 65536);
  84. out_brushes[i].firstSide = SWAP32(in_brushes[i].firstSide);
  85. out_brushes[i].numSides = in_brushes[i].numSides;
  86. out_brushes[i].shaderNum = SWAP16((unsigned short)in_brushes[i].shaderNum);
  87. }
  88. int size = num * sizeof(pdbrush_t);
  89. CHECKED_WRITE("brushes", out_brushes, size);
  90. delete [] out_brushes;
  91. delete [] in_brushes;
  92. }
  93. static void convert_brushsides(const lump_t& lump, FILE* in, bool big_endian, const char* path)
  94. {
  95. int num = lump.filelen / sizeof(dbrushside_t);
  96. dbrushside_t* in_brushesides = new dbrushside_t[num];
  97. CHECKED_READ(in_brushesides, lump.filelen, lump.fileofs);
  98. pdbrushside_t* out_brushesides = new pdbrushside_t[num];
  99. for (int i = 0; i < num; ++i)
  100. {
  101. assert(in_brushesides[i].shaderNum >= 0 && in_brushesides[i].shaderNum < 256);
  102. out_brushesides[i].planeNum = SWAP32(in_brushesides[i].planeNum);
  103. out_brushesides[i].shaderNum = in_brushesides[i].shaderNum;
  104. }
  105. int size = num * sizeof(pdbrushside_t);
  106. CHECKED_WRITE("brushsides", out_brushesides, size);
  107. delete [] out_brushesides;
  108. delete [] in_brushesides;
  109. }
  110. static void convert_leafsurfaces(const lump_t& lump, FILE* in, bool big_endian, const char* path)
  111. {
  112. int num = lump.filelen / sizeof(int);
  113. int* leafsurfaces = new int[num];
  114. CHECKED_READ(leafsurfaces, lump.filelen, lump.fileofs);
  115. for (int i = 0; i < num; ++i)
  116. {
  117. leafsurfaces[i] = SWAP32(leafsurfaces[i]);
  118. }
  119. CHECKED_WRITE("leafsurfaces", leafsurfaces, lump.filelen);
  120. delete [] leafsurfaces;
  121. }
  122. static void convert_nodes(const lump_t& lump, FILE* in, bool big_endian, const char* path)
  123. {
  124. int num = lump.filelen / sizeof(dnode_t);
  125. dnode_t* in_nodes = new dnode_t[num];
  126. CHECKED_READ(in_nodes, lump.filelen, lump.fileofs);
  127. pdnode_t* out_nodes = new pdnode_t[num];
  128. for (int i = 0; i < num; ++i)
  129. {
  130. out_nodes[i].planeNum = SWAP32(in_nodes[i].planeNum);
  131. for (int j = 0; j < 2; ++j)
  132. {
  133. assert(in_nodes[i].children[j] > -32768 && in_nodes[i].children[j] < 32768);
  134. out_nodes[i].children[j] = SWAP16((short)in_nodes[i].children[j]);
  135. }
  136. for (int k = 0; k < 3; ++k)
  137. {
  138. assert(in_nodes[i].mins[k] > -32768 && in_nodes[i].mins[k] < 32768);
  139. out_nodes[i].mins[k] = SWAP16((short)in_nodes[i].mins[k]);
  140. assert(in_nodes[i].maxs[k] > -32768 && in_nodes[i].maxs[k] < 32768);
  141. out_nodes[i].maxs[k] = SWAP16((short)in_nodes[i].maxs[k]);
  142. }
  143. }
  144. int size = num * sizeof(pdnode_t);
  145. CHECKED_WRITE("nodes", out_nodes, size);
  146. delete [] out_nodes;
  147. delete [] in_nodes;
  148. }
  149. static void convert_leafs(const lump_t& lump, FILE* in, bool big_endian, const char* path)
  150. {
  151. int num = lump.filelen / sizeof(dleaf_t);
  152. dleaf_t* in_leafs = new dleaf_t[num];
  153. CHECKED_READ(in_leafs, lump.filelen, lump.fileofs);
  154. pdleaf_t* out_leafs = new pdleaf_t[num];
  155. for (int i = 0; i < num; ++i)
  156. {
  157. assert(in_leafs[i].cluster > -32768 && in_leafs[i].cluster < 32768);
  158. out_leafs[i].cluster = SWAP16((short)in_leafs[i].cluster);
  159. assert(in_leafs[i].area > -128 && in_leafs[i].area < 128);
  160. out_leafs[i].area = in_leafs[i].area;
  161. assert(in_leafs[i].firstLeafSurface >= 0 && in_leafs[i].firstLeafSurface < 65536);
  162. out_leafs[i].firstLeafSurface = SWAP16((short)in_leafs[i].firstLeafSurface);
  163. assert(in_leafs[i].numLeafSurfaces >= 0 && in_leafs[i].numLeafSurfaces < 65536);
  164. out_leafs[i].numLeafSurfaces = SWAP16((short)in_leafs[i].numLeafSurfaces);
  165. assert(in_leafs[i].firstLeafBrush >= 0 && in_leafs[i].firstLeafBrush < 65536);
  166. out_leafs[i].firstLeafBrush = SWAP16((short)in_leafs[i].firstLeafBrush);
  167. assert(in_leafs[i].numLeafBrushes >= 0 && in_leafs[i].numLeafBrushes < 65536);
  168. out_leafs[i].numLeafBrushes = SWAP16((short)in_leafs[i].numLeafBrushes);
  169. for (int k = 0; k < 3; ++k)
  170. {
  171. //assert(in_leafs[i].mins[k] > -32768 && in_leafs[i].mins[k] < 32768);
  172. out_leafs[i].mins[k] = SWAP16((short)in_leafs[i].mins[k]);
  173. //assert(in_leafs[i].maxs[k] > -32768 && in_leafs[i].maxs[k] < 32768);
  174. out_leafs[i].maxs[k] = SWAP16((short)in_leafs[i].maxs[k]);
  175. }
  176. }
  177. int size = num * sizeof(pdleaf_t);
  178. CHECKED_WRITE("leafs", out_leafs, size);
  179. delete [] out_leafs;
  180. delete [] in_leafs;
  181. }
  182. static void convert_models(const lump_t& lump, FILE* in, bool big_endian, const char* path)
  183. {
  184. int num = lump.filelen / sizeof(dmodel_t);
  185. if (num == 0)
  186. {
  187. return;
  188. }
  189. dmodel_t* in_models = new dmodel_t[num];
  190. CHECKED_READ(in_models, lump.filelen, lump.fileofs);
  191. pdmodel_t* out_models = new pdmodel_t[num];
  192. for (int i = 0; i < num; ++i)
  193. {
  194. out_models[i].firstSurface = SWAP32(in_models[i].firstSurface);
  195. assert(in_models[i].numSurfaces >= 0 && in_models[i].numSurfaces < 65536);
  196. out_models[i].numSurfaces = SWAP16((short)in_models[i].numSurfaces);
  197. out_models[i].firstBrush = SWAP32(in_models[i].firstBrush);
  198. assert(in_models[i].numBrushes >= 0 && in_models[i].numBrushes < 65536);
  199. out_models[i].numBrushes = SWAP16((short)in_models[i].numBrushes);
  200. for (int k = 0; k < 3; ++k)
  201. {
  202. out_models[i].mins[k] = SWAPF(in_models[i].mins[k], big_endian);
  203. out_models[i].maxs[k] = SWAPF(in_models[i].maxs[k], big_endian);
  204. }
  205. }
  206. int size = num * sizeof(pdmodel_t);
  207. CHECKED_WRITE("models", out_models, size);
  208. delete [] out_models;
  209. delete [] in_models;
  210. }
  211. static void convert_entities(const lump_t& lump, FILE* in, bool big_endian, const char* path)
  212. {
  213. if (lump.filelen == 0)
  214. {
  215. return;
  216. }
  217. char* entities = new char[lump.filelen];
  218. CHECKED_READ(entities, lump.filelen, lump.fileofs);
  219. CHECKED_WRITE("entities", entities, lump.filelen);
  220. delete [] entities;
  221. }
  222. static void convert_lightgrid(const lump_t& lump, FILE* in, bool big_endian, const char* path)
  223. {
  224. int num = lump.filelen / sizeof(dgrid_t);
  225. if (num == 0)
  226. {
  227. return;
  228. }
  229. dgrid_t* in_grid = new dgrid_t[num];
  230. CHECKED_READ(in_grid, lump.filelen, lump.fileofs);
  231. //figure out how much memory we really need.
  232. int memory = 0;
  233. int i, j;
  234. for(i=0; i<num; i++) {
  235. for(j=0; j<MAXLIGHTMAPS; j++) {
  236. if(in_grid[i].styles[j] != LS_NONE) {
  237. memory++;
  238. }
  239. if(
  240. in_grid[i].ambientLight[j][0] != 0 ||
  241. in_grid[i].ambientLight[j][1] != 0 ||
  242. in_grid[i].ambientLight[j][2] != 0 ||
  243. in_grid[i].directLight[j][0] != 0 ||
  244. in_grid[i].directLight[j][1] != 0 ||
  245. in_grid[i].directLight[j][2] != 0) {
  246. memory += 6;
  247. }
  248. }
  249. }
  250. //Make sure we're going to use less memory than the old system.
  251. int size = sizeof(pdgrid_t) * num + memory;
  252. // assert(lump.filelen >= size);
  253. char* out_grid = new char[size];
  254. char* data = out_grid + (sizeof(pdgrid_t) * num);
  255. //Copy data from old array into new array.
  256. memory = 0;
  257. for(i=0; i<num; i++) {
  258. pdgrid_t* g = (pdgrid_t*)out_grid + i;
  259. g->latLong[0] = in_grid[i].latLong[0];
  260. g->latLong[1] = in_grid[i].latLong[1];
  261. g->flags = 0;
  262. g->data = SWAP32(sizeof(pdgrid_t) * num + memory);
  263. for(j=0; j<MAXLIGHTMAPS; j++) {
  264. if(in_grid[i].styles[j] != LS_NONE) {
  265. data[memory++] = in_grid[i].styles[j];
  266. g->flags |= 1 << (j + 4);
  267. }
  268. if(
  269. in_grid[i].ambientLight[j][0] != 0 ||
  270. in_grid[i].ambientLight[j][1] != 0 ||
  271. in_grid[i].ambientLight[j][2] != 0 ||
  272. in_grid[i].directLight[j][0] != 0 ||
  273. in_grid[i].directLight[j][1] != 0 ||
  274. in_grid[i].directLight[j][2] != 0) {
  275. data[memory++] = in_grid[i].ambientLight[j][0];
  276. data[memory++] = in_grid[i].ambientLight[j][1];
  277. data[memory++] = in_grid[i].ambientLight[j][2];
  278. data[memory++] = in_grid[i].directLight[j][0];
  279. data[memory++] = in_grid[i].directLight[j][1];
  280. data[memory++] = in_grid[i].directLight[j][2];
  281. g->flags |= 1 << j;
  282. }
  283. }
  284. }
  285. CHECKED_WRITE("lightgrid", out_grid, size);
  286. delete [] out_grid;
  287. delete [] in_grid;
  288. }
  289. static void convert_lightarray(const lump_t& lump, FILE* in, bool big_endian, const char* path)
  290. {
  291. int num = lump.filelen / sizeof(short);
  292. if (num == 0)
  293. {
  294. return;
  295. }
  296. short* lightarray = new short[num];
  297. CHECKED_READ(lightarray, lump.filelen, lump.fileofs);
  298. for (int i = 0; i < num; ++i)
  299. {
  300. lightarray[i] = SWAP16(lightarray[i]);
  301. }
  302. CHECKED_WRITE("lightarray", lightarray, lump.filelen);
  303. delete [] lightarray;
  304. }
  305. static void convert_shaders(const lump_t& lump, FILE* in, bool big_endian, const char* path)
  306. {
  307. int num = lump.filelen / sizeof(pdshader_t);
  308. if (num == 0)
  309. {
  310. return;
  311. }
  312. pdshader_t* shaders = new pdshader_t[num];
  313. CHECKED_READ(shaders, lump.filelen, lump.fileofs);
  314. for (int i = 0; i < num; ++i)
  315. {
  316. shaders[i].surfaceFlags = SWAP32(shaders[i].surfaceFlags);
  317. shaders[i].contentFlags = SWAP32(shaders[i].contentFlags);
  318. }
  319. CHECKED_WRITE("shaders", shaders, lump.filelen);
  320. delete [] shaders;
  321. }
  322. static void convert_planes(const lump_t& lump, FILE* in, bool big_endian, const char* path)
  323. {
  324. int num = lump.filelen / sizeof(pdplane_t);
  325. pdplane_t* planes = new pdplane_t[num];
  326. CHECKED_READ(planes, lump.filelen, lump.fileofs);
  327. for (int i = 0; i < num; ++i)
  328. {
  329. planes[i].normal[0] = SWAPF(planes[i].normal[0], big_endian);
  330. planes[i].normal[1] = SWAPF(planes[i].normal[1], big_endian);
  331. planes[i].normal[2] = SWAPF(planes[i].normal[2], big_endian);
  332. planes[i].dist = SWAPF(planes[i].dist, big_endian);
  333. }
  334. CHECKED_WRITE("planes", planes, lump.filelen);
  335. delete [] planes;
  336. }
  337. static void convert_leafbrushes(const lump_t& lump, FILE* in, bool big_endian, const char* path)
  338. {
  339. int num = lump.filelen / sizeof(int);
  340. int* leafbrushes = new int[num];
  341. CHECKED_READ(leafbrushes, lump.filelen, lump.fileofs);
  342. for (int i = 0; i < num; ++i)
  343. {
  344. leafbrushes[i] = SWAP32(leafbrushes[i]);
  345. }
  346. CHECKED_WRITE("leafbrushes", leafbrushes, lump.filelen);
  347. delete [] leafbrushes;
  348. }
  349. static void convert_verts(const lump_t& lump, FILE* in, bool big_endian, const char* path)
  350. {
  351. int num = lump.filelen / sizeof(mapVert_t);
  352. mapVert_t* in_verts = new mapVert_t[num];
  353. CHECKED_READ(in_verts, lump.filelen, lump.fileofs);
  354. pmapVert_t* out_verts = new pmapVert_t[num];
  355. for (int i = 0; i < num; ++i)
  356. {
  357. for (int j = 0; j < 3; ++j)
  358. {
  359. assert(in_verts[i].xyz[j] > -32768 && in_verts[i].xyz[j] < 32768);
  360. out_verts[i].xyz[j] = SWAP16((short)in_verts[i].xyz[j]);
  361. assert(in_verts[i].normal[j] >= -1 && in_verts[i].normal[j] <= 1);
  362. out_verts[i].normal[j] = SWAP16((short)(in_verts[i].normal[j] * 32767.f));
  363. }
  364. for (int k = 0; k < 2; ++k)
  365. {
  366. out_verts[i].st[k] = SWAPF(in_verts[i].st[k], big_endian);
  367. }
  368. for (int m = 0; m < MAXLIGHTMAPS; ++m)
  369. {
  370. for (int n = 0; n < 2; ++n)
  371. {
  372. out_verts[i].lightmap[m][n] = SWAPF(
  373. (in_verts[i].lightmap[m][n] * POINTS_LIGHT_SCALE),
  374. big_endian);
  375. }
  376. for (int p = 0; p < 4; ++p)
  377. {
  378. out_verts[i].color[m][p] = in_verts[i].color[m][p];
  379. }
  380. }
  381. }
  382. int size = num * sizeof(pmapVert_t);
  383. CHECKED_WRITE("verts", out_verts, size);
  384. delete [] out_verts;
  385. delete [] in_verts;
  386. }
  387. static void convert_indexes(const lump_t& lump, FILE* in, bool big_endian, const char* path)
  388. {
  389. int num = lump.filelen / sizeof(int);
  390. int* in_indexes = new int[num];
  391. CHECKED_READ(in_indexes, lump.filelen, lump.fileofs);
  392. short* out_indexes = new short[num];
  393. for (int i = 0; i < num; ++i)
  394. {
  395. assert(in_indexes[i] > -32768 && in_indexes[i] < 32768);
  396. out_indexes[i] = SWAP16((short)in_indexes[i]);
  397. }
  398. int size = num * sizeof(short);
  399. CHECKED_WRITE("indexes", out_indexes, size);
  400. delete [] out_indexes;
  401. delete [] in_indexes;
  402. }
  403. static void scale_color(byte* dst, const byte* src, float factor)
  404. {
  405. byte hichan = 0;
  406. int hiindex = 0;
  407. for (int c = 0; c < 3; ++c)
  408. {
  409. if (hichan < src[c])
  410. {
  411. hichan = src[c];
  412. hiindex = c;
  413. }
  414. }
  415. float test = (float)src[hiindex] * factor;
  416. if (test > 255.f) test = 255.f;
  417. factor = test / (float)src[hiindex];
  418. dst[0] = (byte)((float)src[2] * factor);
  419. dst[1] = (byte)((float)src[1] * factor);
  420. dst[2] = (byte)((float)src[0] * factor);
  421. }
  422. static void convert_lightmaps(const lump_t& lump, FILE* in, bool big_endian, const char* path)
  423. {
  424. int in_map_size = LIGHTMAP_SIZE * LIGHTMAP_SIZE * 3;
  425. int bmp_size = in_map_size + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
  426. int num = lump.filelen / in_map_size;
  427. if (num == 0)
  428. {
  429. return;
  430. }
  431. unsigned char* in_lightmaps = new unsigned char[lump.filelen];
  432. CHECKED_READ(in_lightmaps, lump.filelen, lump.fileofs);
  433. // Setup a BMP file for conversion
  434. char* bmp = new char[bmp_size];
  435. BITMAPFILEHEADER* header = (BITMAPFILEHEADER*)bmp;
  436. ((char*)&header->bfType)[0] = 'B';
  437. ((char*)&header->bfType)[1] = 'M';
  438. header->bfSize = bmp_size;
  439. header->bfReserved1 = 0;
  440. header->bfReserved2 = 0;
  441. header->bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
  442. BITMAPINFOHEADER* info = (BITMAPINFOHEADER*)(bmp + sizeof(BITMAPFILEHEADER));
  443. info->biSize = sizeof(BITMAPINFOHEADER);
  444. info->biWidth = LIGHTMAP_SIZE;
  445. info->biHeight = LIGHTMAP_SIZE;
  446. info->biPlanes = 1;
  447. info->biBitCount = 24;
  448. info->biCompression = BI_RGB;
  449. info->biSizeImage = 0;
  450. info->biXPelsPerMeter = 1;
  451. info->biYPelsPerMeter = 1;
  452. info->biClrUsed = 0;
  453. info->biClrImportant = 0;
  454. // Open the lightmaps output file
  455. char fullname[256];
  456. sprintf(fullname, "%s\\lightmaps.%s", path, big_endian ? "mbe" : "mle");
  457. FILE* flightmaps = fopen(fullname, "wb");
  458. if (!flightmaps)
  459. {
  460. fprintf(stderr, "Error opening %s.\n", fullname);
  461. assert(false);
  462. exit(-1);
  463. }
  464. // For each lightmap...
  465. for (int i = 0; i < num; ++i)
  466. {
  467. // Update the BMP
  468. unsigned char* in_map = in_lightmaps + i * in_map_size;
  469. for (int y = 0; y < LIGHTMAP_SIZE; ++y)
  470. {
  471. for (int x = 0; x < LIGHTMAP_SIZE; ++x)
  472. {
  473. byte* dst = (byte*)bmp + header->bfOffBits +
  474. ((LIGHTMAP_SIZE - y - 1) * LIGHTMAP_SIZE * 3) +
  475. x * 3;
  476. byte* src = (byte*)in_map +
  477. y * LIGHTMAP_SIZE * 3 +
  478. x * 3;
  479. scale_color(dst, src, big_endian ? 1.225f : 1.1f);
  480. }
  481. }
  482. FILE* fbmp = fopen("~temp.bmp", "wb");
  483. if (!fbmp)
  484. {
  485. fprintf(stderr, "Error opening ~temp.bmp.\n");
  486. assert(false);
  487. exit(-1);
  488. }
  489. fwrite(bmp, bmp_size, 1, fbmp);
  490. fclose(fbmp);
  491. char* out_map = NULL;
  492. int out_map_size = 0;
  493. /* if (big_endian)
  494. {
  495. // Use the "texconvpro" GC conversion tool to convert
  496. // ~temp.bmp to s3tc ~temp.out
  497. FILE* ftcs = fopen("~temp.tcs", "wt");
  498. if (!ftcs)
  499. {
  500. fprintf(stderr, "Error opening ~temp.tcs.\n");
  501. assert(false);
  502. exit(-1);
  503. }
  504. fprintf(ftcs, "path = 0\n");
  505. fprintf(ftcs, "file 0 = ~temp.bmp\n");
  506. fprintf(ftcs, "image 0 = 0, 0, RGB5A3, 0, 0, 0\n");
  507. fprintf(ftcs, "texture 0 = 0, x\n");
  508. fclose(ftcs);
  509. const char* cmdline = "texconvpro ~temp.tcs ~temp.tpl";
  510. system(cmdline);
  511. rename("~temp.tpl", "~temp.out");
  512. unlink("~temp.tcs");
  513. }
  514. else
  515. */
  516. {
  517. // Use the "nvdxt" conversion tool to convert
  518. // ~temp.bmp to s3tc ~temp.out
  519. const char* cmdline = "nvdxt -file ~temp.bmp -u565 -nomipmap";
  520. system(cmdline);
  521. rename("~temp.dds", "~temp.out");
  522. }
  523. // Read compressed ~temp.out
  524. FILE* fout = fopen("~temp.out", "rb");
  525. if (!fout)
  526. {
  527. fprintf(stderr, "Error opening ~temp.out.\n");
  528. assert(false);
  529. exit(-1);
  530. }
  531. fseek(fout, 0, SEEK_END);
  532. out_map_size = ftell(fout);
  533. fseek(fout, 0, SEEK_SET);
  534. out_map = new char[out_map_size];
  535. fread(out_map, out_map_size, 1, fout);
  536. fclose(fout);
  537. unlink("~temp.out");
  538. // Append compressed data to lightmaps
  539. if (i == 0)
  540. {
  541. int temp = SWAP32(out_map_size);
  542. fwrite(&temp, sizeof(temp), 1, flightmaps);
  543. }
  544. fwrite(out_map, out_map_size, 1, flightmaps);
  545. unlink("~temp.bmp");
  546. }
  547. fclose(flightmaps);
  548. delete [] bmp;
  549. delete [] in_lightmaps;
  550. }
  551. static void convert_visibility(const lump_t& lump, FILE* in, bool big_endian, const char* path)
  552. {
  553. if(lump.filelen == 0)
  554. return;
  555. unsigned char* in_visibility = new unsigned char[lump.filelen];
  556. CHECKED_READ(in_visibility, lump.filelen, lump.fileofs);
  557. char* out_visibility = new char[lump.filelen*2];
  558. *((int*)out_visibility + 0) = SWAP32(*((int*)in_visibility + 0));
  559. *((int*)out_visibility + 1) = SWAP32(*((int*)in_visibility + 1));
  560. SPARC<unsigned char> vis;
  561. vis.Compress(in_visibility + 8, lump.filelen - 8, 0);
  562. int size = vis.Save(out_visibility + 8, lump.filelen*2 - 8, big_endian) + 8;
  563. CHECKED_WRITE("visibility", out_visibility, size);
  564. delete [] out_visibility;
  565. delete [] in_visibility;
  566. }
  567. static void convert_faces(const lump_t& lump, FILE* in, bool big_endian, const char* path)
  568. {
  569. bool warning = false;
  570. int num = lump.filelen / sizeof(dsurface_t);
  571. if (num == 0)
  572. {
  573. return;
  574. }
  575. dsurface_t* in_surfaces = new dsurface_t[num];
  576. CHECKED_READ(in_surfaces, lump.filelen, lump.fileofs);
  577. pdface_t* out_surfaces = new pdface_t[num];
  578. int counter = 0;
  579. for (int i = 0; i < num; ++i)
  580. {
  581. if (in_surfaces[i].surfaceType == MST_PLANAR)
  582. {
  583. out_surfaces[counter].code = SWAP32(i);
  584. assert(in_surfaces[i].shaderNum >= 0 && in_surfaces[i].shaderNum < 256);
  585. out_surfaces[counter].shaderNum = in_surfaces[i].shaderNum;
  586. assert(in_surfaces[i].fogNum > -128 && in_surfaces[i].fogNum < 128);
  587. out_surfaces[counter].fogNum = in_surfaces[i].fogNum;
  588. assert(in_surfaces[i].firstVert >= 0 && in_surfaces[i].firstVert < 1048576);
  589. assert(in_surfaces[i].numVerts >= 0 && in_surfaces[i].numVerts < 4096);
  590. out_surfaces[counter].verts = SWAP32((in_surfaces[i].firstVert << 12) |
  591. (in_surfaces[i].numVerts & 0xfff));
  592. assert(in_surfaces[i].firstIndex >= 0 && in_surfaces[i].firstIndex < 1048576);
  593. assert(in_surfaces[i].numIndexes >= 0 && in_surfaces[i].numIndexes < 4096);
  594. out_surfaces[counter].indexes = SWAP32((in_surfaces[i].firstIndex << 12) |
  595. (in_surfaces[i].numIndexes & 0xfff));
  596. for (int j = 0; j < MAXLIGHTMAPS; ++j)
  597. {
  598. if (!warning &&
  599. (in_surfaces[i].lightmapNum[j] < -4 ||
  600. in_surfaces[i].lightmapNum[j] >= 252))
  601. {
  602. printf("WARNING: Lightmap index out of range!\n");
  603. warning = true;
  604. }
  605. out_surfaces[counter].lightmapNum[j] = in_surfaces[i].lightmapNum[j] + 4;
  606. if (in_surfaces[i].lightmapNum[0] == LIGHTMAP_BY_VERTEX)
  607. {
  608. out_surfaces[counter].lightmapStyles[j] = in_surfaces[i].vertexStyles[j];
  609. }
  610. else
  611. {
  612. out_surfaces[counter].lightmapStyles[j] = in_surfaces[i].lightmapStyles[j];
  613. }
  614. }
  615. for (int m = 0; m < 3; ++m)
  616. {
  617. assert(in_surfaces[i].lightmapVecs[2][m] >= -1 &&
  618. in_surfaces[i].lightmapVecs[2][m] <= 1);
  619. out_surfaces[counter].lightmapVecs[m] =
  620. SWAP16((short)(in_surfaces[i].lightmapVecs[2][m] * 32767.f));
  621. }
  622. ++counter;
  623. }
  624. }
  625. if (counter == 0) return;
  626. int size = counter * sizeof(pdface_t);
  627. CHECKED_WRITE("faces", out_surfaces, size);
  628. delete [] out_surfaces;
  629. delete [] in_surfaces;
  630. }
  631. static void convert_patches(const lump_t& lump, FILE* in, bool big_endian, const char* path)
  632. {
  633. int num = lump.filelen / sizeof(dsurface_t);
  634. if (num == 0)
  635. {
  636. return;
  637. }
  638. dsurface_t* in_surfaces = new dsurface_t[num];
  639. CHECKED_READ(in_surfaces, lump.filelen, lump.fileofs);
  640. pdpatch_t* out_surfaces = new pdpatch_t[num];
  641. int counter = 0;
  642. for (int i = 0; i < num; ++i)
  643. {
  644. if (in_surfaces[i].surfaceType == MST_PATCH)
  645. {
  646. out_surfaces[counter].code = SWAP32(i);
  647. assert(in_surfaces[i].shaderNum >= 0 && in_surfaces[i].shaderNum < 256);
  648. out_surfaces[counter].shaderNum = in_surfaces[i].shaderNum;
  649. assert(in_surfaces[i].fogNum > -128 && in_surfaces[i].fogNum < 128);
  650. out_surfaces[counter].fogNum = in_surfaces[i].fogNum;
  651. assert(in_surfaces[i].firstVert >= 0 && in_surfaces[i].firstVert < 1048576);
  652. assert(in_surfaces[i].numVerts >= 0 && in_surfaces[i].numVerts < 4096);
  653. out_surfaces[counter].verts = SWAP32((in_surfaces[i].firstVert << 12) |
  654. (in_surfaces[i].numVerts & 0xfff));
  655. assert(in_surfaces[i].patchWidth >= 0 && in_surfaces[i].patchWidth < 256);
  656. out_surfaces[counter].patchWidth = in_surfaces[i].patchWidth;
  657. assert(in_surfaces[i].patchHeight >= 0 && in_surfaces[i].patchHeight < 256);
  658. out_surfaces[counter].patchHeight = in_surfaces[i].patchHeight;
  659. for (int j = 0; j < MAXLIGHTMAPS; ++j)
  660. {
  661. assert(in_surfaces[i].lightmapNum[j] >= -4 && in_surfaces[i].lightmapNum[j] < 252);
  662. out_surfaces[counter].lightmapNum[j] = in_surfaces[i].lightmapNum[j] + 4;
  663. if (in_surfaces[i].lightmapNum[0] == LIGHTMAP_BY_VERTEX)
  664. {
  665. out_surfaces[counter].lightmapStyles[j] = in_surfaces[i].vertexStyles[j];
  666. }
  667. else
  668. {
  669. out_surfaces[counter].lightmapStyles[j] = in_surfaces[i].lightmapStyles[j];
  670. }
  671. }
  672. for (int m = 0; m < 3; ++m)
  673. {
  674. for (int k = 0; k < 2; ++k)
  675. {
  676. assert(in_surfaces[i].lightmapVecs[k][m] > -32768.f &&
  677. in_surfaces[i].lightmapVecs[k][m] < 32768.f);
  678. out_surfaces[counter].lightmapVecs[k][m] =
  679. SWAP16((short)in_surfaces[i].lightmapVecs[k][m]);
  680. }
  681. }
  682. ++counter;
  683. }
  684. }
  685. if (counter == 0) return;
  686. int size = counter * sizeof(pdpatch_t);
  687. CHECKED_WRITE("patches", out_surfaces, size);
  688. delete [] out_surfaces;
  689. delete [] in_surfaces;
  690. }
  691. static void convert_trisurfs(const lump_t& lump, FILE* in, bool big_endian, const char* path)
  692. {
  693. int num = lump.filelen / sizeof(dsurface_t);
  694. if (num == 0)
  695. {
  696. return;
  697. }
  698. dsurface_t* in_surfaces = new dsurface_t[num];
  699. CHECKED_READ(in_surfaces, lump.filelen, lump.fileofs);
  700. pdtrisurf_t* out_surfaces = new pdtrisurf_t[num];
  701. int counter = 0;
  702. for (int i = 0; i < num; ++i)
  703. {
  704. if (in_surfaces[i].surfaceType == MST_TRIANGLE_SOUP)
  705. {
  706. out_surfaces[counter].code = SWAP32(i);
  707. assert(in_surfaces[i].shaderNum >= 0 && in_surfaces[i].shaderNum < 256);
  708. out_surfaces[counter].shaderNum = in_surfaces[i].shaderNum;
  709. assert(in_surfaces[i].fogNum > -128 && in_surfaces[i].fogNum < 128);
  710. out_surfaces[counter].fogNum = in_surfaces[i].fogNum;
  711. assert(in_surfaces[i].firstVert >= 0 && in_surfaces[i].firstVert < 1048576);
  712. assert(in_surfaces[i].numVerts >= 0 && in_surfaces[i].numVerts < 4096);
  713. out_surfaces[counter].verts = SWAP32((in_surfaces[i].firstVert << 12) |
  714. (in_surfaces[i].numVerts & 0xfff));
  715. assert(in_surfaces[i].firstIndex >= 0 && in_surfaces[i].firstIndex < 1048576);
  716. assert(in_surfaces[i].numIndexes >= 0 && in_surfaces[i].numIndexes < 4096);
  717. out_surfaces[counter].indexes = SWAP32((in_surfaces[i].firstIndex << 12) |
  718. (in_surfaces[i].numIndexes & 0xfff));
  719. for (int j = 0; j < MAXLIGHTMAPS; ++j)
  720. {
  721. if (in_surfaces[i].lightmapNum[0] == LIGHTMAP_BY_VERTEX)
  722. {
  723. out_surfaces[counter].lightmapStyles[j] = in_surfaces[i].vertexStyles[j];
  724. }
  725. else
  726. {
  727. out_surfaces[counter].lightmapStyles[j] = in_surfaces[i].lightmapStyles[j];
  728. }
  729. }
  730. ++counter;
  731. }
  732. }
  733. if (counter == 0) return;
  734. int size = counter * sizeof(pdtrisurf_t);
  735. CHECKED_WRITE("trisurfs", out_surfaces, size);
  736. delete [] out_surfaces;
  737. delete [] in_surfaces;
  738. }
  739. static void convert_flares(const lump_t& lump, FILE* in, bool big_endian, const char* path)
  740. {
  741. int num = lump.filelen / sizeof(dsurface_t);
  742. if (num == 0)
  743. {
  744. return;
  745. }
  746. dsurface_t* in_surfaces = new dsurface_t[num];
  747. CHECKED_READ(in_surfaces, lump.filelen, lump.fileofs);
  748. pdflare_t* out_surfaces = new pdflare_t[num];
  749. int counter = 0;
  750. for (int i = 0; i < num; ++i)
  751. {
  752. if (in_surfaces[i].surfaceType == MST_FLARE)
  753. {
  754. out_surfaces[counter].code = SWAP32(i);
  755. assert(in_surfaces[i].shaderNum >= 0 && in_surfaces[i].shaderNum < 256);
  756. out_surfaces[counter].shaderNum = in_surfaces[i].shaderNum;
  757. assert(in_surfaces[i].fogNum > -128 && in_surfaces[i].fogNum < 128);
  758. out_surfaces[counter].fogNum = in_surfaces[i].fogNum;
  759. for (int j = 0; j < 3; ++j)
  760. {
  761. assert(in_surfaces[i].lightmapOrigin[j] > -32768 && in_surfaces[i].lightmapOrigin[j] < 32768);
  762. out_surfaces[counter].origin[j] = SWAP16((short)in_surfaces[i].lightmapOrigin[j]);
  763. assert(in_surfaces[i].lightmapVecs[2][j] >= -1 && in_surfaces[i].lightmapVecs[2][j] <= 1);
  764. out_surfaces[counter].normal[j] = SWAP16((short)(in_surfaces[i].lightmapVecs[2][j] * 32767.f));
  765. out_surfaces[counter].color[j] = (byte)(in_surfaces[i].lightmapVecs[0][j]);
  766. }
  767. ++counter;
  768. }
  769. }
  770. if (counter == 0) return;
  771. int size = counter * sizeof(pdflare_t);
  772. CHECKED_WRITE("flares", out_surfaces, size);
  773. delete [] out_surfaces;
  774. delete [] in_surfaces;
  775. }
  776. static void convert_surfaces(const lump_t& lump, FILE* in, bool big_endian, const char* path)
  777. {
  778. convert_faces(lump, in, big_endian, path);
  779. convert_patches(lump, in, big_endian, path);
  780. convert_trisurfs(lump, in, big_endian, path);
  781. convert_flares(lump, in, big_endian, path);
  782. }
  783. typedef void (*convertfunc_t)(const lump_t& lump, FILE* in, bool big_endian, const char* path);
  784. static convertfunc_t Converters[HEADER_LUMPS] =
  785. {
  786. convert_entities,
  787. convert_shaders,
  788. convert_planes,
  789. convert_nodes,
  790. convert_leafs,
  791. convert_leafsurfaces,
  792. convert_leafbrushes,
  793. convert_models,
  794. convert_brushes,
  795. convert_brushsides,
  796. convert_verts,
  797. convert_indexes,
  798. convert_fogs,
  799. convert_surfaces,
  800. convert_lightmaps,
  801. convert_lightgrid,
  802. convert_visibility,
  803. convert_lightarray,
  804. };
  805. static void write_misc(const dheader_t& header, FILE* in, bool big_endian, const char* path)
  806. {
  807. int num = header.lumps[LUMP_SURFACES].filelen / sizeof(dsurface_t);
  808. num = SWAP32(num);
  809. CHECKED_WRITE("misc", &num, sizeof(int));
  810. }
  811. /*static void optimize_lightmaps(const dheader_t* header, FILE* in)
  812. {
  813. // get the vert data
  814. int num_verts = header->lumps[LUMP_DRAWVERTS].filelen / sizeof(mapVert_t);
  815. mapVert_t* verts = new mapVert_t[num_verts];
  816. CHECKED_READ(verts,
  817. header->lumps[LUMP_DRAWVERTS].filelen,
  818. header->lumps[LUMP_DRAWVERTS].fileofs);
  819. // get the surface data
  820. int num_surfs = header->lumps[LUMP_SURFACES].filelen / sizeof(dsurface_t);
  821. dsurface_t* surfs = new dsurface_t[num_surfs];
  822. CHECKED_READ(surfs,
  823. header->lumps[LUMP_SURFACES].filelen,
  824. header->lumps[LUMP_SURFACES].fileofs);
  825. // get the lightmap data
  826. const int map_size = LIGHTMAP_SIZE * LIGHTMAP_SIZE * 3;
  827. int num_lightmaps = header->lumps[LUMP_LIGHTMAPS].filelen / map_size;
  828. byte* lightmaps = new byte[header->lumps[LUMP_LIGHTMAPS].filelen];
  829. CHECKED_READ(lightmaps,
  830. header->lumps[LUMP_LIGHTMAPS].filelen,
  831. header->lumps[LUMP_LIGHTMAPS].fileofs);
  832. LMOptimizer optimizer;
  833. optimizer.optimize(verts, num_verts, surfs, num_surfs, lightmaps, num_lightmaps);
  834. }*/
  835. static void process(const char* name, bool big_endian)
  836. {
  837. // open the bsp
  838. FILE* in = fopen(name, "rb");
  839. if (!in)
  840. {
  841. fprintf(stderr, "Unable to open %s\n", name);
  842. exit(-1);
  843. }
  844. // get the new path name
  845. char* path = new char[strlen(name) + 1];
  846. strcpy(path, name);
  847. path[strlen(path) - 4] = '\0';
  848. mkdir(path);
  849. dheader_t in_header;
  850. fread(&in_header, sizeof(in_header), 1, in);
  851. for (int i = 0; i < HEADER_LUMPS; ++i)
  852. {
  853. Converters[i](in_header.lumps[i], in, big_endian, path);
  854. }
  855. write_misc(in_header, in, big_endian, path);
  856. delete [] path;
  857. fclose(in);
  858. }
  859. int main(int argc, const char** argv)
  860. {
  861. // check command line
  862. if (argc != 2)
  863. {
  864. fprintf(stderr, "USAGE: %s PATH\n", argv[0]);
  865. return -1;
  866. }
  867. // find all the BSP files in the path
  868. char spec[256];
  869. strcpy(spec, argv[1]);
  870. strcat(spec, "\\*.bsp");
  871. _finddata_t data;
  872. int h = _findfirst(spec, &data);
  873. while (h != -1)
  874. {
  875. printf("Processing %s...\n", data.name);
  876. char name[256];
  877. sprintf(name, "%s\\%s", argv[1], data.name);
  878. process(name, false);
  879. if (_findnext(h, &data)) break;
  880. }
  881. _findclose(h);
  882. return 0;
  883. }