| 493 | | unsigned char* ptr = image_3d->data(1,t,r)+alphaOffset; |
| 494 | | unsigned char* left = image_3d->data(0,t,r)+alphaOffset; |
| 495 | | unsigned char* right = image_3d->data(2,t,r)+alphaOffset; |
| 496 | | unsigned char* above = image_3d->data(1,t+1,r)+alphaOffset; |
| 497 | | unsigned char* below = image_3d->data(1,t-1,r)+alphaOffset; |
| 498 | | unsigned char* in = image_3d->data(1,t,r+1)+alphaOffset; |
| 499 | | unsigned char* out = image_3d->data(1,t,r-1)+alphaOffset; |
| 500 | | |
| 501 | | unsigned char* destination = (unsigned char*) normalmap_3d->data(1,t,r); |
| 502 | | |
| 503 | | for(int s=1;s<image_3d->s()-1;++s) |
| 504 | | { |
| 505 | | |
| 506 | | osg::Vec3 grad((float)(*left)-(float)(*right), |
| 507 | | (float)(*below)-(float)(*above), |
| 508 | | (float)(*out) -(float)(*in)); |
| 509 | | |
| 510 | | grad.normalize(); |
| 511 | | |
| 512 | | if (grad.x()==0.0f && grad.y()==0.0f && grad.z()==0.0f) |
| | 498 | |
| | 499 | if (dataType==GL_UNSIGNED_BYTE) |
| | 500 | { |
| | 501 | unsigned char* ptr = image_3d->data(1,t,r)+alphaOffset; |
| | 502 | unsigned char* left = image_3d->data(0,t,r)+alphaOffset; |
| | 503 | unsigned char* right = image_3d->data(2,t,r)+alphaOffset; |
| | 504 | unsigned char* above = image_3d->data(1,t+1,r)+alphaOffset; |
| | 505 | unsigned char* below = image_3d->data(1,t-1,r)+alphaOffset; |
| | 506 | unsigned char* in = image_3d->data(1,t,r+1)+alphaOffset; |
| | 507 | unsigned char* out = image_3d->data(1,t,r-1)+alphaOffset; |
| | 508 | |
| | 509 | unsigned char* destination = (unsigned char*) normalmap_3d->data(1,t,r); |
| | 510 | |
| | 511 | for(int s=1;s<image_3d->s()-1;++s) |
| 514 | | grad.set(128.0f,128.0f,128.0f); |
| | 513 | |
| | 514 | osg::Vec3 grad((float)(*left)-(float)(*right), |
| | 515 | (float)(*below)-(float)(*above), |
| | 516 | (float)(*out) -(float)(*in)); |
| | 517 | |
| | 518 | grad.normalize(); |
| | 519 | |
| | 520 | if (grad.x()==0.0f && grad.y()==0.0f && grad.z()==0.0f) |
| | 521 | { |
| | 522 | grad.set(128.0f,128.0f,128.0f); |
| | 523 | } |
| | 524 | else |
| | 525 | { |
| | 526 | grad.x() = osg::clampBetween((grad.x()+1.0f)*128.0f,0.0f,255.0f); |
| | 527 | grad.y() = osg::clampBetween((grad.y()+1.0f)*128.0f,0.0f,255.0f); |
| | 528 | grad.z() = osg::clampBetween((grad.z()+1.0f)*128.0f,0.0f,255.0f); |
| | 529 | } |
| | 530 | |
| | 531 | *(destination++) = (unsigned char)(grad.x()); // scale and bias X. |
| | 532 | *(destination++) = (unsigned char)(grad.y()); // scale and bias Y. |
| | 533 | *(destination++) = (unsigned char)(grad.z()); // scale and bias Z. |
| | 534 | |
| | 535 | *destination++ = *ptr; |
| | 536 | |
| | 537 | ptr += sourcePixelIncrement; |
| | 538 | left += sourcePixelIncrement; |
| | 539 | right += sourcePixelIncrement; |
| | 540 | above += sourcePixelIncrement; |
| | 541 | below += sourcePixelIncrement; |
| | 542 | in += sourcePixelIncrement; |
| | 543 | out += sourcePixelIncrement; |
| 516 | | else |
| | 545 | } |
| | 546 | else if (dataType==GL_SHORT) |
| | 547 | { |
| | 548 | short* ptr = (short*)(image_3d->data(1,t,r)+alphaOffset); |
| | 549 | short* left = (short*)(image_3d->data(0,t,r)+alphaOffset); |
| | 550 | short* right = (short*)(image_3d->data(2,t,r)+alphaOffset); |
| | 551 | short* above = (short*)(image_3d->data(1,t+1,r)+alphaOffset); |
| | 552 | short* below = (short*)(image_3d->data(1,t-1,r)+alphaOffset); |
| | 553 | short* in = (short*)(image_3d->data(1,t,r+1)+alphaOffset); |
| | 554 | short* out = (short*)(image_3d->data(1,t,r-1)+alphaOffset); |
| | 555 | |
| | 556 | unsigned char* destination = (unsigned char*) normalmap_3d->data(1,t,r); |
| | 557 | |
| | 558 | for(int s=1;s<image_3d->s()-1;++s) |
| 518 | | grad.x() = osg::clampBetween((grad.x()+1.0f)*128.0f,0.0f,255.0f); |
| 519 | | grad.y() = osg::clampBetween((grad.y()+1.0f)*128.0f,0.0f,255.0f); |
| 520 | | grad.z() = osg::clampBetween((grad.z()+1.0f)*128.0f,0.0f,255.0f); |
| | 560 | |
| | 561 | osg::Vec3 grad((float)(*left)-(float)(*right), |
| | 562 | (float)(*below)-(float)(*above), |
| | 563 | (float)(*out) -(float)(*in)); |
| | 564 | |
| | 565 | grad.normalize(); |
| | 566 | |
| | 567 | //osg::notify(osg::NOTICE)<<"normal "<<grad<<std::endl; |
| | 568 | |
| | 569 | if (grad.x()==0.0f && grad.y()==0.0f && grad.z()==0.0f) |
| | 570 | { |
| | 571 | grad.set(128.0f,128.0f,128.0f); |
| | 572 | } |
| | 573 | else |
| | 574 | { |
| | 575 | grad.x() = osg::clampBetween((grad.x()+1.0f)*128.0f,0.0f,255.0f); |
| | 576 | grad.y() = osg::clampBetween((grad.y()+1.0f)*128.0f,0.0f,255.0f); |
| | 577 | grad.z() = osg::clampBetween((grad.z()+1.0f)*128.0f,0.0f,255.0f); |
| | 578 | } |
| | 579 | |
| | 580 | |
| | 581 | *(destination++) = (unsigned char)(grad.x()); // scale and bias X. |
| | 582 | *(destination++) = (unsigned char)(grad.y()); // scale and bias Y. |
| | 583 | *(destination++) = (unsigned char)(grad.z()); // scale and bias Z. |
| | 584 | |
| | 585 | *destination++ = *ptr/128; |
| | 586 | |
| | 587 | ptr += sourcePixelIncrement; |
| | 588 | left += sourcePixelIncrement; |
| | 589 | right += sourcePixelIncrement; |
| | 590 | above += sourcePixelIncrement; |
| | 591 | below += sourcePixelIncrement; |
| | 592 | in += sourcePixelIncrement; |
| | 593 | out += sourcePixelIncrement; |
| 522 | | |
| 523 | | *(destination++) = (unsigned char)(grad.x()); // scale and bias X. |
| 524 | | *(destination++) = (unsigned char)(grad.y()); // scale and bias Y. |
| 525 | | *(destination++) = (unsigned char)(grad.z()); // scale and bias Z. |
| 526 | | |
| 527 | | *destination++ = *ptr; |
| 528 | | |
| 529 | | ptr += sourcePixelIncrement; |
| 530 | | left += sourcePixelIncrement; |
| 531 | | right += sourcePixelIncrement; |
| 532 | | above += sourcePixelIncrement; |
| 533 | | below += sourcePixelIncrement; |
| 534 | | in += sourcePixelIncrement; |
| 535 | | out += sourcePixelIncrement; |
| 536 | | } |
| 537 | | } |
| 538 | | } |
| | 595 | } |
| | 596 | else if (dataType==GL_UNSIGNED_SHORT) |
| | 597 | { |
| | 598 | unsigned short* ptr = (unsigned short*)(image_3d->data(1,t,r)+alphaOffset); |
| | 599 | unsigned short* left = (unsigned short*)(image_3d->data(0,t,r)+alphaOffset); |
| | 600 | unsigned short* right = (unsigned short*)(image_3d->data(2,t,r)+alphaOffset); |
| | 601 | unsigned short* above = (unsigned short*)(image_3d->data(1,t+1,r)+alphaOffset); |
| | 602 | unsigned short* below = (unsigned short*)(image_3d->data(1,t-1,r)+alphaOffset); |
| | 603 | unsigned short* in = (unsigned short*)(image_3d->data(1,t,r+1)+alphaOffset); |
| | 604 | unsigned short* out = (unsigned short*)(image_3d->data(1,t,r-1)+alphaOffset); |
| | 605 | |
| | 606 | unsigned char* destination = (unsigned char*) normalmap_3d->data(1,t,r); |
| | 607 | |
| | 608 | for(int s=1;s<image_3d->s()-1;++s) |
| | 609 | { |
| | 610 | |
| | 611 | osg::Vec3 grad((float)(*left)-(float)(*right), |
| | 612 | (float)(*below)-(float)(*above), |
| | 613 | (float)(*out) -(float)(*in)); |
| | 614 | |
| | 615 | grad.normalize(); |
| | 616 | |
| | 617 | if (grad.x()==0.0f && grad.y()==0.0f && grad.z()==0.0f) |
| | 618 | { |
| | 619 | grad.set(128.0f,128.0f,128.0f); |
| | 620 | } |
| | 621 | else |
| | 622 | { |
| | 623 | grad.x() = osg::clampBetween((grad.x()+1.0f)*128.0f,0.0f,255.0f); |
| | 624 | grad.y() = osg::clampBetween((grad.y()+1.0f)*128.0f,0.0f,255.0f); |
| | 625 | grad.z() = osg::clampBetween((grad.z()+1.0f)*128.0f,0.0f,255.0f); |
| | 626 | } |
| | 627 | |
| | 628 | *(destination++) = (unsigned char)(grad.x()); // scale and bias X. |
| | 629 | *(destination++) = (unsigned char)(grad.y()); // scale and bias Y. |
| | 630 | *(destination++) = (unsigned char)(grad.z()); // scale and bias Z. |
| | 631 | |
| | 632 | *destination++ = *ptr/256; |
| | 633 | |
| | 634 | ptr += sourcePixelIncrement; |
| | 635 | left += sourcePixelIncrement; |
| | 636 | right += sourcePixelIncrement; |
| | 637 | above += sourcePixelIncrement; |
| | 638 | below += sourcePixelIncrement; |
| | 639 | in += sourcePixelIncrement; |
| | 640 | out += sourcePixelIncrement; |
| | 641 | } |
| | 642 | } |
| | 643 | } |
| | 644 | } |
| | 645 | |
| | 646 | |
| | 647 | osg::notify(osg::NOTICE)<<"Created NormalMapTexture"<<std::endl; |
| 711 | | // set up the 3d texture itself, |
| 712 | | // note, well set the filtering up so that mip mapping is disabled, |
| 713 | | // gluBuild3DMipsmaps doesn't do a very good job of handled the |
| 714 | | // imbalanced dimensions of the 256x256x4 texture. |
| 715 | | osg::Texture3D* texture3D = new osg::Texture3D; |
| 716 | | texture3D->setResizeNonPowerOfTwoHint(false); |
| 717 | | texture3D->setFilter(osg::Texture3D::MIN_FILTER,osg::Texture3D::LINEAR); |
| 718 | | texture3D->setFilter(osg::Texture3D::MAG_FILTER,osg::Texture3D::LINEAR); |
| 719 | | texture3D->setWrap(osg::Texture3D::WRAP_R,osg::Texture3D::CLAMP); |
| 720 | | texture3D->setWrap(osg::Texture3D::WRAP_S,osg::Texture3D::CLAMP); |
| 721 | | texture3D->setWrap(osg::Texture3D::WRAP_T,osg::Texture3D::CLAMP); |
| 722 | | if (image_3d->getPixelFormat()==GL_ALPHA || |
| 723 | | image_3d->getPixelFormat()==GL_LUMINANCE) |
| 724 | | { |
| 725 | | texture3D->setInternalFormatMode(osg::Texture3D::USE_USER_DEFINED_FORMAT); |
| 726 | | texture3D->setInternalFormat(GL_INTENSITY); |
| | 827 | |
| | 828 | osg::Program* program = new osg::Program; |
| | 829 | stateset->setAttribute(program); |
| | 830 | |
| | 831 | // get shaders from source |
| | 832 | |
| | 833 | osg::Shader* vertexShader = osgDB::readShaderFile(osg::Shader::VERTEX, "volume.vert"); |
| | 834 | if (vertexShader) |
| | 835 | { |
| | 836 | program->addShader(vertexShader); |
| 730 | | texture3D->setInternalFormatMode(internalFormatMode); |
| 731 | | } |
| 732 | | |
| 733 | | texture3D->setImage(image_3d.get()); |
| 734 | | |
| 735 | | stateset->setTextureAttributeAndModes(0,texture3D,osg::StateAttribute::ON); |
| 736 | | |
| 737 | | if (tf) |
| | 840 | #include "volume_vert.cpp" |
| | 841 | program->addShader(new osg::Shader(osg::Shader::VERTEX, volume_vert)); |
| | 842 | } |
| | 843 | |
| | 844 | |
| | 845 | if (!(normalmap_3d && tf)) |
| | 846 | { |
| | 847 | // set up the 3d texture itself, |
| | 848 | // note, well set the filtering up so that mip mapping is disabled, |
| | 849 | // gluBuild3DMipsmaps doesn't do a very good job of handled the |
| | 850 | // imbalanced dimensions of the 256x256x4 texture. |
| | 851 | osg::Texture3D* texture3D = new osg::Texture3D; |
| | 852 | texture3D->setResizeNonPowerOfTwoHint(false); |
| | 853 | texture3D->setFilter(osg::Texture3D::MIN_FILTER,minFilter); |
| | 854 | texture3D->setFilter(osg::Texture3D::MAG_FILTER, magFilter); |
| | 855 | texture3D->setWrap(osg::Texture3D::WRAP_R,osg::Texture3D::CLAMP_TO_EDGE); |
| | 856 | texture3D->setWrap(osg::Texture3D::WRAP_S,osg::Texture3D::CLAMP_TO_EDGE); |
| | 857 | texture3D->setWrap(osg::Texture3D::WRAP_T,osg::Texture3D::CLAMP_TO_EDGE); |
| | 858 | if (image_3d->getPixelFormat()==GL_ALPHA || |
| | 859 | image_3d->getPixelFormat()==GL_LUMINANCE) |
| | 860 | { |
| | 861 | texture3D->setInternalFormatMode(osg::Texture3D::USE_USER_DEFINED_FORMAT); |
| | 862 | texture3D->setInternalFormat(GL_INTENSITY); |
| | 863 | } |
| | 864 | else |
| | 865 | { |
| | 866 | texture3D->setInternalFormatMode(internalFormatMode); |
| | 867 | } |
| | 868 | texture3D->setImage(image_3d.get()); |
| | 869 | |
| | 870 | stateset->setTextureAttributeAndModes(0,texture3D,osg::StateAttribute::ON); |
| | 871 | } |
| | 872 | |
| | 873 | |
| | 874 | if (normalmap_3d) |
| | 875 | { |
| | 876 | osg::notify(osg::NOTICE)<<"Setting up normalmapping shader"<<std::endl; |
| | 877 | |
| | 878 | osg::Uniform* normalMapSampler = new osg::Uniform("normalMap",1); |
| | 879 | stateset->addUniform(normalMapSampler); |
| | 880 | |
| | 881 | osg::Texture3D* normalMap = new osg::Texture3D; |
| | 882 | normalMap->setImage(normalmap_3d); |
| | 883 | stateset->setTextureAttributeAndModes(1,normalMap,osg::StateAttribute::ON); |
| | 884 | |
| | 885 | if (tf) |
| | 886 | { |
| | 887 | osg::Texture1D* texture1D = new osg::Texture1D; |
| | 888 | texture1D->setImage(tf->getImage()); |
| | 889 | stateset->setTextureAttributeAndModes(0,texture1D,osg::StateAttribute::ON); |
| | 890 | |
| | 891 | osg::Shader* fragmentShader = osgDB::readShaderFile(osg::Shader::FRAGMENT, "volume-tf-n.frag"); |
| | 892 | if (fragmentShader) |
| | 893 | { |
| | 894 | program->addShader(fragmentShader); |
| | 895 | } |
| | 896 | else |
| | 897 | { |
| | 898 | #include "volume_tf_n_frag.cpp" |
| | 899 | program->addShader(new osg::Shader(osg::Shader::FRAGMENT, volume_tf_n_frag)); |
| | 900 | } |
| | 901 | |
| | 902 | osg::Uniform* tfTextureSampler = new osg::Uniform("tfTexture",0); |
| | 903 | stateset->addUniform(tfTextureSampler); |
| | 904 | } |
| | 905 | else |
| | 906 | { |
| | 907 | osg::Shader* fragmentShader = osgDB::readShaderFile(osg::Shader::FRAGMENT, "volume-n.frag"); |
| | 908 | if (fragmentShader) |
| | 909 | { |
| | 910 | program->addShader(fragmentShader); |
| | 911 | } |
| | 912 | else |
| | 913 | { |
| | 914 | #include "volume_n_frag.cpp" |
| | 915 | program->addShader(new osg::Shader(osg::Shader::FRAGMENT, volume_n_frag)); |
| | 916 | } |
| | 917 | } |
| | 918 | } |
| | 919 | else if (tf) |
| 742 | | } |
| 743 | | |
| 744 | | osg::Program* program = new osg::Program; |
| 745 | | stateset->setAttribute(program); |
| 746 | | |
| 747 | | // get shaders from source |
| 748 | | std::string vertexShaderFile = osgDB::findDataFile("volume.vert"); |
| 749 | | if (!vertexShaderFile.empty()) |
| 750 | | { |
| 751 | | program->addShader(osg::Shader::readShaderFile(osg::Shader::VERTEX, vertexShaderFile)); |
| 752 | | } |
| 753 | | else |
| 754 | | { |
| 755 | | char vertexShaderSource[] = |
| 756 | | "#version 110\n" |
| 757 | | "varying vec4 cameraPos;\n" |
| 758 | | "varying vec4 vertexPos;\n" |
| 759 | | "varying mat4 texgen;\n" |
| 760 | | "\n" |
| 761 | | "void main(void)\n" |
| 762 | | "{\n" |
| 763 | | " gl_Position = ftransform();\n" |
| 764 | | "\n" |
| 765 | | " cameraPos = gl_ModelViewMatrixInverse*vec4(0,0,0,1);\n" |
| 766 | | " vertexPos = gl_Vertex;\n" |
| 767 | | "\n" |
| 768 | | " texgen = mat4(gl_ObjectPlaneS[0], \n" |
| 769 | | " gl_ObjectPlaneT[0],\n" |
| 770 | | " gl_ObjectPlaneR[0],\n" |
| 771 | | " gl_ObjectPlaneQ[0]);\n" |
| 772 | | "}\n"; |
| 773 | | |
| 774 | | osg::Shader* vertex_shader = new osg::Shader(osg::Shader::VERTEX, vertexShaderSource); |
| 775 | | program->addShader(vertex_shader); |
| 776 | | |
| 777 | | } |
| 778 | | |
| 779 | | if (tf) |
| 780 | | { |
| 781 | | std::string fragmentShaderFile = osgDB::findDataFile("volume-tf.frag"); |
| 782 | | if (!fragmentShaderFile.empty()) |
| 783 | | { |
| 784 | | program->addShader(osg::Shader::readShaderFile(osg::Shader::FRAGMENT, fragmentShaderFile)); |
| | 924 | |
| | 925 | osg::Shader* fragmentShader = osgDB::readShaderFile(osg::Shader::FRAGMENT, "volume-tf.frag"); |
| | 926 | if (fragmentShader) |
| | 927 | { |
| | 928 | program->addShader(fragmentShader); |
| 788 | | ////////////////////////////////////////////////////////////////// |
| 789 | | // fragment shader |
| 790 | | // |
| 791 | | char fragmentShaderSource[] = |
| 792 | | "uniform sampler3D baseTexture;\n" |
| 793 | | "uniform sampler1D tfTexture;\n" |
| 794 | | "uniform float sampleDensity;\n" |
| 795 | | "uniform float transparency;\n" |
| 796 | | "uniform float alphaCutOff;\n" |
| 797 | | "\n" |
| 798 | | "varying vec4 cameraPos;\n" |
| 799 | | "varying vec4 vertexPos;\n" |
| 800 | | "varying mat4 texgen;\n" |
| 801 | | "\n" |
| 802 | | "void main(void)\n" |
| 803 | | "{ \n" |
| 804 | | " vec3 t0 = (texgen * vertexPos).xyz;\n" |
| 805 | | " vec3 te = (texgen * cameraPos).xyz;\n" |
| 806 | | "\n" |
| 807 | | " if (te.x>=0.0 && te.x<=1.0 &&\n" |
| 808 | | " te.y>=0.0 && te.y<=1.0 &&\n" |
| 809 | | " te.z>=0.0 && te.z<=1.0)\n" |
| 810 | | " {\n" |
| 811 | | " // do nothing... te inside volume\n" |
| 812 | | " }\n" |
| 813 | | " else\n" |
| 814 | | " {\n" |
| 815 | | " if (te.x<0.0)\n" |
| 816 | | " {\n" |
| 817 | | " float r = -te.x / (t0.x-te.x);\n" |
| 818 | | " te = te + (t0-te)*r;\n" |
| 819 | | " }\n" |
| 820 | | "\n" |
| 821 | | " if (te.x>1.0)\n" |
| 822 | | " {\n" |
| 823 | | " float r = (1.0-te.x) / (t0.x-te.x);\n" |
| 824 | | " te = te + (t0-te)*r;\n" |
| 825 | | " }\n" |
| 826 | | "\n" |
| 827 | | " if (te.y<0.0)\n" |
| 828 | | " {\n" |
| 829 | | " float r = -te.y / (t0.y-te.y);\n" |
| 830 | | " te = te + (t0-te)*r;\n" |
| 831 | | " }\n" |
| 832 | | "\n" |
| 833 | | " if (te.y>1.0)\n" |
| 834 | | " {\n" |
| 835 | | " float r = (1.0-te.y) / (t0.y-te.y);\n" |
| 836 | | " te = te + (t0-te)*r;\n" |
| 837 | | " }\n" |
| 838 | | "\n" |
| 839 | | " if (te.z<0.0)\n" |
| 840 | | " {\n" |
| 841 | | " float r = -te.z / (t0.z-te.z);\n" |
| 842 | | " te = te + (t0-te)*r;\n" |
| 843 | | " }\n" |
| 844 | | "\n" |
| 845 | | " if (te.z>1.0)\n" |
| 846 | | " {\n" |
| 847 | | " float r = (1.0-te.z) / (t0.z-te.z);\n" |
| 848 | | " te = te + (t0-te)*r;\n" |
| 849 | | " }\n" |
| 850 | | " }\n" |
| 851 | | "\n" |
| 852 | | " const float max_iteratrions = 2048.0;\n" |
| 853 | | " float num_iterations = length(te-t0)/sampleDensity;\n" |
| 854 | | " if (num_iterations>max_iteratrions) \n" |
| 855 | | " {\n" |
| 856 | | " num_iterations = max_iteratrions;\n" |
| 857 | | " }\n" |
| 858 | | "\n" |
| 859 | | " vec3 deltaTexCoord=(te-t0)/float(num_iterations-1.0);\n" |
| 860 | | " vec3 texcoord = t0;\n" |
| 861 | | "\n" |
| 862 | | " vec4 fragColor = vec4(0.0, 0.0, 0.0, 0.0); \n" |
| 863 | | " while(num_iterations>0.0)\n" |
| 864 | | " {\n" |
| 865 | | " float v = texture3D( baseTexture, texcoord).s;\n" |
| 866 | | " vec4 color = texture1D( tfTexture, v);\n" |
| 867 | | " float r = color[3]*transparency;\n" |
| 868 | | " if (r>alphaCutOff)\n" |
| 869 | | " {\n" |
| 870 | | " fragColor.xyz = fragColor.xyz*(1.0-r)+color.xyz*r;\n" |
| 871 | | " fragColor.w += r;\n" |
| 872 | | " }\n" |
| 873 | | " texcoord += deltaTexCoord; \n" |
| 874 | | "\n" |
| 875 | | " --num_iterations;\n" |
| 876 | | " }\n" |
| 877 | | "\n" |
| 878 | | " if (fragColor.w>1.0) fragColor.w = 1.0; \n" |
| 879 | | " if (fragColor.w==0.0) discard;\n" |
| 880 | | " gl_FragColor = fragColor;\n" |
| 881 | | "}\n"; |
| 882 | | |
| 883 | | osg::Shader* fragment_shader = new osg::Shader(osg::Shader::FRAGMENT, fragmentShaderSource); |
| 884 | | program->addShader(fragment_shader); |
| | 932 | #include "volume_tf_frag.cpp" |
| | 933 | program->addShader(new osg::Shader(osg::Shader::FRAGMENT, volume_tf_frag)); |
| 900 | | ////////////////////////////////////////////////////////////////// |
| 901 | | // fragment shader |
| 902 | | // |
| 903 | | char fragmentShaderSource[] = |
| 904 | | "uniform sampler3D baseTexture;\n" |
| 905 | | "uniform float sampleDensity;\n" |
| 906 | | "uniform float transparency;\n" |
| 907 | | "uniform float alphaCutOff;\n" |
| 908 | | "\n" |
| 909 | | "varying vec4 cameraPos;\n" |
| 910 | | "varying vec4 vertexPos;\n" |
| 911 | | "varying mat4 texgen;\n" |
| 912 | | "\n" |
| 913 | | "void main(void)\n" |
| 914 | | "{ \n" |
| 915 | | " vec3 t0 = (texgen * vertexPos).xyz;\n" |
| 916 | | " vec3 te = (texgen * cameraPos).xyz;\n" |
| 917 | | "\n" |
| 918 | | " if (te.x>=0.0 && te.x<=1.0 &&\n" |
| 919 | | " te.y>=0.0 && te.y<=1.0 &&\n" |
| 920 | | " te.z>=0.0 && te.z<=1.0)\n" |
| 921 | | " {\n" |
| 922 | | " // do nothing... te inside volume\n" |
| 923 | | " }\n" |
| 924 | | " else\n" |
| 925 | | " {\n" |
| 926 | | " if (te.x<0.0)\n" |
| 927 | | " {\n" |
| 928 | | " float r = -te.x / (t0.x-te.x);\n" |
| 929 | | " te = te + (t0-te)*r;\n" |
| 930 | | " }\n" |
| 931 | | "\n" |
| 932 | | " if (te.x>1.0)\n" |
| 933 | | " {\n" |
| 934 | | " float r = (1.0-te.x) / (t0.x-te.x);\n" |
| 935 | | " te = te + (t0-te)*r;\n" |
| 936 | | " }\n" |
| 937 | | "\n" |
| 938 | | " if (te.y<0.0)\n" |
| 939 | | " {\n" |
| 940 | | " float r = -te.y / (t0.y-te.y);\n" |
| 941 | | " te = te + (t0-te)*r;\n" |
| 942 | | " }\n" |
| 943 | | "\n" |
| 944 | | " if (te.y>1.0)\n" |
| 945 | | " {\n" |
| 946 | | " float r = (1.0-te.y) / (t0.y-te.y);\n" |
| 947 | | " te = te + (t0-te)*r;\n" |
| 948 | | " }\n" |
| 949 | | "\n" |
| 950 | | " if (te.z<0.0)\n" |
| 951 | | " {\n" |
| 952 | | " float r = -te.z / (t0.z-te.z);\n" |
| 953 | | " te = te + (t0-te)*r;\n" |
| 954 | | " }\n" |
| 955 | | "\n" |
| 956 | | " if (te.z>1.0)\n" |
| 957 | | " {\n" |
| 958 | | " float r = (1.0-te.z) / (t0.z-te.z);\n" |
| 959 | | " te = te + (t0-te)*r;\n" |
| 960 | | " }\n" |
| 961 | | " }\n" |
| 962 | | "\n" |
| 963 | | " const float max_iteratrions = 2048.0;\n" |
| 964 | | " float num_iterations = length(te-t0)/sampleDensity;\n" |
| 965 | | " if (num_iterations>max_iteratrions) \n" |
| 966 | | " {\n" |
| 967 | | " num_iterations = max_iteratrions;\n" |
| 968 | | " }\n" |
| 969 | | "\n" |
| 970 | | " vec3 deltaTexCoord=(te-t0)/float(num_iterations-1.0);\n" |
| 971 | | " vec3 texcoord = t0;\n" |
| 972 | | "\n" |
| 973 | | " vec4 fragColor = vec4(0.0, 0.0, 0.0, 0.0); \n" |
| 974 | | " while(num_iterations>0.0)\n" |
| 975 | | " {\n" |
| 976 | | " vec4 color = texture3D( baseTexture, texcoord);\n" |
| 977 | | " float r = color[3]*transparency;\n" |
| 978 | | " if (r>alphaCutOff)\n" |
| 979 | | " {\n" |
| 980 | | " fragColor.xyz = fragColor.xyz*(1.0-r)+color.xyz*r;\n" |
| 981 | | " fragColor.w += r;\n" |
| 982 | | " }\n" |
| 983 | | " texcoord += deltaTexCoord; \n" |
| 984 | | "\n" |
| 985 | | " --num_iterations;\n" |
| 986 | | " }\n" |
| 987 | | "\n" |
| 988 | | " if (fragColor.w>1.0) fragColor.w = 1.0; \n" |
| 989 | | " if (fragColor.w==0.0) discard;\n" |
| 990 | | " gl_FragColor = fragColor;\n" |
| 991 | | "}\n"; |
| 992 | | |
| 993 | | osg::Shader* fragment_shader = new osg::Shader(osg::Shader::FRAGMENT, fragmentShaderSource); |
| 994 | | program->addShader(fragment_shader); |
| | 949 | #include "volume_frag.cpp" |
| | 950 | program->addShader(new osg::Shader(osg::Shader::FRAGMENT, volume_frag)); |
| 1192 | | bump_texture3D->setFilter(osg::Texture3D::MIN_FILTER,osg::Texture3D::LINEAR); |
| 1193 | | bump_texture3D->setFilter(osg::Texture3D::MAG_FILTER,osg::Texture3D::LINEAR); |
| 1194 | | bump_texture3D->setWrap(osg::Texture3D::WRAP_R,osg::Texture3D::CLAMP); |
| 1195 | | bump_texture3D->setWrap(osg::Texture3D::WRAP_S,osg::Texture3D::CLAMP); |
| 1196 | | bump_texture3D->setWrap(osg::Texture3D::WRAP_T,osg::Texture3D::CLAMP); |
| | 1152 | bump_texture3D->setFilter(osg::Texture3D::MIN_FILTER,minFilter); |
| | 1153 | bump_texture3D->setFilter(osg::Texture3D::MAG_FILTER, magFilter); |
| | 1154 | bump_texture3D->setWrap(osg::Texture3D::WRAP_R,osg::Texture3D::CLAMP_TO_EDGE); |
| | 1155 | bump_texture3D->setWrap(osg::Texture3D::WRAP_S,osg::Texture3D::CLAMP_TO_EDGE); |
| | 1156 | bump_texture3D->setWrap(osg::Texture3D::WRAP_T,osg::Texture3D::CLAMP_TO_EDGE); |
| 1229 | | texture3D->setFilter(osg::Texture3D::MIN_FILTER,osg::Texture3D::LINEAR); |
| 1230 | | texture3D->setFilter(osg::Texture3D::MAG_FILTER,osg::Texture3D::LINEAR); |
| 1231 | | texture3D->setWrap(osg::Texture3D::WRAP_R,osg::Texture3D::CLAMP); |
| 1232 | | texture3D->setWrap(osg::Texture3D::WRAP_S,osg::Texture3D::CLAMP); |
| 1233 | | texture3D->setWrap(osg::Texture3D::WRAP_T,osg::Texture3D::CLAMP); |
| | 1189 | texture3D->setFilter(osg::Texture3D::MIN_FILTER,minFilter); |
| | 1190 | texture3D->setFilter(osg::Texture3D::MAG_FILTER, magFilter); |
| | 1191 | texture3D->setWrap(osg::Texture3D::WRAP_R,osg::Texture3D::CLAMP_TO_EDGE); |
| | 1192 | texture3D->setWrap(osg::Texture3D::WRAP_S,osg::Texture3D::CLAMP_TO_EDGE); |
| | 1193 | texture3D->setWrap(osg::Texture3D::WRAP_T,osg::Texture3D::CLAMP_TO_EDGE); |
| 1260 | | bump_texture3D->setFilter(osg::Texture3D::MIN_FILTER,osg::Texture3D::LINEAR); |
| 1261 | | bump_texture3D->setFilter(osg::Texture3D::MAG_FILTER,osg::Texture3D::LINEAR); |
| 1262 | | bump_texture3D->setWrap(osg::Texture3D::WRAP_R,osg::Texture3D::CLAMP); |
| 1263 | | bump_texture3D->setWrap(osg::Texture3D::WRAP_S,osg::Texture3D::CLAMP); |
| 1264 | | bump_texture3D->setWrap(osg::Texture3D::WRAP_T,osg::Texture3D::CLAMP); |
| | 1220 | bump_texture3D->setFilter(osg::Texture3D::MIN_FILTER,minFilter); |
| | 1221 | bump_texture3D->setFilter(osg::Texture3D::MAG_FILTER, magFilter); |
| | 1222 | bump_texture3D->setWrap(osg::Texture3D::WRAP_R,osg::Texture3D::CLAMP_TO_EDGE); |
| | 1223 | bump_texture3D->setWrap(osg::Texture3D::WRAP_S,osg::Texture3D::CLAMP_TO_EDGE); |
| | 1224 | bump_texture3D->setWrap(osg::Texture3D::WRAP_T,osg::Texture3D::CLAMP_TO_EDGE); |
| 1303 | | texture3D->setFilter(osg::Texture3D::MIN_FILTER,osg::Texture3D::LINEAR); |
| 1304 | | texture3D->setFilter(osg::Texture3D::MAG_FILTER,osg::Texture3D::LINEAR); |
| 1305 | | texture3D->setWrap(osg::Texture3D::WRAP_R,osg::Texture3D::CLAMP); |
| 1306 | | texture3D->setWrap(osg::Texture3D::WRAP_S,osg::Texture3D::CLAMP); |
| 1307 | | texture3D->setWrap(osg::Texture3D::WRAP_T,osg::Texture3D::CLAMP); |
| | 1263 | texture3D->setFilter(osg::Texture3D::MIN_FILTER,minFilter); |
| | 1264 | texture3D->setFilter(osg::Texture3D::MAG_FILTER, magFilter); |
| | 1265 | texture3D->setWrap(osg::Texture3D::WRAP_R,osg::Texture3D::CLAMP_TO_EDGE); |
| | 1266 | texture3D->setWrap(osg::Texture3D::WRAP_S,osg::Texture3D::CLAMP_TO_EDGE); |
| | 1267 | texture3D->setWrap(osg::Texture3D::WRAP_T,osg::Texture3D::CLAMP_TO_EDGE); |