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