1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
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.
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.
14#ifndef OSG_TEXTURERECTANGLE
15#define OSG_TEXTURERECTANGLE 1
19#ifndef GL_TEXTURE_RECTANGLE_NV
20#define GL_TEXTURE_RECTANGLE_NV 0x84F5
23#ifndef GL_TEXTURE_RECTANGLE
24#define GL_TEXTURE_RECTANGLE GL_TEXTURE_RECTANGLE_NV
29/** Texture state class which encapsulates OpenGL texture functionality. */
30class OSG_EXPORT TextureRectangle : public Texture
37 TextureRectangle(Image* image);
39 template<class T> TextureRectangle(const osg::ref_ptr<T>& image):
43 setWrap(WRAP_S, CLAMP);
44 setWrap(WRAP_T, CLAMP);
46 setFilter(MIN_FILTER, LINEAR);
47 setFilter(MAG_FILTER, LINEAR);
49 setImage(image.get());
52 /** Copy constructor using CopyOp to manage deep vs shallow copy. */
53 TextureRectangle(const TextureRectangle& text,const CopyOp& copyop=CopyOp::SHALLOW_COPY);
55 META_StateAttribute(osg, TextureRectangle, TEXTURE);
57 /** Return -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs. */
58 virtual int compare(const StateAttribute& rhs) const;
60 virtual GLenum getTextureTarget() const { return GL_TEXTURE_RECTANGLE; }
62 /** Set the texture image. */
63 void setImage(Image* image);
65 template<class T> void setImage(const ref_ptr<T>& image) { setImage(image.get()); }
67 /** Get the texture image. */
68 Image* getImage() { return _image.get(); }
70 /** Get the const texture image. */
71 inline const Image* getImage() const { return _image.get(); }
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]); }
76 inline unsigned int& getModifiedCount(unsigned int contextID) const
78 // get the modified count for the current contextID.
79 return _modifiedCount[contextID];
83 /** Set the texture image, ignoring face value as there is only one image. */
84 virtual void setImage(unsigned int, Image* image) { setImage(image); }
86 /** Get the texture image, ignoring face value as there is only one image. */
87 virtual Image* getImage(unsigned int) { return _image.get(); }
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(); }
92 /** Get the number of images that can be assigned to the Texture. */
93 virtual unsigned int getNumImages() const { return 1; }
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.
99 inline void setTextureSize(int width, int height) const
101 _textureWidth = width;
102 _textureHeight = height;
105 void setTextureWidth(int width) { _textureWidth=width; }
106 void setTextureHeight(int height) { _textureHeight=height; }
108 virtual int getTextureWidth() const { return _textureWidth; }
109 virtual int getTextureHeight() const { return _textureHeight; }
110 virtual int getTextureDepth() const { return 1; }
112 class SubloadCallback : public Referenced
115 virtual void load(const TextureRectangle&, State&) const = 0;
116 virtual void subload(const TextureRectangle&, State&) const = 0;
119 void setSubloadCallback(SubloadCallback* cb) { _subloadCallback = cb;; }
120 SubloadCallback* getSubloadCallback() { return _subloadCallback.get(); }
121 const SubloadCallback* getSubloadCallback() const { return _subloadCallback.get(); }
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 );
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
136 void copyTexSubImage2D(State& state, int xoffset, int yoffset, int x, int y, int width, int height );
138 /** On first apply (unless already compiled), create and bind the
139 * texture, subsequent apply will simply bind to texture.
141 virtual void apply(State& state) const;
145 virtual ~TextureRectangle();
147 virtual void computeInternalFormat() const;
148 void allocateMipmap(State& state) const;
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;
153 ref_ptr<Image> _image;
155 // subloaded images can have different texture and image sizes.
156 mutable GLsizei _textureWidth, _textureHeight;
158 ref_ptr<SubloadCallback> _subloadCallback;
160 typedef buffered_value<unsigned int> ImageModifiedCount;
161 mutable ImageModifiedCount _modifiedCount;