Cpper
C/C++高级工程师 Android高级软件工程师 IT集成工程师 音频工程师 熟悉c,c++,java,c#,py,js,asp等多种语言 程序猿
这是盖莫引擎AABB3的设计 主要参考了几个引擎代码
这个并没有太多需要说明的
唯一要说的就是使用给定矩阵变换AABB3
代码如下:
  1 ////////////////////////////////////////////////////////
  2 /// 盒子类定义
  3 ////////////////////////////////////////////////////////
  4 template<class T = float>
  5 struct AABB3 
  6 {    
  7      ////////////////////////////////////////////////////////
  8     /// 构造盒子
  9     ////////////////////////////////////////////////////////    
 10     AABB3();
 11 
 12      ////////////////////////////////////////////////////////
 13     /// 由一点构造盒子
 14     ////////////////////////////////////////////////////////      
 15     AABB3(const Vector3<T>& v);
 16     
 17      ////////////////////////////////////////////////////////
 18     /// 由指定2点构造盒子
 19     ////////////////////////////////////////////////////////     
 20     AABB3(const Vector3<T>& v1, const Vector3<T>& v2);
 21     
 22      ////////////////////////////////////////////////////////
 23     /// 由盒子中心点和盒子大小构造盒子(限定x,y,z为正)
 24     ////////////////////////////////////////////////////////     
 25     AABB3(const Vector3<T>& center, T x, T y, T z);
 26     
 27     COPY_OBJECT(AABB3<T>)
 28     
 29     ~AABB3();    
 30 
 31      ////////////////////////////////////////////////////////
 32     /// 获取盒子大小
 33     ////////////////////////////////////////////////////////
 34     Vector3<T>    GetSize() const;
 35  
 36      ////////////////////////////////////////////////////////
 37     /// 获取盒子中心
 38     ////////////////////////////////////////////////////////
 39     Vector3<T>    GetCenter() const
 40 
 41      ////////////////////////////////////////////////////////
 42     /// 盒子插点
 43     ////////////////////////////////////////////////////////
 44     void        Add(const Vector3<T> &point);
 45     
 46      ////////////////////////////////////////////////////////
 47     /// 盒子叠加
 48     ////////////////////////////////////////////////////////
 49     void        Add(const AABB3<T> &box);
 50     
 51      ////////////////////////////////////////////////////////
 52     /// 盒子点包含
 53     ////////////////////////////////////////////////////////
 54     bool        Contain(const Vector3<T> &p) const;
 55 
 56      ////////////////////////////////////////////////////////
 57     /// 盒子包含
 58     ////////////////////////////////////////////////////////
 59     bool        Contain(const AABB3<T> &box) const;
 60     
 61      ////////////////////////////////////////////////////////
 62     /// 最近点查询
 63     ////////////////////////////////////////////////////////
 64     Vector3<T>    GetClosestPoint(const Vector3<T> &p) const;
 65     
 66      ////////////////////////////////////////////////////////
 67     /// 盒子与球体相交检测
 68     ////////////////////////////////////////////////////////
 69     bool        IsIntersectWithSphere(const Vector3<T> &center, T radius) const;
 70     
 71      ////////////////////////////////////////////////////////
 72     /// 盒子与直线是否相交 
 73     ////////////////////////////////////////////////////////    
 74     //! add this. 
 75     
 76     //! 检测盒子和平面关系
 77     //! return < 0 盒子在平面后面
 78     //! return > 0 盒子在平面前面
 79     //! return = 0 盒子包含平面
 80     int            GetRalationshipPlane(const Vector3<T> &n, T  d) const;
 81     
 82     //! 检测2盒子关系(如何相交则返回其叠加盒子)
 83     bool        IsIntersectWithBox(const AABB3<T> &box, AABB3<T> *box_ptr = 0);
 84     
 85     //---------------------------------------------------------------------------
 86     // AABB3::获取8顶点坐标
 87     //
 88     // 返回8顶点坐标中的一个.点标记如下: 
 89     //
 90     //            6                                7
 91     //              ------------------------------
 92     //             /|                           /|
 93     //            / |                          / |
 94     //           /  |                         /  |
 95     //          /   |                        /   |
 96     //         /    |                       /    |
 97     //        /     |                      /     |
 98     //       /      |                     /      |
 99     //      /       |                    /       |
100     //     /        |                   /        |
101     //  2 /         |                3 /         |
102     //   /----------------------------/          |
103     //   |          |                 |          |
104     //   |          |                 |          |      +Y
105     //   |        4 |                 |          | 
106     //   |          |-----------------|----------|      |
107     //   |         /                  |         /  5    |
108     //   |        /                   |        /        |       +Z
109     //   |       /                    |       /         |
110     //   |      /                     |      /          |     /
111     //   |     /                      |     /           |    /
112     //   |    /                       |    /            |   /
113     //   |   /                        |   /             |  /
114     //   |  /                         |  /              | /
115     //   | /                          | /               |/
116     //   |/                           |/                ----------------- +X
117     //   ------------------------------
118     //  0                              1
119     Vector3<T>    GetCorner(int i) const;    
120     
121     ////////////////////////////////////////////////////////
122     /// 获取盒子顶点值
123     ////////////////////////////////////////////////////////
124     Vector3<T>  GetMin()const;
125     Vector3<T>  GetMax()const;  
126 
127     ////////////////////////////////////////////////////////
128     /// 获取盒子容积
129     ////////////////////////////////////////////////////////    
130     T     GetVolume()const;      
131     
132     ////////////////////////////////////////////////////////
133     /// 检测盒子是不是1点
134     ////////////////////////////////////////////////////////    
135     bool  IsPoint()const;  
136     
137      ////////////////////////////////////////////////////////
138     /// 移动盒子(差量)
139     ////////////////////////////////////////////////////////    
140      void Move(const Vector3<T> &dif);
141     
142     ////////////////////////////////////////////////////////
143     /// 使用矩阵处理aabb
144     //////////////////////////////////////////////////////// 
145     void Transform(const AABB3<T> &box,Matrix4<T> &m);     
146     
147     ////////////////////////////////////////////////////////
148     /// aabb盒子的缩放
149     //////////////////////////////////////////////////////// 
150     void  Scale(const Vector3<T> &scale);     
151   
152     ////////////////////////////////////////////////////////
153     /// aabb盒子差值 
154     ////////////////////////////////////////////////////////     
155     AABB3<T> GetInterpolatedBox(const AABB3<T>& other,float t) const
156     {
157         return AABB3<T>((other.min*(1-t)) + (min*t),(other.max*(1-t)) + (max*t));
158     }
159    
160     ////////////////////////////////////////////////////////
161     /// aabb盒子比较 
162     ////////////////////////////////////////////////////////     
163     bool operator==(const AABB3<T>& other){return other.min = min && other.max == max;}
164     
165     NOT_EQUAL(AABB3<T>)    
166     
167     ////////////////////////////////////////////////////////
168     /// 检测盒子是不是空的 
169     ////////////////////////////////////////////////////////       
170     bool IsEmpty();
171     
172     ////////////////////////////////////////////////////////
173     /// 转化AABB到数组, 
174     ////////////////////////////////////////////////////////
175     void ToArray(T aabb[6]);
176     void ToBox(const T aabb[6]);    
177     
178     ////////////////////////////////////////////////////////
179     /// 盒子数据成员定义 
180     ////////////////////////////////////////////////////////
181     Vector3<T>    min;
182     Vector3<T>    max;    
183 };
184  
185 #include <GEngine/aabb3.inl>
在AABB的构造中我们使用std::numeric_limits<T>::max来生成空的AABB3
这样可保持AABB3
max = -std::numeric_limits<T>::max
max =  std::numeric_limits<T>::max

posted on 2010-04-04 17:16 ccsdu2009 阅读(363) 评论(0)  编辑 收藏 引用 所属分类: Game引擎

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理