source: GTP/trunk/Lib/Vis/Preprocessing/src/GlRenderer.h @ 2730

Revision 2730, 9.0 KB checked in by mattausch, 16 years ago (diff)
  • Property svn:executable set to *
Line 
1#ifndef __GLRENDERER_H
2#define __GLRENDERER_H
3
4#include "ObjectPvs.h"
5#include "Vector3.h"
6#include "Containers.h"
7#include "Halton.h"
8#include "Renderer.h"
9#include "Beam.h"
10
11
12
13namespace GtpVisibilityPreprocessor {
14
15class SceneGraph;
16class ViewCellsManager;
17class Mesh;
18class MeshInstance;
19class Intersectable;
20class Material;
21class Beam;
22class KdTree;
23class GlRendererBuffer;
24class BeamSampleStatistics;
25class OcclusionQuery;
26class TransformedMeshInstance;
27class TriangleIntersectable;
28class BvhNode;
29class SimpleRayContainer;
30class SceneGraphLeaf;
31class Preprocessor;
32
33struct VssRayContainer;
34struct GLUquadric;
35
36
37struct PvsRenderStatistics
38{
39  float maxError;
40  float sumError;
41  int sumPvsSize;
42  int frames;
43  int errorFreeFrames;
44  int currentPass;
45
46  PvsRenderStatistics(): currentPass(0) { Reset(); }
47 
48  void Reset()
49  {
50        maxError = 0.0f;
51        sumError = 0.0f;
52        frames = 0;
53        errorFreeFrames = 0;
54        sumPvsSize = 0;
55        //cuurentPass = 0;
56  }
57
58  float GetMaxError() { return maxError; }
59  float GetAvgError() { return sumError/frames; }
60  float GetErrorFreeFrames() { return errorFreeFrames/(float)frames; }
61  float GetAvgPvs() { return sumPvsSize/(float)frames; }
62 
63};
64
65
66struct PvsCache
67{
68        PvsCache():mViewCell(NULL), mUnfilteredPvsSize(0) {}
69
70        void Reset()
71        {
72                mViewCell = NULL;
73                mPvs.Clear(false);
74                filteredBoxes.clear();
75                mUnfilteredPvsSize = 0;
76        }
77
78        ViewCell *mViewCell;
79        ObjectPvs mPvs;
80        int mUnfilteredPvsSize;
81        vector<AxisAlignedBox3> filteredBoxes;
82};
83
84
85struct RenderCostSample {
86
87  RenderCostSample() {}
88
89  void Reset() {
90        mVisibleObjects = 0;
91        mVisiblePixels = 0;
92  }
93 
94  Vector3 mPosition;
95
96  // visible object from the given point
97  int mVisibleObjects;
98
99  // visible pixels
100  int mVisiblePixels;
101
102  ObjectPvs mPvs;
103 
104};
105
106/** Class encapsulating gl rendering for the scene.
107        There is no gl context binding so the binding is performed in the
108        derived classes
109*/
110class GlRenderer: public Renderer
111{
112friend class GlobalLinesRenderer;
113friend class ViewCellsManager;
114
115public:
116
117  GlRenderer(SceneGraph *sceneGraph,
118                         ViewCellsManager *viewcells,
119                         KdTree *tree);
120 
121  GlRenderer() {}
122
123  virtual ~GlRenderer();
124
125
126  virtual void RandomViewPoint();
127
128  void SetupFalseColor(const unsigned int id);
129  void RenderIntersectable(Intersectable *);
130  void RenderViewCell(ViewCell *vc);
131  void RenderMeshInstance(MeshInstance *mi);
132  void RenderTransformedMeshInstance(TransformedMeshInstance *mi);
133  void RenderMesh(Mesh *m);
134  void SetupMaterial(Material *m);
135  virtual void SetupCamera();
136
137  void
138  RenderRays(const VssRayContainer &rays, int colorType = 0, int showDistribution = 15, int maxAge = 9999999);
139
140  void
141  RenderTriangle(TriangleIntersectable *object);
142
143  void
144  RenderBox(const AxisAlignedBox3 &box);
145
146  void
147  RenderBvhNode(BvhNode *node);
148
149  void
150  RenderKdNode(KdNode *node);
151
152  bool
153  RenderScene();
154
155  void
156  _RenderScene();
157
158  void
159  _RenderSceneTriangles();
160
161  void _RenderSceneTrianglesWithDrawArrays();
162
163  void
164  RenderViewPoint();
165
166  void _RenderDynamicObject(SceneGraphLeaf *leaf);
167
168  virtual void
169  SetupProjection(const int w, const int h, const float angle = 70.0f);
170
171  virtual bool ValidViewPoint();
172 
173  /** Gets the pixel error of the current view point.
174        returns the size of the pvs for the current view point. pass returns the
175        current pass of the evaluation.
176  */
177  virtual float GetPixelError(int &pvsSize, int pass);
178 
179  virtual void EvalPvsStat();
180
181  /** Prepare the pvs for rendering.
182  */
183  virtual void PreparePvs(const ObjectPvs &pvs);
184  /** Helper function that recursively prepares the indices for VBO renderin.
185  */
186  void _UpdatePvsIndices(KdNode *node, int &indexBufferSize);
187
188       
189  virtual void EvalPvsStat(const SimpleRayContainer &viewPoints);
190
191  virtual void InitGL();
192
193  virtual int GetWidth() const {return 0;}
194  virtual int GetHeight() const {return 0;}
195
196  unsigned int GetId(const unsigned char r,
197                                         const unsigned char g,
198                                         const unsigned char b) const;
199
200  inline const bool GetSnapErrorFrames() { return mSnapErrorFrames; }
201  inline const std::string GetSnapPrefix() { return mSnapPrefix; }
202
203  inline void SetSnapErrorFrames(bool snapframes) { mSnapErrorFrames = snapframes; }
204  inline void SetSnapPrefix(const std::string &pref) { mSnapPrefix = pref; }
205
206  virtual void ClearErrorBuffer();
207 
208  virtual float GetAvgPixelError() {
209        return mPvsStat.GetAvgError()*GetWidth()*GetHeight();
210  }
211
212  virtual float GetMaxPixelError() {
213          return mPvsStat.GetMaxError()*GetWidth()*GetHeight();
214  }
215
216  void SetViewPoint(const Vector3 &vp) { mViewPoint = vp; }
217  void SetViewDirection(const Vector3 &vd) { mViewDirection = vd; }
218
219  Vector3 GetViewPoint() const { return mViewPoint; }
220
221  bool GetUseVbos() const { return mUseVbos; }
222
223
224public:
225
226        int mCurrentFrame;
227        int mFrame;
228        bool mWireFrame;
229
230        PvsRenderStatistics mPvsStat;
231
232        unsigned int mIndexBufferSize;
233
234        int mPvsStatFrames;
235        struct PvsErrorEntry {
236                PvsErrorEntry() {}
237                float mError;
238                int mPvsSize;
239                Vector3 mPosition;
240                Vector3 mDirection;
241        };
242
243        Preprocessor *GetPreprocessor();
244
245
246        vector<PvsErrorEntry> mPvsErrorBuffer;
247
248        bool mComputeGVS;
249
250protected:
251
252        void CreateVertexArrays(SceneGraphLeaf *leaf);
253        void DeleteVbos();
254        void EnableDrawArrays();
255        void DisableDrawArrays();
256
257        void RenderKdLeaf(KdLeaf *leaf);
258
259       
260        //////////////////
261
262        PvsCache mPvsCache;
263
264        unsigned int mVboId;
265        vector<OcclusionQuery *> mOcclusionQueries;
266
267        ObjectContainer mObjects;
268
269        Vector3 mViewPoint;
270        Vector3 mViewDirection;
271
272        int timerId;
273        bool mUseFalseColors;
274        bool mUseForcedColors;
275
276        HaltonSequence halton;
277
278
279        bool mDetectEmptyViewSpace;
280        bool mSnapErrorFrames;
281
282        bool mRenderBoxes;
283
284        bool mUseGlLists;
285
286        std::string mSnapPrefix;
287
288        GLUquadric *mSphere;
289
290        KdTree *mKdTree;
291
292        Vector3 *mData;
293        unsigned int *mIndices;
294
295        bool mUseVbos;
296
297        int mRenderedNodes;
298};
299
300
301/* Class implementing an OpenGl render buffer.
302*/
303class GlRendererBuffer: public GlRenderer
304{
305
306public:
307
308        GlRendererBuffer(SceneGraph *sceneGraph,
309                                         ViewCellsManager *viewcells,
310                                         KdTree *tree);
311
312        virtual ~GlRendererBuffer();
313
314        /** Evaluates render cost of a point sample.
315        @param sample the render cost sample to be evaluated
316        @param useOcclusionQueries if occlusion queries should be used or item buffer
317        @param threshold number of pixels / samples from where an object is considered visible.
318        */
319        virtual void EvalRenderCostSample(RenderCostSample &sample,
320                const bool useOcclusionQueries,
321                const int threshold);
322
323        /** Evaluates render cost of a number of point samples. The point samples
324        are distributed uniformly over the defined view space.
325
326        @param numSamples the number of point samples taken
327        @param samples stores the taken point samples in a container
328        @param useOcclusionQueries if occlusion queries should be used or item buffer
329        @param threshold number of pixels / samples from where an object is considered visible.
330        */
331        virtual void SampleRenderCost(const int numSamples,
332                                                                  vector<RenderCostSample> &samples,
333                                                                  const bool useOcclusionQueries,
334                                                                  const int threshold = 0);
335        /** Implerment in subclasses.
336        */
337        virtual void EvalPvsStat();
338
339        virtual void EvalPvsStat(const SimpleRayContainer &viewPoints);
340
341        virtual int GetWidth() const = 0;
342        virtual int GetHeight() const  = 0;
343
344        virtual void MakeLive() = 0;
345        virtual void DoneLive() = 0;
346
347        virtual bool ValidViewPoint();
348
349        virtual void SampleBeamContributions(
350                                                                                 Intersectable *sourceObject,
351                                                                                 Beam &beam,
352                                                                                 const int samples,
353                                                                                 BeamSampleStatistics &stat
354                                                                                 );
355
356        virtual void SampleViewpointContributions(
357                                                                                          Intersectable *sourceObject,
358                                                                                          const Vector3 viewPoint,
359                                                                                          Beam &beam,
360                                                                                          const int desiredSamples,
361                                                                                          BeamSampleStatistics &stat
362                                                                                          );
363
364        virtual void InitGL();
365        /** Computes rays from information gained with hw sampling-
366        */
367        virtual void ComputeRays(Intersectable *sourceObj, VssRayContainer &rays);
368
369        virtual int ComputePvs() const = 0;
370
371        virtual int ComputePvs(ObjectContainer &objects, ObjectContainer &pvs) const = 0;
372
373
374protected:
375
376        unsigned int *mPixelBuffer;
377
378        static void GenQueries(int numQueries);
379
380        virtual void SetupProjectionForViewPoint(const Vector3 &viewPoint,
381                                                                                         const Beam &beam,
382                                                                                         Intersectable *sourceObject);
383        /** Evaluates query for one direction using item buffer.
384        */
385        virtual void EvalQueryWithItemBuffer();
386        /** Evaluates query for one direction using occlusion queries.
387        */
388        virtual void EvalQueryWithOcclusionQueries();
389};
390
391
392/** Abstract class for implmenenting a gl render widget.
393*/
394class GlRendererWidget: public GlRenderer
395{
396public:
397
398        GlRendererWidget(SceneGraph *sceneGraph, ViewCellsManager *vcm, KdTree *kdTree):
399          GlRenderer(sceneGraph, vcm, kdTree)
400          {}
401
402          GlRendererWidget() {}
403
404          virtual ~GlRendererWidget() {}
405         
406          virtual void Show() {}
407};
408
409
410};
411
412#endif
Note: See TracBrowser for help on using the repository browser.