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.
15#define OSG_TEXTURE2D 1
21/** Encapsulates OpenGL 2D texture functionality. Doesn't support cube maps,
22 * so ignore \a face parameters.
24class OSG_EXPORT Texture2D : public Texture
31 Texture2D(Image* image);
33 template<class T> Texture2D(const osg::ref_ptr<T>& image):
38 setUseHardwareMipMapGeneration(true);
39 setImage(image.get());
42 /** Copy constructor using CopyOp to manage deep vs shallow copy. */
43 Texture2D(const Texture2D& text,const CopyOp& copyop=CopyOp::SHALLOW_COPY);
45 META_StateAttribute(osg, Texture2D,TEXTURE);
47 /** Return -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs. */
48 virtual int compare(const StateAttribute& rhs) const;
50 virtual GLenum getTextureTarget() const { return GL_TEXTURE_2D; }
52 /** Sets the texture image. */
53 void setImage(Image* image);
55 template<class T> void setImage(const ref_ptr<T>& image) { setImage(image.get()); }
57 /** Gets the texture image. */
58 Image* getImage() { return _image.get(); }
60 /** Gets the const texture image. */
61 inline const Image* getImage() const { return _image.get(); }
63 /** return true if the texture image data has been modified and the associated GL texture object needs to be updated.*/
64 virtual bool isDirty(unsigned int contextID) const { return (_image.valid() && _image->getModifiedCount()!=_modifiedCount[contextID]); }
66 inline unsigned int& getModifiedCount(unsigned int contextID) const
68 // get the modified count for the current contextID.
69 return _modifiedCount[contextID];
73 /** Sets the texture image, ignoring face. */
74 virtual void setImage(unsigned int, Image* image) { setImage(image); }
76 template<class T> void setImage(unsigned int, const ref_ptr<T>& image) { setImage(image.get()); }
78 /** Gets the texture image, ignoring face. */
79 virtual Image* getImage(unsigned int) { return _image.get(); }
81 /** Gets the const texture image, ignoring face. */
82 virtual const Image* getImage(unsigned int) const { return _image.get(); }
84 /** Gets the number of images that can be assigned to the Texture. */
85 virtual unsigned int getNumImages() const { return 1; }
88 /** Sets the texture width and height. If width or height are zero,
89 * calculate the respective value from the source image size. */
90 inline void setTextureSize(int width, int height) const
92 _textureWidth = width;
93 _textureHeight = height;
96 void setTextureWidth(int width) { _textureWidth=width; }
97 void setTextureHeight(int height) { _textureHeight=height; }
99 virtual int getTextureWidth() const { return _textureWidth; }
100 virtual int getTextureHeight() const { return _textureHeight; }
101 virtual int getTextureDepth() const { return 1; }
103 class OSG_EXPORT SubloadCallback : public Referenced
107 virtual bool textureObjectValid(const Texture2D& texture, State& state) const
109 return texture.textureObjectValid(state);
112 virtual osg::ref_ptr<TextureObject> generateTextureObject(const Texture2D& texture, State& state) const
114 return osg::Texture::generateTextureObject(&texture, state.getContextID(), GL_TEXTURE_2D);
117 virtual void load(const Texture2D& texture,State& state) const = 0;
118 virtual void subload(const Texture2D& texture,State& state) const = 0;
121 void setSubloadCallback(SubloadCallback* cb) { _subloadCallback = cb;; }
123 SubloadCallback* getSubloadCallback() { return _subloadCallback.get(); }
125 const SubloadCallback* getSubloadCallback() const { return _subloadCallback.get(); }
128 /** Helper function. Sets the number of mipmap levels created for this
129 * texture. Should only be called within an osg::Texture::apply(), or
130 * during a custom OpenGL texture load. */
131 void setNumMipmapLevels(unsigned int num) const { _numMipmapLevels=num; }
133 /** Gets the number of mipmap levels created. */
134 unsigned int getNumMipmapLevels() const { return _numMipmapLevels; }
137 /** Copies pixels into a 2D texture image, as per glCopyTexImage2D.
138 * Creates an OpenGL texture object from the current OpenGL background
139 * framebuffer contents at position \a x, \a y with width \a width and
140 * height \a height. \a width and \a height must be a power of two. */
141 void copyTexImage2D(State& state, int x, int y, int width, int height );
143 /** Copies a two-dimensional texture subimage, as per
144 * glCopyTexSubImage2D. Updates a portion of an existing OpenGL
145 * texture object from the current OpenGL background framebuffer
146 * contents at position \a x, \a y with width \a width and height
147 * \a height. Loads framebuffer data into the texture using offsets
148 * \a xoffset and \a yoffset. \a width and \a height must be powers
150 void copyTexSubImage2D(State& state, int xoffset, int yoffset, int x, int y, int width, int height );
153 /** Bind the texture object. If the texture object hasn't already been
154 * compiled, create the texture mipmap levels. */
155 virtual void apply(State& state) const;
161 virtual ~Texture2D();
163 virtual void computeInternalFormat() const;
164 void allocateMipmap(State& state) const;
166 /** Return true of the TextureObject assigned to the context associate with osg::State object is valid.*/
167 bool textureObjectValid(State& state) const;
169 friend class SubloadCallback;
171 ref_ptr<Image> _image;
173 /** Subloaded images can have different texture and image sizes. */
174 mutable GLsizei _textureWidth, _textureHeight;
176 /** Number of mipmap levels created. */
177 mutable GLsizei _numMipmapLevels;
179 ref_ptr<SubloadCallback> _subloadCallback;
181 typedef buffered_value<unsigned int> ImageModifiedCount;
182 mutable ImageModifiedCount _modifiedCount;