Subversion Repositories gelsvn

Rev

Rev 290 | Rev 319 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
208 jrf 1
#ifndef __BSPTREE_H__
2
#define __BSPTREE_H__
3
 
4
#include <vector>
5
 
6
#include "CGLA/Mat4x4f.h"
7
#include "Geometry/TriMesh.h"
8
 
9
#include "BBox.h"
10
 
290 jrf 11
namespace Geometry 
208 jrf 12
{
13
  struct BSPNode;
14
 
15
  // Initialization structure
16
  struct BSPNode 
17
  {
18
    unsigned char axis_leaf; // 00 = axis 0, 01 = axis 1, 10 = axis 2, 11 = leaf
19
    double plane;
20
    BSPNode *left, *right;
21
    int id;
22
    int count;
23
    unsigned int ref;
24
  };
25
 
26
  // Faster structure
27
  struct BSPLeaf {
28
    // bits 0..30 : offset to first son
29
    // bit 31 (sign) flag whether node is a leaf
30
    unsigned int flagAndOffset;
31
    unsigned int count;
32
  };
33
 
34
  struct BSPInner {
35
    unsigned int flagAndOffset;
36
    // bits 0..1 : splitting dimension
37
    // bits 2..30: offset bits
38
    // bit 31 (sign) flag whether node is a leaf
39
    float splitCoordinate;
40
  };
41
 
42
  union FastBSPNode
43
  {
44
    BSPLeaf leaf;
45
    BSPInner inner;
46
  };
47
 
48
  class BSPTree 
49
  {
50
    bool b_is_build;
51
   public:
52
    static int node_calls;
53
    static int tri_calls;
54
    BSPNode* root;
55
    BBox bbox;
56
    std::vector<Geometry::TriMesh*> trimesh;
57
    std::vector<CGLA::Mat4x4f> transforms;
58
 
59
    std::vector<ISectTri> isecttris;
60
    std::vector<TriAccel> triaccel;
61
    std::vector<ISectTri*> all_objects;
62
    std::vector<TriAccel*> all_triaccel;
63
    std::vector<FastBSPNode> fast_tree;
64
 
65
    unsigned int max_objects;
66
    unsigned int max_level;
67
 
68
    BSPTree();
69
    ~BSPTree();
70
 
71
    void delete_node(BSPNode *node);
72
//	bool intersect2(BRender::Ray &ray, const TriAccel &acc, double t_max) const;
73
    void subdivide_node(BSPNode &node, BBox &bbox, 
74
			unsigned int level, 
75
			std::vector<ISectTri*>& objects, 
76
			std::vector<TriAccel*>& tri_objects);
77
    void init();
78
    void clear();
79
 
80
    void init(std::vector<Geometry::TriMesh*>& trimesh, 
81
	      int max_objects, int max_level);
82
    void init(std::vector<Geometry::TriMesh*>& _trimesh, 
83
	      std::vector<CGLA::Mat4x4f>& _transforms, 
84
	      int _max_objects, int _max_level);
85
    void init(Geometry::TriMesh* mesh, CGLA::Mat4x4f transform, 
86
	      std::vector<int> &trilist, 
87
	      int _max_objects, int _max_level);
88
 
89
    void build();
90
    bool is_build();
91
 
290 jrf 92
    bool intersect_node(Ray &ray, const BSPNode &node, 
208 jrf 93
			double t_min, double t_max) const;
290 jrf 94
    bool intersect(Ray &ray) const;
208 jrf 95
    void print(BSPNode *node, int depth);
96
    int size(BSPNode *node);
97
    int size();
98
 
99
    void BSPTree::make_fast_tree(BSPNode *node);
100
    void BSPTree::push_fast_bsp_node(BSPNode *node, int id);
290 jrf 101
    void BSPTree::intersect_fast_node(Ray &ray, 
208 jrf 102
				      const FastBSPNode *node, 
103
				      double t_min, double t_max) const;
290 jrf 104
    bool intersect(Ray &ray, const ISectTri &isecttri, double t_max) const;
208 jrf 105
  };
106
}
107
#endif
108