openscenegraph
Texture3D
Go to the documentation of this file.
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
14#ifndef OSG_TEXTURE3D
15#define OSG_TEXTURE3D 1
16
17#include <osg/Texture>
18
19namespace osg {
20
21/** Encapsulates OpenGL 3D texture functionality. Doesn't support cube maps,
22 * so ignore \a face parameters.
23*/
24class OSG_EXPORT Texture3D : public Texture
25{
26
27 public :
28
29 Texture3D();
30
31 Texture3D(Image* image);
32
33
34 template<class T> Texture3D(const osg::ref_ptr<T>& image):
35 _textureWidth(0),
36 _textureHeight(0),
37 _textureDepth(0),
38 _numMipmapLevels(0)
39 {
40 setImage(image.get());
41 }
42
43 /** Copy constructor using CopyOp to manage deep vs shallow copy. */
44 Texture3D(const Texture3D& text,const CopyOp& copyop=CopyOp::SHALLOW_COPY);
45
46 META_StateAttribute(osg, Texture3D,TEXTURE);
47
48 /** Return -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs. */
49 virtual int compare(const StateAttribute& rhs) const;
50
51 virtual GLenum getTextureTarget() const { return GL_TEXTURE_3D; }
52
53 /** Sets the texture image. */
54 void setImage(Image* image);
55
56 template<class T> void setImage(const ref_ptr<T>& image) { setImage(image.get()); }
57
58 /** Gets the texture image. */
59 Image* getImage() { return _image.get(); }
60
61 /** Gets the const texture image. */
62 inline const Image* getImage() const { return _image.get(); }
63
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]); }
66
67 inline unsigned int& getModifiedCount(unsigned int contextID) const
68 {
69 // get the modified count for the current contextID.
70 return _modifiedCount[contextID];
71 }
72
73 /** Sets the texture image, ignoring face. */
74 virtual void setImage(unsigned int, Image* image) { setImage(image); }
75
76 /** Gets the texture image, ignoring face. */
77 virtual Image* getImage(unsigned int) { return _image.get(); }
78
79 /** Gets the const texture image, ignoring face. */
80 virtual const Image* getImage(unsigned int) const { return _image.get(); }
81
82 /** Gets the number of images that can be assigned to the Texture. */
83 virtual unsigned int getNumImages() const { return 1; }
84
85
86 /** Sets the texture width, height, and depth. If width, height, or
87 * depth are zero, calculate the respective value from the source
88 * image size. */
89 inline void setTextureSize(int width, int height, int depth) const
90 {
91 _textureWidth = width;
92 _textureHeight = height;
93 _textureDepth = depth;
94 }
95
96 /** Gets the texture subload width. */
97 inline void getTextureSize(int& width, int& height, int& depth) const
98 {
99 width = _textureWidth;
100 height = _textureHeight;
101 depth = _textureDepth;
102 }
103
104 void setTextureWidth(int width) { _textureWidth=width; }
105 void setTextureHeight(int height) { _textureHeight=height; }
106 void setTextureDepth(int depth) { _textureDepth=depth; }
107
108 virtual int getTextureWidth() const { return _textureWidth; }
109 virtual int getTextureHeight() const { return _textureHeight; }
110 virtual int getTextureDepth() const { return _textureDepth; }
111
112
113 class OSG_EXPORT SubloadCallback : public Referenced
114 {
115 public:
116 virtual void load(const Texture3D& texture,State& state) const = 0;
117 virtual void subload(const Texture3D& texture,State& state) const = 0;
118 };
119
120 void setSubloadCallback(SubloadCallback* cb) { _subloadCallback = cb;; }
121
122 SubloadCallback* getSubloadCallback() { return _subloadCallback.get(); }
123
124 const SubloadCallback* getSubloadCallback() const { return _subloadCallback.get(); }
125
126
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; }
131
132 /** Gets the number of mipmap levels created. */
133 unsigned int getNumMipmapLevels() const { return _numMipmapLevels; }
134
135
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);
144
145
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;
149
150 protected :
151
152 virtual ~Texture3D();
153
154 void computeRequiredTextureDimensions(State& state, const osg::Image& image,GLsizei& width, GLsizei& height,GLsizei& depth, GLsizei& numMipmapLevels) const;
155
156 virtual void computeInternalFormat() const;
157 void allocateMipmap(State& state) const;
158
159 void applyTexImage3D(GLenum target, Image* image, State& state, GLsizei& inwidth, GLsizei& inheight, GLsizei& indepth, GLsizei& numMipmapLevels) const;
160
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
164 * is const. */
165 mutable ref_ptr<Image> _image;
166
167 /** Subloaded images can have different texture and image sizes. */
168 mutable GLsizei _textureWidth, _textureHeight, _textureDepth;
169
170 /** Number of mip map levels the texture has been created with, */
171 mutable GLsizei _numMipmapLevels;
172
173 ref_ptr<SubloadCallback> _subloadCallback;
174
175 typedef buffered_value<unsigned int> ImageModifiedCount;
176 mutable ImageModifiedCount _modifiedCount;
177
178};
179
180}
181
182#endif