-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathtoplevel.h
117 lines (105 loc) · 2.83 KB
/
toplevel.h
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
106
107
108
109
110
111
112
113
114
115
116
117
#pragma once
// enable the use of SSE in the AABB intersection function
#define USE_SSE
// bin count
#define BINS 8
namespace Tmpl8
{
// minimalist triangle struct
struct Tri { float3 vertex0, vertex1, vertex2; float3 centroid; };
// ray struct, prepared for SIMD AABB intersection
__declspec(align(64)) struct Ray
{
Ray() { O4 = D4 = rD4 = _mm_set1_ps( 1 ); }
union { struct { float3 O; float dummy1; }; __m128 O4; };
union { struct { float3 D; float dummy2; }; __m128 D4; };
union { struct { float3 rD; float dummy3; }; __m128 rD4; };
float t = 1e30f;
};
// minimalist AABB struct with grow functionality
struct aabb
{
float3 bmin = 1e30f, bmax = -1e30f;
void grow( float3 p ) { bmin = fminf( bmin, p ); bmax = fmaxf( bmax, p ); }
void grow( aabb& b ) { if (b.bmin.x != 1e30f) { grow( b.bmin ); grow( b.bmax ); } }
float area()
{
float3 e = bmax - bmin; // box extent
return e.x * e.y + e.y * e.z + e.z * e.x;
}
};
// 32-byte BVH node struct
struct BVHNode
{
union { struct { float3 aabbMin; uint leftFirst; }; __m128 aabbMin4; };
union { struct { float3 aabbMax; uint triCount; }; __m128 aabbMax4; };
bool isLeaf() { return triCount > 0; }
float CalculateNodeCost()
{
float3 e = aabbMax - aabbMin; // extent of the node
return (e.x * e.y + e.y * e.z + e.z * e.x) * triCount;
}
};
// bvh
class BVH
{
public:
BVH() = default;
BVH( char* triFile, int N );
void Build();
void Refit();
void SetTransform( mat4& transform );
void Intersect( Ray& ray );
private:
void Subdivide( uint nodeIdx );
void UpdateNodeBounds( uint nodeIdx );
float FindBestSplitPlane( BVHNode& node, int& axis, float& splitPos );
BVHNode* bvhNode = 0;
Tri* tri = 0;
uint* triIdx = 0;
uint nodesUsed, triCount;
mat4 invTransform; // inverse transform
aabb bounds; // in world space
};
// top-level node
struct TLASNode
{
float3 aabbMin;
uint leftBLAS;
float3 aabbMax;
uint isLeaf;
};
// top-level BVH
class TLAS
{
public:
TLAS() = default;
TLAS( BVH* bvhList, int N );
void Build();
void Intersect( Ray& ray );
private:
TLASNode* tlasNode = 0;
BVH* blas = 0;
uint nodesUsed, blasCount;
};
// game class
class TopLevelApp : public TheApp
{
public:
// game flow methods
void Init();
void Tick( float deltaTime );
void Shutdown() { /* implement if you want to do something on exit */ }
// input handling
void MouseUp( int button ) { /* implement if you want to detect mouse button presses */ }
void MouseDown( int button ) { /* implement if you want to detect mouse button presses */ }
void MouseMove( int x, int y ) { mousePos.x = x, mousePos.y = y; }
void MouseWheel( float y ) { /* implement if you want to handle the mouse wheel */ }
void KeyUp( int key ) { /* implement if you want to handle keys */ }
void KeyDown( int key ) { /* implement if you want to handle keys */ }
// data members
int2 mousePos;
BVH bvh[64];
TLAS tlas;
};
} // namespace Tmpl8