TextureRectangle 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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_TEXTURERECTANGLE
  14. #define OSG_TEXTURERECTANGLE 1
  15. #include <osg/Texture>
  16. #ifndef GL_TEXTURE_RECTANGLE_NV
  17. #define GL_TEXTURE_RECTANGLE_NV 0x84F5
  18. #endif
  19. #ifndef GL_TEXTURE_RECTANGLE
  20. #define GL_TEXTURE_RECTANGLE GL_TEXTURE_RECTANGLE_NV
  21. #endif
  22. namespace osg {
  23. /** Texture state class which encapsulates OpenGL texture functionality. */
  24. class OSG_EXPORT TextureRectangle : public Texture
  25. {
  26. public :
  27. TextureRectangle();
  28. TextureRectangle(Image* image);
  29. template<class T> TextureRectangle(const osg::ref_ptr<T>& image):
  30. _textureWidth(0),
  31. _textureHeight(0)
  32. {
  33. setWrap(WRAP_S, CLAMP);
  34. setWrap(WRAP_T, CLAMP);
  35. setFilter(MIN_FILTER, LINEAR);
  36. setFilter(MAG_FILTER, LINEAR);
  37. setImage(image.get());
  38. }
  39. /** Copy constructor using CopyOp to manage deep vs shallow copy. */
  40. TextureRectangle(const TextureRectangle& text,const CopyOp& copyop=CopyOp::SHALLOW_COPY);
  41. META_StateAttribute(osg, TextureRectangle, TEXTURE);
  42. /** Return -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs. */
  43. virtual int compare(const StateAttribute& rhs) const;
  44. virtual GLenum getTextureTarget() const { return GL_TEXTURE_RECTANGLE; }
  45. /** Set the texture image. */
  46. void setImage(Image* image);
  47. template<class T> void setImage(const ref_ptr<T>& image) { setImage(image.get()); }
  48. /** Get the texture image. */
  49. Image* getImage() { return _image.get(); }
  50. /** Get the const texture image. */
  51. inline const Image* getImage() const { return _image.get(); }
  52. /** return true if the texture image data has been modified and the associated GL texture object needs to be updated.*/
  53. virtual bool isDirty(unsigned int contextID) const { return (_image.valid() && _image->getModifiedCount()!=_modifiedCount[contextID]); }
  54. inline unsigned int& getModifiedCount(unsigned int contextID) const
  55. {
  56. // get the modified count for the current contextID.
  57. return _modifiedCount[contextID];
  58. }
  59. /** Set the texture image, ignoring face value as there is only one image. */
  60. virtual void setImage(unsigned int, Image* image) { setImage(image); }
  61. /** Get the texture image, ignoring face value as there is only one image. */
  62. virtual Image* getImage(unsigned int) { return _image.get(); }
  63. /** Get the const texture image, ignoring face value as there is only one image. */
  64. virtual const Image* getImage(unsigned int) const { return _image.get(); }
  65. /** Get the number of images that can be assigned to the Texture. */
  66. virtual unsigned int getNumImages() const { return 1; }
  67. /** Set the texture width and height. If width or height are zero then
  68. * the respective size value is calculated from the source image sizes.
  69. */
  70. inline void setTextureSize(int width, int height) const
  71. {
  72. _textureWidth = width;
  73. _textureHeight = height;
  74. }
  75. void setTextureWidth(int width) { _textureWidth=width; }
  76. void setTextureHeight(int height) { _textureHeight=height; }
  77. virtual int getTextureWidth() const { return _textureWidth; }
  78. virtual int getTextureHeight() const { return _textureHeight; }
  79. virtual int getTextureDepth() const { return 1; }
  80. class SubloadCallback : public Referenced
  81. {
  82. public:
  83. virtual void load(const TextureRectangle&, State&) const = 0;
  84. virtual void subload(const TextureRectangle&, State&) const = 0;
  85. };
  86. void setSubloadCallback(SubloadCallback* cb) { _subloadCallback = cb;; }
  87. SubloadCallback* getSubloadCallback() { return _subloadCallback.get(); }
  88. const SubloadCallback* getSubloadCallback() const { return _subloadCallback.get(); }
  89. /** Copies pixels into a 2D texture image, as per glCopyTexImage2D.
  90. * Creates an OpenGL texture object from the current OpenGL background
  91. * framebuffer contents at position \a x, \a y with width \a width and
  92. * height \a height. \a width and \a height must be a power of two. */
  93. void copyTexImage2D(State& state, int x, int y, int width, int height );
  94. /** Copies a two-dimensional texture subimage, as per
  95. * glCopyTexSubImage2D. Updates a portion of an existing OpenGL
  96. * texture object from the current OpenGL background framebuffer
  97. * contents at position \a x, \a y with width \a width and height
  98. * \a height. Loads framebuffer data into the texture using offsets
  99. * \a xoffset and \a yoffset. \a width and \a height must be powers
  100. * of two. */
  101. void copyTexSubImage2D(State& state, int xoffset, int yoffset, int x, int y, int width, int height );
  102. /** On first apply (unless already compiled), create and bind the
  103. * texture, subsequent apply will simply bind to texture.
  104. */
  105. virtual void apply(State& state) const;
  106. protected :
  107. virtual ~TextureRectangle();
  108. virtual void computeInternalFormat() const;
  109. void allocateMipmap(State& state) const;
  110. void applyTexImage_load(GLenum target, Image* image, State& state, GLsizei& inwidth, GLsizei& inheight) const;
  111. void applyTexImage_subload(GLenum target, Image* image, State& state, GLsizei& inwidth, GLsizei& inheight, GLint& inInternalFormat) const;
  112. ref_ptr<Image> _image;
  113. // subloaded images can have different texture and image sizes.
  114. mutable GLsizei _textureWidth, _textureHeight;
  115. ref_ptr<SubloadCallback> _subloadCallback;
  116. typedef buffered_value<unsigned int> ImageModifiedCount;
  117. mutable ImageModifiedCount _modifiedCount;
  118. };
  119. }
  120. #endif