-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbspmodel.cpp
More file actions
105 lines (83 loc) · 3.12 KB
/
bspmodel.cpp
File metadata and controls
105 lines (83 loc) · 3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include "Config.h"
#include <stack>
#include "bspmodel.h"
namespace P3D
{
Stack<unsigned int> BspModel::stack;
List<unsigned int> BspModel::node_list;
void BspModel::Sort(const V3<fp>& p, const AABB<fp>& frustrum, std::vector<const BspModelTriangle *> &out, bool backface_cull) const
{
out.clear();
node_list.Clear();
SortBackToFront(p, frustrum);
OutputTris(frustrum, out, backface_cull);
}
constexpr unsigned int BACK_BIT = 1 << 31;
constexpr unsigned int POST_BIT = 1 << 30;
constexpr unsigned int NODE_MASK = ~(BACK_BIT | POST_BIT);
void BspModel::SortBackToFront(const V3<fp>& p, const AABB<fp>& frustrum) const
{
stack.Push(0);
while(!stack.Empty())
{
const unsigned int item = stack.Pop();
const BspModelNode* n = GetNode(item & NODE_MASK);
if (!frustrum.Intersect(n->child_bb))
continue;
if (item & POST_BIT)
{
if (frustrum.Intersect(n->node_bb))
{
if (item & BACK_BIT)
node_list.Add(item & NODE_MASK);
else
node_list.Add((item & NODE_MASK) | BACK_BIT);
}
}
else
{
if(Distance(n->plane, p) >= 0)
{
if (n->front_node)
stack.Push(n->front_node);
stack.Push(item | POST_BIT);
if (n->back_node)
stack.Push(n->back_node);
}
else
{
if (n->back_node)
stack.Push(n->back_node);
stack.Push(item | POST_BIT | BACK_BIT);
if (n->front_node)
stack.Push(n->front_node);
}
}
}
}
void BspModel::OutputTris(const AABB<fp>& frustrum, std::vector<const BspModelTriangle *> &out, bool backface_cull) const
{
for(unsigned int i = 0; i < node_list.Size(); i++)
{
const unsigned int node = node_list.At(i);
const BspModelNode* n = GetNode(node & NODE_MASK);
const TriIndexList* front = (node & BACK_BIT) ? &n->back_tris : &n->front_tris;
for(unsigned int i = 0; i < front->count; i++)
{
const BspModelTriangle* tri = GetTriangle(front->offset + i);
if(frustrum.Intersect(tri->tri_bb))
out.push_back(tri);
}
if(!backface_cull)
{
const TriIndexList* back = (node & BACK_BIT) ? &n->front_tris : &n->back_tris;
for(unsigned int i = 0; i < back->count; i++)
{
const BspModelTriangle* tri = GetTriangle(back->offset + i);
if(frustrum.Intersect(tri->tri_bb))
out.push_back(tri);
}
}
}
}
}