2012-03-22 01:36:20 +08:00
|
|
|
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
|
2007-03-10 19:53:31 +08:00
|
|
|
*
|
2012-03-22 01:36:20 +08:00
|
|
|
* This library is open source and may be redistributed and/or modified under
|
|
|
|
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
|
2007-03-10 19:53:31 +08:00
|
|
|
* (at your option) any later version. The full license is in LICENSE file
|
|
|
|
* included with this distribution, and on the openscenegraph.org website.
|
2012-03-22 01:36:20 +08:00
|
|
|
*
|
2007-03-10 19:53:31 +08:00
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2012-03-22 01:36:20 +08:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2007-03-10 19:53:31 +08:00
|
|
|
* OpenSceneGraph Public License for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef OSG_TRANSFERFUNCTION
|
|
|
|
#define OSG_TRANSFERFUNCTION 1
|
|
|
|
|
|
|
|
#include <osg/Texture>
|
|
|
|
#include <osg/Shader>
|
|
|
|
|
2008-09-13 17:09:51 +08:00
|
|
|
#include <map>
|
|
|
|
|
2007-03-10 19:53:31 +08:00
|
|
|
namespace osg {
|
|
|
|
|
|
|
|
|
2007-07-12 16:29:39 +08:00
|
|
|
/** TransferFunction is a class that provide a 1D,2D or 3D colour look up table
|
2012-03-22 01:36:20 +08:00
|
|
|
* that can be used on the GPU as a 1D, 2D or 3D texture.
|
|
|
|
* Typically uses include mapping heights to colours when contouring terrain,
|
2007-07-12 16:29:39 +08:00
|
|
|
* or mapping intensities to colours when volume rendering.
|
|
|
|
*/
|
2009-02-01 20:38:55 +08:00
|
|
|
class OSG_EXPORT TransferFunction : public osg::Object
|
2007-03-10 19:53:31 +08:00
|
|
|
{
|
|
|
|
public :
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2007-03-10 19:53:31 +08:00
|
|
|
TransferFunction();
|
|
|
|
|
2009-02-01 20:38:55 +08:00
|
|
|
/** Copy constructor using CopyOp to manage deep vs shallow copy.*/
|
|
|
|
TransferFunction(const TransferFunction& tf, const CopyOp& copyop=CopyOp::SHALLOW_COPY);
|
|
|
|
|
|
|
|
META_Object(osg, TransferFunction)
|
|
|
|
|
2009-02-02 22:43:27 +08:00
|
|
|
/** Get the image that is used for passing the transfer function data to the GPU.*/
|
2007-03-10 19:53:31 +08:00
|
|
|
osg::Image* getImage() { return _image.get(); }
|
2009-02-02 22:43:27 +08:00
|
|
|
|
|
|
|
/** Get the const image that is used for passing the transfer function data to the GPU.*/
|
2007-03-10 19:53:31 +08:00
|
|
|
const osg::Image* getImage() const { return _image.get(); }
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2007-03-10 19:53:31 +08:00
|
|
|
protected:
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2007-03-22 00:34:04 +08:00
|
|
|
virtual ~TransferFunction();
|
|
|
|
|
2007-03-10 19:53:31 +08:00
|
|
|
osg::ref_ptr<osg::Image> _image;
|
|
|
|
};
|
|
|
|
|
2007-07-12 16:29:39 +08:00
|
|
|
/** 1D variant of TransferFunction. */
|
2007-03-10 19:53:31 +08:00
|
|
|
class OSG_EXPORT TransferFunction1D : public osg::TransferFunction
|
|
|
|
{
|
|
|
|
public:
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2007-03-10 19:53:31 +08:00
|
|
|
TransferFunction1D();
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2009-02-01 20:38:55 +08:00
|
|
|
/** Copy constructor using CopyOp to manage deep vs shallow copy.*/
|
|
|
|
TransferFunction1D(const TransferFunction1D& tf, const CopyOp& copyop=CopyOp::SHALLOW_COPY);
|
|
|
|
|
|
|
|
META_Object(osg, TransferFunction1D)
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2010-09-24 22:53:10 +08:00
|
|
|
/** Get the minimum transfer function value.*/
|
2009-02-02 22:43:27 +08:00
|
|
|
float getMinimum() const { return _colorMap.empty() ? 0.0f : _colorMap.begin()->first; }
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2009-02-02 22:43:27 +08:00
|
|
|
/** Get the maximum transfer function value.*/
|
|
|
|
float getMaximum() const { return _colorMap.empty() ? 0.0f : _colorMap.rbegin()->first; }
|
2007-03-10 19:53:31 +08:00
|
|
|
|
2009-02-02 22:43:27 +08:00
|
|
|
/** allocate the osg::Image with specified dimension. The Image tracks the color map, and is used to represent the
|
|
|
|
* transfer function when download to GPU.*/
|
|
|
|
void allocate(unsigned int numImageCells);
|
2007-03-10 19:53:31 +08:00
|
|
|
|
2010-09-24 22:53:10 +08:00
|
|
|
/** Clear the whole range to just represent a single color.*/
|
2007-03-10 19:53:31 +08:00
|
|
|
void clear(const osg::Vec4& color = osg::Vec4(1.0f,1.0f,1.0f,1.0f));
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2009-02-02 22:43:27 +08:00
|
|
|
/** Get pixel value from the image. */
|
|
|
|
osg::Vec4 getPixelValue(unsigned int i) const
|
2008-09-13 17:09:51 +08:00
|
|
|
{
|
2009-02-02 22:43:27 +08:00
|
|
|
if (_image.valid() && i<static_cast<unsigned int>(_image->s()))
|
2012-03-22 01:36:20 +08:00
|
|
|
{
|
2009-02-02 22:43:27 +08:00
|
|
|
return *reinterpret_cast<osg::Vec4*>(_image->data(i));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return osg::Vec4(1.0f,1.0f,1.0f,1.0f);
|
|
|
|
}
|
2008-09-13 17:09:51 +08:00
|
|
|
}
|
2009-02-02 22:43:27 +08:00
|
|
|
|
|
|
|
/** Get the number of image cells that are assigned to the represent the transfer function when download to the GPU.*/
|
|
|
|
unsigned int getNumberImageCells() const { return _image.valid() ? _image->s() : 0; }
|
|
|
|
|
|
|
|
/** Set the color for a specified transfer function value.
|
|
|
|
* updateImage defaults to true, and tells the setColor function to update the associate osg::Image that
|
|
|
|
* tracks the color map. Pass in false as the updateImage parameter if you are setting up many values
|
2010-09-24 22:53:10 +08:00
|
|
|
* at once to avoid recomputation of the image data, then once all setColor calls are made explictly call
|
2012-03-22 01:36:20 +08:00
|
|
|
* updateImage() to bring the osg::Image back into sync with the color map. */
|
2009-02-02 22:43:27 +08:00
|
|
|
void setColor(float v, const osg::Vec4& color, bool updateImage=true);
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2009-02-02 22:43:27 +08:00
|
|
|
/** Get the color for a specified transfer function value, interpolating the value if no exact match is found.*/
|
|
|
|
osg::Vec4 getColor(float v) const;
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2009-02-02 22:43:27 +08:00
|
|
|
typedef std::map<float, osg::Vec4> ColorMap;
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2014-02-26 16:26:51 +08:00
|
|
|
/** set the color map and automatically update the image to make sure they are in sync.*/
|
|
|
|
void setColorMap(const ColorMap& vcm) { assign(vcm); }
|
|
|
|
|
2010-09-24 22:53:10 +08:00
|
|
|
/** Get the color map that stores the mapping between the the transfer function value and the colour it maps to.*/
|
2009-02-02 22:43:27 +08:00
|
|
|
ColorMap& getColorMap() { return _colorMap; }
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2010-09-24 22:53:10 +08:00
|
|
|
/** Get the const color map that stores the mapping between the the transfer function value and the colour it maps to.*/
|
2009-02-02 22:43:27 +08:00
|
|
|
const ColorMap& getColorMap() const { return _colorMap; }
|
|
|
|
|
|
|
|
/** Assign a color map and automatically update the image to make sure they are in sync.*/
|
|
|
|
void assign(const ColorMap& vcm);
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2009-02-02 22:43:27 +08:00
|
|
|
/** Manually update the associate osg::Image to represent the colors assigned in the color map.*/
|
|
|
|
void updateImage();
|
2008-09-13 17:09:51 +08:00
|
|
|
|
2007-03-10 19:53:31 +08:00
|
|
|
protected:
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2009-02-02 22:43:27 +08:00
|
|
|
ColorMap _colorMap;
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2009-02-02 22:43:27 +08:00
|
|
|
void assignToImage(float lower_v, const osg::Vec4& lower_c, float upper_v, const osg::Vec4& upper_c);
|
2007-03-10 19:53:31 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|