Skip to content

Commit bc7d98b

Browse files
committed
Restructure and more docs fixes. Write readme
1 parent 00488cb commit bc7d98b

File tree

7 files changed

+138
-32
lines changed

7 files changed

+138
-32
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,5 @@ add_library(BSPParser
5252
src/displacements/sub-edge-iterator.cpp
5353
src/displacements/normal-blending.cpp
5454
src/displacements/normal-blending.hpp
55+
src/phys-model.hpp
5556
)

README.md

Lines changed: 101 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,110 @@ Simple and modern library for parsing the Valve BSP map format.
44

55
Documentation: https://taservers.github.io/BSPParser/
66

7-
See also: https://github.com/TAServers/MDLParser and https://github.com/TAServers/VTFParser
7+
See also:
8+
9+
- https://github.com/TAServers/MDLParser
10+
- https://github.com/TAServers/VTFParser
11+
- https://github.com/TAServers/PHYParser
812

913
## What's included
1014

11-
TODO
15+
- Class for parsing and abstracing the `BSP` file format for the Source engine.
16+
- Helper functions to simplify accessing the complex data structures of the BSP (see example below)
17+
- Enums and structs with decent coverage of the BSP lumps and their versions.
18+
- Runtime errors for issues when parsing the data due to corruption or a bug in the parser.
1219

1320
## Example
1421

15-
TODO
22+
Generating a triangle list for all models in the BSP:
23+
24+
```cpp
25+
#include "BSPParser.hpp"
26+
27+
// Load from a file on disk, an API, or somewhere in memory
28+
const auto bspData = ...;
29+
30+
// Parse the data (you should wrap this in a try/catch)
31+
const BspParser::Bsp bsp(bspData);
32+
33+
// We want to render the BSP, so pay the performance cost of smoothing displacement normals
34+
bsp.smoothNeighbouringDisplacements();
35+
36+
// For each model...
37+
BspParser::Accessors::iterateModels(
38+
bsp,
39+
[&bsp](
40+
const BspParser::Structs::Model& model,
41+
const std::vector<BspParser::PhysModel>& physicsModels
42+
) {
43+
// For each face...
44+
BspParser::Accessors::iterateFaces(
45+
bsp,
46+
model,
47+
[&bsp](
48+
const BspParser::Structs::Face& face,
49+
const BspParser::Structs::Plane& plane,
50+
const BspParser::Structs::TexInfo& textureInfo,
51+
const std::span<const int32_t> surfaceEdges
52+
) {
53+
if (isFaceNoDraw(surfaceEdges, textureInfo)) {
54+
return;
55+
}
56+
57+
// For each vertex... (you can use getVertexCount to preallocate memory for a single face or the whole BSP)
58+
BspParser::Accessors::generateVertices(
59+
bsp,
60+
face,
61+
plane,
62+
textureInfo,
63+
surfaceEdges,
64+
[](const BspParser::Vertex& vertex) {
65+
// Use vertex.position, vertex.normal, etc.
66+
}
67+
);
68+
69+
// For each index... (you can use getTriangleListIndexCount to preallocate memory here too)
70+
BspParser::Accessors::generateTriangleListIndices(
71+
bsp,
72+
face,
73+
surfaceEdges,
74+
[](const int32_t index0, const int32_t index1, const int32_t index2) {
75+
// Use triangle indices (always clockwise winding)
76+
}
77+
);
78+
}
79+
);
80+
}
81+
);
82+
```
83+
84+
Generating colliders for all physmeshes in the BSP:
85+
86+
```cpp
87+
#include "BSPParser.hpp"
88+
89+
// Load from a file on disk, an API, or somewhere in memory
90+
const auto bspData = ...;
91+
92+
// Parse the data (you should wrap this in a try/catch)
93+
const BspParser::Bsp bsp(bspData);
94+
95+
// We don't want to render the BSP, so don't smooth normals this time
96+
// bsp.smoothNeighbouringDisplacements();
97+
98+
// For each model...
99+
BspParser::Accessors::iterateModels(
100+
bsp,
101+
[&bsp](
102+
const BspParser::Structs::Model& model,
103+
const std::vector<BspParser::PhysModel>& physicsModels
104+
) {
105+
for (const auto& physicsModel : physicsModels) {
106+
// You don't have to use PhyParser here, any solution for parsing .phy file data will do
107+
const auto [solidsForModel, _] = PhyParser::parseSurfaces(physicsModel.collisionData, physicsModel.solidCount);
108+
109+
// Use solids to build your colliders...
110+
}
111+
}
112+
);
113+
```

