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

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