erebus

annotate liberebus/src/geomobj.cc @ 6:bb006fb96f1b

forgot to add two files
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 24 May 2014 02:22:14 +0300
parents
children e2d9bf168a41
rev   line source
nuclear@4 1 #include <float.h>
nuclear@4 2 #include "geomobj.h"
nuclear@4 3
nuclear@4 4 static LambertRefl lambert;
nuclear@4 5
nuclear@4 6 GeomObject::GeomObject()
nuclear@4 7 {
nuclear@4 8 brdf = &lambert;
nuclear@4 9 }
nuclear@4 10
nuclear@4 11 ObjType GeomObject::get_type() const
nuclear@4 12 {
nuclear@4 13 return ObjType::geom;
nuclear@4 14 }
nuclear@4 15
nuclear@4 16 bool GeomObject::intersect(const Ray &ray, RayHit *hit) const
nuclear@4 17 {
nuclear@4 18 return false;
nuclear@4 19 }
nuclear@4 20
nuclear@4 21 // --- class Sphere ---
nuclear@4 22
nuclear@4 23 bool Sphere::intersect(const Ray &ray, RayHit *hit) const
nuclear@4 24 {
nuclear@4 25 // assumes center is the origin and radius is 1
nuclear@4 26 float a = dot_product(ray.dir, ray.dir);
nuclear@4 27 float b = 2.0 * dot_product(ray.dir, ray.origin);
nuclear@4 28 float c = dot_product(ray.origin, ray.origin) - 1.0;
nuclear@4 29
nuclear@4 30 float d = b * b - 4.0 * a * c;
nuclear@4 31 if(d < 1e-4) return false;
nuclear@4 32
nuclear@4 33 float sqrt_d = sqrt(d);
nuclear@4 34 float t0 = (-b + sqrt_d) / (2.0 * a);
nuclear@4 35 float t1 = (-b - sqrt_d) / (2.0 * a);
nuclear@4 36
nuclear@4 37 if(t0 < 1e-4) t0 = t1;
nuclear@4 38 if(t1 < 1e-4) t1 = t0;
nuclear@4 39 float t = t0 < t1 ? t0 : t1;
nuclear@4 40 if(t < 1e-4) return false;
nuclear@4 41
nuclear@4 42 if(hit) {
nuclear@4 43 hit->dist = t;
nuclear@4 44 hit->obj = this;
nuclear@4 45 hit->subobj = 0;
nuclear@4 46 }
nuclear@4 47 return true;
nuclear@4 48 }
nuclear@4 49
nuclear@4 50 // --- class Box ---
nuclear@4 51
nuclear@4 52 bool Box::intersect(const Ray &ray, RayHit *hit) const
nuclear@4 53 {
nuclear@4 54 return false;
nuclear@4 55 }
nuclear@4 56
nuclear@4 57 // --- class Triangle ---
nuclear@4 58
nuclear@4 59 bool Triangle::intersect(const Ray &ray, RayHit *hit) const
nuclear@4 60 {
nuclear@4 61 return false;
nuclear@4 62 }
nuclear@4 63
nuclear@4 64 // --- class Mesh ---
nuclear@4 65
nuclear@4 66 bool Mesh::intersect(const Ray &ray, RayHit *hit) const
nuclear@4 67 {
nuclear@4 68 RayHit nearest;
nuclear@4 69 nearest.dist = FLT_MAX;
nuclear@4 70
nuclear@4 71 for(size_t i=0; i<faces.size(); i++) {
nuclear@4 72 if(faces[i].intersect(ray, hit)) {
nuclear@4 73 if(!hit) return true;
nuclear@4 74 if(hit->dist < nearest.dist) {
nuclear@4 75 nearest = *hit;
nuclear@4 76 }
nuclear@4 77 }
nuclear@4 78 }
nuclear@4 79
nuclear@4 80 if(nearest.dist < FLT_MAX) {
nuclear@4 81 *hit = nearest;
nuclear@4 82 hit->subobj = hit->obj;
nuclear@4 83 hit->obj = this;
nuclear@4 84 return true;
nuclear@4 85 }
nuclear@4 86 return false;
nuclear@4 87 }