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