Renderer.hh 10.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*===========================================================================*\
*                                                                            *
*                              OpenFlipper                                   *
*      Copyright (C) 2001-2011 by Computer Graphics Group, RWTH Aachen       *
*                           www.openflipper.org                              *
*                                                                            *
*--------------------------------------------------------------------------- *
*  This file is part of OpenFlipper.                                         *
*                                                                            *
*  OpenFlipper is free software: you can redistribute it and/or modify       *
*  it under the terms of the GNU Lesser General Public License as            *
*  published by the Free Software Foundation, either version 3 of            *
*  the License, or (at your option) any later version with the               *
*  following exceptions:                                                     *
*                                                                            *
*  If other files instantiate templates or use macros                        *
*  or inline functions from this file, or you compile this file and          *
*  link it with other files to produce an executable, this file does         *
*  not by itself cause the resulting executable to be covered by the         *
*  GNU Lesser General Public License. This exception does not however        *
*  invalidate any other reasons why the executable file might be             *
*  covered by the GNU Lesser General Public License.                         *
*                                                                            *
*  OpenFlipper is distributed in the hope that it will be useful,            *
*  but WITHOUT ANY WARRANTY; without even the implied warranty of            *
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
*  GNU Lesser General Public License for more details.                       *
*                                                                            *
*  You should have received a copy of the GNU LesserGeneral Public           *
*  License along with OpenFlipper. If not,                                   *
*  see <http://www.gnu.org/licenses/>.                                       *
*                                                                            *
\*===========================================================================*/

/*===========================================================================*\
*                                                                            *
*   $Revision: 13374 $                                                       *
*   $LastChangedBy: moebius $                                                *
*   $Date: 2012-01-13 09:38:16 +0100 (Fri, 13 Jan 2012) $                     *
*                                                                            *
\*===========================================================================*/


#pragma once


#include <QObject>

#include <OpenFlipper/BasePlugin/BaseInterface.hh>
#include <OpenFlipper/BasePlugin/RenderInterface.hh>
#include <OpenFlipper/BasePlugin/LoggingInterface.hh>

#include <ACG/Scenegraph/SceneGraph.hh>
#include <ACG/Scenegraph/MaterialNode.hh>
#include <ObjectTypes/Light/LightNode.hh>

#include <ACG/GL/IRenderer.hh>


#include <map>


63
64
65
66
67
/** \brief A simple renderer Example for the shader pipeline
 *
 * It has to derive from ACG::IRenderer as the nodes will will pass back the generated render objects
 * via this objects addRenderObject() function.
 */
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
class Renderer : public QObject, BaseInterface, RenderInterface, LoggingInterface, ACG::IRenderer
{
  Q_OBJECT
    Q_INTERFACES(BaseInterface)
    Q_INTERFACES(RenderInterface)
    Q_INTERFACES(LoggingInterface)

signals:
  // LoggingInterface
  void log(Logtype _type, QString _message);
  void log(QString _message);


public:
  Renderer();
  ~Renderer();

Jan Möbius's avatar
Jan Möbius committed
85
  QString name() { return (QString("Shader Pipeline Renderer Plugin")); };
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
  QString description( ) { return (QString(tr("Shader Based Rendering Pipeline (Alpha Version!)"))); };

public slots:
  QString version() { return QString("1.0"); };


private slots:

  //BaseInterface
  void initializePlugin();
  void exit(){}

  // RenderInterface
  void render(ACG::GLState* _glState, Viewer::ViewerProperties& _properties);
  QString rendererName() {return QString("Alpha_Version_ShaderPipeline");}
  void supportedDrawModes(ACG::SceneGraph::DrawModes::DrawMode& _mode) {_mode = ACG::SceneGraph::DrawModes::DEFAULT;}
  QString checkOpenGL();


105
106
107
//=========================================================================
// Callback for the scenegraph nodes
//=========================================================================
108
public:
109
110
111
112
113
114
115
116
  /** \brief callback for the nodes, which send new render objects via this function.
   *
   * AddRenderObject is typically called by a scenegraph nodes during the collection of renderable
   * objects.
   *
   * A renderobject is a collection of opengl states, buffers and parameters, that correspond to exactly one draw call.
  */
  virtual void addRenderObject(ACG::RenderObject* _renderObject);
117

118
119
120
121
//=========================================================================
// Render object collection
//=========================================================================
private:
122

123
124
125
126
127
128
129
130
  /** \brief Traverse the scenegraph to collect render information
   *
   * Traverses the scenegraph and calls the getRenderObject function of each node.
   * Each node can then add multiple renderobjects via addRenderObject to this renderer.
   *
   * Also collects all light sources in the scenegraph.
   * The currently active list of renderobjects is invalidated too.
   */
131
132
133
  void collectRenderObjects(ACG::GLState* _glState, ACG::SceneGraph::DrawModes::DrawMode _drawMode, ACG::SceneGraph::BaseNode* _sceneGraphRoot);


134
135
136
137
138
139
140
141
  /** \brief Scene graph traversal for render object collection
   *
   * Calls getRenderObjects on each node of the scenegraph recursively.
   */
  void traverseRenderableNodes( ACG::GLState* _glState,
                                ACG::SceneGraph::DrawModes::DrawMode _drawMode,
                                ACG::SceneGraph::BaseNode* _node,
                                const ACG::SceneGraph::Material* _mat);
142

143
144
145
146
//=========================================================================
// Light node collection
//=========================================================================
private:
147

148
149
150
151
152
153
154
155
156
157
158
159
160
  /** \brief Find all light sources in the scene
   *
   * Find all light nodes in the scene and save them for the next render() call.
   *
   * The lights are collected in lights_ while the corresponding type ends up in lightTypes_
   */
  virtual void collectLightNodes(ACG::SceneGraph::BaseNode* _node);

