-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintersect.cl
166 lines (143 loc) · 3.92 KB
/
intersect.cl
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
static Result intersect(
rt_context context,
Ray ray
)
{
// RAY TRACING
Result closest = MISS;
closest.t = RT_DEFAULT_MAX+1.f;
Result nyligst;
//printi( nobjects );
for( int i = 0; i < context.nobjects; i++ )
{
__global int* p = &context.primitives[ context.geometry[ i*4 ] ];
int nprim = context.geometry[ i*4 + 1];
for( int j = 0; j < nprim; j++ )
{
int type = p[ j*2 ];
int index = p[ j*2 + 1 ];
switch( type )
{
case Sphere_t:
{
Sphere sphere = { context.sphere_centers[index], context.sphere_radi[index] };
nyligst = intersect_sphere( ray, sphere );
break;
}
case Triangle_t:
nyligst = intersect_triangle( ray, context.triangles[index*3], context.triangles[index*3+1], context.triangles[index*3+2] );
break;
case AAB_t:
nyligst = intersect_AAB( ray, context.AABs[ index*2 ], context.AABs[ index*2 + 1 ] );
break;
case Box_t:
{
Box box = {
context.boxes[ index*3 + 0 ],
context.boxes[ index*3 + 1 ],
context.boxes[ index*3 + 2 ],
context.box_heights[ index ]
};
nyligst = intersect_box( ray, box );
break;
}
}
if( nyligst.t > RT_DEFAULT_MIN && nyligst.t < closest.t )
{
closest = nyligst;
closest.geo = i;
ifprint("REGULAR New closest at <%d, %d, %d>\n", type, index, i);
}
}
}
return closest;
}
static Result bvhintersect(
rt_context context,
Ray ray
)
{
// RAY TRACING
Result closest = MISS;
closest.t = RT_DEFAULT_MAX+1.f;
Result nyligst;
//printi( nobjects );
int id = 0;
while( id >= 0 )
{
ifprint("\nEntering new id[%d]\n", id);
int type = context.bvh_ints[ id*6 + 0 ];
int index = context.bvh_ints[ id*6 + 1 ];
int geo = context.bvh_ints[ id*6 + 2 ];
int next = context.bvh_ints[ id*6 + 3 ];
int failureLink = context.bvh_ints[ id*6 + 4 ];
ifprint("type: %d\n", type);
ifprint("index: %d\n", index);
ifprint("geo: %d\n", geo);
ifprint("next: %d\n", next);
ifprint("failureLink: %d\n", failureLink);
float t = bound(
ray,
make_float3( context.bvh_floats[ id*6 + 0 ], context.bvh_floats[ id*6 + 1 ], context.bvh_floats[ id*6 + 2 ] ),
make_float3( context.bvh_floats[ id*6 + 3 ], context.bvh_floats[ id*6 + 4 ], context.bvh_floats[ id*6 + 5 ] )
);
float3 bmin = make_float3( context.bvh_floats[ id*6 + 0 ], context.bvh_floats[ id*6 + 1 ], context.bvh_floats[ id*6 + 2 ] );
float3 bmax = make_float3( context.bvh_floats[ id*6 + 3 ], context.bvh_floats[ id*6 + 4 ], context.bvh_floats[ id*6 + 5 ] );
ifprint("bmin: <%.4g, %.4g, %.4g>\n", bmin.x, bmin.y, bmin.z);
ifprint("bmax: <%.4g, %.4g, %.4g>\n", bmax.x, bmax.y, bmax.z);
ifprint("t: %g\n", t);
if( t < closest.t )
{
// Node is relevant
if( type >= 0 )
{
// Leaf node
// Test the primitive
switch( type )
{
case Sphere_t:
{
Sphere sphere = { context.sphere_centers[index], context.sphere_radi[index] };
nyligst = intersect_sphere( ray, sphere );
break;
}
case Triangle_t:
nyligst = intersect_triangle( ray, context.triangles[index*3], context.triangles[index*3+1], context.triangles[index*3+2] );
break;
case AAB_t:
nyligst = intersect_AAB( ray, context.AABs[ index*2 ], context.AABs[ index*2 + 1 ] );
break;
case Box_t:
{
Box box = {
context.boxes[ index*3 + 0 ],
context.boxes[ index*3 + 1 ],
context.boxes[ index*3 + 2 ],
context.box_heights[ index ]
};
nyligst = intersect_box( ray, box );
break;
}
}
if( nyligst.t > RT_DEFAULT_MIN && nyligst.t < closest.t )
{
closest = nyligst;
closest.geo = geo;
ifprint("New closest at <%d, %d, %d>\n", type, index, geo);
}
id = next;
}
else
{
// Internal node, continue exploring the tree
id = next;
}
}
else
{
//Follow failure link
id = failureLink;
}
}
return closest;
}