1 | /* |
---|
2 | * Copyright (C) 2005-2008 MaNGOS <http://www.mangosproject.org/> |
---|
3 | * |
---|
4 | * Copyright (C) 2008 Trinity <http://www.trinitycore.org/> |
---|
5 | * |
---|
6 | * This program is free software; you can redistribute it and/or modify |
---|
7 | * it under the terms of the GNU General Public License as published by |
---|
8 | * the Free Software Foundation; either version 2 of the License, or |
---|
9 | * (at your option) any later version. |
---|
10 | * |
---|
11 | * This program is distributed in the hope that it will be useful, |
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | * GNU General Public License for more details. |
---|
15 | * |
---|
16 | * You should have received a copy of the GNU General Public License |
---|
17 | * along with this program; if not, write to the Free Software |
---|
18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
19 | */ |
---|
20 | |
---|
21 | #ifndef _SHORTBOX_H |
---|
22 | #define _SHORTBOX_H |
---|
23 | |
---|
24 | #include <G3D/Vector3.h> |
---|
25 | #include <G3D/AABox.h> |
---|
26 | #include <G3D/Triangle.h> |
---|
27 | #include <G3D/Ray.h> |
---|
28 | |
---|
29 | #include "ShortVector.h" |
---|
30 | |
---|
31 | /** |
---|
32 | This is a box and a triangle Class using ShortVectors. Each vector has 16 bit an a fixed point 12.4 representation. |
---|
33 | */ |
---|
34 | |
---|
35 | namespace VMAP |
---|
36 | { |
---|
37 | |
---|
38 | class ShortBox |
---|
39 | { |
---|
40 | private: |
---|
41 | ShortVector iV1; |
---|
42 | ShortVector iV2; |
---|
43 | public: |
---|
44 | ShortBox() {} |
---|
45 | inline const ShortVector& getLo() const { return(iV1); } |
---|
46 | inline const ShortVector& getHi() const { return(iV2); } |
---|
47 | inline void setLo(const ShortVector& pV){ iV1 = pV; } |
---|
48 | inline void setHi(const ShortVector& pV){ iV2 = pV; } |
---|
49 | inline void setLo(const G3D::Vector3& pV){ iV1 = ShortVector(pV); } |
---|
50 | inline void setHi(const G3D::Vector3& pV){ iV2 = ShortVector(pV); } |
---|
51 | |
---|
52 | inline bool operator==(const ShortBox& b) const |
---|
53 | { |
---|
54 | return ((iV1 == b.iV1) && (iV2 == b.iV2)); |
---|
55 | } |
---|
56 | |
---|
57 | inline bool operator!=(const ShortBox& b) const |
---|
58 | { |
---|
59 | return !((iV1 == b.iV1) && (iV2 == b.iV2)); |
---|
60 | } |
---|
61 | }; |
---|
62 | |
---|
63 | //===================================================================== |
---|
64 | #ifdef _DEBUG_VMAPS |
---|
65 | #ifndef gBoxArray |
---|
66 | extern G3D::Vector3 p1,p2,p3,p4,p5,p6,p7; |
---|
67 | extern G3D::Array<G3D::AABox>gBoxArray; |
---|
68 | extern G3D::Array<G3D::Triangle>gTriArray; |
---|
69 | extern int gCount1, gCount2, gCount3, gCount4; |
---|
70 | extern bool myfound; |
---|
71 | #endif |
---|
72 | #endif |
---|
73 | |
---|
74 | static const G3D::Vector3 dummyZeroPosition = G3D::Vector3(0,0,0); |
---|
75 | |
---|
76 | class TriangleBox |
---|
77 | { |
---|
78 | private: |
---|
79 | ShortVector _vertex[3]; |
---|
80 | //ShortBox iBox; |
---|
81 | public: |
---|
82 | inline TriangleBox() { } |
---|
83 | inline TriangleBox(const ShortVector& pV1, const ShortVector& pV2, const ShortVector& pV3) |
---|
84 | { |
---|
85 | _vertex[0] = pV1; |
---|
86 | _vertex[1] = pV2; |
---|
87 | _vertex[2] = pV3; |
---|
88 | |
---|
89 | } |
---|
90 | inline const ShortVector& vertex (int n) const |
---|
91 | { |
---|
92 | return(_vertex[n]); |
---|
93 | } |
---|
94 | |
---|
95 | inline const ShortBox getBounds()const |
---|
96 | { |
---|
97 | ShortBox box; |
---|
98 | |
---|
99 | ShortVector lo = _vertex[0]; |
---|
100 | ShortVector hi = lo; |
---|
101 | |
---|
102 | for (int i = 1; i < 3; ++i) |
---|
103 | { |
---|
104 | lo = lo.min(_vertex[i]); |
---|
105 | hi = hi.max(_vertex[i]); |
---|
106 | } |
---|
107 | box.setLo(lo); |
---|
108 | box.setHi(hi); |
---|
109 | return(box); |
---|
110 | } |
---|
111 | inline const G3D::Vector3& getBasePosition() { return(dummyZeroPosition); } |
---|
112 | |
---|
113 | inline const G3D::AABox getAABoxBounds() const { ShortBox box = getBounds(); return(G3D::AABox(box.getLo().getVector3(), box.getHi().getVector3())); } |
---|
114 | |
---|
115 | inline bool operator==(const TriangleBox& t) const |
---|
116 | { |
---|
117 | return ((_vertex[0] == t._vertex[0]) && (_vertex[1] == t._vertex[1]) &&(_vertex[2] == t._vertex[2])); |
---|
118 | } |
---|
119 | |
---|
120 | inline bool operator!=(const TriangleBox& t) const |
---|
121 | { |
---|
122 | return !((_vertex[0] == t._vertex[0]) && (_vertex[1] == t._vertex[1]) &&(_vertex[2] == t._vertex[2])); |
---|
123 | } |
---|
124 | |
---|
125 | inline void intersect(const G3D::Ray& pRay, float& pMaxDist, bool /*pStopAtFirstHitDummy*/, G3D::Vector3& /*pOutLocationDummy*/, G3D::Vector3& /*pOutNormalDummy*/) const |
---|
126 | { |
---|
127 | static const double epsilon = 0.00001; |
---|
128 | G3D::Triangle testT(vertex(0).getVector3(),vertex(1).getVector3(),vertex(2).getVector3()); |
---|
129 | float t = pRay.intersectionTime(testT); |
---|
130 | if ((t < pMaxDist) || t < (pMaxDist + epsilon)) |
---|
131 | pMaxDist = t; |
---|
132 | else |
---|
133 | { |
---|
134 | testT = G3D::Triangle(vertex(2).getVector3(),vertex(1).getVector3(),vertex(0).getVector3()); |
---|
135 | |
---|
136 | #ifdef _DEBUG_VMAPS |
---|
137 | { |
---|
138 | G3D::Triangle myt(testT.vertex(0)+p6, testT.vertex(1)+p6,testT.vertex(2)+p6); |
---|
139 | gTriArray.push_back(myt); |
---|
140 | } |
---|
141 | #endif |
---|
142 | t = pRay.intersectionTime(testT); |
---|
143 | if ((t < pMaxDist) || t < (pMaxDist + epsilon)) |
---|
144 | pMaxDist = t; |
---|
145 | } |
---|
146 | } |
---|
147 | }; |
---|
148 | |
---|
149 | } |
---|
150 | #endif |
---|