gpuray_glsl

changeset 3:297dbc5080c4

cone intersection
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 09 Nov 2014 20:13:33 +0200
parents 6e3a4daf3159
children 2ed3da7dc0bc
files src/cone.cc src/cone.h src/sphere.cc
diffstat 3 files changed, 90 insertions(+), 2 deletions(-) [+]
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  }
     2.1 --- a/src/cone.h	Sun Nov 09 14:39:01 2014 +0200
     2.2 +++ b/src/cone.h	Sun Nov 09 20:13:33 2014 +0200
     2.3 @@ -1,3 +1,20 @@
     2.4 +/*
     2.5 +Simple introductory ray tracer
     2.6 +Copyright (C) 2012  John Tsiombikas <nuclear@member.fsf.org>
     2.7 +
     2.8 +This program is free software: you can redistribute it and/or modify
     2.9 +it under the terms of the GNU General Public License as published by
    2.10 +the Free Software Foundation, either version 3 of the License, or
    2.11 +(at your option) any later version.
    2.12 +
    2.13 +This program is distributed in the hope that it will be useful,
    2.14 +but WITHOUT ANY WARRANTY; without even the implied warranty of
    2.15 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    2.16 +GNU General Public License for more details.
    2.17 +
    2.18 +You should have received a copy of the GNU General Public License
    2.19 +along with this program.  If not, see <http://www.gnu.org/licenses/>.
    2.20 +*/
    2.21  #ifndef CONE_H_
    2.22  #define CONE_H_
    2.23  
     3.1 --- a/src/sphere.cc	Sun Nov 09 14:39:01 2014 +0200
     3.2 +++ b/src/sphere.cc	Sun Nov 09 20:13:33 2014 +0200
     3.3 @@ -1,3 +1,20 @@
     3.4 +/*
     3.5 +Simple introductory ray tracer
     3.6 +Copyright (C) 2012  John Tsiombikas <nuclear@member.fsf.org>
     3.7 +
     3.8 +This program is free software: you can redistribute it and/or modify
     3.9 +it under the terms of the GNU General Public License as published by
    3.10 +the Free Software Foundation, either version 3 of the License, or
    3.11 +(at your option) any later version.
    3.12 +
    3.13 +This program is distributed in the hope that it will be useful,
    3.14 +but WITHOUT ANY WARRANTY; without even the implied warranty of
    3.15 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    3.16 +GNU General Public License for more details.
    3.17 +
    3.18 +You should have received a copy of the GNU General Public License
    3.19 +along with this program.  If not, see <http://www.gnu.org/licenses/>.
    3.20 +*/
    3.21  #include <stdio.h>
    3.22  #include "sphere.h"
    3.23