Click here to Skip to main content
15,881,248 members
Articles / Desktop Programming / Win32

Half Life Game Level Viewer

Rate me:
Please Sign up or sign in to vote.
4.61/5 (23 votes)
7 Feb 2009CPOL26 min read 79.5K   2.3K   60  
DirectX based application to open and view Half Life 1 game files
//
//	BSPFileDefs.h
//
//	Copyright 2005 Paul Higinbotham
//
//	This file contains all of the Half Life BSP data lump structures used in this application.
//  The data lump structures define the various game level data stored in the BSP file.  The
//	BSPFile class is used to read the data lumps from a bsp file and the BSPData class is used
//	to store an entire bsp file level for use by the application.
//


#ifndef __BSPFILEDEFS_H_
#define __BSPFILEDEFS_H_


namespace ZGraphics
{

// These defines are indexes into the file directory for level data lump.  These indexes are used
// the BSPFile class to find and read each level data lump into memory.
#define LUMP_ENTITIES		0		// Entity data lump, name/value pairs, parsed in EntityParse class.
#define LUMP_PLANES			1		// Array of bspf_plane data structs, which define splitting/face planes.
#define LUMP_TEXTURES		2		// Texture information data lump. 
#define LUMP_VERTEXES		3		// Array of bspf_vertex data structures defining all vertex data for all level and model geometry.
#define LUMP_VISIBILITY		4		// Compressed (RLE) visibility data that maps all visible leafs to a given leaf in the BSP tree.
#define LUMP_NODES			5		// Array of bspf_node data structures that define the level BSP tree.
#define LUMP_TEXINFO		6		// Array of bspf_textinfo data structures defining texture mapping for each geometry face.
#define LUMP_FACES			7		// Array of bspf_face data structures defining geometry faces.
#define LUMP_LIGHTING		8		// Data lump of light textures for geometry.  bspf_face struct contains offset into this data.
#define LUMP_CLIPNODES		9		// Not used in HLViewer application.
#define LUMP_LEAFS			10		// Array of bspf_leaf data structures.  Defines BSP leaf node data.
#define LUMP_MARKSURFACES	11		// Array of integers that maps a leaf node face index into an actual bspf_face data structure (LUMP_FACES).
#define LUMP_EDGES			12		// Array of bspf_edge data structures.  
#define LUMP_SURFEDGES		13		// Maps face edge reference to bspf_edge data structure.
#define LUMP_MODELS			14		// Array of bspf_dmodel data structures.
#define HEADER_LUMPS		15		// Defines number of data lumps in file directory.


#define AMBIENT_WATER		0
#define AMBIENT_SKY			1
#define AMBIENT_SLIME		2
#define AMBIENT_LAVA		3
#define NUM_AMBIENTS		4		// automatic ambient sounds

// This enum defines what a leaf node in the BSP tree describes.  This application currently only uses
// the CONTENTS_EMPTY, CONTENTS_SOLID, CONTENTS_WATER, CONTENTS_SLIME, CONTENTS_LAVA leaf node types.
// This leaf node type information is used in collision detection to determine if a player/camera bounding
// box intersection protrudes into solid geometry space, which cannot be allowed.  Intersections with empty, 
// water, slime, lava spaces are allowed since the player can move through empty level spaces and liquids.
// Note that this application doesn't currently render the sky box.  The other node types are unknown at
// this point.
typedef enum
{
    CONTENTS_EMPTY = -1,
    CONTENTS_SOLID = -2,
    CONTENTS_WATER = -3,
    CONTENTS_SLIME = -4,
    CONTENTS_LAVA = -5,
    CONTENTS_SKY = -6,
    CONTENTS_ORIGIN = -7,
    CONTENTS_CLIP = -8,

    CONTENTS_CURRENT_0 = -9,
    CONTENTS_CURRENT_90 = -10,
    CONTENTS_CURRENT_180 = -11,
    CONTENTS_CURRENT_270 = -12,
    CONTENTS_CURRENT_UP = -13,
    CONTENTS_CURRENT_DOWN = -14,

    CONTENTS_TRANSLUCENT = -15,
    CONTENTS_HINT = -16,
} bspf_contents;

//
// BSP file data structs
//

// BSP file directory data lump.  Contains the file offset and size of the data lump.
typedef struct
{
	int		offset;						// offset (in bytes) to lump data from beginning of file
	int		length;						// length (in bytes) of lump data
} bspf_lump;

// BSP file header information.  Contains an array directory data used for loading data lumps.
// The bspf_contents enum (above) defines the data lump types in the lump array.
typedef struct
{
	int			version;	
	bspf_lump	lump[HEADER_LUMPS];		// directory of data lumps
} bspf_header;

// Vertex data structure used by bsp file.  Defines three floats as the vertex coordinates.
typedef struct
{
	float	fPoint[3];
} bspf_vertex;

// Specifies indexes into the vertex array (LUMP_VERTEXES).  The two vertices specified here make up
// a single edge of a (polygonal) geometry face.  The edges array (LUMP_EDGES) index into an array
// of this data struct, which in turn define the vertices for that edge.
// Note that winding order depends on the sign of the index from the LUMP_EDGES array.  If the index
// is negative then the vertex order is reversed.
typedef struct
{
	unsigned short	nV1;				// Index into vertex array for first edge point
	unsigned short	nV2;				// Index into vertex array for next edge point
} bspf_edge;

// Specifies a geometry face through the plane it resides on, the edges that make up the face
// (which in turn reference actual vertices of the face edge), how the face is textured, and
// which light map to apply to the face.
#define MAXLIGHTMAPS    4
typedef struct
{
	unsigned short	nPlane;				// Index into plane array
	short			cSide;				// Zero if face normal is same as plane normal

	int				nE1;				// Index of first edge into face_edge array
	short			cEdges;				// Number of consecutive edges in face edge array
	short			nTextureInfo;		// Index into texture info structure

	unsigned char	LightMapStyles[MAXLIGHTMAPS];	// Styles (bit flags) for light maps
	int				nLightMapOfs;		// Offset into light map lump (in bytes) for the lightmap
} bspf_face;

// face_edge array is array of unsigned long integers

// Structure for geometric plane data used in the bsp file.  The LUMP_PLANES data lump is an array of this
// data.  The BSP data references this array to specify splitting planes.  Note that all leaf node faces
// reside on one of these planes.  
typedef struct
{
	float			fNormal[3];			// Plane normal
	float			fDistance;			// Plane distance from origin
	unsigned long	type;				// Type of plane (X, Y, Z, X/Y, etc.)
} bspf_plane;

// Data structure for BSP node data.  Contains a reference to the node splitting plane (unless it
// is a leaf node in which case the plane reference is negative).  Also contains references to the
// next front or back splitting node (LUMP_NODES).  If the next node is a leaf node (not a splitting 
// node) then the reference (nFront or nBack) is negative and references a bspf_leaf data structure 
// (LUMP_LEAFS) and not another bspf_node data structure.
// This structure also contains coordinates for a bounding box of the half space defined by this 
// splitting node.  Finally, geometry face references are provided.  The bounding box and face data
// is used for geometry culling and collision detection.
typedef struct
{
	int				nPlane;				// Index into plane array.

	short			nFront;				// Index to child node in front of plane
	short			nBack;				// Index to child node in back of plane
										// Negative numbers are leaf nodes leaf array index = -(childindex+1)

	short			MinBox[3];			// Min x,y,z integer coordinates of node bounding box
	short			MaxBox[3];			// Max x,y,z integer coordinates of node bounding box

	unsigned short	nFirstFace;			// Index into face array of first face in node
	unsigned short	cFaces;				// Number of faces in face array for node
} bspf_node;

// Defines the data structure for the LUMP_LEAFS data array.  The leaf node is a special case of 
// the BSP tree.  It contains information as to the type of leaf, an offset into the PVS list, which
// is a list of all other leaf nodes potentially visible from this leaf node, as well as a bounding box
// and geometry faces belonging to this space.
typedef struct
{
	int				Contents;			// type of leaf, solid, empty, ...
	int				ofsCluster;			// Offset into visibility set for leaf cluster.  -1 means no visibility info

	short			MinBox[3];			// Min x,y,z integer coordinates of hull bounding box
	short			MaxBox[3];			// Max x,y,z integer corrdinates of hull bounding box

	unsigned short	nFirstFace;			// Index into the LEAF face array for first face of hull
	unsigned short	cFaces;				// Number of faces in face array

	unsigned char	ambient_level[NUM_AMBIENTS];	// ambient sounds
} bspf_leaf;

// Leaf face array is an array of unsigned shorts that index into the face array


// Data structure for the LUMP_TEXINFO data array.  Each of these data structures corresponds
// to a geometric face and contains U,V texture coordinates as well as a reference to the
// actual texture information (bspf_textlump) in the LUMP_TEXTURES data lump.
typedef struct
{
	float	uAxis[3];					// U texture axis for face
	float	uOffset;					// Offset of texture coordinate along u axis for face
	// u = x * uAxis.x + y * uAxis.y + z * uAxis.z + uOffset;
	
	float	vAxis[3];					// V texture axis for face
	float	vOffset;					// Offset of texture coordinate along v axis for face
	// v = x * vAxis.x + y * vAxis.y + z * vAxis.z + vOffset;

	int		nMipTex;					// Index into text lump array (texture name, size, mips)
	int		flags;
} bspf_textinfo;

// This data struct defines the top part of the LUMP_TEXTURES data lump and contains an array of
// offsets into the rest of the data lump.  The offset points to a bspf_miptex struct that defines
// the actual texture being referenced.
typedef struct
{
    int		nummiptex;
    int		dataofs[4];
} bspf_textlump;

// This is the actual texture information contained for each texture reference in the LUMP_TEXUTRES
// data lump.  It contains the texture name as it exists in the WAD file, size, and MIP levels.
#define MIPLEVELS	4
typedef struct
{
    char			szname[16];
    unsigned int	width, height;
    unsigned int	offsets[MIPLEVELS];	// four mip maps stored
} bspf_miptex;

// Defines model information from LUMP_MODELS data lump, which is an array of these data structs.
// Models can be entity brush models (crates, doors, etc.) and character models.
// Includes bounding box for model and indexes into face array (LUMP_FACES) used to render model.
#define MAX_MAP_HULLS	4
typedef struct
{
    float           mins[3], maxs[3];
    float           origin[3];
    int             headnode[MAX_MAP_HULLS];
    int             visleafs;                              // not including the solid leaf 0
    int             firstface, numfaces;
} bspf_dmodel;


// File data classes
template <typename T>
class DataLump
{
public:
	DataLump() : m_pArray(0), m_cSize(0) {}
	DataLump(T * pArray, int cSize) : m_pArray(pArray), m_cSize(cSize) {}
	~DataLump()
	{
		delete [] m_pArray;
	}

	T *			m_pArray;
	int			m_cSize;
};


} /* namespace ZGraphics */


#endif /* __BSPFILEDEFS_H_ */

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
I am a senior software developer currently doing contract work for Microsoft. My educational background is in electrical engineering and I hold a masters degree from the University of Washington. I have experience in hardware and systems design but have done primarily software development for the last two decades. I have worked for various small companies as well as start-up companies, and have worked as a full time employee SDE at Microsoft Corporation.

Comments and Discussions