@@ -23,20 +23,24 @@ inline bool HitTest_Sphere(const Sphere& sphere, const Ray& ray, HitRecord& hitR
2323	const  float  radius2{ sphere.radius  * sphere.radius  };
2424	if  (d2 > radius2)
2525		return  false ;
26- 	if  (not  ignoreHitRecord)
26+ 	const  float  thc{ sqrt (radius2 - d2) }; //  distance from ray to sphere surface
27+ 	float  t0{ tca - thc }; //  distance from ray's origin to sphere surface
28+ 	float  t1{ tca + thc }; //  distance from ray's origin to sphere surface
29+ 	// if (t0 > t1)
30+ 	// 	std::swap(t0, t1);
31+ 	if  (t0 > ray.min  and  t0 < ray.max )
2732	{
28- 		const  float  thc{ sqrt (radius2 - d2) }; //  distance from ray to sphere surface
29- 		float  t0{ tca - thc }; //  distance from ray's origin to sphere surface
30- 		float  t1{ tca + thc }; //  distance from ray's origin to sphere surface
31- 		// if (t0 > t1)
32- 		// 	std::swap(t0, t1);
33- 		hitRecord.didHit  = true ;
34- 		hitRecord.t  = t0;
35- 		// hitRecord.origin = ray.origin + ray.direction * hitRecord.t;
36- 		// hitRecord.normal = (hitRecord.origin - sphere.origin) / sphere.radius;
37- 		hitRecord.materialIndex  = sphere.materialIndex ;
33+ 		if  (not  ignoreHitRecord)
34+ 		{
35+ 			hitRecord.didHit  = true ;
36+ 			hitRecord.t  = t0;
37+ 			hitRecord.origin  = ray.origin  + ray.direction  * hitRecord.t ;
38+ 			hitRecord.normal  = (hitRecord.origin  - sphere.origin ) / sphere.radius ;
39+ 			hitRecord.materialIndex  = sphere.materialIndex ;
40+ 		}
41+ 		return  true ;
3842	}
39- 	return  true ;
43+ 	return  false ;
4044}
4145
4246		inline  bool  HitTest_Sphere (const  Sphere& sphere, const  Ray& ray)
@@ -51,20 +55,18 @@ inline bool HitTest_Sphere(const Sphere& sphere, const Ray& ray, HitRecord& hitR
5155		{
5256			const  float  denom{ Vector3::Dot (plane.normal , ray.direction ) };
5357			// if (denom < 0.0f)
54- 			if  (true )
58+ 			// if (true)
5559			{
5660				const  float  t{ Vector3::Dot (plane.origin  - ray.origin , plane.normal ) / denom };
57- 				if  (t >= ray.min  and  t < ray.max )
61+ 				if  (t > ray.max  or  t < ray.min ) return  false ;
62+ 				if  (not  ignoreHitRecord)
5863				{
59- 					if  (not  ignoreHitRecord)
60- 					{
61- 						hitRecord.didHit  = true ;
62- 						hitRecord.t  = t;
63- 						hitRecord.origin  = ray.origin  + ray.direction  * hitRecord.t ;
64- 						hitRecord.normal  = plane.normal ;
65- 						hitRecord.materialIndex  = plane.materialIndex ;
66- 					}
67- 					return  true ;
64+ 					hitRecord.didHit  = true ;
65+ 					hitRecord.t  = t;
66+ 					hitRecord.origin  = ray.origin  + ray.direction  * hitRecord.t ;
67+ 					hitRecord.normal  = plane.normal ;
68+ 					hitRecord.materialIndex  = plane.materialIndex ;
69+ 				return  true ;
6870				}
6971			}
7072		}
@@ -111,9 +113,7 @@ inline bool HitTest_Sphere(const Sphere& sphere, const Ray& ray, HitRecord& hitR
111113		// Direction from target to light
112114		inline  Vector3 GetDirectionToLight (const  Light& light, const  Vector3 origin)
113115		{
114- 			// todo W3
115- 			assert (false  && " No Implemented Yet!" 
116- 			return  {};
116+ 			return  light.origin  - origin;
117117		}
118118
119119		inline  ColorRGB GetRadiance (const  Light& light, const  Vector3& target)
0 commit comments