SSAO.hh 7.19 KB
Newer Older
Jan Möbius's avatar
   
Jan Möbius committed
1
2
/*===========================================================================*\
*                                                                            *
Jan Möbius's avatar
Jan Möbius committed
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
*                              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$                                                       *
*   $LastChangedBy$                                                *
*   $Date$                     *
Jan Möbius's avatar
   
Jan Möbius committed
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
*                                                                            *
\*===========================================================================*/

#include <QObject>

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

#include <ACG/Scenegraph/SceneGraph.hh>

#include <map>

class SSAOPlugin : public QObject, BaseInterface, RenderInterface, LoggingInterface
{
  Q_OBJECT
    Q_INTERFACES(BaseInterface)
    Q_INTERFACES(RenderInterface)
    Q_INTERFACES(LoggingInterface)
    
    
signals:
  // LoggingInterface
    void log(Logtype _type, QString _message);
    void log(QString _message);


public :
  QString name() { return (QString("SSAO Plugin")); };
  QString description( ) { return (QString(tr("Screen Space Ambient Occlusion"))); };

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();
  void supportedDrawModes(ACG::SceneGraph::DrawModes::DrawMode& _mode);

private:

  /// free all gl resources
  void destroyResources();

  /// free viewer specific gl resources
  void destroyResources(int _viewerId);

  /// reload gl resources
Christopher Tenter's avatar
Christopher Tenter committed
94
  void reloadResources(int _viewerId, unsigned int _sceneTexWidth, unsigned int _sceneTexHeight);
Jan Möbius's avatar
   
Jan Möbius committed
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128

  /// draw a quad in projection space (only positions)
  void drawQuadProj(float _x0 = -1.0f, float _y0 = 1.0f, 
    float _w = 2.0f, float _h = 2.0f);

  /// find all light nodes in the scene
  void traverseLightNodes(BaseNode* _node);

  /// peel shader generator based on lights and texture mode
  void generatePeelingShaders(GLSL::StringList* _strVertexShaderOut,
    GLSL::StringList* _strFragmentShaderOut, bool _textured);

  /// draw the current scene
  void drawScenePass(ACG::GLState* _glState, Viewer::ViewerProperties& _properties, BaseNode* _sceneGraphRoot);

  /// gaussian blur pass
  struct ViewerResources;
  void gaussianBlurPass(const ViewerResources* _pViewer, const float* _texelSize, 
    GLenum _targetAttachement, GLuint _srcTexture);

  /// computes a hemisphere sampling kernel in [0,1] range
  void generateSamplingKernel();
private:
  
  struct ViewerResources
  {
    ViewerResources();

    /// viewer window width
    unsigned int glWidth_;

    /// viewer window height
    unsigned int glHeight_;

Christopher Tenter's avatar
Christopher Tenter committed
129
130
131
132
133
    /// scene render target width
    unsigned int rtSceneWidth_; 
    /// scene render target height
    unsigned int rtSceneHeight_;

Jan Möbius's avatar
   
Jan Möbius committed
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
    /// render target width
    unsigned int rtWidth_; 
    /// render target height
    unsigned int rtHeight_;

    /// downsampled rt width
    unsigned int rtDownWidth_;
    /// downsampled rt height
    unsigned int rtDownHeight_;

    /// depth buffer render target
    GLuint depthBufTex_;

    /// scene normal buffer render target
    /// R8G8B8 format
    GLuint sceneNormalTex_;

Christopher Tenter's avatar
Christopher Tenter committed
151
152
153
154
155
    /// depth renderbuffer for ssaoFbo
    GLuint depthSSAORenderBuf_;

    /// depth renderbuffer for sceneFbo
    GLuint depthSceneRenderBuf_;
Jan Möbius's avatar
   
Jan Möbius committed
156
157
158
159
160
161
162
163
164
165
166
167
168

    /// standard scene without a render target
    GLuint sceneBufTex_;

    /// downsampled depth render target
    GLuint downsampledTex_;

    /// downsampled temp rt for intermediate results
    GLuint downsampledTmpTex_;

    /// occlusion render target
    GLuint occlusionTex_;

Christopher Tenter's avatar
Christopher Tenter committed
169
170
    /// sceneFbo for scene color rendering only (seperated for multisampling)
    /// attachment order: only scene color texture
Jan Möbius's avatar
   
Jan Möbius committed
171
172
    GLuint sceneFbo_;

Christopher Tenter's avatar
Christopher Tenter committed
173
174
175
176
    /// ssaoFbo for deferred rendering
    /// attachment order: depth, normal, occlusion
    GLuint ssaoFbo_;

Jan Möbius's avatar
   
Jan Möbius committed
177
    /// blurFbo for downsampling and gaussian blur filter
Christopher Tenter's avatar
Christopher Tenter committed
178
    /// attachment order: downsampled, downsampledTmp, occlusion
Jan Möbius's avatar
   
Jan Möbius committed
179
180
181
182
183
184
    GLuint blurFbo_;
  };

  std::map<int, ViewerResources> viewerRes_;

  /// shader resources
Christopher Tenter's avatar
Christopher Tenter committed
185
  GLSL::Shader* shaders_[10];
Jan Möbius's avatar
   
Jan Möbius committed
186
187
188
189
190
191
192

  enum
  { 
    PROG_INIT = 0,  // early Z pass + standard scene rendering
    PROG_DOWNSAMPLING,
    PROG_BLUR,
    PROG_SSAO,
Christopher Tenter's avatar
Christopher Tenter committed
193
194
    PROG_FINAL,
    PROG_FINAL_MSAA
Jan Möbius's avatar
   
Jan Möbius committed
195
196
197
  };
  
  /// shader programs
Christopher Tenter's avatar
Christopher Tenter committed
198
  GLSL::Program* programs_[6];
Jan Möbius's avatar
   
Jan Möbius committed
199
200
201
202
203
204
205
206
207
208

  /// random vector table for sample offset rotation
  GLuint randomVecTex_;

  /// number of samples
  static const unsigned int numSamples_;

  /// ssao sampling kernel
  ACG::Vec3f samplingKernel_[128];
};