Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

plane.cpp

Go to the documentation of this file.
00001 /*
00002     toxic - A Global Illumination Renderer
00003     Copyright (C) 2003-2004 Francois Beaune
00004     Contact: http://toxicengine.sourceforge.net/
00005 
00006     This file is part of toxic.
00007 
00008     toxic is free software; you can redistribute it and/or modify
00009     it under the terms of the GNU General Public License as published by
00010     the Free Software Foundation; either version 2 of the License, or
00011     (at your option) any later version.
00012 
00013     toxic is distributed in the hope that it will be useful,
00014     but WITHOUT ANY WARRANTY; without even the implied warranty of
00015     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016     GNU General Public License for more details.
00017 
00018     You should have received a copy of the GNU General Public License
00019     along with toxic; if not, write to the Free Software
00020     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021 */
00022 
00023 #include "plane.h"  // include first
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 /*= INTERSECT_ALL_RAYS*/) :
00039     IObject(m, surface_shader, intersection_mask)
00040 {
00041 }
00042 
00043 bool Plane::Intersect(const Context &context,
00044                       const Ray &ray,   //!< 'ray' is expressed in world space.
00045                       Hit *hit /*= 0*/) const
00046 {
00047     // Test whether this object intersects with this type of ray.
00048     if(!(m_intersection_mask & ray.GetType()))
00049         return false;
00050 
00051     // Note: rescaling of the intersection abscissa has been optimized out from this code.
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         // The ray is parallel to the plane.
00068 
00069         if(oy != 0.0) {
00070             // The ray is parallel to the plane but does not lie in the plane.
00071             return false;
00072         }
00073 
00074         // The ray lies in the plane: report an intersection at abscissa 0.
00075         ++context.m_statistics->m_intersections_found;
00076 
00077         if(hit) {
00078             hit->m_object = this;
00079             hit->m_abscissa = 0.0;  // world space
00080             hit->m_geometric_normal = Vector3(0.0, 1.0, 0.0);   // object space -- normal is somewhat arbitrary in this case
00081             hit->m_shading_normal = hit->m_geometric_normal;    // object space
00082         }
00083 
00084         return true;
00085     } else {
00086         // The ray is not parallel to the plane.
00087 
00088         const Real t = - oy / dy;
00089 
00090         if(t < 0.0)
00091             return false;   // the intersection point is not on the ray
00092 
00093         // The ray intersects the plane.
00094         ++context.m_statistics->m_intersections_found;
00095 
00096         if(hit) {
00097             hit->m_object = this;
00098             hit->m_abscissa = t;    // world space
00099             hit->m_geometric_normal = Vector3(0.0, 1.0, 0.0);   // object space
00100             hit->m_shading_normal = hit->m_geometric_normal;    // object space
00101         }
00102 
00103         return true;
00104     }
00105 }

Generated on Tue May 11 01:31:51 2004 for toxic by doxygen 1.3.6