From 1fbbd347bfc62e938f5a911691edfbece31e27d4 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Wed, 8 Jun 2011 17:45:24 +0000 Subject: [PATCH] Cleaned up example to use the new ImageUtils functions for creating a 3D image from a list of images. --- examples/osgvolume/osgvolume.cpp | 609 +++---------------------------- 1 file changed, 49 insertions(+), 560 deletions(-) diff --git a/examples/osgvolume/osgvolume.cpp b/examples/osgvolume/osgvolume.cpp index d0d1efe0f..7eb6013a6 100644 --- a/examples/osgvolume/osgvolume.cpp +++ b/examples/osgvolume/osgvolume.cpp @@ -78,30 +78,43 @@ enum ShadingModel MaximumIntensityProjection }; -struct PassThroughTransformFunction + +osg::Image* createTexture3D(osg::ImageList& imageList, + unsigned int numComponentsDesired, + int s_maximumTextureSize, + int t_maximumTextureSize, + int r_maximumTextureSize, + bool resizeToPowerOfTwo) { - unsigned char operator() (unsigned char c) const { return c; } -}; + if (numComponentsDesired==0) + { + return osg::createImage3DWithAlpha(imageList, + s_maximumTextureSize, + t_maximumTextureSize, + r_maximumTextureSize, + resizeToPowerOfTwo); + } + else + { + GLenum desiredPixelFormat = 0; + switch(numComponentsDesired) + { + case(1) : desiredPixelFormat = GL_LUMINANCE; break; + case(2) : desiredPixelFormat = GL_LUMINANCE_ALPHA; break; + case(3) : desiredPixelFormat = GL_RGB; break; + case(4) : desiredPixelFormat = GL_RGBA; break; + } -void clampToNearestValidPowerOfTwo(int& sizeX, int& sizeY, int& sizeZ, int s_maximumTextureSize, int t_maximumTextureSize, int r_maximumTextureSize) -{ - // compute nearest powers of two for each axis. - int s_nearestPowerOfTwo = 1; - while(s_nearestPowerOfTwo image = osg::createImage3D(imageList, - desiredPixelFormat, - s_maximumTextureSize, - t_maximumTextureSize, - r_maximumTextureSize, - resizeToPowerOfTwo); - if (image.valid()) - { - if (modulateAlphaByLuminance) - { - osg::modifyImage(image.get(), ModulateAlphaByLuminanceOperator()); - } - return image.release(); - } - else - { - return 0; - } -} -#else - -struct ProcessRow -{ - virtual ~ProcessRow() {} - - virtual void operator() (unsigned int num, - GLenum source_pixelFormat, unsigned char* source, - GLenum dest_pixelFormat, unsigned char* dest) const - { - switch(source_pixelFormat) - { - case(GL_LUMINANCE): - case(GL_ALPHA): - switch(dest_pixelFormat) - { - case(GL_LUMINANCE): - case(GL_ALPHA): A_to_A(num, source, dest); break; - case(GL_LUMINANCE_ALPHA): A_to_LA(num, source, dest); break; - case(GL_RGB): A_to_RGB(num, source, dest); break; - case(GL_RGBA): A_to_RGBA(num, source, dest); break; - } - break; - case(GL_LUMINANCE_ALPHA): - switch(dest_pixelFormat) - { - case(GL_LUMINANCE): - case(GL_ALPHA): LA_to_A(num, source, dest); break; - case(GL_LUMINANCE_ALPHA): LA_to_LA(num, source, dest); break; - case(GL_RGB): LA_to_RGB(num, source, dest); break; - case(GL_RGBA): LA_to_RGBA(num, source, dest); break; - } - break; - case(GL_RGB): - switch(dest_pixelFormat) - { - case(GL_LUMINANCE): - case(GL_ALPHA): RGB_to_A(num, source, dest); break; - case(GL_LUMINANCE_ALPHA): RGB_to_LA(num, source, dest); break; - case(GL_RGB): RGB_to_RGB(num, source, dest); break; - case(GL_RGBA): RGB_to_RGBA(num, source, dest); break; - } - break; - case(GL_RGBA): - switch(dest_pixelFormat) - { - case(GL_LUMINANCE): - case(GL_ALPHA): RGBA_to_A(num, source, dest); break; - case(GL_LUMINANCE_ALPHA): RGBA_to_LA(num, source, dest); break; - case(GL_RGB): RGBA_to_RGB(num, source, dest); break; - case(GL_RGBA): RGBA_to_RGBA(num, source, dest); break; - } - break; - case(GL_BGR): - switch(dest_pixelFormat) - { - case(GL_LUMINANCE): - case(GL_ALPHA): BGR_to_A(num, source, dest); break; - case(GL_LUMINANCE_ALPHA): BGR_to_LA(num, source, dest); break; - case(GL_RGB): BGR_to_RGB(num, source, dest); break; - case(GL_RGBA): BGR_to_RGBA(num, source, dest); break; - } - break; - case(GL_BGRA): - switch(dest_pixelFormat) - { - case(GL_LUMINANCE): - case(GL_ALPHA): BGRA_to_A(num, source, dest); break; - case(GL_LUMINANCE_ALPHA): BGRA_to_LA(num, source, dest); break; - case(GL_RGB): BGRA_to_RGB(num, source, dest); break; - case(GL_RGBA): BGRA_to_RGBA(num, source, dest); break; - } - break; - } - } - - /////////////////////////////////////////////////////////////////////////////// - // alpha sources.. - virtual void A_to_A(unsigned int num, unsigned char* source, unsigned char* dest) const - { - for(unsigned int i=0;iget(); - GLenum pixelFormat = image->getPixelFormat(); - if (pixelFormat==GL_ALPHA || - pixelFormat==GL_INTENSITY || - pixelFormat==GL_LUMINANCE || - pixelFormat==GL_LUMINANCE_ALPHA || - pixelFormat==GL_RGB || - pixelFormat==GL_RGBA || - pixelFormat==GL_BGR || - pixelFormat==GL_BGRA) - { - max_s = osg::maximum(image->s(), max_s); - max_t = osg::maximum(image->t(), max_t); - max_components = osg::maximum(osg::Image::computeNumComponents(pixelFormat), max_components); - total_r += image->r(); - } - else - { - osg::notify(osg::NOTICE)<<"Image "<getFileName()<<" has unsuitable pixel format 0x"<< std::hex<< pixelFormat << std::dec << std::endl; - } - } - - if (max_components==3) - { - // change RGB to a RGBA - max_components = 4; - } - - if (numComponentsDesired!=0) max_components = numComponentsDesired; - - GLenum desiredPixelFormat = 0; - switch(max_components) - { - case(1): - osg::notify(osg::NOTICE)<<"desiredPixelFormat = GL_LUMINANCE" << std::endl; - desiredPixelFormat = GL_LUMINANCE; - break; - case(2): - osg::notify(osg::NOTICE)<<"desiredPixelFormat = GL_LUMINANCE_ALPHA" << std::endl; - desiredPixelFormat = GL_LUMINANCE_ALPHA; - break; - case(3): - osg::notify(osg::NOTICE)<<"desiredPixelFormat = GL_RGB" << std::endl; - desiredPixelFormat = GL_RGB; - break; - case(4): - osg::notify(osg::NOTICE)<<"desiredPixelFormat = GL_RGBA" << std::endl; - desiredPixelFormat = GL_RGBA; - break; - } - if (desiredPixelFormat==0) return 0; - - // compute nearest powers of two for each axis. - - int s_nearestPowerOfTwo = 1; - int t_nearestPowerOfTwo = 1; - int r_nearestPowerOfTwo = 1; - - if (resizeToPowerOfTwo) - { - while(s_nearestPowerOfTwo