Code-style consistency improvement:

Apply clang-format-all.sh using the _clang-format file through all the cpp/.h files.
make sure not to apply it to certain serialization structures, since some parser expects the * as part of the name, instead of type.
This commit contains no other changes aside from adding and applying clang-format-all.sh
This commit is contained in:
erwincoumans
2018-09-23 14:17:31 -07:00
parent b73b05e9fb
commit ab8f16961e
1773 changed files with 1081087 additions and 474249 deletions

View File

@@ -20,60 +20,55 @@ subject to the following restrictions:
#include <stdio.h>
#include <string.h>
void BspConverter::convertBsp(BspLoader& bspLoader,float scaling)
void BspConverter::convertBsp(BspLoader& bspLoader, float scaling)
{
{
float playstartf[3] = {0,0,100};
float playstartf[3] = {0, 0, 100};
if (bspLoader.findVectorByName(&playstartf[0],"info_player_start"))
if (bspLoader.findVectorByName(&playstartf[0], "info_player_start"))
{
printf("found playerstart\n");
}
}
else
{
if (bspLoader.findVectorByName(&playstartf[0],"info_player_deathmatch"))
if (bspLoader.findVectorByName(&playstartf[0], "info_player_deathmatch"))
{
printf("found deatchmatch start\n");
}
}
btVector3 playerStart (playstartf[0],playstartf[1],playstartf[2]);
btVector3 playerStart(playstartf[0], playstartf[1], playstartf[2]);
playerStart[2] += 20.f; //start a bit higher
playerStart[2] += 20.f; //start a bit higher
playerStart *= scaling;
//progressBegin("Loading bsp");
for (int i=0;i<bspLoader.m_numleafs;i++)
for (int i = 0; i < bspLoader.m_numleafs; i++)
{
printf("Reading bspLeaf %i from total %i (%f procent)\n",i, bspLoader.m_numleafs,(100.f*(float)i/float(bspLoader.m_numleafs)) );
printf("Reading bspLeaf %i from total %i (%f procent)\n", i, bspLoader.m_numleafs, (100.f * (float)i / float(bspLoader.m_numleafs)));
bool isValidBrush = false;
BSPLeaf& leaf = bspLoader.m_dleafs[i];
for (int b=0;b<leaf.numLeafBrushes;b++)
BSPLeaf& leaf = bspLoader.m_dleafs[i];
for (int b = 0; b < leaf.numLeafBrushes; b++)
{
btAlignedObjectArray<btVector3> planeEquations;
int brushid = bspLoader.m_dleafbrushes[leaf.firstLeafBrush+b];
int brushid = bspLoader.m_dleafbrushes[leaf.firstLeafBrush + b];
BSPBrush& brush = bspLoader.m_dbrushes[brushid];
if (brush.shaderNum!=-1)
if (brush.shaderNum != -1)
{
if (bspLoader.m_dshaders[ brush.shaderNum ].contentFlags & BSPCONTENTS_SOLID)
if (bspLoader.m_dshaders[brush.shaderNum].contentFlags & BSPCONTENTS_SOLID)
{
brush.shaderNum = -1;
for (int p=0;p<brush.numSides;p++)
for (int p = 0; p < brush.numSides; p++)
{
int sideid = brush.firstSide+p;
int sideid = brush.firstSide + p;
BSPBrushSide& brushside = bspLoader.m_dbrushsides[sideid];
int planeid = brushside.planeNum;
BSPPlane& plane = bspLoader.m_dplanes[planeid];
@@ -82,45 +77,44 @@ void BspConverter::convertBsp(BspLoader& bspLoader,float scaling)
plane.normal[0],
plane.normal[1],
plane.normal[2]);
planeEq[3] = scaling*-plane.dist;
planeEq[3] = scaling * -plane.dist;
planeEquations.push_back(planeEq);
isValidBrush=true;
isValidBrush = true;
}
if (isValidBrush)
{
btAlignedObjectArray<btVector3> vertices;
btGeometryUtil::getVerticesFromPlaneEquations(planeEquations, vertices);
btAlignedObjectArray<btVector3> vertices;
btGeometryUtil::getVerticesFromPlaneEquations(planeEquations,vertices);
bool isEntity = false;
btVector3 entityTarget(0.f,0.f,0.f);
addConvexVerticesCollider(vertices,isEntity,entityTarget);
btVector3 entityTarget(0.f, 0.f, 0.f);
addConvexVerticesCollider(vertices, isEntity, entityTarget);
}
}
}
}
}
}
#define USE_ENTITIES
#ifdef USE_ENTITIES
{
int i;
for (i=0;i<bspLoader.m_num_entities;i++)
for (i = 0; i < bspLoader.m_num_entities; i++)
{
const BSPEntity& entity = bspLoader.m_entities[i];
const char* cl = bspLoader.getValueForKey(&entity, "classname");
if (!strcmp(cl, "trigger_push"))
{
const BSPEntity& entity = bspLoader.m_entities[i];
const char* cl = bspLoader.getValueForKey(&entity,"classname");
if ( !strcmp( cl, "trigger_push" ) ) {
btVector3 targetLocation(0.f,0.f,0.f);
btVector3 targetLocation(0.f, 0.f, 0.f);
cl = bspLoader.getValueForKey(&entity,"target");
if ( strcmp( cl, "" ) ) {
//its not empty so ...
cl = bspLoader.getValueForKey(&entity, "target");
if (strcmp(cl, ""))
{
//its not empty so ...
/*
/*
//lookup the target position for the jumppad:
const BSPEntity* targetentity = bspLoader.getEntityByValue( "targetname" , cl );
if (targetentity)
@@ -132,76 +126,63 @@ void BspConverter::convertBsp(BspLoader& bspLoader,float scaling)
}
*/
cl = bspLoader.getValueForKey(&entity,"model");
if ( strcmp( cl, "" ) ) {
// add the model as a brush
if (cl[0] == '*')
cl = bspLoader.getValueForKey(&entity, "model");
if (strcmp(cl, ""))
{
// add the model as a brush
if (cl[0] == '*')
{
int modelnr = atoi(&cl[1]);
if ((modelnr >= 0) && (modelnr < bspLoader.m_nummodels))
{
int modelnr = atoi(&cl[1]);
if ((modelnr >=0) && (modelnr < bspLoader.m_nummodels))
const BSPModel& model = bspLoader.m_dmodels[modelnr];
for (int n = 0; n < model.numBrushes; n++)
{
const BSPModel& model = bspLoader.m_dmodels[modelnr];
for (int n=0;n<model.numBrushes;n++)
btAlignedObjectArray<btVector3> planeEquations;
bool isValidBrush = false;
//convert brush
const BSPBrush& brush = bspLoader.m_dbrushes[model.firstBrush + n];
{
btAlignedObjectArray<btVector3> planeEquations;
bool isValidBrush = false;
//convert brush
const BSPBrush& brush = bspLoader.m_dbrushes[model.firstBrush+n];
for (int p = 0; p < brush.numSides; p++)
{
for (int p=0;p<brush.numSides;p++)
{
int sideid = brush.firstSide+p;
BSPBrushSide& brushside = bspLoader.m_dbrushsides[sideid];
int planeid = brushside.planeNum;
BSPPlane& plane = bspLoader.m_dplanes[planeid];
btVector3 planeEq;
planeEq.setValue(
plane.normal[0],
plane.normal[1],
plane.normal[2]);
planeEq[3] = scaling*-plane.dist;
planeEquations.push_back(planeEq);
isValidBrush=true;
}
if (isValidBrush)
{
btAlignedObjectArray<btVector3> vertices;
btGeometryUtil::getVerticesFromPlaneEquations(planeEquations,vertices);
bool isEntity=true;
addConvexVerticesCollider(vertices,isEntity,targetLocation);
}
int sideid = brush.firstSide + p;
BSPBrushSide& brushside = bspLoader.m_dbrushsides[sideid];
int planeid = brushside.planeNum;
BSPPlane& plane = bspLoader.m_dplanes[planeid];
btVector3 planeEq;
planeEq.setValue(
plane.normal[0],
plane.normal[1],
plane.normal[2]);
planeEq[3] = scaling * -plane.dist;
planeEquations.push_back(planeEq);
isValidBrush = true;
}
if (isValidBrush)
{
btAlignedObjectArray<btVector3> vertices;
btGeometryUtil::getVerticesFromPlaneEquations(planeEquations, vertices);
bool isEntity = true;
addConvexVerticesCollider(vertices, isEntity, targetLocation);
}
}
}
}
else
{
printf("unsupported trigger_push model, md3 ?\n");
}
}
else
{
printf("unsupported trigger_push model, md3 ?\n");
}
}
}
}
}
#endif //USE_ENTITIES
}
#endif //USE_ENTITIES
//progressEnd();
}
}
}

