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#pragma once
45
46
47#include <ACG/GL/gl.hh>
48#include <ACG/Math/GLMatrixT.hh>
49#include <ACG/GL/ShaderGenerator.hh>
50#include <ACG/GL/RenderObject.hh>
51
53#include <ACG/Scenegraph/MaterialNode.hh>
54
55
56
57namespace GLSL{
58 class Program;
59}
60
61namespace ACG
62{
63
64// forward declaration
65class VertexDeclaration;
66class GLState;
67class FBO;
68class Texture;
69class Texture2D;
70
71namespace SceneGraph {
72 namespace DrawModes {
74 }
75 class Material;
76}
77
78class ACGDLLEXPORT IRenderer
79{
80public:
81 IRenderer();
82 virtual ~IRenderer();
83
84
85public:
86 //=========================================================================
87 // Callbacks for the scenegraph nodes
88 //=========================================================================
89
102 virtual void addRenderObject(RenderObject* _renderObject);
103
105 {
106 LightData()
107 : ltype(ACG::SG_LIGHT_DIRECTIONAL),
108 diffuse(1.0f, 1.0f, 1.0f), ambient(1.0f, 1.0f, 1.0f), specular(1.0f, 1.0f, 1.0f),
109 pos(0.0f, 0.0f, 0.0f), dir(1.0f, 0.0f, 0.0f), atten(1.0f, 0.0f, 0.0f),
110 spotCutoffExponent(0.0f, 0.0f)
111 { }
112
113 ACG::ShaderGenLightType ltype; // directional, spot- or pointlight
114 ACG::Vec3f diffuse, ambient, specular; // light color factor
115 ACG::Vec3f pos, dir; // position, direction in view-space
116 ACG::Vec3f atten; // (constant, linear, quadratic) attenuation
117 ACG::Vec2f spotCutoffExponent; // (cutoff angle, exponent) for spotlights
118 };
119
121 public:
123 RenderObjectRange(std::vector<ACG::RenderObject>::iterator begin,
124 std::vector<ACG::RenderObject>::iterator end) :
125 begin_(begin), end_(end) {}
126
127 std::vector<ACG::RenderObject>::iterator begin() const {
128 return begin_;
129 }
130
131 std::vector<ACG::RenderObject>::iterator end() const {
132 return end_;
133 }
134
135 private:
136 std::vector<ACG::RenderObject>::iterator begin_, end_;
137 };
138
147 virtual void addLight(const LightData& _light);
148
160 virtual void addRenderObjectModifier(RenderObjectModifier* _mod);
161
170 virtual void removeRenderObjectModifier(RenderObjectModifier* _mod);
171
172 //=========================================================================
173 // Render object collection and OpenGL setup for shader-based rendering
174 //=========================================================================
175protected:
187 virtual void prepareRenderingPipeline(ACG::GLState* _glState, ACG::SceneGraph::DrawModes::DrawMode _drawMode, ACG::SceneGraph::BaseNode* _scenegraphRoot);
188
197 virtual void collectRenderObjects(ACG::GLState* _glState, ACG::SceneGraph::DrawModes::DrawMode _drawMode, ACG::SceneGraph::BaseNode* _sceneGraphRoot);
198
199
207 void traverseRenderableNodes(ACG::GLState* _glState, ACG::SceneGraph::DrawModes::DrawMode _drawMode, ACG::SceneGraph::BaseNode &_node, const ACG::SceneGraph::Material &_mat);
208
209
210
211 //=========================================================================
212 // Sorting
213 //=========================================================================
214protected:
215
220 virtual void sortRenderObjects();
221
222
223 //=========================================================================
224 // Rendering
225 //=========================================================================
226protected:
227
238 virtual void renderObject(ACG::RenderObject* _obj, GLSL::Program* _prog = 0, bool _constRenderStates = false, const std::vector<unsigned int>* _shaderModifiers = 0);
239
247 virtual void bindObjectVBO(ACG::RenderObject* _obj,
248 GLSL::Program* _prog);
249
257 virtual void bindObjectUniforms(ACG::RenderObject* _obj,
258 GLSL::Program* _prog);
259
268 virtual void bindObjectRenderStates(ACG::RenderObject* _obj);
269
270
277 virtual void drawObject(ACG::RenderObject* _obj);
278
279
280 //=========================================================================
281 // Restore OpenGL State
282 //=========================================================================
283
284protected:
285
286
295 virtual void finishRenderingPipeline(bool _drawOverlay = true);
296
299 virtual void saveInputFbo();
300
303 virtual void restoreInputFbo();
304
310 virtual void saveActiveFbo(GLint* _outFboId, GLint* _outViewport, GLint* _outDrawBuffer) const;
311
317 virtual void restoreFbo(GLint _fboId, const GLint* _outViewport, GLint _drawBuffer) const;
318
324 virtual void clearInputFbo(const ACG::Vec4f& _clearColor);
325
326 //=========================================================================
327 // Other Convenience
328 //=========================================================================
329
330protected:
331
344 virtual void copyDepthToBackBuffer(GLuint _depthTex, float _scale = 1.0f);
345
346
359 virtual void renderDepthMap(int _viewerID, int _width, int _height);
360
361
362 //=========================================================================
363 // Internal shader modifiers
364 //=========================================================================
365protected:
366
367 // depth map modifier: writes gl_FragCoord.z to red color channel
369 {
370 public:
371 void modifyFragmentEndCode(QStringList* _code);
372
373 static DepthMapPass instance;
374 };
375
376
377 //=========================================================================
378 // Debugging
379 //=========================================================================
380public:
381
388 void dumpRenderObjectsToFile(const char* _fileName, ACG::RenderObject** _sortedList = 0) const;
389
397 virtual QString dumpCurrentRenderObjectsToString(ACG::RenderObject** _list = 0, bool _outputShaders = false, std::vector<ACG::ShaderModifier*>* _modifiers = 0);
398
410 void setErrorDetectionLevel(int _level);
411
413 int getErrorDetectionLevel() const;
414
415 //=========================================================================
416 // Variables
417 //=========================================================================
418protected:
419
421 int getNumRenderObjects() const;
422
424 int getNumLights() const;
425
427 ACG::RenderObject* getRenderObject(int i);
428
430 ACG::RenderObject* getOverlayRenderObject(int i);
431
433 ACG::RenderObject* getLineGL42RenderObject(int i);
434
436 LightData* getLight(int i);
437
438
440 const ACG::Vec3f& getGlobalAmbientScale() const {return globalLightModelAmbient_;}
441
442
444 ACG::SceneGraph::BaseNode* getRenderObjectNode(int i);
445
447 ACG::SceneGraph::BaseNode* getOverlayRenderObjectNode(int i);
448
449
450
459 void setLineThicknessRenderingGL42(bool _enable);
460
461
462 //=========================================================================
463 // Internally called by OpenFlipper core
464 //=========================================================================
465public:
466
475 void setViewerID(int _viewerID);
476
477
478
483 void setCoreProfileMode(bool _enable) {coreProfile_ = _enable;}
484
494 return current_subtree_objects_;
495 }
496
497protected:
500
502 LightData lights_[SG_MAX_SHADER_LIGHTS];
503
507
509 std::vector<ACG::RenderObject> renderObjects_;
510
512 std::vector<int> sortListObjects_;
514 std::vector<int> sortListOverlays_;
515
517 std::vector<ACG::RenderObject*> sortedObjects_;
518
520 std::vector<ACG::RenderObject*> overlayObjects_;
521
523 std::vector<ACG::RenderObjectModifier*> renderObjectModifiers_;
524
526 std::vector<ACG::SceneGraph::BaseNode*> renderObjectSource_;
527
529 std::vector<ACG::SceneGraph::BaseNode*> overlayObjectSource_;
530
536 std::map<int, ACG::FBO*> depthMaps_;
537
540
543
546
549
552
554 GLint prevFbo_;
555
558
560 GLint prevViewport_[4];
561
564
566 GLint prevVAO_;
567
570
573
574
577
578
581
582 RenderObjectRange current_subtree_objects_;
583private:
584
585 //=========================================================================
586 // Default rendering of thick lines
587 //=========================================================================
588
595
597 std::vector<RenderObject*> lineGL42Objects_;
598
600 std::map< int, Texture* > lineColorBuffers_;
601
602 void renderLineThicknessGL42();
603
604
605};
606
607
608
609//=============================================================================
610} // namespace ACG
611//=============================================================================
GLSL::Program * depthCopyShader_
shader copies depth of the first front layer to the back buffer
Definition: IRenderer.hh:569
std::vector< ACG::RenderObject * > sortedObjects_
sorted list of renderobjects without overlay objects (sorted in rendering order)
Definition: IRenderer.hh:517
int errorDetectionLevel_
error-detection level for checking render objects
Definition: IRenderer.hh:572
bool prevFboSaved_
flag indicating a that saveCurrentFbo() has been called prior restoreFbo()
Definition: IRenderer.hh:563
int curViewerID_
currently active viewer ID as specified in prepareRenderObjects()
Definition: IRenderer.hh:542
bool depthMapUsed_
true if at least one renderobject requires a scene depthmap, false otherwise
Definition: IRenderer.hh:539
std::vector< int > sortListOverlays_
map sortedID -> original renderObjectID
Definition: IRenderer.hh:514
bool coreProfile_
core profile mode
Definition: IRenderer.hh:576
std::vector< ACG::SceneGraph::BaseNode * > overlayObjectSource_
source node that added the overlay render object (map renderObjectID -> scenegraph node)
Definition: IRenderer.hh:529
std::vector< int > sortListObjects_
map sortedID -> original renderObjectID
Definition: IRenderer.hh:512
GLint prevFbo_
previous fbo
Definition: IRenderer.hh:554
std::map< int, Texture * > lineColorBuffers_
map from viewport id to line buffer
Definition: IRenderer.hh:600
static int maxClipDistances_
max number of clip distance outputs in a vertex shader
Definition: IRenderer.hh:580
std::vector< ACG::RenderObject * > overlayObjects_
sorted list of overlay-only renderobjects (sorted in rendering order)
Definition: IRenderer.hh:520
Vec3f camDirWS_
direction the camera is looking to in world-space
Definition: IRenderer.hh:551
std::vector< ACG::SceneGraph::BaseNode * > renderObjectSource_
source node that added the render object (map renderObjectID -> scenegraph node)
Definition: IRenderer.hh:526
Vec3f camPosWS_
cam position in world-space
Definition: IRenderer.hh:548
std::vector< ACG::RenderObject > renderObjects_
array of renderobjects, filled by addRenderObject()
Definition: IRenderer.hh:509
GLint prevDrawBuffer_
previous drawbuffer target (ie GL_BACK, GL_COLOR_ATTACHMENTi...)
Definition: IRenderer.hh:557
std::vector< ACG::RenderObjectModifier * > renderObjectModifiers_
active render object modifiers
Definition: IRenderer.hh:523
const RenderObjectRange & getCollectedSubtreeObjects() const
Definition: IRenderer.hh:493
GLint prevVAO_
previously bound VAO
Definition: IRenderer.hh:566
int numLights_
Number of Lights.
Definition: IRenderer.hh:499
GLMatrixf viewMatrix_
view transform
Definition: IRenderer.hh:545
void setCoreProfileMode(bool _enable)
Tell renderer to use core or compatibility profile.
Definition: IRenderer.hh:483
std::map< int, ACG::FBO * > depthMaps_
Definition: IRenderer.hh:536
std::vector< RenderObject * > lineGL42Objects_
default line render objects that are rendered with gl4.2
Definition: IRenderer.hh:597
const ACG::Vec3f & getGlobalAmbientScale() const
Get global ambient light contribution from GL_LIGHT_MODEL_AMBIENT.
Definition: IRenderer.hh:440
bool enableLineThicknessGL42_
Enable/disable gl4.2 based technique for rendering thick lines.
Definition: IRenderer.hh:594
ACG::Vec3f globalLightModelAmbient_
Definition: IRenderer.hh:506
Interface for modifying render objects.
DrawModeProperties stores a set of properties that defines, how to render an object.
Definition: DrawModes.hh:177
GLSL program class.
Definition: GLSLShader.hh:211
Namespace providing different geometric functions concerning angles.
This namespace contains all the classes and functions for handling GLSL shader and program objects.
Definition: AntiAliasing.hh:66
Interface class between scenegraph and renderer.
Definition: RenderObject.hh:99