Texture1D 6.3 KB

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