View File

@@ -23,17 +23,14 @@ class BspLoader;
///BspConverter turns a loaded bsp level into convex parts (vertices)
class BspConverter
{
public:
void convertBsp(BspLoader& bspLoader,float scaling);
virtual ~BspConverter()
{
}
///this callback is called for each brush that succesfully converted into vertices
virtual void addConvexVerticesCollider(btAlignedObjectArray<btVector3>& vertices, bool isEntity, const btVector3& entityTargetLocation) = 0;
public:
void convertBsp(BspLoader& bspLoader, float scaling);
virtual ~BspConverter()
{
}
///this callback is called for each brush that succesfully converted into vertices
virtual void addConvexVerticesCollider(btAlignedObjectArray<btVector3>& vertices, bool isEntity, const btVector3& entityTargetLocation) = 0;
};
#endif //BSP_CONVERTER_H
#endif //BSP_CONVERTER_H

View File

@@ -20,26 +20,25 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
===========================================================================
*/
#include "BspLoader.h"
#include <stdio.h>
#include <string.h>
typedef struct
{
char filename[1024];
char *buffer,*script_p,*end_p;
int line;
char filename[1024];
char *buffer, *script_p, *end_p;
int line;
} BSPScript;
#define MAX_INCLUDES 8
BSPScript scriptstack[MAX_INCLUDES];
BSPScript *script;
int scriptline;
#define MAX_INCLUDES 8
BSPScript scriptstack[MAX_INCLUDES];
BSPScript *script;
int scriptline;
char token[BSPMAXTOKEN];
char token[BSPMAXTOKEN];
bool endofscript;
bool tokenready; // only true if UnGetToken was just called
bool tokenready; // only true if UnGetToken was just called
//
//loadBSPFile
@@ -48,143 +47,137 @@ bool tokenready; // only true if UnGetToken was just called
int extrasize = 100;
BspLoader::BspLoader()
:m_num_entities(0)
: m_num_entities(0)
{
m_Endianness = getMachineEndianness();
if (m_Endianness == BSP_BIG_ENDIAN)
{
printf("Machine is BIG_ENDIAN\n");
} else
}
else
{
printf("Machine is Little Endian\n");
}
}
bool BspLoader::loadBSPFile( void* memoryBuffer) {
BSPHeader *header = (BSPHeader*) memoryBuffer;
bool BspLoader::loadBSPFile(void *memoryBuffer)
{
BSPHeader *header = (BSPHeader *)memoryBuffer;
// load the file header
if (header)
{
// swap the header
swapBlock( (int *)header, sizeof(*header) );
swapBlock((int *)header, sizeof(*header));
int length = (header->lumps[BSPLUMP_SHADERS].filelen) / sizeof(BSPShader);
m_dshaders.resize(length+extrasize);
m_numShaders = copyLump( header, BSPLUMP_SHADERS, &m_dshaders[0], sizeof(BSPShader) );
m_dshaders.resize(length + extrasize);
m_numShaders = copyLump(header, BSPLUMP_SHADERS, &m_dshaders[0], sizeof(BSPShader));
length = (header->lumps[LUMP_MODELS].filelen) / sizeof(BSPModel);
m_dmodels.resize(length+extrasize);
m_nummodels = copyLump( header, LUMP_MODELS, &m_dmodels[0], sizeof(BSPModel) );
m_dmodels.resize(length + extrasize);
m_nummodels = copyLump(header, LUMP_MODELS, &m_dmodels[0], sizeof(BSPModel));
length = (header->lumps[BSPLUMP_PLANES].filelen) / sizeof(BSPPlane);
m_dplanes.resize(length+extrasize);
m_numplanes = copyLump( header, BSPLUMP_PLANES, &m_dplanes[0], sizeof(BSPPlane) );
m_dplanes.resize(length + extrasize);
m_numplanes = copyLump(header, BSPLUMP_PLANES, &m_dplanes[0], sizeof(BSPPlane));
length = (header->lumps[BSPLUMP_LEAFS].filelen) / sizeof(BSPLeaf);
m_dleafs.resize(length+extrasize);
m_numleafs = copyLump( header, BSPLUMP_LEAFS, &m_dleafs[0], sizeof(BSPLeaf) );
m_dleafs.resize(length + extrasize);
m_numleafs = copyLump(header, BSPLUMP_LEAFS, &m_dleafs[0], sizeof(BSPLeaf));
length = (header->lumps[BSPLUMP_NODES].filelen) / sizeof(BSPNode);
m_dnodes.resize(length+extrasize);
m_numnodes = copyLump( header, BSPLUMP_NODES, &m_dnodes[0], sizeof(BSPNode) );
m_dnodes.resize(length + extrasize);
m_numnodes = copyLump(header, BSPLUMP_NODES, &m_dnodes[0], sizeof(BSPNode));
length = (header->lumps[BSPLUMP_LEAFSURFACES].filelen) / sizeof(m_dleafsurfaces[0]);
m_dleafsurfaces.resize(length+extrasize);
m_numleafsurfaces = copyLump( header, BSPLUMP_LEAFSURFACES, &m_dleafsurfaces[0], sizeof(m_dleafsurfaces[0]) );
m_dleafsurfaces.resize(length + extrasize);
m_numleafsurfaces = copyLump(header, BSPLUMP_LEAFSURFACES, &m_dleafsurfaces[0], sizeof(m_dleafsurfaces[0]));
length = (header->lumps[BSPLUMP_LEAFBRUSHES].filelen) / sizeof(m_dleafbrushes[0]) ;
m_dleafbrushes.resize(length+extrasize);
m_numleafbrushes = copyLump( header, BSPLUMP_LEAFBRUSHES, &m_dleafbrushes[0], sizeof(m_dleafbrushes[0]) );
length = (header->lumps[BSPLUMP_LEAFBRUSHES].filelen) / sizeof(m_dleafbrushes[0]);
m_dleafbrushes.resize(length + extrasize);
m_numleafbrushes = copyLump(header, BSPLUMP_LEAFBRUSHES, &m_dleafbrushes[0], sizeof(m_dleafbrushes[0]));
length = (header->lumps[LUMP_BRUSHES].filelen) / sizeof(BSPBrush);
m_dbrushes.resize(length+extrasize);
m_numbrushes = copyLump( header, LUMP_BRUSHES, &m_dbrushes[0], sizeof(BSPBrush) );
m_dbrushes.resize(length + extrasize);
m_numbrushes = copyLump(header, LUMP_BRUSHES, &m_dbrushes[0], sizeof(BSPBrush));
length = (header->lumps[LUMP_BRUSHSIDES].filelen) / sizeof(BSPBrushSide);
m_dbrushsides.resize(length+extrasize);
m_numbrushsides = copyLump( header, LUMP_BRUSHSIDES, &m_dbrushsides[0], sizeof(BSPBrushSide) );
m_dbrushsides.resize(length + extrasize);
m_numbrushsides = copyLump(header, LUMP_BRUSHSIDES, &m_dbrushsides[0], sizeof(BSPBrushSide));
length = (header->lumps[LUMP_SURFACES].filelen) / sizeof(BSPSurface);
m_drawSurfaces.resize(length+extrasize);
m_numDrawSurfaces = copyLump( header, LUMP_SURFACES, &m_drawSurfaces[0], sizeof(BSPSurface) );
m_drawSurfaces.resize(length + extrasize);
m_numDrawSurfaces = copyLump(header, LUMP_SURFACES, &m_drawSurfaces[0], sizeof(BSPSurface));
length = (header->lumps[LUMP_DRAWINDEXES].filelen) / sizeof(m_drawIndexes[0]);
m_drawIndexes.resize(length+extrasize);
m_numDrawIndexes = copyLump( header, LUMP_DRAWINDEXES, &m_drawIndexes[0], sizeof(m_drawIndexes[0]) );
m_drawIndexes.resize(length + extrasize);
m_numDrawIndexes = copyLump(header, LUMP_DRAWINDEXES, &m_drawIndexes[0], sizeof(m_drawIndexes[0]));
length = (header->lumps[LUMP_VISIBILITY].filelen) / 1;
m_visBytes.resize(length+extrasize);
m_numVisBytes = copyLump( header, LUMP_VISIBILITY, &m_visBytes[0], 1 );
m_visBytes.resize(length + extrasize);
m_numVisBytes = copyLump(header, LUMP_VISIBILITY, &m_visBytes[0], 1);
length = (header->lumps[LUMP_LIGHTMAPS].filelen) / 1;
m_lightBytes.resize(length+extrasize);
m_numLightBytes = copyLump( header, LUMP_LIGHTMAPS, &m_lightBytes[0], 1 );
m_lightBytes.resize(length + extrasize);
m_numLightBytes = copyLump(header, LUMP_LIGHTMAPS, &m_lightBytes[0], 1);
length = (header->lumps[BSPLUMP_ENTITIES].filelen) / 1;
m_dentdata.resize(length+extrasize);
m_entdatasize = copyLump( header, BSPLUMP_ENTITIES, &m_dentdata[0], 1);
m_dentdata.resize(length + extrasize);
m_entdatasize = copyLump(header, BSPLUMP_ENTITIES, &m_dentdata[0], 1);
length = (header->lumps[LUMP_LIGHTGRID].filelen) / 1;
m_gridData.resize(length+extrasize);
m_numGridPoints = copyLump( header, LUMP_LIGHTGRID, &m_gridData[0], 8 );
m_gridData.resize(length + extrasize);
m_numGridPoints = copyLump(header, LUMP_LIGHTGRID, &m_gridData[0], 8);
// swap everything
swapBSPFile();
return true;
}
return false;
}
const char *BspLoader::getValueForKey(const BSPEntity *ent, const char *key) const
{
const BSPKeyValuePair *ep;
const char* BspLoader::getValueForKey( const BSPEntity* ent, const char* key ) const {
const BSPKeyValuePair* ep;
for (ep=ent->epairs ; ep ; ep=ep->next) {
if (!strcmp(ep->key, key) ) {
for (ep = ent->epairs; ep; ep = ep->next)
{
if (!strcmp(ep->key, key))
{
return ep->value;
}
}
return "";
}
float BspLoader::getFloatForKey( const BSPEntity *ent, const char *key ) {
const char *k;
k = getValueForKey( ent, key );
float BspLoader::getFloatForKey(const BSPEntity *ent, const char *key)
{
const char *k;
k = getValueForKey(ent, key);
return float(atof(k));
}
bool BspLoader::getVectorForKey( const BSPEntity *ent, const char *key, BSPVector3 vec ) {
const char *k;
k = getValueForKey (ent, key);
bool BspLoader::getVectorForKey(const BSPEntity *ent, const char *key, BSPVector3 vec)
{
const char *k;
k = getValueForKey(ent, key);
if (strcmp(k, ""))
{
sscanf (k, "%f %f %f", &vec[0], &vec[1], &vec[2]);
sscanf(k, "%f %f %f", &vec[0], &vec[1], &vec[2]);
return true;
}
return false;
}
/*
==============
parseFromMemory
==============
*/
void BspLoader::parseFromMemory (char *buffer, int size)
void BspLoader::parseFromMemory(char *buffer, int size)
{
script = scriptstack;
script++;
@@ -192,7 +185,7 @@ void BspLoader::parseFromMemory (char *buffer, int size)
{
//printf("script file exceeded MAX_INCLUDES");
}
strcpy (script->filename, "memory buffer" );
strcpy(script->filename, "memory buffer");
script->buffer = buffer;
script->line = 1;
@@ -203,20 +196,19 @@ void BspLoader::parseFromMemory (char *buffer, int size)
tokenready = false;
}
bool BspLoader::isEndOfScript (bool crossline)
bool BspLoader::isEndOfScript(bool crossline)
{
if (!crossline)
//printf("Line %i is incomplete\n",scriptline);
if (!strcmp (script->filename, "memory buffer"))
{
endofscript = true;
return false;
}
if (!strcmp(script->filename, "memory buffer"))
{
endofscript = true;
return false;
}
//free (script->buffer);
if (script == scriptstack+1)
if (script == scriptstack + 1)
{
endofscript = true;
return false;
@@ -224,7 +216,7 @@ bool BspLoader::isEndOfScript (bool crossline)
script--;
scriptline = script->line;
//printf ("returning to %s\n", script->filename);
return getToken (crossline);
return getToken(crossline);
}
/*
@@ -233,18 +225,18 @@ bool BspLoader::isEndOfScript (bool crossline)
getToken
==============
*/
bool BspLoader::getToken (bool crossline)
bool BspLoader::getToken(bool crossline)
{
char *token_p;
char *token_p;
if (tokenready) // is a token allready waiting?
if (tokenready) // is a token allready waiting?
{
tokenready = false;
return true;
}
if (script->script_p >= script->end_p)
return isEndOfScript (crossline);
return isEndOfScript(crossline);
//
// skip space
@@ -253,7 +245,7 @@ skipspace:
while (*script->script_p <= 32)
{
if (script->script_p >= script->end_p)
return isEndOfScript (crossline);
return isEndOfScript(crossline);
if (*script->script_p++ == '\n')
{
if (!crossline)
@@ -265,11 +257,10 @@ skipspace:
}
if (script->script_p >= script->end_p)
return isEndOfScript (crossline);
return isEndOfScript(crossline);
// ; # // comments
if (*script->script_p == ';' || *script->script_p == '#'
|| ( script->script_p[0] == '/' && script->script_p[1] == '/') )
if (*script->script_p == ';' || *script->script_p == '#' || (script->script_p[0] == '/' && script->script_p[1] == '/'))
{
if (!crossline)
{
@@ -277,7 +268,7 @@ skipspace:
}
while (*script->script_p++ != '\n')
if (script->script_p >= script->end_p)
return isEndOfScript (crossline);
return isEndOfScript(crossline);
scriptline = script->line++;
goto skipspace;
}
@@ -289,23 +280,24 @@ skipspace:
{
//printf("Line %i is incomplete\n",scriptline);
}
script->script_p+=2;
script->script_p += 2;
while (script->script_p[0] != '*' && script->script_p[1] != '/')
{
if ( *script->script_p == '\n' ) {
if (*script->script_p == '\n')
{
scriptline = script->line++;
}
script->script_p++;
if (script->script_p >= script->end_p)
return isEndOfScript (crossline);
return isEndOfScript(crossline);
}
script->script_p += 2;
goto skipspace;
}
//
// copy token
//
//
// copy token
//
token_p = token;
if (*script->script_p == '"')
@@ -324,25 +316,25 @@ skipspace:
}
script->script_p++;
}
else // regular token
while ( *script->script_p > 32 && *script->script_p != ';')
{
*token_p++ = *script->script_p++;
if (script->script_p == script->end_p)
break;
if (token_p == &token[BSPMAXTOKEN])
else // regular token
while (*script->script_p > 32 && *script->script_p != ';')
{
//printf ("Token too large on line %i\n",scriptline);
*token_p++ = *script->script_p++;
if (script->script_p == script->end_p)
break;
if (token_p == &token[BSPMAXTOKEN])
{
//printf ("Token too large on line %i\n",scriptline);
}
}
}
*token_p = 0;
if (!strcmp (token, "$include"))
if (!strcmp(token, "$include"))
{
//getToken (false);
//AddScriptToStack (token);
return false;//getToken (crossline);
return false; //getToken (crossline);
}
return true;
@@ -350,16 +342,17 @@ skipspace:
char *BspLoader::copystring(const char *s)
{
char *b;
b = (char*) malloc( strlen(s)+1);
strcpy (b, s);
char *b;
b = (char *)malloc(strlen(s) + 1);
strcpy(b, s);
return b;
}
void BspLoader::stripTrailing( char *e ) {
char *s;
void BspLoader::stripTrailing(char *e)
{
char *s;
s = e + strlen(e)-1;
s = e + strlen(e) - 1;
while (s >= e && *s <= 32)
{
*s = 0;
@@ -371,47 +364,50 @@ void BspLoader::stripTrailing( char *e ) {
parseEpair
=================
*/
BSPKeyValuePair *BspLoader::parseEpair( void ) {
BSPKeyValuePair *e;
BSPKeyValuePair *BspLoader::parseEpair(void)
{
BSPKeyValuePair *e;
e = (struct BSPPair*) malloc( sizeof(BSPKeyValuePair));
memset( e, 0, sizeof(BSPKeyValuePair) );
if ( strlen(token) >= BSPMAX_KEY-1 ) {
e = (struct BSPPair *)malloc(sizeof(BSPKeyValuePair));
memset(e, 0, sizeof(BSPKeyValuePair));
if (strlen(token) >= BSPMAX_KEY - 1)
{
//printf ("ParseEpar: token too long");
}
e->key = copystring( token );
getToken( false );
if ( strlen(token) >= BSPMAX_VALUE-1 ) {
e->key = copystring(token);
getToken(false);
if (strlen(token) >= BSPMAX_VALUE - 1)
{
//printf ("ParseEpar: token too long");
}
e->value = copystring( token );
e->value = copystring(token);
// strip trailing spaces that sometimes get accidentally
// added in the editor
stripTrailing( e->key );
stripTrailing( e->value );
stripTrailing(e->key);
stripTrailing(e->value);
return e;
}
/*
================
parseEntity
================
*/
bool BspLoader::parseEntity( void ) {
BSPKeyValuePair *e;
BSPEntity *mapent;
bool BspLoader::parseEntity(void)
{
BSPKeyValuePair *e;
BSPEntity *mapent;
if ( !getToken (true) ) {
if (!getToken(true))
{
return false;
}
if ( strcmp (token, "{") ) {
if (strcmp(token, "{"))
{
//printf ("parseEntity: { not found");
}
@@ -425,21 +421,24 @@ bool BspLoader::parseEntity( void ) {
bla.patches = 0;
m_entities.push_back(bla);
mapent = &m_entities[m_entities.size()-1];
mapent = &m_entities[m_entities.size() - 1];
m_num_entities++;
do {
if ( !getToken (true) ) {
do
{
if (!getToken(true))
{
//printf("parseEntity: EOF without closing brace");
}
if ( !strcmp (token, "}") ) {
if (!strcmp(token, "}"))
{
break;
}
e = (struct BSPPair*)parseEpair ();
e = (struct BSPPair *)parseEpair();
e->next = mapent->epairs;
mapent->epairs = e;
} while (1);
return true;
}
@@ -450,113 +449,108 @@ parseEntities
Parses the dentdata string into entities
================
*/
void BspLoader::parseEntities( void ) {
void BspLoader::parseEntities(void)
{
m_num_entities = 0;
m_entities.clear();
parseFromMemory( &m_dentdata[0], m_entdatasize );
parseFromMemory(&m_dentdata[0], m_entdatasize);
while ( parseEntity () ) {
}
while (parseEntity())
{
}
}
int BspLoader::getMachineEndianness()
{
long int i = 1;
const char *p = (const char *) &i;
if (p[0] == 1) // Lowest address contains the least significant byte
return BSP_LITTLE_ENDIAN;
else
return BSP_BIG_ENDIAN;
long int i = 1;
const char *p = (const char *)&i;
if (p[0] == 1) // Lowest address contains the least significant byte
return BSP_LITTLE_ENDIAN;
else
return BSP_BIG_ENDIAN;
}
short BspLoader::isLittleShort (short l)
short BspLoader::isLittleShort(short l)
{
if (machineEndianness() == BSP_BIG_ENDIAN)
{
unsigned char b1,b2;
unsigned char b1, b2;
b1 = l&255;
b2 = (l>>8)&255;
b1 = l & 255;
b2 = (l >> 8) & 255;
return (b1<<8) + b2;
return (b1 << 8) + b2;
}
//little endian
return l;
}
short BspLoader::isBigShort (short l)
short BspLoader::isBigShort(short l)
{
if (machineEndianness() == BSP_BIG_ENDIAN)
{
return l;
}
unsigned char b1,b2;
b1 = l&255;
b2 = (l>>8)&255;
return (b1<<8) + b2;
unsigned char b1, b2;
b1 = l & 255;
b2 = (l >> 8) & 255;
return (b1 << 8) + b2;
}
int BspLoader::isLittleLong (int l)
int BspLoader::isLittleLong(int l)
{
if (machineEndianness() == BSP_BIG_ENDIAN)
{
unsigned char b1,b2,b3,b4;
unsigned char b1, b2, b3, b4;
b1 = l&255;
b2 = (l>>8)&255;
b3 = (l>>16)&255;
b4 = (l>>24)&255;
b1 = l & 255;
b2 = (l >> 8) & 255;
b3 = (l >> 16) & 255;
b4 = (l >> 24) & 255;
return ((int)b1<<24) + ((int)b2<<16) + ((int)b3<<8) + b4;
return ((int)b1 << 24) + ((int)b2 << 16) + ((int)b3 << 8) + b4;
}
//little endian
return l;
}
int BspLoader::isBigLong (int l)
int BspLoader::isBigLong(int l)
{
if (machineEndianness() == BSP_BIG_ENDIAN)
{
return l;
}
unsigned char b1, b2, b3, b4;
unsigned char b1,b2,b3,b4;
b1 = l&255;
b2 = (l>>8)&255;
b3 = (l>>16)&255;
b4 = (l>>24)&255;
return ((int)b1<<24) + ((int)b2<<16) + ((int)b3<<8) + b4;
b1 = l & 255;
b2 = (l >> 8) & 255;
b3 = (l >> 16) & 255;
b4 = (l >> 24) & 255;
return ((int)b1 << 24) + ((int)b2 << 16) + ((int)b3 << 8) + b4;
}
float BspLoader::isLittleFloat (float l)
float BspLoader::isLittleFloat(float l)
{
if (machineEndianness() == BSP_BIG_ENDIAN)
{
union {unsigned char b[4]; float f;} in, out;
union {
unsigned char b[4];
float f;
} in, out;
in.f = l;
out.b[0] = in.b[3];
out.b[1] = in.b[2];
out.b[2] = in.b[1];
out.b[3] = in.b[0];
return out.f;
}
@@ -564,40 +558,40 @@ float BspLoader::isLittleFloat (float l)
return l;
}
float BspLoader::isBigFloat (float l)
float BspLoader::isBigFloat(float l)
{
if (machineEndianness() == BSP_BIG_ENDIAN)
{
return l;
}
//little endian
union {unsigned char b[4]; float f;} in, out;
union {
unsigned char b[4];
float f;
} in, out;
in.f = l;
out.b[0] = in.b[3];
out.b[1] = in.b[2];
out.b[2] = in.b[1];
out.b[3] = in.b[0];
return out.f;
}
//
// swapBlock
// If all values are 32 bits, this can be used to swap everything
//
void BspLoader::swapBlock( int *block, int sizeOfBlock ) {
int i;
void BspLoader::swapBlock(int *block, int sizeOfBlock)
{
int i;
sizeOfBlock >>= 2;
for ( i = 0 ; i < sizeOfBlock ; i++ ) {
block[i] = isLittleLong( block[i] );
for (i = 0; i < sizeOfBlock; i++)
{
block[i] = isLittleLong(block[i]);
}
}
@@ -605,99 +599,96 @@ void BspLoader::swapBlock( int *block, int sizeOfBlock ) {
// copyLump
//
int BspLoader::copyLump( BSPHeader *header, int lump, void *dest, int size ) {
int length, ofs;
int BspLoader::copyLump(BSPHeader *header, int lump, void *dest, int size)
{
int length, ofs;
length = header->lumps[lump].filelen;
ofs = header->lumps[lump].fileofs;
//if ( length % size ) {
// printf ("loadBSPFile: odd lump size");
//}
memcpy( dest, (unsigned char *)header + ofs, length );
memcpy(dest, (unsigned char *)header + ofs, length);
return length / size;
}
//
// swapBSPFile
//
void BspLoader::swapBSPFile( void ) {
int i;
// models
swapBlock( (int *) &m_dmodels[0], m_nummodels * sizeof( m_dmodels[0] ) );
void BspLoader::swapBSPFile(void)
{
int i;
// models
swapBlock((int *)&m_dmodels[0], m_nummodels * sizeof(m_dmodels[0]));
// shaders (don't swap the name)
for ( i = 0 ; i < m_numShaders ; i++ ) {
m_dshaders[i].contentFlags = isLittleLong( m_dshaders[i].contentFlags );
m_dshaders[i].surfaceFlags = isLittleLong( m_dshaders[i].surfaceFlags );
for (i = 0; i < m_numShaders; i++)
{
m_dshaders[i].contentFlags = isLittleLong(m_dshaders[i].contentFlags);
m_dshaders[i].surfaceFlags = isLittleLong(m_dshaders[i].surfaceFlags);
}
// planes
swapBlock( (int *)&m_dplanes[0], m_numplanes * sizeof( m_dplanes[0] ) );
swapBlock((int *)&m_dplanes[0], m_numplanes * sizeof(m_dplanes[0]));
// nodes
swapBlock( (int *)&m_dnodes[0], m_numnodes * sizeof( m_dnodes[0] ) );
swapBlock((int *)&m_dnodes[0], m_numnodes * sizeof(m_dnodes[0]));
// leafs
swapBlock( (int *)&m_dleafs[0], m_numleafs * sizeof( m_dleafs[0] ) );
swapBlock((int *)&m_dleafs[0], m_numleafs * sizeof(m_dleafs[0]));
// leaffaces
swapBlock( (int *)&m_dleafsurfaces[0], m_numleafsurfaces * sizeof( m_dleafsurfaces[0] ) );
swapBlock((int *)&m_dleafsurfaces[0], m_numleafsurfaces * sizeof(m_dleafsurfaces[0]));
// leafbrushes
swapBlock( (int *)&m_dleafbrushes[0], m_numleafbrushes * sizeof( m_dleafbrushes[0] ) );
swapBlock((int *)&m_dleafbrushes[0], m_numleafbrushes * sizeof(m_dleafbrushes[0]));
// brushes
swapBlock( (int *)&m_dbrushes[0], m_numbrushes * sizeof( m_dbrushes[0] ) );
swapBlock((int *)&m_dbrushes[0], m_numbrushes * sizeof(m_dbrushes[0]));
// brushsides
swapBlock( (int *)&m_dbrushsides[0], m_numbrushsides * sizeof( m_dbrushsides[0] ) );
swapBlock((int *)&m_dbrushsides[0], m_numbrushsides * sizeof(m_dbrushsides[0]));
// vis
((int *)&m_visBytes)[0] = isLittleLong( ((int *)&m_visBytes)[0] );
((int *)&m_visBytes)[1] = isLittleLong( ((int *)&m_visBytes)[1] );
((int *)&m_visBytes)[0] = isLittleLong(((int *)&m_visBytes)[0]);
((int *)&m_visBytes)[1] = isLittleLong(((int *)&m_visBytes)[1]);
// drawindexes
swapBlock( (int *)&m_drawIndexes[0], m_numDrawIndexes * sizeof( m_drawIndexes[0] ) );
swapBlock((int *)&m_drawIndexes[0], m_numDrawIndexes * sizeof(m_drawIndexes[0]));
// drawsurfs
swapBlock( (int *)&m_drawSurfaces[0], m_numDrawSurfaces * sizeof( m_drawSurfaces[0] ) );
swapBlock((int *)&m_drawSurfaces[0], m_numDrawSurfaces * sizeof(m_drawSurfaces[0]));
}
bool BspLoader::findVectorByName(float* outvec,const char* name)
bool BspLoader::findVectorByName(float *outvec, const char *name)
{
const char *cl;
BSPVector3 origin;
bool found = false;
parseEntities();
for ( int i = 1; i < m_num_entities; i++ ) {
cl = getValueForKey (&m_entities[i], "classname");
if ( !strcmp( cl, "info_player_start" ) ) {
getVectorForKey( &m_entities[i], "origin", origin );
found = true;
break;
}
if ( !strcmp( cl, "info_player_deathmatch" ) ) {
getVectorForKey( &m_entities[i], "origin", origin );
found = true;
break;
}
for (int i = 1; i < m_num_entities; i++)
{
cl = getValueForKey(&m_entities[i], "classname");
if (!strcmp(cl, "info_player_start"))
{
getVectorForKey(&m_entities[i], "origin", origin);
found = true;
break;
}
if (!strcmp(cl, "info_player_deathmatch"))
{
getVectorForKey(&m_entities[i], "origin", origin);
found = true;
break;
}
}
if (found)
@@ -708,23 +699,21 @@ bool BspLoader::findVectorByName(float* outvec,const char* name)
}
return found;
}
const BSPEntity * BspLoader::getEntityByValue( const char* name, const char* value)
const BSPEntity *BspLoader::getEntityByValue(const char *name, const char *value)
{
const BSPEntity* entity = NULL;
const BSPEntity *entity = NULL;
for ( int i = 1; i < m_num_entities; i++ ) {
for (int i = 1; i < m_num_entities; i++)
{
const BSPEntity &ent = m_entities[i];
const BSPEntity& ent = m_entities[i];
const char* cl = getValueForKey (&m_entities[i], name);
if ( !strcmp( cl, value ) ) {
const char *cl = getValueForKey(&m_entities[i], name);
if (!strcmp(cl, value))
{
entity = &ent;
break;
}
}
return entity;
}

View File

@@ -20,276 +20,269 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
===========================================================================
*/
#ifndef BSP_LOADER_H
#define BSP_LOADER_H
#include "LinearMath/btAlignedObjectArray.h"
#define BSPMAXTOKEN 1024
#define BSPMAX_KEY 32
#define BSPMAX_VALUE 1024
#define BSPCONTENTS_SOLID 1
#define BSPCONTENTS_AREAPORTAL 0x8000
#define BSPLUMP_ENTITIES 0
#define BSPLUMP_SHADERS 1
#define BSPLUMP_PLANES 2
#define BSPLUMP_NODES 3
#define BSPLUMP_LEAFS 4
#define BSPLUMP_LEAFSURFACES 5
#define BSPLUMP_LEAFBRUSHES 6
#define LUMP_MODELS 7
#define LUMP_BRUSHES 8
#define LUMP_BRUSHSIDES 9
#define LUMP_DRAWVERTS 10
#define LUMP_DRAWINDEXES 11
#define LUMP_SURFACES 13
#define LUMP_LIGHTMAPS 14
#define LUMP_LIGHTGRID 15
#define LUMP_VISIBILITY 16
#define HEADER_LUMPS 17
#define MAX_QPATH 64
#define BSPMAXTOKEN 1024
#define BSPMAX_KEY 32
#define BSPMAX_VALUE 1024
#define BSPCONTENTS_SOLID 1
#define BSPCONTENTS_AREAPORTAL 0x8000
#define BSPLUMP_ENTITIES 0
#define BSPLUMP_SHADERS 1
#define BSPLUMP_PLANES 2
#define BSPLUMP_NODES 3
#define BSPLUMP_LEAFS 4
#define BSPLUMP_LEAFSURFACES 5
#define BSPLUMP_LEAFBRUSHES 6
#define LUMP_MODELS 7
#define LUMP_BRUSHES 8
#define LUMP_BRUSHSIDES 9
#define LUMP_DRAWVERTS 10
#define LUMP_DRAWINDEXES 11
#define LUMP_SURFACES 13
#define LUMP_LIGHTMAPS 14
#define LUMP_LIGHTGRID 15
#define LUMP_VISIBILITY 16
#define HEADER_LUMPS 17
#define MAX_QPATH 64
typedef struct {
int fileofs, filelen;
typedef struct
{
int fileofs, filelen;
} BSPLump;
typedef float BSPVector3[3];
typedef struct {
int ident;
int version;
BSPLump lumps[HEADER_LUMPS];
typedef struct
{
int ident;
int version;
BSPLump lumps[HEADER_LUMPS];
} BSPHeader;
typedef struct {
float mins[3], maxs[3];
int firstSurface, numSurfaces;
int firstBrush, numBrushes;
typedef struct
{
float mins[3], maxs[3];
int firstSurface, numSurfaces;
int firstBrush, numBrushes;
} BSPModel;
typedef struct {
char shader[MAX_QPATH];
int surfaceFlags;
int contentFlags;
typedef struct
{
char shader[MAX_QPATH];
int surfaceFlags;
int contentFlags;
} BSPShader;
typedef struct {
float normal[3];
float dist;
typedef struct
{
float normal[3];
float dist;
} BSPPlane;
typedef struct {
int planeNum;
int children[2];
int mins[3];
int maxs[3];
typedef struct
{
int planeNum;
int children[2];
int mins[3];
int maxs[3];
} BSPNode;
typedef struct {
int cluster;
int area;
int mins[3];
int maxs[3];
int firstLeafSurface;
int numLeafSurfaces;
int firstLeafBrush;
int numLeafBrushes;
typedef struct
{
int cluster;
int area;
int mins[3];
int maxs[3];
int firstLeafSurface;
int numLeafSurfaces;
int firstLeafBrush;
int numLeafBrushes;
} BSPLeaf;
typedef struct {
int planeNum;
int shaderNum;
typedef struct
{
int planeNum;
int shaderNum;
} BSPBrushSide;
typedef struct {
int firstSide;
int numSides;
int shaderNum;
typedef struct
{
int firstSide;
int numSides;
int shaderNum;
} BSPBrush;
typedef struct BSPPair {
struct BSPPair *next;
char *key;
char *value;
typedef struct BSPPair
{
struct BSPPair *next;
char *key;
char *value;
} BSPKeyValuePair;
typedef struct {
BSPVector3 origin;
struct bspbrush_s *brushes;
struct parseMesh_s *patches;
int firstDrawSurf;
BSPKeyValuePair *epairs;
typedef struct
{
BSPVector3 origin;
struct bspbrush_s *brushes;
struct parseMesh_s *patches;
int firstDrawSurf;
BSPKeyValuePair *epairs;
} BSPEntity;
typedef enum {
typedef enum
{
MST_BAD,
MST_PLANAR,
MST_PATCH,
MST_TRIANGLE_SOUP,
MST_FLARE
MST_PLANAR,
MST_PATCH,
MST_TRIANGLE_SOUP,
MST_FLARE
} BSPMapSurface;
typedef struct {
int shaderNum;
int fogNum;
int surfaceType;
int firstVert;
int numVerts;
int firstIndex;
int numIndexes;
int lightmapNum;
int lightmapX, lightmapY;
int lightmapWidth, lightmapHeight;
BSPVector3 lightmapOrigin;
BSPVector3 lightmapVecs[3];
int patchWidth;
int patchHeight;
typedef struct
{
int shaderNum;
int fogNum;
int surfaceType;
int firstVert;
int numVerts;
int firstIndex;
int numIndexes;
int lightmapNum;
int lightmapX, lightmapY;
int lightmapWidth, lightmapHeight;
BSPVector3 lightmapOrigin;
BSPVector3 lightmapVecs[3];
int patchWidth;
int patchHeight;
} BSPSurface;
///GPL code from IdSofware to parse a Quake 3 BSP file
///check that your platform define __BIG_ENDIAN__ correctly (in BspLoader.cpp)
class BspLoader
{
int m_Endianness;
public:
public:
BspLoader();
BspLoader();
bool loadBSPFile(void *memoryBuffer);
bool loadBSPFile( void* memoryBuffer);
const char *getValueForKey(const BSPEntity *ent, const char *key) const;
const char* getValueForKey( const BSPEntity *ent, const char *key ) const;
bool getVectorForKey(const BSPEntity *ent, const char *key, BSPVector3 vec);
bool getVectorForKey( const BSPEntity *ent, const char *key, BSPVector3 vec );
float getFloatForKey( const BSPEntity *ent, const char *key );
float getFloatForKey(const BSPEntity *ent, const char *key);
void parseEntities( void );
void parseEntities(void);
bool findVectorByName(float* outvec,const char* name);
bool findVectorByName(float *outvec, const char *name);
const BSPEntity * getEntityByValue( const char* name, const char* value);
const BSPEntity *getEntityByValue(const char *name, const char *value);
protected:
void parseFromMemory(char *buffer, int size);
protected:
bool isEndOfScript(bool crossline);
void parseFromMemory (char *buffer, int size);
bool getToken(bool crossline);
char *copystring(const char *s);
bool isEndOfScript (bool crossline);
void stripTrailing(char *e);
bool getToken (bool crossline);
BSPKeyValuePair *parseEpair(void);
char *copystring(const char *s);
void stripTrailing( char *e );
bool parseEntity(void);
BSPKeyValuePair * parseEpair( void );
short isLittleShort(short l);
int isLittleLong(int l);
float isLittleFloat(float l);
bool parseEntity( void );
int isBigLong(int l);
short isBigShort(short l);
float isBigFloat(float l);
short isLittleShort (short l);
int isLittleLong (int l);
float isLittleFloat (float l);
void swapBlock(int *block, int sizeOfBlock);
int isBigLong (int l);
short isBigShort (short l);
float isBigFloat (float l);
int copyLump(BSPHeader *header, int lump, void *dest, int size);
void swapBlock( int *block, int sizeOfBlock );
void swapBSPFile(void);
int copyLump( BSPHeader *header, int lump, void *dest, int size );
public: //easier for conversion
int m_num_entities;
btAlignedObjectArray<BSPEntity> m_entities;
void swapBSPFile( void );
int m_nummodels;
btAlignedObjectArray<BSPModel> m_dmodels;
public: //easier for conversion
int m_num_entities;
btAlignedObjectArray<BSPEntity> m_entities;
int m_nummodels;
btAlignedObjectArray<BSPModel> m_dmodels;
int m_numShaders;
btAlignedObjectArray<BSPShader> m_dshaders;
int m_numShaders;
btAlignedObjectArray<BSPShader> m_dshaders;
int m_entdatasize;
btAlignedObjectArray<char> m_dentdata;
int m_entdatasize;
btAlignedObjectArray<char> m_dentdata;
int m_numleafs;
btAlignedObjectArray<BSPLeaf> m_dleafs;
int m_numleafs;
btAlignedObjectArray<BSPLeaf> m_dleafs;
int m_numplanes;
btAlignedObjectArray<BSPPlane> m_dplanes;
int m_numplanes;
btAlignedObjectArray<BSPPlane> m_dplanes;
int m_numnodes;
btAlignedObjectArray<BSPNode> m_dnodes;
int m_numnodes;
btAlignedObjectArray<BSPNode> m_dnodes;
int m_numleafsurfaces;
btAlignedObjectArray<int> m_dleafsurfaces;
int m_numleafsurfaces;
btAlignedObjectArray<int> m_dleafsurfaces;
int m_numleafbrushes;
btAlignedObjectArray<int> m_dleafbrushes;
int m_numleafbrushes;
btAlignedObjectArray<int> m_dleafbrushes;
int m_numbrushes;
btAlignedObjectArray<BSPBrush> m_dbrushes;
int m_numbrushes;
btAlignedObjectArray<BSPBrush> m_dbrushes;
int m_numbrushsides;
btAlignedObjectArray<BSPBrushSide> m_dbrushsides;
int m_numbrushsides;
btAlignedObjectArray<BSPBrushSide> m_dbrushsides;
int m_numLightBytes;
btAlignedObjectArray<unsigned char> m_lightBytes;
int m_numLightBytes;
btAlignedObjectArray<unsigned char> m_lightBytes;
int m_numGridPoints;
btAlignedObjectArray<unsigned char> m_gridData;
int m_numGridPoints;
btAlignedObjectArray<unsigned char> m_gridData;
int m_numVisBytes;
btAlignedObjectArray<unsigned char> m_visBytes;
int m_numVisBytes;
btAlignedObjectArray<unsigned char> m_visBytes;
int m_numDrawIndexes;
btAlignedObjectArray<int> m_drawIndexes;
int m_numDrawIndexes;
btAlignedObjectArray<int> m_drawIndexes;
int m_numDrawSurfaces;
btAlignedObjectArray<BSPSurface> m_drawSurfaces;
int m_numDrawSurfaces;
btAlignedObjectArray<BSPSurface> m_drawSurfaces;
enum
{
BSP_LITTLE_ENDIAN = 0,
BSP_BIG_ENDIAN = 1
};
enum
{
BSP_LITTLE_ENDIAN = 0,
BSP_BIG_ENDIAN = 1
};
//returns machines big endian / little endian
//
int getMachineEndianness();
inline int machineEndianness()
{
return m_Endianness;
}
//returns machines big endian / little endian
//
int getMachineEndianness();
inline int machineEndianness()
{
return m_Endianness;
}
};
#endif //BSP_LOADER_H
#endif //BSP_LOADER_H

View File

@@ -19,198 +19,163 @@ subject to the following restrictions:
#include "LinearMath/btQuickprof.h"
#define QUAKE_BSP_IMPORTING 1
#ifdef QUAKE_BSP_IMPORTING
#include "BspLoader.h"
#include "BspConverter.h"
#endif //QUAKE_BSP_IMPORTING
#include <stdio.h> //printf debugging
#endif //QUAKE_BSP_IMPORTING
#include <stdio.h> //printf debugging
#include "LinearMath/btAlignedObjectArray.h"
#include "../CommonInterfaces/CommonRigidBodyBase.h"
///BspDemo shows the convex collision detection, by converting a Quake BSP file into convex objects and allowing interaction with boxes.
class BspDemo : public CommonRigidBodyBase
{
public:
public:
//keep the collision shapes, for deletion/cleanup
BspDemo(struct GUIHelperInterface* helper)
:CommonRigidBodyBase(helper)
: CommonRigidBodyBase(helper)
{
}
virtual ~BspDemo();
virtual void initPhysics();
virtual void initPhysics();
void initPhysics(const char* bspfilename);
void initPhysics(const char* bspfilename);
virtual void resetCamera()
{
float dist = 43;
float pitch = -12;
float yaw = -175;
float targetPos[3]={4,-25,-6};
m_guiHelper->resetCamera(dist,yaw,pitch,targetPos[0],targetPos[1],targetPos[2]);
float targetPos[3] = {4, -25, -6};
m_guiHelper->resetCamera(dist, yaw, pitch, targetPos[0], targetPos[1], targetPos[2]);
}
};
#define CUBE_HALF_EXTENTS 1
#define EXTRA_HEIGHT -20.f
///BspToBulletConverter extends the BspConverter to convert to Bullet datastructures
class BspToBulletConverter : public BspConverter
{
BspDemo* m_demoApp;
public:
BspToBulletConverter(BspDemo* demoApp)
:m_demoApp(demoApp)
BspToBulletConverter(BspDemo* demoApp)
: m_demoApp(demoApp)
{
}
virtual void addConvexVerticesCollider(btAlignedObjectArray<btVector3>& vertices, bool isEntity, const btVector3& entityTargetLocation)
virtual void addConvexVerticesCollider(btAlignedObjectArray<btVector3>& vertices, bool isEntity, const btVector3& entityTargetLocation)
{
///perhaps we can do something special with entities (isEntity)
///like adding a collision Triggering (as example)
if (vertices.size() > 0)
{
///perhaps we can do something special with entities (isEntity)
///like adding a collision Triggering (as example)
float mass = 0.f;
btTransform startTransform;
//can use a shift
startTransform.setIdentity();
startTransform.setOrigin(btVector3(0, 0, -10.f));
//this create an internal copy of the vertices
if (vertices.size() > 0)
{
float mass = 0.f;
btTransform startTransform;
//can use a shift
startTransform.setIdentity();
startTransform.setOrigin(btVector3(0,0,-10.f));
//this create an internal copy of the vertices
btCollisionShape* shape = new btConvexHullShape(&(vertices[0].getX()), vertices.size());
m_demoApp->m_collisionShapes.push_back(shape);
btCollisionShape* shape = new btConvexHullShape(&(vertices[0].getX()),vertices.size());
m_demoApp->m_collisionShapes.push_back(shape);
//btRigidBody* body = m_demoApp->localCreateRigidBody(mass, startTransform,shape);
m_demoApp->createRigidBody(mass, startTransform,shape);
}
//btRigidBody* body = m_demoApp->localCreateRigidBody(mass, startTransform,shape);
m_demoApp->createRigidBody(mass, startTransform, shape);
}
}
};
////////////////////////////////////
BspDemo::~BspDemo()
{
exitPhysics(); //will delete all default data
exitPhysics(); //will delete all default data
}
void BspDemo::initPhysics()
void BspDemo::initPhysics()
{
const char* bspfilename = "BspDemo.bsp";
initPhysics(bspfilename);
}
void BspDemo::initPhysics(const char* bspfilename)
void BspDemo::initPhysics(const char* bspfilename)
{
int cameraUpAxis =2;
int cameraUpAxis = 2;
m_guiHelper->setUpAxis(cameraUpAxis);
btVector3 grav(0,0,0);
btVector3 grav(0, 0, 0);
grav[cameraUpAxis] = -10;
m_guiHelper->setUpAxis(cameraUpAxis);
//_cameraUp = btVector3(0,0,1);
//_forwardAxis = 1;
//etCameraDistance(22.f);
//_cameraUp = btVector3(0,0,1);
//_forwardAxis = 1;
//etCameraDistance(22.f);
///Setup a Physics Simulation Environment
m_collisionConfiguration = new btDefaultCollisionConfiguration();
// btCollisionShape* groundShape = new btBoxShape(btVector3(50,3,50));
// btCollisionShape* groundShape = new btBoxShape(btVector3(50,3,50));
m_dispatcher = new btCollisionDispatcher(m_collisionConfiguration);
btVector3 worldMin(-1000,-1000,-1000);
btVector3 worldMax(1000,1000,1000);
btVector3 worldMin(-1000, -1000, -1000);
btVector3 worldMax(1000, 1000, 1000);
m_broadphase = new btDbvtBroadphase();
//m_broadphase = new btAxisSweep3(worldMin,worldMax);
//btOverlappingPairCache* broadphase = new btSimpleBroadphase();
m_solver = new btSequentialImpulseConstraintSolver();
//ConstraintSolver* solver = new OdeConstraintSolver;
m_dynamicsWorld = new btDiscreteDynamicsWorld(m_dispatcher,m_broadphase,m_solver,m_collisionConfiguration);
m_dynamicsWorld = new btDiscreteDynamicsWorld(m_dispatcher, m_broadphase, m_solver, m_collisionConfiguration);
m_guiHelper->createPhysicsDebugDrawer(m_dynamicsWorld);
m_dynamicsWorld->setGravity(grav);
#ifdef QUAKE_BSP_IMPORTING
void* memoryBuffer = 0;
const char* filename = "BspDemo.bsp";
const char* prefix[]={"./","./data/","../data/","../../data/","../../../data/","../../../../data/"};
int numPrefixes = sizeof(prefix)/sizeof(const char*);
char relativeFileName[1024];
FILE* file=0;
for (int i=0;i<numPrefixes;i++)
{
sprintf(relativeFileName,"%s%s",prefix[i],filename);
file = fopen(relativeFileName,"r");
if (file)
break;
}
const char* prefix[] = {"./", "./data/", "../data/", "../../data/", "../../../data/", "../../../../data/"};
int numPrefixes = sizeof(prefix) / sizeof(const char*);
char relativeFileName[1024];
FILE* file = 0;
for (int i = 0; i < numPrefixes; i++)
{
sprintf(relativeFileName, "%s%s", prefix[i], filename);
file = fopen(relativeFileName, "r");
if (file)
break;
}
if (file)
{
BspLoader bspLoader;
int size=0;
if (fseek(file, 0, SEEK_END) || (size = ftell(file)) == EOF || fseek(file, 0, SEEK_SET)) { /* File operations denied? ok, just close and return failure */
int size = 0;
if (fseek(file, 0, SEEK_END) || (size = ftell(file)) == EOF || fseek(file, 0, SEEK_SET))
{ /* File operations denied? ok, just close and return failure */
printf("Error: cannot get filesize from %s\n", bspfilename);
} else
}
else
{
//how to detect file size?
memoryBuffer = malloc(size+1);
fread(memoryBuffer,1,size,file);
bspLoader.loadBSPFile( memoryBuffer);
memoryBuffer = malloc(size + 1);
fread(memoryBuffer, 1, size, file);
bspLoader.loadBSPFile(memoryBuffer);
BspToBulletConverter bsp2bullet(this);
float bspScaling = 0.1f;
bsp2bullet.convertBsp(bspLoader,bspScaling);
bsp2bullet.convertBsp(bspLoader, bspScaling);
}
fclose(file);
}
@@ -218,18 +183,8 @@ void BspDemo::initPhysics(const char* bspfilename)
#endif
m_guiHelper->autogenerateGraphicsObjects(m_dynamicsWorld);
}
//some code that de-mangles the windows filename passed in as argument
char cleaned_filename[512];
char* getLastFileName()
@@ -238,33 +193,31 @@ char* getLastFileName()
}
char* makeExeToBspFilename(const char* lpCmdLine)
{
// We might get a windows-style path on the command line, this can mess up the DOM which expects
// all paths to be URI's. This block of code does some conversion to try and make the input
// compliant without breaking the ability to accept a properly formatted URI. Right now this only
// displays the first filename
const char *in = lpCmdLine;
const char* in = lpCmdLine;
char* out = cleaned_filename;
*out = '\0';
// If the first character is a ", skip it (filenames with spaces in them are quoted)
if(*in == '\"')
if (*in == '\"')
{
in++;
}
int i;
for(i =0; i<512; i++)
for (i = 0; i < 512; i++)
{
//if we get '.' we stop as well, unless it's the first character. Then we add .bsp as extension
// If we hit a null or a quote, stop copying. This will get just the first filename.
if(i && (in[0] == '.') && (in[1] == 'e') && (in[2] == 'x') && (in[3] == 'e'))
if (i && (in[0] == '.') && (in[1] == 'e') && (in[2] == 'x') && (in[3] == 'e'))
break;
// If we hit a null or a quote, stop copying. This will get just the first filename.
if(*in == '\0' || *in == '\"')
if (*in == '\0' || *in == '\"')
break;
// Copy while swapping backslashes for forward ones
if(*in == '\\')
if (*in == '\\')
{
*out = '/';
}
@@ -284,14 +237,12 @@ char* makeExeToBspFilename(const char* lpCmdLine)
return cleaned_filename;
}
CommonExampleInterface* ImportBspCreateFunc(struct CommonExampleOptions& options)
CommonExampleInterface* ImportBspCreateFunc(struct CommonExampleOptions& options)
{
BspDemo* demo = new BspDemo(options.m_guiHelper);
demo->initPhysics("BspDemo.bsp");
return demo;
demo->initPhysics("BspDemo.bsp");
return demo;
}
/*
static DemoApplication* Create()

View File

@@ -15,9 +15,6 @@ subject to the following restrictions:
#ifndef BSP_DEMO_H
#define BSP_DEMO_H
class CommonExampleInterface* ImportBspCreateFunc(struct CommonExampleOptions& options);
#endif //BSP_DEMO_H
class CommonExampleInterface* ImportBspCreateFunc(struct CommonExampleOptions& options);
#endif //BSP_DEMO_H