fixed: auto_ptr -> unique_ptr
[opensg.git] / Source / System / Window / Foreground / OSGFBOGrabForeground.cpp
blob31fca622a5c58d33301f4f51e98cbe242f2bb422
1 /*---------------------------------------------------------------------------*\
2 * OpenSG *
3 * *
4 * *
5 * Copyright (C) 2000-2013 by the OpenSG Forum *
6 * *
7 * www.opensg.org *
8 * *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, carsten_neumann@gmx.net *
10 * *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
13 * License *
14 * *
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. *
18 * *
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. *
23 * *
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. *
27 * *
28 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
30 * Changes *
31 * *
32 * *
33 * *
34 * *
35 * *
36 * *
37 \*---------------------------------------------------------------------------*/
39 //---------------------------------------------------------------------------
40 // Includes
41 //---------------------------------------------------------------------------
43 #include <cstdlib>
44 #include <cstdio>
46 #include "OSGConfig.h"
48 #include "OSGGL.h"
49 #include "OSGGLFuncProtos.h"
51 #include "OSGViewport.h"
53 #include "OSGFBOGrabForeground.h"
55 #include "OSGAction.h"
56 #include "OSGNode.h"
57 #include "OSGCamera.h"
58 #include "OSGBackground.h"
59 #include "OSGDrawEnv.h"
60 #include "OSGFrameBufferObject.h"
62 OSG_BEGIN_NAMESPACE
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 /***************************************************************************\
70 * Class variables *
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 /***************************************************************************\
86 * Class methods *
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 /***************************************************************************\
113 * Instance methods *
114 \***************************************************************************/
116 /*-------------------------------------------------------------------------*\
117 - private -
118 \*-------------------------------------------------------------------------*/
120 /*----------------------- constructors & destructors ----------------------*/
122 FBOGrabForeground::FBOGrabForeground(void) :
123 Inherited()
127 FBOGrabForeground::FBOGrabForeground(const FBOGrabForeground &source) :
128 Inherited(source)
132 FBOGrabForeground::~FBOGrabForeground(void)
136 /*----------------------------- class specific ----------------------------*/
138 void FBOGrabForeground::changed(ConstFieldMaskArg whichField,
139 UInt32 origin,
140 BitVector details)
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)
158 return;
160 FrameBufferObject* fbo = getFrameBufferObject();
162 if(fbo == NULL) // No FBO, no grab.
163 return;
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,
184 win );
186 OSGGETGLFUNCBYID_GL3( glBlitFramebuffer,
187 osgGlBlitFramebuffer,
188 _uiFuncBlitFramebuffer,
189 win );
191 if(pEnv->getActiveFBO() != 0)
193 osgGlBindFramebuffer(GL_READ_FRAMEBUFFER_EXT,
194 win->getGLObjectId(pEnv->getActiveFBO()));
196 else
198 osgGlBindFramebuffer(GL_READ_FRAMEBUFFER_EXT,
199 0 );
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.
208 w = osgMin(
209 static_cast<Int32>(_sfFrameBufferObject.getValue()->getWidth ()),
210 pEnv->getPixelWidth ());
211 h = osgMin(
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,
223 w, h,
225 (GL_COLOR_BUFFER_BIT |
226 GL_DEPTH_BUFFER_BIT |
227 GL_STENCIL_BUFFER_BIT),
228 GL_NEAREST);
230 if(pEnv->getActiveFBO() != 0)
232 osgGlBindFramebuffer(GL_DRAW_FRAMEBUFFER_EXT,
233 win->getGLObjectId(pEnv->getActiveFBO()));
235 else
237 osgGlBindFramebuffer(GL_DRAW_FRAMEBUFFER_EXT,
238 0 );
242 OSG_END_NAMESPACE