@@ -14,130 +14,6 @@ __host__ __device__ inline Ray makeOffsetedRay(glm::vec3 ori, glm::vec3 dir) {
1414 return { ori + dir * 1e-5f , dir };
1515}
1616
17- // CHECKITOUT
18- /* *
19- * Compute a point at parameter value `t` on ray `r`.
20- * Falls slightly short so that it doesn't intersect the object it's hitting.
21- */
22- __host__ __device__ static glm::vec3 getPointOnRay (Ray r, float dist) {
23- return r.origin + (dist - .0001f ) * glm::normalize (r.direction );
24- }
25-
26- /* *
27- * Multiplies a mat4 and a vec4 and returns a vec3 clipped from the vec4.
28- */
29- __host__ __device__ static glm::vec3 multiplyMV (glm::mat4 m, glm::vec4 v) {
30- return glm::vec3 (m * v);
31- }
32-
33- // CHECKITOUT
34- /* *
35- * Test intersection between a ray and a transformed cube. Untransformed,
36- * the cube ranges from -0.5 to 0.5 in each axis and is centered at the origin.
37- *
38- * @param intersectionPoint Output parameter for point of intersection.
39- * @param normal Output parameter for surface normal.
40- * @param outside Output param for whether the ray came from outside.
41- * @return Ray parameter `t` value. -1 if no intersection.
42- */
43- __host__ __device__ static float boxIntersectionTest (Geom box, Ray r,
44- glm::vec3 &intersectionPoint, glm::vec3 &normal, bool &outside) {
45- Ray q;
46- q.origin = multiplyMV (box.inverseTransform , glm::vec4 (r.origin , 1 .0f ));
47- q.direction = glm::normalize (multiplyMV (box.inverseTransform , glm::vec4 (r.direction , 0 .0f )));
48-
49- float tmin = -1e38f;
50- float tmax = 1e38f;
51- glm::vec3 tmin_n;
52- glm::vec3 tmax_n;
53- for (int xyz = 0 ; xyz < 3 ; ++xyz) {
54- float qdxyz = q.direction [xyz];
55- /* if (glm::abs(qdxyz) > 0.00001f)*/ {
56- float t1 = (-0 .5f - q.origin [xyz]) / qdxyz;
57- float t2 = (+0 .5f - q.origin [xyz]) / qdxyz;
58- float ta = glm::min (t1, t2);
59- float tb = glm::max (t1, t2);
60- glm::vec3 n;
61- n[xyz] = t2 < t1 ? +1 : -1 ;
62- if (ta > 0 && ta > tmin) {
63- tmin = ta;
64- tmin_n = n;
65- }
66- if (tb < tmax) {
67- tmax = tb;
68- tmax_n = n;
69- }
70- }
71- }
72-
73- if (tmax >= tmin && tmax > 0 ) {
74- outside = true ;
75- if (tmin <= 0 ) {
76- tmin = tmax;
77- tmin_n = tmax_n;
78- outside = false ;
79- }
80- intersectionPoint = multiplyMV (box.transform , glm::vec4 (getPointOnRay (q, tmin), 1 .0f ));
81- normal = glm::normalize (multiplyMV (box.invTranspose , glm::vec4 (tmin_n, 0 .0f )));
82- return glm::length (r.origin - intersectionPoint);
83- }
84- return -1 ;
85- }
86-
87- // CHECKITOUT
88- /* *
89- * Test intersection between a ray and a transformed sphere. Untransformed,
90- * the sphere always has radius 0.5 and is centered at the origin.
91- *
92- * @param intersectionPoint Output parameter for point of intersection.
93- * @param normal Output parameter for surface normal.
94- * @param outside Output param for whether the ray came from outside.
95- * @return Ray parameter `t` value. -1 if no intersection.
96- */
97- __host__ __device__ static float sphereIntersectionTest (Geom sphere, Ray r,
98- glm::vec3 &intersectionPoint, glm::vec3 &normal, bool &outside) {
99- float radius = .5 ;
100-
101- glm::vec3 ro = multiplyMV (sphere.inverseTransform , glm::vec4 (r.origin , 1 .0f ));
102- glm::vec3 rd = glm::normalize (multiplyMV (sphere.inverseTransform , glm::vec4 (r.direction , 0 .0f )));
103-
104- Ray rt;
105- rt.origin = ro;
106- rt.direction = rd;
107-
108- float vDotDirection = glm::dot (rt.origin , rt.direction );
109- float radicand = vDotDirection * vDotDirection - (glm::dot (rt.origin , rt.origin ) - powf (radius, 2 ));
110- if (radicand < 0 ) {
111- return -1 ;
112- }
113-
114- float squareRoot = sqrt (radicand);
115- float firstTerm = -vDotDirection;
116- float t1 = firstTerm + squareRoot;
117- float t2 = firstTerm - squareRoot;
118-
119- float dist = 0 ;
120- if (t1 < 0 && t2 < 0 ) {
121- return -1 ;
122- } else if (t1 > 0 && t2 > 0 ) {
123- dist = std::min (t1, t2);
124- outside = true ;
125- } else {
126- dist = std::max (t1, t2);
127- outside = false ;
128- }
129-
130- glm::vec3 objspaceIntersection = getPointOnRay (rt, dist);
131-
132- intersectionPoint = multiplyMV (sphere.transform , glm::vec4 (objspaceIntersection, 1 .f ));
133- normal = glm::normalize (multiplyMV (sphere.invTranspose , glm::vec4 (objspaceIntersection, 0 .f )));
134- if (!outside) {
135- normal = -normal;
136- }
137-
138- return glm::length (r.origin - intersectionPoint);
139- }
140-
14117__host__ __device__ static bool intersectTriangle (Ray ray , glm ::vec3 v0 , glm ::vec3 v1 , glm ::vec3 v2 ,
14218 glm ::vec2 & bary , float & dist
14319) {
0 commit comments