fixed: compile issue
[opensg.git] / Source / System / Window / FrameBufferObjects / OSGRenderBuffer.cpp
blob610bd30a59674cca819d4015207afc2dc384fe47
1 /*---------------------------------------------------------------------------*\
2 * OpenSG *
3 * *
4 * *
5 * Copyright (C) 2000-2002 by the OpenSG Forum *
6 * *
7 * www.opensg.org *
8 * *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
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>
45 #include <boost/bind.hpp>
47 #include "OSGConfig.h"
48 #include "OSGGL.h"
49 #include "OSGGLEXT.h"
50 #include "OSGGLFuncProtos.h"
52 #include "OSGRenderBuffer.h"
53 #include "OSGDrawEnv.h"
54 #include "OSGGLU.h"
55 #include "OSGImage.h"
57 OSG_BEGIN_NAMESPACE
59 UInt32 RenderBuffer::_uiFramebufferObjectArb =
60 Window::invalidExtensionID;
62 UInt32 RenderBuffer::_uiFramebufferMultiSampleExt =
63 Window::invalidExtensionID;
65 UInt32 RenderBuffer::_uiFramebufferMultiSampleCoverageNV =
66 Window::invalidExtensionID;
69 UInt32 RenderBuffer::_uiFuncFramebufferRenderbuffer =
70 Window::invalidFunctionID;
72 UInt32 RenderBuffer::_uiFuncGenRenderbuffers =
73 Window::invalidFunctionID;
75 UInt32 RenderBuffer::_uiFuncDeleteRenderbuffers =
76 Window::invalidFunctionID;
78 UInt32 RenderBuffer::_uiFuncBindRenderbuffer =
79 Window::invalidFunctionID;
81 UInt32 RenderBuffer::_uiFuncRenderbufferStorage =
82 Window::invalidFunctionID;
84 UInt32 RenderBuffer::_uiFuncRenderbufferStorageMultiSample =
85 Window::invalidFunctionID;
87 UInt32 RenderBuffer::_uiFuncRenderbufferStorageMultiSampleCoverage =
88 Window::invalidFunctionID;
90 // Documentation for this class is emited in the
91 // OSGRenderBufferBase.cpp file.
92 // To modify it, please change the .fcd file (OSGRenderBuffer.fcd) and
93 // regenerate the base file.
95 void RenderBuffer::bind(DrawEnv *pEnv, UInt32 index)
97 Window *pWindow = pEnv->getWindow();
99 pWindow->validateGLObject(getGLId(), pEnv);
101 OSGGETGLFUNCBYID_GL3_ES( glFramebufferRenderbuffer,
102 osgGlFramebufferRenderbuffer,
103 _uiFuncFramebufferRenderbuffer,
104 pWindow);
106 osgGlFramebufferRenderbuffer(GL_FRAMEBUFFER,
107 index,
108 GL_RENDERBUFFER,
109 pWindow->getGLObjectId(getGLId()));
112 void RenderBuffer::validate(DrawEnv *pEnv)
114 Window *pWindow = pEnv->getWindow();
116 pWindow->validateGLObject(getGLId(), pEnv);
120 /*----------------------- constructors & destructors ----------------------*/
122 RenderBuffer::RenderBuffer(void) :
123 Inherited()
127 RenderBuffer::RenderBuffer(const RenderBuffer &source) :
128 Inherited(source)
132 RenderBuffer::~RenderBuffer(void)
134 if(getGLId() > 0)
135 Window::destroyGLObject(getGLId(), 1);
138 /*----------------------------- class specific ----------------------------*/
140 void RenderBuffer::initMethod(InitPhase ePhase)
142 Inherited::initMethod(ePhase);
144 if(ePhase == TypeObject::SystemPost)
146 _uiFramebufferObjectArb =
147 Window::registerExtension("GL_ARB_framebuffer_object");
149 _uiFramebufferMultiSampleExt =
150 Window::registerExtension("GL_EXT_framebuffer_multisample");
152 _uiFramebufferMultiSampleCoverageNV =
153 Window::registerExtension("GL_NV_framebuffer_multisample_coverage");
155 _uiFuncFramebufferRenderbuffer =
156 Window::registerFunction (
157 OSG_DLSYM_UNDERSCORE"glFramebufferRenderbuffer",
158 _uiFramebufferObjectArb);
160 _uiFuncGenRenderbuffers =
161 Window::registerFunction (
162 OSG_DLSYM_UNDERSCORE"glGenRenderbuffers",
163 _uiFramebufferObjectArb);
165 _uiFuncDeleteRenderbuffers =
166 Window::registerFunction (
167 OSG_DLSYM_UNDERSCORE"glDeleteRenderbuffers",
168 _uiFramebufferObjectArb);
170 _uiFuncRenderbufferStorage =
171 Window::registerFunction (
172 OSG_DLSYM_UNDERSCORE"glRenderbufferStorage",
173 _uiFramebufferObjectArb);
176 _uiFuncBindRenderbuffer =
177 Window::registerFunction (
178 OSG_DLSYM_UNDERSCORE"glBindRenderbuffer",
179 _uiFramebufferObjectArb);
182 _uiFuncRenderbufferStorageMultiSample =
183 Window::registerFunction (
184 OSG_DLSYM_UNDERSCORE"glRenderbufferStorageMultisampleEXT",
185 _uiFramebufferMultiSampleExt);
187 _uiFuncRenderbufferStorageMultiSampleCoverage =
188 Window::registerFunction (
189 OSG_DLSYM_UNDERSCORE"glRenderbufferStorageMultisampleCoverageNV",
190 _uiFramebufferMultiSampleCoverageNV);
194 void RenderBuffer::onCreate(const RenderBuffer *source)
196 if(GlobalSystemState == Startup)
197 return;
199 setGLId(
200 Window::registerGLObject(
201 boost::bind(&RenderBuffer::handleGL,
202 RenderBufferMTUncountedPtr(this),
203 _1, _2, _3, _4),
204 &RenderBuffer::handleDestroyGL));
207 void RenderBuffer::changed(ConstFieldMaskArg whichField,
208 UInt32 origin,
209 BitVector details)
211 Inherited::changed(whichField, origin, details);
213 if(0x0000 != (whichField & (WidthFieldMask | HeightFieldMask)))
215 Window::reinitializeGLObject(getGLId());
219 void RenderBuffer::dump( UInt32 ,
220 const BitVector ) const
222 SLOG << "Dump RenderBuffer NI" << std::endl;
225 void RenderBuffer::resizeBuffers(UInt32 uiWidth, UInt32 uiHeight)
227 Inherited::resize(uiWidth, uiHeight);
229 Window::reinitializeGLObject(getGLId());
232 GLenum RenderBuffer::getBufferFormat(void) const
234 return _sfInternalFormat.getValue();
237 UInt32 RenderBuffer::handleGL(DrawEnv *pEnv,
238 UInt32 osgid,
239 Window::GLObjectStatusE mode,
240 UInt64 )
242 Window *pWindow = pEnv->getWindow();
244 if(mode == Window::initialize || mode == Window::reinitialize ||
245 mode == Window::needrefresh )
247 GLuint uiBufferId = 0;
249 if(mode == Window::initialize)
251 OSGGETGLFUNCBYID_GL3_ES( glGenRenderbuffers,
252 osgGlGenRenderbuffers,
253 _uiFuncGenRenderbuffers,
254 pWindow);
256 osgGlGenRenderbuffers(1, &uiBufferId);
258 pWindow->setGLObjectId(osgid, uiBufferId);
260 else
262 uiBufferId = pWindow->getGLObjectId(osgid);
265 OSGGETGLFUNCBYID_GL3_ES( glBindRenderbuffer,
266 osgGlBindRenderbuffer,
267 _uiFuncBindRenderbuffer,
268 pWindow);
270 OSGGETGLFUNCBYID_GL3_ES( glRenderbufferStorage,
271 osgGlRenderbufferStorage,
272 _uiFuncRenderbufferStorage,
273 pWindow);
275 osgGlBindRenderbuffer(GL_RENDERBUFFER, uiBufferId);
277 GLenum internalFormat = getInternalFormat();
279 if(internalFormat == GL_NONE && getImage() != NULL)
281 internalFormat = getImage()->getPixelFormat();
285 if(pWindow->hasExtension(_uiFramebufferMultiSampleCoverageNV) == true &&
287 (_sfColorSamples .getValue() != 0 ||
288 _sfCoverageSamples.getValue() != 0 ) )
290 OSGGETGLFUNCBYID_EXT(
291 glRenderbufferStorageMultisampleCoverage,
292 osgGlRenderbufferStorageMultisampleCoverage,
293 _uiFuncRenderbufferStorageMultiSampleCoverage,
294 pWindow);
296 osgGlRenderbufferStorageMultisampleCoverage(
297 GL_RENDERBUFFER,
298 _sfCoverageSamples.getValue(),
299 _sfColorSamples .getValue(),
300 internalFormat,
301 getWidth(),
302 getHeight());
304 else if(pWindow->hasExtOrVersion(
305 _uiFramebufferMultiSampleExt,
306 0x0300 ) == true &&
308 (_sfColorSamples .getValue() != 0) )
310 OSGGETGLFUNCBYID_GL3( glRenderbufferStorageMultisample,
311 osgGlRenderbufferStorageMultisample,
312 _uiFuncRenderbufferStorageMultiSample,
313 pWindow );
315 osgGlRenderbufferStorageMultisample(
316 GL_RENDERBUFFER,
317 _sfColorSamples.getValue(),
318 internalFormat,
319 getWidth(),
320 getHeight() );
322 else
324 osgGlRenderbufferStorage(GL_RENDERBUFFER,
325 internalFormat,
326 getWidth(),
327 getHeight());
331 return 0;
335 void RenderBuffer::handleDestroyGL(DrawEnv *pEnv,
336 UInt32 osgid,
337 Window::GLObjectStatusE mode)
339 Window *pWindow = pEnv->getWindow();
341 if(mode == Window::destroy)
343 GLuint uiBufferId = pWindow->getGLObjectId(osgid);
345 OSGGETGLFUNCBYID_GL3_ES( glDeleteRenderbuffers,
346 osgGlDeleteRenderbuffers,
347 _uiFuncDeleteRenderbuffers,
348 pWindow);
350 osgGlDeleteRenderbuffers(1, &uiBufferId);
352 pWindow->setGLObjectId(osgid, 0);
354 else if(mode == Window::finaldestroy)
356 //SWARNING << "Last texture user destroyed" << std::endl;
360 void RenderBuffer::processPreDeactivate (DrawEnv *pEnv, UInt32 index)
362 if(_sfReadBack.getValue() == true)
364 Image *pImg = this->getImage();
366 if(pImg == NULL)
367 return;
369 if(pImg->getData() == NULL)
371 SINFO << "TextureBuffer::render: (Re)Allocating image "
372 << "for read-back."
373 << endLog;
375 pImg->set(pImg->getPixelFormat(),
376 pImg->getWidth (),
377 pImg->getHeight (),
378 pImg->getDepth (),
379 pImg->getMipMapCount(),
380 pImg->getFrameCount (),
381 pImg->getFrameDelay (),
382 NULL,
383 pImg->getDataType (),
384 true,
385 pImg->getSideCount () );
388 // select GL_COLORATTACHMENTn and read data into image
389 #ifndef OSG_OGL_ES2
390 glReadBuffer(index);
391 #endif
392 glReadPixels(0, 0,
393 pImg->getWidth (),
394 pImg->getHeight (),
395 pImg->getPixelFormat(),
396 pImg->getDataType (),
397 pImg->editData ());
399 #ifndef OSG_OGL_ES2
400 glReadBuffer(GL_NONE);
401 #endif
403 glErr("renderbuffer:predeactivate");
407 void RenderBuffer::processPostDeactivate(DrawEnv *pEnv)
411 OSG_END_NAMESPACE