# HG changeset patch # User John Tsiombikas # Date 1415556813 -7200 # Node ID 297dbc5080c47d80a0a1a7119621f836e8f55a6b # Parent 6e3a4daf3159bf9544e40772f2c12eddcdbb3abd cone intersection diff -r 6e3a4daf3159 -r 297dbc5080c4 src/cone.cc --- a/src/cone.cc Sun Nov 09 14:39:01 2014 +0200 +++ b/src/cone.cc Sun Nov 09 20:13:33 2014 +0200 @@ -1,3 +1,20 @@ +/* +Simple introductory ray tracer +Copyright (C) 2012 John Tsiombikas + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ #include "cone.h" Cone::Cone() @@ -14,7 +31,44 @@ this->ymax = ymax; } -bool Cone::intersect(const Ray &ray, HitPoint *pt) const +#define SQ(x) ((x) * (x)) +bool Cone::intersect(const Ray &inray, HitPoint *pt) const { - return false; // TODO + Ray ray = inray.transformed(inv_xform); + + float slope = 2.0 * angle / M_PI; + + float a = SQ(ray.dir.x) - SQ(slope * ray.dir.y) + SQ(ray.dir.z); + float b = 2.0 * ray.origin.x * ray.dir.x - 2.0 * SQ(slope) * ray.origin.y * ray.dir.y + + 2.0 * ray.origin.z * ray.dir.z; + float c = SQ(ray.origin.x) - SQ(slope * ray.origin.y) + SQ(ray.origin.z); + + float discr = b * b - 4.0 * a * c; + if(discr < 1e-4) + return false; + + float sqrt_discr = sqrt(discr); + float t0 = (-b + sqrt_discr) / (2.0 * a); + float t1 = (-b - sqrt_discr) / (2.0 * a); + + if(t0 < 1e-4) + t0 = t1; + if(t1 < 1e-4) + t1 = t0; + + float t = t0 < t1 ? t0 : t1; + if(t < 1e-4) + return false; + + // fill the HitPoint structure + pt->obj = this; + pt->dist = t; + pt->pos = ray.origin + ray.dir * t; + + pt->normal.z = sin(angle); + + //pt->normal = (pt->pos - pos) / radius; TODO continue here + + pt->pos.transform(xform); + pt->normal.transform(dir_xform); } diff -r 6e3a4daf3159 -r 297dbc5080c4 src/cone.h --- a/src/cone.h Sun Nov 09 14:39:01 2014 +0200 +++ b/src/cone.h Sun Nov 09 20:13:33 2014 +0200 @@ -1,3 +1,20 @@ +/* +Simple introductory ray tracer +Copyright (C) 2012 John Tsiombikas + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ #ifndef CONE_H_ #define CONE_H_ diff -r 6e3a4daf3159 -r 297dbc5080c4 src/sphere.cc --- a/src/sphere.cc Sun Nov 09 14:39:01 2014 +0200 +++ b/src/sphere.cc Sun Nov 09 20:13:33 2014 +0200 @@ -1,3 +1,20 @@ +/* +Simple introductory ray tracer +Copyright (C) 2012 John Tsiombikas + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ #include #include "sphere.h"