jedioutcast/code/ghoul2/g2_surfaces.cpp

424 lines
12 KiB
C++
Raw Normal View History

2013-04-04 14:52:42 +00:00
// leave this as first line for PCH reasons...
//
#include "../server/exe_headers.h"
#ifndef __Q_SHARED_H
#include "../game/q_shared.h"
#endif
#if !defined(TR_LOCAL_H)
#include "../renderer/tr_local.h"
#endif
#if !defined(G2_H_INC)
#include "G2.h"
#endif
2013-04-04 18:02:27 +00:00
#define G2_MODEL_OK(g) ((g)&&(g)->mValid&&(g)->aHeader&&(g)->currentModel&&(g)->animModel)
2013-04-04 14:52:42 +00:00
#pragma warning(disable : 4512) //assignment op could not be genereated
2013-04-04 18:01:17 +00:00
class CQuickOverride
2013-04-04 14:52:42 +00:00
{
2013-04-04 18:01:17 +00:00
int mOverride[512];
int mAt[512];
int mCurrentTouch;
2013-04-04 14:52:42 +00:00
public:
2013-04-04 18:01:17 +00:00
CQuickOverride()
{
mCurrentTouch=1;
int i;
for (i=0;i<512;i++)
{
mOverride[i]=0;
}
}
void Invalidate()
{
mCurrentTouch++;
}
void Set(int index,int pos)
{
if (index==10000)
{
return;
}
assert(index>=0&&index<512);
mOverride[index]=mCurrentTouch;
mAt[index]=pos;
}
int Test(int index)
{
assert(index>=0&&index<512);
if (mOverride[index]!=mCurrentTouch)
{
return -1;
}
else
{
return mAt[index];
}
}
2013-04-04 14:52:42 +00:00
};
2013-04-04 18:01:17 +00:00
static CQuickOverride QuickOverride;
2013-04-04 14:52:42 +00:00
// find a particular surface in the surface override list
2013-04-04 18:01:17 +00:00
const surfaceInfo_t *G2_FindOverrideSurface(int surfaceNum,const surfaceInfo_v &surfaceList)
2013-04-04 14:52:42 +00:00
{
2013-04-04 18:01:17 +00:00
if (surfaceNum<0)
{
// starting a new lookup
QuickOverride.Invalidate();
int i;
for(i=0; i<surfaceList.size(); i++)
{
if (surfaceList[i].surface>=0)
{
QuickOverride.Set(surfaceList[i].surface,i);
}
}
return NULL;
}
int idx=QuickOverride.Test(surfaceNum);
if (idx<0)
2013-04-04 14:52:42 +00:00
{
2013-04-04 18:01:17 +00:00
int i;
if (surfaceNum==10000)
2013-04-04 14:52:42 +00:00
{
2013-04-04 18:01:17 +00:00
for(i=0; i<surfaceList.size(); i++)
{
if (surfaceList[i].surface == surfaceNum)
{
return &surfaceList[i];
}
}
}
#if _DEBUG
// look through entire list
for(i=0; i<surfaceList.size(); i++)
{
if (surfaceList[i].surface == surfaceNum)
{
break;
}
2013-04-04 14:52:42 +00:00
}
2013-04-04 18:01:17 +00:00
// didn't find it.
assert(i==surfaceList.size()); // our quickoverride is not working right
#endif
return NULL;
2013-04-04 14:52:42 +00:00
}
2013-04-04 18:01:17 +00:00
assert(idx>=0&&idx<surfaceList.size());
assert(surfaceList[idx].surface == surfaceNum);
return &surfaceList[idx];
2013-04-04 14:52:42 +00:00
}
2013-04-04 18:01:17 +00:00
2013-04-04 14:52:42 +00:00
// given a surface name, lets see if it's legal in the model
2013-04-04 18:02:27 +00:00
int G2_IsSurfaceLegal(const model_s *mod_m, const char *surfaceName, int *flags)
2013-04-04 14:52:42 +00:00
{
2013-04-04 18:02:27 +00:00
assert(mod_m);
assert(mod_m->mdxm);
2013-04-04 14:52:42 +00:00
// damn include file dependancies
mdxmSurfHierarchy_t *surf;
surf = (mdxmSurfHierarchy_t *) ( (byte *)mod_m->mdxm + mod_m->mdxm->ofsSurfHierarchy );
for ( int i = 0 ; i < mod_m->mdxm->numSurfaces ; i++)
{
if (!stricmp(surfaceName, surf->name))
{
*flags = surf->flags;
return i;
}
// find the next surface
surf = (mdxmSurfHierarchy_t *)( (byte *)surf + (int)( &((mdxmSurfHierarchy_t *)0)->childIndexes[ surf->numChildren ] ));
}
return -1;
}
/************************************************************************************************
* G2_FindSurface
* find a surface in a ghoul2 surface override list based on it's name
*
* Input
* filename of model, surface list of model instance, name of surface, int to be filled in
* with the index of this surface (defaults to NULL)
*
* Output
* pointer to surface if successful, false otherwise
*
************************************************************************************************/
2013-04-04 18:02:27 +00:00
const mdxmSurface_t *G2_FindSurface(CGhoul2Info *ghlInfo, surfaceInfo_v &slist, const char *surfaceName,
2013-04-04 14:52:42 +00:00
int *surfIndex/*NULL*/)
{
int i = 0;
// find the model we want
2013-04-04 18:02:27 +00:00
assert(G2_MODEL_OK(ghlInfo));
2013-04-04 14:52:42 +00:00
2013-04-04 18:02:27 +00:00
const mdxmHierarchyOffsets_t *surfIndexes = (mdxmHierarchyOffsets_t *)((byte *)ghlInfo->currentModel->mdxm + sizeof(mdxmHeader_t));
2013-04-04 14:52:42 +00:00
// first find if we already have this surface in the list
for (i = slist.size() - 1; i >= 0; i--)
{
if ((slist[i].surface != 10000) && (slist[i].surface != -1))
{
2013-04-04 18:02:27 +00:00
const mdxmSurface_t *surf = (mdxmSurface_t *)G2_FindSurface(ghlInfo->currentModel, slist[i].surface, 0);
2013-04-04 14:52:42 +00:00
// back track and get the surfinfo struct for this surface
const mdxmSurfHierarchy_t *surfInfo = (mdxmSurfHierarchy_t *)((byte *)surfIndexes + surfIndexes->offsets[surf->thisSurfaceIndex]);
// are these the droids we're looking for?
if (!stricmp (surfInfo->name, surfaceName))
{
// yup
if (surfIndex)
{
*surfIndex = i;
}
return surf;
}
}
}
// didn't find it
if (surfIndex)
{
*surfIndex = -1;
}
return 0;
}
// set a named surface offFlags - if it doesn't find a surface with this name in the list then it will add one.
2013-04-04 18:02:27 +00:00
qboolean G2_SetSurfaceOnOff (CGhoul2Info *ghlInfo, surfaceInfo_v &slist, const char *surfaceName, const int offFlags)
2013-04-04 14:52:42 +00:00
{
int surfIndex = -1;
surfaceInfo_t temp_slist_entry;
// find the model we want
// first find if we already have this surface in the list
2013-04-04 18:02:27 +00:00
const mdxmSurface_t *surf = G2_FindSurface(ghlInfo, slist, surfaceName, &surfIndex);
2013-04-04 14:52:42 +00:00
if (surf)
{
// set descendants value
2013-04-04 18:02:27 +00:00
// slist[surfIndex].offFlags = offFlags;
// seems to me that we shouldn't overwrite the other flags.
// the only bit we really care about in the incoming flags is the off bit
slist[surfIndex].offFlags &= ~(G2SURFACEFLAG_OFF | G2SURFACEFLAG_NODESCENDANTS);
slist[surfIndex].offFlags |= offFlags & (G2SURFACEFLAG_OFF | G2SURFACEFLAG_NODESCENDANTS);
2013-04-04 14:52:42 +00:00
return qtrue;
}
else
{
// ok, not in the list already - in that case, lets verify this surface exists in the model mesh
int flags;
2013-04-04 18:02:27 +00:00
int surfaceNum = G2_IsSurfaceLegal(ghlInfo->currentModel, surfaceName, &flags);
2013-04-04 14:52:42 +00:00
if (surfaceNum != -1)
{
// the only bit we really care about in the incoming flags is the off bit
2013-04-04 18:02:27 +00:00
flags &= ~(G2SURFACEFLAG_OFF | G2SURFACEFLAG_NODESCENDANTS);
2013-04-04 14:52:42 +00:00
flags |= offFlags & (G2SURFACEFLAG_OFF | G2SURFACEFLAG_NODESCENDANTS);
// insert here then
temp_slist_entry.offFlags = flags;
temp_slist_entry.surface = surfaceNum;
slist.push_back(temp_slist_entry);
return qtrue;
}
}
return qfalse;
}
void G2_FindRecursiveSurface(const model_t *currentModel, int surfaceNum, surfaceInfo_v &rootList, int *activeSurfaces)
{
2013-04-04 18:02:27 +00:00
assert(currentModel);
assert(currentModel->mdxm);
2013-04-04 14:52:42 +00:00
int i;
2013-04-04 18:02:27 +00:00
const mdxmSurface_t *surface = (mdxmSurface_t *)G2_FindSurface(currentModel, surfaceNum, 0);
2013-04-04 14:52:42 +00:00
const mdxmHierarchyOffsets_t *surfIndexes = (mdxmHierarchyOffsets_t *)((byte *)currentModel->mdxm + sizeof(mdxmHeader_t));
const mdxmSurfHierarchy_t *surfInfo = (mdxmSurfHierarchy_t *)((byte *)surfIndexes + surfIndexes->offsets[surface->thisSurfaceIndex]);
// see if we have an override surface in the surface list
const surfaceInfo_t *surfOverride = G2_FindOverrideSurface(surfaceNum, rootList);
// really, we should use the default flags for this surface unless it's been overriden
int offFlags = surfInfo->flags;
// set the off flags if we have some
if (surfOverride)
{
offFlags = surfOverride->offFlags;
}
// if this surface is not off, indicate as such in the active surface list
if (!(offFlags & G2SURFACEFLAG_OFF))
{
activeSurfaces[surfaceNum] = 1;
}
else
// if we are turning off all descendants, then stop this recursion now
if (offFlags & G2SURFACEFLAG_NODESCENDANTS)
{
return;
}
// now recursively call for the children
for (i=0; i< surfInfo->numChildren; i++)
{
surfaceNum = surfInfo->childIndexes[i];
G2_FindRecursiveSurface(currentModel, surfaceNum, rootList, activeSurfaces);
}
}
2013-04-04 18:02:27 +00:00
qboolean G2_SetRootSurface( CGhoul2Info_v &ghoul2, const int modelIndex, const char *surfaceName)
2013-04-04 14:52:42 +00:00
{
int surf;
2013-04-04 18:02:27 +00:00
int flags;
assert(modelIndex>=0&&modelIndex<ghoul2.size());
assert(ghoul2[modelIndex].currentModel);
assert(ghoul2[modelIndex].currentModel->mdxm);
2013-04-04 14:52:42 +00:00
// first find if we already have this surface in the list
2013-04-04 18:02:27 +00:00
surf = G2_IsSurfaceLegal(ghoul2[modelIndex].currentModel, surfaceName, &flags);
2013-04-04 14:52:42 +00:00
if (surf != -1)
{
ghoul2[modelIndex].mSurfaceRoot = surf;
2013-04-04 18:02:27 +00:00
return qtrue;
2013-04-04 14:52:42 +00:00
}
assert(0);
return qfalse;
}
2013-04-04 18:02:27 +00:00
extern int G2_DecideTraceLod(CGhoul2Info &ghoul2, int useLod);
2013-04-04 14:52:42 +00:00
int G2_AddSurface(CGhoul2Info *ghoul2, int surfaceNumber, int polyNumber, float BarycentricI, float BarycentricJ, int lod )
{
2013-04-04 18:02:27 +00:00
lod = G2_DecideTraceLod(*ghoul2, lod);
2013-04-04 14:52:42 +00:00
// first up, see if we have a free one already set up - look only from the end of the constant surfaces onwards
2013-04-04 18:02:27 +00:00
int i;
for (i=0; i<ghoul2->mSlist.size(); i++)
2013-04-04 14:52:42 +00:00
{
// is the surface count -1? That would indicate it's free
if (ghoul2->mSlist[i].surface == -1)
{
2013-04-04 18:02:27 +00:00
break;
2013-04-04 14:52:42 +00:00
}
}
2013-04-04 18:02:27 +00:00
if (i==ghoul2->mSlist.size())
{
ghoul2->mSlist.push_back(surfaceInfo_t());
}
ghoul2->mSlist[i].offFlags = G2SURFACEFLAG_GENERATED;
ghoul2->mSlist[i].surface = 10000; // no model will ever have 10000 surfaces
ghoul2->mSlist[i].genBarycentricI = BarycentricI;
ghoul2->mSlist[i].genBarycentricJ = BarycentricJ;
ghoul2->mSlist[i].genPolySurfaceIndex = ((polyNumber & 0xffff) << 16) | (surfaceNumber & 0xffff);
ghoul2->mSlist[i].genLod = lod;
return i;
2013-04-04 14:52:42 +00:00
}
qboolean G2_RemoveSurface(surfaceInfo_v &slist, const int index)
{
if (index != -1)
{
slist[index].surface = -1;
return qtrue;
}
assert(0);
return qfalse;
}
2013-04-04 18:02:27 +00:00
int G2_GetParentSurface(CGhoul2Info *ghlInfo, const int index)
2013-04-04 14:52:42 +00:00
{
2013-04-04 18:02:27 +00:00
assert(ghlInfo->currentModel);
assert(ghlInfo->currentModel->mdxm);
const mdxmHierarchyOffsets_t *surfIndexes = (mdxmHierarchyOffsets_t *)((byte *)ghlInfo->currentModel->mdxm + sizeof(mdxmHeader_t));
2013-04-04 14:52:42 +00:00
// walk each surface and see if this index is listed in it's children
2013-04-04 18:02:27 +00:00
const mdxmSurface_t *surf = (mdxmSurface_t *)G2_FindSurface(ghlInfo->currentModel, index, 0);
2013-04-04 14:52:42 +00:00
const mdxmSurfHierarchy_t *surfInfo = (mdxmSurfHierarchy_t *)((byte *)surfIndexes + surfIndexes->offsets[surf->thisSurfaceIndex]);
return surfInfo->parentIndex;
}
2013-04-04 18:02:27 +00:00
int G2_GetSurfaceIndex(CGhoul2Info *ghlInfo, const char *surfaceName)
2013-04-04 14:52:42 +00:00
{
int flags;
2013-04-04 18:02:27 +00:00
assert(ghlInfo->currentModel);
return G2_IsSurfaceLegal(ghlInfo->currentModel, surfaceName, &flags);
2013-04-04 14:52:42 +00:00
}
2013-04-04 18:02:27 +00:00
int G2_IsSurfaceRendered(CGhoul2Info *ghlInfo, const char *surfaceName, surfaceInfo_v &slist)
2013-04-04 14:52:42 +00:00
{
int surfIndex = -1;
const mdxmSurface_t *parentSurf = 0;
int flags = 0, surfFlags = 0;
int parentFlags = 0;
int parentSurfNum = 0;
2013-04-04 18:02:27 +00:00
assert(ghlInfo->currentModel);
assert(ghlInfo->currentModel->mdxm);
if (!ghlInfo->currentModel->mdxm)
2013-04-04 14:52:42 +00:00
{
return 0;
}
2013-04-04 18:02:27 +00:00
const mdxmHierarchyOffsets_t *surfIndexes = (mdxmHierarchyOffsets_t *)((byte *)ghlInfo->currentModel->mdxm + sizeof(mdxmHeader_t));
const mdxmSurfHierarchy_t *parentSurfInfo = 0;
2013-04-04 14:52:42 +00:00
// now see if we already have overriden this surface in the slist
2013-04-04 18:02:27 +00:00
const mdxmSurface_t *surf = G2_FindSurface(ghlInfo, slist, surfaceName, &surfIndex);
2013-04-04 14:52:42 +00:00
if (surf)
{
// set descendants value
surfFlags = slist[surfIndex].offFlags;
}
// ok, at this point in flags we have what this surface is set to, and the index of the surface itself
// now travel up the skeleton to see if any of it's ancestors have a 'no descendants' turned on
// find the original surface in the surface list
2013-04-04 18:02:27 +00:00
int surfNum = G2_IsSurfaceLegal(ghlInfo->currentModel, surfaceName, &flags);
2013-04-04 14:52:42 +00:00
if ( surfNum != -1 )
{//must be legal
const mdxmSurfHierarchy_t *surfInfo = (mdxmSurfHierarchy_t *)((byte *)surfIndexes + surfIndexes->offsets[surfNum]);
surfNum = surfInfo->parentIndex;
// walk the surface hierarchy up until we hit the root
while (surfNum != -1)
{
parentSurfInfo = (mdxmSurfHierarchy_t *)((byte *)surfIndexes + surfIndexes->offsets[surfNum]);
// find the original surface in the surface list
2013-04-04 18:02:27 +00:00
parentSurfNum = G2_IsSurfaceLegal(ghlInfo->currentModel, surfaceName, &parentFlags);
2013-04-04 14:52:42 +00:00
// now see if we already have overriden this surface in the slist
2013-04-04 18:02:27 +00:00
parentSurf = G2_FindSurface(ghlInfo, slist, parentSurfInfo->name, &surfIndex);
2013-04-04 14:52:42 +00:00
if (parentSurf)
{
// set descendants value
parentFlags = slist[surfIndex].offFlags;
}
// now we have the parent flags, lets see if any have the 'no descendants' flag set
if (parentFlags & G2SURFACEFLAG_NODESCENDANTS)
{
2013-04-04 18:02:27 +00:00
flags |= G2SURFACEFLAG_OFF;
2013-04-04 14:52:42 +00:00
}
// set up scan of next parent
surfNum = parentSurfInfo->parentIndex;
}
}
if ( flags == 0 )
{//it's not being overridden by a parent
flags = surfFlags;//use whatever flags it had on itself
}
return flags;
}