Changeset 8850 for OpenSceneGraph/trunk/examples/osgvolume/osgvolume.cpp
- Timestamp:
- 09/12/08 17:41:30 (5 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
OpenSceneGraph/trunk/examples/osgvolume/osgvolume.cpp
r8802 r8850 35 35 #include <osg/BlendFunc> 36 36 #include <osg/BlendEquation> 37 #include <osg/TransferFunction> 37 38 38 39 #include <osgDB/Registry> … … 421 422 int s_maximumTextureSize, 422 423 int t_maximumTextureSize, 423 int r_maximumTextureSize ) 424 int r_maximumTextureSize, 425 bool resizeToPowerOfTwo) 424 426 { 425 427 int max_s = 0; … … 476 478 477 479 // compute nearest powers of two for each axis. 480 478 481 int s_nearestPowerOfTwo = 1; 479 while(s_nearestPowerOfTwo<max_s && s_nearestPowerOfTwo<s_maximumTextureSize) s_nearestPowerOfTwo*=2;480 481 482 int t_nearestPowerOfTwo = 1; 482 while(t_nearestPowerOfTwo<max_t && t_nearestPowerOfTwo<t_maximumTextureSize) t_nearestPowerOfTwo*=2;483 484 483 int r_nearestPowerOfTwo = 1; 485 while(r_nearestPowerOfTwo<total_r && r_nearestPowerOfTwo<r_maximumTextureSize) r_nearestPowerOfTwo*=2; 486 487 488 osg::notify(osg::NOTICE)<<"max image width = "<<max_s<<" nearest power of two = "<<s_nearestPowerOfTwo<<std::endl; 489 osg::notify(osg::NOTICE)<<"max image height = "<<max_t<<" nearest power of two = "<<t_nearestPowerOfTwo<<std::endl; 490 osg::notify(osg::NOTICE)<<"max image depth = "<<total_r<<" nearest power of two = "<<r_nearestPowerOfTwo<<std::endl; 484 485 if (resizeToPowerOfTwo) 486 { 487 while(s_nearestPowerOfTwo<max_s && s_nearestPowerOfTwo<s_maximumTextureSize) s_nearestPowerOfTwo*=2; 488 while(t_nearestPowerOfTwo<max_t && t_nearestPowerOfTwo<t_maximumTextureSize) t_nearestPowerOfTwo*=2; 489 while(r_nearestPowerOfTwo<total_r && r_nearestPowerOfTwo<r_maximumTextureSize) r_nearestPowerOfTwo*=2; 490 491 osg::notify(osg::NOTICE)<<"max image width = "<<max_s<<" nearest power of two = "<<s_nearestPowerOfTwo<<std::endl; 492 osg::notify(osg::NOTICE)<<"max image height = "<<max_t<<" nearest power of two = "<<t_nearestPowerOfTwo<<std::endl; 493 osg::notify(osg::NOTICE)<<"max image depth = "<<total_r<<" nearest power of two = "<<r_nearestPowerOfTwo<<std::endl; 494 } 495 else 496 { 497 s_nearestPowerOfTwo = max_s; 498 t_nearestPowerOfTwo = max_t; 499 r_nearestPowerOfTwo = total_r; 500 } 491 501 492 502 // now allocate the 3d texture; … … 829 839 { 830 840 char vertexShaderSource[] = 841 "#version 110\n" 831 842 "varying vec4 cameraPos;\n" 832 843 "varying vec4 vertexPos;\n" … … 921 932 " }\n" 922 933 "\n" 923 " const int max_iteratrions = 256;\n"924 " int num_iterations = length(te-t0)/sampleDensity;\n"934 " const float max_iteratrions = 256.0;\n" 935 " float num_iterations = length(te-t0)/sampleDensity;\n" 925 936 " if (num_iterations>max_iteratrions) \n" 926 937 " {\n" … … 928 939 " }\n" 929 940 "\n" 930 " vec3 deltaTexCoord=(te-t0)/float(num_iterations-1 );\n"941 " vec3 deltaTexCoord=(te-t0)/float(num_iterations-1.0);\n" 931 942 " vec3 texcoord = t0;\n" 932 943 "\n" 933 " gl_FragColor = vec4(0.0, 0.0, 0.0, 0.0); \n"934 " while(num_iterations>0 )\n"944 " vec4 fragColor = vec4(0.0, 0.0, 0.0, 0.0); \n" 945 " while(num_iterations>0.0)\n" 935 946 " {\n" 936 947 " vec4 color = texture3D( baseTexture, texcoord);\n" … … 938 949 " if (r>alphaCutOff)\n" 939 950 " {\n" 940 " gl_FragColor.xyz = gl_FragColor.xyz*(1.0-r)+color.xyz*r;\n"941 " gl_FragColor.w += r;\n"951 " fragColor.xyz = fragColor.xyz*(1.0-r)+color.xyz*r;\n" 952 " fragColor.w += r;\n" 942 953 " }\n" 943 954 " texcoord += deltaTexCoord; \n" … … 946 957 " }\n" 947 958 "\n" 948 " if (gl_FragColor.w>1.0) gl_FragColor.w = 1.0; \n" 949 " if (gl_FragColor.w==0.0) discard;\n" 959 " if (fragColor.w>1.0) fragColor.w = 1.0; \n" 960 " if (fragColor.w==0.0) discard;\n" 961 " gl_FragColor = fragColor;\n" 950 962 "}\n"; 951 963 … … 1041 1053 } 1042 1054 1043 osg::Node* createModel(osg::ref_ptr<osg::Image>& image_3d, osg::ref_ptr<osg::Image>& normalmap_3d, 1055 osg::Node* createModel(osg::ref_ptr<osg::Image>& image_3d, 1056 osg::ref_ptr<osg::Image>& normalmap_3d, 1044 1057 osg::Texture::InternalFormatMode internalFormatMode, 1045 1058 float xSize, float ySize, float zSize, … … 1540 1553 } 1541 1554 } 1555 1556 1557 struct ApplyTransferFunctionOperator 1558 { 1559 ApplyTransferFunctionOperator(osg::TransferFunction1D* tf, unsigned char* data): 1560 _tf(tf), 1561 _data(data) {} 1562 1563 inline void luminance(float l) const 1564 { 1565 osg::Vec4 c = _tf->getInterpolatedValue(l); 1566 //std::cout<<"l = "<<l<<" c="<<c<<std::endl; 1567 *(_data++) = (unsigned char)(c[0]*255.0f + 0.5f); 1568 *(_data++) = (unsigned char)(c[1]*255.0f + 0.5f); 1569 *(_data++) = (unsigned char)(c[2]*255.0f + 0.5f); 1570 *(_data++) = (unsigned char)(c[3]*255.0f + 0.5f); 1571 } 1572 1573 inline void alpha(float a) const 1574 { 1575 luminance(a); 1576 } 1577 1578 inline void luminance_alpha(float l,float a) const 1579 { 1580 luminance(l); 1581 } 1582 1583 inline void rgb(float r,float g,float b) const 1584 { 1585 luminance((r+g+b)*0.3333333); 1586 } 1587 1588 inline void rgba(float r,float g,float b,float a) const 1589 { 1590 luminance(a); 1591 } 1592 1593 mutable osg::ref_ptr<osg::TransferFunction1D> _tf; 1594 mutable unsigned char* _data; 1595 }; 1596 1597 osg::Image* applyTransferFunction(osg::Image* image, osg::TransferFunction1D* transferFunction) 1598 { 1599 std::cout<<"Applying transfer function"<<std::endl; 1600 osg::Image* output_image = new osg::Image; 1601 output_image->allocateImage(image->s(),image->t(), image->r(), GL_RGBA, GL_UNSIGNED_BYTE); 1602 1603 readImage(image,ApplyTransferFunctionOperator(transferFunction, output_image->data())); 1604 1605 return output_image; 1606 } 1607 1608 osg::TransferFunction1D* readTransferFunctionFile(const std::string& filename) 1609 { 1610 std::string foundFile = osgDB::findDataFile(filename); 1611 if (foundFile.empty()) 1612 { 1613 std::cout<<"Error: could not find transfer function file : "<<filename<<std::endl; 1614 return 0; 1615 } 1616 1617 std::cout<<"Reading transfer function "<<filename<<std::endl; 1618 1619 osg::TransferFunction1D::ValueMap valueMap; 1620 std::ifstream fin(foundFile.c_str()); 1621 while(fin) 1622 { 1623 float value, red, green, blue, alpha; 1624 fin >> value >> red >> green >> blue >> alpha; 1625 if (fin) 1626 { 1627 std::cout<<"value = "<<value<<" ("<<red<<", "<<green<<", "<<blue<<", "<<alpha<<")"<<std::endl; 1628 valueMap[value] = osg::Vec4(red,green,blue,alpha); 1629 } 1630 } 1631 1632 if (valueMap.empty()) 1633 { 1634 std::cout<<"Error: No values read from transfer function file: "<<filename<<std::endl; 1635 return 0; 1636 } 1637 1638 osg::TransferFunction1D* tf = new osg::TransferFunction1D; 1639 tf->assign(valueMap, true); 1640 1641 return tf; 1642 } 1643 1542 1644 1543 1645 class TestSupportOperation: public osg::GraphicsOperation … … 1625 1727 1626 1728 1729 1730 osg::ref_ptr<osg::TransferFunction1D> transferFunction; 1731 std::string tranferFunctionFile; 1732 while (arguments.read("--tf",tranferFunctionFile)) 1733 { 1734 transferFunction = readTransferFunctionFile(tranferFunctionFile); 1735 } 1736 1627 1737 unsigned int numSlices=500; 1628 1738 while (arguments.read("-s",numSlices)) {} … … 1658 1768 1659 1769 int maximumTextureSize = testSupportOperation->maximumTextureSize; 1660 int s_maximumTextureSize = 256;1661 int t_maximumTextureSize = 256;1662 int r_maximumTextureSize = 256;1770 int s_maximumTextureSize = maximumTextureSize; 1771 int t_maximumTextureSize = maximumTextureSize; 1772 int r_maximumTextureSize = maximumTextureSize; 1663 1773 while(arguments.read("--maxTextureSize",maximumTextureSize)) 1664 1774 { … … 1686 1796 while(arguments.read("--replace-alpha-with-luminance")) { colourSpaceOperation = REPLACE_ALPHA_WITH_LUMINACE; } 1687 1797 1798 bool resizeToPowerOfTwo = false; 1688 1799 1689 1800 unsigned int numComponentsDesired = 0; … … 1718 1829 // pack the textures into a single texture. 1719 1830 ProcessRow processRow; 1720 image_3d = createTexture3D(imageList, processRow, numComponentsDesired, s_maximumTextureSize, t_maximumTextureSize, r_maximumTextureSize );1831 image_3d = createTexture3D(imageList, processRow, numComponentsDesired, s_maximumTextureSize, t_maximumTextureSize, r_maximumTextureSize, resizeToPowerOfTwo); 1721 1832 } 1722 1833 … … 1751 1862 { 1752 1863 doColourSpaceConversion(colourSpaceOperation, image_3d.get(), colourModulate); 1864 } 1865 1866 if (transferFunction.valid()) 1867 { 1868 image_3d = applyTransferFunction(image_3d.get(), transferFunction.get()); 1753 1869 } 1754 1870
