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_OCTREE_H
00024 #define TOXIC_RENDERER_OCTREE_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/vector3.h"
00032 #include "common/math/real.h"
00033 #include "common/misc/minmax.h"
00034 #include "globals.h"
00035 #include "hit.h"
00036 #include "iboundedobject.h"
00037
00038 #include <vector>
00039
00040 namespace toxic {
00041
00042 class Context;
00043 class Framebuffer;
00044 class ICamera;
00045 class Ray;
00046 class Scene;
00047
00048 const int DEFAULT_OCTREE_POP_THRESHOLD = 8;
00049 const int DEFAULT_OCTREE_MAX_SUBDIV = 6;
00050
00051 class Octree : public IBoundedObject {
00052 public:
00053
00054 Octree(
00055 const sheep::Matrix4 &m,
00056 int pop_threshold = DEFAULT_OCTREE_POP_THRESHOLD,
00057 int max_subdiv = DEFAULT_OCTREE_MAX_SUBDIV,
00058 IntersectionMask intersection_mask = INTERSECT_ALL_RAYS);
00059
00060
00061 virtual ~Octree();
00062
00063 void Insert(IBoundedObject *object);
00064
00065
00066
00067 virtual void Finalize(const Context &context);
00068
00069
00070
00071
00072 virtual int GetObjectCount() const;
00073
00074
00075
00076 virtual const sheep::AABB3 &GetAABB() const;
00077
00078
00079 virtual sheep::Real ComputeSurfaceArea() const;
00080
00081
00082
00083
00084 virtual void EvaluateSurface(
00085 const sheep::Point2 &input,
00086 sheep::Point3 *point,
00087 sheep::Vector3 *geometric_normal
00088 ) const;
00089
00090
00091
00092
00093
00094 virtual bool Intersect(
00095 const Context &context,
00096 const Ray &ray,
00097 Hit *hit = 0
00098 ) const;
00099
00100 virtual void Annotate(
00101 const Context &context,
00102 const ICamera *camera,
00103 Framebuffer *framebuffer
00104 ) const;
00105
00106 private:
00107 typedef std::vector<IBoundedObject *> object_vector;
00108 typedef object_vector::const_iterator object_vector_const_it;
00109
00110 typedef sheep::OctreeNode<const IBoundedObject *> octree_node;
00111 typedef sheep::Octree<const IBoundedObject *> octree;
00112
00113 struct object_voxel_intersector {
00114 bool Intersect(const IBoundedObject *object, const sheep::AABB3 &voxel) const;
00115 };
00116
00117
00118
00119
00120
00121 struct octree_test_visitor {
00122 const Context &m_context;
00123 const Ray &m_ray;
00124 bool m_found;
00125
00126 octree_test_visitor(const Context &context, const Ray &ray);
00127
00128 bool Trace(const octree_node *node, sheep::Real t0, sheep::Real t1);
00129 };
00130
00131
00132
00133
00134 struct octree_full_visitor {
00135 const Context &m_context;
00136 const Ray &m_ray;
00137 Hit m_nearest;
00138
00139 octree_full_visitor(const Context &context, const Ray &ray);
00140
00141 bool Trace(const octree_node *node, sheep::Real t0, sheep::Real t1);
00142 };
00143
00144 object_vector m_objects;
00145 int m_object_count;
00146
00147
00148 int m_pop_threshold;
00149 int m_max_subdiv;
00150
00151 octree *m_octree;
00152
00153 sheep::AABB3 m_aabb;
00154 };
00155
00156 #include "octree.inl"
00157
00158 }
00159
00160 #endif // !TOXIC_RENDERER_OCTREE_H