1
+ #include < algorithm>
2
+ #include < stdio.h>
3
+ #include " bvh.h"
4
+
5
+
6
+ namespace BVH
7
+ {
8
+ KP::KP ( Primitives primitives )
9
+ : m_Size(0 )
10
+ {
11
+ root = KPNode::pointer ( new KPNode ( primitives, KPNode::X_Axis, m_Size, -1 ) );
12
+ }
13
+
14
+ KP::pointer KP::build ( Primitives primitives )
15
+ {
16
+ return pointer ( new KP ( primitives ) );
17
+ }
18
+
19
+ static bool compareX ( Primdata i, Primdata j )
20
+ {
21
+ return ( i.bound .min ().x + i.bound .max ().x ) / 2 < ( j.bound .min ().x + j.bound .max ().x ) / 2 ;
22
+ }
23
+ static bool compareY ( Primdata i, Primdata j )
24
+ {
25
+ return ( i.bound .min ().y + i.bound .max ().y ) / 2 < ( j.bound .min ().y + j.bound .max ().y ) / 2 ;
26
+ }
27
+ static bool compareZ ( Primdata i, Primdata j )
28
+ {
29
+ return ( i.bound .min ().z + i.bound .max ().z ) / 2 < ( j.bound .min ().z + j.bound .max ().z ) / 2 ;
30
+ }
31
+
32
+
33
+ KPNode::KPNode ( Primitives primitives, Axis axis, int & id, int failureLink )
34
+ : m_Id(id), m_FailureLink(failureLink)
35
+ {
36
+ id++;
37
+ if ( !primitives.size () )
38
+ {
39
+ // Should not happen
40
+ return ;
41
+ }
42
+
43
+ if ( primitives.size () == 1 )
44
+ {
45
+ m_Data = primitives[0 ];
46
+ m_Leaf = true ;
47
+ return ;
48
+ }
49
+
50
+ m_Leaf = false ;
51
+ Axis nextAxis;
52
+
53
+ switch (axis)
54
+ {
55
+ case X_Axis:
56
+ std::sort ( primitives.begin (), primitives.end (), compareX );
57
+ nextAxis = Y_Axis;
58
+ break ;
59
+
60
+ case Y_Axis:
61
+ std::sort ( primitives.begin (), primitives.end (), compareY );
62
+ nextAxis = Z_Axis;
63
+ break ;
64
+
65
+ case Z_Axis:
66
+ std::sort ( primitives.begin (), primitives.end (), compareZ );
67
+ nextAxis = X_Axis;
68
+ break ;
69
+ }
70
+
71
+ m_Data = primitives[0 ];
72
+
73
+ for ( Primdata& data: primitives )
74
+ {
75
+ m_Data.bound = m_Data.bound .merge ( data.bound );
76
+ }
77
+
78
+ m_First = pointer ( new KPNode ( Primitives ( primitives.begin (), primitives.begin () + primitives.size ()/2 ), nextAxis, id, m_FailureLink ) );
79
+ m_Second = pointer ( new KPNode ( Primitives ( primitives.begin () + primitives.size ()/2 , primitives.end () ), nextAxis, id, m_First->id () ) );
80
+
81
+ /*
82
+ fprintf( stdout, "Buildt[%d] < %.4g, %.4g, %.4g > , < %.4g, %.4g, %.4g >\n", m_Id,
83
+ m_Data.bound.min().x, m_Data.bound.min().y, m_Data.bound.min().z, m_Data.bound.max().x, m_Data.bound.max().y, m_Data.bound.max().z );
84
+ fprintf( stdout, "\ttype: %d\n", m_Data.type );
85
+ fprintf( stdout, "\tindex: %d\n", m_Data.index );
86
+ fprintf( stdout, "\tshader: %d\n", m_Data.shader );
87
+ fprintf( stdout, "\tshaderIndex: %d\n", m_Data.shaderIndex );
88
+ if( !m_Leaf ) fprintf( stdout, "\tnext: %d\n", m_Second->id() );
89
+ fprintf( stdout, "\tfailureLink: %d\n", failureLink );
90
+ */
91
+ }
92
+
93
+ void BVH::write ( int * ints, float * floats )
94
+ {
95
+ root->write ( ints, floats );
96
+
97
+ }
98
+
99
+ void KPNode::write ( int * ints, float * floats )
100
+ {
101
+
102
+ // fprintf( stdout, "[ %d ] < %.4g, %.4g, %.4g > , < %.4g, %.4g, %.4g >\n", m_Id,
103
+ // m_Data.bound.min().x, m_Data.bound.min().y, m_Data.bound.min().z, m_Data.bound.max().x, m_Data.bound.max().y, m_Data.bound.max().z );
104
+
105
+ floats[ m_Id*6 + 0 ] = m_Data.bound .min ().x ;
106
+ floats[ m_Id*6 + 1 ] = m_Data.bound .min ().y ;
107
+ floats[ m_Id*6 + 2 ] = m_Data.bound .min ().z ;
108
+ floats[ m_Id*6 + 3 ] = m_Data.bound .max ().x ;
109
+ floats[ m_Id*6 + 4 ] = m_Data.bound .max ().y ;
110
+ floats[ m_Id*6 + 5 ] = m_Data.bound .max ().z ;
111
+
112
+ if ( ! m_Leaf )
113
+ {
114
+ ints[ m_Id*6 + 0 ] = -1 ;
115
+ ints[ m_Id*6 + 4 ] = m_Second->id ();
116
+ ints[ m_Id*6 + 5 ] = m_FailureLink;
117
+ m_First->write ( ints, floats );
118
+ m_Second->write ( ints, floats );
119
+ }
120
+ else
121
+ {
122
+ ints[ m_Id*6 + 0 ] = m_Data.type ;
123
+ ints[ m_Id*6 + 1 ] = m_Data.index ;
124
+ ints[ m_Id*6 + 2 ] = m_Data.shader ;
125
+ ints[ m_Id*6 + 3 ] = m_Data.shaderIndex ;
126
+ ints[ m_Id*6 + 4 ] = m_FailureLink;
127
+ ints[ m_Id*6 + 5 ] = m_FailureLink;
128
+ }
129
+ }
130
+
131
+ }
0 commit comments