You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
289 lines
11 KiB
289 lines
11 KiB
#pragma once
|
|
|
|
typedef struct // A Directory entry
|
|
{
|
|
long offset; // Offset to entry, in bytes, from start of file
|
|
long size; // Size of entry in file, in bytes
|
|
} dentry_t;
|
|
|
|
typedef struct // The BSP file header
|
|
{
|
|
long version; // Model version, must be 0x17 (23).
|
|
dentry_t entities; // List of Entities.
|
|
dentry_t planes; // Map Planes.
|
|
// numplanes = size/sizeof(plane_t)
|
|
dentry_t miptex; // Wall Textures.
|
|
dentry_t vertices; // Map Vertices.
|
|
// numvertices = size/sizeof(vertex_t)
|
|
dentry_t visilist; // Leaves Visibility lists.
|
|
dentry_t nodes; // BSP Nodes.
|
|
// numnodes = size/sizeof(node_t)
|
|
dentry_t texinfo; // Texture Info for faces.
|
|
// numtexinfo = size/sizeof(texinfo_t)
|
|
dentry_t faces; // Faces of each surface.
|
|
// numfaces = size/sizeof(face_t)
|
|
dentry_t lightmaps; // Wall Light Maps.
|
|
dentry_t clipnodes; // clip nodes, for Models.
|
|
// numclips = size/sizeof(clipnode_t)
|
|
dentry_t leaves; // BSP Leaves.
|
|
// numlaves = size/sizeof(leaf_t)
|
|
dentry_t lface; // List of Faces.
|
|
dentry_t edges; // Edges of faces.
|
|
// numedges = Size/sizeof(edge_t)
|
|
dentry_t ledges; // List of Edges.
|
|
dentry_t models; // List of Models.
|
|
// nummodels = Size/sizeof(model_t)
|
|
} dheader_t;
|
|
|
|
typedef struct
|
|
{
|
|
vec3_t normal; // Vector orthogonal to plane (Nx,Ny,Nz)
|
|
// with Nx2+Ny2+Nz2 = 1
|
|
scalar_t dist; // Offset to plane, along the normal vector.
|
|
// Distance from (0,0,0) to the plane
|
|
long type; // Type of plane, depending on normal vector.
|
|
} plane_t;
|
|
|
|
typedef struct BoundBox // Bounding Box, Float values
|
|
{
|
|
vec3_t min; // minimum values of X,Y,Z
|
|
vec3_t max; // maximum values of X,Y,Z
|
|
|
|
BoundBox(): min(FLT_MAX, FLT_MAX, FLT_MAX), max(FLT_MIN, FLT_MIN, FLT_MIN) { }
|
|
|
|
void init(vec3_t point)
|
|
{
|
|
min = point;
|
|
max = point;
|
|
}
|
|
|
|
void includePoint(vec3_t point)
|
|
{
|
|
if (point.x < min.x)
|
|
min.x = point.x;
|
|
if (point.y < min.y)
|
|
min.y = point.y;
|
|
if (point.z < min.z)
|
|
min.z = point.z;
|
|
|
|
if (point.x > max.x)
|
|
max.x = point.x;
|
|
if (point.y > max.y)
|
|
max.y = point.y;
|
|
if (point.z > max.z)
|
|
max.z = point.z;
|
|
}
|
|
} boundbox_t;
|
|
|
|
typedef struct BBoxShort // Bounding Box, Short values
|
|
{
|
|
short min[3]; // minimum values of X,Y,Z
|
|
short max[3]; // maximum values of X,Y,Z
|
|
|
|
vec3_t getCenter() const
|
|
{
|
|
return vec3_t(
|
|
((float)max[0] + (float)min[0]) / 2,
|
|
((float)max[1] + (float)min[1]) / 2,
|
|
((float)max[2] + (float)min[2]) / 2
|
|
);
|
|
}
|
|
} bboxshort_t;
|
|
|
|
typedef struct // Mip texture list header
|
|
{
|
|
long numtex; // Number of textures in Mip Texture list
|
|
long *offset; // Offset to each of the individual texture
|
|
} mipheader_t; // from the beginning of mipheader_t
|
|
|
|
typedef struct // Mip Texture
|
|
{
|
|
char name[16]; // Name of the texture.
|
|
unsigned long width; // width of picture, must be a multiple of 8
|
|
unsigned long height; // height of picture, must be a multiple of 8
|
|
unsigned long offset1; // offset to u_char Pix[width * height]
|
|
unsigned long offset2; // offset to u_char Pix[width/2 * height/2]
|
|
unsigned long offset4; // offset to u_char Pix[width/4 * height/4]
|
|
unsigned long offset8; // offset to u_char Pix[width/8 * height/8]
|
|
} miptex_t;
|
|
|
|
typedef struct Vertex
|
|
{
|
|
float X; // X,Y,Z coordinates of the vertex
|
|
float Y; // usually some integer value
|
|
float Z; // but coded in floating point
|
|
|
|
vec3_t toVec() const
|
|
{
|
|
return vec3_t(X, Y, Z);
|
|
}
|
|
} vertex_t;
|
|
|
|
typedef struct
|
|
{
|
|
unsigned short vertex0; // index of the start vertex
|
|
// must be in [0,numvertices[
|
|
unsigned short vertex1; // index of the end vertex
|
|
// must be in [0,numvertices[
|
|
} edge_t;
|
|
|
|
typedef struct
|
|
{
|
|
vec3_t vectorS; // S vector, horizontal in texture space)
|
|
scalar_t distS; // horizontal offset in texture space
|
|
vec3_t vectorT; // T vector, vertical in texture space
|
|
scalar_t distT; // vertical offset in texture space
|
|
unsigned long texture_id; // Index of Mip Texture
|
|
// must be in [0,numtex[
|
|
unsigned long animated; // 0 for ordinary textures, 1 for water
|
|
} texinfo_t;
|
|
|
|
typedef struct
|
|
{
|
|
unsigned short plane_id; // The plane in which the face lies
|
|
// must be in [0,numplanes[
|
|
unsigned short side; // 0 if in front of the plane, 1 if behind the plane
|
|
long ledge_id; // first edge in the List of edges
|
|
// must be in [0,numledges[
|
|
unsigned short ledge_num; // number of edges in the List of edges
|
|
unsigned short texinfo_id; // index of the Texture info the face is part of
|
|
// must be in [0,numtexinfos[
|
|
unsigned char typelight; // type of lighting, for the face
|
|
unsigned char baselight; // from 0xFF (dark) to 0 (bright)
|
|
unsigned char light[2]; // two additional light models
|
|
long lightmap; // Pointer inside the general light map, or -1
|
|
// this define the start of the face light map
|
|
} face_t;
|
|
|
|
typedef struct
|
|
{
|
|
long plane_id; // The plane that splits the node
|
|
// must be in [0,numplanes[
|
|
short front; // If bit15==0, index of Front child node
|
|
// If bit15==1, ~front = index of child leaf
|
|
short back; // If bit15==0, id of Back child node
|
|
// If bit15==1, ~back = id of child leaf
|
|
bboxshort_t box; // Bounding box of node and all childs
|
|
unsigned short face_id; // Index of first Polygons in the node
|
|
unsigned short face_num; // Number of faces in the node
|
|
} node_t;
|
|
|
|
typedef struct
|
|
{
|
|
long type; // Special type of leaf
|
|
long vislist; // Beginning of visibility lists
|
|
// must be -1 or in [0,numvislist[
|
|
bboxshort_t bound; // Bounding box of the leaf
|
|
unsigned short lface_id; // First item of the list of faces
|
|
// must be in [0,numlfaces[
|
|
unsigned short lface_num; // Number of faces in the leaf
|
|
unsigned char sndwater; // level of the four ambient sounds:
|
|
unsigned char sndsky; // 0 is no sound
|
|
unsigned char sndslime; // 0xFF is maximum volume
|
|
unsigned char sndlava; //
|
|
} dleaf_t;
|
|
|
|
typedef struct World
|
|
{
|
|
const char* name;
|
|
dheader_t header;
|
|
|
|
mipheader_t mipheader;
|
|
miptex_t* miptexes;
|
|
unsigned char** textures;
|
|
|
|
int numPlanes;
|
|
plane_t* planes;
|
|
|
|
int numVertices;
|
|
vertex_t* vertices;
|
|
|
|
int numEdges;
|
|
edge_t* edges;
|
|
|
|
int edgeListLength;
|
|
int* edgeList;
|
|
|
|
int numTexInfos;
|
|
texinfo_t* texInfos;
|
|
|
|
int numFaces;
|
|
face_t* faces;
|
|
|
|
int faceListLength;
|
|
unsigned short* faceList;
|
|
|
|
int visListLength;
|
|
unsigned char *visList;
|
|
|
|
int numNodes;
|
|
node_t* nodes;
|
|
|
|
int numLeaves;
|
|
dleaf_t* leaves;
|
|
|
|
int entitiesLength;
|
|
char* entities;
|
|
|
|
int lightmapLength;
|
|
unsigned char* lightmap;
|
|
|
|
// Find the list of all faces that contain the given point, i.e. the point lies on the face's plane and is contained within its polygon bounds
|
|
std::vector<const face_t*> facesWithPoint(const Vec3& point) const
|
|
{
|
|
std::vector<const face_t*> outFaces;
|
|
for (int faceIdx = 0; faceIdx < numFaces; ++faceIdx)
|
|
{
|
|
const face_t* face = &faces[faceIdx];
|
|
const plane_t* plane = &planes[face->plane_id];
|
|
|
|
// Check if the point lies on the face's plane (it's not strictly necessary to do this, but this check makes the whole function a lot faster)
|
|
double pointPlaneDist = ((double)point.x * plane->normal.x + (double)point.y * plane->normal.y + (double)point.z * plane->normal.z) - plane->dist;
|
|
if (fabs(pointPlaneDist) > 0.0001)
|
|
continue;
|
|
|
|
// Check if the point is contained within the face's polygon
|
|
double angleSum = 0;
|
|
for (int edgeListIdx = 0; edgeListIdx < face->ledge_num; ++edgeListIdx)
|
|
{
|
|
int edgeIdx = edgeList[face->ledge_id + edgeListIdx];
|
|
|
|
Vec3 v0, v1;
|
|
if (edgeIdx > 0)
|
|
{
|
|
const edge_t* edge = &edges[edgeIdx];
|
|
v0 = vertices[edge->vertex0].toVec();
|
|
v1 = vertices[edge->vertex1].toVec();
|
|
}
|
|
else
|
|
{
|
|
const edge_t* edge = &edges[-edgeIdx];
|
|
v0 = vertices[edge->vertex1].toVec();
|
|
v1 = vertices[edge->vertex0].toVec();
|
|
}
|
|
|
|
Vec3 p0 = v0 - point;
|
|
Vec3 p1 = v1 - point;
|
|
double m0 = p0.magnitude();
|
|
double m1 = p1.magnitude();
|
|
|
|
if ((m0 * m1) <= 0.0001)
|
|
{
|
|
// Point is on one of the vertices
|
|
outFaces.push_back(face);
|
|
break;
|
|
}
|
|
|
|
double dot = p0.dotProduct(p1) / (m0 * m1);
|
|
angleSum += acos(dot);
|
|
}
|
|
|
|
// If the point is inside the polygon, then the sum of all the above angles will be exactly 360 degrees
|
|
if (fabs(2 * M_PI - angleSum) <= 0.0001)
|
|
outFaces.push_back(face);
|
|
}
|
|
|
|
return outFaces;
|
|
}
|
|
|
|
} world_t;
|