Developer Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 
450  ACG::SceneGraph::BaseNode* getRenderObjectNode(int i);
451 
453  ACG::SceneGraph::BaseNode* getOverlayRenderObjectNode(int i);
454 
455 
456 
465  void setLineThicknessRenderingGL42(bool _enable);
466 
467 
468  //=========================================================================
469  // Internally called by OpenFlipper core
470  //=========================================================================
471 public:
472 
481  void setViewerID(int _viewerID);
482 
492  return current_subtree_objects_;
493  }
494 
495 protected:
498 
500  LightData lights_[SG_MAX_SHADER_LIGHTS];
501 
505 
507  std::vector<ACG::RenderObject> renderObjects_;
508 
510  std::vector<int> sortListObjects_;
512  std::vector<int> sortListOverlays_;
513 
515  std::vector<ACG::RenderObject*> sortedObjects_;
516 
518  std::vector<ACG::RenderObject*> overlayObjects_;
519 
521  std::vector<ACG::RenderObjectModifier*> renderObjectModifiers_;
522 
524  std::vector<ACG::SceneGraph::BaseNode*> renderObjectSource_;
525 
527  std::vector<ACG::SceneGraph::BaseNode*> overlayObjectSource_;
528 
534  std::map<int, ACG::FBO*> depthMaps_;
535 
538 
541 
544 
547 
550 
552  GLint prevFbo_;
553 
556 
558  GLint prevViewport_[4];
559 
562 
565 
568 
570  static int maxClipDistances_;
571 
572  RenderObjectRange current_subtree_objects_;
573 private:
574 
575  //=========================================================================
576  // Default rendering of thick lines
577  //=========================================================================
578 
585 
587  std::vector<RenderObject*> lineGL42Objects_;
588 
590  std::map< int, Texture* > lineColorBuffers_;
591 
592  void renderLineThicknessGL42();
593 
594 
595 };
596 
597 
598 
599 //=============================================================================
600 } // namespace ACG
601 //=============================================================================
Vec3f camPosWS_
cam position in world-space
Definition: IRenderer.hh:546
GLSL::Program * depthCopyShader_
shader copies depth of the first front layer to the back buffer
Definition: IRenderer.hh:564
std::map< int, Texture * > lineColorBuffers_
map from viewport id to line buffer
Definition: IRenderer.hh:590
ACG::Vec3f globalLightModelAmbient_
Definition: IRenderer.hh:504
std::vector< int > sortListOverlays_
map sortedID -> original renderObjectID
Definition: IRenderer.hh:512
int numLights_
Number of Lights.
Definition: IRenderer.hh:497
const ACG::Vec3f & getGlobalAmbientScale() const
Get global ambient light contribution from GL_LIGHT_MODEL_AMBIENT.
Definition: IRenderer.hh:446
Namespace providing different geometric functions concerning angles.
Definition: DBSCANT.cc:51
std::vector< ACG::RenderObjectModifier * > renderObjectModifiers_
active render object modifiers
Definition: IRenderer.hh:521
DrawModeProperties stores a set of properties that defines, how to render an object.
Definition: DrawModes.hh:183
std::vector< ACG::RenderObject * > sortedObjects_
sorted list of renderobjects without overlay objects (sorted in rendering order)
Definition: IRenderer.hh:515
const RenderObjectRange & getCollectedSubtreeObjects() const
Definition: IRenderer.hh:491
GLMatrixf viewMatrix_
view transform
Definition: IRenderer.hh:543
std::vector< ACG::SceneGraph::BaseNode * > overlayObjectSource_
source node that added the overlay render object (map renderObjectID -> scenegraph node) ...
Definition: IRenderer.hh:527
std::vector< RenderObject * > lineGL42Objects_
default line render objects that are rendered with gl4.2
Definition: IRenderer.hh:587
GLint prevDrawBuffer_
previous drawbuffer target (ie GL_BACK, GL_COLOR_ATTACHMENTi...)
Definition: IRenderer.hh:555
Vec3f camDirWS_
direction the camera is looking to in world-space
Definition: IRenderer.hh:549
std::vector< ACG::SceneGraph::BaseNode * > renderObjectSource_
source node that added the render object (map renderObjectID -> scenegraph node)
Definition: IRenderer.hh:524
bool enableLineThicknessGL42_
Enable/disable gl4.2 based technique for rendering thick lines.
Definition: IRenderer.hh:584
int errorDetectionLevel_
error-detection level for checking render objects
Definition: IRenderer.hh:567
std::vector< ACG::RenderObject * > overlayObjects_
sorted list of overlay-only renderobjects (sorted in rendering order)
Definition: IRenderer.hh:518
bool depthMapUsed_
true if at least one renderobject requires a scene depthmap, false otherwise
Definition: IRenderer.hh:537
GLint prevFbo_
previous fbo
Definition: IRenderer.hh:552
static int maxClipDistances_
max number of clip distance outputs in a vertex shader
Definition: IRenderer.hh:570
Interface for modifying render objects.
std::map< int, ACG::FBO * > depthMaps_
Definition: IRenderer.hh:534
bool prevFboSaved_
flag indicating a that saveCurrentFbo() has been called prior restoreFbo()
Definition: IRenderer.hh:561
GLSL program class.
Definition: GLSLShader.hh:217
std::vector< ACG::RenderObject > renderObjects_
array of renderobjects, filled by addRenderObject()
Definition: IRenderer.hh:507
std::vector< int > sortListObjects_
map sortedID -> original renderObjectID
Definition: IRenderer.hh:510
This namespace contains all the classes and functions for handling GLSL shader and program objects...
Definition: AntiAliasing.hh:75
Interface class between scenegraph and renderer.
int curViewerID_
currently active viewer ID as specified in prepareRenderObjects()
Definition: IRenderer.hh:540