Main Page Modules Namespace List Class Hierarchy Alphabetical List Compound List File List Namespace Members Compound Members File Members Related Pages

# BrushOctree.hpp

Go to the documentation of this file.

    00001 ///////////////////////////////////////////////////////////////////////////////
    00002 /// @file BrushOctree.hpp
    00003 ///
    00004 /// @brief Brush Octree header
    00005 ///
    00006 /// @author Assassin
    00007 ///
    00008 /// This file is the intellectual property of Novus Delta, LLC.. Usage of the
    00009 /// contents of this file is subject to the Destiny3D Member License which
    00010 /// can be found at http://www.destiny3d.com.  Any other usage is prohibited.
    00011 ///
    00012 /// This file is distributed "AS IS" without warranty of any kind.  Novus
    00013 /// Delta, LLC. does not guarantee the fitness of the contents of this file
    00014 /// for any particular purpose.
    00015 ///
    00016 /// Copyright (C) 2001-2003 Novus Delta, LLC. All Rights Reserved.
    00017 ///
    00018 /// <hr>
    00019 ///                                 Change History
    00020 /// <hr>
    00021 ///
    00022 /// @date Nov 2001
    00023 /// @author Assassin
    00024 /// @remarks Creation
    00025 ///
    00026 /// @date Jan 2002
    00027 /// @author Assassin
    00028 /// @remarks January Release
    00029 ///
    00030 /// @date Dec 2002
    00031 /// @author Assassin
    00032 /// @remarks Rewrite for new scenegraph code
    00033 ///
    00034 ///////////////////////////////////////////////////////////////////////////////
    00035
    00036 #ifndef BRUSH_OCTREE_HPP
    00037 #define BRUSH_OCTREE_HPP
    00038
    00039 #include "deGlobalTypes.hpp"
    00040 #include "deList.hpp"
    00041 #include "deArray.hpp"
    00042 #include "deBrush_priv.hpp"
    00043
    00044 #include "deHash.hpp"
    00045 #include "deFunctors.hpp"
    00046
    00047 class deBrushOctree
    00048 {
    00049 public:
    00050     class deIndexedPoly
    00051     {
    00052     public:
    00053         //deBoolean CanRemove;
    00054         s32         NumVerts;
    00055         long        PolyIndex;
    00056         long        TextureIndex;
    00057         u16 *       PosIndices;
    00058         u16 *       NormIndices;
    00059         u16 *       MatIndices;
    00060         deDouble    Dist;
    00061         deVec3d     Normal;
    00062         ~deIndexedPoly()
    00063         { delete[] PosIndices; }
    00064         deIndexedPoly(s32 vertnum = 3)
    00065         {
    00066             //CanRemove = deFALSE;
    00067             NumVerts = vertnum;
    00068             PosIndices = new u16[NumVerts*3];
    00069             NormIndices= &PosIndices[NumVerts];
    00070             MatIndices = &PosIndices[NumVerts*2];
    00071             TextureIndex = -1;
    00072             PolyIndex = -1;
    00073         }
    00074         deIndexedPoly(const deIndexedPoly & ref)
    00075         {
    00076             //CanRemove = deFALSE;
    00077             Normal = ref.Normal;
    00078             Dist = ref.Dist;
    00079             NumVerts = ref.NumVerts;
    00080             TextureIndex = ref.TextureIndex;
    00081             PosIndices = new u16[NumVerts*3];
    00082             NormIndices= &PosIndices[NumVerts];
    00083             MatIndices = &PosIndices[NumVerts*2];
    00084             memcpy(PosIndices, ref.PosIndices, NumVerts*sizeof(u16)*3);
    00085             PolyIndex = -1;
    00086         }
    00087         deIndexedPoly & operator=(const deIndexedPoly & ref)
    00088         {
    00089             Normal = ref.Normal;
    00090             Dist = ref.Dist;
    00091             TextureIndex = ref.TextureIndex;
    00092             NumVerts = ref.NumVerts;
    00093             if (NumVerts != ref.NumVerts)
    00094             {
    00095                 delete[] PosIndices;
    00096                 PosIndices = new u16[NumVerts*3];
    00097                 NormIndices= &PosIndices[NumVerts];
    00098                 MatIndices = &PosIndices[NumVerts*2];
    00099             }
    00100             memcpy(PosIndices, ref.PosIndices, NumVerts*sizeof(u16)*3);
    00101             PolyIndex = -1;
    00102             return *this;
    00103         }
    00104         void Resize(s32 vertnum)
    00105         {
    00106             if (vertnum == NumVerts) return;
    00107             u16* pTemp;
    00108             NumVerts = vertnum;
    00109
    00110             pTemp = new u16[NumVerts*3];
    00111             memcpy(pTemp, PosIndices, NumVerts*sizeof(u16)*3);
    00112             delete[] PosIndices;
    00113             PosIndices = pTemp;
    00114             NormIndices= &PosIndices[NumVerts];
    00115             MatIndices = &PosIndices[NumVerts*2];
    00116         }
    00117     };
    00118
    00119     struct deVertMat
    00120     {
    00121         deARGB Diffuse;
    00122         deTexCoord UV;
    00123     };
    00124
    00125 private:
    00126     struct OctNode
    00127     {
    00128         long Depth;
    00129         DWORD Contents;
    00130         deVec3d Min, Max;
    00131         struct OctNode* Children[9];
    00132         deTArray <long> PolyIndexList;
    00133     };
    00134
    00135 public:
    00136     deBrushOctree();
    00137     ~deBrushOctree();
    00138
    00139     deBoolean AddPositions(deVec3d *VertArray, u16 NumVerts, u16 &Offset);
    00140     deBoolean AddNormals(deVec3d *VertArray, u16 NumVerts, u16 &Offset);
    00141     deBoolean AddVertMats(deVertMat *VertArray, u16 NumVerts, u16 &Offset);
    00142     deBoolean AddPolyGetRef(deIndexedPoly* & poly);
    00143     deBoolean AddPoly(deIndexedPoly * poly, u16 PosOffset, u16 NormOffset, u16 MatOffset);
    00144
    00145     deBoolean GetPoly(deIndexedPoly* & poly, long PolyIndex);
    00146
    00147     deBoolean RemovePoly(const deIndexedPoly * poly);
    00148
    00149     deBoolean Generate();
    00150     deBoolean Clear();
    00151
    00152     deBoolean GetPolysInBox(const deVec3d & Min, const deVec3d & Max, deTArray <deIndexedPoly*> &PolyArray, OctNode * node = NULL);
    00153     deBoolean GetAllPolys(deTArray <deIndexedPoly*>* &PolyArray);
    00154     deBoolean GetPolyInfo(deIndexedPoly* poly, deVec3d* &PosArray, deVec3d* &NormArray, deVertMat* &VertMatArray);
    00155 /*  deBoolean RayTrace(const deVec3d & start, const deVec3d & end, deIndexedPoly * & HitPoly, deVec3d & intersect);
    00156
    00157     deBoolean AddMesh(deBrushOctree & adder);
    00158     deBoolean SubtractMesh(deBrushOctree & cutter);
    00159     deBoolean IntersectMesh(deBrushOctree & brush);
    00160
    00161     deBoolean Copy(deBrushOctree & target);
    00162 */
    00163 private:
    00164     OctNode *m_Root;
    00165     deTList  <long>                 m_PolyAddList;  // list of polygons that haven't been put into the array yet
    00166     deTList  <long>             m_PolyRemoveList;   // list of polygons that have been invalidated and can be replaced
    00167     deTArray <deIndexedPoly*>       m_PolyArray;    // all the polygons that are inserted into the octree
    00168     deTArray <deTArray<OctNode*> >  m_PolyNodes;    // lists of which nodes a polygon is "in"
    00169     deTArray <deVec3d>              m_Positions;    // positions buffer
    00170     deTArray <deVec3d>              m_Normals;      // normals buffer
    00171     deTArray <deVertMat>            m_VertMats;     // materials buffer
    00172     // for splitting polygons
    00173     dePlane     m_SplittingPlane;
    00174     deTHashFunctor<u16,HashEdge>    m_SplitPosHash;
    00175     deTHashFunctor<u16,HashEdge>    m_SplitNormHash;
    00176     deTHashFunctor<u16,HashEdge>    m_SplitMatHash;
    00177
    00178 //  void CopyNode(OctNode * source, OctNode * & target);
    00179     void DestroyNode(OctNode * node);
    00180
    00181     deBoolean   InsertPolyIntoTree(long PolyIndex);
    00182     u8      DecideChildNodes(long PolyIndex, OctNode* Node);
    00183     OctNode*    GetSmallestNodeForAABB(const deAABB & bbox);
    00184     deBoolean   InsertPolyIntoNode(long PolyIndex, OctNode* Node);
    00185
    00186 /*
    00187     void SubdivideNode(OctNode * node);
    00188     void Optimize(OctNode * node);
    00189     void InsertPoly(long index);
    00190     bool InsertPoly(PolyPointer * p, OctNode * node);
    00191     void RemovePoly(PolyPointer * p);
    00192     bool PolyInNode(long poly, OctNode * node);
    00193     long PolysIntersecting(deBPoly & tri, deTList <PolyPointer*> & polylist, bool CheckIntersection = false);
    00194     long GetAllPolys(deTList <PolyPointer*> & polylist, OctNode * node);
    00195     void Split(deBrushOctree & In1, deBrushOctree & In2, deBrushOctree & Out1, deBrushOctree & Out2);
    00196 */
    00197
    00198     static char      FrontmostOctNodeCorner(const deVec3d & norm);
    00199     static void      OctNodeCorner(const OctNode* node, const char Corner, deVec3d & vec);
    00200     static void      OctNodeMidPoint(const OctNode* node, const char MidPoint, deVec3d & vec);
    00201     static deBoolean OctNodeIntersectsPlane(OctNode *node, const dePlane & plane);
    00202     static inline void OctNodeCenter(const OctNode* node, deVec3d & vec)
    00203     {
    00204         vec = node->Min; vec += node->Max; vec *= 0.5;
    00205     }
    00206
    00207     // not static because they depend on the current octree's vertices
    00208     // mostly polygon stuff
    00209     char        TriLineIntersect(   const deIndexedPoly * T, u16 VertNum,
    00210                                     const deVec3d & LinePt1, const deVec3d & LinePt2,
    00211                                     deVec3d & Res, deBoolean & HitEdge); // not implemented
    00212     deBoolean   PolyPlaneIntersect(const deVec3d & norm, const deDouble & dist, const deIndexedPoly * poly, deVec3d* & ResPair);
    00213     deBoolean   PolyPolyIntersect(const deIndexedPoly * p1, const deIndexedPoly * p2);
    00214     deBoolean   MakePlane(long PolyIndex);
    00215     deBoolean   MakePlane(deIndexedPoly * input);
    00216     deBoolean   SplitPoly(const deIndexedPoly * input, deIndexedPoly* & out1, deIndexedPoly* & out2);
    00217     u16         SplitEdgePos(const HashEdge & Edge, deDouble & Percent);
    00218     u16         SplitEdgeNorm(const HashEdge & Edge, const deDouble & Percent);
    00219     u16         SplitEdgeMat(const HashEdge & Edge, const deDouble & Percent);
    00220
    00221     deBoolean   SplitAllPolys(deIndexedPoly* splitter);
    00222
    00223     deBoolean   AssembleVertexBuffer(IdeVertexBuffer * pVB);
    00224     deBoolean   AssembleVertexBufferNoNormals(IdeVertexBuffer * pVB);
    00225     deBoolean   TriangulateIndices(const deIndexedPoly * Poly, u16 LastVertIndex, u16 * IndexArray3);
    00226
    00227     void TEST();
    00228 };
    00229
    00230 /*
    00231         ________Max
    00232        / 6 / 7 /|
    00233       /___/___/7|       Y+  Z+
    00234      / 2 / 3 /| |       |  /
    00235     /___/___/3|/|       | /
    00236     |   |   | /5|       |/____X+
    00237 4=> | 2 | 3 |/| |
    00238     |-------|1|/
    00239     | 0 | 1 | /
    00240     |___|___|/
    00241   Min
    00242
    00243   mins:
    00244   X: i%2 == 0
    00245   Y: i%4 <  2
    00246   Z: i   <  4
    00247
    00248 */
    00249
    00250 #endif