Updated iso surface shaders

This commit is contained in:
Robert Osfield
2009-01-30 17:10:27 +00:00
parent cbaf9d639c
commit a358bd7370
3 changed files with 107 additions and 104 deletions

View File

@@ -131,7 +131,7 @@ void RayTracedTechnique::init()
// get shaders from source
osg::Shader* vertexShader = osgDB::readShaderFile(osg::Shader::VERTEX, "volume.vert");
osg::Shader* vertexShader = osgDB::readShaderFile(osg::Shader::VERTEX, "shaders/volume.vert");
if (vertexShader)
{
program->addShader(vertexShader);
@@ -181,7 +181,7 @@ void RayTracedTechnique::init()
texture1D->setImage(tf->getImage());
stateset->setTextureAttributeAndModes(1,texture1D,osg::StateAttribute::ON);
osg::Shader* fragmentShader = osgDB::readShaderFile(osg::Shader::FRAGMENT, "volume_tf_mip.frag");
osg::Shader* fragmentShader = osgDB::readShaderFile(osg::Shader::FRAGMENT, "shaders/volume_tf_mip.frag");
if (fragmentShader)
{
program->addShader(fragmentShader);
@@ -198,7 +198,7 @@ void RayTracedTechnique::init()
}
else
{
osg::Shader* fragmentShader = osgDB::readShaderFile(osg::Shader::FRAGMENT, "volume_mip.frag");
osg::Shader* fragmentShader = osgDB::readShaderFile(osg::Shader::FRAGMENT, "shaders/volume_mip.frag");
if (fragmentShader)
{
program->addShader(fragmentShader);
@@ -228,7 +228,7 @@ void RayTracedTechnique::init()
osg::Uniform* tfTextureSampler = new osg::Uniform("tfTexture",1);
stateset->addUniform(tfTextureSampler);
osg::Shader* fragmentShader = osgDB::readShaderFile(osg::Shader::FRAGMENT, "volume_tf_iso.frag");
osg::Shader* fragmentShader = osgDB::readShaderFile(osg::Shader::FRAGMENT, "shaders/volume_tf_iso.frag");
if (fragmentShader)
{
program->addShader(fragmentShader);
@@ -241,13 +241,17 @@ void RayTracedTechnique::init()
}
else
{
osg::Shader* fragmentShader = osgDB::readShaderFile(osg::Shader::FRAGMENT, "volume_iso.frag");
osg::Shader* fragmentShader = osgDB::readShaderFile(osg::Shader::FRAGMENT, "shaders/volume_iso.frag");
if (fragmentShader)
{
osg::notify(osg::NOTICE)<<"Shader found"<<std::endl;
program->addShader(fragmentShader);
}
else
{
osg::notify(osg::NOTICE)<<"No Shader found"<<std::endl;
#include "Shaders/volume_iso_frag.cpp"
program->addShader(new osg::Shader(osg::Shader::FRAGMENT, volume_iso_frag));
}
@@ -312,7 +316,7 @@ void RayTracedTechnique::init()
osg::Uniform* tfTextureSampler = new osg::Uniform("tfTexture",1);
stateset->addUniform(tfTextureSampler);
osg::Shader* fragmentShader = osgDB::readShaderFile(osg::Shader::FRAGMENT, "volume_tf.frag");
osg::Shader* fragmentShader = osgDB::readShaderFile(osg::Shader::FRAGMENT, "shaders/volume_tf.frag");
if (fragmentShader)
{
program->addShader(fragmentShader);
@@ -327,7 +331,7 @@ void RayTracedTechnique::init()
else
{
osg::Shader* fragmentShader = osgDB::readShaderFile(osg::Shader::FRAGMENT, "volume.frag");
osg::Shader* fragmentShader = osgDB::readShaderFile(osg::Shader::FRAGMENT, "shaders/volume.frag");
if (fragmentShader)
{
program->addShader(fragmentShader);

View File

@@ -132,7 +132,5 @@ char volume_iso_frag[] = "uniform sampler3D baseTexture;\n"
"\n"
" // we didn't find an intersection so just discard fragment\n"
" discard;\n"
"\n"
"}\n"
";\n"
"\n";

View File

@@ -10,120 +10,121 @@ char volume_tf_iso_frag[] = "uniform sampler3D baseTexture;\n"
"\n"
"void main(void)\n"
"{ \n"
" vec3 t0 = (texgen * vertexPos).xyz;\n"
" vec3 te = (texgen * cameraPos).xyz;\n"
" vec3 t0 = (texgen * vertexPos).xyz;\n"
" vec3 te = (texgen * cameraPos).xyz;\n"
"\n"
" vec3 eyeDirection = normalize(te-t0);\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"
" 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"
" 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"
" 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"
" 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"
" 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"
" 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)/SampleDensityValue);\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"
" const float max_iteratrions = 2048.0;\n"
" float num_iterations = ceil(length(te-t0)/SampleDensityValue);\n"
" if (num_iterations<2.0) num_iterations = 2.0;\n"
"\n"
" vec3 deltaTexCoord=(t0-te)/float(num_iterations-1.0);\n"
" vec3 texcoord = te;\n"
" float previousV = texture3D( baseTexture, texcoord).a;\n"
" if (num_iterations>max_iteratrions) \n"
" {\n"
" num_iterations = max_iteratrions;\n"
" }\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"
" vec3 deltaTexCoord=(t0-te)/float(num_iterations-1.0);\n"
" vec3 texcoord = te;\n"
" float previousV = texture3D( baseTexture, texcoord).a;\n"
"\n"
" vec4 fragColor = vec4(0.0, 0.0, 0.0, 0.0); \n"
" while(num_iterations>0.0)\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"
" float v = texture3D( baseTexture, texcoord).a;\n"
" while(num_iterations>0.0)\n"
" {\n"
"\n"
" float m = (previousV-IsoSurfaceValue) * (v-IsoSurfaceValue);\n"
" if (m <= 0.0)\n"
" {\n"
" float r = (IsoSurfaceValue-v)/(previousV-v);\n"
" texcoord = texcoord - r*deltaTexCoord;\n"
" float v = texture3D( baseTexture, texcoord).a;\n"
"\n"
" v = texture3D( baseTexture, texcoord).a;\n"
" vec4 color = texture1D( tfTexture, v);\n"
" float m = (previousV-IsoSurfaceValue) * (v-IsoSurfaceValue);\n"
" if (m <= 0.0)\n"
" {\n"
" float r = (IsoSurfaceValue-v)/(previousV-v);\n"
" texcoord = texcoord - r*deltaTexCoord;\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"
" v = texture3D( baseTexture, texcoord).a;\n"
" vec4 color = texture1D( tfTexture, v);\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"
" 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 lightScale = 0.1 + abs(dot(normal.xyz, eyeDirection))*0.9;\n"
" \n"
" color.x *= lightScale;\n"
" color.y *= lightScale;\n"
" color.z *= lightScale;\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"
" fragColor = color;\n"
" \n"
" break;\n"
" }\n"
" \n"
" previousV = v;\n"
" \n"
" texcoord += deltaTexCoord; \n"
" vec3 grad = vec3(px-nx, py-ny, pz-nz);\n"
" if (grad.x!=0.0 || grad.y!=0.0 || grad.z!=0.0)\n"
" {\n"
" vec3 normal = normalize(grad);\n"
" float lightScale = 0.1 + abs(dot(normal.xyz, eyeDirection))*0.9;\n"
"\n"
" --num_iterations;\n"
" }\n"
" color.x *= lightScale;\n"
" color.y *= lightScale;\n"
" color.z *= lightScale;\n"
" }\n"
"\n"
" gl_FragColor = color;\n"
"\n"
" return;\n"
" }\n"
"\n"
" previousV = v;\n"
"\n"
" texcoord += deltaTexCoord; \n"
"\n"
" --num_iterations;\n"
" }\n"
"\n"
" // we didn't find an intersection so just discard fragment\n"
" discard;\n"
"\n"
" if (fragColor.w>1.0) fragColor.w = 1.0; \n"
" if (fragColor.w<IsoSurfaceValue) discard;\n"
" gl_FragColor = fragColor;\n"
"}\n"
"\n";