| | 642 | osg::Node* createShaderModel(osg::ref_ptr<osg::Image>& image_3d, osg::ref_ptr<osg::Image>& normalmap_3d, |
| | 643 | osg::Texture::InternalFormatMode internalFormatMode, |
| | 644 | float xSize, float ySize, float zSize, |
| | 645 | float xMultiplier, float yMultiplier, float zMultiplier, |
| | 646 | unsigned int numSlices=500, float sliceEnd=1.0f, float alphaFuncValue=0.02f) |
| | 647 | { |
| | 648 | osg::Geode* geode = new osg::Geode; |
| | 649 | osg::StateSet* stateset = geode->getOrCreateStateSet(); |
| | 650 | |
| | 651 | // set up the 3d texture itself, |
| | 652 | // note, well set the filtering up so that mip mapping is disabled, |
| | 653 | // gluBuild3DMipsmaps doesn't do a very good job of handled the |
| | 654 | // inbalanced dimensions of the 256x256x4 texture. |
| | 655 | osg::Texture3D* texture3D = new osg::Texture3D; |
| | 656 | texture3D->setFilter(osg::Texture3D::MIN_FILTER,osg::Texture3D::LINEAR); |
| | 657 | texture3D->setFilter(osg::Texture3D::MAG_FILTER,osg::Texture3D::LINEAR); |
| | 658 | texture3D->setWrap(osg::Texture3D::WRAP_R,osg::Texture3D::CLAMP); |
| | 659 | texture3D->setWrap(osg::Texture3D::WRAP_S,osg::Texture3D::CLAMP); |
| | 660 | texture3D->setWrap(osg::Texture3D::WRAP_T,osg::Texture3D::CLAMP); |
| | 661 | if (image_3d->getPixelFormat()==GL_ALPHA || |
| | 662 | image_3d->getPixelFormat()==GL_LUMINANCE) |
| | 663 | { |
| | 664 | texture3D->setInternalFormatMode(osg::Texture3D::USE_USER_DEFINED_FORMAT); |
| | 665 | texture3D->setInternalFormat(GL_INTENSITY); |
| | 666 | } |
| | 667 | else |
| | 668 | { |
| | 669 | texture3D->setInternalFormatMode(internalFormatMode); |
| | 670 | } |
| | 671 | |
| | 672 | texture3D->setImage(image_3d.get()); |
| | 673 | |
| | 674 | stateset->setTextureAttributeAndModes(0,texture3D,osg::StateAttribute::ON); |
| | 675 | |
| | 676 | osg::Program* program = new osg::Program; |
| | 677 | stateset->setAttribute(program); |
| | 678 | |
| | 679 | // get shaders from source |
| | 680 | program->addShader(osg::Shader::readShaderFile(osg::Shader::VERTEX, osgDB::findDataFile("volume.vert"))); |
| | 681 | program->addShader(osg::Shader::readShaderFile(osg::Shader::FRAGMENT, osgDB::findDataFile("volume.frag"))); |
| | 682 | |
| | 683 | osg::Uniform* baseTextureSampler = new osg::Uniform("baseTexture",0); |
| | 684 | stateset->addUniform(baseTextureSampler); |
| | 685 | |
| | 686 | osg::Uniform* deltaTexCoord = new osg::Uniform("deltaTexCoord",osg::Vec3(0.0f,0.0f,1.0f/256.0f)); |
| | 687 | stateset->addUniform(deltaTexCoord); |
| | 688 | |
| | 689 | { |
| | 690 | geode->addDrawable(osg::createTexturedQuadGeometry(osg::Vec3(0,0,0),osg::Vec3(1.0,0.0,0.0),osg::Vec3(0.0,1.0,0.0))); |
| | 691 | } |
| | 692 | |
| | 693 | return geode; |
| | 694 | } |
| | 695 | |
| 1294 | | osg::Node* rootNode = createModel(image_3d, normalmap_3d, |
| 1295 | | internalFormatMode, |
| 1296 | | xSize, ySize, zSize, |
| 1297 | | xMultiplier, yMultiplier, zMultiplier, |
| 1298 | | numSlices, sliceEnd, alphaFunc); |
| 1299 | | |
| | 1351 | osg::Node* rootNode = 0; |
| | 1352 | |
| | 1353 | if (useShader) |
| | 1354 | { |
| | 1355 | rootNode = createShaderModel(image_3d, normalmap_3d, |
| | 1356 | internalFormatMode, |
| | 1357 | xSize, ySize, zSize, |
| | 1358 | xMultiplier, yMultiplier, zMultiplier, |
| | 1359 | numSlices, sliceEnd, alphaFunc); |
| | 1360 | } |
| | 1361 | else |
| | 1362 | { |
| | 1363 | rootNode = createModel(image_3d, normalmap_3d, |
| | 1364 | internalFormatMode, |
| | 1365 | xSize, ySize, zSize, |
| | 1366 | xMultiplier, yMultiplier, zMultiplier, |
| | 1367 | numSlices, sliceEnd, alphaFunc); |
| | 1368 | } |
| | 1369 | |