Introduced class interfaces for FixedFunctionTechnique and ShaderTechnique volume rendering techniques.

This commit is contained in:
Robert Osfield 2009-01-13 17:20:32 +00:00
parent 7bcdaa74bf
commit f260737cd8
19 changed files with 1289 additions and 11 deletions

View File

@ -63,6 +63,8 @@
#include <osg/ImageUtils>
#include <osgVolume/Volume>
#include <osgVolume/VolumeTile>
#include <osgVolume/ShaderTechnique>
#include <osgVolume/FixedFunctionTechnique>
typedef std::vector< osg::ref_ptr<osg::Image> > ImageList;
@ -2291,9 +2293,29 @@ int main( int argc, char **argv )
osg::ref_ptr<osgVolume::Volume> volume = new osgVolume::Volume;
osg::ref_ptr<osgVolume::VolumeTile> tile = new osgVolume::VolumeTile;
osg::ref_ptr<osgVolume::Layer> layer = new osgVolume::ImageLayer(image_3d);
tile->addLayer(layer.get());
volume->addChild(tile);
osg::ref_ptr<osgVolume::Layer> layer = new osgVolume::ImageLayer(image_3d);
layer->setTransferFunction(transferFunction.get());
if (matrix)
{
osgVolume::Locator* locator = new osgVolume::Locator(*matrix);
layer->setLocator(locator);
tile->setLocator(locator);
}
tile->addLayer(layer.get());
if (useShader)
{
tile->setVolumeTechnique(new osgVolume::ShaderTechnique);
}
else
{
tile->setVolumeTechnique(new osgVolume::FixedFunctionTechnique);
}
rootNode = volume.get();

View File

