gpuray_glsl

annotate src/box.cc @ 2:6e3a4daf3159

adding cones
author John Tsiombikas <nuclear@mutantstargoat.com>
date Sun, 09 Nov 2014 14:39:01 +0200
parents
children
rev   line source
nuclear@0 1 #include "box.h"
nuclear@0 2
nuclear@0 3 Box::Box()
nuclear@0 4 : min(-0.5, -0.5, -0.5), max(0.5, 0.5, 0.5)
nuclear@0 5 {
nuclear@0 6 }
nuclear@0 7
nuclear@0 8 Box::Box(const Vector3 &min_arg, const Vector3 &max_arg)
nuclear@0 9 : min(min_arg), max(max_arg)
nuclear@0 10 {
nuclear@0 11 }
nuclear@0 12
nuclear@0 13 bool Box::intersect(const Ray &inray, HitPoint *pt) const
nuclear@0 14 {
nuclear@0 15 Ray ray = inray.transformed(inv_xform);
nuclear@0 16
nuclear@0 17 Vector3 param[2] = {min, max};
nuclear@0 18 Vector3 inv_dir(1.0 / ray.dir.x, 1.0 / ray.dir.y, 1.0 / ray.dir.z);
nuclear@0 19 int sign[3] = {inv_dir.x < 0, inv_dir.y < 0, inv_dir.z < 0};
nuclear@0 20
nuclear@0 21 float tmin = (param[sign[0]].x - ray.origin.x) * inv_dir.x;
nuclear@0 22 float tmax = (param[1 - sign[0]].x - ray.origin.x) * inv_dir.x;
nuclear@0 23 float tymin = (param[sign[1]].y - ray.origin.y) * inv_dir.y;
nuclear@0 24 float tymax = (param[1 - sign[1]].y - ray.origin.y) * inv_dir.y;
nuclear@0 25
nuclear@0 26 pt->normal = Vector3(ray.origin.x > 0.0 ? 1 : -1, 0, 0);
nuclear@0 27
nuclear@0 28 if(tmin > tymax || tymin > tmax) {
nuclear@0 29 return false;
nuclear@0 30 }
nuclear@0 31 if(tymin > tmin) {
nuclear@0 32 pt->normal = Vector3(0, ray.origin.y > 0.0 ? 1 : -1, 0);
nuclear@0 33 tmin = tymin;
nuclear@0 34 }
nuclear@0 35 if(tymax < tmax) {
nuclear@0 36 tmax = tymax;
nuclear@0 37 }
nuclear@0 38
nuclear@0 39 float tzmin = (param[sign[2]].z - ray.origin.z) * inv_dir.z;
nuclear@0 40 float tzmax = (param[1 - sign[2]].z - ray.origin.z) * inv_dir.z;
nuclear@0 41
nuclear@0 42 if(tmin > tzmax || tzmin > tmax) {
nuclear@0 43 return false;
nuclear@0 44 }
nuclear@0 45 if(tzmin > tmin) {
nuclear@0 46 pt->normal = Vector3(0, 0, ray.origin.z > 0.0 ? 1 : -1);
nuclear@0 47 tmin = tzmin;
nuclear@0 48 }
nuclear@0 49 if(tzmax < tmax) {
nuclear@0 50 tmax = tzmax;
nuclear@0 51 }
nuclear@0 52
nuclear@0 53 float t = tmin < 1e-4 ? tmax : tmin;
nuclear@0 54 if(t >= 1e-4) {
nuclear@0 55 pt->obj = this;
nuclear@0 56 pt->dist = t;
nuclear@0 57 pt->pos = ray.origin + ray.dir * t;
nuclear@0 58 pt->pos.transform(xform);
nuclear@0 59 pt->normal.transform(dir_xform);
nuclear@0 60 return true;
nuclear@0 61 }
nuclear@0 62 return false;
nuclear@0 63 }