qbsp: add -objexport flag to export to obj
This commit is contained in:
parent
c61ab264c7
commit
a4c24ac0fa
|
|
@ -32,6 +32,7 @@ set(QBSP_SOURCES
|
|||
wad.c
|
||||
winding.c
|
||||
writebsp.c
|
||||
exportobj.c
|
||||
${QBSP_INCLUDES})
|
||||
|
||||
add_definitions(-DDOUBLEVEC_T)
|
||||
|
|
|
|||
|
|
@ -0,0 +1,97 @@
|
|||
/*
|
||||
Copyright (C) 2016 Eric Wasylishen
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to the Free Software
|
||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
See file, 'COPYING', for details.
|
||||
*/
|
||||
|
||||
#include "qbsp.h"
|
||||
#include "wad.h"
|
||||
|
||||
static FILE *
|
||||
InitObjFile(void)
|
||||
{
|
||||
FILE *objfile;
|
||||
|
||||
StripExtension(options.szBSPName);
|
||||
strcat(options.szBSPName, ".obj");
|
||||
objfile = fopen(options.szBSPName, "wt");
|
||||
if (!objfile)
|
||||
Error("Failed to open %s: %s", options.szBSPName, strerror(errno));
|
||||
|
||||
return objfile;
|
||||
}
|
||||
|
||||
static void
|
||||
GetUV(const texinfo_t *texinfo, const vec_t *pos, const int width, const int height, vec_t *u, vec_t *v)
|
||||
{
|
||||
*u = (pos[0]*texinfo->vecs[0][0] + pos[1]*texinfo->vecs[0][1] + pos[2]*texinfo->vecs[0][2] + texinfo->vecs[0][3]) / width;
|
||||
*v = (pos[0]*texinfo->vecs[1][0] + pos[1]*texinfo->vecs[1][1] + pos[2]*texinfo->vecs[1][2] + texinfo->vecs[1][3]) / height;
|
||||
}
|
||||
|
||||
static void
|
||||
ExportObjFace(FILE *f, const face_t *face, int *vertcount)
|
||||
{
|
||||
const texinfo_t *texinfos = pWorldEnt->lumps[LUMP_TEXINFO].data;
|
||||
const texinfo_t *texinfo = &texinfos[face->texinfo];
|
||||
const char *texname = map.miptex[texinfo->miptex];
|
||||
|
||||
const texture_t *texture = WADList_GetTexture(texname);
|
||||
const int width = texture ? texture->width : 64;
|
||||
const int height = texture ? texture->height : 64;
|
||||
|
||||
// export the vertices and uvs
|
||||
for (int i=0; i<face->w.numpoints; i++)
|
||||
{
|
||||
const vec_t *pos = face->w.points[i];
|
||||
fprintf(f, "v %.9g %.9g %.9g\n", pos[0], pos[1], pos[2]);
|
||||
|
||||
vec_t u, v;
|
||||
GetUV(texinfo, pos, width, height, &u, &v);
|
||||
|
||||
// not sure why -v is needed, .obj uses (0, 0) in the top left apparently?
|
||||
fprintf(f, "vt %.9g %.9g\n", u, -v);
|
||||
}
|
||||
|
||||
fprintf(f, "usemtl %s\n", texname);
|
||||
fprintf(f, "f");
|
||||
for (int i=0; i<face->w.numpoints; i++) {
|
||||
// .obj vertexes start from 1
|
||||
// .obj faces are CCW, quake is CW, so reverse the order
|
||||
const int vertindex = *vertcount + (face->w.numpoints - 1 - i) + 1;
|
||||
fprintf(f, " %d/%d", vertindex, vertindex);
|
||||
}
|
||||
fprintf(f, "\n");
|
||||
|
||||
*vertcount += face->w.numpoints;
|
||||
}
|
||||
|
||||
void
|
||||
ExportObj(const surface_t *surfaces)
|
||||
{
|
||||
FILE *objfile = InitObjFile();
|
||||
int vertcount = 0;
|
||||
|
||||
const surface_t *surf;
|
||||
for (surf = surfaces; surf; surf = surf->next) {
|
||||
const face_t *face;
|
||||
for (face = surf->faces; face; face = face->next) {
|
||||
ExportObjFace(objfile, face, &vertcount);
|
||||
}
|
||||
}
|
||||
|
||||
fclose(objfile);
|
||||
}
|
||||
|
|
@ -133,6 +133,10 @@ ProcessEntity(mapentity_t *entity, const int hullnum)
|
|||
surfs = CSGFaces(entity);
|
||||
FreeBrushes(entity->brushes);
|
||||
|
||||
if (options.fObjExport && entity == pWorldEnt && hullnum == 0) {
|
||||
ExportObj(surfs);
|
||||
}
|
||||
|
||||
if (hullnum != 0) {
|
||||
nodes = SolidBSP(entity, surfs, true);
|
||||
if (entity == pWorldEnt && !options.fNofill) {
|
||||
|
|
@ -608,6 +612,7 @@ PrintOptions(void)
|
|||
" -oldrottex Use old rotate_ brush texturing aligned at (0 0 0)\n"
|
||||
" -maxnodesize [n]Triggers simpler BSP Splitting when node exceeds size (default 1024, 0 to disable)\n"
|
||||
" -epsilon [n] Customize ON_EPSILON (default 0.0001)\n"
|
||||
" -objexport Export the map file as an .OBJ model after the CSG phase\n"
|
||||
" sourcefile .MAP file to process\n"
|
||||
" destfile .BSP file to output\n");
|
||||
|
||||
|
|
@ -779,6 +784,8 @@ ParseOptions(char *szOptions)
|
|||
Error("Invalid argument to option %s", szTok);
|
||||
options.on_epsilon= atof(szTok2);
|
||||
szTok = szTok2;
|
||||
} else if (!Q_strcasecmp(szTok, "objexport")) {
|
||||
options.fObjExport = true;
|
||||
} else if (!Q_strcasecmp(szTok, "?") || !Q_strcasecmp(szTok, "help"))
|
||||
PrintOptions();
|
||||
else
|
||||
|
|
|
|||
|
|
@ -485,6 +485,7 @@ typedef struct options_s {
|
|||
char szBSPName[512];
|
||||
char wadPath[512];
|
||||
vec_t on_epsilon;
|
||||
bool fObjExport;
|
||||
} options_t;
|
||||
|
||||
extern options_t options;
|
||||
|
|
@ -598,6 +599,8 @@ void BSPX_Brushes_Finalize(struct bspxbrushes_s *ctx);
|
|||
void BSPX_Brushes_Init(struct bspxbrushes_s *ctx);
|
||||
void BSPX_Brushes_AddModel(struct bspxbrushes_s *ctx, int modelnum, brush_t *brushes);
|
||||
|
||||
void ExportObj(const surface_t *surfaces);
|
||||
|
||||
// util.c
|
||||
|
||||
#define msgWarning 1
|
||||
|
|
|
|||
32
qbsp/wad.c
32
qbsp/wad.c
|
|
@ -28,12 +28,15 @@ static void WADList_LoadTextures(const wad_t *wadlist, dmiptexlump_t *lump);
|
|||
static int WAD_LoadLump(const wad_t *wad, const char *name, byte *dest);
|
||||
static void WADList_AddAnimationFrames(const wad_t *wadlist);
|
||||
|
||||
static texture_t *textures;
|
||||
|
||||
static bool
|
||||
WAD_LoadInfo(wad_t *wad)
|
||||
{
|
||||
wadinfo_t *hdr = &wad->header;
|
||||
int i, len, lumpinfosize, disksize;
|
||||
dmiptex_t miptex;
|
||||
texture_t *tex;
|
||||
|
||||
len = fread(hdr, 1, sizeof(wadinfo_t), wad->file);
|
||||
if (len != sizeof(wadinfo_t))
|
||||
|
|
@ -54,6 +57,24 @@ WAD_LoadInfo(wad_t *wad)
|
|||
if (len != lumpinfosize)
|
||||
return false;
|
||||
|
||||
/* Get the dimensions and make a texture_t */
|
||||
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))
|
||||
{
|
||||
tex = AllocMem(OTHER, sizeof(texture_t), true);
|
||||
tex->next = textures;
|
||||
textures = tex;
|
||||
memcpy(tex->name, miptex.name, 16);
|
||||
tex->name[15] = '\0';
|
||||
tex->width = miptex.width;
|
||||
tex->height = miptex.height;
|
||||
|
||||
//printf("Created texture_t %s %d %d\n", tex->name, tex->width, tex->height);
|
||||
}
|
||||
}
|
||||
|
||||
if (wad->version == 2)
|
||||
return true;
|
||||
|
||||
|
|
@ -273,3 +294,14 @@ WADList_AddAnimationFrames(const wad_t *wadlist)
|
|||
|
||||
Message(msgStat, "%8d texture frames added", map.nummiptex - oldcount);
|
||||
}
|
||||
|
||||
const texture_t *WADList_GetTexture(const char *name)
|
||||
{
|
||||
texture_t *tex;
|
||||
for (tex = textures; tex; tex = tex->next)
|
||||
{
|
||||
if (!strcmp(name, tex->name))
|
||||
return tex;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -40,6 +40,12 @@ typedef struct {
|
|||
char name[16]; // must be null terminated
|
||||
} lumpinfo_t;
|
||||
|
||||
typedef struct texture_s {
|
||||
char name[16];
|
||||
int width, height;
|
||||
struct texture_s *next;
|
||||
} texture_t;
|
||||
|
||||
#define MIPLEVELS 4
|
||||
typedef struct {
|
||||
char name[16];
|
||||
|
|
@ -58,5 +64,7 @@ typedef struct wad_s {
|
|||
wad_t *WADList_Init(const char *wadstring);
|
||||
void WADList_Process(const wad_t *wadlist);
|
||||
void WADList_Free(wad_t *wadlist);
|
||||
const texture_t *WADList_GetTexture(const char *name);
|
||||
// for getting a texture width/height
|
||||
|
||||
#endif /* WAD_H */
|
||||
|
|
|
|||
Loading…
Reference in New Issue