From b000198cc47e95c013100187f273f158f752ffe6 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Fri, 12 Sep 2008 15:41:30 +0000 Subject: [PATCH] Removed use of ints and reading from gl_FragColor in shader --- examples/osgvolume/osgvolume.cpp | 162 ++++++++++++++++++++++++++----- 1 file changed, 139 insertions(+), 23 deletions(-) diff --git a/examples/osgvolume/osgvolume.cpp b/examples/osgvolume/osgvolume.cpp index ee0df95ba..671ddcaaf 100644 --- a/examples/osgvolume/osgvolume.cpp +++ b/examples/osgvolume/osgvolume.cpp @@ -34,6 +34,7 @@ #include #include #include +#include #include #include @@ -420,7 +421,8 @@ osg::Image* createTexture3D(ImageList& imageList, ProcessRow& processRow, unsigned int numComponentsDesired, int s_maximumTextureSize, int t_maximumTextureSize, - int r_maximumTextureSize ) + int r_maximumTextureSize, + bool resizeToPowerOfTwo) { int max_s = 0; int max_t = 0; @@ -475,19 +477,27 @@ osg::Image* createTexture3D(ImageList& imageList, ProcessRow& processRow, if (desiredPixelFormat==0) return 0; // compute nearest powers of two for each axis. + int s_nearestPowerOfTwo = 1; - while(s_nearestPowerOfTwo& image_3d, osg::ref_ptrmax_iteratrions) \n" " {\n" " num_iterations = max_iteratrions;\n" " }\n" "\n" - " vec3 deltaTexCoord=(te-t0)/float(num_iterations-1);\n" + " vec3 deltaTexCoord=(te-t0)/float(num_iterations-1.0);\n" " vec3 texcoord = t0;\n" "\n" - " gl_FragColor = vec4(0.0, 0.0, 0.0, 0.0); \n" - " while(num_iterations>0)\n" + " vec4 fragColor = vec4(0.0, 0.0, 0.0, 0.0); \n" + " while(num_iterations>0.0)\n" " {\n" " vec4 color = texture3D( baseTexture, texcoord);\n" " float r = color[3]*transparency;\n" " if (r>alphaCutOff)\n" " {\n" - " gl_FragColor.xyz = gl_FragColor.xyz*(1.0-r)+color.xyz*r;\n" - " gl_FragColor.w += r;\n" + " fragColor.xyz = fragColor.xyz*(1.0-r)+color.xyz*r;\n" + " fragColor.w += r;\n" " }\n" " texcoord += deltaTexCoord; \n" "\n" " --num_iterations;\n" " }\n" "\n" - " if (gl_FragColor.w>1.0) gl_FragColor.w = 1.0; \n" - " if (gl_FragColor.w==0.0) discard;\n" + " if (fragColor.w>1.0) fragColor.w = 1.0; \n" + " if (fragColor.w==0.0) discard;\n" + " gl_FragColor = fragColor;\n" "}\n"; osg::Shader* fragment_shader = new osg::Shader(osg::Shader::FRAGMENT, fragmentShaderSource); @@ -1040,7 +1052,8 @@ osg::Node* createShaderModel(osg::ref_ptr& image_3d, osg::ref_ptr& image_3d, osg::ref_ptr& normalmap_3d, +osg::Node* createModel(osg::ref_ptr& image_3d, + osg::ref_ptr& normalmap_3d, osg::Texture::InternalFormatMode internalFormatMode, float xSize, float ySize, float zSize, float xMultiplier, float yMultiplier, float zMultiplier, @@ -1540,6 +1553,95 @@ void doColourSpaceConversion(ColourSpaceOperation op, osg::Image* image, osg::Ve } } + +struct ApplyTransferFunctionOperator +{ + ApplyTransferFunctionOperator(osg::TransferFunction1D* tf, unsigned char* data): + _tf(tf), + _data(data) {} + + inline void luminance(float l) const + { + osg::Vec4 c = _tf->getInterpolatedValue(l); + //std::cout<<"l = "<allocateImage(image->s(),image->t(), image->r(), GL_RGBA, GL_UNSIGNED_BYTE); + + readImage(image,ApplyTransferFunctionOperator(transferFunction, output_image->data())); + + return output_image; +} + +osg::TransferFunction1D* readTransferFunctionFile(const std::string& filename) +{ + std::string foundFile = osgDB::findDataFile(filename); + if (foundFile.empty()) + { + std::cout<<"Error: could not find transfer function file : "<> value >> red >> green >> blue >> alpha; + if (fin) + { + std::cout<<"value = "<assign(valueMap, true); + + return tf; +} + + class TestSupportOperation: public osg::GraphicsOperation { public: @@ -1624,6 +1726,14 @@ int main( int argc, char **argv ) while (arguments.read("-o",outputFile)) {} + + osg::ref_ptr transferFunction; + std::string tranferFunctionFile; + while (arguments.read("--tf",tranferFunctionFile)) + { + transferFunction = readTransferFunctionFile(tranferFunctionFile); + } + unsigned int numSlices=500; while (arguments.read("-s",numSlices)) {} @@ -1657,9 +1767,9 @@ int main( int argc, char **argv ) viewer.realize(); int maximumTextureSize = testSupportOperation->maximumTextureSize; - int s_maximumTextureSize = 256; - int t_maximumTextureSize = 256; - int r_maximumTextureSize = 256; + int s_maximumTextureSize = maximumTextureSize; + int t_maximumTextureSize = maximumTextureSize; + int r_maximumTextureSize = maximumTextureSize; while(arguments.read("--maxTextureSize",maximumTextureSize)) { s_maximumTextureSize = maximumTextureSize; @@ -1685,6 +1795,7 @@ int main( int argc, char **argv ) while(arguments.read("--modulate-alpha-by-colour", colourModulate.x(),colourModulate.y(),colourModulate.z(),colourModulate.w() )) { colourSpaceOperation = MODULATE_ALPHA_BY_COLOUR; } while(arguments.read("--replace-alpha-with-luminance")) { colourSpaceOperation = REPLACE_ALPHA_WITH_LUMINACE; } + bool resizeToPowerOfTwo = false; unsigned int numComponentsDesired = 0; while(arguments.read("--num-components", numComponentsDesired)) {} @@ -1717,7 +1828,7 @@ int main( int argc, char **argv ) // pack the textures into a single texture. ProcessRow processRow; - image_3d = createTexture3D(imageList, processRow, numComponentsDesired, s_maximumTextureSize, t_maximumTextureSize, r_maximumTextureSize); + image_3d = createTexture3D(imageList, processRow, numComponentsDesired, s_maximumTextureSize, t_maximumTextureSize, r_maximumTextureSize, resizeToPowerOfTwo); } @@ -1752,6 +1863,11 @@ int main( int argc, char **argv ) doColourSpaceConversion(colourSpaceOperation, image_3d.get(), colourModulate); } + if (transferFunction.valid()) + { + image_3d = applyTransferFunction(image_3d.get(), transferFunction.get()); + } + osg::ref_ptr normalmap_3d = createNormalMap ? createNormalMapTexture(image_3d.get()) : 0;