openscenegraph
TextureRectangle
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_TEXTURERECTANGLE
15#define OSG_TEXTURERECTANGLE 1
16
17#include <osg/Texture>
18
19#ifndef GL_TEXTURE_RECTANGLE_NV
20#define GL_TEXTURE_RECTANGLE_NV 0x84F5
21#endif
22
23#ifndef GL_TEXTURE_RECTANGLE
24#define GL_TEXTURE_RECTANGLE GL_TEXTURE_RECTANGLE_NV
25#endif
26
27namespace osg {
28
29/** Texture state class which encapsulates OpenGL texture functionality. */
30class OSG_EXPORT TextureRectangle : public Texture
31{
32
33 public :
34
35 TextureRectangle();
36
37 TextureRectangle(Image* image);
38
39 template<class T> TextureRectangle(const osg::ref_ptr<T>& image):
40 _textureWidth(0),
41 _textureHeight(0)
42 {
43 setWrap(WRAP_S, CLAMP);
44 setWrap(WRAP_T, CLAMP);
45
46 setFilter(MIN_FILTER, LINEAR);
47 setFilter(MAG_FILTER, LINEAR);
48
49 setImage(image.get());
50 }
51
52 /** Copy constructor using CopyOp to manage deep vs shallow copy. */
53 TextureRectangle(const TextureRectangle& text,const CopyOp& copyop=CopyOp::SHALLOW_COPY);
54
55 META_StateAttribute(osg, TextureRectangle, TEXTURE);
56
57 /** Return -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs. */
58 virtual int compare(const StateAttribute& rhs) const;
59
60 virtual GLenum getTextureTarget() const { return GL_TEXTURE_RECTANGLE; }
61
62 /** Set the texture image. */
63 void setImage(Image* image);
64
65 template<class T> void setImage(const ref_ptr<T>& image) { setImage(image.get()); }
66
67 /** Get the texture image. */
68 Image* getImage() { return _image.get(); }
69
70 /** Get the const texture image. */
71 inline const Image* getImage() const { return _image.get(); }
72
73 /** return true if the texture image data has been modified and the associated GL texture object needs to be updated.*/
74 virtual bool isDirty(unsigned int contextID) const { return (_image.valid() && _image->getModifiedCount()!=_modifiedCount[contextID]); }
75
76 inline unsigned int& getModifiedCount(unsigned int contextID) const
77 {
78 // get the modified count for the current contextID.
79 return _modifiedCount[contextID];
80 }
81
82
83 /** Set the texture image, ignoring face value as there is only one image. */
84 virtual void setImage(unsigned int, Image* image) { setImage(image); }
85
86 /** Get the texture image, ignoring face value as there is only one image. */
87 virtual Image* getImage(unsigned int) { return _image.get(); }
88
89 /** Get the const texture image, ignoring face value as there is only one image. */
90 virtual const Image* getImage(unsigned int) const { return _image.get(); }
91
92 /** Get the number of images that can be assigned to the Texture. */
93 virtual unsigned int getNumImages() const { return 1; }
94
95
96 /** Set the texture width and height. If width or height are zero then
97 * the respective size value is calculated from the source image sizes.
98 */
99 inline void setTextureSize(int width, int height) const
100 {
101 _textureWidth = width;
102 _textureHeight = height;
103 }
104
105 void setTextureWidth(int width) { _textureWidth=width; }
106 void setTextureHeight(int height) { _textureHeight=height; }
107
108 virtual int getTextureWidth() const { return _textureWidth; }
109 virtual int getTextureHeight() const { return _textureHeight; }
110 virtual int getTextureDepth() const { return 1; }
111
112 class SubloadCallback : public Referenced
113 {
114 public:
115 virtual void load(const TextureRectangle&, State&) const = 0;
116 virtual void subload(const TextureRectangle&, State&) const = 0;
117 };
118
119 void setSubloadCallback(SubloadCallback* cb) { _subloadCallback = cb;; }
120 SubloadCallback* getSubloadCallback() { return _subloadCallback.get(); }
121 const SubloadCallback* getSubloadCallback() const { return _subloadCallback.get(); }
122
123 /** Copies pixels into a 2D texture image, as per glCopyTexImage2D.
124 * Creates an OpenGL texture object from the current OpenGL background
125 * framebuffer contents at position \a x, \a y with width \a width and
126 * height \a height. \a width and \a height must be a power of two. */
127 void copyTexImage2D(State& state, int x, int y, int width, int height );
128
129 /** Copies a two-dimensional texture subimage, as per
130 * glCopyTexSubImage2D. Updates a portion of an existing OpenGL
131 * texture object from the current OpenGL background framebuffer
132 * contents at position \a x, \a y with width \a width and height
133 * \a height. Loads framebuffer data into the texture using offsets
134 * \a xoffset and \a yoffset. \a width and \a height must be powers
135 * of two. */
136 void copyTexSubImage2D(State& state, int xoffset, int yoffset, int x, int y, int width, int height );
137
138 /** On first apply (unless already compiled), create and bind the
139 * texture, subsequent apply will simply bind to texture.
140 */
141 virtual void apply(State& state) const;
142
143 protected :
144
145 virtual ~TextureRectangle();
146
147 virtual void computeInternalFormat() const;
148 void allocateMipmap(State& state) const;
149
150 void applyTexImage_load(GLenum target, Image* image, State& state, GLsizei& inwidth, GLsizei& inheight) const;
151 void applyTexImage_subload(GLenum target, Image* image, State& state, GLsizei& inwidth, GLsizei& inheight, GLint& inInternalFormat) const;
152
153 ref_ptr<Image> _image;
154
155 // subloaded images can have different texture and image sizes.
156 mutable GLsizei _textureWidth, _textureHeight;
157
158 ref_ptr<SubloadCallback> _subloadCallback;
159
160 typedef buffered_value<unsigned int> ImageModifiedCount;
161 mutable ImageModifiedCount _modifiedCount;
162};
163
164}
165
166#endif