#include "Box.hxx"
-Box::Box() : m_min(-Infinity), m_max(Infinity)
+Box::Box() : m_min(Infinity, Infinity, Infinity),
+ m_max(-Infinity, -Infinity, -Infinity)
+{
+}
+
+Box::Box(Vec3f min, Vec3f max) : m_min(min), m_max(max)
{
}
{
public:
Box();
+ Box(Vec3f min, Vec3f max);
~Box();
Box(const Box& b);
Box& operator=(const Box& b);
-
+
//! Extend the bounding box to contain point a
void Extend(const Vec3f& a);
//! Clear the bounding box, i.e. set dimensions to infinity.
#include "Sphere.hxx"
Sphere::Sphere(const Vec3f& center, float radius, Shader* shader)
- : Primitive(shader),
+ : Primitive(shader),
m_center(center),
m_radius(radius)
{
if( B*B-4*A*C < 0 )
return false;
-
+
float root = sqrtf(B*B-4*A*C);
float t = (-B-root)/(2.0f*A);
if(t > ray.t())
return false;
-
+
if( t < Epsilon )
{
t = (-B+root)/(2.0f*A);
if( t < Epsilon || t > ray.t())
return false;
}
-
+
ray.setT(t);
ray.setHit(this);
-
+
return true;
}
Box
Sphere::CalcBounds()
{
- return Box();
+ Vec3f min(m_center[0] - m_radius/2, m_center[1] - m_radius/2,
+ m_center[2] - m_radius/2);
+ Vec3f max(m_center[0] + m_radius/2, m_center[1] + m_radius/2,
+ m_center[2] + m_radius/2);
+ return Box(min, max);
}
bool
{
const Vec3f edge1 = m_b-m_a;
const Vec3f edge2 = m_c-m_a;
-
+
const Vec3f pvec = ray.direction().cross(edge2);
-
+
const float det = edge1.dot(pvec);
if (fabs(det) < Epsilon) return false;
-
+
const float inv_det = 1.0f / det;
-
+
const Vec3f tvec = ray.origin()-m_a;
float lambda = tvec.dot( pvec );
lambda *= inv_det;
-
- if (lambda < 0.0f || lambda > 1.0f)
+
+ if (lambda < 0.0f || lambda > 1.0f)
return false;
const Vec3f qvec = tvec.cross(edge1);
float mue = ray.direction().dot(qvec);
mue *= inv_det;
-
- if (mue < 0.0f || mue+lambda > 1.0f)
+
+ if (mue < 0.0f || mue+lambda > 1.0f)
return false;
float f = edge2.dot(qvec);
f *= inv_det;
- if (ray.t() <= f || f < Epsilon )
+ if (ray.t() <= f || f < Epsilon )
return false;
-
+
ray.setT(f);
ray.setHit(this);
Box
Triangle::CalcBounds()
{
+ Box bounds;
+ bounds.Extend(m_a);
+ bounds.Extend(m_b);
+ bounds.Extend(m_c);
return Box();
}