gpuray_glsl

view src/box.cc @ 5:000017955721

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