Skip to content

Commit 2e72122

Browse files
committed
Wrote a basic KP BVH implementation.
1 parent ca8d961 commit 2e72122

14 files changed

+765
-79
lines changed

Diff for: Makefile_windows

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ CLFLAGS = -I"D:\usr\dev\include" -I"C:\Program Files (x86)\AMD APP SDK\2.9\inclu
66
LFLAGS = -lpthread -lOpenCL -lmingw32 -lSOIL -lglfw3 -lglu32 -lopengl32 -lgdi32
77

88
TARG = LemmingsRT.exe
9-
OBJS = main.o clstuff.o gfx_glfw.o control_glfw.o geometry.o shaders.o oglstuff.o clutil.o texture.o host_tracer.o menu.o debug.o
10-
includes = $(wildcard *.h)
9+
OBJS = main.o clstuff.o gfx_glfw.o control_glfw.o geometry.o shaders.o oglstuff.o clutil.o texture.o host_tracer.o menu.o debug.o bvh.o
10+
includes = $(wildcard *.h) intersectors.inc
1111

1212

1313
all : $(TARG)

Diff for: bvh.cpp

+131
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
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+
}

Diff for: bvh.h

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#include <iostream>
2+
#include <utility>
3+
#include <vector>
4+
#include "common.h"
5+
#include "geometry.h"
6+
7+
namespace BVH
8+
{
9+
10+
struct Primdata
11+
{
12+
int type;
13+
int index;
14+
int shader;
15+
int shaderIndex;
16+
AAB_t bound;
17+
};
18+
19+
typedef std::vector< Primdata > Primitives;
20+
21+
class BVHNode
22+
{
23+
public:
24+
typedef shared_ptr< BVHNode > pointer;
25+
AAB_t bound(){ return m_Data.bound; }
26+
virtual void write( int* ints, float* floats ){}
27+
28+
protected:
29+
Primdata m_Data;
30+
};
31+
32+
class BVH
33+
{
34+
public:
35+
typedef shared_ptr< BVH > pointer;
36+
BVHNode::pointer root;
37+
virtual void write( int* ints, float* floats );
38+
39+
protected:
40+
BVH(){};
41+
};
42+
43+
class KP : public BVH
44+
{
45+
public:
46+
typedef shared_ptr< KP > pointer;
47+
static pointer build( Primitives );
48+
int size(){ return m_Size; }
49+
50+
private:
51+
int m_Size;
52+
KP( Primitives );
53+
};
54+
55+
class KPNode : public BVHNode
56+
{
57+
public:
58+
enum Axis{ X_Axis, Y_Axis, Z_Axis };
59+
typedef shared_ptr< KPNode > pointer;
60+
KPNode( Primitives, Axis axis, int& id, int m_FailureLink );
61+
int id(){ return m_Id; }
62+
virtual void write( int* ints, float* floats );
63+
64+
private:
65+
bool m_Leaf;
66+
int m_Id;
67+
int m_FailureLink;
68+
pointer m_First;
69+
pointer m_Second;
70+
};
71+
72+
}

Diff for: clbuffer.spec

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/***********************************************
2+
*
3+
* Specification of the various buffers used in raytracer.cl
4+
*
5+
* All buffers contain only base types such as int and float
6+
* The specification explains the member data for each buffer
7+
* The buffers contain these members grouped linearly as if they were structs as described below
8+
*
9+
***********************************************/
10+
11+
12+
BVH_FLOATS =
13+
{
14+
AAB_s
15+
};
16+
17+
BVH_INTS =
18+
{
19+
// Primitive type enum, negative for internal nodes
20+
type,
21+
// Index of the primitive, not set for internal nodes
22+
index,
23+
// Shader used by the primitive, not set for internal nodes
24+
shader,
25+
// Index to shader data used by the primitive, not set for internal nodes
26+
shader index
27+
// Index to the next bvh node to examine (if no failure)
28+
next,
29+
// Index to the next bvh node to examine (if failure)
30+
failure link
31+
}
32+
33+
if( ! m_Leaf )
34+
{
35+
ints[ m_Id*4 + 0 ] = -1;
36+
ints[ m_Id*4 + 4 ] = m_Second->id();
37+
ints[ m_Id*4 + 5 ] = m_FailureLink;
38+
m_First->write( ints, floats );
39+
m_Second->write( ints, floats );
40+
}
41+
else
42+
{
43+
ints[ m_Id*4 + 0 ] = m_Data.type;
44+
ints[ m_Id*4 + 1 ] = m_Data.index;
45+
ints[ m_Id*4 + 2 ] = m_Data.shader;
46+
ints[ m_Id*4 + 3 ] = m_Data.shaderIndex;
47+
ints[ m_Id*4 + 4 ] = m_FailureLink;
48+
ints[ m_Id*4 + 5 ] = m_FailureLink;
49+
}

0 commit comments

Comments
 (0)