TextureCubeMap 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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_TEXTURECUBEMAP
  14. #define OSG_TEXTURECUBEMAP 1
  15. #include <osg/Texture>
  16. namespace osg {
  17. /** TextureCubeMap state class which encapsulates OpenGL texture cubemap functionality. */
  18. class OSG_EXPORT TextureCubeMap : public Texture
  19. {
  20. public :
  21. TextureCubeMap();
  22. /** Copy constructor using CopyOp to manage deep vs shallow copy. */
  23. TextureCubeMap(const TextureCubeMap& cm,const CopyOp& copyop=CopyOp::SHALLOW_COPY);
  24. META_StateAttribute(osg, TextureCubeMap,TEXTURE);
  25. /** Return -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs. */
  26. virtual int compare(const StateAttribute& rhs) const;
  27. virtual GLenum getTextureTarget() const { return GL_TEXTURE_CUBE_MAP; }
  28. enum Face {
  29. POSITIVE_X=0,
  30. NEGATIVE_X=1,
  31. POSITIVE_Y=2,
  32. NEGATIVE_Y=3,
  33. POSITIVE_Z=4,
  34. NEGATIVE_Z=5
  35. };
  36. /** Set the texture image for specified face. */
  37. virtual void setImage(unsigned int face, Image* image);
  38. template<class T> void setImage(unsigned int face, const ref_ptr<T>& image) { setImage(face, image.get()); }
  39. /** Get the texture image for specified face. */
  40. virtual Image* getImage(unsigned int face);
  41. /** Get the const texture image for specified face. */
  42. virtual const Image* getImage(unsigned int face) const;
  43. /** Get the number of images that can be assigned to the Texture. */
  44. virtual unsigned int getNumImages() const { return 6; }
  45. /** return true if the texture image data has been modified and the associated GL texture object needs to be updated.*/
  46. virtual bool isDirty(unsigned int contextID) const
  47. {
  48. return (_images[0].valid() && _images[0]->getModifiedCount()!=_modifiedCount[0][contextID]) ||
  49. (_images[1].valid() && _images[1]->getModifiedCount()!=_modifiedCount[1][contextID]) ||
  50. (_images[2].valid() && _images[2]->getModifiedCount()!=_modifiedCount[2][contextID]) ||
  51. (_images[3].valid() && _images[3]->getModifiedCount()!=_modifiedCount[3][contextID]) ||
  52. (_images[4].valid() && _images[4]->getModifiedCount()!=_modifiedCount[4][contextID]) ||
  53. (_images[5].valid() && _images[5]->getModifiedCount()!=_modifiedCount[5][contextID]);
  54. }
  55. inline unsigned int& getModifiedCount(unsigned int face,unsigned int contextID) const
  56. {
  57. // get the modified count for the current contextID.
  58. return _modifiedCount[face][contextID];
  59. }
  60. /** Set the texture width and height. If width or height are zero then
  61. * the respective size value is calculated from the source image sizes.
  62. */
  63. inline void setTextureSize(int width, int height) const
  64. {
  65. _textureWidth = width;
  66. _textureHeight = height;
  67. }
  68. void setTextureWidth(int width) { _textureWidth=width; }
  69. void setTextureHeight(int height) { _textureHeight=height; }
  70. virtual int getTextureWidth() const { return _textureWidth; }
  71. virtual int getTextureHeight() const { return _textureHeight; }
  72. virtual int getTextureDepth() const { return 1; }
  73. class OSG_EXPORT SubloadCallback : public Referenced
  74. {
  75. public:
  76. virtual void load(const TextureCubeMap& texture,State& state) const = 0;
  77. virtual void subload(const TextureCubeMap& texture,State& state) const = 0;
  78. };
  79. void setSubloadCallback(SubloadCallback* cb) { _subloadCallback = cb;; }
  80. SubloadCallback* getSubloadCallback() { return _subloadCallback.get(); }
  81. const SubloadCallback* getSubloadCallback() const { return _subloadCallback.get(); }
  82. /** Set the number of mip map levels the texture has been created with.
  83. * Should only be called within an osg::Texuture::apply() and custom OpenGL texture load.
  84. */
  85. void setNumMipmapLevels(unsigned int num) const { _numMipmapLevels=num; }
  86. /** Get the number of mip map levels the texture has been created with. */
  87. unsigned int getNumMipmapLevels() const { return _numMipmapLevels; }
  88. /** Copies a two-dimensional texture subimage, as per
  89. * glCopyTexSubImage2D. Updates a portion of an existing OpenGL
  90. * texture object from the current OpenGL background framebuffer
  91. * contents at position \a x, \a y with width \a width and height
  92. * \a height. Loads framebuffer data into the texture using offsets
  93. * \a xoffset and \a yoffset. \a width and \a height must be powers
  94. * of two. */
  95. void copyTexSubImageCubeMap(State& state, int face, int xoffset, int yoffset, int x, int y, int width, int height );
  96. /** On first apply (unless already compiled), create the mipmapped
  97. * texture and bind it. Subsequent apply will simple bind to texture.
  98. */
  99. virtual void apply(State& state) const;
  100. protected :
  101. virtual ~TextureCubeMap();
  102. bool imagesValid() const;
  103. virtual void computeInternalFormat() const;
  104. void allocateMipmap(State& state) const;
  105. ref_ptr<Image> _images[6];
  106. // subloaded images can have different texture and image sizes.
  107. mutable GLsizei _textureWidth, _textureHeight;
  108. // number of mip map levels the texture has been created with,
  109. mutable GLsizei _numMipmapLevels;
  110. ref_ptr<SubloadCallback> _subloadCallback;
  111. typedef buffered_value<unsigned int> ImageModifiedCount;
  112. mutable ImageModifiedCount _modifiedCount[6];
  113. };
  114. }
  115. #endif