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

# dePolyABT.hpp

Go to the documentation of this file.

    00001 ///////////////////////////////////////////////////////////////////////////////
    00002 /// @file dePolyABT.hpp
    00003 ///
    00004 /// @brief Adaptive Binary Tree for polygons
    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 Mar 2003
    00023 /// @author Assassin
    00024 /// @remarks Creation
    00025 ///
    00026 ///////////////////////////////////////////////////////////////////////////////
    00027
    00028 #ifndef DE_POLYABT_HPP
    00029 #define DE_POLYABT_HPP
    00030
    00031 #include "deGlobalTypes.hpp"
    00032 #include "deArray.hpp"
    00033 #include "deSurface.hpp"
    00034 #include "deScene.hpp"
    00035 #include "deMath.hpp"
    00036 #include "deWorld_Helper.hpp"
    00037
    00038 class IdeVertexBuffer;
    00039 class IdeSceneObject;
    00040
    00041 const int deABT_TexCoords = 4;
    00042
    00043 class dePolyABT
    00044 {
    00045 public:
    00046     // all vertices stored in the ABT structure will store this information only
    00047     struct ABTVertex
    00048     {
    00049         deVec3d     Position;
    00050         deVec3f     Normal;
    00051         deTexCoord2 TexCoords[deABT_TexCoords];
    00052         deARGB      Diffuse;
    00053     };
    00054     // all ABT faces are triangles, and each knows its surface ID
    00055     struct ABTFace
    00056     {
    00057         long Indices[3];
    00058         long SurfaceID;
    00059     };
    00060     enum ABTSplitMethod
    00061     {
    00062         Split_NoMethod,
    00063         Split_SuccessiveApprox,
    00064         Split_AllVerts,
    00065         Split_MultiSample,
    00066         Split_Statistical,
    00067         Split_Count,
    00068         Split_Force32Bit = 0x7fffffff
    00069     };
    00070
    00071     dePolyABT();
    00072     ~dePolyABT();
    00073
    00074     /// surface state machine setting
    00075     deBoolean SetCurrentSurface(IdeSurface* CurrSurface);
    00076
    00077     /// add vertices that will be indexed by faces
    00078     deBoolean AddVertices(ABTVertex* VertexArray, long NumVerts, long & BaseVertexIndex, const deTransformInfo & worldpos);
    00079     /// add indices for faces, offset by a value obtained from AddVertices
    00080     deBoolean AddTriangleIndices(long* IndexArray, long BaseVertexIndex, long NumIndices);
    00081     /// handles an entire sceneobject without decomposing it into geometry
    00082     deBoolean AddSceneObject(IdeSceneObject* pObject);
    00083     /// add raw geometry from a vertex buffer - can be just vertices, just indices, or both
    00084     deBoolean AddIndexedVBuffer(IdeVertexBuffer* VBuffer, long & BaseVertexIndex, const deTransformInfo & worldpos);
    00085     /// add the geometry from an IdeMesh object
    00086     deBoolean AddMeshGeometry(IdeMesh* pMesh, const deTransformInfo & worldpos);
    00087
    00088     /// set the threshold for maximum triangles in a leaf node. defaults to 50.
    00089     deBoolean SetMaxLeafTris(long MaxTris);
    00090     /// set the threshold for maximum triangles in a renderable vertex buffer. defaults to 2000.
    00091     deBoolean SetMaxVBufferTris(long MaxTris);
    00092     /// set the weighting values for determining split plane locations.
    00093     /// sum of all weights should be 1.0, and will be normalized if it is not.
    00094     /// recommended to put most weight into FaceBalance to get more adaptive splits,
    00095     /// but leave some weight in the others to avoid degenerate splits.
    00096     /// @param Axis weight towards largest axis
    00097     /// @param Volume weight towards center of chunk volume
    00098     /// @param FaceBalance weight towards equal number of faces on each side of split
    00099     /// @param NumSplit weight away from inducing splits of faces in a node
    00100     deBoolean SetSplitWeights(deFloat Axis, deFloat Volume, deFloat FaceBalance, deFloat NumSplit);
    00101
    00102     /// compile the entire ABT geometry into renderable VBs and leaf nodes
    00103     deBoolean CompileABT();
    00104
    00105     /// render the currently visible geometry, according to the input Params.
    00106     deBoolean RenderVisibleGeometry(IdeSceneGraph::deSceneTraversal* Params);
    00107
    00108     /// get the AABB extents of the ABT
    00109     void GetVisibleAABB(deAABB& bbox);
    00110
    00111     // serialization stuff
    00112     deBoolean Serialize(IdeFile * File);
    00113     deBoolean DeSerialize(IdeFile * File, u32 DataLength, u32 & AmtRead);
    00114     deBoolean DeSerializeLoad();
    00115
    00116 private:
    00117     /// a split in an ABT node, along an axis (0,1,2) at a certain percentage
    00118     struct ABTSplit
    00119     {
    00120         long        Axis;
    00121         deDouble    Percent;
    00122     };
    00123     /// a chunk of geometry that can be rendered independently.
    00124     /// chunks should generally be 2000 faces or more
    00125     struct ABTRenderChunk
    00126     {
    00127         IdeVertexBuffer*    VBuffer;
    00128         u32                 Surface;
    00129     };
    00130     /// a partition within the ABT.
    00131     /// either has children or renderables, never both.
    00132     /// non-leaf nodes can have faces if in limbo while compiling.
    00133     struct ABTPartition
    00134     {
    00135         deAABB BBox;
    00136         ABTSplit Split;
    00137         deTArray <long> SceneObjectList;
    00138         deTArray<ABTFace>   Faces;
    00139         deTArray<ABTRenderChunk> Renderables;
    00140         ABTPartition* Children[2]; // 0 = minside, 1 = maxside
    00141         ABTPartition* Parent;
    00142         deBoolean RenderableBuilt;
    00143     };
    00144     /// information about a split - most important being the score.
    00145     /// used only while partitioning a box, to score the various sampled splits
    00146     struct ABTSplitStat
    00147     {
    00148         ABTSplit Split;
    00149         dePlane Plane;
    00150         long FrontBackDiff;
    00151         long NumOn;
    00152         deFloat Score;
    00153     };
    00154
    00155     deTArray<ABTVertex>     m_Vertices;
    00156     deTArray<ABTFace>       m_AllFaces;
    00157     deTArray<IdeSurface*>   m_Surfaces;
    00158     deTArray<IdeSceneObject*>   m_SceneObjects;
    00159     deAABB                  m_TotalBBox;
    00160     deVec3d                 m_WorldOffset;
    00161     ABTPartition*           m_RootPartition;
    00162     u32                     m_CurrSurfaceIndex;
    00163     u32                     m_OriginalVertNum;
    00164     u32                     m_MaxLeafTris;
    00165     u32                     m_MaxVBTris;
    00166     deFloat                 m_SplitWeights[4];
    00167
    00168     // setup
    00169     void            DestroyABT();
    00170     // building the ABT
    00171     ABTPartition*   CreateABTPartition(ABTPartition * Parent, int ChildNum);
    00172     deBoolean       DestroyABTPartition(ABTPartition* &pPart);
    00173     deBoolean       ShrinkPartitionBBoxes(ABTPartition * Parent);
    00174     deBoolean       ProcessABTPartition(ABTPartition * pPart);
    00175     void            PickBestSplit(const ABTPartition* pPart, ABTSplitStat & best, ABTSplitMethod method) const;
    00176     deBoolean       ScorePartitionSplit(const ABTPartition* pPart, ABTSplitStat * pStat, deFloat AxisScores[3]) const;
    00177     deBoolean       InterpolateCutEdge(ABTVertex * NewVert, long Index1, long Index2, dePlane& SplitPlane);
    00178     deBoolean       BuildPartitionVBuffer(ABTPartition* pPart);
    00179
    00180     // using the ABT
    00181     deBoolean       RenderVisibleABTPartitions(IdeSceneGraph::deSceneTraversal* Params, ABTPartition* CurrentPart, deBoolean AllVisible);
    00182     deBoolean       RenderABTPartition(IdeSceneGraph::deSceneTraversal* Params, ABTPartition* pPart);
    00183
    00184     // stuff for serialization
    00185     deTArray<deIDPair> m_SerialSurfaces, m_SerialObjects;
    00186     struct ABTFilePartition_01
    00187     {
    00188         deAABB BBox;
    00189         ABTSplit Split;
    00190         u32 childPart[2]; u32 parentPart;
    00191         u32 numFaces, faceIndexStart;
    00192         u32 numObjects, objectIndexStart;
    00193     };
    00194     static u32 AppendFilePartition(deTArray<ABTFilePartition_01> & fileparts, const ABTPartition* current, u32 parentIdx, u32 & TotalFaces, u32 & TotalObjects);
    00195     ABTPartition* DecomposeFilePartition(const deTArray<ABTFilePartition_01> & fileparts, u32 currentIdx, ABTPartition* parent, const deTArray<u32> objectIndices, const deTArray<ABTFace> faces);
    00196     struct readWriteObject_t
    00197     {
    00198         IdeFile * m_File;
    00199         deBoolean m_Write;
    00200         u32 m_DataLength, *m_AmtRead;
    00201         deBoolean operator()(IdeWorldObject* obj);
    00202     };
    00203     struct readWriteVertex_t
    00204     {
    00205         IdeFile * m_File;
    00206         deBoolean m_Write;
    00207         u32 m_DataLength, *m_AmtRead;
    00208         deBoolean operator()(ABTVertex& vert);
    00209     };
    00210     struct readWriteFace_t
    00211     {
    00212         IdeFile * m_File;
    00213         deBoolean m_Write;
    00214         u32 m_DataLength, *m_AmtRead;
    00215         deBoolean operator()(ABTFace& face);
    00216     };
    00217     struct readWritePartition_t
    00218     {
    00219         IdeFile * m_File;
    00220         deBoolean m_Write;
    00221         u32 m_DataLength, *m_AmtRead;
    00222         deBoolean operator()(ABTFilePartition_01& part);
    00223     };
    00224     struct grabPartitionData_t
    00225     {
    00226         deTArray<u32> objects;
    00227         deTArray<ABTFace> faces;
    00228         deBoolean operator()(ABTFilePartition_01& part);
    00229     };
    00230 };
    00231
    00232 #endif // DE_POLYABT_HPP