#ifndef __GLRENDERER_H #define __GLRENDERER_H #include "ObjectPvs.h" #include "Vector3.h" #include "Containers.h" #include "Halton.h" #include "Renderer.h" #include "Beam.h" namespace GtpVisibilityPreprocessor { class SceneGraph; class ViewCellsManager; class Mesh; class MeshInstance; class Intersectable; class Material; class Beam; class KdTree; class GlRendererBuffer; class BeamSampleStatistics; class OcclusionQuery; class TransformedMeshInstance; class TriangleIntersectable; class BvhNode; class SimpleRayContainer; class SceneGraphLeaf; struct VssRayContainer; struct GLUquadric; struct PvsRenderStatistics { float maxError; float sumError; int sumPvsSize; int frames; int errorFreeFrames; PvsRenderStatistics() { Reset(); } void Reset() { maxError = 0.0f; sumError = 0.0f; frames = 0; errorFreeFrames = 0; sumPvsSize = 0; } float GetMaxError() { return maxError; } float GetAvgError() { return sumError/frames; } float GetErrorFreeFrames() { return errorFreeFrames/(float)frames; } float GetAvgPvs() { return sumPvsSize/(float)frames; } }; struct PvsCache { PvsCache():mViewCell(NULL), mUnfilteredPvsSize(0) {} void Reset() { mViewCell = NULL; mPvs.Clear(); filteredBoxes.clear(); mUnfilteredPvsSize = 0; } ViewCell *mViewCell; ObjectPvs mPvs; int mUnfilteredPvsSize; vector filteredBoxes; }; struct RenderCostSample { RenderCostSample() {} void Reset() { mVisibleObjects = 0; mVisiblePixels = 0; } Vector3 mPosition; // visible object from the given point int mVisibleObjects; // visible pixels int mVisiblePixels; ObjectPvs mPvs; }; /** Class encapsulating gl rendering for the scene. There is no gl context binding so the binding is performed in the derived classes */ class GlRenderer: public Renderer { friend class GlobalLinesRenderer; friend class ViewCellsManager; public: GlRenderer(SceneGraph *sceneGraph, ViewCellsManager *viewcells, KdTree *tree); GlRenderer() {} virtual ~GlRenderer(); virtual void RandomViewPoint(); void SetupFalseColor(const unsigned int id); void RenderIntersectable(Intersectable *); void RenderViewCell(ViewCell *vc); void RenderMeshInstance(MeshInstance *mi); void RenderTransformedMeshInstance(TransformedMeshInstance *mi); void RenderMesh(Mesh *m); void SetupMaterial(Material *m); virtual void SetupCamera(); void RenderRays(const VssRayContainer &rays, int colorType = 0, int showDistribution = 15, int maxAge = 9999999); void RenderTriangle(TriangleIntersectable *object); void RenderBox(const AxisAlignedBox3 &box); void RenderBvhNode(BvhNode *node); void RenderKdNode(KdNode *node); bool RenderScene(); void _RenderScene(); void _RenderSceneTriangles(); void _RenderSceneTrianglesWithDrawArrays(); void RenderViewPoint(); void _RenderDynamicObject(SceneGraphLeaf *leaf); virtual void SetupProjection(const int w, const int h, const float angle = 70.0f); virtual bool ValidViewPoint(); virtual float GetPixelError(int &pvsSize); virtual void EvalPvsStat(); /** Prepare the pvs for rendering. */ virtual void PreparePvs(const ObjectPvs &pvs); /** Helper function that recursively prepares the indices for VBO renderin. */ void _UpdatePvsIndices(KdNode *node, int &indexBufferSize); virtual void EvalPvsStat(const SimpleRayContainer &viewPoints); virtual void InitGL(); virtual int GetWidth() const { return 0; } virtual int GetHeight() const { return 0; } unsigned int GetId(const unsigned char r, const unsigned char g, const unsigned char b) const; inline const bool GetSnapErrorFrames() { return mSnapErrorFrames; } inline const std::string GetSnapPrefix() { return mSnapPrefix; } inline void SetSnapErrorFrames(bool snapframes) { mSnapErrorFrames = snapframes; } inline void SetSnapPrefix(const std::string &pref) { mSnapPrefix = pref; } virtual void ClearErrorBuffer(); virtual float GetAvgPixelError() { return mPvsStat.GetAvgError()*GetWidth()*GetHeight(); } virtual float GetMaxPixelError() { return mPvsStat.GetMaxError()*GetWidth()*GetHeight(); } void SetViewPoint(const Vector3 &vp) { mViewPoint = vp; } void SetViewDirection(const Vector3 &vd) { mViewDirection = vd; } Vector3 GetViewPoint() const { return mViewPoint; } public: int mCurrentFrame; int mFrame; bool mWireFrame; PvsRenderStatistics mPvsStat; unsigned int mIndexBufferSize; int mPvsStatFrames; struct PvsErrorEntry { PvsErrorEntry() {} float mError; int mPvsSize; Vector3 mPosition; Vector3 mDirection; }; vector mPvsErrorBuffer; bool mComputeGVS; protected: void CreateVertexArrays(SceneGraphLeaf *leaf); void DeleteVbos(); void EnableDrawArrays(); void DisableDrawArrays(); void RenderKdLeaf(KdLeaf *leaf); ////////////////// PvsCache mPvsCache; unsigned int mVboId; vector mOcclusionQueries; ObjectContainer mObjects; Vector3 mViewPoint; Vector3 mViewDirection; int timerId; bool mUseFalseColors; bool mUseForcedColors; HaltonSequence halton; bool mDetectEmptyViewSpace; bool mSnapErrorFrames; bool mRenderBoxes; bool mUseGlLists; std::string mSnapPrefix; GLUquadric *mSphere; KdTree *mKdTree; Vector3 *mData; unsigned int *mIndices; bool mUseVbos; }; /* Class implementing an OpenGl render buffer. */ class GlRendererBuffer: public GlRenderer { public: GlRendererBuffer(SceneGraph *sceneGraph, ViewCellsManager *viewcells, KdTree *tree); virtual ~GlRendererBuffer(); /** Evaluates render cost of a point sample. @param sample the render cost sample to be evaluated @param useOcclusionQueries if occlusion queries should be used or item buffer @param threshold number of pixels / samples from where an object is considered visible. */ virtual void EvalRenderCostSample(RenderCostSample &sample, const bool useOcclusionQueries, const int threshold); /** Evaluates render cost of a number of point samples. The point samples are distributed uniformly over the defined view space. @param numSamples the number of point samples taken @param samples stores the taken point samples in a container @param useOcclusionQueries if occlusion queries should be used or item buffer @param threshold number of pixels / samples from where an object is considered visible. */ virtual void SampleRenderCost(const int numSamples, vector &samples, const bool useOcclusionQueries, const int threshold = 0); /** Implerment in subclasses. */ virtual void EvalPvsStat(); virtual void EvalPvsStat(const SimpleRayContainer &viewPoints); virtual int GetWidth() const = 0; virtual int GetHeight() const = 0; virtual void MakeCurrent() = 0; virtual void DoneCurrent() = 0; virtual bool ValidViewPoint(); virtual void SampleBeamContributions( Intersectable *sourceObject, Beam &beam, const int samples, BeamSampleStatistics &stat ); virtual void SampleViewpointContributions( Intersectable *sourceObject, const Vector3 viewPoint, Beam &beam, const int desiredSamples, BeamSampleStatistics &stat ); virtual void InitGL(); /** Computes rays from information gained with hw sampling- */ virtual void ComputeRays(Intersectable *sourceObj, VssRayContainer &rays); virtual int ComputePvs() const = 0; virtual int ComputePvs(ObjectContainer &objects, ObjectContainer &pvs) const = 0; protected: unsigned int *mPixelBuffer; static void GenQueries(int numQueries); virtual void SetupProjectionForViewPoint(const Vector3 &viewPoint, const Beam &beam, Intersectable *sourceObject); /** Evaluates query for one direction using item buffer. */ virtual void EvalQueryWithItemBuffer(); /** Evaluates query for one direction using occlusion queries. */ virtual void EvalQueryWithOcclusionQueries(); }; /** Abstract class for implmenenting a gl render widget. */ class GlRendererWidget: public GlRenderer { public: GlRendererWidget(SceneGraph *sceneGraph, ViewCellsManager *vcm, KdTree *kdTree): GlRenderer(sceneGraph, vcm, kdTree) {} GlRendererWidget() {} virtual ~GlRendererWidget() {} virtual void Show() {} }; }; #endif