diff --git a/examples/osgvolume/osgvolume.cpp b/examples/osgvolume/osgvolume.cpp index b34ccc72c..2275a2ff9 100644 --- a/examples/osgvolume/osgvolume.cpp +++ b/examples/osgvolume/osgvolume.cpp @@ -921,20 +921,6 @@ osg::Node* createShaderModel(ShadingModel shadingModel, } else if (shadingModel==Isosurface) { - osg::Uniform* normalMapSampler = new osg::Uniform("normalMap",1); - stateset->addUniform(normalMapSampler); - - osg::Texture3D* normalMap = new osg::Texture3D; - normalMap->setImage(normalmap_3d); - normalMap->setResizeNonPowerOfTwoHint(false); - normalMap->setInternalFormatMode(internalFormatMode); - normalMap->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR); - normalMap->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR); - normalMap->setWrap(osg::Texture::WRAP_R,osg::Texture::CLAMP_TO_EDGE); - normalMap->setWrap(osg::Texture::WRAP_S,osg::Texture::CLAMP_TO_EDGE); - normalMap->setWrap(osg::Texture::WRAP_T,osg::Texture::CLAMP_TO_EDGE); - - stateset->setTextureAttributeAndModes(1,normalMap,osg::StateAttribute::ON); if (tf) { @@ -944,7 +930,10 @@ osg::Node* createShaderModel(ShadingModel shadingModel, 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(0,texture1D,osg::StateAttribute::ON); + 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_iso.frag"); if (fragmentShader) @@ -956,10 +945,6 @@ osg::Node* createShaderModel(ShadingModel shadingModel, #include "volume_tf_iso_frag.cpp" program->addShader(new osg::Shader(osg::Shader::FRAGMENT, volume_tf_iso_frag)); } - - osg::Uniform* tfTextureSampler = new osg::Uniform("tfTexture",0); - stateset->addUniform(tfTextureSampler); - } else { @@ -1769,7 +1754,10 @@ int main( int argc, char **argv ) arguments.getApplicationUsage()->addCommandLineOption("-n","Create normal map for per voxel lighting."); arguments.getApplicationUsage()->addCommandLineOption("-s ","Number of slices to create."); arguments.getApplicationUsage()->addCommandLineOption("--images [filenames]","Specify a stack of 2d images to build the 3d volume from."); - arguments.getApplicationUsage()->addCommandLineOption("--shader","Use OpenGL Shading Language."); + arguments.getApplicationUsage()->addCommandLineOption("--shader","Use OpenGL Shading Language. (default)"); + arguments.getApplicationUsage()->addCommandLineOption("--no-shader","Disable use of OpenGL Shading Language."); + arguments.getApplicationUsage()->addCommandLineOption("--gpu-tf","Aply the transfer function on the GPU. (default)"); + arguments.getApplicationUsage()->addCommandLineOption("--cpu-tf","Apply the transfer function on the CPU."); arguments.getApplicationUsage()->addCommandLineOption("--mip","Use Maximum Intensity Projection (MIP) filtering."); arguments.getApplicationUsage()->addCommandLineOption("--xSize ","Relative width of rendered brick."); arguments.getApplicationUsage()->addCommandLineOption("--ySize ","Relative length of rendered brick."); @@ -1849,7 +1837,6 @@ int main( int argc, char **argv ) while (arguments.read("--isosurface")) { shadingModel = Isosurface; - createNormalMap=true; } float xSize=1.0f, ySize=1.0f, zSize=1.0f; @@ -1901,11 +1888,13 @@ int main( int argc, char **argv ) unsigned int numComponentsDesired = 0; while(arguments.read("--num-components", numComponentsDesired)) {} - bool useShader = false; + bool useShader = true; while(arguments.read("--shader")) { useShader = true; } + while(arguments.read("--no-shader")) { useShader = true; } - bool gpuTransferFunction = false; + bool gpuTransferFunction = true; while(arguments.read("--gpu-tf")) { gpuTransferFunction = true; } + while(arguments.read("--cpu-tf")) { gpuTransferFunction = false; } osg::ref_ptr image_3d; diff --git a/examples/osgvolume/volume_iso_frag.cpp b/examples/osgvolume/volume_iso_frag.cpp index 9268bb96c..afa32401d 100644 --- a/examples/osgvolume/volume_iso_frag.cpp +++ b/examples/osgvolume/volume_iso_frag.cpp @@ -1,5 +1,4 @@ char volume_iso_frag[] = "uniform sampler3D baseTexture;\n" - "uniform sampler3D normalMap;\n" "uniform float sampleDensity;\n" "uniform float transparency;\n" "uniform float alphaCutOff;\n" @@ -70,29 +69,59 @@ char volume_iso_frag[] = "uniform sampler3D baseTexture;\n" " num_iterations = max_iteratrions;\n" " }\n" "\n" - " vec3 deltaTexCoord=(te-t0)/float(num_iterations-1.0);\n" - " vec3 texcoord = t0;\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 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 normal = texture3D( normalMap, texcoord);\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];\n" - " if (r>alphaCutOff)\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));\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" diff --git a/examples/osgvolume/volume_tf_iso_frag.cpp b/examples/osgvolume/volume_tf_iso_frag.cpp index 3453f03d4..725726156 100644 --- a/examples/osgvolume/volume_tf_iso_frag.cpp +++ b/examples/osgvolume/volume_tf_iso_frag.cpp @@ -1,4 +1,4 @@ -char volume_tf_iso_frag[] = "uniform sampler3D normalMap;\n" +char volume_tf_iso_frag[] = "uniform sampler3D baseTexture;\n" "uniform sampler1D tfTexture;\n" "uniform float sampleDensity;\n" "uniform float transparency;\n" @@ -69,31 +69,61 @@ char volume_tf_iso_frag[] = "uniform sampler3D normalMap;\n" " num_iterations = max_iteratrions;\n" " }\n" "\n" - " vec3 deltaTexCoord=(te-t0)/float(num_iterations-1.0);\n" - " vec3 texcoord = t0;\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" - " vec4 normal = texture3D( normalMap, texcoord);\n" - " float v = normal.w;\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" - " color.w = 1.0;\n" - " \n" - " if (v>=alphaCutOff)\n" + " float v = texture3D( baseTexture, texcoord).a;\n" + "\n" + " float m = (previousV-alphaCutOff) * (v-alphaCutOff);\n" + " if (m <= 0.0)\n" " {\n" - " fragColor = color;\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));\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 = color;\n" + " \n" + " break;\n" + " }\n" + " \n" + " previousV = v;\n" + " \n" " texcoord += deltaTexCoord; \n" "\n" " --num_iterations;\n"