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_TEXTURECUBEMAP
15#define OSG_TEXTURECUBEMAP 1
22/** TextureCubeMap state class which encapsulates OpenGL texture cubemap functionality. */
23class OSG_EXPORT TextureCubeMap : public Texture
30 /** Copy constructor using CopyOp to manage deep vs shallow copy. */
31 TextureCubeMap(const TextureCubeMap& cm,const CopyOp& copyop=CopyOp::SHALLOW_COPY);
33 META_StateAttribute(osg, TextureCubeMap,TEXTURE);
35 /** Return -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs. */
36 virtual int compare(const StateAttribute& rhs) const;
38 virtual GLenum getTextureTarget() const { return GL_TEXTURE_CUBE_MAP; }
49 /** Set the texture image for specified face. */
50 virtual void setImage(unsigned int face, Image* image);
52 template<class T> void setImage(unsigned int face, const ref_ptr<T>& image) { setImage(face, image.get()); }
54 /** Get the texture image for specified face. */
55 virtual Image* getImage(unsigned int face);
57 /** Get the const texture image for specified face. */
58 virtual const Image* getImage(unsigned int face) const;
60 /** Get the number of images that can be assigned to the Texture. */
61 virtual unsigned int getNumImages() const { return 6; }
64 /** return true if the texture image data has been modified and the associated GL texture object needs to be updated.*/
65 virtual bool isDirty(unsigned int contextID) const
67 return (_images[0].valid() && _images[0]->getModifiedCount()!=_modifiedCount[0][contextID]) ||
68 (_images[1].valid() && _images[1]->getModifiedCount()!=_modifiedCount[1][contextID]) ||
69 (_images[2].valid() && _images[2]->getModifiedCount()!=_modifiedCount[2][contextID]) ||
70 (_images[3].valid() && _images[3]->getModifiedCount()!=_modifiedCount[3][contextID]) ||
71 (_images[4].valid() && _images[4]->getModifiedCount()!=_modifiedCount[4][contextID]) ||
72 (_images[5].valid() && _images[5]->getModifiedCount()!=_modifiedCount[5][contextID]);
75 inline unsigned int& getModifiedCount(unsigned int face,unsigned int contextID) const
77 // get the modified count for the current contextID.
78 return _modifiedCount[face][contextID];
81 /** Set the texture width and height. If width or height are zero then
82 * the respective size value is calculated from the source image sizes.
84 inline void setTextureSize(int width, int height) const
86 _textureWidth = width;
87 _textureHeight = height;
90 void setTextureWidth(int width) { _textureWidth=width; }
91 void setTextureHeight(int height) { _textureHeight=height; }
93 virtual int getTextureWidth() const { return _textureWidth; }
94 virtual int getTextureHeight() const { return _textureHeight; }
95 virtual int getTextureDepth() const { return 1; }
97 class OSG_EXPORT SubloadCallback : public Referenced
100 virtual void load(const TextureCubeMap& texture,State& state) const = 0;
101 virtual void subload(const TextureCubeMap& texture,State& state) const = 0;
104 void setSubloadCallback(SubloadCallback* cb) { _subloadCallback = cb;; }
106 SubloadCallback* getSubloadCallback() { return _subloadCallback.get(); }
108 const SubloadCallback* getSubloadCallback() const { return _subloadCallback.get(); }
111 /** Set the number of mip map levels the texture has been created with.
112 * Should only be called within an osg::Texuture::apply() and custom OpenGL texture load.
114 void setNumMipmapLevels(unsigned int num) const { _numMipmapLevels=num; }
116 /** Get the number of mip map levels the texture has been created with. */
117 unsigned int getNumMipmapLevels() const { return _numMipmapLevels; }
119 /** Copies a two-dimensional texture subimage, as per
120 * glCopyTexSubImage2D. Updates a portion of an existing OpenGL
121 * texture object from the current OpenGL background framebuffer
122 * contents at position \a x, \a y with width \a width and height
123 * \a height. Loads framebuffer data into the texture using offsets
124 * \a xoffset and \a yoffset. \a width and \a height must be powers
126 void copyTexSubImageCubeMap(State& state, int face, int xoffset, int yoffset, int x, int y, int width, int height );
129 /** On first apply (unless already compiled), create the mipmapped
130 * texture and bind it. Subsequent apply will simple bind to texture.
132 virtual void apply(State& state) const;
136 virtual ~TextureCubeMap();
138 bool imagesValid() const;
140 virtual void computeInternalFormat() const;
141 void allocateMipmap(State& state) const;
143 ref_ptr<Image> _images[6];
145 // subloaded images can have different texture and image sizes.
146 mutable GLsizei _textureWidth, _textureHeight;
148 // number of mip map levels the texture has been created with,
149 mutable GLsizei _numMipmapLevels;
151 ref_ptr<SubloadCallback> _subloadCallback;
153 typedef buffered_value<unsigned int> ImageModifiedCount;
154 mutable ImageModifiedCount _modifiedCount[6];