openscenegraph
Texture2DArray
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_TEXTURE2DARRAY
15#define OSG_TEXTURE2DARRAY 1
16
17#include <osg/Texture>
18#include <list>
19
20namespace osg {
21
22/** Texture2DArray state class which encapsulates OpenGL 2D array texture functionality.
23 * Texture arrays were introduced with Shader Model 4.0 hardware.
24 *
25 * A 2D texture array does contain textures sharing the same properties (e.g. size, bitdepth,...)
26 * in a layered structure. See http://www.opengl.org/registry/specs/EXT/texture_array.txt for more info.
27 */
28class OSG_EXPORT Texture2DArray : public Texture
29{
30
31 public :
32
33 Texture2DArray();
34
35 /** Copy constructor using CopyOp to manage deep vs shallow copy. */
36 Texture2DArray(const Texture2DArray& cm,const CopyOp& copyop=CopyOp::SHALLOW_COPY);
37
38 META_StateAttribute(osg, Texture2DArray, TEXTURE);
39
40 /** Return -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs. */
41 virtual int compare(const StateAttribute& rhs) const;
42
43 virtual GLenum getTextureTarget() const { return GL_TEXTURE_2D_ARRAY; }
44
45 /** Texture2DArray is related to non fixed pipeline usage only so isn't appropriate to enable/disable.*/
46 virtual bool getModeUsage(StateAttribute::ModeUsage&) const { return false; }
47
48 /** Set the texture image for specified layer. */
49 virtual void setImage(unsigned int layer, Image* image);
50
51 template<class T> void setImage(unsigned int layer, const ref_ptr<T>& image) { setImage(layer, image.get()); }
52
53 /** Get the texture image for specified layer. */
54 virtual Image* getImage(unsigned int layer);
55
56 /** Get the const texture image for specified layer. */
57 virtual const Image* getImage(unsigned int layer) const;
58
59 /** Get the number of images that are assigned to the Texture.
60 * The number is equal to the texture depth. To get the maximum possible
61 * image/layer count, you have to use the extension subclass, since it provides
62 * graphic context dependent information.
63 */
64 virtual unsigned int getNumImages() const { return _images.size(); }
65
66 /** return true if the texture image data has been modified and the associated GL texture object needs to be updated.*/
67 virtual bool isDirty(unsigned int contextID) const
68 {
69 for(unsigned int i=0; i<_images.size(); ++i)
70 {
71 if (_images[i].valid() && _images[i]->getModifiedCount()!=_modifiedCount[i][contextID]) return true;
72 }
73 return false;
74 }
75
76 /** Check how often was a certain layer in the given context modified */
77 inline unsigned int& getModifiedCount(unsigned int layer, unsigned int contextID) const
78 {
79 // get the modified count for the current contextID.
80 return _modifiedCount[layer][contextID];
81 }
82
83 /** Set the texture width and height. If width or height are zero then
84 * the respective size value is calculated from the source image sizes.
85 * Depth parameter specifies the number of layers to be used.
86 */
87 void setTextureSize(int width, int height, int depth);
88
89 void setTextureWidth(int width) { _textureWidth=width; }
90 void setTextureHeight(int height) { _textureHeight=height; }
91 void setTextureDepth(int depth);
92
93 virtual int getTextureWidth() const { return _textureWidth; }
94 virtual int getTextureHeight() const { return _textureHeight; }
95 virtual int getTextureDepth() const { return _textureDepth; }
96
97 class OSG_EXPORT SubloadCallback : public Referenced
98 {
99 public:
100 virtual void load(const Texture2DArray& texture,State& state) const = 0;
101 virtual void subload(const Texture2DArray& texture,State& state) const = 0;
102 };
103
104
105 void setSubloadCallback(SubloadCallback* cb) { _subloadCallback = cb;; }
106
107 SubloadCallback* getSubloadCallback() { return _subloadCallback.get(); }
108
109 const SubloadCallback* getSubloadCallback() const { return _subloadCallback.get(); }
110
111
112
113 /** Set the number of mip map levels the texture has been created with.
114 * Should only be called within an osg::Texture::apply() and custom OpenGL texture load.
115 */
116 void setNumMipmapLevels(unsigned int num) const { _numMipmapLevels=num; }
117
118 /** Get the number of mip map levels the texture has been created with. */
119 unsigned int getNumMipmapLevels() const { return _numMipmapLevels; }
120
121 /** Copies a two-dimensional texture subimage, as per
122 * glCopyTexSubImage3D. Updates a portion of an existing OpenGL
123 * texture object from the current OpenGL background framebuffer
124 * contents at position \a x, \a y with width \a width and height
125 * \a height. Loads framebuffer data into the texture using offsets
126 * \a xoffset and \a yoffset. \a zoffset specifies the layer of the texture
127 * array to which the result is copied.
128 */
129 void copyTexSubImage2DArray(State& state, int xoffset, int yoffset, int zoffset, int x, int y, int width, int height );
130
131 /** Bind the texture if already compiled. Otherwise recompile.
132 */
133 virtual void apply(State& state) const;
134
135 protected :
136
137 virtual ~Texture2DArray();
138
139 bool imagesValid() const;
140
141 virtual void computeInternalFormat() const;
142 GLsizei computeTextureDepth() const;
143 void allocateMipmap(State& state) const;
144
145 void applyTexImage2DArray_subload(State& state, Image* image, GLsizei layer, GLsizei inwidth, GLsizei inheight, GLsizei indepth, GLint inInternalFormat, GLsizei& numMipmapLevels) const;
146
147 typedef std::vector< ref_ptr<Image> > Images;
148 Images _images;
149
150 // subloaded images can have different texture and image sizes.
151 mutable GLsizei _textureWidth, _textureHeight, _textureDepth;
152
153 // number of mip map levels the texture has been created with,
154 mutable GLsizei _numMipmapLevels;
155
156 ref_ptr<SubloadCallback> _subloadCallback;
157
158 typedef buffered_value<unsigned int> ImageModifiedCount;
159 mutable std::vector<ImageModifiedCount> _modifiedCount;
160};
161
162}
163
164#endif