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

Revision 1004, 9.5 KB checked in by mattausch, 18 years ago (diff)

environment as a singleton

  • Property svn:executable set to *
Line 
1#ifndef __GLRENDERER_H
2#define __GLRENDERER_H
3
4//#include "glInterface.h"
5#include <QtOpenGL>
6#include <QWaitCondition>
7
8//#include <QGLPixelBuffer>
9
10#include "Vector3.h"
11#include "Containers.h"
12#include "Halton.h"
13#include "Renderer.h"
14#include "Beam.h"
15
16class QWidget;
17
18namespace GtpVisibilityPreprocessor {
19
20class SceneGraph;
21class ViewCellsManager;
22class Mesh;
23class MeshInstance;
24class Intersectable;
25class Material;
26class Beam;
27class KdTree;
28class GlRendererBuffer;
29class BeamSampleStatistics;
30class OcclusionQuery;
31class TransformedMeshInstance;
32
33struct VssRayContainer;
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
60struct RenderCostSample {
61
62  RenderCostSample() {}
63
64  void Reset() {
65        mVisibleObjects = 0;
66        mVisiblePixels = 0;
67  }
68 
69  Vector3 mPosition;
70
71  // visible object from the given point
72  int mVisibleObjects;
73
74  // visible pixels
75  int mVisiblePixels;
76 
77};
78
79/** Class encapsulating gl rendering for the scene.
80        There is no gl context binding so the binding is performed in the
81        derived classes
82*/
83
84class GlRenderer: public Renderer
85{
86
87public:
88  ObjectContainer mObjects;
89   
90  Vector3 mViewPoint;
91  Vector3 mViewDirection;
92
93  int timerId;
94  bool mUseFalseColors;
95  bool mUseForcedColors;
96
97  HaltonSequence halton;
98 
99  int mFrame;
100  bool mWireFrame;
101 
102  bool mDetectEmptyViewSpace;
103  bool mSnapErrorFrames;
104
105  bool mUseGlLists;
106 
107  QString mSnapPrefix;
108
109  KdTree *mKdTree;
110
111  QWaitCondition mRenderingFinished;
112 
113  vector<OcclusionQuery *> mOcclusionQueries;
114 
115  GlRenderer(SceneGraph *sceneGraph,
116                         ViewCellsManager *viewcells,
117                         KdTree *tree);
118 
119  ~GlRenderer();
120
121
122  void SetupFalseColor(const int id);
123  void RenderIntersectable(Intersectable *);
124  void RenderViewCell(ViewCell *vc);
125  void RenderMeshInstance(MeshInstance *mi);
126  void RenderTransformedMeshInstance(TransformedMeshInstance *mi);
127  void RenderMesh(Mesh *m);
128  void SetupMaterial(Material *m);
129  virtual void SetupCamera();
130 
131  bool
132  RenderScene();
133
134  void
135  _RenderScene();
136
137
138  virtual void
139  SetupProjection(const int w, const int h, const float angle = 70.0f);
140
141 
142 
143
144  void InitGL();
145
146  virtual int GetWidth() const = 0;
147  virtual int GetHeight() const = 0;
148
149  int GetId(int r, int g, int b) const;
150};
151
152
153class GlRendererBuffer : public QObject, public QGLPixelBuffer, public GlRenderer
154{
155Q_OBJECT
156public:
157GlRendererBuffer(const int w,
158                                 const int h,
159                                 SceneGraph *sceneGraph,
160                                 ViewCellsManager *viewcells,
161                                 KdTree *tree);
162
163
164        /** Evaluates render cost of a point sample.
165                @param sample the render cost sample to be evaluated
166                @param useOcclusionQueries if occlusion queries should be used or item buffer
167                @param threshold number of pixels / samples from where an object is considered visible.
168        */
169        void EvalRenderCostSample(RenderCostSample &sample,
170                                                          const bool useOcclusionQueries,
171                                                          const int threshold);
172
173        /** Evaluates render cost of a number of point samples. The point samples
174                are distributed uniformly over the defined view space.
175
176                @param numSamples the number of point samples taken
177                @param samples stores the taken point samples in a container
178                @param useOcclusionQueries if occlusion queries should be used or item buffer
179                @param threshold number of pixels / samples from where an object is considered visible.
180        */
181        void SampleRenderCost(const int numSamples,
182                                                  vector<RenderCostSample> &samples,
183                                                  const bool useOcclusionQueries,
184                                                  const int threshold = 0);
185
186
187  void
188  EvalPvsStat();
189
190  void
191  ClearErrorBuffer();
192 
193
194  virtual int GetWidth() const { return width(); }
195  virtual int GetHeight() const { return height(); }
196
197
198  void RandomViewPoint();
199  void SampleBeamContributions(
200                                                           Intersectable *sourceObject,
201                                                           Beam &beam,
202                                                           const int samples,
203                                                           BeamSampleStatistics &stat
204                                                           );
205
206  void
207  SampleViewpointContributions(
208                                                           Intersectable *sourceObject,
209                                                           const Vector3 viewPoint,
210                                                           Beam &beam,
211                                                           const int desiredSamples,
212                                                           BeamSampleStatistics &stat
213                                                           );
214
215  void InitGL();
216
217  /** Computes rays from information gained with hw sampling-
218  */
219  void ComputeRays(Intersectable *sourceObj, VssRayContainer &rays);
220
221  int ComputePvs() const;
222
223  float
224  GetPixelError(int &pvsSize);
225
226  int ComputePvs(ObjectContainer &objects, ObjectContainer &pvs) const;
227
228  PvsRenderStatistics mPvsStat;
229   
230  int mPvsStatFrames;
231  struct PvsErrorEntry {
232        PvsErrorEntry() {}
233        float mError;
234        int mPvsSize;
235        Vector3 mPosition;
236        Vector3 mDirection;
237  };
238 
239  vector<PvsErrorEntry> mPvsErrorBuffer;
240
241 
242private:
243  unsigned int *mPixelBuffer;
244 
245        static void GenQueries(const int numQueries);
246       
247        void SetupProjectionForViewPoint(const Vector3 &viewPoint,
248                                                                         const Beam &beam,
249
250                                                                         Intersectable *sourceObject);
251
252        /** Evaluates query for one direction using item buffer.
253        */
254        void EvalQueryWithItemBuffer();
255        /** Evaluates query for one direction using occlusion queries.
256        */
257        void EvalQueryWithOcclusionQueries();
258
259public:
260  signals:
261        UpdatePvsErrorItem(int i,
262                                           GlRendererBuffer::PvsErrorEntry &);
263};
264
265
266class RendererControlWidget : public QWidget
267{
268  Q_OBJECT
269public:
270
271  QListWidget *mPvsErrorWidget;
272
273  RendererControlWidget(QWidget * parent = 0, Qt::WFlags f = 0);
274
275  public slots:
276
277  void FocusNextPvsErrorFrame();
278  void UpdatePvsErrorItem(int i,
279                                                  GlRendererBuffer::PvsErrorEntry &);
280
281  signals:
282 
283  SetViewCellGranularity(int);
284  SetSceneCut(int);
285  SetTopDistance(int);
286  SetVisibilityFilterSize(int);
287  SetSpatialFilterSize(int);
288
289  SetRenderFilter(bool);
290  SetUseFilter(bool);
291  SetUseSpatialFilter(bool);
292  SetRenderErrors(bool);
293  SetShowViewCells(bool);
294  SetShowRenderCost(bool);
295  SetShowPvsSizes(bool);
296  SetTopView(bool);
297  SetCutViewCells(bool);
298  SetCutScene(bool);
299
300 
301};
302
303class GlRendererWidget : public QGLWidget, public GlRenderer
304{
305  Q_OBJECT
306public:
307
308  // point of the last mouse click used for movement in the scene
309  Vector3 mousePoint;
310
311  bool mTopView;
312  bool mRenderViewCells;
313  bool mCutViewCells;
314  bool mCutScene;
315  bool mRenderErrors;
316  bool mRenderFilter;
317  bool mUseFilter;
318  bool mUseSpatialFilter;
319  bool mShowRenderCost;
320 
321  bool mShowPvsSizes;
322  float mSpatialFilterSize;
323 
324  Plane3 mSceneCutPlane;
325  float mTopDistance;
326
327  // some statistics
328  int mPvsSize;
329  float mRenderError;
330
331  RendererControlWidget *mControlWidget;
332 
333  GlRendererWidget(SceneGraph *sceneGraph,
334                                   ViewCellsManager *viewcells,
335                                   KdTree *tree,
336                                   QWidget * parent = 0, const QGLWidget * shareWidget = 0, Qt::WFlags f = 0
337                                   );
338
339
340  virtual void SetupCamera();
341
342  void initializeGL() {
343        InitGL();
344  }
345  void resizeGL(int w, int h);
346  void paintGL();
347  void timerEvent(QTimerEvent *) {
348          update();
349  }
350
351  void mousePressEvent(QMouseEvent *);
352  void mouseReleaseEvent(QMouseEvent *);
353  void mouseMoveEvent(QMouseEvent *);
354
355  void keyPressEvent ( QKeyEvent * e ) ;
356
357  void
358  RenderPvs();
359
360  float
361  RenderErrors();
362  void
363  RenderInfo();
364
365  virtual int GetWidth() const { return width(); }
366  virtual int GetHeight() const { return height(); }
367
368 virtual void
369  SetupProjection(const int w, const int h, const float angle = 70.0f);
370
371  void
372  RenderViewCells();
373
374 public slots:
375
376 void SetRenderErrors(bool b) {
377   mRenderErrors = b;
378   updateGL();
379 }
380
381 void SetRenderFilter(bool b) {
382   mRenderFilter = b;
383   updateGL();
384 }
385
386  void SetUseFilter(bool b) {
387   mUseFilter = b;
388   updateGL();
389 }
390
391  void SetUseSpatialFilter(bool b) {
392        mUseSpatialFilter = b;
393        updateGL();
394  }
395
396 
397  void
398  SetViewCellGranularity(int number);
399 
400  void
401  SetVisibilityFilterSize(int number);
402
403  void
404  SetSpatialFilterSize(int number);
405 
406  void
407  SetSceneCut(int cut);
408 
409  void
410  SetTopDistance(int dist);
411 
412  void SetShowViewCells(bool b) {
413        mRenderViewCells = b;
414        updateGL();
415  }
416
417  void SetShowRenderCost(bool b) {
418        mShowRenderCost = b;
419        updateGL();
420  }
421
422  void SetShowPvsSizes(bool b) {
423        mShowPvsSizes = b;
424        updateGL();
425  }
426
427  void SetTopView(bool b) {
428        mTopView = b;
429        updateGL();
430  }
431
432  void SetCutViewCells(bool b) {
433        mCutViewCells = b;
434        updateGL();
435  }
436  void SetCutScene(bool b) {
437        mCutScene = b;
438        updateGL();
439  }
440
441
442};
443
444
445extern GlRendererWidget *rendererWidget;
446
447class GlDebuggerWidget : public QGLWidget
448{
449        Q_OBJECT
450public:
451    GlDebuggerWidget(GlRendererBuffer *buf, QWidget *parent = NULL);
452    ~GlDebuggerWidget();
453    void initializeGL();
454    void resizeGL(int w, int h);
455    void paintGL();
456    void timerEvent(QTimerEvent *) { update(); }
457    void mousePressEvent(QMouseEvent *) { killTimer(timerId); }
458    void mouseReleaseEvent(QMouseEvent *) { timerId = startTimer(20); }
459
460    void initCommon();
461    void initPbuffer();
462
463 
464         GlRendererBuffer *mRenderBuffer;
465
466         Beam mBeam;
467         int mSamples;
468         Intersectable *mSourceObject;
469
470private:
471    GLuint dynamicTexture;
472    int timerId;
473};
474
475extern GlDebuggerWidget *debuggerWidget;
476
477};
478
479#endif
480
Note: See TracBrowser for help on using the repository browser.