Developer Documentation
IRenderer.hh
1 /*===========================================================================*\
2  * *
3  * OpenFlipper *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openflipper.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenFlipper. *
11  *---------------------------------------------------------------------------*
12  * *
13  * Redistribution and use in source and binary forms, with or without *
14  * modification, are permitted provided that the following conditions *
15  * are met: *
16  * *
17  * 1. Redistributions of source code must retain the above copyright notice, *
18  * this list of conditions and the following disclaimer. *
19  * *
20  * 2. Redistributions in binary form must reproduce the above copyright *
21  * notice, this list of conditions and the following disclaimer in the *
22  * documentation and/or other materials provided with the distribution. *
23  * *
24  * 3. Neither the name of the copyright holder nor the names of its *
25  * contributors may be used to endorse or promote products derived from *
26  * this software without specific prior written permission. *
27  * *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39  * *
40 \*===========================================================================*/
41 
42 /*===========================================================================*\
43  * *
44  * $Revision$ *
45  * $Author$ *
46  * $Date$ *
47  * *
48 \*===========================================================================*/
49 
50 #pragma once
51 
52 
53 #include <ACG/GL/gl.hh>
54 #include <ACG/Math/GLMatrixT.hh>
55 #include <ACG/GL/ShaderGenerator.hh>
56 #include <ACG/GL/RenderObject.hh>
57 
59 #include <ACG/Scenegraph/MaterialNode.hh>
60 
61 
62 
63 namespace GLSL{
64  class Program;
65 }
66 
67 namespace ACG
68 {
69 
70 // forward declaration
71 class VertexDeclaration;
72 class GLState;
73 class FBO;
74 class Texture;
75 class Texture2D;
76 
77 namespace SceneGraph {
78  namespace DrawModes {
79  class DrawModeProperties;
80  }
81  class Material;
82 }
83 
84 class ACGDLLEXPORT IRenderer
85 {
86 public:
87  IRenderer();
88  virtual ~IRenderer();
89 
90 
91 public:
92  //=========================================================================
93  // Callbacks for the scenegraph nodes
94  //=========================================================================
95 
108  virtual void addRenderObject(RenderObject* _renderObject);
109 
110  struct LightData
111  {
112  LightData()
113  : ltype(ACG::SG_LIGHT_DIRECTIONAL),
114  diffuse(1.0f, 1.0f, 1.0f), ambient(1.0f, 1.0f, 1.0f), specular(1.0f, 1.0f, 1.0f),
115  pos(0.0f, 0.0f, 0.0f), dir(1.0f, 0.0f, 0.0f), atten(1.0f, 0.0f, 0.0f),
116  spotCutoffExponent(0.0f, 0.0f)
117  { }
118 
119  ACG::ShaderGenLightType ltype; // directional, spot- or pointlight
120  ACG::Vec3f diffuse, ambient, specular; // light color factor
121  ACG::Vec3f pos, dir; // position, direction in view-space
122  ACG::Vec3f atten; // (constant, linear, quadratic) attenuation
123  ACG::Vec2f spotCutoffExponent; // (cutoff angle, exponent) for spotlights
124  };
125 
127  public:
128  RenderObjectRange() {}
129  RenderObjectRange(std::vector<ACG::RenderObject>::iterator begin,
130  std::vector<ACG::RenderObject>::iterator end) :
131  begin_(begin), end_(end) {}
132 
133  std::vector<ACG::RenderObject>::iterator begin() const {
134  return begin_;
135  }
136 
137  std::vector<ACG::RenderObject>::iterator end() const {
138  return end_;
139  }
140 
141  private:
142  std::vector<ACG::RenderObject>::iterator begin_, end_;
143  };
144 
153  virtual void addLight(const LightData& _light);
154 
166  virtual void addRenderObjectModifier(RenderObjectModifier* _mod);
167 
176  virtual void removeRenderObjectModifier(RenderObjectModifier* _mod);
177 
178  //=========================================================================
179  // Render object collection and OpenGL setup for shader-based rendering
180  //=========================================================================
181 protected:
193  virtual void prepareRenderingPipeline(ACG::GLState* _glState, ACG::SceneGraph::DrawModes::DrawMode _drawMode, ACG::SceneGraph::BaseNode* _scenegraphRoot);
194 
203  virtual void collectRenderObjects(ACG::GLState* _glState, ACG::SceneGraph::DrawModes::DrawMode _drawMode, ACG::SceneGraph::BaseNode* _sceneGraphRoot);
204 
205 
213  void traverseRenderableNodes(ACG::GLState* _glState, ACG::SceneGraph::DrawModes::DrawMode _drawMode, ACG::SceneGraph::BaseNode &_node, const ACG::SceneGraph::Material &_mat);
214 
215 
216 
217  //=========================================================================
218  // Sorting
219  //=========================================================================
220 protected:
221 
226  virtual void sortRenderObjects();
227 
228 
229  //=========================================================================
230  // Rendering
231  //=========================================================================
232 protected:
233 
244  virtual void renderObject(ACG::RenderObject* _obj, GLSL::Program* _prog = 0, bool _constRenderStates = false, const std::vector<unsigned int>* _shaderModifiers = 0);
245 
253  virtual void bindObjectVBO(ACG::RenderObject* _obj,
254  GLSL::Program* _prog);
255 
263  virtual void bindObjectUniforms(ACG::RenderObject* _obj,
264  GLSL::Program* _prog);
265 
274  virtual void bindObjectRenderStates(ACG::RenderObject* _obj);
275 
276 
283  virtual void drawObject(ACG::RenderObject* _obj);
284 
285 
286  //=========================================================================
287  // Restore OpenGL State
288  //=========================================================================
289 
290 protected:
291 
292 
301  virtual void finishRenderingPipeline(bool _drawOverlay = true);
302 
305  virtual void saveInputFbo();
306 
309  virtual void restoreInputFbo();
310 
316  virtual void saveActiveFbo(GLint* _outFboId, GLint* _outViewport, GLint* _outDrawBuffer) const;
317 
323  virtual void restoreFbo(GLint _fboId, const GLint* _outViewport, GLint _drawBuffer) const;
324 
330  virtual void clearInputFbo(const ACG::Vec4f& _clearColor);
331 
332  //=========================================================================
333  // Other Convenience
334  //=========================================================================
335 
336 protected:
337 
350  virtual void copyDepthToBackBuffer(GLuint _depthTex, float _scale = 1.0f);
351 
352 
365  virtual void renderDepthMap(int _viewerID, int _width, int _height);
366 
367 
368  //=========================================================================
369  // Internal shader modifiers
370  //=========================================================================
371 protected:
372 
373  // depth map modifier: writes gl_FragCoord.z to red color channel
375  {
376  public:
377  void modifyFragmentEndCode(QStringList* _code);
378 
379  static DepthMapPass instance;
380  };
381 
382 
383  //=========================================================================
384  // Debugging
385  //=========================================================================
386 public:
387 
394  void dumpRenderObjectsToFile(const char* _fileName, ACG::RenderObject** _sortedList = 0) const;
395 
403  virtual QString dumpCurrentRenderObjectsToString(ACG::RenderObject** _list = 0, bool _outputShaders = false, std::vector<ACG::ShaderModifier*>* _modifiers = 0);
404 
416  void setErrorDetectionLevel(int _level);
417 
419  int getErrorDetectionLevel() const;
420 
421  //=========================================================================
422  // Variables
423  //=========================================================================
424 protected:
425 
427  int getNumRenderObjects() const;
428 
430  int getNumLights() const;
431 
433  ACG::RenderObject* getRenderObject(int i);
434 
436  ACG::RenderObject* getOverlayRenderObject(int i);
437 
439  ACG::RenderObject* getLineGL42RenderObject(int i);
440 
442  LightData* getLight(int i);
443 
444 
446  const ACG::Vec3f& getGlobalAmbientScale() const {return globalLightModelAmbient_;}
447 
448 
457  void setLineThicknessRenderingGL42(bool _enable);
458 
459 
460  //=========================================================================
461  // Internally called by OpenFlipper core
462  //=========================================================================
463 public:
464 
473  void setViewerID(int _viewerID);
474 
484  return current_subtree_objects_;
485  }
486 
487 protected:
490 
492  LightData lights_[SG_MAX_SHADER_LIGHTS];
493 
497 
499  std::vector<ACG::RenderObject> renderObjects_;
500 
501 
503  std::vector<ACG::RenderObject*> sortedObjects_;
504 
506  std::vector<ACG::RenderObject*> overlayObjects_;
507 
509  std::vector<ACG::RenderObjectModifier*> renderObjectModifiers_;
510 
516  std::map<int, ACG::FBO*> depthMaps_;
517 
520 
523 
526 
529 
532 
534  GLint prevFbo_;
535 
538 
540  GLint prevViewport_[4];
541 
544 
547 
550 
552  static int maxClipDistances_;
553 
554  RenderObjectRange current_subtree_objects_;
555 private:
556 
557  //=========================================================================
558  // Default rendering of thick lines
559  //=========================================================================
560 
567 
569  std::vector<RenderObject*> lineGL42Objects_;
570 
572  std::map< int, Texture* > lineColorBuffers_;
573 
574  void renderLineThicknessGL42();
575 
576 
577 };
578 
579 
580 
581 //=============================================================================
582 } // namespace ACG
583 //=============================================================================
GLMatrixf viewMatrix_
view transform
Definition: IRenderer.hh:525
int curViewerID_
currently active viewer ID as specified in prepareRenderObjects()
Definition: IRenderer.hh:522
DrawModeProperties stores a set of properties that defines, how to render an object.
Definition: DrawModes.hh:183
GLSL::Program * depthCopyShader_
shader copies depth of the first front layer to the back buffer
Definition: IRenderer.hh:546
std::vector< ACG::RenderObject * > sortedObjects_
sorted list of renderobjects without overlay objects (sorted in rendering order)
Definition: IRenderer.hh:503
int numLights_
Number of Lights.
Definition: IRenderer.hh:489
Interface for modifying render objects.
Vec3f camDirWS_
direction the camera is looking to in world-space
Definition: IRenderer.hh:531
std::vector< ACG::RenderObjectModifier * > renderObjectModifiers_
active render object modifiers
Definition: IRenderer.hh:509
static int maxClipDistances_
max number of clip distance outputs in a vertex shader
Definition: IRenderer.hh:552
const ACG::Vec3f & getGlobalAmbientScale() const
Get global ambient light contribution from GL_LIGHT_MODEL_AMBIENT.
Definition: IRenderer.hh:446
ACG::Vec3f globalLightModelAmbient_
Definition: IRenderer.hh:496
int errorDetectionLevel_
error-detection level for checking render objects
Definition: IRenderer.hh:549
bool enableLineThicknessGL42_
Enable/disable gl4.2 based technique for rendering thick lines.
Definition: IRenderer.hh:566
std::vector< ACG::RenderObject * > overlayObjects_
sorted list of overlay-only renderobjects (sorted in rendering order)
Definition: IRenderer.hh:506
Namespace providing different geometric functions concerning angles.
Definition: DBSCANT.cc:51
std::vector< RenderObject * > lineGL42Objects_
default line render objects that are rendered with gl4.2
Definition: IRenderer.hh:569
Vec3f camPosWS_
cam position in world-space
Definition: IRenderer.hh:528
bool prevFboSaved_
flag indicating a that saveCurrentFbo() has been called prior restoreFbo()
Definition: IRenderer.hh:543
GLint prevFbo_
previous fbo
Definition: IRenderer.hh:534
const RenderObjectRange & getCollectedSubtreeObjects() const
Definition: IRenderer.hh:483
This namespace contains all the classes and functions for handling GLSL shader and program objects...
Definition: AntiAliasing.hh:75
std::vector< ACG::RenderObject > renderObjects_
array of renderobjects, filled by addRenderObject()
Definition: IRenderer.hh:499
bool depthMapUsed_
true if at least one renderobject requires a scene depthmap, false otherwise
Definition: IRenderer.hh:519
GLint prevDrawBuffer_
previous drawbuffer target (ie GL_BACK, GL_COLOR_ATTACHMENTi...)
Definition: IRenderer.hh:537
Interface class between scenegraph and renderer.
std::map< int, Texture * > lineColorBuffers_
map from viewport id to line buffer
Definition: IRenderer.hh:572
std::map< int, ACG::FBO * > depthMaps_
Definition: IRenderer.hh:516
GLSL program class.
Definition: GLSLShader.hh:217