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        inline unsigned int& getModifiedCount(unsigned int contextID) const
65        {
66            // get the modified count for the current contextID.
67            return _modifiedCount[contextID];
68        }
69
70        /** Sets the texture image, ignoring face. */
71        virtual void setImage(unsigned int, Image* image) { setImage(image); }
72
73        /** Gets the texture image, ignoring face. */
74        virtual Image* getImage(unsigned int) { return _image.get(); }
75
76        /** Gets the const texture image, ignoring face. */
77        virtual const Image* getImage(unsigned int) const { return _image.get(); }
78
79        /** Gets the number of images that can be assigned to the Texture. */
80        virtual unsigned int getNumImages() const { return 1; }
81
82
83        /** Sets the texture width, height, and depth. If width, height, or
84          * depth are zero, calculate the respective value from the source
85          * image size. */
86        inline void setTextureSize(int width, int height, int depth) const
87        {
88            _textureWidth = width;
89            _textureHeight = height;
90            _textureDepth = depth;
91        }
92
93        /** Gets the texture subload width. */
94        inline void getTextureSize(int& width, int& height, int& depth) const
95        {
96            width = _textureWidth;
97            height = _textureHeight;
98            depth = _textureDepth;
99        }
100
101        void setTextureWidth(int width) { _textureWidth=width; }
102        void setTextureHeight(int height) { _textureHeight=height; }
103        void setTextureDepth(int depth) { _textureDepth=depth; }
104
105        virtual int getTextureWidth() const { return _textureWidth; }
106        virtual int getTextureHeight() const { return _textureHeight; }
107        virtual int getTextureDepth() const { return _textureDepth; }
108
109
110        class OSG_EXPORT SubloadCallback : public Referenced
111        {
112            public:
113                virtual void load(const Texture3D& texture,State& state) const = 0;
114                virtual void subload(const Texture3D& texture,State& state) const = 0;
115        };
116
117        void setSubloadCallback(SubloadCallback* cb) { _subloadCallback = cb;; }
118
119        SubloadCallback* getSubloadCallback() { return _subloadCallback.get(); }
120
121        const SubloadCallback* getSubloadCallback() const { return _subloadCallback.get(); }
122
123
124        /** Helper function. Sets the number of mipmap levels created for this
125          * texture. Should only be called within an osg::Texture::apply(), or
126          * during a custom OpenGL texture load. */
127        void setNumMipmapLevels(unsigned int num) const { _numMipmapLevels=num; }
128
129        /** Gets the number of mipmap levels created. */
130        unsigned int getNumMipmapLevels() const { return _numMipmapLevels; }
131
132
133        /** Copies a two-dimensional texture subimage, as per
134          * glCopyTexSubImage3D. Updates a portion of an existing OpenGL
135          * texture object from the current OpenGL background framebuffer
136          * contents at position \a x, \a y with width \a width and height
137          * \a height. Loads framebuffer data into the texture using offsets
138          * \a xoffset, \a yoffset, and \a zoffset. \a width and \a height
139          * must be powers of two. */
140        void copyTexSubImage3D(State& state, int xoffset, int yoffset, int zoffset, int x, int y, int width, int height);
141
142
143        /** Bind the texture object. If the texture object hasn't already been
144          * compiled, create the texture mipmap levels. */
145        virtual void apply(State& state) const;
146
147    protected :
148
149        virtual ~Texture3D();
150
151        void computeRequiredTextureDimensions(State& state, const osg::Image& image,GLsizei& width, GLsizei& height,GLsizei& depth, GLsizei& numMipmapLevels) const;
152
153        virtual void computeInternalFormat() const;
154        void allocateMipmap(State& state) const;
155
156        void applyTexImage3D(GLenum target, Image* image, State& state, GLsizei& inwidth, GLsizei& inheight, GLsizei& indepth, GLsizei& numMipmapLevels) const;
157
158        /** It's not ideal that _image is mutable, but it's required since
159          * Image::ensureDimensionsArePowerOfTwo() can only be called in a
160          * valid OpenGL context, and therefore within Texture::apply, which
161          * is const. */
162        mutable ref_ptr<Image> _image;
163
164        /** Subloaded images can have different texture and image sizes. */
165        mutable GLsizei _textureWidth, _textureHeight, _textureDepth;
166
167        /** Number of mip map levels the texture has been created with, */
168        mutable GLsizei _numMipmapLevels;
169
170        ref_ptr<SubloadCallback> _subloadCallback;
171
172        typedef buffered_value<unsigned int> ImageModifiedCount;
173        mutable ImageModifiedCount _modifiedCount;
174
175};
176
177}
178
179#endif
180