2003-01-22 00:45:36 +08:00
|
|
|
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2003 Robert Osfield
|
|
|
|
*
|
|
|
|
* This library is open source and may be redistributed and/or modified under
|
|
|
|
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
|
|
|
|
* (at your option) any later version. The full license is in LICENSE file
|
|
|
|
* included with this distribution, and on the openscenegraph.org website.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* OpenSceneGraph Public License for more details.
|
|
|
|
*/
|
2002-08-25 03:39:39 +08:00
|
|
|
|
|
|
|
#ifndef OSG_TEXTURE2D
|
|
|
|
#define OSG_TEXTURE2D 1
|
|
|
|
|
2002-08-28 23:28:11 +08:00
|
|
|
#include <osg/Texture>
|
2002-08-25 03:39:39 +08:00
|
|
|
|
|
|
|
namespace osg {
|
|
|
|
|
2004-09-14 00:10:59 +08:00
|
|
|
/** Encapsulates OpenGl 2D texture functionality. Doesn't support cube maps,
|
|
|
|
* so ignore \a face parameters.
|
|
|
|
*/
|
2002-08-28 23:28:11 +08:00
|
|
|
class SG_EXPORT Texture2D : public Texture
|
2002-08-25 03:39:39 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
public :
|
|
|
|
|
|
|
|
Texture2D();
|
|
|
|
|
2004-03-09 22:59:33 +08:00
|
|
|
Texture2D(Image* image);
|
2003-06-25 05:57:13 +08:00
|
|
|
|
2004-09-14 00:10:59 +08:00
|
|
|
/** Copy constructor using CopyOp to manage deep vs shallow copy. */
|
2002-08-25 03:39:39 +08:00
|
|
|
Texture2D(const Texture2D& text,const CopyOp& copyop=CopyOp::SHALLOW_COPY);
|
|
|
|
|
|
|
|
META_StateAttribute(osg, Texture2D,TEXTURE);
|
|
|
|
|
2004-09-14 00:10:59 +08:00
|
|
|
/** Return -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs. */
|
2002-08-25 03:39:39 +08:00
|
|
|
virtual int compare(const StateAttribute& rhs) const;
|
|
|
|
|
2004-03-03 23:38:22 +08:00
|
|
|
virtual bool getModeUsage(ModeUsage& usage) const
|
2002-08-25 03:39:39 +08:00
|
|
|
{
|
2004-03-03 23:38:22 +08:00
|
|
|
usage.usesTextureMode(GL_TEXTURE_2D);
|
|
|
|
return true;
|
2002-08-25 03:39:39 +08:00
|
|
|
}
|
|
|
|
|
2004-09-14 00:10:59 +08:00
|
|
|
/** Sets the texture image. */
|
2002-08-25 03:39:39 +08:00
|
|
|
void setImage(Image* image);
|
|
|
|
|
2004-09-14 00:10:59 +08:00
|
|
|
/** Gets the texture image. */
|
2002-08-25 03:39:39 +08:00
|
|
|
Image* getImage() { return _image.get(); }
|
|
|
|
|
2004-09-14 00:10:59 +08:00
|
|
|
/** Gets the const texture image. */
|
2002-08-25 03:39:39 +08:00
|
|
|
inline const Image* getImage() const { return _image.get(); }
|
|
|
|
|
2005-02-09 18:39:45 +08:00
|
|
|
inline unsigned int& getModifiedCount(unsigned int contextID) const
|
2003-04-07 17:46:06 +08:00
|
|
|
{
|
2005-02-09 18:39:45 +08:00
|
|
|
// get the modified count for the current contextID.
|
|
|
|
return _modifiedCount[contextID];
|
2003-04-07 17:46:06 +08:00
|
|
|
}
|
2002-08-25 03:39:39 +08:00
|
|
|
|
2004-07-27 18:11:45 +08:00
|
|
|
|
2004-09-14 00:10:59 +08:00
|
|
|
/** Sets the texture image, ignoring face. */
|
2004-07-27 18:11:45 +08:00
|
|
|
virtual void setImage(unsigned int, Image* image) { setImage(image); }
|
|
|
|
|
2004-09-14 00:10:59 +08:00
|
|
|
/** Gets the texture image, ignoring face. */
|
2004-07-27 18:11:45 +08:00
|
|
|
virtual Image* getImage(unsigned int) { return _image.get(); }
|
|
|
|
|
2004-09-14 00:10:59 +08:00
|
|
|
/** Gets the const texture image, ignoring face. */
|
2004-07-27 18:11:45 +08:00
|
|
|
virtual const Image* getImage(unsigned int) const { return _image.get(); }
|
|
|
|
|
2004-09-14 00:10:59 +08:00
|
|
|
/** Gets the number of images that can be assigned to the Texture. */
|
2004-07-27 18:11:45 +08:00
|
|
|
virtual unsigned int getNumImages() const { return 1; }
|
|
|
|
|
|
|
|
|
2004-09-14 00:10:59 +08:00
|
|
|
/** Sets the texture width and height. If width or height are zero,
|
|
|
|
* calculate the respective value from the source image size. */
|
2002-09-02 20:31:35 +08:00
|
|
|
inline void setTextureSize(int width, int height) const
|
2002-08-25 03:39:39 +08:00
|
|
|
{
|
|
|
|
_textureWidth = width;
|
|
|
|
_textureHeight = height;
|
|
|
|
}
|
|
|
|
|
2004-12-13 09:07:24 +08:00
|
|
|
void setTextureWidth(int width) { _textureWidth=width; }
|
2003-03-03 05:05:05 +08:00
|
|
|
int getTextureWidth() const { return _textureWidth; }
|
2004-12-13 09:07:24 +08:00
|
|
|
|
|
|
|
void setTextureHeight(int height) { _textureHeight=height; }
|
2003-03-03 05:05:05 +08:00
|
|
|
int getTextureHeight() const { return _textureHeight; }
|
|
|
|
|
2004-09-14 00:10:59 +08:00
|
|
|
/** Deprecated. */
|
2002-08-25 03:39:39 +08:00
|
|
|
inline void getTextureSize(int& width, int& height) const
|
|
|
|
{
|
|
|
|
width = _textureWidth;
|
|
|
|
height = _textureHeight;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-03-19 03:42:34 +08:00
|
|
|
class SG_EXPORT SubloadCallback : public Referenced
|
2002-08-25 03:39:39 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual void load(const Texture2D& texture,State& state) const = 0;
|
|
|
|
virtual void subload(const Texture2D& texture,State& state) const = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
void setSubloadCallback(SubloadCallback* cb) { _subloadCallback = cb;; }
|
|
|
|
|
|
|
|
SubloadCallback* getSubloadCallback() { return _subloadCallback.get(); }
|
|
|
|
|
|
|
|
const SubloadCallback* getSubloadCallback() const { return _subloadCallback.get(); }
|
|
|
|
|
|
|
|
|
2004-09-14 00:10:59 +08:00
|
|
|
/** Helper function. Sets the number of mipmap levels created for this
|
|
|
|
* texture. Should only be called within an osg::Texuture::apply(), or
|
|
|
|
* during a custom OpenGL texture load. */
|
2003-04-30 19:40:17 +08:00
|
|
|
void setNumMipmapLevels(unsigned int num) const { _numMipmapLevels=num; }
|
2002-08-25 03:39:39 +08:00
|
|
|
|
2004-09-14 00:10:59 +08:00
|
|
|
/** Gets the number of mipmap levels created. */
|
2003-04-30 19:40:17 +08:00
|
|
|
unsigned int getNumMipmapLevels() const { return _numMipmapLevels; }
|
2002-08-25 03:39:39 +08:00
|
|
|
|
|
|
|
|
2004-09-14 00:10:59 +08:00
|
|
|
/** Copies pixels into a 2D texture image, as per glCopyTexImage2D.
|
2002-08-25 03:39:39 +08:00
|
|
|
* Creates an OpenGL texture object from the current OpenGL background
|
2004-09-14 00:10:59 +08:00
|
|
|
* framebuffer contents at position \a x, \a y with width \a width and
|
|
|
|
* height \a height. \a width and \a height must be a power of two. */
|
2002-08-25 03:39:39 +08:00
|
|
|
void copyTexImage2D(State& state, int x, int y, int width, int height );
|
|
|
|
|
2004-09-14 00:10:59 +08:00
|
|
|
/** Copies a two-dimensional texture subimage, as per
|
|
|
|
* glCopyTexSubImage2D. Updates a portion of an existing OpenGL
|
|
|
|
* texture object from the current OpenGL background framebuffer
|
|
|
|
* contents at position \a x, \a y with width \a width and height
|
|
|
|
* \a height. Loads framebuffer data into the texture using offsets
|
|
|
|
* \a xoffset and \a yoffset. \a width and \a height must be powers
|
|
|
|
* of two. */
|
2002-08-25 03:39:39 +08:00
|
|
|
void copyTexSubImage2D(State& state, int xoffset, int yoffset, int x, int y, int width, int height );
|
|
|
|
|
|
|
|
|
2004-09-14 00:10:59 +08:00
|
|
|
/** Bind the texture object. If the texture object hasn't already been
|
|
|
|
* compiled, create the texture mipmap levels. */
|
2002-08-25 03:39:39 +08:00
|
|
|
virtual void apply(State& state) const;
|
|
|
|
|
|
|
|
protected :
|
|
|
|
|
|
|
|
virtual ~Texture2D();
|
|
|
|
|
|
|
|
virtual void computeInternalFormat() const;
|
|
|
|
|
|
|
|
|
2004-09-14 00:10:59 +08:00
|
|
|
/** It's not ideal that _image is mutable, but it's required since
|
|
|
|
* Image::ensureDimensionsArePowerOfTwo() can only be called in a
|
|
|
|
* valid OpenGL context, and therefore within Texture::apply, which
|
|
|
|
* is const. */
|
2003-04-01 19:49:09 +08:00
|
|
|
ref_ptr<Image> _image;
|
2002-08-25 03:39:39 +08:00
|
|
|
|
2004-09-14 00:10:59 +08:00
|
|
|
/** Subloaded images can have different texture and image sizes. */
|
2002-08-25 03:39:39 +08:00
|
|
|
mutable GLsizei _textureWidth, _textureHeight;
|
|
|
|
|
2004-09-14 00:10:59 +08:00
|
|
|
/** Number of mipmap levels created. */
|
2003-04-30 19:40:17 +08:00
|
|
|
mutable GLsizei _numMipmapLevels;
|
2002-08-25 03:39:39 +08:00
|
|
|
|
|
|
|
ref_ptr<SubloadCallback> _subloadCallback;
|
2003-04-07 17:46:06 +08:00
|
|
|
|
2005-02-09 18:39:45 +08:00
|
|
|
typedef buffered_value<unsigned int> ImageModifiedCount;
|
|
|
|
mutable ImageModifiedCount _modifiedCount;
|
2003-04-07 17:46:06 +08:00
|
|
|
|
2002-08-25 03:39:39 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|