openscenegraph
Texture2D
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_TEXTURE2D
15#define OSG_TEXTURE2D 1
16
17#include <osg/Texture>
18
19namespace osg {
20
21/** Encapsulates OpenGL 2D texture functionality. Doesn't support cube maps,
22 * so ignore \a face parameters.
23*/
24class OSG_EXPORT Texture2D : public Texture
25{
26
27 public :
28
29 Texture2D();
30
31 Texture2D(Image* image);
32
33 template<class T> Texture2D(const osg::ref_ptr<T>& image):
34 _textureWidth(0),
35 _textureHeight(0),
36 _numMipmapLevels(0)
37 {
38 setUseHardwareMipMapGeneration(true);
39 setImage(image.get());
40 }
41
42 /** Copy constructor using CopyOp to manage deep vs shallow copy. */
43 Texture2D(const Texture2D& text,const CopyOp& copyop=CopyOp::SHALLOW_COPY);
44
45 META_StateAttribute(osg, Texture2D,TEXTURE);
46
47 /** Return -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs. */
48 virtual int compare(const StateAttribute& rhs) const;
49
50 virtual GLenum getTextureTarget() const { return GL_TEXTURE_2D; }
51
52 /** Sets the texture image. */
53 void setImage(Image* image);
54
55 template<class T> void setImage(const ref_ptr<T>& image) { setImage(image.get()); }
56
57 /** Gets the texture image. */
58 Image* getImage() { return _image.get(); }
59
60 /** Gets the const texture image. */
61 inline const Image* getImage() const { return _image.get(); }
62
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]); }
65
66 inline unsigned int& getModifiedCount(unsigned int contextID) const
67 {
68 // get the modified count for the current contextID.
69 return _modifiedCount[contextID];
70 }
71
72
73 /** Sets the texture image, ignoring face. */
74 virtual void setImage(unsigned int, Image* image) { setImage(image); }
75
76 template<class T> void setImage(unsigned int, const ref_ptr<T>& image) { setImage(image.get()); }
77
78 /** Gets the texture image, ignoring face. */
79 virtual Image* getImage(unsigned int) { return _image.get(); }
80
81 /** Gets the const texture image, ignoring face. */
82 virtual const Image* getImage(unsigned int) const { return _image.get(); }
83
84 /** Gets the number of images that can be assigned to the Texture. */
85 virtual unsigned int getNumImages() const { return 1; }
86
87
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
91 {
92 _textureWidth = width;
93 _textureHeight = height;
94 }
95
96 void setTextureWidth(int width) { _textureWidth=width; }
97 void setTextureHeight(int height) { _textureHeight=height; }
98
99 virtual int getTextureWidth() const { return _textureWidth; }
100 virtual int getTextureHeight() const { return _textureHeight; }
101 virtual int getTextureDepth() const { return 1; }
102
103 class OSG_EXPORT SubloadCallback : public Referenced
104 {
105 public:
106
107 virtual bool textureObjectValid(const Texture2D& texture, State& state) const
108 {
109 return texture.textureObjectValid(state);
110 }
111
112 virtual osg::ref_ptr<TextureObject> generateTextureObject(const Texture2D& texture, State& state) const
113 {
114 return osg::Texture::generateTextureObject(&texture, state.getContextID(), GL_TEXTURE_2D);
115 }
116
117 virtual void load(const Texture2D& texture,State& state) const = 0;
118 virtual void subload(const Texture2D& texture,State& state) const = 0;
119 };
120
121 void setSubloadCallback(SubloadCallback* cb) { _subloadCallback = cb;; }
122
123 SubloadCallback* getSubloadCallback() { return _subloadCallback.get(); }
124
125 const SubloadCallback* getSubloadCallback() const { return _subloadCallback.get(); }
126
127
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; }
132
133 /** Gets the number of mipmap levels created. */
134 unsigned int getNumMipmapLevels() const { return _numMipmapLevels; }
135
136
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 );
142
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
149 * of two. */
150 void copyTexSubImage2D(State& state, int xoffset, int yoffset, int x, int y, int width, int height );
151
152
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;
156
157
158
159 protected :
160
161 virtual ~Texture2D();
162
163 virtual void computeInternalFormat() const;
164 void allocateMipmap(State& state) const;
165
166 /** Return true of the TextureObject assigned to the context associate with osg::State object is valid.*/
167 bool textureObjectValid(State& state) const;
168
169 friend class SubloadCallback;
170
171 ref_ptr<Image> _image;
172
173 /** Subloaded images can have different texture and image sizes. */
174 mutable GLsizei _textureWidth, _textureHeight;
175
176 /** Number of mipmap levels created. */
177 mutable GLsizei _numMipmapLevels;
178
179 ref_ptr<SubloadCallback> _subloadCallback;
180
181 typedef buffered_value<unsigned int> ImageModifiedCount;
182 mutable ImageModifiedCount _modifiedCount;
183
184};
185
186}
187
188#endif