openscenegraph
TextureCubeMap
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_TEXTURECUBEMAP
15#define OSG_TEXTURECUBEMAP 1
16
17#include <osg/Texture>
18
19
20namespace osg {
21
22/** TextureCubeMap state class which encapsulates OpenGL texture cubemap functionality. */
23class OSG_EXPORT TextureCubeMap : public Texture
24{
25
26 public :
27
28 TextureCubeMap();
29
30 /** Copy constructor using CopyOp to manage deep vs shallow copy. */
31 TextureCubeMap(const TextureCubeMap& cm,const CopyOp& copyop=CopyOp::SHALLOW_COPY);
32
33 META_StateAttribute(osg, TextureCubeMap,TEXTURE);
34
35 /** Return -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs. */
36 virtual int compare(const StateAttribute& rhs) const;
37
38 virtual GLenum getTextureTarget() const { return GL_TEXTURE_CUBE_MAP; }
39
40 enum Face {
41 POSITIVE_X=0,
42 NEGATIVE_X=1,
43 POSITIVE_Y=2,
44 NEGATIVE_Y=3,
45 POSITIVE_Z=4,
46 NEGATIVE_Z=5
47 };
48
49 /** Set the texture image for specified face. */
50 virtual void setImage(unsigned int face, Image* image);
51
52 template<class T> void setImage(unsigned int face, const ref_ptr<T>& image) { setImage(face, image.get()); }
53
54 /** Get the texture image for specified face. */
55 virtual Image* getImage(unsigned int face);
56
57 /** Get the const texture image for specified face. */
58 virtual const Image* getImage(unsigned int face) const;
59
60 /** Get the number of images that can be assigned to the Texture. */
61 virtual unsigned int getNumImages() const { return 6; }
62
63
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
66 {
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]);
73 }
74
75 inline unsigned int& getModifiedCount(unsigned int face,unsigned int contextID) const
76 {
77 // get the modified count for the current contextID.
78 return _modifiedCount[face][contextID];
79 }
80
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.
83 */
84 inline void setTextureSize(int width, int height) const
85 {
86 _textureWidth = width;
87 _textureHeight = height;
88 }
89
90 void setTextureWidth(int width) { _textureWidth=width; }
91 void setTextureHeight(int height) { _textureHeight=height; }
92
93 virtual int getTextureWidth() const { return _textureWidth; }
94 virtual int getTextureHeight() const { return _textureHeight; }
95 virtual int getTextureDepth() const { return 1; }
96
97 class OSG_EXPORT SubloadCallback : public Referenced
98 {
99 public:
100 virtual void load(const TextureCubeMap& texture,State& state) const = 0;
101 virtual void subload(const TextureCubeMap& texture,State& state) const = 0;
102 };
103
104 void setSubloadCallback(SubloadCallback* cb) { _subloadCallback = cb;; }
105
106 SubloadCallback* getSubloadCallback() { return _subloadCallback.get(); }
107
108 const SubloadCallback* getSubloadCallback() const { return _subloadCallback.get(); }
109
110
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.
113 */
114 void setNumMipmapLevels(unsigned int num) const { _numMipmapLevels=num; }
115
116 /** Get the number of mip map levels the texture has been created with. */
117 unsigned int getNumMipmapLevels() const { return _numMipmapLevels; }
118
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
125 * of two. */
126 void copyTexSubImageCubeMap(State& state, int face, int xoffset, int yoffset, int x, int y, int width, int height );
127
128
129 /** On first apply (unless already compiled), create the mipmapped
130 * texture and bind it. Subsequent apply will simple bind to texture.
131 */
132 virtual void apply(State& state) const;
133
134 protected :
135
136 virtual ~TextureCubeMap();
137
138 bool imagesValid() const;
139
140 virtual void computeInternalFormat() const;
141 void allocateMipmap(State& state) const;
142
143 ref_ptr<Image> _images[6];
144
145 // subloaded images can have different texture and image sizes.
146 mutable GLsizei _textureWidth, _textureHeight;
147
148 // number of mip map levels the texture has been created with,
149 mutable GLsizei _numMipmapLevels;
150
151 ref_ptr<SubloadCallback> _subloadCallback;
152
153 typedef buffered_value<unsigned int> ImageModifiedCount;
154 mutable ImageModifiedCount _modifiedCount[6];
155};
156
157}
158
159#endif