forked from hkust-psan1/psan1-rendering-engine
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sphere.cu
112 lines (90 loc) · 3.13 KB
/
sphere.cu
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
/*
* Copyright (c) 2008 - 2009 NVIDIA Corporation. All rights reserved.
*
* NVIDIA Corporation and its licensors retain all intellectual property and proprietary
* rights in and to this software, related documentation and any modifications thereto.
* Any use, reproduction, disclosure or distribution of this software and related
* documentation without an express license agreement from NVIDIA Corporation is strictly
* prohibited.
*
* TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, THIS SOFTWARE IS PROVIDED *AS IS*
* AND NVIDIA AND ITS SUPPLIERS DISCLAIM ALL WARRANTIES, EITHER EXPRESS OR IMPLIED,
* INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE. IN NO EVENT SHALL NVIDIA OR ITS SUPPLIERS BE LIABLE FOR ANY
* SPECIAL, INCIDENTAL, INDIRECT, OR CONSEQUENTIAL DAMAGES WHATSOEVER (INCLUDING, WITHOUT
* LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS, BUSINESS INTERRUPTION, LOSS OF
* BUSINESS INFORMATION, OR ANY OTHER PECUNIARY LOSS) ARISING OUT OF THE USE OF OR
* INABILITY TO USE THIS SOFTWARE, EVEN IF NVIDIA HAS BEEN ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGES
*/
#include <optix_world.h>
using namespace optix;
rtDeclareVariable(float4, sphere, , );
rtDeclareVariable(float3, geometric_normal, attribute geometric_normal, );
rtDeclareVariable(float3, shading_normal, attribute shading_normal, );
rtDeclareVariable(optix::Ray, ray, rtCurrentRay, );
template<bool use_robust_method>
static __device__
void intersect_sphere(void)
{
float3 center = make_float3(sphere);
float3 O = ray.origin - center;
float3 D = ray.direction;
float radius = sphere.w;
float b = dot(O, D);
float c = dot(O, O)-radius*radius;
float disc = b*b-c;
if(disc > 0.0f){
float sdisc = sqrtf(disc);
float root1 = (-b - sdisc);
bool do_refine = false;
float root11 = 0.0f;
if(use_robust_method && fabsf(root1) > 10.f * radius) {
do_refine = true;
}
if(do_refine) {
// refine root1
float3 O1 = O + root1 * ray.direction;
b = dot(O1, D);
c = dot(O1, O1) - radius*radius;
disc = b*b - c;
if(disc > 0.0f) {
sdisc = sqrtf(disc);
root11 = (-b - sdisc);
}
}
bool check_second = true;
if( rtPotentialIntersection( root1 + root11 ) ) {
shading_normal = geometric_normal = (O + (root1 + root11)*D)/radius;
if(rtReportIntersection(0))
check_second = false;
}
if(check_second) {
float root2 = (-b + sdisc) + (do_refine ? root1 : 0);
if( rtPotentialIntersection( root2 ) ) {
shading_normal = geometric_normal = (O + root2*D)/radius;
rtReportIntersection(0);
}
}
}
}
RT_PROGRAM void intersect(int primIdx)
{
intersect_sphere<false>();
}
RT_PROGRAM void robust_intersect(int primIdx)
{
intersect_sphere<true>();
}
RT_PROGRAM void bounds (int, float result[6])
{
const float3 cen = make_float3( sphere );
const float3 rad = make_float3( sphere.w );
optix::Aabb* aabb = (optix::Aabb*)result;
if( rad.x > 0.0f && !isinf(rad.x) ) {
aabb->m_min = cen - rad;
aabb->m_max = cen + rad;
} else {
aabb->invalidate();
}
}