00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "plane.h"
00024 #include "common/math/real.h"
00025 #include "common/math/vector3.h"
00026 #include "context.h"
00027 #include "hit.h"
00028 #include "ray.h"
00029 #include "statistics.h"
00030
00031 #include <cassert>
00032
00033 using namespace sheep;
00034 using namespace toxic;
00035
00036 Plane::Plane(const Matrix4 &m,
00037 const ISurfaceShader *surface_shader,
00038 IntersectionMask intersection_mask ) :
00039 IObject(m, surface_shader, intersection_mask)
00040 {
00041 }
00042
00043 bool Plane::Intersect(const Context &context,
00044 const Ray &ray,
00045 Hit *hit ) const
00046 {
00047
00048 if(!(m_intersection_mask & ray.GetType()))
00049 return false;
00050
00051
00052
00053 ++context.m_statistics->m_tested_intersections;
00054
00055 const Real oy =
00056 m_inv_m[4] * ray.m_origin.m_x +
00057 m_inv_m[5] * ray.m_origin.m_y +
00058 m_inv_m[6] * ray.m_origin.m_z +
00059 m_inv_m[7];
00060
00061 const Real dy =
00062 m_inv_m[4] * ray.m_direction.m_x +
00063 m_inv_m[5] * ray.m_direction.m_y +
00064 m_inv_m[6] * ray.m_direction.m_z;
00065
00066 if(dy == 0.0) {
00067
00068
00069 if(oy != 0.0) {
00070
00071 return false;
00072 }
00073
00074
00075 ++context.m_statistics->m_intersections_found;
00076
00077 if(hit) {
00078 hit->m_object = this;
00079 hit->m_abscissa = 0.0;
00080 hit->m_geometric_normal = Vector3(0.0, 1.0, 0.0);
00081 hit->m_shading_normal = hit->m_geometric_normal;
00082 }
00083
00084 return true;
00085 } else {
00086
00087
00088 const Real t = - oy / dy;
00089
00090 if(t < 0.0)
00091 return false;
00092
00093
00094 ++context.m_statistics->m_intersections_found;
00095
00096 if(hit) {
00097 hit->m_object = this;
00098 hit->m_abscissa = t;
00099 hit->m_geometric_normal = Vector3(0.0, 1.0, 0.0);
00100 hit->m_shading_normal = hit->m_geometric_normal;
00101 }
00102
00103 return true;
00104 }
00105 }