#ifndef __RENDERTRAVERSER_H #define __RENDERTRAVERSER_H #include #include "Bvh.h" #include "OcclusionQuery.h" #include "Camera.h" #include "RenderQueue.h" #include "Timer/PerfTimer.h" namespace CHCDemoEngine { class Camera; class Matrix4x4; struct TraversalStatistics { public: void Reset(); ////////// //-- several statistics for a rendering pass int mNumTraversedNodes; int mNumQueryCulledNodes; int mNumFrustumCulledNodes; int mNumRenderedGeometry; int mNumRenderedTriangles; int mNumRenderedNodes; int mNumIssuedQueries; int mNumStateChanges; int mNumBatches; double mWaitTime; double mQueryTime; double mRestTime; }; /** Abstract class implementing a scene traversal for rendering. */ class RenderTraverser { public: /** Default constructor. */ RenderTraverser(); /** Virtual constructor, has to be redefined in subclasses */ virtual ~RenderTraverser(); /** Abstract method that traverses and renders the scene */ void RenderScene(); enum {CULL_FRUSTUM, STOP_AND_WAIT, CHC, CHCPLUSPLUS, NUM_TRAVERSAL_TYPES}; /** Returns the type of the traversal. The type is one of CULL_FRUSTUM: view frustum culling only STOP_AND_WAIT: hierarchical stop and wait algorithm CHC: coherent hierarchical algorithm CHCPLUSPLUS: coherent hierarchical algorithm revisited */ virtual int GetType() const = 0; /** Sets the scene hierarchy. */ void SetHierarchy(Bvh *bvh); /** Sets the camera. */ void SetCamera(Camera *cam) {mCamera = cam;} /** Sets the render queue. */ void SetRenderQueue(RenderQueue *rq) {mRenderQueue =rq;} /** Renders a visualization of the hierarchy */ void RenderVisualization(); /** Sets the current render state */ void SetRenderState(RenderState *state); /** The traversal statistics */ const TraversalStatistics &GetStats() const { return mStats; } /** The current frame id */ int GetCurrentFrameId() const { return mFrameId; } ////////////////// //-- options for the different rendering algorithms /** If a render queue should be used to batch up and sort scene entities before rendering. */ void SetUseRenderQueue(bool useRenderQueue); /** If depth pass should be used. If true, the entities found visible in the current pass are stored */ void SetUseDepthPass(bool storeVisibleObjects); /** Sets visible pixels threshold for visibility classification */ void SetVisibilityThreshold(int threshold); /////////////////// //-- CHC / CHC ++ related options /** CHC optimization to query the geometry itself instead of the bounding box. */ void SetUseOptimization(bool useOptimization); /** The number of frames a previously visible node is assumed to stay visible. */ void SetAssumedVisibleFrames(int assumedVisibleFrames); /** The maximum batch size for the i-queue */ void SetMaxBatchSize(int batchSize); /** If multiqueries should be used. */ void SetUseMultiQueries(bool useMultiQueries); /** If thight bounds should be used or the bounding boxes should be tested. */ void SetUseTightBounds(bool useTightBounds); /** If bounds should be shown */ void SetShowBounds(bool showBounds); /** Returns the entities found visible in current frame (only if StoreVisibleObjects is set) */ const SceneEntityContainer &GetVisibleObjects() const { return mVisibleObjects; } protected: /** This is the actual rendering algorithm. It must be implemented by all the subclasses. */ virtual void Traverse() = 0; /** Hierarchy traversal */ void TraverseNode(BvhNode *node); /** Issues occlusion query for a single node */ OcclusionQuery *IssueOcclusionQuery(BvhNode *node); /** Issue multiquery. */ void IssueOcclusionQuery(const OcclusionQuery &query); /** Retunrs true if the current bvh node intersects the near plane. */ inline bool IntersectsNearPlane(BvhNode *node) const; /** Enqueues a bvh node for distance traversal */ void EnqueueNode(BvhNode *node); /** Renders the bvh node. */ void RenderNode(BvhNode *node); /** Renders and clears the contents of the render queue. */ void ApplyRenderQueue(); //////////// //-- members /// the current camera Camera *mCamera; /// the root of the scene hierarchy Bvh *mBvh; /// the priority queue used for front to back traversal TraversalQueue mDistanceQueue; /// the current frame id int mFrameId; /// the current render state RenderState *mRenderState; /// manages creation and destruction of the queries QueryHandler mQueryHandler; /// the statisitcs TraversalStatistics mStats; RenderQueue *mRenderQueue; /// the objects found visible in current frame SceneEntityContainer mVisibleObjects; ///////////////// //-- algorithm parametes int mVisibilityThreshold; bool mUseOptimization; bool mUseRenderQueue; int mAssumedVisibleFrames; int mMaxBatchSize; bool mUseMultiQueries; bool mUseTightBounds; bool mShowBounds; bool mUseDepthPass; }; inline bool RenderTraverser::IntersectsNearPlane(BvhNode *node) const { return mBvh->GetDistance(node) < mCamera->GetNear(); } } #endif // RENDERTRAVERSER_H