backport the mesh improvements from 0.5

This commit is contained in:
Bill Currie 2001-08-15 16:25:23 +00:00
parent f4dc013a3d
commit 363aca5b84
2 changed files with 218 additions and 125 deletions

View file

@ -39,38 +39,96 @@
#include <stdio.h> #include <stdio.h>
#include "console.h" #include "console.h"
#include "cvar.h"
#include "mdfour.h" #include "mdfour.h"
#include "model.h" #include "model.h"
#include "quakefs.h" #include "quakefs.h"
#include "sys.h"
#include "compat.h"
/* /*
ALIAS MODEL DISPLAY LIST GENERATION ALIAS MODEL DISPLAY LIST GENERATION
*/ */
extern cvar_t *gl_mesh_cache;
model_t *aliasmodel; model_t *aliasmodel;
aliashdr_t *paliashdr; aliashdr_t *paliashdr;
qboolean used[8192]; qboolean *used;
int used_size;
// the command list holds counts and s/t values that are valid for // the command list holds counts and s/t values that are valid for
// every frame // every frame
int commands[8192]; int *commands;
int numcommands; int numcommands;
int commands_size;
// all frames will have their vertexes rearranged and expanded // all frames will have their vertexes rearranged and expanded
// so they are in the order expected by the command list // so they are in the order expected by the command list
int vertexorder[8192]; int *vertexorder;
int numorder; int numorder;
int vertexorder_size;
int allverts, alltris; int allverts, alltris;
int stripverts[128]; int *stripverts;
int striptris[128]; int *striptris;
int stripcount; int stripcount;
int strip_size;
void
alloc_used (int size)
{
if (size <= used_size)
return;
size = (size + 1023) & ~1023;
used = realloc (used, size * sizeof (used[0]));
if (!used)
Sys_Error ("gl_mesh: out of memory");
used_size = size;
}
void
add_command (int cmd)
{
if (numcommands + 1 > commands_size) {
commands_size += 1024;
commands = realloc (commands, commands_size * sizeof (commands[0]));
if (!commands)
Sys_Error ("gl_mesh: out of memory");
}
commands[numcommands++] = cmd;
}
void
add_vertex (int vert)
{
if (numorder + 1 > vertexorder_size) {
vertexorder_size += 1024;
vertexorder = realloc (vertexorder, vertexorder_size * sizeof (vertexorder[0]));
if (!vertexorder)
Sys_Error ("gl_mesh: out of memory");
}
vertexorder[numorder++] = vert;
}
void
add_strip (int vert, int tri)
{
if (stripcount + 1 > strip_size) {
strip_size += 1024;
stripverts = realloc (stripverts, strip_size * sizeof (stripverts[0]));
striptris = realloc (striptris, strip_size * sizeof (striptris[0]));
if (!stripverts || !striptris)
Sys_Error ("gl_mesh: out of memory");
}
stripverts[stripcount] = vert;
striptris[stripcount] = tri;
stripcount++;
}
/*
StripLength
*/
int int
StripLength (int starttri, int startv) StripLength (int starttri, int startv)
{ {
@ -83,12 +141,10 @@ StripLength (int starttri, int startv)
last = &triangles[starttri]; last = &triangles[starttri];
stripverts[0] = last->vertindex[(startv) % 3]; stripcount = 0;
stripverts[1] = last->vertindex[(startv + 1) % 3]; add_strip (last->vertindex[(startv) % 3], starttri);
stripverts[2] = last->vertindex[(startv + 2) % 3]; add_strip (last->vertindex[(startv + 1) % 3], starttri);
add_strip (last->vertindex[(startv + 2) % 3], starttri);
striptris[0] = starttri;
stripcount = 1;
m1 = last->vertindex[(startv + 2) % 3]; m1 = last->vertindex[(startv + 2) % 3];
m2 = last->vertindex[(startv + 1) % 3]; m2 = last->vertindex[(startv + 1) % 3];
@ -117,9 +173,7 @@ nexttri:
else else
m1 = check->vertindex[(k + 2) % 3]; m1 = check->vertindex[(k + 2) % 3];
stripverts[stripcount + 2] = check->vertindex[(k + 2) % 3]; add_strip (check->vertindex[(k + 2) % 3], j);
striptris[stripcount] = j;
stripcount++;
used[j] = 2; used[j] = 2;
goto nexttri; goto nexttri;
@ -132,12 +186,9 @@ done:
if (used[j] == 2) if (used[j] == 2)
used[j] = 0; used[j] = 0;
return stripcount; return stripcount - 2;
} }
/*
FanLength
*/
int int
FanLength (int starttri, int startv) FanLength (int starttri, int startv)
{ {
@ -150,12 +201,10 @@ FanLength (int starttri, int startv)
last = &triangles[starttri]; last = &triangles[starttri];
stripverts[0] = last->vertindex[(startv) % 3]; stripcount = 0;
stripverts[1] = last->vertindex[(startv + 1) % 3]; add_strip (last->vertindex[(startv) % 3], starttri);
stripverts[2] = last->vertindex[(startv + 2) % 3]; add_strip (last->vertindex[(startv + 1) % 3], starttri);
add_strip (last->vertindex[(startv + 2) % 3], starttri);
striptris[0] = starttri;
stripcount = 1;
m1 = last->vertindex[(startv + 0) % 3]; m1 = last->vertindex[(startv + 0) % 3];
m2 = last->vertindex[(startv + 2) % 3]; m2 = last->vertindex[(startv + 2) % 3];
@ -182,9 +231,7 @@ FanLength (int starttri, int startv)
// the new edge // the new edge
m2 = check->vertindex[(k + 2) % 3]; m2 = check->vertindex[(k + 2) % 3];
stripverts[stripcount + 2] = m2; add_strip (m2, j);
striptris[stripcount] = j;
stripcount++;
used[j] = 2; used[j] = 2;
goto nexttri; goto nexttri;
@ -197,7 +244,7 @@ FanLength (int starttri, int startv)
if (used[j] == 2) if (used[j] == 2)
used[j] = 0; used[j] = 0;
return stripcount; return stripcount - 2;
} }
@ -214,16 +261,17 @@ BuildTris (void)
int startv; int startv;
float s, t; float s, t;
int len, bestlen, besttype = 0; int len, bestlen, besttype = 0;
int bestverts[1024]; int *bestverts = 0;
int besttris[1024]; int *besttris = 0;
int type; int type;
//
// build tristrips // build tristrips
//
numorder = 0; numorder = 0;
numcommands = 0; numcommands = 0;
memset (used, 0, sizeof (used)); stripcount = 0;
alloc_used (pheader->mdl.numtris);
memset (used, 0, used_size * sizeof (used[0]));
for (i = 0; i < pheader->mdl.numtris; i++) { for (i = 0; i < pheader->mdl.numtris; i++) {
// pick an unused triangle and start the trifan // pick an unused triangle and start the trifan
if (used[i]) if (used[i])
@ -241,27 +289,31 @@ BuildTris (void)
if (len > bestlen) { if (len > bestlen) {
besttype = type; besttype = type;
bestlen = len; bestlen = len;
for (j = 0; j < bestlen + 2; j++) if (bestverts)
bestverts[j] = stripverts[j]; free (bestverts);
for (j = 0; j < bestlen; j++) if (besttris)
besttris[j] = striptris[j]; free (besttris);
bestverts = stripverts;
besttris = striptris;
stripverts = striptris = 0;
strip_size = 0;
} }
} }
} }
// mark the tris on the best strip as used // mark the tris on the best strip as used
for (j = 0; j < bestlen; j++) for (j = 0; j < bestlen; j++)
used[besttris[j]] = 1; used[besttris[j + 2]] = 1;
if (besttype == 1) if (besttype == 1)
commands[numcommands++] = (bestlen + 2); add_command (bestlen + 2);
else else
commands[numcommands++] = -(bestlen + 2); add_command (-(bestlen + 2));
for (j = 0; j < bestlen + 2; j++) { for (j = 0; j < bestlen + 2; j++) {
// emit a vertex into the reorder buffer // emit a vertex into the reorder buffer
k = bestverts[j]; k = bestverts[j];
vertexorder[numorder++] = k; add_vertex (k);
// emit s/t coords into the commands stream // emit s/t coords into the commands stream
s = stverts[k].s; s = stverts[k].s;
@ -271,24 +323,25 @@ BuildTris (void)
s = (s + 0.5) / pheader->mdl.skinwidth; s = (s + 0.5) / pheader->mdl.skinwidth;
t = (t + 0.5) / pheader->mdl.skinheight; t = (t + 0.5) / pheader->mdl.skinheight;
*(float *) &commands[numcommands++] = s; add_command (*(int*)&s);
*(float *) &commands[numcommands++] = t; add_command (*(int*)&t);
} }
} }
commands[numcommands++] = 0; // end of list marker add_command (0); // end of list marker
Con_DPrintf ("%3i tri %3i vert %3i cmd\n", pheader->mdl.numtris, numorder, Con_DPrintf ("%3i tri %3i vert %3i cmd\n", pheader->mdl.numtris, numorder,
numcommands); numcommands);
allverts += numorder; allverts += numorder;
alltris += pheader->mdl.numtris; alltris += pheader->mdl.numtris;
if (bestverts)
free (bestverts);
if (besttris)
free (besttris);
} }
/*
GL_MakeAliasModelDisplayLists
*/
void void
GL_MakeAliasModelDisplayLists (model_t *m, aliashdr_t *hdr, void *_m, int _s) GL_MakeAliasModelDisplayLists (model_t *m, aliashdr_t *hdr, void *_m, int _s)
{ {
@ -296,73 +349,103 @@ GL_MakeAliasModelDisplayLists (model_t *m, aliashdr_t *hdr, void *_m, int _s)
int *cmds; int *cmds;
trivertx_t *verts; trivertx_t *verts;
char cache[MAX_QPATH], fullpath[MAX_OSPATH]; char cache[MAX_QPATH], fullpath[MAX_OSPATH];
QFile *f; VFile *f;
unsigned char model_digest[MDFOUR_DIGEST_BYTES]; unsigned char model_digest[MDFOUR_DIGEST_BYTES];
unsigned char mesh_digest[MDFOUR_DIGEST_BYTES]; unsigned char mesh_digest[MDFOUR_DIGEST_BYTES];
qboolean remesh = true; qboolean remesh = true;
qboolean do_cache = false;
aliasmodel = m; aliasmodel = m;
paliashdr = hdr; // (aliashdr_t *)Mod_Extradata (m); paliashdr = hdr; // (aliashdr_t *)Mod_Extradata (m);
if (gl_mesh_cache->int_val
&& gl_mesh_cache->int_val <= paliashdr->mdl.numtris) {
do_cache = true;
mdfour (model_digest, (unsigned char *) _m, _s); mdfour (model_digest, (unsigned char *) _m, _s);
//
// look for a cached version // look for a cached version
//
strcpy (cache, "glquake/"); strcpy (cache, "glquake/");
COM_StripExtension (m->name + strlen ("progs/"), COM_StripExtension (m->name + strlen ("progs/"),
cache + strlen ("glquake/")); cache + strlen ("glquake/"));
strncat (cache, ".ms2", sizeof (cache) - strlen (cache)); strncat (cache, ".qfms", sizeof (cache) - strlen (cache));
COM_FOpenFile (cache, &f); COM_FOpenFile (cache, &f);
if (f) { if (f) {
unsigned char d1[MDFOUR_DIGEST_BYTES]; unsigned char d1[MDFOUR_DIGEST_BYTES];
unsigned char d2[MDFOUR_DIGEST_BYTES]; unsigned char d2[MDFOUR_DIGEST_BYTES];
struct mdfour md; struct mdfour md;
int c[8192]; int *c = 0;
int nc; int nc = 0;
int vo[8192]; int *vo = 0;
int no; int no = 0;
int len;
int vers;
memset (d1, 0, sizeof (d1)); memset (d1, 0, sizeof (d1));
memset (d2, 0, sizeof (d2)); memset (d2, 0, sizeof (d2));
Qread (f, &nc, 4);
Qread (f, &no, 4); Qread (f, &vers, sizeof (int));
if (nc <= 8192 && no <= 8192) { Qread (f, &len, sizeof (int));
Qread (f, &c, nc * sizeof (c[0])); Qread (f, &nc, sizeof (int));
Qread (f, &vo, no * sizeof (vo[0])); Qread (f, &no, sizeof (int));
if (vers == 1 && (nc + no) == len) {
c = malloc (((nc + 1023) & ~1023) * sizeof (c[0]));
vo = malloc (((no + 1023) & ~1023) * sizeof (vo[0]));
if (!c || !vo)
Sys_Error ("gl_mesh.c: out of memory");
Qread (f, c, nc * sizeof (c[0]));
Qread (f, vo, no * sizeof (vo[0]));
Qread (f, d1, MDFOUR_DIGEST_BYTES); Qread (f, d1, MDFOUR_DIGEST_BYTES);
Qread (f, d2, MDFOUR_DIGEST_BYTES); Qread (f, d2, MDFOUR_DIGEST_BYTES);
Qclose (f); Qclose (f);
mdfour_begin (&md); mdfour_begin (&md);
mdfour_update (&md, (unsigned char*)&nc, 4); mdfour_update (&md, (unsigned char *) &vers, sizeof(int));
mdfour_update (&md, (unsigned char*)&no, 4); mdfour_update (&md, (unsigned char *) &len, sizeof(int));
mdfour_update (&md, (unsigned char*)&c, nc * sizeof (c[0])); mdfour_update (&md, (unsigned char *) &nc, sizeof(int));
mdfour_update (&md, (unsigned char*)&vo, no * sizeof (vo[0])); mdfour_update (&md, (unsigned char *) &no, sizeof(int));
mdfour_update (&md, (unsigned char *) c, nc * sizeof (c[0]));
mdfour_update (&md, (unsigned char *) vo, no * sizeof (vo[0]));
mdfour_update (&md, d1, MDFOUR_DIGEST_BYTES); mdfour_update (&md, d1, MDFOUR_DIGEST_BYTES);
mdfour_result (&md, mesh_digest); mdfour_result (&md, mesh_digest);
if (memcmp (d2, mesh_digest, MDFOUR_DIGEST_BYTES) == 0 && memcmp (d1, model_digest, MDFOUR_DIGEST_BYTES) == 0) { if (memcmp (d2, mesh_digest, MDFOUR_DIGEST_BYTES) == 0
&& memcmp (d1, model_digest, MDFOUR_DIGEST_BYTES) == 0) {
remesh = false; remesh = false;
numcommands = nc; numcommands = nc;
numorder = no; numorder = no;
if (numcommands > commands_size) {
if (commands)
free (commands);
commands_size = (numcommands + 1023) & ~1023;
commands = c;
} else {
memcpy (commands, c, numcommands * sizeof (c[0])); memcpy (commands, c, numcommands * sizeof (c[0]));
free(c);
}
if (numorder > vertexorder_size) {
if (vertexorder)
free (vertexorder);
vertexorder_size = (numorder + 1023) & ~1023;
vertexorder = vo;
} else {
memcpy (vertexorder, vo, numorder * sizeof (vo[0])); memcpy (vertexorder, vo, numorder * sizeof (vo[0]));
free (vo);
}
}
} }
} }
} }
if (remesh) { if (remesh) {
//
// build it from scratch // build it from scratch
// Con_DPrintf ("meshing %s...\n", m->name);
Con_Printf ("meshing %s...\n", m->name);
BuildTris (); // trifans or lists BuildTris (); // trifans or lists
// if (do_cache) {
// save out the cached version // save out the cached version
//
snprintf (fullpath, sizeof (fullpath), "%s/%s", com_gamedir, cache); snprintf (fullpath, sizeof (fullpath), "%s/%s", com_gamedir, cache);
f = Qopen (fullpath, "wbz9"); f = Qopen (fullpath, "wbz9");
if (!f) { if (!f) {
@ -372,34 +455,40 @@ GL_MakeAliasModelDisplayLists (model_t *m, aliashdr_t *hdr, void *_m, int _s)
if (f) { if (f) {
struct mdfour md; struct mdfour md;
int vers = 1;
int len = numcommands + numorder;
mdfour_begin (&md); mdfour_begin (&md);
mdfour_update (&md, (unsigned char*)&numcommands, 4); mdfour_update (&md, (unsigned char *) &vers, sizeof (int));
mdfour_update (&md, (unsigned char*)&numorder, 4); mdfour_update (&md, (unsigned char *) &len, sizeof (int));
mdfour_update (&md, (unsigned char*)&commands, numcommands * sizeof (commands[0])); mdfour_update (&md, (unsigned char *) &numcommands, sizeof (int));
mdfour_update (&md, (unsigned char*)&vertexorder, mdfour_update (&md, (unsigned char *) &numorder, sizeof (int));
mdfour_update (&md, (unsigned char *) commands,
numcommands * sizeof (commands[0]));
mdfour_update (&md, (unsigned char *) vertexorder,
numorder * sizeof (vertexorder[0])); numorder * sizeof (vertexorder[0]));
mdfour_update (&md, model_digest, MDFOUR_DIGEST_BYTES); mdfour_update (&md, model_digest, MDFOUR_DIGEST_BYTES);
mdfour_result (&md, mesh_digest); mdfour_result (&md, mesh_digest);
Qwrite (f, &numcommands, 4); Qwrite (f, &vers, sizeof (int));
Qwrite (f, &numorder, 4); Qwrite (f, &len, sizeof (int));
Qwrite (f, &commands, numcommands * sizeof (commands[0])); Qwrite (f, &numcommands, sizeof (int));
Qwrite (f, &vertexorder, numorder * sizeof (vertexorder[0])); Qwrite (f, &numorder, sizeof (int));
Qwrite (f, commands, numcommands * sizeof (commands[0]));
Qwrite (f, vertexorder, numorder * sizeof (vertexorder[0]));
Qwrite (f, model_digest, MDFOUR_DIGEST_BYTES); Qwrite (f, model_digest, MDFOUR_DIGEST_BYTES);
Qwrite (f, mesh_digest, MDFOUR_DIGEST_BYTES); Qwrite (f, mesh_digest, MDFOUR_DIGEST_BYTES);
Qclose (f); Qclose (f);
} }
} }
}
// save the data out // save the data out
paliashdr->poseverts = numorder; paliashdr->poseverts = numorder;
cmds = Hunk_Alloc (numcommands * 4); cmds = Hunk_Alloc (numcommands * sizeof (int));
paliashdr->commands = (byte *) cmds - (byte *) paliashdr; paliashdr->commands = (byte *) cmds - (byte *) paliashdr;
memcpy (cmds, commands, numcommands * 4); memcpy (cmds, commands, numcommands * sizeof (int));
verts = Hunk_Alloc (paliashdr->numposes * paliashdr->poseverts verts = Hunk_Alloc (paliashdr->numposes * paliashdr->poseverts

View file

@ -57,6 +57,7 @@ model_t mod_known[MAX_MOD_KNOWN];
int mod_numknown; int mod_numknown;
cvar_t *gl_subdivide_size; cvar_t *gl_subdivide_size;
cvar_t *gl_mesh_cache;
extern byte mod_novis[MAX_MAP_LEAFS / 8]; extern byte mod_novis[MAX_MAP_LEAFS / 8];
@ -76,6 +77,9 @@ Mod_Init_Cvars (void)
{ {
gl_subdivide_size = gl_subdivide_size =
Cvar_Get ("gl_subdivide_size", "128", CVAR_ARCHIVE, NULL, "Sets the division value for the sky brushes."); Cvar_Get ("gl_subdivide_size", "128", CVAR_ARCHIVE, NULL, "Sets the division value for the sky brushes.");
gl_mesh_cache = Cvar_Get ("gl_mesh_cache", "256", CVAR_ARCHIVE, NULL,
"minimum triangle count in a model for its mesh"
" to be cached. 0 to disable caching");
} }
/* /*