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_TEXTURE3D 1
21/** Encapsulates OpenGL 3D texture functionality. Doesn't support cube maps,
22 * so ignore \a face parameters.
24class OSG_EXPORT Texture3D : public Texture
31 Texture3D(Image* image);
34 template<class T> Texture3D(const osg::ref_ptr<T>& image):
40 setImage(image.get());
43 /** Copy constructor using CopyOp to manage deep vs shallow copy. */
44 Texture3D(const Texture3D& text,const CopyOp& copyop=CopyOp::SHALLOW_COPY);
46 META_StateAttribute(osg, Texture3D,TEXTURE);
48 /** Return -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs. */
49 virtual int compare(const StateAttribute& rhs) const;
51 virtual GLenum getTextureTarget() const { return GL_TEXTURE_3D; }
53 /** Sets the texture image. */
54 void setImage(Image* image);
56 template<class T> void setImage(const ref_ptr<T>& image) { setImage(image.get()); }
58 /** Gets the texture image. */
59 Image* getImage() { return _image.get(); }
61 /** Gets the const texture image. */
62 inline const Image* getImage() const { return _image.get(); }
64 /** return true if the texture image data has been modified and the associated GL texture object needs to be updated.*/
65 virtual bool isDirty(unsigned int contextID) const { return (_image.valid() && _image->getModifiedCount()!=_modifiedCount[contextID]); }
67 inline unsigned int& getModifiedCount(unsigned int contextID) const
69 // get the modified count for the current contextID.
70 return _modifiedCount[contextID];
73 /** Sets the texture image, ignoring face. */
74 virtual void setImage(unsigned int, Image* image) { setImage(image); }
76 /** Gets the texture image, ignoring face. */
77 virtual Image* getImage(unsigned int) { return _image.get(); }
79 /** Gets the const texture image, ignoring face. */
80 virtual const Image* getImage(unsigned int) const { return _image.get(); }
82 /** Gets the number of images that can be assigned to the Texture. */
83 virtual unsigned int getNumImages() const { return 1; }
86 /** Sets the texture width, height, and depth. If width, height, or
87 * depth are zero, calculate the respective value from the source
89 inline void setTextureSize(int width, int height, int depth) const
91 _textureWidth = width;
92 _textureHeight = height;
93 _textureDepth = depth;
96 /** Gets the texture subload width. */
97 inline void getTextureSize(int& width, int& height, int& depth) const
99 width = _textureWidth;
100 height = _textureHeight;
101 depth = _textureDepth;
104 void setTextureWidth(int width) { _textureWidth=width; }
105 void setTextureHeight(int height) { _textureHeight=height; }
106 void setTextureDepth(int depth) { _textureDepth=depth; }
108 virtual int getTextureWidth() const { return _textureWidth; }
109 virtual int getTextureHeight() const { return _textureHeight; }
110 virtual int getTextureDepth() const { return _textureDepth; }
113 class OSG_EXPORT SubloadCallback : public Referenced
116 virtual void load(const Texture3D& texture,State& state) const = 0;
117 virtual void subload(const Texture3D& texture,State& state) const = 0;
120 void setSubloadCallback(SubloadCallback* cb) { _subloadCallback = cb;; }
122 SubloadCallback* getSubloadCallback() { return _subloadCallback.get(); }
124 const SubloadCallback* getSubloadCallback() const { return _subloadCallback.get(); }
127 /** Helper function. Sets the number of mipmap levels created for this
128 * texture. Should only be called within an osg::Texture::apply(), or
129 * during a custom OpenGL texture load. */
130 void setNumMipmapLevels(unsigned int num) const { _numMipmapLevels=num; }
132 /** Gets the number of mipmap levels created. */
133 unsigned int getNumMipmapLevels() const { return _numMipmapLevels; }
136 /** Copies a two-dimensional texture subimage, as per
137 * glCopyTexSubImage3D. Updates a portion of an existing OpenGL
138 * texture object from the current OpenGL background framebuffer
139 * contents at position \a x, \a y with width \a width and height
140 * \a height. Loads framebuffer data into the texture using offsets
141 * \a xoffset, \a yoffset, and \a zoffset. \a width and \a height
142 * must be powers of two. */
143 void copyTexSubImage3D(State& state, int xoffset, int yoffset, int zoffset, int x, int y, int width, int height);
146 /** Bind the texture object. If the texture object hasn't already been
147 * compiled, create the texture mipmap levels. */
148 virtual void apply(State& state) const;
152 virtual ~Texture3D();
154 void computeRequiredTextureDimensions(State& state, const osg::Image& image,GLsizei& width, GLsizei& height,GLsizei& depth, GLsizei& numMipmapLevels) const;
156 virtual void computeInternalFormat() const;
157 void allocateMipmap(State& state) const;
159 void applyTexImage3D(GLenum target, Image* image, State& state, GLsizei& inwidth, GLsizei& inheight, GLsizei& indepth, GLsizei& numMipmapLevels) const;
161 /** It's not ideal that _image is mutable, but it's required since
162 * Image::ensureDimensionsArePowerOfTwo() can only be called in a
163 * valid OpenGL context, and therefore within Texture::apply, which
165 mutable ref_ptr<Image> _image;
167 /** Subloaded images can have different texture and image sizes. */
168 mutable GLsizei _textureWidth, _textureHeight, _textureDepth;
170 /** Number of mip map levels the texture has been created with, */
171 mutable GLsizei _numMipmapLevels;
173 ref_ptr<SubloadCallback> _subloadCallback;
175 typedef buffered_value<unsigned int> ImageModifiedCount;
176 mutable ImageModifiedCount _modifiedCount;