gpuray_glsl
diff src/cone.cc @ 3:297dbc5080c4
cone intersection
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 09 Nov 2014 20:13:33 +0200 |
parents | 6e3a4daf3159 |
children | 2ed3da7dc0bc |
line diff
1.1 --- a/src/cone.cc Sun Nov 09 14:39:01 2014 +0200 1.2 +++ b/src/cone.cc Sun Nov 09 20:13:33 2014 +0200 1.3 @@ -1,3 +1,20 @@ 1.4 +/* 1.5 +Simple introductory ray tracer 1.6 +Copyright (C) 2012 John Tsiombikas <nuclear@member.fsf.org> 1.7 + 1.8 +This program is free software: you can redistribute it and/or modify 1.9 +it under the terms of the GNU General Public License as published by 1.10 +the Free Software Foundation, either version 3 of the License, or 1.11 +(at your option) any later version. 1.12 + 1.13 +This program is distributed in the hope that it will be useful, 1.14 +but WITHOUT ANY WARRANTY; without even the implied warranty of 1.15 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1.16 +GNU General Public License for more details. 1.17 + 1.18 +You should have received a copy of the GNU General Public License 1.19 +along with this program. If not, see <http://www.gnu.org/licenses/>. 1.20 +*/ 1.21 #include "cone.h" 1.22 1.23 Cone::Cone() 1.24 @@ -14,7 +31,44 @@ 1.25 this->ymax = ymax; 1.26 } 1.27 1.28 -bool Cone::intersect(const Ray &ray, HitPoint *pt) const 1.29 +#define SQ(x) ((x) * (x)) 1.30 +bool Cone::intersect(const Ray &inray, HitPoint *pt) const 1.31 { 1.32 - return false; // TODO 1.33 + Ray ray = inray.transformed(inv_xform); 1.34 + 1.35 + float slope = 2.0 * angle / M_PI; 1.36 + 1.37 + float a = SQ(ray.dir.x) - SQ(slope * ray.dir.y) + SQ(ray.dir.z); 1.38 + float b = 2.0 * ray.origin.x * ray.dir.x - 2.0 * SQ(slope) * ray.origin.y * ray.dir.y + 1.39 + 2.0 * ray.origin.z * ray.dir.z; 1.40 + float c = SQ(ray.origin.x) - SQ(slope * ray.origin.y) + SQ(ray.origin.z); 1.41 + 1.42 + float discr = b * b - 4.0 * a * c; 1.43 + if(discr < 1e-4) 1.44 + return false; 1.45 + 1.46 + float sqrt_discr = sqrt(discr); 1.47 + float t0 = (-b + sqrt_discr) / (2.0 * a); 1.48 + float t1 = (-b - sqrt_discr) / (2.0 * a); 1.49 + 1.50 + if(t0 < 1e-4) 1.51 + t0 = t1; 1.52 + if(t1 < 1e-4) 1.53 + t1 = t0; 1.54 + 1.55 + float t = t0 < t1 ? t0 : t1; 1.56 + if(t < 1e-4) 1.57 + return false; 1.58 + 1.59 + // fill the HitPoint structure 1.60 + pt->obj = this; 1.61 + pt->dist = t; 1.62 + pt->pos = ray.origin + ray.dir * t; 1.63 + 1.64 + pt->normal.z = sin(angle); 1.65 + 1.66 + //pt->normal = (pt->pos - pos) / radius; TODO continue here 1.67 + 1.68 + pt->pos.transform(xform); 1.69 + pt->normal.transform(dir_xform); 1.70 }