  /** \brief Light Node traverser
   *
   * Finds all Light Nodes in the scenegraph and stores them for later use. This is a recursive function.
   */
  void traverseLightNodes(ACG::SceneGraph::BaseNode* _node);
161
162
163



164
165
166
167
//=========================================================================
// Sorting
//=========================================================================
private:
168

169
170
171
172
  /** \brief Compare priority of render objects
   *
   * compare function for qsort. This is required to compare render objects based
   * on their prioerity and render them in the right order
173
  */
174
  static int cmpPriority(const void*, const void*);
175

176
177
178
179
//=========================================================================
// Rendering
//=========================================================================
private:
180

181
182
183
184
185
186
187
188
  /** \brief Binding VBOs (First state function)
   *
   * This is the first function called by renderObject().
   *
   * It binds vertex, index buffer and vertex format of a
   * render object.
   *
   */
189
190
191
  virtual void bindObjectVBO(ACG::RenderObject* _obj,
                             GLSL::Program*     _prog);

192
193
194
195
196
197
198
  /** \brief Binding Uniforms (Second state function)
   *
   * This is the second function called by renderObject().
   *
   * Set common shader constants like model-view-projection matrix,
   * material colors and light params.
   */
199
200
201
  virtual void bindObjectUniforms(ACG::RenderObject* _obj,
                                  GLSL::Program*     _prog);

202
203
204
205
206
207
208
209
210

  /** \brief Binding Render state (Third state function)
   *
   * This is the third function called by renderObject().
   *
   * Prepare the opengl state machine for a renderobject draw call.
   *
   * This includes any glEnable/glDisable states, depth-cmp functions, blend equation..
   */
211
212
  virtual void bindObjectRenderStates(ACG::RenderObject* _obj);

213
214
215
216
217
218
  /** \brief Executes the opengl draw call for one object (Fourth function)
   *
   * This is the fourth function called by renderObject().
   *
   * Executes one draw call for the given render object
   */
219
220
  virtual void drawObject(ACG::RenderObject* _obj);

221
222
223
224
225
226
227
  /** \brief Render one render object
   *
   * Fully prepares opengl for a renderobject and executes the draw call.
   * This combines bindObjectVBO, bindObjectUniforms...
   *
   * Optionally render-states may not be changed, in case depth-peeling or
   * similar global shader operations may require a fixed state setting.
228
229
230
  */
  virtual void renderObject(ACG::RenderObject* _obj, GLSL::Program* _prog = 0, bool _constRenderStates = false);

231
232
233
234
  //=========================================================================
  // Debugging
  //=========================================================================
private:
235

236
  /** \brief Debugging function to dump list of render objects into a file
237
   *
238
239
240
   * Dump list of render objects to text file.
   * @param _fileName name of text file to write to
   * @param _sortedList dump sorted render objects in order, may be 0 to use the unsorted list instead
241
   */
242
  void dumpRenderObjectsToText(const char* _fileName, ACG::RenderObject** _sortedList = 0) const;
243
244


245
246
247
248
  //=========================================================================
  // Variables
  //=========================================================================
private:
249

250
  /// Current number of lights  (Filled by collectLightNodes)
251
  int numLights_;
252
253

  /// Light sources (Filled by collectLightNodes)
254
  ACG::SceneGraph::LightSource lights_[SG_MAX_SHADER_LIGHTS];
255
256

  /// Type of light sources (Filled by collectLightNodes)
257
258
259
  ACG::ShaderGenLightType lightTypes_[SG_MAX_SHADER_LIGHTS];


260
  /// Array of renderobjects  (Filled by addRenderObject)
261
262
263
264
265
  std::vector<ACG::RenderObject> renderObjects_;


};