-
Notifications
You must be signed in to change notification settings - Fork 0
/
MBVH.cpp
61 lines (55 loc) · 1.66 KB
/
MBVH.cpp
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
#include "MBVH.h"
#include "BVH.h"
#include <new>
//Custom allocation for friggin alignment
void* MBVHNode::operator new(size_t size){
return _aligned_malloc(size, 16);
}
void MBVHNode::operator delete(void* memory){
_aligned_free(memory);
}
MBVHNode::MBVHNode(){
for(int i=0; i<4; ++i) children[i] = nullptr;
}
MBVHNode::~MBVHNode(){
if(children[0]){
delete children[0];
delete children[1];
delete children[2];
delete children[3];
}
}
void MBVHNode::fromBvh(const class BVHNode& root){
_fill(root);
}
void MBVHNode::_fill(const BVHNode& node){
if(!node.m_Left) return;
const BVHNode* nodes[4] = {
node.m_Left->m_Left ? node.m_Left->m_Left : node.m_Left,
node.m_Left->m_Left ? node.m_Left->m_Right : nullptr,
node.m_Right->m_Left ? node.m_Right->m_Left : node.m_Right,
node.m_Right->m_Left ? node.m_Right->m_Right : nullptr
};
for(int i=0; i<4; ++i){
children[i] = new MBVHNode;
if(nodes[i]){
children[i]->primList = nodes[i]->m_PrimiveList;
children[i]->primCount = nodes[i]->m_PrimitiveCount;
reinterpret_cast<float*>(&minx4)[i] = nodes[i]->m_Bounds.min.x;
reinterpret_cast<float*>(&miny4)[i] = nodes[i]->m_Bounds.min.y;
reinterpret_cast<float*>(&minz4)[i] = nodes[i]->m_Bounds.min.z;
reinterpret_cast<float*>(&maxx4)[i] = nodes[i]->m_Bounds.max.x;
reinterpret_cast<float*>(&maxy4)[i] = nodes[i]->m_Bounds.max.y;
reinterpret_cast<float*>(&maxz4)[i] = nodes[i]->m_Bounds.max.z;
primList = nodes[i]->m_PrimiveList;
primCount = nodes[i]->m_PrimitiveCount;
if(nodes[i]->m_Left){
children[i] = new MBVHNode;
children[i]->_fill(*nodes[i]);
}
}else{
children[i]->primCount = 0;
children[i]->primList = nullptr;
}
}
}