Improved the isosurface shaders, and mde GLSL usage the default

This commit is contained in:
Robert Osfield 2008-09-28 15:16:13 +00:00
parent d8cfc9b1f5
commit 38171cc1b9
3 changed files with 106 additions and 58 deletions

View File

@ -921,20 +921,6 @@ osg::Node* createShaderModel(ShadingModel shadingModel,
} }
else if (shadingModel==Isosurface) 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) if (tf)
{ {
@ -944,7 +930,10 @@ osg::Node* createShaderModel(ShadingModel shadingModel,
texture1D->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR); texture1D->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR);
texture1D->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR); texture1D->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR);
texture1D->setWrap(osg::Texture::WRAP_R,osg::Texture::CLAMP_TO_EDGE); 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"); osg::Shader* fragmentShader = osgDB::readShaderFile(osg::Shader::FRAGMENT, "volume_tf_iso.frag");
if (fragmentShader) if (fragmentShader)
@ -956,10 +945,6 @@ osg::Node* createShaderModel(ShadingModel shadingModel,
#include "volume_tf_iso_frag.cpp" #include "volume_tf_iso_frag.cpp"
program->addShader(new osg::Shader(osg::Shader::FRAGMENT, volume_tf_iso_frag)); program->addShader(new osg::Shader(osg::Shader::FRAGMENT, volume_tf_iso_frag));
} }
osg::Uniform* tfTextureSampler = new osg::Uniform("tfTexture",0);
stateset->addUniform(tfTextureSampler);
} }
else 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("-n","Create normal map for per voxel lighting.");
arguments.getApplicationUsage()->addCommandLineOption("-s <numSlices>","Number of slices to create."); arguments.getApplicationUsage()->addCommandLineOption("-s <numSlices>","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("--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("--mip","Use Maximum Intensity Projection (MIP) filtering.");
arguments.getApplicationUsage()->addCommandLineOption("--xSize <size>","Relative width of rendered brick."); arguments.getApplicationUsage()->addCommandLineOption("--xSize <size>","Relative width of rendered brick.");
arguments.getApplicationUsage()->addCommandLineOption("--ySize <size>","Relative length of rendered brick."); arguments.getApplicationUsage()->addCommandLineOption("--ySize <size>","Relative length of rendered brick.");
@ -1849,7 +1837,6 @@ int main( int argc, char **argv )
while (arguments.read("--isosurface")) while (arguments.read("--isosurface"))
{ {
shadingModel = Isosurface; shadingModel = Isosurface;
createNormalMap=true;
} }
float xSize=1.0f, ySize=1.0f, zSize=1.0f; float xSize=1.0f, ySize=1.0f, zSize=1.0f;
@ -1901,11 +1888,13 @@ int main( int argc, char **argv )
unsigned int numComponentsDesired = 0; unsigned int numComponentsDesired = 0;
while(arguments.read("--num-components", numComponentsDesired)) {} while(arguments.read("--num-components", numComponentsDesired)) {}
bool useShader = false; bool useShader = true;
while(arguments.read("--shader")) { 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("--gpu-tf")) { gpuTransferFunction = true; }
while(arguments.read("--cpu-tf")) { gpuTransferFunction = false; }
osg::ref_ptr<osg::Image> image_3d; osg::ref_ptr<osg::Image> image_3d;

View File

@ -1,5 +1,4 @@
char volume_iso_frag[] = "uniform sampler3D baseTexture;\n" char volume_iso_frag[] = "uniform sampler3D baseTexture;\n"
"uniform sampler3D normalMap;\n"
"uniform float sampleDensity;\n" "uniform float sampleDensity;\n"
"uniform float transparency;\n" "uniform float transparency;\n"
"uniform float alphaCutOff;\n" "uniform float alphaCutOff;\n"
@ -70,29 +69,59 @@ char volume_iso_frag[] = "uniform sampler3D baseTexture;\n"
" num_iterations = max_iteratrions;\n" " num_iterations = max_iteratrions;\n"
" }\n" " }\n"
"\n" "\n"
" vec3 deltaTexCoord=(te-t0)/float(num_iterations-1.0);\n" " vec3 deltaTexCoord=(t0-te)/float(num_iterations-1.0);\n"
" vec3 texcoord = t0;\n" " vec3 texcoord = te;\n"
"\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" " while(num_iterations>0.0)\n"
" {\n" " {\n"
" vec4 normal = texture3D( normalMap, texcoord);\n"
" vec4 color = texture3D( baseTexture, texcoord);\n" " vec4 color = texture3D( baseTexture, texcoord);\n"
"\n" "\n"
" normal.x = normal.x*2.0-1.0;\n" " float m = (previousColor.a-alphaCutOff) * (color.a-alphaCutOff);\n"
" normal.y = normal.y*2.0-1.0;\n" " if (m <= 0.0)\n"
" normal.z = normal.z*2.0-1.0;\n" " {\n"
" float r = (alphaCutOff-color.a)/(previousColor.a-color.a);\n"
" texcoord = texcoord - r*deltaTexCoord;\n"
" \n" " \n"
" float lightScale = 0.1 + max(dot(normal.xyz, eyeDirection), 0.0);\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.x *= lightScale;\n"
" color.y *= lightScale;\n" " color.y *= lightScale;\n"
" color.z *= lightScale;\n" " color.z *= lightScale;\n"
"#else\n"
" color.x = lightScale;\n"
" color.y = lightScale;\n"
" color.z = lightScale;\n"
"#endif\n"
"\n" "\n"
" float r = normal[3];\n"
" if (r>alphaCutOff)\n"
" {\n"
" fragColor = vec4(lightScale, lightScale, lightScale, 1.0);\n" " fragColor = vec4(lightScale, lightScale, lightScale, 1.0);\n"
" \n"
" break;\n"
" }\n" " }\n"
" \n"
" previousColor = color;\n"
" \n"
" texcoord += deltaTexCoord; \n" " texcoord += deltaTexCoord; \n"
"\n" "\n"
" --num_iterations;\n" " --num_iterations;\n"

View File

@ -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 sampler1D tfTexture;\n"
"uniform float sampleDensity;\n" "uniform float sampleDensity;\n"
"uniform float transparency;\n" "uniform float transparency;\n"
@ -69,31 +69,61 @@ char volume_tf_iso_frag[] = "uniform sampler3D normalMap;\n"
" num_iterations = max_iteratrions;\n" " num_iterations = max_iteratrions;\n"
" }\n" " }\n"
"\n" "\n"
" vec3 deltaTexCoord=(te-t0)/float(num_iterations-1.0);\n" " vec3 deltaTexCoord=(t0-te)/float(num_iterations-1.0);\n"
" vec3 texcoord = t0;\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" "\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"
" while(num_iterations>0.0)\n" " while(num_iterations>0.0)\n"
" {\n" " {\n"
" vec4 normal = texture3D( normalMap, texcoord);\n" "\n"
" float v = normal.w;\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" " vec4 color = texture1D( tfTexture, v);\n"
"\n" "\n"
" normal.x = normal.x*2.0-1.0;\n" " float px = texture3D( baseTexture, texcoord + deltaX).a;\n"
" normal.y = normal.y*2.0-1.0;\n" " float py = texture3D( baseTexture, texcoord + deltaY).a;\n"
" normal.z = normal.z*2.0-1.0;\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" " \n"
" float lightScale = 0.1 + max(dot(normal.xyz, eyeDirection), 0.0);\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.x *= lightScale;\n"
" color.y *= lightScale;\n" " color.y *= lightScale;\n"
" color.z *= lightScale;\n" " color.z *= lightScale;\n"
" color.w = 1.0;\n" "#else\n"
" \n" " color.x = lightScale;\n"
" if (v>=alphaCutOff)\n" " color.y = lightScale;\n"
" {\n" " color.z = lightScale;\n"
" fragColor = color;\n" "#endif\n"
" }\n"
"\n" "\n"
" fragColor = color;\n"
" \n"
" break;\n"
" }\n"
" \n"
" previousV = v;\n"
" \n"
" texcoord += deltaTexCoord; \n" " texcoord += deltaTexCoord; \n"
"\n" "\n"
" --num_iterations;\n" " --num_iterations;\n"