00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
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
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
00072
00073 virtual void Finalize(const Context &context);
00074
00075
00076
00077 virtual const sheep::AABB3 &GetAABB() const;
00078
00079
00080 virtual sheep::Real ComputeSurfaceArea() const;
00081
00082
00083
00084
00085 virtual void EvaluateSurface(
00086 const sheep::Point2 &input,
00087 sheep::Point3 *point,
00088 sheep::Vector3 *geometric_normal
00089 ) const;
00090
00091
00092
00093
00094
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
00111 mutable int m_mailbox_ray_id;
00112 #endif // USE_MAILBOXES
00113
00114 int m_v0, m_v1, m_v2;
00115 int m_n0, m_n1, m_n2;
00116 int m_t0, m_t1, m_t2;
00117
00118 sheep::Vector3 m_geometric_normal;
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
00146
00147
00148
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
00164
00165
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;
00187
00188 sheep::AABB3 m_aabb;
00189
00190 void finalize_geometry();
00191 };
00192
00193 #include "mesh.inl"
00194
00195 }
00196
00197 #endif // !TOXIC_RENDERER_MESH_H