2001-02-19 21:15:25 +00:00
|
|
|
/*
|
|
|
|
model.c
|
|
|
|
|
|
|
|
model loading and caching
|
|
|
|
|
|
|
|
Copyright (C) 1996-1997 Id Software, Inc.
|
|
|
|
|
|
|
|
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:
|
|
|
|
|
|
|
|
Free Software Foundation, Inc.
|
|
|
|
59 Temple Place - Suite 330
|
|
|
|
Boston, MA 02111-1307, USA
|
|
|
|
|
|
|
|
$Id$
|
|
|
|
*/
|
|
|
|
|
|
|
|
// models are the only shared resource between a client and server running
|
|
|
|
// on the same machine.
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRING_H
|
|
|
|
# include <string.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRINGS_H
|
|
|
|
# include <strings.h>
|
|
|
|
#endif
|
|
|
|
|
2001-05-10 06:01:11 +00:00
|
|
|
#include "QF/console.h"
|
2001-03-27 20:33:07 +00:00
|
|
|
#include "QF/cvar.h"
|
2001-04-10 06:55:28 +00:00
|
|
|
#include "QF/model.h"
|
2001-03-27 20:33:07 +00:00
|
|
|
#include "QF/qendian.h"
|
2001-05-10 06:01:11 +00:00
|
|
|
#include "QF/sys.h"
|
2001-05-30 04:34:06 +00:00
|
|
|
#include "QF/vfs.h"
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
void Mod_LoadAliasModel (model_t *mod, void *buf);
|
|
|
|
void Mod_LoadSpriteModel (model_t *mod, void *buf);
|
|
|
|
void Mod_LoadBrushModel (model_t *mod, void *buf);
|
|
|
|
|
|
|
|
model_t *loadmodel;
|
|
|
|
char loadname[32]; // for hunk tags
|
|
|
|
|
|
|
|
#define MAX_MOD_KNOWN 512
|
|
|
|
model_t mod_known[MAX_MOD_KNOWN];
|
|
|
|
int mod_numknown;
|
|
|
|
|
|
|
|
cvar_t *gl_subdivide_size;
|
|
|
|
|
|
|
|
extern byte mod_novis[MAX_MAP_LEAFS / 8];
|
|
|
|
|
|
|
|
texture_t *r_notexture_mip;
|
|
|
|
|
|
|
|
/*
|
|
|
|
Mod_Init
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
Mod_Init (void)
|
|
|
|
{
|
2001-05-16 17:08:56 +00:00
|
|
|
int x, y, m;
|
|
|
|
byte *dest;
|
2001-05-31 18:11:05 +00:00
|
|
|
int mip0size = 16*16;
|
|
|
|
int mip1size = 8*8;
|
|
|
|
int mip2size = 4*4;
|
|
|
|
int mip3size = 2*2;
|
2001-05-16 17:08:56 +00:00
|
|
|
|
2001-02-19 21:15:25 +00:00
|
|
|
memset (mod_novis, 0xff, sizeof (mod_novis));
|
2001-05-31 18:11:05 +00:00
|
|
|
r_notexture_mip = Hunk_AllocName (sizeof (texture_t)
|
|
|
|
+ mip0size + mip1size
|
|
|
|
+ mip2size + mip3size, "notexture");
|
2001-05-16 17:08:56 +00:00
|
|
|
|
|
|
|
r_notexture_mip->width = r_notexture_mip->height = 16;
|
|
|
|
r_notexture_mip->offsets[0] = sizeof (texture_t);
|
|
|
|
|
2001-05-31 18:11:05 +00:00
|
|
|
r_notexture_mip->offsets[1] = r_notexture_mip->offsets[0] + mip0size;
|
|
|
|
r_notexture_mip->offsets[2] = r_notexture_mip->offsets[1] + mip1size;
|
|
|
|
r_notexture_mip->offsets[3] = r_notexture_mip->offsets[2] + mip2size;
|
2001-05-16 17:08:56 +00:00
|
|
|
|
|
|
|
for (m = 0; m < 4; m++) {
|
|
|
|
dest = (byte *) r_notexture_mip + r_notexture_mip->offsets[m];
|
|
|
|
for (y = 0; y < (16 >> m); y++)
|
|
|
|
for (x = 0; x < (16 >> m); x++) {
|
|
|
|
if ((y < (8 >> m)) ^ (x < (8 >> m)))
|
|
|
|
*dest++ = 0;
|
|
|
|
else
|
|
|
|
*dest++ = 0xff;
|
|
|
|
}
|
|
|
|
}
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Mod_Init_Cvars (void)
|
|
|
|
{
|
|
|
|
gl_subdivide_size =
|
2001-04-10 23:39:30 +00:00
|
|
|
Cvar_Get ("gl_subdivide_size", "128", CVAR_ARCHIVE, NULL,
|
|
|
|
"Sets the division value for the sky brushes.");
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Mod_ClearAll
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
Mod_ClearAll (void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
model_t *mod;
|
|
|
|
|
|
|
|
for (i = 0, mod = mod_known; i < mod_numknown; i++, mod++)
|
|
|
|
if (mod->type != mod_alias)
|
|
|
|
mod->needload = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Mod_FindName
|
|
|
|
*/
|
|
|
|
model_t *
|
|
|
|
Mod_FindName (char *name)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
model_t *mod;
|
|
|
|
|
|
|
|
if (!name[0])
|
2001-05-10 15:29:29 +00:00
|
|
|
Sys_Error ("Mod_FindName: NULL name");
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// search the currently loaded models
|
|
|
|
//
|
|
|
|
for (i = 0, mod = mod_known; i < mod_numknown; i++, mod++)
|
|
|
|
if (!strcmp (mod->name, name))
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (i == mod_numknown) {
|
|
|
|
if (mod_numknown == MAX_MOD_KNOWN)
|
2001-05-10 06:01:11 +00:00
|
|
|
Sys_Error ("mod_numknown == MAX_MOD_KNOWN");
|
2001-02-19 21:15:25 +00:00
|
|
|
strcpy (mod->name, name);
|
|
|
|
mod->needload = true;
|
|
|
|
mod_numknown++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mod;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Mod_LoadModel
|
|
|
|
|
|
|
|
Loads a model into the cache
|
|
|
|
*/
|
|
|
|
model_t *
|
|
|
|
Mod_LoadModel (model_t *mod, qboolean crash)
|
|
|
|
{
|
|
|
|
void *d;
|
|
|
|
unsigned int *buf;
|
|
|
|
byte stackbuf[1024]; // avoid dirtying the cache heap
|
|
|
|
|
|
|
|
if (!mod->needload) {
|
|
|
|
if (mod->type == mod_alias) {
|
|
|
|
d = Cache_Check (&mod->cache);
|
|
|
|
if (d)
|
|
|
|
return mod;
|
|
|
|
} else
|
|
|
|
return mod; // not cached at all
|
|
|
|
}
|
|
|
|
//
|
|
|
|
// load the file
|
|
|
|
//
|
|
|
|
buf =
|
|
|
|
(unsigned int *) COM_LoadStackFile (mod->name, stackbuf,
|
|
|
|
sizeof (stackbuf));
|
|
|
|
if (!buf) {
|
|
|
|
if (crash)
|
2001-05-10 15:29:29 +00:00
|
|
|
Sys_Error ("Mod_LoadModel: %s not found", mod->name);
|
2001-02-19 21:15:25 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
//
|
|
|
|
// allocate a new model
|
|
|
|
//
|
|
|
|
COM_FileBase (mod->name, loadname);
|
|
|
|
|
|
|
|
loadmodel = mod;
|
|
|
|
|
|
|
|
//
|
|
|
|
// fill it in
|
|
|
|
//
|
|
|
|
|
|
|
|
// call the apropriate loader
|
|
|
|
mod->needload = false;
|
|
|
|
mod->hasfullbrights = false;
|
|
|
|
|
|
|
|
switch (LittleLong (*(unsigned int *) buf)) {
|
|
|
|
case IDPOLYHEADER:
|
|
|
|
Mod_LoadAliasModel (mod, buf);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IDSPRITEHEADER:
|
|
|
|
Mod_LoadSpriteModel (mod, buf);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
Mod_LoadBrushModel (mod, buf);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mod;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Mod_ForName
|
|
|
|
|
|
|
|
Loads in a model for the given name
|
|
|
|
*/
|
|
|
|
model_t *
|
|
|
|
Mod_ForName (char *name, qboolean crash)
|
|
|
|
{
|
|
|
|
model_t *mod;
|
|
|
|
|
|
|
|
mod = Mod_FindName (name);
|
|
|
|
|
|
|
|
Con_DPrintf ("Mod_ForName: %s, %p\n", name, mod);
|
|
|
|
return Mod_LoadModel (mod, crash);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2001-05-10 15:29:29 +00:00
|
|
|
Mod_Extradata
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
Caches the data if needed
|
|
|
|
*/
|
|
|
|
void *
|
|
|
|
Mod_Extradata (model_t *mod)
|
|
|
|
{
|
|
|
|
void *r;
|
|
|
|
|
|
|
|
r = Cache_Check (&mod->cache);
|
|
|
|
if (r)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
Mod_LoadModel (mod, true);
|
|
|
|
|
|
|
|
if (!mod->cache.data)
|
2001-05-10 06:01:11 +00:00
|
|
|
Sys_Error ("Mod_Extradata: caching failed");
|
2001-02-19 21:15:25 +00:00
|
|
|
return mod->cache.data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Mod_TouchModel
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
Mod_TouchModel (char *name)
|
|
|
|
{
|
|
|
|
model_t *mod;
|
|
|
|
|
|
|
|
mod = Mod_FindName (name);
|
|
|
|
|
|
|
|
if (!mod->needload) {
|
|
|
|
if (mod->type == mod_alias)
|
|
|
|
Cache_Check (&mod->cache);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Mod_Print
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
Mod_Print (void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
model_t *mod;
|
|
|
|
|
|
|
|
Con_Printf ("Cached models:\n");
|
|
|
|
for (i = 0, mod = mod_known; i < mod_numknown; i++, mod++) {
|
|
|
|
Con_Printf ("%8p : %s\n", mod->cache.data, mod->name);
|
|
|
|
}
|
|
|
|
}
|