TransferFunction 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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_TRANSFERFUNCTION
  14. #define OSG_TRANSFERFUNCTION 1
  15. #include <osg/Texture>
  16. #include <osg/Shader>
  17. #include <map>
  18. namespace osg {
  19. /** TransferFunction is a class that provide a 1D,2D or 3D colour look up table
  20. * that can be used on the GPU as a 1D, 2D or 3D texture.
  21. * Typically uses include mapping heights to colours when contouring terrain,
  22. * or mapping intensities to colours when volume rendering.
  23. */
  24. class OSG_EXPORT TransferFunction : public osg::Object
  25. {
  26. public :
  27. TransferFunction();
  28. /** Copy constructor using CopyOp to manage deep vs shallow copy.*/
  29. TransferFunction(const TransferFunction& tf, const CopyOp& copyop=CopyOp::SHALLOW_COPY);
  30. META_Object(osg, TransferFunction)
  31. /** Get the image that is used for passing the transfer function data to the GPU.*/
  32. osg::Image* getImage() { return _image.get(); }
  33. /** Get the const image that is used for passing the transfer function data to the GPU.*/
  34. const osg::Image* getImage() const { return _image.get(); }
  35. protected:
  36. virtual ~TransferFunction();
  37. osg::ref_ptr<osg::Image> _image;
  38. };
  39. /** 1D variant of TransferFunction. */
  40. class OSG_EXPORT TransferFunction1D : public osg::TransferFunction
  41. {
  42. public:
  43. TransferFunction1D();
  44. /** Copy constructor using CopyOp to manage deep vs shallow copy.*/
  45. TransferFunction1D(const TransferFunction1D& tf, const CopyOp& copyop=CopyOp::SHALLOW_COPY);
  46. META_Object(osg, TransferFunction1D)
  47. /** Get the minimum transfer function value.*/
  48. float getMinimum() const { return _colorMap.empty() ? 0.0f : _colorMap.begin()->first; }
  49. /** Get the maximum transfer function value.*/
  50. float getMaximum() const { return _colorMap.empty() ? 0.0f : _colorMap.rbegin()->first; }
  51. /** allocate the osg::Image with specified dimension. The Image tracks the color map, and is used to represent the
  52. * transfer function when download to GPU.*/
  53. void allocate(unsigned int numImageCells);
  54. /** Clear the whole range to just represent a single color.*/
  55. void clear(const osg::Vec4& color = osg::Vec4(1.0f,1.0f,1.0f,1.0f));
  56. /** Get pixel value from the image. */
  57. osg::Vec4 getPixelValue(unsigned int i) const
  58. {
  59. if (_image.valid() && i<static_cast<unsigned int>(_image->s()))
  60. {
  61. return *reinterpret_cast<osg::Vec4*>(_image->data(i));
  62. }
  63. else
  64. {
  65. return osg::Vec4(1.0f,1.0f,1.0f,1.0f);
  66. }
  67. }
  68. /** Get the number of image cells that are assigned to the represent the transfer function when download to the GPU.*/
  69. unsigned int getNumberImageCells() const { return _image.valid() ? _image->s() : 0; }
  70. /** Set the color for a specified transfer function value.
  71. * updateImage defaults to true, and tells the setColor function to update the associate osg::Image that
  72. * tracks the color map. Pass in false as the updateImage parameter if you are setting up many values
  73. * at once to avoid recomputation of the image data, then once all setColor calls are made explicitly call
  74. * updateImage() to bring the osg::Image back into sync with the color map. */
  75. void setColor(float v, const osg::Vec4& color, bool updateImage=true);
  76. /** Get the color for a specified transfer function value, interpolating the value if no exact match is found.*/
  77. osg::Vec4 getColor(float v) const;
  78. typedef std::map<float, osg::Vec4> ColorMap;
  79. /** set the color map and automatically update the image to make sure they are in sync.*/
  80. void setColorMap(const ColorMap& vcm) { assign(vcm); }
  81. /** Get the color map that stores the mapping between the transfer function value and the colour it maps to.*/
  82. ColorMap& getColorMap() { return _colorMap; }
  83. /** Get the const color map that stores the mapping between the transfer function value and the colour it maps to.*/
  84. const ColorMap& getColorMap() const { return _colorMap; }
  85. /** Assign a color map and automatically update the image to make sure they are in sync.*/
  86. void assign(const ColorMap& vcm);
  87. /** Manually update the associate osg::Image to represent the colors assigned in the color map.*/
  88. void updateImage();
  89. protected:
  90. ColorMap _colorMap;
  91. void assignToImage(float lower_v, const osg::Vec4& lower_c, float upper_v, const osg::Vec4& upper_c);
  92. };
  93. }
  94. #endif