5 GeomObject::~GeomObject()
15 Sphere::Sphere(const Vec3 ¢, float radius)
18 this->radius = radius;
21 GeomObjectType Sphere::get_type() const
26 void Sphere::set_union(const GeomObject *obj1, const GeomObject *obj2)
28 if(obj1->get_type() != GOBJ_SPHERE || obj2->get_type() != GOBJ_SPHERE) {
29 fprintf(stderr, "Sphere::set_union: arguments must be spheres");
32 const Sphere *sph1 = (const Sphere*)obj1;
33 const Sphere *sph2 = (const Sphere*)obj2;
35 float dist = length(sph1->center - sph2->center);
36 float surf_dist = dist - (sph1->radius + sph2->radius);
37 float d1 = sph1->radius + surf_dist / 2.0;
38 float d2 = sph2->radius + surf_dist / 2.0;
39 float t = d1 / (d1 + d2);
44 center = sph1->center * t + sph2->center * (1.0 - t);
45 radius = std::max(dist * t + sph2->radius, dist * (1.0f - t) + sph1->radius);
48 void Sphere::set_intersection(const GeomObject *obj1, const GeomObject *obj2)
50 fprintf(stderr, "Sphere::intersection undefined\n");
53 bool Sphere::intersect(const Ray &ray, HitPoint *hit) const
55 float a = dot(ray.dir, ray.dir);
56 float b = 2.0 * ray.dir.x * (ray.origin.x - center.x) +
57 2.0 * ray.dir.y * (ray.origin.y - center.y) +
58 2.0 * ray.dir.z * (ray.origin.z - center.z);
59 float c = dot(ray.origin, ray.origin) + dot(center, center) -
60 2.0 * dot(ray.origin, center) - radius * radius;
62 float discr = b * b - 4.0 * a * c;
67 float sqrt_discr = sqrt(discr);
68 float t0 = (-b + sqrt_discr) / (2.0 * a);
69 float t1 = (-b - sqrt_discr) / (2.0 * a);
76 float t = t0 < t1 ? t0 : t1;
81 // fill the HitPoint structure
85 hit->pos = ray.origin + ray.dir * t;
86 hit->normal = (hit->pos - center) / radius;
91 bool Sphere::contains(const Vec3 &pt) const
93 return length_sq(pt - center) <= radius * radius;
96 float Sphere::distance(const Vec3 &v) const
98 return length(v - center) - radius;
105 AABox::AABox(const Vec3 &vmin, const Vec3 &vmax)
106 : min(vmin), max(vmax)
110 GeomObjectType AABox::get_type() const
115 void AABox::set_union(const GeomObject *obj1, const GeomObject *obj2)
117 if(obj1->get_type() != GOBJ_AABOX || obj2->get_type() != GOBJ_AABOX) {
118 fprintf(stderr, "AABox::set_union: arguments must be AABoxes too\n");
121 const AABox *box1 = (const AABox*)obj1;
122 const AABox *box2 = (const AABox*)obj2;
124 min.x = std::min(box1->min.x, box2->min.x);
125 min.y = std::min(box1->min.y, box2->min.y);
126 min.z = std::min(box1->min.z, box2->min.z);
128 max.x = std::max(box1->max.x, box2->max.x);
129 max.y = std::max(box1->max.y, box2->max.y);
130 max.z = std::max(box1->max.z, box2->max.z);
133 void AABox::set_intersection(const GeomObject *obj1, const GeomObject *obj2)
135 if(obj1->get_type() != GOBJ_AABOX || obj2->get_type() != GOBJ_AABOX) {
136 fprintf(stderr, "AABox::set_intersection: arguments must be AABoxes too\n");
139 const AABox *box1 = (const AABox*)obj1;
140 const AABox *box2 = (const AABox*)obj2;
142 for(int i=0; i<3; i++) {
143 min[i] = std::max(box1->min[i], box2->min[i]);
144 max[i] = std::min(box1->max[i], box2->max[i]);
146 if(max[i] < min[i]) {
152 bool AABox::intersect(const Ray &ray, HitPoint *hit) const
154 Vec3 param[2] = {min, max};
155 Vec3 inv_dir(1.0 / ray.dir.x, 1.0 / ray.dir.y, 1.0 / ray.dir.z);
156 int sign[3] = {inv_dir.x < 0, inv_dir.y < 0, inv_dir.z < 0};
158 float tmin = (param[sign[0]].x - ray.origin.x) * inv_dir.x;
159 float tmax = (param[1 - sign[0]].x - ray.origin.x) * inv_dir.x;
160 float tymin = (param[sign[1]].y - ray.origin.y) * inv_dir.y;
161 float tymax = (param[1 - sign[1]].y - ray.origin.y) * inv_dir.y;
163 if(tmin > tymax || tymin > tmax) {
173 float tzmin = (param[sign[2]].z - ray.origin.z) * inv_dir.z;
174 float tzmax = (param[1 - sign[2]].z - ray.origin.z) * inv_dir.z;
176 if(tmin > tzmax || tzmin > tmax) {
186 float t = tmin < 1e-4 ? tmax : tmin;
192 hit->pos = ray.origin + ray.dir * t;
194 float min_dist = FLT_MAX;
195 Vec3 offs = min + (max - min) / 2.0;
196 Vec3 local_hit = hit->pos - offs;
198 static const Vec3 axis[] = {
199 Vec3(1, 0, 0), Vec3(0, 1, 0), Vec3(0, 0, 1)
201 //int tcidx[][2] = {{2, 1}, {0, 2}, {0, 1}};
203 for(int i=0; i<3; i++) {
204 float dist = fabs((max[i] - offs[i]) - fabs(local_hit[i]));
205 if(dist < min_dist) {
207 hit->normal = axis[i] * (local_hit[i] < 0.0 ? 1.0 : -1.0);
208 //hit->texcoord = Vec2(hit->pos[tcidx[i][0]], hit->pos[tcidx[i][1]]);
217 bool AABox::contains(const Vec3 &v) const
219 return v.x >= min.x && v.y >= min.y && v.z >= min.z &&
220 v.x <= max.x && v.y <= max.y && v.z <= max.z;
223 float AABox::distance(const Vec3 &v) const
230 : normal(0.0, 1.0, 0.0)
234 Plane::Plane(const Vec3 &p, const Vec3 &norm)
237 normal = normalize(norm);
240 Plane::Plane(const Vec3 &p1, const Vec3 &p2, const Vec3 &p3)
243 normal = normalize(cross(p2 - p1, p3 - p1));
246 Plane::Plane(const Vec3 &normal, float dist)
248 this->normal = normalize(normal);
249 pt = this->normal * dist;
252 GeomObjectType Plane::get_type() const
257 void Plane::set_union(const GeomObject *obj1, const GeomObject *obj2)
259 fprintf(stderr, "Plane::set_union undefined\n");
262 void Plane::set_intersection(const GeomObject *obj1, const GeomObject *obj2)
264 fprintf(stderr, "Plane::set_intersection undefined\n");
267 bool Plane::intersect(const Ray &ray, HitPoint *hit) const
269 float ndotdir = dot(normal, ray.dir);
270 if(fabs(ndotdir) < 1e-4) {
275 Vec3 ptdir = pt - ray.origin;
276 float t = dot(normal, ptdir) / ndotdir;
279 hit->pos = ray.origin + ray.dir * t;
280 hit->normal = normal;
286 bool Plane::contains(const Vec3 &v) const
288 return dot(v, normal) <= 0.0;
291 float Plane::distance(const Vec3 &v) const
293 return dot(v - pt, normal);