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

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