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

mesh.h

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 #ifndef TOXIC_RENDERER_MESH_H
00024 #define TOXIC_RENDERER_MESH_H
00025 
00026 #include "common/math/aabb3.h"
00027 #include "common/math/matrix4.h"
00028 #include "common/math/octree.h"
00029 #include "common/math/point2.h"
00030 #include "common/math/point3.h"
00031 #include "common/math/real.h"
00032 #include "common/math/vector2.h"
00033 #include "common/math/vector3.h"
00034 #include "color3.h"
00035 #include "globals.h"
00036 #include "hit.h"
00037 #include "iarealight.h"
00038 
00039 #include <cassert>
00040 #include <vector>
00041 
00042 namespace toxic {
00043 
00044     class Context;
00045     class Framebuffer;
00046     class ICamera;
00047     class ISurfaceShader;
00048     class Ray;
00049     class Scene;
00050 
00051     class Mesh : public IAreaLight {
00052         friend class MeshBuilder;
00053     public:
00054         //! m is the object space to world space transformation matrix.
00055         Mesh(
00056             const sheep::Matrix4 &m,
00057             const ISurfaceShader *surface_shader,
00058             IntersectionMask intersection_mask = INTERSECT_ALL_RAYS);
00059 
00060         virtual ~Mesh();
00061 
00062         typedef unsigned int FeatureId;
00063 
00064         FeatureId AppendVertex(const sheep::Point3 &v);
00065         FeatureId AppendNormal(const sheep::Vector3 &vn);
00066         FeatureId AppendTexCoord(const sheep::Vector2 &vt);
00067         FeatureId AppendTriangle(FeatureId v0, FeatureId v1, FeatureId v2);
00068         void SetTriangleNormals(FeatureId tri, FeatureId n0, FeatureId n1, FeatureId n2);
00069         void SetTriangleTexCoords(FeatureId tri, FeatureId t0, FeatureId t1, FeatureId t2);
00070 
00071         //! This method must be called only once, after the object is completely
00072         //! configured, and before it is used for the first time.
00073         virtual void Finalize(const Context &context);
00074 
00075         //! Returns the Axis Aligned Bounding Box of this object. The AABB is
00076         //! expressed in world space.
00077         virtual const sheep::AABB3 &GetAABB() const;
00078 
00079         //! Computes the surface area of this object.
00080         virtual sheep::Real ComputeSurfaceArea() const;
00081 
00082         //! Given a point in the unit square (the set of points in [0..1]^2),
00083         //! this method computes the corresponding surface point and normal
00084         //! (both are expressed in world space).
00085         virtual void EvaluateSurface(
00086             const sheep::Point2 &input,
00087             sheep::Point3 *point,
00088             sheep::Vector3 *geometric_normal
00089         ) const;
00090 
00091         //! This method returns true if the ray intersect the object, or false otherwise.
00092         //! If 'hit' is not null, intersection data are reported through the 'hit' parameter
00093         //! (only if an intersection has been found; if no intersection has been found, the
00094         //! 'hit' parameter is left unchanged). The ray is expressed in world space.
00095         virtual bool Intersect(
00096             const Context &context,
00097             const Ray &ray,
00098             Hit *hit = 0
00099         ) const;
00100 
00101         virtual void Annotate(
00102             const Context &context,
00103             const ICamera *camera,
00104             Framebuffer *framebuffer
00105         ) const;
00106 
00107     private:
00108         struct triangle {
00109 #ifdef USE_MAILBOXES
00110             //!\todo Allocate one ray id per rendering thread.
00111             mutable int m_mailbox_ray_id;   //!< Id of the last ray tested against this triangle.
00112 #endif  // USE_MAILBOXES
00113 
00114             int m_v0, m_v1, m_v2;       //!< Vertex indices.
00115             int m_n0, m_n1, m_n2;       //!< Vertex normal indices.
00116             int m_t0, m_t1, m_t2;       //!< Texture coordinate indices.
00117 
00118             sheep::Vector3 m_geometric_normal;  //!< Geometric normal to the triangle surface (unit-length) expressed in object space.
00119 
00120             triangle();
00121         };
00122 
00123         typedef std::vector<sheep::Point3> vertex_vector;
00124         typedef std::vector<sheep::Vector3> normal_vector;
00125         typedef std::vector<sheep::Vector2> texcoord_vector;
00126         typedef std::vector<triangle> triangle_vector;
00127 
00128         typedef vertex_vector::iterator vertex_vector_it;
00129         typedef triangle_vector::iterator triangle_vector_it;
00130         typedef vertex_vector::const_iterator vertex_vector_const_it;
00131         typedef triangle_vector::const_iterator triangle_vector_const_it;
00132 
00133         typedef sheep::Octree<const triangle *> octree;
00134         typedef sheep::OctreeNode<const triangle *> octree_node;
00135 
00136         struct triangle_voxel_intersector {
00137             const vertex_vector &m_vertices;
00138 
00139             triangle_voxel_intersector(const vertex_vector &vertices) :
00140                 m_vertices(vertices) {}
00141 
00142             bool Intersect(const triangle *tri, const sheep::AABB3 &voxel) const;
00143         };
00144 
00145         //! This visitor is invoked by the Mesh::Intersect() method when the hit
00146         //! parameter is 0 (null). It is aimed at testing whether the ray touches
00147         //! an object in the octree or not. It does not search for the nearest
00148         //! object and it does not compute the intersection distance.
00149         struct octree_test_visitor {
00150             const vertex_vector &m_vertices;
00151             const Context &m_context;
00152             const Ray &m_ray;
00153             bool m_found;
00154 
00155             octree_test_visitor(
00156                 const vertex_vector &vertices,
00157                 const Context &context,
00158                 const Ray &ray);
00159 
00160             bool Trace(const octree_node *node, sheep::Real t0, sheep::Real t1);
00161         };
00162 
00163         //! This visitor is invoked by the Mesh::Intersect() method when the hit
00164         //! parameter is not null. It searches for the nearest intersected object
00165         //! and reports the intersection distance.
00166         struct octree_full_visitor {
00167             const vertex_vector &m_vertices;
00168             const Context &m_context;
00169             const Ray &m_ray;
00170             sheep::Real m_t, m_u, m_v;
00171             const triangle *m_triangle;
00172 
00173             octree_full_visitor(
00174                 const vertex_vector &vertices,
00175                 const Context &context,
00176                 const Ray &ray);
00177 
00178             bool Trace(const octree_node *node, sheep::Real t0, sheep::Real t1);
00179         };
00180 
00181         vertex_vector m_vertices;
00182         normal_vector m_normals;
00183         texcoord_vector m_texcoords;
00184         triangle_vector m_triangles;
00185 
00186         octree *m_octree;       //!< Used only if the mesh contains enough triangles.
00187 
00188         sheep::AABB3 m_aabb;    //!< Bounding box of the object, expressed in world space.
00189 
00190         void finalize_geometry();
00191     };
00192 
00193 #include "mesh.inl"
00194 
00195 }
00196 
00197 #endif  // !TOXIC_RENDERER_MESH_H

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