From bda89b113eb1823453cfa8d10d63b7bf740f4ab4 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Tue, 20 Jan 2009 15:29:32 +0000 Subject: [PATCH] Added lighting based shaders --- src/osgVolume/ShaderTechnique.cpp | 101 +++++++++++---- src/osgVolume/Shaders/volume_lit.cpp | 99 +++++++++++++++ src/osgVolume/Shaders/volume_lit_frag.cpp | 122 ++++++++++++++++++ src/osgVolume/Shaders/volume_lit_tf.cpp | 102 +++++++++++++++ src/osgVolume/Shaders/volume_lit_tf_frag.cpp | 127 +++++++++++++++++++ 5 files changed, 523 insertions(+), 28 deletions(-) create mode 100644 src/osgVolume/Shaders/volume_lit.cpp create mode 100644 src/osgVolume/Shaders/volume_lit_frag.cpp create mode 100644 src/osgVolume/Shaders/volume_lit_tf.cpp create mode 100644 src/osgVolume/Shaders/volume_lit_tf_frag.cpp diff --git a/src/osgVolume/ShaderTechnique.cpp b/src/osgVolume/ShaderTechnique.cpp index 6f33751df..7a9ac4445 100644 --- a/src/osgVolume/ShaderTechnique.cpp +++ b/src/osgVolume/ShaderTechnique.cpp @@ -254,46 +254,91 @@ void ShaderTechnique::init() } } } - else if (tf) + else if (shadingModel==Light) { - osg::Texture1D* texture1D = new osg::Texture1D; - texture1D->setImage(tf->getImage()); - texture1D->setResizeNonPowerOfTwoHint(false); - texture1D->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR); - texture1D->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR); - texture1D->setWrap(osg::Texture::WRAP_R,osg::Texture::CLAMP_TO_EDGE); - stateset->setTextureAttributeAndModes(1,texture1D,osg::StateAttribute::ON); - - osg::Uniform* tfTextureSampler = new osg::Uniform("tfTexture",1); - stateset->addUniform(tfTextureSampler); - - osg::Shader* fragmentShader = osgDB::readShaderFile(osg::Shader::FRAGMENT, "volume-tf.frag"); - if (fragmentShader) + if (tf) { - program->addShader(fragmentShader); + osg::Texture1D* texture1D = new osg::Texture1D; + texture1D->setImage(tf->getImage()); + texture1D->setResizeNonPowerOfTwoHint(false); + texture1D->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR); + texture1D->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR); + texture1D->setWrap(osg::Texture::WRAP_R,osg::Texture::CLAMP_TO_EDGE); + stateset->setTextureAttributeAndModes(1,texture1D,osg::StateAttribute::ON); + + osg::Uniform* tfTextureSampler = new osg::Uniform("tfTexture",1); + stateset->addUniform(tfTextureSampler); + + osg::Shader* fragmentShader = osgDB::readShaderFile(osg::Shader::FRAGMENT, "shaders/volume_lit_tf.frag"); + if (fragmentShader) + { + program->addShader(fragmentShader); + } + else + { + #include "Shaders/volume_lit_tf_frag.cpp" + program->addShader(new osg::Shader(osg::Shader::FRAGMENT, volume_lit_tf_frag)); + } + } else - { - #include "Shaders/volume_tf_frag.cpp" - program->addShader(new osg::Shader(osg::Shader::FRAGMENT, volume_tf_frag)); - } + { + osg::Shader* fragmentShader = osgDB::readShaderFile(osg::Shader::FRAGMENT, "shaders/volume_lit.frag"); + if (fragmentShader) + { + program->addShader(fragmentShader); + } + else + { + #include "Shaders/volume_lit_frag.cpp" + program->addShader(new osg::Shader(osg::Shader::FRAGMENT, volume_lit_frag)); + } + } } else - { - - osg::Shader* fragmentShader = osgDB::readShaderFile(osg::Shader::FRAGMENT, "volume.frag"); - if (fragmentShader) + { + if (tf) { - program->addShader(fragmentShader); + osg::Texture1D* texture1D = new osg::Texture1D; + texture1D->setImage(tf->getImage()); + texture1D->setResizeNonPowerOfTwoHint(false); + texture1D->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR); + texture1D->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR); + texture1D->setWrap(osg::Texture::WRAP_R,osg::Texture::CLAMP_TO_EDGE); + stateset->setTextureAttributeAndModes(1,texture1D,osg::StateAttribute::ON); + + osg::Uniform* tfTextureSampler = new osg::Uniform("tfTexture",1); + stateset->addUniform(tfTextureSampler); + + osg::Shader* fragmentShader = osgDB::readShaderFile(osg::Shader::FRAGMENT, "volume_tf.frag"); + if (fragmentShader) + { + program->addShader(fragmentShader); + } + else + { + #include "Shaders/volume_tf_frag.cpp" + program->addShader(new osg::Shader(osg::Shader::FRAGMENT, volume_tf_frag)); + } + } else - { - #include "Shaders/volume_frag.cpp" - program->addShader(new osg::Shader(osg::Shader::FRAGMENT, volume_frag)); + { + + osg::Shader* fragmentShader = osgDB::readShaderFile(osg::Shader::FRAGMENT, "volume.frag"); + if (fragmentShader) + { + program->addShader(fragmentShader); + } + else + { + #include "Shaders/volume_frag.cpp" + program->addShader(new osg::Shader(osg::Shader::FRAGMENT, volume_frag)); + } } } - + if (cpv._sampleDensityProperty.valid()) stateset->addUniform(cpv._sampleDensityProperty->getUniform()); else diff --git a/src/osgVolume/Shaders/volume_lit.cpp b/src/osgVolume/Shaders/volume_lit.cpp new file mode 100644 index 000000000..1cc4cfb90 --- /dev/null +++ b/src/osgVolume/Shaders/volume_lit.cpp @@ -0,0 +1,99 @@ +char volume_frag[] = "uniform sampler3D baseTexture;\n" + "uniform float SampleDensityValue;\n" + "uniform float TransparencyValue;\n" + "uniform float AlphaFuncValue;\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)/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" + "\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]*TransparencyValue;\n" + " if (r>AlphaFuncValue)\n" + " {\n" + " fragColor.xyz = fragColor.xyz*(1.0-r)+color.xyz*r;\n" + " fragColor.w += r;\n" + " }\n" + "\n" + " if (fragColor.w1.0) fragColor.w = 1.0; \n" + " if (fragColor.w=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)/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" + "\n" + " vec3 deltaTexCoord=(te-t0)/float(num_iterations-1.0);\n" + " vec3 texcoord = t0;\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" + " vec4 color = texture3D( baseTexture, texcoord);\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));\n" + "\n" + " color.x *= lightScale;\n" + " color.y *= lightScale;\n" + " color.z *= lightScale;\n" + "\n" + " float r = color[3]*TransparencyValue;\n" + " if (r>AlphaFuncValue)\n" + " {\n" + " fragColor.xyz = fragColor.xyz*(1.0-r)+color.xyz*r;\n" + " fragColor.w += r;\n" + " }\n" + "\n" + " if (fragColor.w1.0) fragColor.w = 1.0; \n" + " if (fragColor.w=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)/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" + "\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]*TransparencyValue;\n" + " if (r>AlphaFuncValue)\n" + " {\n" + " fragColor.xyz = fragColor.xyz*(1.0-r)+color.xyz*r;\n" + " fragColor.w += r;\n" + " }\n" + "\n" + " if (fragColor.w1.0) fragColor.w = 1.0; \n" + " if (fragColor.w=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)/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" + "\n" + " vec3 deltaTexCoord=(te-t0)/float(num_iterations-1.0);\n" + " vec3 texcoord = t0;\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" + " float v = texture3D( baseTexture, texcoord).a;\n" + " vec4 color = texture1D( tfTexture, v);\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));\n" + "\n" + " color.x *= lightScale;\n" + " color.y *= lightScale;\n" + " color.z *= lightScale;\n" + "\n" + " float r = color[3]*TransparencyValue;\n" + " if (r>AlphaFuncValue)\n" + " {\n" + " fragColor.xyz = fragColor.xyz*(1.0-r)+color.xyz*r;\n" + " fragColor.w += r;\n" + " }\n" + "\n" + " if (fragColor.w1.0) fragColor.w = 1.0; \n" + " if (fragColor.w