51 #include <ACG/GL/acg_glew.hh>
52 #include <ACG/GL/GLError.hh>
53 #include <ACG/GL/AntiAliasing.hh>
55 #include <OpenFlipper/common/RendererInfo.hh>
58 #include "PostProcessing.hh"
63 PostProcessing::PostProcessing()
64 : debugLevel_(10), backbufferFBO_(0), backbufferTarget_(GL_BACK), stereoMode_(false)
66 backbufferViewport_[0] = backbufferViewport_[1] = backbufferViewport_[2] = backbufferViewport_[3] = 0;
70 PostProcessing::~PostProcessing()
83 const int numProcs = postProcessorManager().
numActive(_viewerID);
85 if (!numProcs && _stereoEye < 0)
109 bool requiresFP =
false;
111 for (
int i = 0; i < numProcs && !requiresFP; ++i)
118 fmt.outputFormat_ = PostProcessorFormatDesc::PostProcessorFormat_DONTCARE;
119 pi->
plugin->getFormatDesc(&fmt);
121 if (!fmt.inputFormats_.empty())
123 size_t numInputs = fmt.inputFormats_.size();
125 for (
size_t k = 0; k < numInputs; ++k)
127 if (fmt.inputFormats_[k] == PostProcessorFormatDesc::PostProcessorFormat_FLOAT)
135 if (fmt.outputFormat_ == PostProcessorFormatDesc::PostProcessorFormat_FLOAT)
145 const int numEyes = _stereoEye < 0 ? 1 : 2;
147 GLuint texInternalFmt = requiresFP ? GL_RGBA32F : GL_RGBA;
153 glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, (GLint*)&
backbufferFBO_);
154 glGetIntegerv(GL_DRAW_BUFFER, (GLint*)&backbufferTarget_);
155 glGetIntegerv(GL_VIEWPORT, backbufferViewport_);
160 for (
int i = 0; i < numEyes; ++i)
161 setupFBO(
sceneFBO_ + i, _width, _height, texInternalFmt, _samples);
165 int numTempBuffers = std::min(numProcs - 1, 2);
168 numTempBuffers = std::min(numProcs, 2);
170 for (
int i = 0; i < numTempBuffers; ++i)
171 setupFBO(
procFBO_ + i, _width, _height, texInternalFmt, 0);
174 for (
int i = 0; i < (_stereoEye >= 0 ? 2 : 0); ++i)
175 setupFBO(
stereoFBO_ + i, _width, _height, texInternalFmt, 0);
180 int targetFboId = std::max(std::min(_stereoEye, 1), 0);
182 glDrawBuffer(GL_COLOR_ATTACHMENT0);
183 glViewport(0, 0, _width, _height);
186 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
193 glEnable(GL_MULTISAMPLE);
199 void PostProcessing::setupFBO(
ACG::FBO* _dst,
int _width,
int _height, GLuint _intfmt ,
int _samples )
202 GLuint filterMode = _samples ? GL_NEAREST : GL_LINEAR;
208 _dst->
attachTexture2D(GL_COLOR_ATTACHMENT0, _width, _height, _intfmt, GL_RGBA, GL_CLAMP, filterMode, filterMode);
226 _dst->
resize(_width, _height);
230 _dst->
attachTexture2D(GL_COLOR_ATTACHMENT0, _width, _height, _intfmt, GL_RGBA, GL_CLAMP, filterMode, filterMode);
238 if (
sceneFBO_[0].getMultisamplingCount())
239 glDisable(GL_MULTISAMPLE);
245 std::vector<const PostProcessorInput*> postProcInputVec;
246 postProcInputVec.push_back(&postProcInput);
249 const int numProcs = postProcessorManager().
numActive(_viewerID);
255 GLint saveDepthFunc = GL_LESS;
256 glGetIntegerv(GL_DEPTH_FUNC, &saveDepthFunc);
259 glColorMask(1,1,1,1);
264 for (
int chainId = 0; chainId < numChainExecs; ++chainId) {
271 postProcInput.depthRange_[0] = 0.0f;
272 postProcInput.depthRange_[1] = 1.0f;
274 postProcInput.view_ = _modelview;
275 postProcInput.proj_ = chainId ? _proj2 : _proj1;
278 if (
sceneFBO_[chainId].getMultisamplingCount())
284 resolveMultisampling(resolveTarget, &
sceneFBO_[chainId]);
287 postProcInput.colorTex_ = resolveTarget->
getAttachment(GL_COLOR_ATTACHMENT0);
288 postProcInput.depthTex_ = resolveTarget->
getAttachment(GL_DEPTH_ATTACHMENT);
309 for (
int i = 0; i < numProcs; ++i) {
311 int postProcTarget = 1 - postProcSrc;
315 GLuint outputDrawbuffer = GL_COLOR_ATTACHMENT0;
318 const GLint* outputViewportPtr = outputViewport;
321 if (i + 1 == numProcs)
328 outputDrawbuffer = chainId ? GL_BACK_RIGHT : GL_BACK_LEFT;
330 outputViewportPtr = backbufferViewport_;
335 outputDrawbuffer = GL_COLOR_ATTACHMENT0;
337 outputViewport[0] = outputViewport[1] = 0;
345 outputDrawbuffer = backbufferTarget_;
346 outputViewportPtr = backbufferViewport_;
351 procFBO_[postProcTarget].width(),
procFBO_[postProcTarget].height(), outputViewportPtr);
359 glColorMask(1,1,1,1);
361 glEnable(GL_CULL_FACE);
365 glDepthFunc(GL_LEQUAL);
372 GLint testFBO = 0, testDrawbuffer = 0;
375 glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &testFBO);
376 glGetIntegerv(GL_DRAW_BUFFER, &testDrawbuffer);
377 glGetIntegerv(GL_VIEWPORT, testVp);
379 if (GLuint(testFBO) != postProcOutput.fbo_)
380 std::cerr <<
"Error: PostProcessor does not write to specified FBO: " << proc->
plugin->
postProcessorName().toStdString() << std::endl;
382 if (GLuint(testDrawbuffer) != postProcOutput.drawBuffer_)
383 std::cerr <<
"Error: PostProcessor does not write to specified draw-buffer: " << proc->
plugin->
postProcessorName().toStdString() << std::endl;
385 if (testVp[0] != postProcOutput.viewport_[0] || testVp[1] != postProcOutput.viewport_[1] ||
386 testVp[2] != postProcOutput.viewport_[2] || testVp[3] != postProcOutput.viewport_[3])
387 std::cerr <<
"Error: PostProcessor does not write to specified viewport: " << proc->
plugin->
postProcessorName().toStdString()
388 <<
" actual: (" << testVp[0] <<
" " << testVp[1] <<
" " << testVp[2] <<
" " << testVp[3] <<
") expected: ("
389 << postProcOutput.viewport_[0] <<
" " << postProcOutput.viewport_[1] <<
" " << postProcOutput.viewport_[2] <<
" " << postProcOutput.viewport_[3] <<
")" << std::endl;
394 postProcSrc = postProcTarget;
404 glDrawBuffer(backbufferTarget_);
405 glViewport(backbufferViewport_[0], backbufferViewport_[1], backbufferViewport_[2], backbufferViewport_[3]);
408 glDepthFunc(saveDepthFunc);
413 const int resolveMethod = 0;
416 glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &curFbo);
418 if (resolveMethod == 0)
422 glBindFramebuffer(GL_READ_FRAMEBUFFER, _src->
getFboID());
423 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, _dst->
getFboID());
428 std::cerr <<
"PostProcessing::resolveMultisampling - FBO size mismatch : src ("
429 << _src->
width() <<
" " << _src->
height() <<
") dst ("
430 << _dst->
width() <<
" " << _dst->
height() <<
")" << std::endl;
433 glBlitFramebuffer(0, 0, _src->
width(), _src->
height(),
435 GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT, GL_NEAREST);
442 glDrawBuffer(GL_COLOR_ATTACHMENT0);
443 GLuint multisampledColorTex = _src->
getAttachment(GL_COLOR_ATTACHMENT0);
444 GLuint multisampledDepthTex = _src->
getAttachment(GL_DEPTH_ATTACHMENT);
445 glDepthFunc(GL_ALWAYS);
446 ACG::MSTextureSampler::filterMSAATexture_Nearest(multisampledColorTex, multisampledDepthTex, _src->
getMultisamplingCount());
447 glDepthFunc(GL_LEQUAL);
452 glBindFramebuffer(GL_FRAMEBUFFER, curFbo);
463 const int numProcs = postProcessorManager().
numActive(_viewerID);
467 for (
int i = 0; i < 2; ++i)
473 if (!numProcs && !
sceneFBO_[i].getMultisamplingCount())
476 inputs[i].colorTex_ = eyeSrcFBO->
getAttachment(GL_COLOR_ATTACHMENT0);
477 inputs[i].depthTex_ = eyeSrcFBO->
getAttachment(GL_DEPTH_ATTACHMENT);
478 inputs[i].width = eyeSrcFBO->
width();
479 inputs[i].height = eyeSrcFBO->
height();
485 std::vector<const PostProcessorInput*> anaglyphInputVec(2);
486 anaglyphInputVec[0] = inputs;
487 anaglyphInputVec[1] = inputs + 1;
491 inputs[0].width, inputs[0].height, backbufferViewport_);
496 std::cerr <<
"error: stereo anaglyph plugin missing!" << std::endl;
ACG::FBO stereoFBO_[2]
store post processing results for each stereo eyes, input for anaglyph processor, no msaa ...
PostProcessorInfo * getPostProcessor(QString _name)
get post processor with the given name
void attachTexture2D(GLenum _attachment, GLsizei _width, GLsizei _height, GLuint _internalFmt, GLenum _format, GLint _wrapMode=GL_CLAMP, GLint _minFilter=GL_NEAREST, GLint _magFilter=GL_NEAREST)
function to attach a texture to fbo
PostProcessorInterface * plugin
Pointer to the loaded plugin (Already casted when loading it)
ACG::FBO procFBO_[2]
post-processing read/write FBOs, no msaa
void resolveStereoAnyglyph(int _viewerID)
Resolve stereo buffers as anaglyph.
GLsizei width() const
get width of fbo texture
virtual void postProcess(ACG::GLState *_glState, const std::vector< const PostProcessorInput * > &_input, const PostProcessorOutput &_output)=0
post processor function
GLsizei getMultisamplingCount() const
get number of samples
virtual QString postProcessorName()=0
announce name for the postProcessor function
int numActive(int _id)
Get the number of active post processors for viewer.
void resize(GLsizei _width, GLsizei _height, bool _forceResize=false)
resize function (if textures created by this class)
GLuint getFboID()
return opengl id
void attachTexture2DDepth(GLsizei _width, GLsizei _height, GLuint _internalFmt=GL_DEPTH_COMPONENT32, GLenum _format=GL_DEPTH_COMPONENT)
function to attach a depth-buffer texture to fbo (using GL_DEPTH_ATTACHMENT)
GLuint getAttachment(GLenum _attachment)
return attached texture id
PostProcessorInfo * active(int _id, int _chainIdx=0)
Get the current active post processor for viewer at chain index.
ACG::FBO sceneFBO_[2]
FBO for scene rendering, one for each eye in stereo mode, possibly multisampled.
int debugLevel_
perform extra checks for debugging: 0 -> no checks, 1 -> fast checks, 2 -> more checks etc...
GLuint getInternalFormat(GLenum _attachment)
return internal texture format of attachment
bool stereoMode_
postprocessing in stereo mode
GLsizei height() const
get height of fbo texture
int setupScene(int _viewerID, int _width, int _height, int _samples=0, int _stereoEye=-1)
Bind fbo for scene rendering.
GLuint backbufferFBO_
backbuffer output, not owned by this class
void postProcess(int _viewerID, ACG::GLState *_glstate, const ACG::GLMatrixd &_modelview, const ACG::GLMatrixd &_proj1, const ACG::GLMatrixd &_proj2, bool _hwOpenGLStereo=false)
Perform all post processing.
ACG::Vec2i size() const
get width and height of fbo texture
GLsizei setMultisampling(GLsizei _samples, GLboolean _fixedsamplelocations=GL_TRUE)
bool bind()
bind the fbo and sets it as rendertarget