1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
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.
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.
17#define OSG_TEXTURE1D 1
22 #define GL_TEXTURE_1D 0x0DE0
27/** Encapsulates OpenGL 1D texture functionality. Doesn't support cube maps,
28 * so ignore \a face parameters.
30class OSG_EXPORT Texture1D : public Texture
37 Texture1D(Image* image);
39 template<class T> Texture1D(const osg::ref_ptr<T>& image):
43 setImage(image.get());
46 /** Copy constructor using CopyOp to manage deep vs shallow copy. */
47 Texture1D(const Texture1D& text,const CopyOp& copyop=CopyOp::SHALLOW_COPY);
49 META_StateAttribute(osg, Texture1D,TEXTURE);
51 /** Return -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs. */
52 virtual int compare(const StateAttribute& rhs) const;
54 virtual GLenum getTextureTarget() const { return GL_TEXTURE_1D; }
56 /** Sets the texture image. */
57 void setImage(Image* image);
59 template<class T> void setImage(const ref_ptr<T>& image) { setImage(image.get()); }
61 /** Gets the texture image. */
62 Image* getImage() { return _image.get(); }
64 /** Gets the const texture image. */
65 inline const Image* getImage() const { return _image.get(); }
68 /** return true if the texture image data has been modified and the associated GL texture object needs to be updated.*/
69 virtual bool isDirty(unsigned int contextID) const { return (_image.valid() && _image->getModifiedCount()!=_modifiedCount[contextID]); }
72 inline unsigned int& getModifiedCount(unsigned int contextID) const
74 // get the modified count for the current contextID.
75 return _modifiedCount[contextID];
79 /** Sets the texture image, ignoring face. */
80 virtual void setImage(unsigned int, Image* image) { setImage(image); }
82 /** Gets the texture image, ignoring face. */
83 virtual Image* getImage(unsigned int) { return _image.get(); }
85 /** Gets the const texture image, ignoring face. */
86 virtual const Image* getImage(unsigned int) const { return _image.get(); }
88 /** Gets the number of images that can be assigned to the Texture. */
89 virtual unsigned int getNumImages() const { return 1; }
92 /** Sets the texture width. If width is zero, calculate the value
93 * from the source image width. */
94 inline void setTextureWidth(int width) { _textureWidth = width; }
96 /** Gets the texture width. */
97 virtual int getTextureWidth() const { return _textureWidth; }
98 virtual int getTextureHeight() const { return 1; }
99 virtual int getTextureDepth() const { return 1; }
102 class OSG_EXPORT SubloadCallback : public Referenced
105 virtual void load(const Texture1D& texture,State& state) const = 0;
106 virtual void subload(const Texture1D& texture,State& state) const = 0;
109 void setSubloadCallback(SubloadCallback* cb) { _subloadCallback = cb;; }
111 SubloadCallback* getSubloadCallback() { return _subloadCallback.get(); }
113 const SubloadCallback* getSubloadCallback() const { return _subloadCallback.get(); }
116 /** Helper function. Sets the number of mipmap levels created for this
117 * texture. Should only be called within an osg::Texture::apply(), or
118 * during a custom OpenGL texture load. */
119 void setNumMipmapLevels(unsigned int num) const { _numMipmapLevels=num; }
121 /** Gets the number of mipmap levels created. */
122 unsigned int getNumMipmapLevels() const { return _numMipmapLevels; }
125 /** Copies pixels into a 1D texture image, as per glCopyTexImage1D.
126 * Creates an OpenGL texture object from the current OpenGL background
127 * framebuffer contents at position \a x, \a y with width \a width.
128 * \a width must be a power of two. */
129 void copyTexImage1D(State& state, int x, int y, int width);
131 /** Copies a one-dimensional texture subimage, as per
132 * glCopyTexSubImage1D. Updates a portion of an existing OpenGL
133 * texture object from the current OpenGL background framebuffer
134 * contents at position \a x, \a y with width \a width. */
135 void copyTexSubImage1D(State& state, int xoffset, int x, int y, int width);
138 /** Bind the texture object. If the texture object hasn't already been
139 * compiled, create the texture mipmap levels. */
140 virtual void apply(State& state) const;
144 virtual ~Texture1D();
146 virtual void computeInternalFormat() const;
147 void allocateMipmap(State& state) const;
149 /** Helper method. Create the texture without setting or using a
150 * texture binding. */
151 void applyTexImage1D(GLenum target, Image* image, State& state, GLsizei& width, GLsizei& numMipmapLevels) const;
154 /** It's not ideal that _image is mutable, but it's required since
155 * Image::ensureDimensionsArePowerOfTwo() can only be called in a
156 * valid OpenGL context, and therefore within Texture::apply, which
158 mutable ref_ptr<Image> _image;
160 /** Subloaded images can have different texture and image sizes. */
161 mutable GLsizei _textureWidth;
163 /** Number of mipmap levels created. */
164 mutable GLsizei _numMipmapLevels;
166 ref_ptr<SubloadCallback> _subloadCallback;
168 typedef buffered_value<unsigned int> ImageModifiedCount;
169 mutable ImageModifiedCount _modifiedCount;