Added shader based transfer function, enabled via --gpu-tf
This commit is contained in:
@@ -20,6 +20,7 @@
|
||||
#include <osg/Geometry>
|
||||
#include <osg/Notify>
|
||||
#include <osg/Texture3D>
|
||||
#include <osg/Texture1D>
|
||||
#include <osg/TexGen>
|
||||
#include <osg/Geode>
|
||||
#include <osg/Billboard>
|
||||
@@ -687,7 +688,9 @@ class FollowMouseCallback : public osgGA::GUIEventHandler, public osg::StateSet:
|
||||
|
||||
};
|
||||
|
||||
osg::Node* createShaderModel(osg::ref_ptr<osg::Image>& image_3d, osg::ref_ptr<osg::Image>& /*normalmap_3d*/,
|
||||
osg::Node* createShaderModel(osg::ref_ptr<osg::Image>& image_3d,
|
||||
osg::ref_ptr<osg::Image>& /*normalmap_3d*/,
|
||||
osg::TransferFunction1D* tf,
|
||||
osg::Texture::InternalFormatMode internalFormatMode,
|
||||
float xSize, float ySize, float zSize,
|
||||
float /*xMultiplier*/, float /*yMultiplier*/, float /*zMultiplier*/,
|
||||
@@ -730,6 +733,13 @@ osg::Node* createShaderModel(osg::ref_ptr<osg::Image>& image_3d, osg::ref_ptr<os
|
||||
texture3D->setImage(image_3d.get());
|
||||
|
||||
stateset->setTextureAttributeAndModes(0,texture3D,osg::StateAttribute::ON);
|
||||
|
||||
if (tf)
|
||||
{
|
||||
osg::Texture1D* texture1D = new osg::Texture1D;
|
||||
texture1D->setImage(tf->getImage());
|
||||
stateset->setTextureAttributeAndModes(1,texture1D,osg::StateAttribute::ON);
|
||||
}
|
||||
|
||||
osg::Program* program = new osg::Program;
|
||||
stateset->setAttribute(program);
|
||||
@@ -766,110 +776,224 @@ osg::Node* createShaderModel(osg::ref_ptr<osg::Image>& image_3d, osg::ref_ptr<os
|
||||
|
||||
}
|
||||
|
||||
std::string fragmentShaderFile = osgDB::findDataFile("volume.frag");
|
||||
if (!fragmentShaderFile.empty())
|
||||
if (tf)
|
||||
{
|
||||
program->addShader(osg::Shader::readShaderFile(osg::Shader::FRAGMENT, fragmentShaderFile));
|
||||
std::string fragmentShaderFile = osgDB::findDataFile("volume-tf.frag");
|
||||
if (!fragmentShaderFile.empty())
|
||||
{
|
||||
program->addShader(osg::Shader::readShaderFile(osg::Shader::FRAGMENT, fragmentShaderFile));
|
||||
}
|
||||
else
|
||||
{
|
||||
//////////////////////////////////////////////////////////////////
|
||||
// fragment shader
|
||||
//
|
||||
char fragmentShaderSource[] =
|
||||
"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 = length(te-t0)/sampleDensity;\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"
|
||||
" 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"
|
||||
" 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";
|
||||
|
||||
osg::Shader* fragment_shader = new osg::Shader(osg::Shader::FRAGMENT, fragmentShaderSource);
|
||||
program->addShader(fragment_shader);
|
||||
}
|
||||
|
||||
osg::Uniform* tfTextureSampler = new osg::Uniform("tfTexture",1);
|
||||
stateset->addUniform(tfTextureSampler);
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
//////////////////////////////////////////////////////////////////
|
||||
// fragment shader
|
||||
//
|
||||
char fragmentShaderSource[] =
|
||||
"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 = length(te-t0)/sampleDensity;\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"
|
||||
" 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";
|
||||
{
|
||||
std::string fragmentShaderFile = osgDB::findDataFile("volume.frag");
|
||||
if (!fragmentShaderFile.empty())
|
||||
{
|
||||
program->addShader(osg::Shader::readShaderFile(osg::Shader::FRAGMENT, fragmentShaderFile));
|
||||
}
|
||||
else
|
||||
{
|
||||
//////////////////////////////////////////////////////////////////
|
||||
// fragment shader
|
||||
//
|
||||
char fragmentShaderSource[] =
|
||||
"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 = length(te-t0)/sampleDensity;\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"
|
||||
" 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";
|
||||
|
||||
osg::Shader* fragment_shader = new osg::Shader(osg::Shader::FRAGMENT, fragmentShaderSource);
|
||||
program->addShader(fragment_shader);
|
||||
osg::Shader* fragment_shader = new osg::Shader(osg::Shader::FRAGMENT, fragmentShaderSource);
|
||||
program->addShader(fragment_shader);
|
||||
}
|
||||
}
|
||||
|
||||
osg::Uniform* baseTextureSampler = new osg::Uniform("baseTexture",0);
|
||||
stateset->addUniform(baseTextureSampler);
|
||||
|
||||
@@ -1688,6 +1812,9 @@ int main( int argc, char **argv )
|
||||
bool useShader = false;
|
||||
while(arguments.read("--shader")) { useShader = true; }
|
||||
|
||||
bool gpuTransferFunction = false;
|
||||
while(arguments.read("--gpu-tf")) { gpuTransferFunction = true; }
|
||||
|
||||
osg::ref_ptr<osg::Image> image_3d;
|
||||
|
||||
std::string vh_filename;
|
||||
@@ -1902,7 +2029,7 @@ int main( int argc, char **argv )
|
||||
doColourSpaceConversion(colourSpaceOperation, image_3d.get(), colourModulate);
|
||||
}
|
||||
|
||||
if (transferFunction.valid())
|
||||
if (!gpuTransferFunction && transferFunction.valid())
|
||||
{
|
||||
image_3d = applyTransferFunction(image_3d.get(), transferFunction.get());
|
||||
}
|
||||
@@ -1917,6 +2044,7 @@ int main( int argc, char **argv )
|
||||
if (useShader)
|
||||
{
|
||||
rootNode = createShaderModel(image_3d, normalmap_3d,
|
||||
(gpuTransferFunction ? transferFunction.get() : 0),
|
||||
internalFormatMode,
|
||||
xSize, ySize, zSize,
|
||||
xMultiplier, yMultiplier, zMultiplier,
|
||||
|
||||
Reference in New Issue
Block a user