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

square.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 "square.h" // include first
00024 #include "common/math/point3.h"
00025 #include "common/math/real.h"
00026 #include "common/math/vector3.h"
00027 #include "context.h"
00028 #include "hit.h"
00029 #include "isurfaceshader.h"
00030 #include "ray.h"
00031 #include "scene.h"
00032 #include "settings.h"
00033 #include "statistics.h"
00034 #include "utilities.h"
00035 
00036 #include <cmath>
00037 
00038 using namespace sheep;
00039 using namespace std;
00040 using namespace toxic;
00041 
00042 Square::Square(const Matrix4 &m,
00043                const ISurfaceShader *surface_shader,
00044                IntersectionMask intersection_mask /*= INTERSECT_ALL_RAYS*/) :
00045     IAreaLight(m, surface_shader, intersection_mask),
00046     m_center(TransformToWorld(Point3(0.0))),
00047     m_normal(TransformNormalToWorld(Vector3(0.0, 1.0, 0.0))),
00048     m_basis(m_normal),
00049     m_ex(TransformToWorld(Vector3(1.0, 0.0, 0.0))),
00050     m_ez(TransformToWorld(Vector3(0.0, 0.0, 1.0)))
00051 {
00052     m_aabb = AABB3(Point3(-0.5, 0.0, -0.5), Point3(0.5, 0.0, 0.5)).Transform(m);
00053 
00054     // Enlarge the object bounding box to avoid numerical instabilities.
00055     m_aabb.Extend(OBJECT_AABB_EXTENSION);
00056 }
00057 
00058 bool Square::Intersect(const Context &context,
00059                        const Ray &ray,  //!< 'ray' is expressed in world space.
00060                        Hit *hit /*= 0*/) const
00061 {
00062     // Test whether this object intersects with this type of ray.
00063     if(!(m_intersection_mask & ray.GetType()))
00064         return false;
00065 
00066     // Note: rescaling of the intersection abscissa has been optimized out from this code.
00067 
00068     ++context.m_statistics->m_tested_intersections;
00069 
00070     const Ray r = TransformToLocal(ray);
00071 
00072     if(r.m_direction.m_y == 0.0)
00073         return false;   // the ray is parallel to the plane
00074 
00075     const Real t = - r.m_origin.m_y / r.m_direction.m_y;
00076 
00077     if(t < 0.0)
00078         return false;   // the intersection point is not on the ray
00079 
00080     const Real x = r.m_origin.m_x + t * r.m_direction.m_x;
00081 
00082     if(x < -0.5 || x > 0.5)
00083         return false;   // the intersection point is not on the square
00084 
00085     const Real z = r.m_origin.m_z + t * r.m_direction.m_z;
00086 
00087     if(z < -0.5 || z > 0.5)
00088         return false;   // the intersection point is not on the square
00089 
00090     ++context.m_statistics->m_intersections_found;
00091 
00092     if(hit) {
00093         hit->m_object = this;
00094         hit->m_abscissa = t;    // world space
00095         hit->m_geometric_normal = Vector3(0.0, 1.0, 0.0);   // object space
00096         hit->m_shading_normal = hit->m_geometric_normal;    // object space
00097         hit->m_u = 0.5 + x; // object space
00098         hit->m_v = 0.5 + z; // object space
00099     }
00100 
00101     return true;
00102 }

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