Developer Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
dof.cc
1 /*===========================================================================*\
2 * *
3 * OpenFlipper *
4 * Copyright (C) 2001-2011 by Computer Graphics Group, RWTH Aachen *
5 * www.openflipper.org *
6 * *
7 *--------------------------------------------------------------------------- *
8 * This file is part of OpenFlipper. *
9 * *
10 * OpenFlipper is free software: you can redistribute it and/or modify *
11 * it under the terms of the GNU Lesser General Public License as *
12 * published by the Free Software Foundation, either version 3 of *
13 * the License, or (at your option) any later version with the *
14 * following exceptions: *
15 * *
16 * If other files instantiate templates or use macros *
17 * or inline functions from this file, or you compile this file and *
18 * link it with other files to produce an executable, this file does *
19 * not by itself cause the resulting executable to be covered by the *
20 * GNU Lesser General Public License. This exception does not however *
21 * invalidate any other reasons why the executable file might be *
22 * covered by the GNU Lesser General Public License. *
23 * *
24 * OpenFlipper is distributed in the hope that it will be useful, *
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
27 * GNU Lesser General Public License for more details. *
28 * *
29 * You should have received a copy of the GNU LesserGeneral Public *
30 * License along with OpenFlipper. If not, *
31 * see <http://www.gnu.org/licenses/>. *
32 * *
33 \*===========================================================================*/
34 
35 /*===========================================================================*\
36 * *
37 * $Revision: 17080 $ *
38 * $LastChangedBy: moeller $ *
39 * $Date: 2013-07-19 12:58:31 +0200 (Fri, 19 Jul 2013) $ *
40 * *
41 \*===========================================================================*/
42 
43 
44 #include <ACG/GL/acg_glew.hh>
45 
46 #include "dof.hh"
47 
48 #include <ACG/GL/ScreenQuad.hh>
49 #include <ACG/GL/ShaderCache.hh>
50 //#include <ACG/GL/Debug.hh>
51 
52 #include "sat.hh"
53 
54 using namespace ACG;
55 
56 
57 PostProcessorDoF::PostProcessorDoF()
58  : plan2D_(0)
59 {
60 
61 }
62 
63 PostProcessorDoF::~PostProcessorDoF()
64 {
65  delete plan2D_;
66 }
67 
68 QString PostProcessorDoF::checkOpenGL()
69 {
70  if (!ACG::openGLVersion(4,3))
71  return QString("Depth of Field plugin requires OpenGL 4.3!");
72 
73  return QString("");
74 }
75 
76 
77 void PostProcessorDoF::postProcess( ACG::GLState* _glstate, const std::vector<const PostProcessorInput*>& _input, const PostProcessorOutput& _output )
78 {
79  // ======================================================================================================
80  // Setup render states
81  // ======================================================================================================
82 
83  glDepthMask(0);
84  glColorMask(0,0,0,0);
85 
86  glDisable(GL_DEPTH_TEST);
87  glDisable(GL_BLEND);
88 
89 
90  // ======================================================================================================
91  // Convert to rgba32f
92  // ======================================================================================================
93 
94  int w = _input[0]->width;
95  int h = _input[0]->height;
96 
97  GLSL::Program* imgConvF = ACG::ShaderCache::getInstance()->getProgram("ScreenQuad/screenquad.glsl", "DoF/conv.glsl");
98 
99  if (imgConvF)
100  {
101  if (scene32F_.getWidth() != w || scene32F_.getHeight() != h)
102  {
103  scene32F_.del();
104  scene32F_.setStorage(1, GL_RGBA32F, w, h);
105  scene32F_.parameter(GL_TEXTURE_MIN_FILTER, GL_NEAREST);
106  scene32F_.parameter(GL_TEXTURE_MAG_FILTER, GL_NEAREST);
107 
108  sat32F_.del();
109  sat32F_.setStorage(1, GL_RGBA32F, w, h);
110  sat32F_.parameter(GL_TEXTURE_MIN_FILTER, GL_NEAREST);
111  sat32F_.parameter(GL_TEXTURE_MAG_FILTER, GL_NEAREST);
112  }
113 
114  scene32F_.bindAsImage(0, GL_READ_WRITE);
115 
116  glActiveTexture(GL_TEXTURE0);
117  glBindTexture(GL_TEXTURE_2D, _input[0]->colorTex_);
118 
119  ACG::ScreenQuad::draw(imgConvF);
120  }
121 
122  if (!plan2D_ || plan2D_->getRowPlan()->width() != w || plan2D_->getRowPlan()->height() != h)
123  {
124  delete plan2D_;
125  plan2D_ = new SATPlan(w,h, GL_RGBA32F, 128);
126  }
127  plan2D_->execute(&scene32F_, &sat32F_);
128 
129 
130  // ======================================================================================================
131  // Bind output FBO
132  // ======================================================================================================
133 
134  glBindFramebuffer(GL_FRAMEBUFFER, _output.fbo_);
135  glDrawBuffer(_output.drawBuffer_);
136 
137  glDepthMask(1);
138  glColorMask(1,1,1,1);
139 
140 
141  GLSL::Program* progDoF = ACG::ShaderCache::getInstance()->getProgram("ScreenQuad/screenquad.glsl", "DoF/dof.glsl");
142 
143  if (progDoF)
144  {
145  progDoF->use();
146  progDoF->setUniform("g_P", _input[0]->proj_);
147 
148  ACG::Vec2f clipPlanes = _input[0]->proj_.extract_planes();
149  progDoF->setUniform("g_ClipPlanes", clipPlanes);
150  printf("n/f: %f %f \n", clipPlanes[0], clipPlanes[1]);
151 
152  glActiveTexture(GL_TEXTURE1);
153  glBindTexture(GL_TEXTURE_2D, _input[0]->depthTex_);
154 
155  scene32F_.bind(GL_TEXTURE2);
156 
157  sat32F_.bind(GL_TEXTURE0);
158 
159 
160 
161  ScreenQuad::draw(progDoF);
162  }
163  else
164  ScreenQuad::drawTexture2D(_input[0]->colorTex_);
165 }
166 
167 
168 
169 #if QT_VERSION < 0x050000
170 Q_EXPORT_PLUGIN2( postprocessordofplugin , PostProcessorDoF );
171 #endif
172 
static void draw(GLSL::Program *_prog=0)
Draw the screen quad.
Definition: ScreenQuad.cc:147
static ShaderCache * getInstance()
Return instance of the ShaderCache singleton.
Definition: ShaderCache.cc:90
Namespace providing different geometric functions concerning angles.
Definition: DBSCANT.cc:51
Definition: sat.hh:151
bool openGLVersion(const int _major, const int _minor)
Definition: gl.cc:95
void setUniform(const char *_name, GLint _value)
Set int uniform to specified value.
Definition: GLSLShader.cc:391
GLSL::Program * getProgram(const ShaderGenDesc *_desc, const std::vector< unsigned int > &_mods)
Query a dynamically generated program from cache.
Definition: ShaderCache.cc:108
GLSL program class.
Definition: GLSLShader.hh:217
void use()
Enables the program object for using.
Definition: GLSLShader.cc:351