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

Revision 10723, 15.9 kB (checked in by robert, 5 years ago)

Fixed build with no automatic conversion of ref_ptr<> to C pointer

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