openscenegraph
Texture1D
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// -*-c++-*-
15
16#ifndef OSG_TEXTURE1D
17#define OSG_TEXTURE1D 1
18
19#include <osg/Texture>
20
21#ifndef GL_TEXTURE_1D
22 #define GL_TEXTURE_1D 0x0DE0
23#endif
24
25namespace osg {
26
27/** Encapsulates OpenGL 1D texture functionality. Doesn't support cube maps,
28 * so ignore \a face parameters.
29*/
30class OSG_EXPORT Texture1D : public Texture
31{
32
33 public :
34
35 Texture1D();
36
37 Texture1D(Image* image);
38
39 template<class T> Texture1D(const osg::ref_ptr<T>& image):
40 _textureWidth(0),
41 _numMipmapLevels(0)
42 {
43 setImage(image.get());
44 }
45
46 /** Copy constructor using CopyOp to manage deep vs shallow copy. */
47 Texture1D(const Texture1D& text,const CopyOp& copyop=CopyOp::SHALLOW_COPY);
48
49 META_StateAttribute(osg, Texture1D,TEXTURE);
50
51 /** Return -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs. */
52 virtual int compare(const StateAttribute& rhs) const;
53
54 virtual GLenum getTextureTarget() const { return GL_TEXTURE_1D; }
55
56 /** Sets the texture image. */
57 void setImage(Image* image);
58
59 template<class T> void setImage(const ref_ptr<T>& image) { setImage(image.get()); }
60
61 /** Gets the texture image. */
62 Image* getImage() { return _image.get(); }
63
64 /** Gets the const texture image. */
65 inline const Image* getImage() const { return _image.get(); }
66
67
68 /** return true if the texture image data has been modified and the associated GL texture object needs to be updated.*/
69 virtual bool isDirty(unsigned int contextID) const { return (_image.valid() && _image->getModifiedCount()!=_modifiedCount[contextID]); }
70
71
72 inline unsigned int& getModifiedCount(unsigned int contextID) const
73 {
74 // get the modified count for the current contextID.
75 return _modifiedCount[contextID];
76 }
77
78
79 /** Sets the texture image, ignoring face. */
80 virtual void setImage(unsigned int, Image* image) { setImage(image); }
81
82 /** Gets the texture image, ignoring face. */
83 virtual Image* getImage(unsigned int) { return _image.get(); }
84
85 /** Gets the const texture image, ignoring face. */
86 virtual const Image* getImage(unsigned int) const { return _image.get(); }
87
88 /** Gets the number of images that can be assigned to the Texture. */
89 virtual unsigned int getNumImages() const { return 1; }
90
91
92 /** Sets the texture width. If width is zero, calculate the value
93 * from the source image width. */
94 inline void setTextureWidth(int width) { _textureWidth = width; }
95
96 /** Gets the texture width. */
97 virtual int getTextureWidth() const { return _textureWidth; }
98 virtual int getTextureHeight() const { return 1; }
99 virtual int getTextureDepth() const { return 1; }
100
101
102 class OSG_EXPORT SubloadCallback : public Referenced
103 {
104 public:
105 virtual void load(const Texture1D& texture,State& state) const = 0;
106 virtual void subload(const Texture1D& texture,State& state) const = 0;
107 };
108
109 void setSubloadCallback(SubloadCallback* cb) { _subloadCallback = cb;; }
110
111 SubloadCallback* getSubloadCallback() { return _subloadCallback.get(); }
112
113 const SubloadCallback* getSubloadCallback() const { return _subloadCallback.get(); }
114
115
116 /** Helper function. Sets the number of mipmap levels created for this
117 * texture. Should only be called within an osg::Texture::apply(), or
118 * during a custom OpenGL texture load. */
119 void setNumMipmapLevels(unsigned int num) const { _numMipmapLevels=num; }
120
121 /** Gets the number of mipmap levels created. */
122 unsigned int getNumMipmapLevels() const { return _numMipmapLevels; }
123
124
125 /** Copies pixels into a 1D texture image, as per glCopyTexImage1D.
126 * Creates an OpenGL texture object from the current OpenGL background
127 * framebuffer contents at position \a x, \a y with width \a width.
128 * \a width must be a power of two. */
129 void copyTexImage1D(State& state, int x, int y, int width);
130
131 /** Copies a one-dimensional texture subimage, as per
132 * glCopyTexSubImage1D. Updates a portion of an existing OpenGL
133 * texture object from the current OpenGL background framebuffer
134 * contents at position \a x, \a y with width \a width. */
135 void copyTexSubImage1D(State& state, int xoffset, int x, int y, int width);
136
137
138 /** Bind the texture object. If the texture object hasn't already been
139 * compiled, create the texture mipmap levels. */
140 virtual void apply(State& state) const;
141
142 protected :
143
144 virtual ~Texture1D();
145
146 virtual void computeInternalFormat() const;
147 void allocateMipmap(State& state) const;
148
149 /** Helper method. Create the texture without setting or using a
150 * texture binding. */
151 void applyTexImage1D(GLenum target, Image* image, State& state, GLsizei& width, GLsizei& numMipmapLevels) const;
152
153
154 /** It's not ideal that _image is mutable, but it's required since
155 * Image::ensureDimensionsArePowerOfTwo() can only be called in a
156 * valid OpenGL context, and therefore within Texture::apply, which
157 * is const. */
158 mutable ref_ptr<Image> _image;
159
160 /** Subloaded images can have different texture and image sizes. */
161 mutable GLsizei _textureWidth;
162
163 /** Number of mipmap levels created. */
164 mutable GLsizei _numMipmapLevels;
165
166 ref_ptr<SubloadCallback> _subloadCallback;
167
168 typedef buffered_value<unsigned int> ImageModifiedCount;
169 mutable ImageModifiedCount _modifiedCount;
170
171
172};
173
174}
175
176#endif