BSP File Format


Intro

Gabriel Knight 3 stores all the room geometry inside BSP files (for more info on BSP in general see http://en.wikipedia.org/wiki/Binary_space_partitioning). These BSP files contain a serialized BSP tree of all the polygons that make up the room or area, and also info about "models," which in this case are groups of polygons than can represent a "hotspot" for the player to click on.

Header

Header
OffsetLengthTypeDescription
0 4 4cc File identifier (appears as "NECS") in the file
4 4 uint32 Version (usually is 0x202)
8 4 uint32 Data size (file size - header size)
12 4 uint32 Index of root node
16 4 uint32 Number of models
20 4 uint32 Number of vertices
24 4 uint32 Number of texture coords
28 4 uint32 Number of vertex indices
32 4 uint32 Number of texture indices
36 4 uint32 Number of surfaces
40 4 uint32 Number of planes
44 4 uint32 Number of nodes
48 4 uint32 Number of polygons

Models

After the header comes an array of models. The info in the header specifies how many models there are in the file. Each model is just a 32-character long string that looks like this:

OffsetSizeTypeDescription
? 32 string Name of the model

Surfaces

Next is an array of surfaces. Surfaces describe texture info. The header tells how many surfaces there are.

OffsetSizeTypeDescription
? 4 uint32 Index of the model this surface belongs to
? + 4 32 string Texture for this surface
? + 36 4 float U coord
? + 40 4 float V coord
? + 44 4 float U scale
? + 48 4 float V scale
? + 52 4 float Size?
? + 56 4 uint32 Flags

BSP

This section stores the nodes that make up the actual BSP tree. The number of nodes is specified in the header.

OffsetSizeTypeDescription
? 2 uint16 Index of child node?
? + 2 2 uint16 Index of child node?
? + 4 2 uint16 Plane index
? + 6 2 uint16 Starting index of polygons
? + 8 2 uint16 Unknown!
? + 10 2 uint16 Number of polygons in this node
? + 12 2 uint16 Unknown!
? + 14 2 uint16 Unknown!

Polygons

Next is an array of polygons. Polygons work by defining the number of vertices in the polygon and the offset to the index array where the polygon begins. The number of polygons is defined in the header.

OffsetSizeTypeDescription
? 2 uint16 Index of vertex coord
? + 2 2 uint16 Flags?
? + 4 2 uint16 Number of vertices
? + 6 2 uint16 Index of surface

Planes

Next is the array of planes. Planes are believed to be stored as a normal and a distance from the origin.

OffsetSizeTypeDescription
? 4 float Normal X
? + 4 4 float Normal Y
? + 8 4 float Normal Z
? + 12 4 float Distance from origin

Vertices

OffsetSizeTypeDescription
? 4 float X
? + 4 4 float Y
? + 8 4 float Z

Texture coords

OffsetSizeTypeDescription
? 4 float U
? + 4 4 float V

Vertex indices

OffsetSizeTypeDescription
? 2 uint16 Index

Texture indices

OffsetSizeTypeDescription
? 2 uint16 Index

Bounding spheres

This is an array of bounding spheres. The number of spheres in the array is equal to the number of nodes defined in the header.

OffsetSizeTypeDescription
? 4 float Bounding sphere X
? + 4 4 float Bounding sphere Y
? + 8 4 float Bounding sphere Z
? + 12 4 float Radius

Extension

Version 1.02 and up of the format have extra info at this point in the file. As for what it is for or how it is used in the game we don't know.

OffsetSizeTypeDescription
? 4 float Bounding sphere X
? + 4 4 float Bounding sphere Y
? + 8 4 float Bounding sphere Z
? + 12 4 float Bounding sphere radius
? + 16 4 float Chrome value
? + 20 4 float Grazing value
? + 24 4 COLORREF Chrome color
? + 28 4 uint32 Number of indices
? + 32 4 uint32 Number of triangles

Following each element in this array are two more arrays:

OffsetSizeTypeDescription
? 2 uint16 Vertex indices
OffsetSizeTypeDescription
? 2 uint16 Triangles