gpuray_glsl
annotate src/plane.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 |
rev | line source |
---|---|
nuclear@0 | 1 #include "plane.h" |
nuclear@0 | 2 |
nuclear@0 | 3 Plane::Plane() |
nuclear@0 | 4 : normal(0.0, 1.0, 0.0) |
nuclear@0 | 5 { |
nuclear@0 | 6 dist = 0.0; |
nuclear@0 | 7 } |
nuclear@0 | 8 |
nuclear@0 | 9 Plane::Plane(const Vector3 &norm, float dist) |
nuclear@0 | 10 : normal(norm) |
nuclear@0 | 11 { |
nuclear@0 | 12 normal.normalize(); |
nuclear@0 | 13 this->dist = dist; |
nuclear@0 | 14 } |
nuclear@0 | 15 |
nuclear@0 | 16 bool Plane::intersect(const Ray &inray, HitPoint *pt) const |
nuclear@0 | 17 { |
nuclear@0 | 18 Ray ray = inray.transformed(inv_xform); |
nuclear@0 | 19 |
nuclear@0 | 20 float ndotdir = dot_product(normal, ray.dir); |
nuclear@0 | 21 if(fabs(ndotdir) < 1e-5) { |
nuclear@0 | 22 return false; // ray parallel to the plane |
nuclear@0 | 23 } |
nuclear@0 | 24 |
nuclear@0 | 25 Vector3 planept = normal * dist; |
nuclear@0 | 26 Vector3 pptdir = planept - ray.origin; |
nuclear@0 | 27 |
nuclear@0 | 28 float t = dot_product(normal, pptdir) / ndotdir; |
nuclear@0 | 29 if(t < 1e-4) { |
nuclear@0 | 30 return false; // discard intersections behind the origin |
nuclear@0 | 31 } |
nuclear@0 | 32 |
nuclear@0 | 33 // fill the HitPoint structure |
nuclear@0 | 34 pt->obj = this; |
nuclear@0 | 35 pt->dist = t; |
nuclear@0 | 36 |
nuclear@0 | 37 pt->pos = ray.origin + ray.dir * t; |
nuclear@0 | 38 |
nuclear@0 | 39 pt->texcoord.x = pt->pos.x; |
nuclear@0 | 40 pt->texcoord.y = pt->pos.z; |
nuclear@0 | 41 |
nuclear@0 | 42 pt->pos.transform(xform); |
nuclear@0 | 43 pt->normal = normal.transformed(dir_xform);//.normalized(); |
nuclear@0 | 44 return true; |
nuclear@0 | 45 } |