Texture2D 7.0 KB

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