root/OpenSceneGraph/trunk/src/osg/Texture1D.cpp @ 10924

Revision 10924, 16.6 kB (checked in by robert, 4 years ago)

Refactored the way that osg::Image/ImageSequence manages the update callback that needs to be attached to Textures to make it possible to use the Image::update() mechansim in other subclasses from osg::Image.
To enable the automatic attachment of the required update callback to call osg::Image::update(..) subclasses from osg::Image will
need to implement the osg::Image::requestUpdateCall() and return true, and implement the osg::Image::update(NodeVisitor?*) method to recieve the update call during the update traversal.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
2 *
3 * This library is open source and may be redistributed and/or modified under 
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version.  The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 * OpenSceneGraph Public License for more details.
12*/
13#include <osg/GLExtensions>
14#include <osg/Texture1D>
15#include <osg/State>
16#include <osg/GLU>
17
18typedef void (APIENTRY * MyCompressedTexImage1DArbProc) (GLenum target, GLint level, GLenum internalformat, GLsizei width, GLint border, GLsizei imageSize, const GLvoid *data);
19
20using namespace osg;
21
22Texture1D::Texture1D():
23            _textureWidth(0),
24            _numMipmapLevels(0)
25{
26}
27
28Texture1D::Texture1D(osg::Image* image):
29            _textureWidth(0),
30            _numMipmapLevels(0)
31{
32    setImage(image);
33}
34
35Texture1D::Texture1D(const Texture1D& text,const CopyOp& copyop):
36            Texture(text,copyop),
37            _image(copyop(text._image.get())),
38            _textureWidth(text._textureWidth),
39            _numMipmapLevels(text._numMipmapLevels),
40            _subloadCallback(text._subloadCallback)
41{
42}
43
44Texture1D::~Texture1D()
45{
46}
47
48int Texture1D::compare(const StateAttribute& sa) const
49{
50    // check the types are equal and then create the rhs variable
51    // used by the COMPARE_StateAttribute_Parameter macro's below.
52    COMPARE_StateAttribute_Types(Texture1D,sa)
53
54    if (_image!=rhs._image) // smart pointer comparison.
55    {
56        if (_image.valid())
57        {
58            if (rhs._image.valid())
59            {
60                int result = _image->compare(*rhs._image);
61                if (result!=0) return result;
62            }
63            else
64            {
65                return 1; // valid lhs._image is greater than null.
66            }
67        }
68        else if (rhs._image.valid())
69        {
70            return -1; // valid rhs._image is greater than null.
71        }
72    }
73
74    if (!_image && !rhs._image)
75    {
76        // no image attached to either Texture2D
77        // but could these textures already be downloaded?
78        // check the _textureObjectBuffer to see if they have been
79        // downloaded
80
81        int result = compareTextureObjects(rhs);
82        if (result!=0) return result;
83    }
84
85    int result = compareTexture(rhs);
86    if (result!=0) return result;
87
88    // compare each parameter in turn against the rhs.
89    COMPARE_StateAttribute_Parameter(_textureWidth)
90    COMPARE_StateAttribute_Parameter(_subloadCallback)
91
92    return 0; // passed all the above comparison macro's, must be equal.
93}
94
95void Texture1D::setImage(Image* image)
96{
97    if (_image == image) return;
98
99    if (_image.valid() && _image->requiresUpdateCall())
100    {
101        setUpdateCallback(0);
102        setDataVariance(osg::Object::STATIC);
103    }
104
105    // delete old texture objects.
106    dirtyTextureObject();
107
108    _image = image;
109    _modifiedCount.setAllElementsTo(0);
110   
111    if (_image.valid() && _image->requiresUpdateCall())
112    {
113        setUpdateCallback(new Image::UpdateCallback());
114        setDataVariance(osg::Object::DYNAMIC);
115    }
116}
117
118
119void Texture1D::apply(State& state) const
120{
121#if !defined(OSG_GLES1_AVAILABLE) && !defined(OSG_GLES2_AVAILABLE)
122    // get the contextID (user defined ID of 0 upwards) for the
123    // current OpenGL context.
124    const unsigned int contextID = state.getContextID();
125
126    Texture::TextureObjectManager* tom = Texture::getTextureObjectManager(contextID).get();
127    ElapsedTime elapsedTime(&(tom->getApplyTime()));
128    tom->getNumberApplied()++;
129
130    // get the texture object for the current contextID.
131    TextureObject* textureObject = getTextureObject(contextID);
132
133    if (textureObject)
134    {
135        if (_image.valid() && getModifiedCount(contextID) != _image->getModifiedCount())
136        {
137            // compute the internal texture format, this set the _internalFormat to an appropriate value.
138            computeInternalFormat();
139
140            GLsizei new_width = _image->s();
141            GLsizei new_numMipmapLevels = _numMipmapLevels;
142
143            if (!textureObject->match(GL_TEXTURE_1D, new_numMipmapLevels, _internalFormat, new_width, 1, 1, _borderWidth))
144            {
145                Texture::releaseTextureObject(contextID, _textureObjectBuffer[contextID].get());
146                _textureObjectBuffer[contextID] = 0;
147                textureObject = 0;
148            }
149        }
150    }
151
152    if (textureObject)
153    {
154        textureObject->bind();
155
156        if (getTextureParameterDirty(state.getContextID())) applyTexParameters(GL_TEXTURE_1D,state);
157
158        if (_subloadCallback.valid())
159        {
160            _subloadCallback->subload(*this,state);
161        }
162        else if (_image.valid() && getModifiedCount(contextID) != _image->getModifiedCount())
163        {
164            applyTexImage1D(GL_TEXTURE_1D,_image.get(),state, _textureWidth, _numMipmapLevels);
165
166            // update the modified count to show that it is upto date.
167            getModifiedCount(contextID) = _image->getModifiedCount();
168        }
169
170    }
171    else if (_subloadCallback.valid())
172    {
173
174        // we don't have a applyTexImage1D_subload yet so can't reuse.. so just generate a new texture object.       
175        _textureObjectBuffer[contextID] = textureObject = generateTextureObject(this, contextID, GL_TEXTURE_1D);
176
177        textureObject->bind();
178
179        applyTexParameters(GL_TEXTURE_1D,state);
180
181        _subloadCallback->load(*this,state);
182
183        textureObject->setAllocated(_numMipmapLevels,_internalFormat,_textureWidth,1,1,0);
184
185        // in theory the following line is redundent, but in practice
186        // have found that the first frame drawn doesn't apply the textures
187        // unless a second bind is called?!!
188        // perhaps it is the first glBind which is not required...
189        //glBindTexture( GL_TEXTURE_1D, handle );
190
191    }
192    else if (_image.valid() && _image->data())
193    {
194
195        // we don't have a applyTexImage1D_subload yet so can't reuse.. so just generate a new texture object.       
196        textureObject = generateTextureObject(this, contextID,GL_TEXTURE_1D);
197
198        textureObject->bind();
199
200        applyTexParameters(GL_TEXTURE_1D,state);
201
202        applyTexImage1D(GL_TEXTURE_1D,_image.get(),state, _textureWidth, _numMipmapLevels);
203
204        textureObject->setAllocated(_numMipmapLevels,_internalFormat,_textureWidth,1,1,0);
205
206        // update the modified count to show that it is upto date.
207        getModifiedCount(contextID) = _image->getModifiedCount();
208   
209        _textureObjectBuffer[contextID] = textureObject;
210   
211        if (state.getMaxTexturePoolSize()==0 && _unrefImageDataAfterApply && areAllTextureObjectsLoaded() && _image->getDataVariance()==STATIC)
212        {
213            Texture1D* non_const_this = const_cast<Texture1D*>(this);
214            non_const_this->_image = 0;
215        }
216       
217    }
218    else if ( (_textureWidth!=0) && (_internalFormat!=0) )
219    {
220        _textureObjectBuffer[contextID] = textureObject = generateTextureObject(
221                this,contextID,GL_TEXTURE_1D,_numMipmapLevels,_internalFormat,_textureWidth,1,1,0);
222       
223        textureObject->bind();
224
225        applyTexParameters(GL_TEXTURE_1D,state);
226
227        // no image present, but dimensions are set so lets create the texture
228        glTexImage1D( GL_TEXTURE_1D, 0, _internalFormat,
229                     _textureWidth, _borderWidth,
230                     _sourceFormat ? _sourceFormat : _internalFormat,
231                     _sourceType ? _sourceType : GL_UNSIGNED_BYTE,
232                     0);               
233                     
234        if (_readPBuffer.valid())
235        {
236            _readPBuffer->bindPBufferToTexture(GL_FRONT);
237        }
238
239    }
240    else
241    {
242        glBindTexture( GL_TEXTURE_1D, 0 );
243    }
244
245    // if texture object is now valid and we have to allocate mipmap levels, then
246    if (textureObject != 0 && _texMipmapGenerationDirtyList[contextID])
247    {
248        generateMipmap(state);
249    }
250#else
251    osg::notify(osg::NOTICE)<<"Warning: Texture1D::apply(State& state) not supported."<<std::endl;
252#endif
253}
254
255void Texture1D::computeInternalFormat() const
256{
257    if (_image.valid()) computeInternalFormatWithImage(*_image);
258    else computeInternalFormatType();
259}
260
261void Texture1D::applyTexImage1D(GLenum target, Image* image, State& state, GLsizei& inwidth, GLsizei& numMipmapLevels) const
262{
263#if !defined(OSG_GLES1_AVAILABLE) && !defined(OSG_GLES2_AVAILABLE)
264    // if we don't have a valid image we can't create a texture!
265    if (!image || !image->data())
266        return;
267
268    // get the contextID (user defined ID of 0 upwards) for the
269    // current OpenGL context.
270    const unsigned int contextID = state.getContextID();
271    const Extensions* extensions = getExtensions(contextID,true);
272
273
274    // compute the internal texture format, this set the _internalFormat to an appropriate value.
275    computeInternalFormat();
276
277    // select the internalFormat required for the texture.
278    bool compressed = isCompressedInternalFormat(_internalFormat);
279   
280    //Rescale if resize hint is set or NPOT not supported or dimension exceeds max size
281    if( _resizeNonPowerOfTwoHint || !extensions->isNonPowerOfTwoTextureSupported(_min_filter) || inwidth > extensions->maxTextureSize() )
282    {
283        // this is not thread safe... should really create local image data and rescale to that as per Texture2D.
284        image->ensureValidSizeForTexturing(extensions->maxTextureSize());
285    }
286
287    glPixelStorei(GL_UNPACK_ALIGNMENT,image->getPacking());
288
289    static MyCompressedTexImage1DArbProc glCompressedTexImage1D_ptr =
290        convertPointerType<MyCompressedTexImage1DArbProc, void*>(getGLExtensionFuncPtr("glCompressedTexImage1DARB"));
291
292    if( _min_filter == LINEAR || _min_filter == NEAREST )
293    {
294        if ( !compressed )
295        {
296            numMipmapLevels = 1;
297            glTexImage1D( target, 0, _internalFormat,
298                image->s(), _borderWidth,
299                (GLenum)image->getPixelFormat(),
300                (GLenum)image->getDataType(),
301                image->data() );
302
303        }
304        else if(glCompressedTexImage1D_ptr)
305        {
306            numMipmapLevels = 1;
307            GLint blockSize = ( _internalFormat == GL_COMPRESSED_RGB_S3TC_DXT1_EXT ? 8 : 16 );
308            GLint size = ((image->s()+3)/4)*((image->t()+3)/4)*blockSize;
309            glCompressedTexImage1D_ptr(target, 0, _internalFormat,
310                  image->s(), _borderWidth,
311                  size,
312                  image->data());               
313
314        }
315
316    }
317    else
318    {
319        if(!image->isMipmap())
320        {
321
322            numMipmapLevels = 1;
323
324#ifdef OSG_GLU_AVAILABLE
325            gluBuild1DMipmaps( target, _internalFormat,
326                image->s(),
327                (GLenum)image->getPixelFormat(), (GLenum)image->getDataType(),
328                image->data() );
329#else
330            osg::notify(osg::NOTICE)<<"Warning: gluBuild1DMipmaps(..) not supported."<<std::endl;
331#endif
332
333        }
334        else
335        {
336            numMipmapLevels = image->getNumMipmapLevels();
337
338            int width  = image->s();
339
340            if( !compressed )
341            {
342                for( GLsizei k = 0 ; k < numMipmapLevels  && width ;k++)
343                {
344
345                    glTexImage1D( target, k, _internalFormat,
346                         width,_borderWidth,
347                        (GLenum)image->getPixelFormat(),
348                        (GLenum)image->getDataType(),
349                        image->getMipmapData(k));
350
351                    width >>= 1;
352                }
353            }
354            else if(glCompressedTexImage1D_ptr)
355            {
356                GLint blockSize = ( _internalFormat == GL_COMPRESSED_RGB_S3TC_DXT1_EXT ? 8 : 16 );
357                GLint size = 0;
358                for( GLsizei k = 0 ; k < numMipmapLevels  && width ;k++)
359                {
360
361                    size = ((width+3)/4)*blockSize;
362                    glCompressedTexImage1D_ptr(target, k, _internalFormat,
363                        width,  _borderWidth, size, image->getMipmapData(k));               
364
365                    width >>= 1;
366                }
367            }
368        }
369
370    }
371
372    inwidth = image->s();
373#else
374    osg::notify(osg::NOTICE)<<"Warning: Texture1D::applyTexImage1D(State& state) not supported."<<std::endl;
375#endif
376}
377
378void Texture1D::copyTexImage1D(State& state, int x, int y, int width)
379{
380#if !defined(OSG_GLES1_AVAILABLE) && !defined(OSG_GLES2_AVAILABLE)
381    const unsigned int contextID = state.getContextID();
382
383    // get the texture object for the current contextID.
384    TextureObject* textureObject = getTextureObject(contextID);
385
386    if (textureObject != 0)
387    {
388        if (width==(int)_textureWidth)
389        {
390            // we have a valid texture object which is the right size
391            // so lets play clever and use copyTexSubImage1D instead.
392            // this allows use to reuse the texture object and avoid
393            // expensive memory allocations.
394            copyTexSubImage1D(state,0 ,x, y, width);
395            return;
396        }
397        // the relevent texture object is not of the right size so
398        // needs to been deleted   
399        // remove previously bound textures.
400        dirtyTextureObject();
401        // note, dirtyTextureObject() dirties all the texture objects for
402        // this texture, is this right?  Perhaps we should dirty just the
403        // one for this context.  Note sure yet will leave till later.
404        // RO July 2001.
405    }
406   
407   
408    // remove any previously assigned images as these are nolonger valid.
409    _image = NULL;
410
411    // switch off mip-mapping.
412    _min_filter = LINEAR;
413    _mag_filter = LINEAR;
414
415    _textureObjectBuffer[contextID] = textureObject = generateTextureObject(this, contextID,GL_TEXTURE_1D);
416
417    textureObject->bind();
418
419
420    applyTexParameters(GL_TEXTURE_1D,state);
421    glCopyTexImage1D( GL_TEXTURE_1D, 0, GL_RGBA, x, y, width, 0 );
422
423    _textureWidth = width;
424    _numMipmapLevels = 1;
425   
426    textureObject->setAllocated(_numMipmapLevels,_internalFormat,_textureWidth,1,1,0);
427
428    // inform state that this texture is the current one bound.
429    state.haveAppliedTextureAttribute(state.getActiveTextureUnit(), this);
430#else
431    osg::notify(osg::NOTICE)<<"Warning: Texture1D::copyTexImage1D(..) not supported."<<std::endl;
432#endif
433}
434
435void Texture1D::copyTexSubImage1D(State& state, int xoffset, int x, int y, int width)
436{
437#if !defined(OSG_GLES1_AVAILABLE) && !defined(OSG_GLES2_AVAILABLE)
438    const unsigned int contextID = state.getContextID();
439
440    // get the texture object for the current contextID.
441    TextureObject* textureObject = getTextureObject(contextID);
442
443    if (textureObject != 0)
444    {
445
446        textureObject->bind();
447
448        // we have a valid image
449        applyTexParameters(GL_TEXTURE_1D,state);
450        glCopyTexSubImage1D( GL_TEXTURE_1D, 0, xoffset, x, y, width);
451
452        // inform state that this texture is the current one bound.
453        state.haveAppliedTextureAttribute(state.getActiveTextureUnit(), this);
454
455    }
456    else
457    {
458        // no texture object already exsits for this context so need to
459        // create it upfront - simply call copyTexImage1D.
460        copyTexImage1D(state,x,y,width);
461    }
462#else
463    osg::notify(osg::NOTICE)<<"Warning: Texture1D::copyTexSubImage1D(..) not supported."<<std::endl;
464#endif
465}
466
467void Texture1D::allocateMipmap(State& state) const
468{
469#if !defined(OSG_GLES1_AVAILABLE) && !defined(OSG_GLES2_AVAILABLE)
470    const unsigned int contextID = state.getContextID();
471
472    // get the texture object for the current contextID.
473    TextureObject* textureObject = getTextureObject(contextID);
474   
475    if (textureObject && _textureWidth != 0)
476    {
477        // bind texture
478        textureObject->bind();
479
480        // compute number of mipmap levels
481        int width = _textureWidth;
482        int numMipmapLevels = Image::computeNumberOfMipmapLevels(width);
483
484        // we do not reallocate the level 0, since it was already allocated
485        width >>= 1;
486       
487        for( GLsizei k = 1; k < numMipmapLevels  && width; k++)
488        {
489            if (width == 0)
490                width = 1;
491
492            glTexImage1D( GL_TEXTURE_1D, k, _internalFormat,
493                     width, _borderWidth,
494                     _sourceFormat ? _sourceFormat : _internalFormat,
495                     _sourceType ? _sourceType : GL_UNSIGNED_BYTE, NULL);
496
497            width >>= 1;
498        }
499               
500        // inform state that this texture is the current one bound.
501        state.haveAppliedTextureAttribute(state.getActiveTextureUnit(), this);       
502    }
503#else
504    osg::notify(osg::NOTICE)<<"Warning: Texture1D::allocateMipmap(..) not supported."<<std::endl;
505#endif
506}
Note: See TracBrowser for help on using the browser.