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

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