src/scene.cc
branchpyrit
changeset 38 5d043eeb09d9
parent 36 b490093b0ac3
child 40 929aad02c5f2
equal deleted inserted replaced
37:5f954c0d34fc 38:5d043eeb09d9
   142 		}
   142 		}
   143 	}
   143 	}
   144 	return false;
   144 	return false;
   145 }
   145 }
   146 
   146 
       
   147 bool Sphere::intersect_bbox(const BBox &bbox) const
       
   148 {
       
   149 	register float dmin = 0;
       
   150 	for (int i = 0; i < 3; i++)
       
   151 	{
       
   152 		if (center[i] < bbox.L[i])
       
   153 			dmin += (center[i] - bbox.L[i])*(center[i] - bbox.L[i]);
       
   154 		else
       
   155 		if (center[i] > bbox.H[i])
       
   156 			dmin += (center[i] - bbox.H[i])*(center[i] - bbox.H[i]);
       
   157 	}
       
   158 	if (dmin <= sqr_radius)
       
   159 		return true;
       
   160 	return false;
       
   161 };
       
   162 
   147 BBox Sphere::get_bbox() const
   163 BBox Sphere::get_bbox() const
   148 {
   164 {
   149 	BBox bbox = BBox();
   165 	return BBox(center - radius, center + radius);
   150 	bbox.L = center - radius;
       
   151 	bbox.H = center + radius;
       
   152 	return bbox;
       
   153 }
   166 }
   154 
   167 
   155 bool Box::intersect(const Ray &ray, Float &dist) const
   168 bool Box::intersect(const Ray &ray, Float &dist) const
   156 {
   169 {
   157 	Float a,b;
   170 	Float a,b;
   158 	bool res = get_bbox().intersect(ray, a, b);
   171 	if (get_bbox().intersect(ray, a, b) && a < dist)
   159 	if (res && a < dist)
       
   160 	{
   172 	{
   161 		dist = a;
   173 		dist = a;
   162 		return true;
   174 		return true;
   163 	}
   175 	}
   164 	else
   176 	else
   165 		return false;
   177 		return false;
       
   178 }
       
   179 
       
   180 bool Box::intersect_bbox(const BBox &bbox) const
       
   181 {
       
   182 	return (
       
   183 	H.x > bbox.L.x && L.x < bbox.H.x &&
       
   184 	H.y > bbox.L.y && L.y < bbox.H.y &&
       
   185 	H.z > bbox.L.z && L.z < bbox.H.z);
   166 }
   186 }
   167 
   187 
   168 const Vector3 Box::normal(const Vector3 &P) const
   188 const Vector3 Box::normal(const Vector3 &P) const
   169 {
   189 {
   170 	Vector3 N;
   190 	Vector3 N;