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

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