From 367626144128ba9229efc8c7af363f8f7124797b Mon Sep 17 00:00:00 2001 From: Kevin Shanahan Date: Wed, 13 Mar 2013 10:58:31 +1030 Subject: [PATCH] qbsp: add support for WAD3 texture wad format Signed-off-by: Kevin Shanahan --- changelog.txt | 1 + qbsp/wad.c | 55 +++++++++++++++++++++++++++++++++++++-------------- qbsp/wad.h | 8 ++++++++ 3 files changed, 49 insertions(+), 15 deletions(-) diff --git a/changelog.txt b/changelog.txt index 61c7f017..23103b14 100644 --- a/changelog.txt +++ b/changelog.txt @@ -1,5 +1,6 @@ Unreleased +* qbsp: added support for using WAD3 texture wads used by Hammer * light: implemented self shadowing and full shadows for brush models * light: removed the "-nominlimit" option (now the default behaviour) * light: remove support for negative color components (never worked properly) diff --git a/qbsp/wad.c b/qbsp/wad.c index 0acd588a..72ef3ef9 100644 --- a/qbsp/wad.c +++ b/qbsp/wad.c @@ -30,24 +30,49 @@ static int WAD_LoadLump(wad_t *w, char *name, byte *dest); static bool -WAD_LoadInfo(wad_t *w) +WAD_LoadInfo(wad_t *wad) { - wadinfo_t *hdr = &w->header; - bool ret = false; - int len; + wadinfo_t *hdr = &wad->header; + int i, len, lumpinfosize, disksize; + dmiptex_t miptex; - len = fread(hdr, 1, sizeof(wadinfo_t), w->file); - if (len == sizeof(wadinfo_t)) - if (!strncmp(hdr->identification, "WAD2", 4)) { - const int lumpinfo = sizeof(lumpinfo_t) * hdr->numlumps; - fseek(w->file, hdr->infotableofs, SEEK_SET); - w->lumps = AllocMem(OTHER, lumpinfo, true); - len = fread(w->lumps, 1, lumpinfo, w->file); - if (len == lumpinfo) - ret = true; - } + len = fread(hdr, 1, sizeof(wadinfo_t), wad->file); + if (len != sizeof(wadinfo_t)) + return false; - return ret; + wad->version = 0; + if (!strncmp(hdr->identification, "WAD2", 4)) + wad->version = 2; + else if (!strncmp(hdr->identification, "WAD3", 4)) + wad->version = 3; + if (!wad->version) + return false; + + lumpinfosize = sizeof(lumpinfo_t) * hdr->numlumps; + fseek(wad->file, hdr->infotableofs, SEEK_SET); + wad->lumps = AllocMem(OTHER, lumpinfosize, true); + len = fread(wad->lumps, 1, lumpinfosize, wad->file); + if (len != lumpinfosize) + return false; + + if (wad->version == 2) + return true; + + /* + * WAD3 format includes a palette after the mipmap data. + * Reduce the disksize in the lumpinfo so we can treat it like WAD2. + */ + for (i = 0; i < wad->header.numlumps; i++) { + fseek(wad->file, wad->lumps[i].filepos, SEEK_SET); + len = fread(&miptex, 1, sizeof(miptex), wad->file); + if (len != sizeof(miptex)) + return false; + disksize = miptex.width * miptex.height / 64 * 85; + if (disksize < wad->lumps[i].disksize) + wad->lumps[i].disksize = disksize; + } + + return true; } diff --git a/qbsp/wad.h b/qbsp/wad.h index 4d4ac428..a98ece2f 100644 --- a/qbsp/wad.h +++ b/qbsp/wad.h @@ -40,8 +40,16 @@ typedef struct { char name[16]; // must be null terminated } lumpinfo_t; +#define MIPLEVELS 4 +typedef struct { + char name[16]; + uint32_t width, height; + uint32_t offsets[MIPLEVELS]; +} dmiptex_t; + typedef struct { wadinfo_t header; + int version; lumpinfo_t *lumps; FILE *file; } wad_t;