1 /*---------------------------------------------------------------------------*\
5 * Copyright (C) 2000-2013 by the OpenSG Forum *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, carsten_neumann@gmx.net *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
15 * This library is free software; you can redistribute it and/or modify it *
16 * under the terms of the GNU Library General Public License as published *
17 * by the Free Software Foundation, version 2. *
19 * This library is distributed in the hope that it will be useful, but *
20 * WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
22 * Library General Public License for more details. *
24 * You should have received a copy of the GNU Library General Public *
25 * License along with this library; if not, write to the Free Software *
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
28 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
37 \*---------------------------------------------------------------------------*/
39 //---------------------------------------------------------------------------
41 //---------------------------------------------------------------------------
46 #include "OSGConfig.h"
49 #include "OSGGLFuncProtos.h"
51 #include "OSGViewport.h"
53 #include "OSGFBOGrabForeground.h"
55 #include "OSGAction.h"
57 #include "OSGCamera.h"
58 #include "OSGBackground.h"
59 #include "OSGDrawEnv.h"
60 #include "OSGFrameBufferObject.h"
64 // Documentation for this class is emitted in the
65 // OSGFBOGrabForegroundBase.cpp file.
66 // To modify it, please change the .fcd file (OSGFBOGrabForeground.fcd) and
67 // regenerate the base file.
69 /***************************************************************************\
71 \***************************************************************************/
73 UInt32
FBOGrabForeground::_uiFramebufferObjectExt
=
74 Window::invalidExtensionID
;
76 UInt32
FBOGrabForeground::_uiFramebufferBlitExt
=
77 Window::invalidExtensionID
;
79 UInt32
FBOGrabForeground::_uiFuncBindFramebuffer
=
80 Window::invalidFunctionID
;
82 UInt32
FBOGrabForeground::_uiFuncBlitFramebuffer
=
83 Window::invalidFunctionID
;
85 /***************************************************************************\
87 \***************************************************************************/
89 void FBOGrabForeground::initMethod(InitPhase ePhase
)
91 Inherited::initMethod(ePhase
);
93 if(ePhase
== TypeObject::SystemPost
)
95 _uiFramebufferObjectExt
=
96 Window::registerExtension("GL_EXT_framebuffer_object");
97 _uiFramebufferBlitExt
=
98 Window::registerExtension("GL_EXT_framebuffer_blit");
100 _uiFuncBindFramebuffer
=
101 Window::registerFunction (
102 OSG_DLSYM_UNDERSCORE
"glBindFramebufferEXT",
103 _uiFramebufferObjectExt
);
105 _uiFuncBlitFramebuffer
=
106 Window::registerFunction (
107 OSG_DLSYM_UNDERSCORE
"glBlitFramebufferEXT",
108 _uiFramebufferBlitExt
);
112 /***************************************************************************\
114 \***************************************************************************/
116 /*-------------------------------------------------------------------------*\
118 \*-------------------------------------------------------------------------*/
120 /*----------------------- constructors & destructors ----------------------*/
122 FBOGrabForeground::FBOGrabForeground(void) :
127 FBOGrabForeground::FBOGrabForeground(const FBOGrabForeground
&source
) :
132 FBOGrabForeground::~FBOGrabForeground(void)
136 /*----------------------------- class specific ----------------------------*/
138 void FBOGrabForeground::changed(ConstFieldMaskArg whichField
,
142 Inherited::changed(whichField
, origin
, details
);
145 void FBOGrabForeground::dump( UInt32
,
146 const BitVector
) const
148 SLOG
<< "Dump FBOGrabForeground NI" << std::endl
;
152 /*! Grab the image, if it is actually set.
155 void FBOGrabForeground::draw(DrawEnv
* pEnv
)
157 if(getActive() == false)
160 FrameBufferObject
* fbo
= getFrameBufferObject();
162 if(fbo
== NULL
) // No FBO, no grab.
165 Int32 w
= osgMax(2, pEnv
->getPixelWidth ());
166 Int32 h
= osgMax(2, pEnv
->getPixelHeight());
168 // If fbo is smaller than 2x2, resize it to vp size.
169 // If autoResize then update img size if vp changed
171 if( (fbo
->getWidth() <= 1 || fbo
->getHeight() <= 1) ||
172 (getAutoResize() && (w
!= fbo
->getWidth() || h
!= fbo
->getHeight())) )
174 fbo
->resizeAll(w
, h
);
177 Window
*win
= pEnv
->getWindow();
179 win
->validateGLObject(fbo
->getGLId(), pEnv
);
181 OSGGETGLFUNCBYID_GL3_ES( glBindFramebuffer
,
182 osgGlBindFramebuffer
,
183 _uiFuncBindFramebuffer
,
186 OSGGETGLFUNCBYID_GL3( glBlitFramebuffer
,
187 osgGlBlitFramebuffer
,
188 _uiFuncBlitFramebuffer
,
191 if(pEnv
->getActiveFBO() != 0)
193 osgGlBindFramebuffer(GL_READ_FRAMEBUFFER_EXT
,
194 win
->getGLObjectId(pEnv
->getActiveFBO()));
198 osgGlBindFramebuffer(GL_READ_FRAMEBUFFER_EXT
,
202 osgGlBindFramebuffer(
203 GL_DRAW_FRAMEBUFFER_EXT
,
204 win
->getGLObjectId(_sfFrameBufferObject
.getValue()->getGLId()));
206 // Fill FBO but only up to size of viewport. This will not scale the
207 // captured image unless it is larger than the destination.
209 static_cast<Int32
>(_sfFrameBufferObject
.getValue()->getWidth ()),
210 pEnv
->getPixelWidth ());
212 static_cast<Int32
>(_sfFrameBufferObject
.getValue()->getHeight()),
213 pEnv
->getPixelHeight());
215 osgGlBlitFramebuffer(
216 pEnv
->getPixelLeft (),
217 pEnv
->getPixelBottom(),
218 pEnv
->getPixelRight ()+1,
219 pEnv
->getPixelTop ()+1,
225 (GL_COLOR_BUFFER_BIT
|
226 GL_DEPTH_BUFFER_BIT
|
227 GL_STENCIL_BUFFER_BIT
),
230 if(pEnv
->getActiveFBO() != 0)
232 osgGlBindFramebuffer(GL_DRAW_FRAMEBUFFER_EXT
,
233 win
->getGLObjectId(pEnv
->getActiveFBO()));
237 osgGlBindFramebuffer(GL_DRAW_FRAMEBUFFER_EXT
,