-
Notifications
You must be signed in to change notification settings - Fork 0
/
BVH_Cuda.h
139 lines (89 loc) · 2.59 KB
/
BVH_Cuda.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#pragma once
#include "BVH.h"
#include "BVH2.h"
#include <vector>
using namespace std;
//using namespace FW;
struct BVH_Node_ {
AABB aabb;
int offset_left;
int offset_right;
int offset_tris;
int num_tris;
BVH_Node_() {
offset_left = -1;
offset_right = -1;
offset_tris = -1;
num_tris = 0;
};
} ;
//struct BVH_Node_Tris {
//float3 v0, v1, v2;
//} ;
class BVH_Cuda
{
struct SP {
} ;
public:
vector <BVH_Node_> bvh_nodes; //
vector <int> tri_indices; //
public:
BVH_Cuda(void);
~BVH_Cuda(void);
// return offset node in bvh_nodes
int build ( BVH_Node * node ) { // , int & offLeft, int & offRight, int
static int num_bvh_nodes = 0;
static int tri_index = 0;
int curr_bvh_node_index = num_bvh_nodes;
BVH_Node_ bvh_node;
bvh_nodes.push_back( bvh_node );
bvh_node.aabb = node->bounds;
if ( !node->isLeaf ) {
++num_bvh_nodes;
bvh_node.offset_left = build ( node->pLeft );
++num_bvh_nodes;
bvh_node.offset_right = build ( node->pRight );
} else {
//tri_indices.resize( tri_indices.size() + node->tris.size() );
for ( int i=0; i<node->tris.size(); ++i )
tri_indices.push_back( node->tris[i] );
bvh_node.offset_tris = tri_index;
bvh_node.num_tris = node->tris.size();
tri_index += node->tris.size();
}
bvh_nodes[ curr_bvh_node_index ] = bvh_node;
return curr_bvh_node_index;
} ;
void build_from_bvh2 ( FW::BVH2 & bvh2 ) {
//tri_indices.resize( bvh2.m_triIndices.size() );
tri_indices = bvh2.m_triIndices;
for ( int i=0; i<tri_indices.size(); ++i )
tri_indices[i] *= 3;
build2( bvh2.getRoot() );
} ;
int build2 ( FW::BVHNode * node ) { // , int & offLeft, int & offRight, int
static int num_bvh_nodes = 0;
static int tri_index = 0;
int curr_bvh_node_index = num_bvh_nodes;
BVH_Node_ bvh_node;
bvh_nodes.push_back( bvh_node );
//bvh_node.aabb = node->m_bounds;
bvh_node.aabb.set( node->m_bounds.minf(), node->m_bounds.maxf() );
if ( !node->isLeaf() ) {
++num_bvh_nodes;
bvh_node.offset_left = build2 ( node->getChildNode(0) ); // pLeft
++num_bvh_nodes;
bvh_node.offset_right = build2 ( node->getChildNode(1) ); // pRight
} else {
//tri_indices.resize( tri_indices.size() + node->tris.size() );
//for ( int i=0; i<node->tris.size(); ++i )
// tri_indices.push_back( node->tris[i] );
FW::LeafNode * pLeaf = (FW::LeafNode*)node;
bvh_node.offset_tris = pLeaf->m_lo; // tri_index;
bvh_node.num_tris = pLeaf->getNumTriangles(); // tris.size();
//tri_index += node->tris.size();
}
bvh_nodes[ curr_bvh_node_index ] = bvh_node;
return curr_bvh_node_index;
} ;
};