@ -0,0 +1,51 @@
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2008 Robert Osfield
*
* 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
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* OpenSceneGraph Public License for more details.
*/
#ifndef OSGVOLUME_FIXEDFUNCTIONTECHNIQUE
#define OSGVOLUME_FIXEDFUNCTIONTECHNIQUE 1
#include <osgVolume/VolumeTechnique>
namespace osgVolume {
class OSGVOLUME_EXPORT FixedFunctionTechnique : public VolumeTechnique
{
public:
FixedFunctionTechnique();
/** Copy constructor using CopyOp to manage deep vs shallow copy.*/
FixedFunctionTechnique(const FixedFunctionTechnique&,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
META_Object(osgVolume, FixedFunctionTechnique);
virtual void init();
virtual void update(osgUtil::UpdateVisitor* nv);
virtual void cull(osgUtil::CullVisitor* nv);
/** Clean scene graph from any terrain technique specific nodes.*/
virtual void cleanSceneGraph();
/** Traverse the terrain subgraph.*/
virtual void traverse(osg::NodeVisitor& nv);
protected:
virtual ~FixedFunctionTechnique();
};
}
#endif

View File

@ -0,0 +1,52 @@
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2008 Robert Osfield
*
* 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
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* OpenSceneGraph Public License for more details.
*/
#ifndef OSGVOLUME_SHADERTECHNIQUE
#define OSGVOLUME_SHADERTECHNIQUE 1
#include <osgVolume/VolumeTechnique>
namespace osgVolume {
class OSGVOLUME_EXPORT ShaderTechnique : public VolumeTechnique
{
public:
ShaderTechnique();
ShaderTechnique(const ShaderTechnique&,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
META_Object(osgVolume, ShaderTechnique);
virtual void init();
virtual void update(osgUtil::UpdateVisitor* nv);
virtual void cull(osgUtil::CullVisitor* nv);
/** Clean scene graph from any terrain technique specific nodes.*/
virtual void cleanSceneGraph();
/** Traverse the terrain subgraph.*/
virtual void traverse(osg::NodeVisitor& nv);
protected:
virtual ~ShaderTechnique();
osg::ref_ptr<osg::Geode> _geode;
};
}
#endif

View File

@ -122,7 +122,7 @@ class OSGVOLUME_EXPORT VolumeTile : public osg::Group
void addLayer(Layer* layer) { if (layer) _layers.push_back(layer); }
unsigned int getNumLayers() { return _layers.size(); }
/** Set the VolumeTechnique*/
void setVolumeTechnique(VolumeTechnique* VolumeTechnique);

View File

@ -136,12 +136,19 @@ class ReaderWriterDICOM : public osgDB::ReaderWriter
// scale up to provide scale of complete tile
osg::Vec3d scale(osg::Vec3(result.getImage()->s(),result.getImage()->t(), result.getImage()->r()));
matrix->postMultScale(scale);
osgVolume::Locator* locator = new osgVolume::Locator(*matrix);
tile->setLocator(new osgVolume::Locator(*matrix));
tile->setLocator(locator);
layer->setLocator(locator);
result.getImage()->setUserData(0);
// result.getImage()->setUserData(0);
notice()<<"Locator "<<*matrix<<std::endl;
osg::notify(osg::NOTICE)<<"Locator "<<*matrix<<std::endl;
}
else
{
osg::notify(osg::NOTICE)<<"No Locator found on osg::Image"<<std::endl;
}
volume->addChild(tile.get());

View File

@ -9,10 +9,12 @@ SET(LIB_NAME osgVolume)
SET(HEADER_PATH ${OpenSceneGraph_SOURCE_DIR}/include/${LIB_NAME})
SET(LIB_PUBLIC_HEADERS
${HEADER_PATH}/Export
${HEADER_PATH}/FixedFunctionTechnique
${HEADER_PATH}/Layer
${HEADER_PATH}/Locator
${HEADER_PATH}/ShaderTechnique
${HEADER_PATH}/Version
${HEADER_PATH}/Volume
${HEADER_PATH}/Locator
${HEADER_PATH}/Layer
${HEADER_PATH}/VolumeTechnique
${HEADER_PATH}/VolumeTile
)
@ -21,11 +23,13 @@ SET(LIB_PUBLIC_HEADERS
ADD_LIBRARY(${LIB_NAME}
${OPENSCENEGRAPH_USER_DEFINED_DYNAMIC_OR_STATIC}
${LIB_PUBLIC_HEADERS}
FixedFunctionTechnique.cpp
Layer.cpp
Locator.cpp
ShaderTechnique.cpp
Version.cpp
Volume.cpp
VolumeTechnique.cpp
Layer.cpp
Locator.cpp
VolumeTile.cpp
)

View File

@ -0,0 +1,55 @@
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2009 Robert Osfield
*
* 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
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* OpenSceneGraph Public License for more details.
*/
#include <osgVolume/FixedFunctionTechnique>
using namespace osgVolume;
FixedFunctionTechnique::FixedFunctionTechnique()
{
}
FixedFunctionTechnique::FixedFunctionTechnique(const FixedFunctionTechnique& fft,const osg::CopyOp& copyop):
VolumeTechnique(fft,copyop)
{
}
FixedFunctionTechnique::~FixedFunctionTechnique()
{
}
void FixedFunctionTechnique::init()
{
osg::notify(osg::NOTICE)<<"FixedFunctionTechnique::init()"<<std::endl;
}
void FixedFunctionTechnique::update(osgUtil::UpdateVisitor* nv)
{
osg::notify(osg::NOTICE)<<"FixedFunctionTechnique:update(osgUtil::UpdateVisitor* nv):"<<std::endl;
}
void FixedFunctionTechnique::cull(osgUtil::CullVisitor* nv)
{
osg::notify(osg::NOTICE)<<"FixedFunctionTechnique::cull(osgUtil::CullVisitor* nv)"<<std::endl;
}
void FixedFunctionTechnique::cleanSceneGraph()
{
osg::notify(osg::NOTICE)<<"FixedFunctionTechnique::cleanSceneGraph()"<<std::endl;
}
void FixedFunctionTechnique::traverse(osg::NodeVisitor& nv)
{
osg::notify(osg::NOTICE)<<"FixedFunctionTechnique::traverse(osg::NodeVisitor& nv)"<<std::endl;
}

View File

@ -13,6 +13,9 @@
#include <osgVolume/Layer>
#include <osg/Notify>
#include <osg/io_utils>
using namespace osgVolume;
Layer::Layer():
@ -39,6 +42,9 @@ osg::BoundingSphere Layer::computeBound() const
osg::Vec3d left, right;
getLocator()->computeLocalBounds(left, right);
osg::notify(osg::NOTICE)<<"left = "<<left<<std::endl;
osg::notify(osg::NOTICE)<<"right = "<<right<<std::endl;
return osg::BoundingSphere((left+right)*0.5, (right-left).length()*0.5);
}

View File

@ -0,0 +1,181 @@
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2009 Robert Osfield
*
* 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
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* OpenSceneGraph Public License for more details.
*/
#include <osgVolume/ShaderTechnique>
#include <osgVolume/VolumeTile>
#include <osg/Geometry>
#include <osg/io_utils>
using namespace osgVolume;
ShaderTechnique::ShaderTechnique()
{
}
ShaderTechnique::ShaderTechnique(const ShaderTechnique& fft,const osg::CopyOp& copyop):
VolumeTechnique(fft,copyop)
{
}
ShaderTechnique::~ShaderTechnique()
{
}
void ShaderTechnique::init()
{
osg::notify(osg::NOTICE)<<"ShaderTechnique::init()"<<std::endl;
if (!_volumeTile) return;
_geode = new osg::Geode;
osgVolume::Locator* masterLocator = _volumeTile->getLocator();
for(unsigned int i = 0;
i < _volumeTile->getNumLayers() && !masterLocator;
++i)
{
if (_volumeTile->getLayer(i))
{
masterLocator = _volumeTile->getLayer(i)->getLocator();
osg::notify(osg::NOTICE)<<"assigning locator = "<<masterLocator<<std::endl;
}
}
osg::Matrix matrix;
if (masterLocator)
{
matrix = masterLocator->getTransform();
}
osg::notify(osg::NOTICE)<<"Matrix = "<<matrix<<std::endl;
{
osg::Geometry* geom = new osg::Geometry;
osg::Vec3Array* coords = new osg::Vec3Array(8);
(*coords)[0] = osg::Vec3d(0.0,0.0,0.0) * matrix;
(*coords)[1] = osg::Vec3d(1.0,0.0,0.0) * matrix;
(*coords)[2] = osg::Vec3d(1.0,1.0,0.0) * matrix;
(*coords)[3] = osg::Vec3d(0.0,1.0,0.0) * matrix;
(*coords)[4] = osg::Vec3d(0.0,0.0,1.0) * matrix;
(*coords)[5] = osg::Vec3d(1.0,0.0,1.0) * matrix;
(*coords)[6] = osg::Vec3d(1.0,1.0,1.0) * matrix;
(*coords)[7] = osg::Vec3d(0.0,1.0,1.0) * matrix;
geom->setVertexArray(coords);
osg::Vec4Array* colours = new osg::Vec4Array(1);
(*colours)[0].set(1.0f,1.0f,1.0,1.0f);
geom->setColorArray(colours);
geom->setColorBinding(osg::Geometry::BIND_OVERALL);
osg::DrawElementsUShort* drawElements = new osg::DrawElementsUShort(GL_QUADS);
// bottom
drawElements->push_back(0);
drawElements->push_back(1);
drawElements->push_back(2);
drawElements->push_back(3);
// bottom
drawElements->push_back(3);
drawElements->push_back(2);
drawElements->push_back(6);
drawElements->push_back(7);
// left
drawElements->push_back(0);
drawElements->push_back(3);
drawElements->push_back(7);
drawElements->push_back(4);
// right
drawElements->push_back(5);
drawElements->push_back(6);
drawElements->push_back(2);
drawElements->push_back(1);
// front
drawElements->push_back(1);
drawElements->push_back(0);
drawElements->push_back(4);
drawElements->push_back(5);
// top
drawElements->push_back(7);
drawElements->push_back(6);
drawElements->push_back(5);
drawElements->push_back(4);
geom->addPrimitiveSet(drawElements);
_geode->addDrawable(geom);
}
}
void ShaderTechnique::update(osgUtil::UpdateVisitor* uv)
{
// osg::notify(osg::NOTICE)<<"ShaderTechnique:update(osgUtil::UpdateVisitor* nv):"<<std::endl;
}
void ShaderTechnique::cull(osgUtil::CullVisitor* cv)
{
// osg::notify(osg::NOTICE)<<"ShaderTechnique::cull(osgUtil::CullVisitor* nv)"<<std::endl;
if (_geode.valid())
{
_geode->accept(*cv);
}
}
void ShaderTechnique::cleanSceneGraph()
{
osg::notify(osg::NOTICE)<<"ShaderTechnique::cleanSceneGraph()"<<std::endl;
}
void ShaderTechnique::traverse(osg::NodeVisitor& nv)
{
// osg::notify(osg::NOTICE)<<"ShaderTechnique::traverse(osg::NodeVisitor& nv)"<<std::endl;
if (!_volumeTile) return;
// if app traversal update the frame count.
if (nv.getVisitorType()==osg::NodeVisitor::UPDATE_VISITOR)
{
if (_volumeTile->getDirty()) _volumeTile->init();
osgUtil::UpdateVisitor* uv = dynamic_cast<osgUtil::UpdateVisitor*>(&nv);
if (uv)
{
update(uv);
return;
}
}
else if (nv.getVisitorType()==osg::NodeVisitor::CULL_VISITOR)
{
osgUtil::CullVisitor* cv = dynamic_cast<osgUtil::CullVisitor*>(&nv);
if (cv)
{
cull(cv);
return;
}
}
if (_volumeTile->getDirty())
{
osg::notify(osg::INFO)<<"******* Doing init ***********"<<std::endl;
_volumeTile->init();
}
}

View File

@ -0,0 +1,99 @@
char volume_frag[] = "uniform sampler3D baseTexture;\n"
"uniform float sampleDensity;\n"
"uniform float transparency;\n"
"uniform float alphaCutOff;\n"
"\n"
"varying vec4 cameraPos;\n"
"varying vec4 vertexPos;\n"
"varying mat4 texgen;\n"
"\n"
"void main(void)\n"
"{ \n"
" vec3 t0 = (texgen * vertexPos).xyz;\n"
" vec3 te = (texgen * cameraPos).xyz;\n"
"\n"
" if (te.x>=0.0 && te.x<=1.0 &&\n"
" te.y>=0.0 && te.y<=1.0 &&\n"
" te.z>=0.0 && te.z<=1.0)\n"
" {\n"
" // do nothing... te inside volume\n"
" }\n"
" else\n"
" {\n"
" if (te.x<0.0)\n"
" {\n"
" float r = -te.x / (t0.x-te.x);\n"
" te = te + (t0-te)*r;\n"
" }\n"
"\n"
" if (te.x>1.0)\n"
" {\n"
" float r = (1.0-te.x) / (t0.x-te.x);\n"
" te = te + (t0-te)*r;\n"
" }\n"
"\n"
" if (te.y<0.0)\n"
" {\n"
" float r = -te.y / (t0.y-te.y);\n"
" te = te + (t0-te)*r;\n"
" }\n"
"\n"
" if (te.y>1.0)\n"
" {\n"
" float r = (1.0-te.y) / (t0.y-te.y);\n"
" te = te + (t0-te)*r;\n"
" }\n"
"\n"
" if (te.z<0.0)\n"
" {\n"
" float r = -te.z / (t0.z-te.z);\n"
" te = te + (t0-te)*r;\n"
" }\n"
"\n"
" if (te.z>1.0)\n"
" {\n"
" float r = (1.0-te.z) / (t0.z-te.z);\n"
" te = te + (t0-te)*r;\n"
" }\n"
" }\n"
"\n"
" const float max_iteratrions = 2048.0;\n"
" float num_iterations = ceil(length(te-t0)/sampleDensity);\n"
" if (num_iterations<2.0) num_iterations = 2.0;\n"
"\n"
" if (num_iterations>max_iteratrions) \n"
" {\n"
" num_iterations = max_iteratrions;\n"
" }\n"
"\n"
" vec3 deltaTexCoord=(te-t0)/float(num_iterations-1.0);\n"
" vec3 texcoord = t0;\n"
"\n"
" vec4 fragColor = vec4(0.0, 0.0, 0.0, 0.0); \n"
" while(num_iterations>0.0)\n"
" {\n"
" vec4 color = texture3D( baseTexture, texcoord);\n"
" float r = color[3]*transparency;\n"
" if (r>alphaCutOff)\n"
" {\n"
" fragColor.xyz = fragColor.xyz*(1.0-r)+color.xyz*r;\n"
" fragColor.w += r;\n"
" }\n"
"\n"
" if (fragColor.w<color.w)\n"
" {\n"
" fragColor = color;\n"
" }\n"
" texcoord += deltaTexCoord; \n"
"\n"
" --num_iterations;\n"
" }\n"
"\n"
" fragColor.w *= transparency;\n"
"\n"
" if (fragColor.w>1.0) fragColor.w = 1.0; \n"
" if (fragColor.w<alphaCutOff) discard;\n"
" \n"
" gl_FragColor = fragColor;\n"
"}\n"
"\n";

View File

@ -0,0 +1,134 @@
char volume_iso_frag[] = "uniform sampler3D baseTexture;\n"
"uniform float sampleDensity;\n"
"uniform float transparency;\n"
"uniform float alphaCutOff;\n"
"\n"
"varying vec4 cameraPos;\n"
"varying vec4 vertexPos;\n"
"varying mat4 texgen;\n"
"\n"
"void main(void)\n"
"{ \n"
"\n"
" vec3 t0 = (texgen * vertexPos).xyz;\n"
" vec3 te = (texgen * cameraPos).xyz;\n"
"\n"
" vec3 eyeDirection = normalize(te-t0);\n"
"\n"
" if (te.x>=0.0 && te.x<=1.0 &&\n"
" te.y>=0.0 && te.y<=1.0 &&\n"
" te.z>=0.0 && te.z<=1.0)\n"
" {\n"
" // do nothing... te inside volume\n"
" }\n"
" else\n"
" {\n"
" if (te.x<0.0)\n"
" {\n"
" float r = -te.x / (t0.x-te.x);\n"
" te = te + (t0-te)*r;\n"
" }\n"
"\n"
" if (te.x>1.0)\n"
" {\n"
" float r = (1.0-te.x) / (t0.x-te.x);\n"
" te = te + (t0-te)*r;\n"
" }\n"
"\n"
" if (te.y<0.0)\n"
" {\n"
" float r = -te.y / (t0.y-te.y);\n"
" te = te + (t0-te)*r;\n"
" }\n"
"\n"
" if (te.y>1.0)\n"
" {\n"
" float r = (1.0-te.y) / (t0.y-te.y);\n"
" te = te + (t0-te)*r;\n"
" }\n"
"\n"
" if (te.z<0.0)\n"
" {\n"
" float r = -te.z / (t0.z-te.z);\n"
" te = te + (t0-te)*r;\n"
" }\n"
"\n"
" if (te.z>1.0)\n"
" {\n"
" float r = (1.0-te.z) / (t0.z-te.z);\n"
" te = te + (t0-te)*r;\n"
" }\n"
" }\n"
"\n"
" const float max_iteratrions = 2048.0;\n"
" float num_iterations = ceil(length(te-t0)/sampleDensity);\n"
" if (num_iterations<2.0) num_iterations = 2.0;\n"
"\n"
" if (num_iterations>max_iteratrions) \n"
" {\n"
" num_iterations = max_iteratrions;\n"
" }\n"
"\n"
" vec3 deltaTexCoord=(t0-te)/float(num_iterations-1.0);\n"
" vec3 texcoord = te;\n"
"\n"
" vec4 fragColor = vec4(0.0, 0.0, 0.0, 0.0); \n"
" vec4 previousColor = texture3D( baseTexture, texcoord);\n"
" \n"
" float normalSampleDistance = 1.0/512.0;\n"
" vec3 deltaX = vec3(normalSampleDistance, 0.0, 0.0);\n"
" vec3 deltaY = vec3(0.0, normalSampleDistance, 0.0);\n"
" vec3 deltaZ = vec3(0.0, 0.0, normalSampleDistance);\n"
" \n"
" while(num_iterations>0.0)\n"
" {\n"
" vec4 color = texture3D( baseTexture, texcoord);\n"
"\n"
" float m = (previousColor.a-alphaCutOff) * (color.a-alphaCutOff);\n"
" if (m <= 0.0)\n"
" {\n"
" float r = (alphaCutOff-color.a)/(previousColor.a-color.a);\n"
" texcoord = texcoord - r*deltaTexCoord;\n"
" \n"
" float a = color.a;\n"
" float px = texture3D( baseTexture, texcoord + deltaX).a;\n"
" float py = texture3D( baseTexture, texcoord + deltaY).a;\n"
" float pz = texture3D( baseTexture, texcoord + deltaZ).a;\n"
"\n"
" float nx = texture3D( baseTexture, texcoord - deltaX).a;\n"
" float ny = texture3D( baseTexture, texcoord - deltaY).a;\n"
" float nz = texture3D( baseTexture, texcoord - deltaZ).a;\n"
" \n"
" vec3 grad = vec3(px-nx, py-ny, pz-nz);\n"
" vec3 normal = normalize(grad);\n"
"\n"
" float lightScale = 0.1 + abs(dot(normal.xyz, eyeDirection))*0.9;\n"
" \n"
" \n"
"#if 0\n"
" color.x *= lightScale;\n"
" color.y *= lightScale;\n"
" color.z *= lightScale;\n"
"#else\n"
" color.x = lightScale;\n"
" color.y = lightScale;\n"
" color.z = lightScale;\n"
"#endif\n"
"\n"
" fragColor = vec4(lightScale, lightScale, lightScale, 1.0);\n"
" \n"
" break;\n"
" }\n"
" \n"
" previousColor = color;\n"
" \n"
" texcoord += deltaTexCoord; \n"
"\n"
" --num_iterations;\n"
" }\n"
"\n"
" if (fragColor.w>1.0) fragColor.w = 1.0; \n"
" //if (fragColor.w<alphaCutOff) discard;\n"
" gl_FragColor = fragColor;\n"
"}\n"
"\n";

View File

@ -0,0 +1,92 @@
char volume_mip_frag[] = "uniform sampler3D baseTexture;\n"
"uniform float sampleDensity;\n"
"uniform float transparency;\n"
"uniform float alphaCutOff;\n"
"\n"
"varying vec4 cameraPos;\n"
"varying vec4 vertexPos;\n"
"varying mat4 texgen;\n"
"\n"
"void main(void)\n"
"{ \n"
" vec3 t0 = (texgen * vertexPos).xyz;\n"
" vec3 te = (texgen * cameraPos).xyz;\n"
"\n"
" if (te.x>=0.0 && te.x<=1.0 &&\n"
" te.y>=0.0 && te.y<=1.0 &&\n"
" te.z>=0.0 && te.z<=1.0)\n"
" {\n"
" // do nothing... te inside volume\n"
" }\n"
" else\n"
" {\n"
" if (te.x<0.0)\n"
" {\n"
" float r = -te.x / (t0.x-te.x);\n"
" te = te + (t0-te)*r;\n"
" }\n"
"\n"
" if (te.x>1.0)\n"
" {\n"
" float r = (1.0-te.x) / (t0.x-te.x);\n"
" te = te + (t0-te)*r;\n"
" }\n"
"\n"
" if (te.y<0.0)\n"
" {\n"
" float r = -te.y / (t0.y-te.y);\n"
" te = te + (t0-te)*r;\n"
" }\n"
"\n"
" if (te.y>1.0)\n"
" {\n"
" float r = (1.0-te.y) / (t0.y-te.y);\n"
" te = te + (t0-te)*r;\n"
" }\n"
"\n"
" if (te.z<0.0)\n"
" {\n"
" float r = -te.z / (t0.z-te.z);\n"
" te = te + (t0-te)*r;\n"
" }\n"
"\n"
" if (te.z>1.0)\n"
" {\n"
" float r = (1.0-te.z) / (t0.z-te.z);\n"
" te = te + (t0-te)*r;\n"
" }\n"
" }\n"
"\n"
" const float max_iteratrions = 2048.0;\n"
" float num_iterations = ceil(length(te-t0)/sampleDensity);\n"
" if (num_iterations<2.0) num_iterations = 2.0;\n"
"\n"
" if (num_iterations>max_iteratrions) \n"
" {\n"
" num_iterations = max_iteratrions;\n"
" }\n"
"\n"
" vec3 deltaTexCoord=(te-t0)/float(num_iterations-1.0);\n"
" vec3 texcoord = t0;\n"
"\n"
" vec4 fragColor = vec4(0.0, 0.0, 0.0, 0.0); \n"
" while(num_iterations>0.0)\n"
" {\n"
" vec4 color = texture3D( baseTexture, texcoord);\n"
" if (fragColor.w<color.w)\n"
" {\n"
" fragColor = color;\n"
" }\n"
" texcoord += deltaTexCoord; \n"
"\n"
" --num_iterations;\n"
" }\n"
"\n"
" fragColor.w *= transparency;\n"
"\n"
" if (fragColor.w>1.0) fragColor.w = 1.0; \n"
" if (fragColor.w<alphaCutOff) discard;\n"
" \n"
" gl_FragColor = fragColor;\n"
"}\n"
"\n";

View File

@ -0,0 +1,120 @@
char volume_n_frag[] = "uniform sampler3D baseTexture;\n"
"uniform sampler3D normalMap;\n"
"uniform float sampleDensity;\n"
"uniform float transparency;\n"
"uniform float alphaCutOff;\n"
"\n"
"varying vec4 cameraPos;\n"
"varying vec4 vertexPos;\n"
"varying mat4 texgen;\n"
"\n"
"void main(void)\n"
"{ \n"
"\n"
" vec3 t0 = (texgen * vertexPos).xyz;\n"
" vec3 te = (texgen * cameraPos).xyz;\n"
"\n"
" vec3 eyeDirection = normalize(te-t0);\n"
"\n"
" if (te.x>=0.0 && te.x<=1.0 &&\n"
" te.y>=0.0 && te.y<=1.0 &&\n"
" te.z>=0.0 && te.z<=1.0)\n"
" {\n"
" // do nothing... te inside volume\n"
" }\n"
" else\n"
" {\n"
" if (te.x<0.0)\n"
" {\n"
" float r = -te.x / (t0.x-te.x);\n"
" te = te + (t0-te)*r;\n"
" }\n"
"\n"
" if (te.x>1.0)\n"
" {\n"
" float r = (1.0-te.x) / (t0.x-te.x);\n"
" te = te + (t0-te)*r;\n"
" }\n"
"\n"
" if (te.y<0.0)\n"
" {\n"
" float r = -te.y / (t0.y-te.y);\n"
" te = te + (t0-te)*r;\n"
" }\n"
"\n"
" if (te.y>1.0)\n"
" {\n"
" float r = (1.0-te.y) / (t0.y-te.y);\n"
" te = te + (t0-te)*r;\n"
" }\n"
"\n"
" if (te.z<0.0)\n"
" {\n"
" float r = -te.z / (t0.z-te.z);\n"
" te = te + (t0-te)*r;\n"
" }\n"
"\n"
" if (te.z>1.0)\n"
" {\n"
" float r = (1.0-te.z) / (t0.z-te.z);\n"
" te = te + (t0-te)*r;\n"
" }\n"
" }\n"
"\n"
" const float max_iteratrions = 2048.0;\n"
" float num_iterations = ceil(length(te-t0)/sampleDensity);\n"
" if (num_iterations<2.0) num_iterations = 2.0;\n"
"\n"
" if (num_iterations>max_iteratrions) \n"
" {\n"
" num_iterations = max_iteratrions;\n"
" }\n"
"\n"
" vec3 deltaTexCoord=(te-t0)/float(num_iterations-1.0);\n"
" vec3 texcoord = t0;\n"
"\n"
" vec4 fragColor = vec4(0.0, 0.0, 0.0, 0.0); \n"
" while(num_iterations>0.0)\n"
" {\n"
" vec4 normal = texture3D( normalMap, texcoord);\n"
"#if 1\n"
" vec4 color = texture3D( baseTexture, texcoord);\n"
"\n"
" normal.x = normal.x*2.0-1.0;\n"
" normal.y = normal.y*2.0-1.0;\n"
" normal.z = normal.z*2.0-1.0;\n"
" \n"
" float lightScale = 0.1 + max(dot(normal.xyz, eyeDirection), 0.0);\n"
" color.x *= lightScale;\n"
" color.y *= lightScale;\n"
" color.z *= lightScale;\n"
"\n"
" float r = normal[3]*transparency;\n"
"#else\n"
" vec4 color = texture3D( normalMap, texcoord);\n"
" color.x = color.x*2.0 - 1.0;\n"
" color.y = color.y*2.0 - 1.0;\n"
" color.z = color.z*2.0 - 1.0;\n"
" \n"
" float lightScale = 0.1 + max(dot(color.xyz, eyeDirection), 0.0);\n"
" color.x = lightScale;\n"
" color.y = lightScale;\n"
" color.z = lightScale;\n"
"\n"
" float r = color[3]*transparency;\n"
"#endif \n"
" if (r>alphaCutOff)\n"
" {\n"
" fragColor.xyz = fragColor.xyz*(1.0-r)+color.xyz*r;\n"
" fragColor.w += r;\n"
" }\n"
" texcoord += deltaTexCoord; \n"
"\n"
" --num_iterations;\n"
" }\n"
"\n"
" if (fragColor.w>1.0) fragColor.w = 1.0; \n"
" if (fragColor.w==0.0) discard;\n"
" gl_FragColor = fragColor;\n"
"}\n"
"\n";

View File

@ -0,0 +1,102 @@
char volume_tf_frag[] = "uniform sampler3D baseTexture;\n"
"uniform sampler1D tfTexture;\n"
"uniform float sampleDensity;\n"
"uniform float transparency;\n"
"uniform float alphaCutOff;\n"
"\n"
"varying vec4 cameraPos;\n"
"varying vec4 vertexPos;\n"
"varying mat4 texgen;\n"
"\n"
"void main(void)\n"
"{ \n"
" vec3 t0 = (texgen * vertexPos).xyz;\n"
" vec3 te = (texgen * cameraPos).xyz;\n"
"\n"
" if (te.x>=0.0 && te.x<=1.0 &&\n"
" te.y>=0.0 && te.y<=1.0 &&\n"
" te.z>=0.0 && te.z<=1.0)\n"
" {\n"
" // do nothing... te inside volume\n"
" }\n"
" else\n"
" {\n"
" if (te.x<0.0)\n"
" {\n"
" float r = -te.x / (t0.x-te.x);\n"
" te = te + (t0-te)*r;\n"
" }\n"
"\n"
" if (te.x>1.0)\n"
" {\n"
" float r = (1.0-te.x) / (t0.x-te.x);\n"
" te = te + (t0-te)*r;\n"
" }\n"
"\n"
" if (te.y<0.0)\n"
" {\n"
" float r = -te.y / (t0.y-te.y);\n"
" te = te + (t0-te)*r;\n"
" }\n"
"\n"
" if (te.y>1.0)\n"
" {\n"
" float r = (1.0-te.y) / (t0.y-te.y);\n"
" te = te + (t0-te)*r;\n"
" }\n"
"\n"
" if (te.z<0.0)\n"
" {\n"
" float r = -te.z / (t0.z-te.z);\n"
" te = te + (t0-te)*r;\n"
" }\n"
"\n"
" if (te.z>1.0)\n"
" {\n"
" float r = (1.0-te.z) / (t0.z-te.z);\n"
" te = te + (t0-te)*r;\n"
" }\n"
" }\n"
"\n"
" const float max_iteratrions = 2048.0;\n"
" float num_iterations = ceil(length(te-t0)/sampleDensity);\n"
" if (num_iterations<2.0) num_iterations = 2.0;\n"
"\n"
" if (num_iterations>max_iteratrions) \n"
" {\n"
" num_iterations = max_iteratrions;\n"
" }\n"
"\n"
" vec3 deltaTexCoord=(te-t0)/float(num_iterations-1.0);\n"
" vec3 texcoord = t0;\n"
"\n"
" vec4 fragColor = vec4(0.0, 0.0, 0.0, 0.0); \n"
" while(num_iterations>0.0)\n"
" {\n"
" float v = texture3D( baseTexture, texcoord).a;\n"
" vec4 color = texture1D( tfTexture, v);\n"
"\n"
" float r = color[3]*transparency;\n"
" if (r>alphaCutOff)\n"
" {\n"
" fragColor.xyz = fragColor.xyz*(1.0-r)+color.xyz*r;\n"
" fragColor.w += r;\n"
" }\n"
"\n"
" if (fragColor.w<color.w)\n"
" {\n"
" fragColor = color;\n"
" }\n"
" texcoord += deltaTexCoord; \n"
"\n"
" --num_iterations;\n"
" }\n"
"\n"
" fragColor.w *= transparency;\n"
"\n"
" if (fragColor.w>1.0) fragColor.w = 1.0; \n"
" if (fragColor.w<alphaCutOff) discard;\n"
" \n"
" gl_FragColor = fragColor;\n"
"}\n"
"\n";

View File

@ -0,0 +1,129 @@
char volume_tf_iso_frag[] = "uniform sampler3D baseTexture;\n"
"uniform sampler1D tfTexture;\n"
"uniform float sampleDensity;\n"
"uniform float transparency;\n"
"uniform float alphaCutOff;\n"
"\n"
"varying vec4 cameraPos;\n"
"varying vec4 vertexPos;\n"
"varying mat4 texgen;\n"
"\n"
"void main(void)\n"
"{ \n"
" vec3 t0 = (texgen * vertexPos).xyz;\n"
" vec3 te = (texgen * cameraPos).xyz;\n"
"\n"
" vec3 eyeDirection = normalize(te-t0);\n"
"\n"
" if (te.x>=0.0 && te.x<=1.0 &&\n"
" te.y>=0.0 && te.y<=1.0 &&\n"
" te.z>=0.0 && te.z<=1.0)\n"
" {\n"
" // do nothing... te inside volume\n"
" }\n"
" else\n"
" {\n"
" if (te.x<0.0)\n"
" {\n"
" float r = -te.x / (t0.x-te.x);\n"
" te = te + (t0-te)*r;\n"
" }\n"
"\n"
" if (te.x>1.0)\n"
" {\n"
" float r = (1.0-te.x) / (t0.x-te.x);\n"
" te = te + (t0-te)*r;\n"
" }\n"
"\n"
" if (te.y<0.0)\n"
" {\n"
" float r = -te.y / (t0.y-te.y);\n"
" te = te + (t0-te)*r;\n"
" }\n"
"\n"
" if (te.y>1.0)\n"
" {\n"
" float r = (1.0-te.y) / (t0.y-te.y);\n"
" te = te + (t0-te)*r;\n"
" }\n"
"\n"
" if (te.z<0.0)\n"
" {\n"
" float r = -te.z / (t0.z-te.z);\n"
" te = te + (t0-te)*r;\n"
" }\n"
"\n"
" if (te.z>1.0)\n"
" {\n"
" float r = (1.0-te.z) / (t0.z-te.z);\n"
" te = te + (t0-te)*r;\n"
" }\n"
" }\n"
"\n"
" const float max_iteratrions = 2048.0;\n"
" float num_iterations = ceil(length(te-t0)/sampleDensity);\n"
" if (num_iterations<2.0) num_iterations = 2.0;\n"
" \n"
" if (num_iterations>max_iteratrions) \n"
" {\n"
" num_iterations = max_iteratrions;\n"
" }\n"
"\n"
" vec3 deltaTexCoord=(t0-te)/float(num_iterations-1.0);\n"
" vec3 texcoord = te;\n"
" float previousV = texture3D( baseTexture, texcoord).a;\n"
"\n"
" float normalSampleDistance = 1.0/512.0;\n"
" vec3 deltaX = vec3(normalSampleDistance, 0.0, 0.0);\n"
" vec3 deltaY = vec3(0.0, normalSampleDistance, 0.0);\n"
" vec3 deltaZ = vec3(0.0, 0.0, normalSampleDistance);\n"
"\n"
" vec4 fragColor = vec4(0.0, 0.0, 0.0, 0.0); \n"
" while(num_iterations>0.0)\n"
" {\n"
"\n"
" float v = texture3D( baseTexture, texcoord).a;\n"
"\n"
" float m = (previousV-alphaCutOff) * (v-alphaCutOff);\n"
" if (m <= 0.0)\n"
" {\n"
" float r = (alphaCutOff-v)/(previousV-v);\n"
" texcoord = texcoord - r*deltaTexCoord;\n"
"\n"
" v = texture3D( baseTexture, texcoord).a;\n"
" vec4 color = texture1D( tfTexture, v);\n"
"\n"
" float px = texture3D( baseTexture, texcoord + deltaX).a;\n"
" float py = texture3D( baseTexture, texcoord + deltaY).a;\n"
" float pz = texture3D( baseTexture, texcoord + deltaZ).a;\n"
"\n"
" float nx = texture3D( baseTexture, texcoord - deltaX).a;\n"
" float ny = texture3D( baseTexture, texcoord - deltaY).a;\n"
" float nz = texture3D( baseTexture, texcoord - deltaZ).a;\n"
" \n"
" vec3 grad = vec3(px-nx, py-ny, pz-nz);\n"
" vec3 normal = normalize(grad);\n"
"\n"
" float lightScale = 0.1 + abs(dot(normal.xyz, eyeDirection))*0.9;\n"
" \n"
" color.x *= lightScale;\n"
" color.y *= lightScale;\n"
" color.z *= lightScale;\n"
"\n"
" fragColor = color;\n"
" \n"
" break;\n"
" }\n"
" \n"
" previousV = v;\n"
" \n"
" texcoord += deltaTexCoord; \n"
"\n"
" --num_iterations;\n"
" }\n"
"\n"
" if (fragColor.w>1.0) fragColor.w = 1.0; \n"
" if (fragColor.w<alphaCutOff) discard;\n"
" gl_FragColor = fragColor;\n"
"}\n"
"\n";

View File

@ -0,0 +1,93 @@
char volume_tf_mip_frag[] = "uniform sampler3D baseTexture;\n"
"uniform sampler1D tfTexture;\n"
"uniform float sampleDensity;\n"
"uniform float transparency;\n"
"uniform float alphaCutOff;\n"
"\n"
"varying vec4 cameraPos;\n"
"varying vec4 vertexPos;\n"
"varying mat4 texgen;\n"
"\n"
"void main(void)\n"
"{ \n"
" vec3 t0 = (texgen * vertexPos).xyz;\n"
" vec3 te = (texgen * cameraPos).xyz;\n"
"\n"
" if (te.x>=0.0 && te.x<=1.0 &&\n"
" te.y>=0.0 && te.y<=1.0 &&\n"
" te.z>=0.0 && te.z<=1.0)\n"
" {\n"
" // do nothing... te inside volume\n"
" }\n"
" else\n"
" {\n"
" if (te.x<0.0)\n"
" {\n"
" float r = -te.x / (t0.x-te.x);\n"
" te = te + (t0-te)*r;\n"
" }\n"
"\n"
" if (te.x>1.0)\n"
" {\n"
" float r = (1.0-te.x) / (t0.x-te.x);\n"
" te = te + (t0-te)*r;\n"
" }\n"
"\n"
" if (te.y<0.0)\n"
" {\n"
" float r = -te.y / (t0.y-te.y);\n"
" te = te + (t0-te)*r;\n"
" }\n"
"\n"
" if (te.y>1.0)\n"
" {\n"
" float r = (1.0-te.y) / (t0.y-te.y);\n"
" te = te + (t0-te)*r;\n"
" }\n"
"\n"
" if (te.z<0.0)\n"
" {\n"
" float r = -te.z / (t0.z-te.z);\n"
" te = te + (t0-te)*r;\n"
" }\n"
"\n"
" if (te.z>1.0)\n"
" {\n"
" float r = (1.0-te.z) / (t0.z-te.z);\n"
" te = te + (t0-te)*r;\n"
" }\n"
" }\n"
"\n"
" const float max_iteratrions = 2048.0;\n"
" float num_iterations = ceil(length(te-t0)/sampleDensity);\n"
" if (num_iterations<2.0) num_iterations = 2.0;\n"
" \n"
" if (num_iterations>max_iteratrions) \n"
" {\n"
" num_iterations = max_iteratrions;\n"
" }\n"
"\n"
" vec3 deltaTexCoord=(te-t0)/float(num_iterations-1.0);\n"
" vec3 texcoord = t0;\n"
"\n"
" vec4 fragColor = vec4(0.0, 0.0, 0.0, 0.0); \n"
" while(num_iterations>0.0)\n"
" {\n"
" float v = texture3D( baseTexture, texcoord).s;\n"
" vec4 color = texture1D( tfTexture, v);\n"
" if (fragColor.w<color.w)\n"
" {\n"
" fragColor = color;\n"
" }\n"
" texcoord += deltaTexCoord; \n"
"\n"
" --num_iterations;\n"
" }\n"
"\n"
" fragColor.w *= transparency;\n"
"\n"
" if (fragColor.w>1.0) fragColor.w = 1.0; \n"
" if (fragColor.w<alphaCutOff) discard;\n"
" gl_FragColor = fragColor;\n"
"}\n"
"\n";

View File

@ -0,0 +1,107 @@
char volume_tf_n_frag[] = "uniform sampler3D baseTexture;\n"
"uniform sampler3D normalMap;\n"
"uniform sampler1D tfTexture;\n"
"uniform float sampleDensity;\n"
"uniform float transparency;\n"
"uniform float alphaCutOff;\n"
"\n"
"varying vec4 cameraPos;\n"
"varying vec4 vertexPos;\n"
"varying mat4 texgen;\n"
"\n"
"void main(void)\n"
"{ \n"
" vec3 t0 = (texgen * vertexPos).xyz;\n"
" vec3 te = (texgen * cameraPos).xyz;\n"
"\n"
" vec3 eyeDirection = normalize(te-t0);\n"
"\n"
" if (te.x>=0.0 && te.x<=1.0 &&\n"
" te.y>=0.0 && te.y<=1.0 &&\n"
" te.z>=0.0 && te.z<=1.0)\n"
" {\n"
" // do nothing... te inside volume\n"
" }\n"
" else\n"
" {\n"
" if (te.x<0.0)\n"
" {\n"
" float r = -te.x / (t0.x-te.x);\n"
" te = te + (t0-te)*r;\n"
" }\n"
"\n"
" if (te.x>1.0)\n"
" {\n"
" float r = (1.0-te.x) / (t0.x-te.x);\n"
" te = te + (t0-te)*r;\n"
" }\n"
"\n"
" if (te.y<0.0)\n"
" {\n"
" float r = -te.y / (t0.y-te.y);\n"
" te = te + (t0-te)*r;\n"
" }\n"
"\n"
" if (te.y>1.0)\n"
" {\n"
" float r = (1.0-te.y) / (t0.y-te.y);\n"
" te = te + (t0-te)*r;\n"
" }\n"
"\n"
" if (te.z<0.0)\n"
" {\n"
" float r = -te.z / (t0.z-te.z);\n"
" te = te + (t0-te)*r;\n"
" }\n"
"\n"
" if (te.z>1.0)\n"
" {\n"
" float r = (1.0-te.z) / (t0.z-te.z);\n"
" te = te + (t0-te)*r;\n"
" }\n"
" }\n"
"\n"
" const float max_iteratrions = 2048.0;\n"
" float num_iterations = ceil(length(te-t0)/sampleDensity);\n"
" if (num_iterations<2.0) num_iterations = 2.0;\n"
" \n"
" if (num_iterations>max_iteratrions) \n"
" {\n"
" num_iterations = max_iteratrions;\n"
" }\n"
"\n"
" vec3 deltaTexCoord=(te-t0)/float(num_iterations-1.0);\n"
" vec3 texcoord = t0;\n"
"\n"
" vec4 fragColor = vec4(0.0, 0.0, 0.0, 0.0); \n"
" while(num_iterations>0.0)\n"
" {\n"
" vec4 normal = texture3D( normalMap, texcoord);\n"
" float v = normal.a; // texture3D( baseTexture, texcoord).s;\n"
" vec4 color = texture1D( tfTexture, v);\n"
"\n"
" normal.x = normal.x*2.0-1.0;\n"
" normal.y = normal.y*2.0-1.0;\n"
" normal.z = normal.z*2.0-1.0;\n"
" \n"
" float lightScale = 0.1 + max(dot(normal.xyz, eyeDirection), 0.0);\n"
" color.x *= lightScale;\n"
" color.y *= lightScale;\n"
" color.z *= lightScale;\n"
"\n"
" float r = normal[3]*transparency;\n"
" if (r>alphaCutOff)\n"
" {\n"
" fragColor.xyz = fragColor.xyz*(1.0-r)+color.xyz*r;\n"
" fragColor.w += r;\n"
" }\n"
" texcoord += deltaTexCoord; \n"
"\n"
" --num_iterations;\n"
" }\n"
"\n"
" if (fragColor.w>1.0) fragColor.w = 1.0; \n"
" if (fragColor.w==0.0) discard;\n"
" gl_FragColor = fragColor;\n"
"}\n"
"\n";

View File

@ -0,0 +1,17 @@
char volume_vert[] = "#version 110\n"
"varying vec4 cameraPos;\n"
"varying vec4 vertexPos;\n"
"varying mat4 texgen;\n"
"\n"
"void main(void)\n"
"{\n"
" gl_Position = ftransform();\n"
"\n"
" cameraPos = gl_ModelViewMatrixInverse*vec4(0,0,0,1);\n"
" vertexPos = gl_Vertex;\n"
"\n"
" texgen = mat4(gl_ObjectPlaneS[0], \n"
" gl_ObjectPlaneT[0],\n"
" gl_ObjectPlaneR[0],\n"
" gl_ObjectPlaneQ[0]);\n"
"}\n";

View File

@ -167,7 +167,14 @@ osg::BoundingSphere VolumeTile::computeBound() const
{
osg::BoundingSphere bs;
osg::notify(osg::NOTICE)<<"TODO VolumeTile::computeBound()"<<std::endl;
for(Layers::const_iterator itr = _layers.begin();
itr != _layers.end();
++itr)
{
if (itr->valid()) bs.expandBy((*itr)->computeBound());
}
return bs;
return bs;
}