src/accessors/face-accessors.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ namespace BspParser::Accessors {
1515

1616
void iterateModels(
1717
const Bsp& bsp,
18-
const std::function<void(const Structs::Model& model, const std::vector<Bsp::PhysModel>& physicsModels)>& iteratee
18+
const std::function<void(const Structs::Model& model, const std::vector<PhysModel>& physicsModels)>& iteratee
1919
) {
20-
std::vector<Bsp::PhysModel> physicsModels;
20+
std::vector<PhysModel> physicsModels;
2121

2222
for (int32_t modelIndex = 0; modelIndex < bsp.models.size(); modelIndex++) {
2323
physicsModels.clear();

src/accessors/face-accessors.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace BspParser::Accessors {
1717
*/
1818
void iterateModels(
1919
const Bsp& bsp,
20-
const std::function<void(const Structs::Model& model, const std::vector<Bsp::PhysModel>& physicsModels)>& iteratee
20+
const std::function<void(const Structs::Model& model, const std::vector<PhysModel>& physicsModels)>& iteratee
2121
);
2222

2323
/**

src/bsp.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ namespace BspParser {
127127
);
128128
}
129129

130-
std::vector<Bsp::PhysModel> Bsp::parsePhysCollideLump() const {
130+
std::vector<PhysModel> Bsp::parsePhysCollideLump() const {
131131
const auto& lumpHeader = header->lumps.at(static_cast<size_t>(Enums::Lump::PhysCollide));
132132
assertLumpHeaderValid(Enums::Lump::PhysCollide, lumpHeader);
133133

src/bsp.hpp

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22

33
#include "errors.hpp"
4+
#include "phys-model.hpp"
45
#include "displacements/triangulated-displacement.hpp"
56
#include "enums/lump.hpp"
67
#include "helpers/offset-data-view.hpp"
@@ -26,31 +27,6 @@ namespace BspParser {
2627
* @note Does not take ownership of the passed data. It is your responsibility to ensure the lifetime of the BSP does not exceed that of the underlying data.
2728
*/
2829
struct Bsp {
29-
/**
30-
* Raw physmesh solid and text data for a given model.
31-
*/
32-
struct PhysModel {
33-
/**
34-
* Index into the model lump this physics model is for
35-
*/
36-
int32_t modelIndex;
37-
38-
/**
39-
* Total number of solids in the collision surface sections
40-
*/
41-
int32_t solidCount;
42-
43-
/**
44-
* Raw .PHY solid data. Use `PhyParser::parseSurfaces` from the accompanying PHYParser library to parse this.
45-
*/
46-
std::span<const std::byte> collisionData;
47-
48-
/**
49-
* Raw .PHY text section data. Use `PhyParser::parseTextSection` from the accompanying PHYParser library to parse this.
50-
*/
51-
std::span<const std::byte> textSectionData;
52-
};
53-
5430
explicit Bsp(std::span<const std::byte> data);
5531

5632
std::span<const std::byte> data;

src/phys-model.hpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#pragma once
2+
3+
#include <cstdint>
4+
#include <span>
5+
6+
namespace BspParser {
7+
/**
8+
* Raw physmesh solid and text data for a given model.
9+
*/
10+
struct PhysModel {
11+
/**
12+
* Index into the model lump this physics model is for
13+
*/
14+
int32_t modelIndex = 0;
15+
16+
/**
17+
* Total number of solids in the collision surface sections
18+
*/
19+
int32_t solidCount = 0;
20+
21+
/**
22+
* Raw .PHY solid data. Use `PhyParser::parseSurfaces` from the accompanying PHYParser library to parse this.
23+
*/
24+
std::span<const std::byte> collisionData;
25+
26+
/**
27+
* Raw .PHY text section data. Use `PhyParser::parseTextSection` from the accompanying PHYParser library to parse this.
28+
*/
29+
std::span<const std::byte> textSectionData;
30+
};
31+
}

0 commit comments

Comments
 (0)