src/scene.cc
branchpyrit
changeset 56 d4481fc43952
parent 53 228cb8bfdd54
child 69 303583d2fb97
equal deleted inserted replaced
55:f6d75ae82c88 56:d4481fc43952
   112 }
   112 }
   113 
   113 
   114 bool Sphere::intersect(const Ray &ray, Float &dist) const
   114 bool Sphere::intersect(const Ray &ray, Float &dist) const
   115 {
   115 {
   116 	Vector3 V = ray.o - center;
   116 	Vector3 V = ray.o - center;
   117 	register Float d = dot(V, ray.dir);
   117 	register Float d = -dot(V, ray.dir);
   118 	register Float Det = d * d - (dot(V,V) - sqr_radius);
   118 	register Float Det = d * d - (dot(V,V) - sqr_radius);
       
   119 	register Float t1,t2;
   119 	if (Det > 0) {
   120 	if (Det > 0) {
   120 		d = -d - sqrtf(Det);
   121 		Det = sqrtf(Det);
   121 		if (d > 0 && d < dist)
   122 		t1 = d - Det;
   122 		{
   123 		t2 = d + Det;
   123 			dist = d;
   124 		if (t1 > 0)
       
   125 		{
       
   126 			if (t1 < dist)
       
   127 			{
       
   128 				dist = t1;
       
   129 				return true;
       
   130 			}
       
   131 		}
       
   132 		else if (t2 > 0 && t2 < dist)
       
   133 		{
       
   134 			dist = t2;
   124 			return true;
   135 			return true;
   125 		}
   136 		}
   126 	}
   137 	}
   127 	return false;
   138 	return false;
   128 }
   139 }