fixed: auto_ptr -> unique_ptr
[opensg.git] / Source / System / Window / FrameBufferObjects / OSGTextureBuffer.cpp
blob7fc564ffebf2ea4921da8772d13b4bf0b3178359
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>
46 #include "OSGConfig.h"
47 #include "OSGGL.h"
48 #include "OSGGLEXT.h"
49 #include "OSGGLFuncProtos.h"
51 #include "OSGTextureBuffer.h"
52 #include "OSGWindow.h"
53 #include "OSGDrawEnv.h"
54 #include "OSGTextureObjChunk.h"
55 #include "OSGImage.h"
57 OSG_BEGIN_NAMESPACE
59 UInt32 TextureBuffer::_uiFramebuffer_object_extension =
60 Window::invalidExtensionID;
62 UInt32 TextureBuffer::_uiFuncFramebufferTexture1D = Window::invalidFunctionID;
63 UInt32 TextureBuffer::_uiFuncFramebufferTexture2D = Window::invalidFunctionID;
64 UInt32 TextureBuffer::_uiFuncFramebufferTexture3D = Window::invalidFunctionID;
65 UInt32 TextureBuffer::_uiFuncFramebufferTextureLayer = Window::invalidFunctionID;
66 UInt32 TextureBuffer::_uiFuncGenerateMipmap = Window::invalidFunctionID;
68 // Documentation for this class is emited in the
69 // OSGTextureBufferBase.cpp file.
70 // To modify it, please change the .fcd file (OSGTextureBuffer.fcd) and
71 // regenerate the base file.
73 void TextureBuffer::bind(DrawEnv *pEnv, UInt32 index)
75 Window *pWindow = pEnv->getWindow();
77 if(_sfTexture.getValue() != NULL)
79 pWindow->validateGLObject(_sfTexture.getValue()->getGLId(), pEnv);
81 GLenum target;
83 if(getTexTarget() != GL_NONE)
85 target = getTexTarget();
87 else
89 target = _sfTexture.getValue()->determineTextureTarget(pWindow);
92 switch(target)
94 #ifndef OSG_OGL_ES2
95 case GL_TEXTURE_1D:
97 OSGGETGLFUNCBYID_GL3( glFramebufferTexture1D,
98 osgGlFramebufferTexture1D,
99 _uiFuncFramebufferTexture1D,
100 pWindow);
102 osgGlFramebufferTexture1D(
103 GL_FRAMEBUFFER,
104 index,
105 target,
106 pWindow->getGLObjectId(_sfTexture.getValue()->getGLId()),
107 getLevel());
109 break;
110 #endif
112 case GL_TEXTURE_2D:
113 case GL_TEXTURE_RECTANGLE_ARB:
114 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
115 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
116 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
117 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
118 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
119 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
121 OSGGETGLFUNCBYID_GL3_ES( glFramebufferTexture2D,
122 osgGlFramebufferTexture2D,
123 _uiFuncFramebufferTexture2D,
124 pWindow);
126 osgGlFramebufferTexture2D(
127 GL_FRAMEBUFFER,
128 index,
129 target,
130 pWindow->getGLObjectId(_sfTexture.getValue()->getGLId()),
131 getLevel());
133 break;
135 case GL_TEXTURE_3D:
137 OSGGETGLFUNCBYID_GL3( glFramebufferTexture3D,
138 osgGlFramebufferTexture3D,
139 _uiFuncFramebufferTexture3D,
140 pWindow);
142 osgGlFramebufferTexture3D(
143 GL_FRAMEBUFFER,
144 index,
145 target,
146 pWindow->getGLObjectId(_sfTexture.getValue()->getGLId()),
147 getLevel(),
148 getLayer());
150 break;
152 case GL_TEXTURE_2D_ARRAY:
153 case GL_TEXTURE_CUBE_MAP_ARRAY:
155 OSGGETGLFUNCBYID_GL3( glFramebufferTextureLayer,
156 osgGlFramebufferTextureLayer,
157 _uiFuncFramebufferTextureLayer,
158 pWindow);
160 osgGlFramebufferTextureLayer(
161 GL_FRAMEBUFFER,
162 index,
163 pWindow->getGLObjectId(_sfTexture.getValue()->getGLId()),
164 getLevel(),
165 getLayer());
167 break;
173 void TextureBuffer::validate(DrawEnv *pEnv)
175 Window *pWindow = pEnv->getWindow();
177 if(_sfTexture.getValue() != NULL)
179 pWindow->validateGLObject(_sfTexture.getValue()->getGLId(), pEnv);
183 void TextureBuffer::resizeBuffers(UInt32 uiWidth, UInt32 uiHeight)
185 TextureObjChunk *pTex = this->getTexture();
187 if(pTex == NULL)
188 return;
190 Image *pImg = pTex->getImage();
192 if(pImg == NULL)
193 return;
195 pImg->set(pImg->getPixelFormat(), //Image::OSG_RGB_PF,
196 uiWidth,
197 uiHeight,
198 pImg->getDepth (),
199 pImg->getMipMapCount(),
200 pImg->getFrameCount (),
201 pImg->getFrameDelay (),
202 NULL,
203 pImg->getDataType (), //Image::OSG_UINT8_IMAGEDATA,
204 false,
205 pImg->getSideCount ());
208 GLenum TextureBuffer::getBufferFormat(void) const
210 TextureObjChunk *pTex = this->getTexture();
212 if(pTex == NULL)
213 return GL_NONE;
215 if(pTex->getInternalFormat() != GL_NONE)
216 return pTex->getInternalFormat();
218 Image *pImg = pTex->getImage();
220 if(pImg == NULL)
221 return GL_NONE;
223 return pImg->getPixelFormat();
227 void TextureBuffer::processPreDeactivate(DrawEnv *pEnv, UInt32 index)
229 if(_sfReadBack.getValue() == true)
231 TextureObjChunk *pTexObj = this->getTexture();
233 if(pTexObj == NULL)
234 return;
236 Image *pTexImg = pTexObj->getImage();
238 if(pTexImg == NULL)
239 return;
241 if(pTexImg->getData() == NULL)
243 SINFO << "TextureBuffer::render: (Re)Allocating image "
244 << "for read-back."
245 << endLog;
247 pTexImg->set(pTexImg->getPixelFormat(),
248 pTexImg->getWidth (),
249 pTexImg->getHeight (),
250 pTexImg->getDepth (),
251 pTexImg->getMipMapCount(),
252 pTexImg->getFrameCount (),
253 pTexImg->getFrameDelay (),
254 NULL,
255 pTexImg->getDataType (),
256 true,
257 pTexImg->getSideCount () );
260 UInt32 mipMapLevel = _sfLevel.getValue();
261 UInt32 frame = 0;
262 UInt32 side = 0;
263 GLenum target;
264 Window *pWindow = pEnv->getWindow();
266 if(_sfTexTarget.getValue() != GL_NONE)
268 target = _sfTexTarget.getValue();
270 else
272 target = _sfTexture.getValue()->determineTextureTarget(pWindow);
275 switch(target)
277 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
278 side = 0;
279 break;
280 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
281 side = 1;
282 break;
283 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
284 side = 2;
285 break;
286 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
287 side = 3;
288 break;
289 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
290 side = 4;
291 break;
292 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
293 side = 5;
294 break;
297 // select GL_COLORATTACHMENTn and read data into image
298 #ifndef OSG_OGL_ES2
299 glReadBuffer(index);
300 #endif
301 glReadPixels(0, 0,
302 pTexImg->getWidth (),
303 pTexImg->getHeight (),
304 pTexImg->getPixelFormat(),
305 pTexImg->getDataType (),
306 pTexImg->editData (mipMapLevel, frame, side));
308 #ifndef OSG_OGL_ES2
309 glReadBuffer(GL_NONE);
310 #endif
314 void TextureBuffer::processPostDeactivate(DrawEnv *pEnv)
316 Window *win = pEnv->getWindow();
318 // If there are TextureBuffers with mipmap filters attached,
319 // the mipmaps need to be regenerated
320 TextureObjChunk *pTexObj = this->getTexture();
322 if(pTexObj == NULL)
323 return;
325 GLenum target = pTexObj->determineTextureTarget(win);
327 if(target == GL_TEXTURE_2D &&
328 (pTexObj->getMinFilter() == GL_NEAREST_MIPMAP_NEAREST ||
329 pTexObj->getMinFilter() == GL_LINEAR_MIPMAP_NEAREST ||
330 pTexObj->getMinFilter() == GL_NEAREST_MIPMAP_LINEAR ||
331 pTexObj->getMinFilter() == GL_LINEAR_MIPMAP_LINEAR ) )
333 OSGGETGLFUNCBYID_GL3_ES( glGenerateMipmap,
334 osgGlGenerateMipmap,
335 _uiFuncGenerateMipmap,
336 win);
338 glBindTexture(target, win->getGLObjectId(pTexObj->getGLId()));
340 osgGlGenerateMipmap(target);
344 /*----------------------- constructors & destructors ----------------------*/
346 TextureBuffer::TextureBuffer(void) :
347 Inherited()
351 TextureBuffer::TextureBuffer(const TextureBuffer &source) :
352 Inherited(source)
356 TextureBuffer::~TextureBuffer(void)
360 /*----------------------------- class specific ----------------------------*/
362 void TextureBuffer::initMethod(InitPhase ePhase)
364 Inherited::initMethod(ePhase);
366 if(ePhase == TypeObject::SystemPost)
368 _uiFramebuffer_object_extension =
369 Window::registerExtension("GL_ARB_framebuffer_object");
371 _uiFuncFramebufferTexture1D =
372 Window::registerFunction (
373 OSG_DLSYM_UNDERSCORE"glFramebufferTexture1D",
374 _uiFramebuffer_object_extension);
376 _uiFuncFramebufferTexture2D =
377 Window::registerFunction (
378 OSG_DLSYM_UNDERSCORE"glFramebufferTexture2D",
379 _uiFramebuffer_object_extension);
381 _uiFuncFramebufferTexture3D =
382 Window::registerFunction (
383 OSG_DLSYM_UNDERSCORE"glFramebufferTexture3D",
384 _uiFramebuffer_object_extension);
386 _uiFuncFramebufferTextureLayer =
387 Window::registerFunction (
388 OSG_DLSYM_UNDERSCORE"glFramebufferTextureLayer",
389 _uiFramebuffer_object_extension);
391 _uiFuncGenerateMipmap =
392 Window::registerFunction (
393 OSG_DLSYM_UNDERSCORE"glGenerateMipmap",
394 _uiFramebuffer_object_extension);
399 void TextureBuffer::changed(ConstFieldMaskArg whichField,
400 UInt32 origin,
401 BitVector details)
403 Inherited::changed(whichField, origin, details);
406 void TextureBuffer::dump( UInt32 ,
407 const BitVector ) const
409 SLOG << "Dump TextureBuffer NI" << std::endl;
412 OSG_END_NAMESPACE