gpuray_glsl

diff src/plane.cc @ 0:f234630e38ff

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 09 Nov 2014 13:03:36 +0200
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/plane.cc	Sun Nov 09 13:03:36 2014 +0200
     1.3 @@ -0,0 +1,45 @@
     1.4 +#include "plane.h"
     1.5 +
     1.6 +Plane::Plane()
     1.7 +	: normal(0.0, 1.0, 0.0)
     1.8 +{
     1.9 +	dist = 0.0;
    1.10 +}
    1.11 +
    1.12 +Plane::Plane(const Vector3 &norm, float dist)
    1.13 +	: normal(norm)
    1.14 +{
    1.15 +	normal.normalize();
    1.16 +	this->dist = dist;
    1.17 +}
    1.18 +
    1.19 +bool Plane::intersect(const Ray &inray, HitPoint *pt) const
    1.20 +{
    1.21 +	Ray ray = inray.transformed(inv_xform);
    1.22 +
    1.23 +	float ndotdir = dot_product(normal, ray.dir);
    1.24 +	if(fabs(ndotdir) < 1e-5) {
    1.25 +		return false;	// ray parallel to the plane
    1.26 +	}
    1.27 +
    1.28 +	Vector3 planept = normal * dist;
    1.29 +	Vector3 pptdir = planept - ray.origin;
    1.30 +
    1.31 +	float t = dot_product(normal, pptdir) / ndotdir;
    1.32 +	if(t < 1e-4) {
    1.33 +		return false;	// discard intersections behind the origin
    1.34 +	}
    1.35 +
    1.36 +	// fill the HitPoint structure
    1.37 +	pt->obj = this;
    1.38 +	pt->dist = t;
    1.39 +
    1.40 +	pt->pos = ray.origin + ray.dir * t;
    1.41 +
    1.42 +	pt->texcoord.x = pt->pos.x;
    1.43 +	pt->texcoord.y = pt->pos.z;
    1.44 +
    1.45 +	pt->pos.transform(xform);
    1.46 +	pt->normal = normal.transformed(dir_xform);//.normalized();
    1.47 +	return true;
    1.48 +}