Texture3D 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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_TEXTURE3D
  14. #define OSG_TEXTURE3D 1
  15. #include <osg/Texture>
  16. namespace osg {
  17. /** Encapsulates OpenGL 3D texture functionality. Doesn't support cube maps,
  18. * so ignore \a face parameters.
  19. */
  20. class OSG_EXPORT Texture3D : public Texture
  21. {
  22. public :
  23. Texture3D();
  24. Texture3D(Image* image);
  25. template<class T> Texture3D(const osg::ref_ptr<T>& image):
  26. _textureWidth(0),
  27. _textureHeight(0),
  28. _textureDepth(0),
  29. _numMipmapLevels(0)
  30. {
  31. setImage(image.get());
  32. }
  33. /** Copy constructor using CopyOp to manage deep vs shallow copy. */
  34. Texture3D(const Texture3D& text,const CopyOp& copyop=CopyOp::SHALLOW_COPY);
  35. META_StateAttribute(osg, Texture3D,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_3D; }
  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. /** Gets the texture image, ignoring face. */
  56. virtual Image* getImage(unsigned int) { return _image.get(); }
  57. /** Gets the const texture image, ignoring face. */
  58. virtual const Image* getImage(unsigned int) const { return _image.get(); }
  59. /** Gets the number of images that can be assigned to the Texture. */
  60. virtual unsigned int getNumImages() const { return 1; }
  61. /** Sets the texture width, height, and depth. If width, height, or
  62. * depth are zero, calculate the respective value from the source
  63. * image size. */
  64. inline void setTextureSize(int width, int height, int depth) const
  65. {
  66. _textureWidth = width;
  67. _textureHeight = height;
  68. _textureDepth = depth;
  69. }
  70. /** Gets the texture subload width. */
  71. inline void getTextureSize(int& width, int& height, int& depth) const
  72. {
  73. width = _textureWidth;
  74. height = _textureHeight;
  75. depth = _textureDepth;
  76. }
  77. void setTextureWidth(int width) { _textureWidth=width; }
  78. void setTextureHeight(int height) { _textureHeight=height; }
  79. void setTextureDepth(int depth) { _textureDepth=depth; }
  80. virtual int getTextureWidth() const { return _textureWidth; }
  81. virtual int getTextureHeight() const { return _textureHeight; }
  82. virtual int getTextureDepth() const { return _textureDepth; }
  83. class OSG_EXPORT SubloadCallback : public Referenced
  84. {
  85. public:
  86. virtual void load(const Texture3D& texture,State& state) const = 0;
  87. virtual void subload(const Texture3D& texture,State& state) const = 0;
  88. };
  89. void setSubloadCallback(SubloadCallback* cb) { _subloadCallback = cb;; }
  90. SubloadCallback* getSubloadCallback() { return _subloadCallback.get(); }
  91. const SubloadCallback* getSubloadCallback() const { return _subloadCallback.get(); }
  92. /** Helper function. Sets the number of mipmap levels created for this
  93. * texture. Should only be called within an osg::Texture::apply(), or
  94. * during a custom OpenGL texture load. */
  95. void setNumMipmapLevels(unsigned int num) const { _numMipmapLevels=num; }
  96. /** Gets the number of mipmap levels created. */
  97. unsigned int getNumMipmapLevels() const { return _numMipmapLevels; }
  98. /** Copies a two-dimensional texture subimage, as per
  99. * glCopyTexSubImage3D. Updates a portion of an existing OpenGL
  100. * texture object from the current OpenGL background framebuffer
  101. * contents at position \a x, \a y with width \a width and height
  102. * \a height. Loads framebuffer data into the texture using offsets
  103. * \a xoffset, \a yoffset, and \a zoffset. \a width and \a height
  104. * must be powers of two. */
  105. void copyTexSubImage3D(State& state, int xoffset, int yoffset, int zoffset, int x, int y, int width, int height);
  106. /** Bind the texture object. If the texture object hasn't already been
  107. * compiled, create the texture mipmap levels. */
  108. virtual void apply(State& state) const;
  109. protected :
  110. virtual ~Texture3D();
  111. void computeRequiredTextureDimensions(State& state, const osg::Image& image,GLsizei& width, GLsizei& height,GLsizei& depth, GLsizei& numMipmapLevels) const;
  112. virtual void computeInternalFormat() const;
  113. void allocateMipmap(State& state) const;
  114. void applyTexImage3D(GLenum target, Image* image, State& state, GLsizei& inwidth, GLsizei& inheight, GLsizei& indepth, GLsizei& numMipmapLevels) const;
  115. /** It's not ideal that _image is mutable, but it's required since
  116. * Image::ensureDimensionsArePowerOfTwo() can only be called in a
  117. * valid OpenGL context, and therefore within Texture::apply, which
  118. * is const. */
  119. mutable ref_ptr<Image> _image;
  120. /** Subloaded images can have different texture and image sizes. */
  121. mutable GLsizei _textureWidth, _textureHeight, _textureDepth;
  122. /** Number of mip map levels the texture has been created with, */
  123. mutable GLsizei _numMipmapLevels;
  124. ref_ptr<SubloadCallback> _subloadCallback;
  125. typedef buffered_value<unsigned int> ImageModifiedCount;
  126. mutable ImageModifiedCount _modifiedCount;
  127. };
  128. }
  129. #endif