From c86e2361d20b6b9f78696fc7de5dee55767adef4 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Mon, 19 Dec 2011 09:37:57 +0000 Subject: [PATCH] Moved the createSpotLightImage function into include/osg/ImageUtils --- include/osg/ImageUtils | 5 +- src/osg/ImageUtils.cpp | 73 ++++++++++++++++++++++++- src/osgParticle/PrecipitationEffect.cpp | 72 +----------------------- 3 files changed, 77 insertions(+), 73 deletions(-) diff --git a/include/osg/ImageUtils b/include/osg/ImageUtils index bd4bd7faf..d3f2a0dc4 100644 --- a/include/osg/ImageUtils +++ b/include/osg/ImageUtils @@ -153,7 +153,10 @@ extern OSG_EXPORT osg::Image* createImage3DWithAlpha(const ImageList& imageList, int r_maximumImageSize = 1024, bool resizeToPowerOfTwo = false); - +/** create a 2D osg::Image that provides a point at the center of the image. + * The colour across th image is computed from a balance between the center color and the background color controlled by the power of the radius from the center.*/ +extern OSG_EXPORT osg::Image* createSpotLightImage(const osg::Vec4& centerColour, const osg::Vec4& backgroudColour, unsigned int size, float power); + } diff --git a/src/osg/ImageUtils.cpp b/src/osg/ImageUtils.cpp index edbaa2f82..498241a19 100644 --- a/src/osg/ImageUtils.cpp +++ b/src/osg/ImageUtils.cpp @@ -543,6 +543,77 @@ osg::Image* createImage3DWithAlpha(const ImageList& imageList, } } - + +static void fillSpotLightImage(unsigned char* ptr, const osg::Vec4& centerColour, const osg::Vec4& backgroudColour, unsigned int size, float power) +{ + if (size==1) + { + float r = 0.5f; + osg::Vec4 color = centerColour*r+backgroudColour*(1.0f-r); + *ptr++ = (unsigned char)((color[0])*255.0f); + *ptr++ = (unsigned char)((color[1])*255.0f); + *ptr++ = (unsigned char)((color[2])*255.0f); + *ptr++ = (unsigned char)((color[3])*255.0f); + return; + } + + float mid = (float(size)-1.0f)*0.5f; + float div = 2.0f/float(size); + for(unsigned int r=0;rdata(0,r,0); + for(unsigned int c=0;cdata(0,0,0); + fillSpotLightImage(ptr, centerColour, backgroudColour, size, power); + + return image; +#else + osg::Image* image = new osg::Image; + osg::Image::MipmapDataType mipmapData; + unsigned int s = size; + unsigned int totalSize = 0; + unsigned i; + for(i=0; s>0; s>>=1, ++i) + { + if (i>0) mipmapData.push_back(totalSize); + totalSize += s*s*4; + } + + unsigned char* ptr = new unsigned char[totalSize]; + image->setImage(size, size, size, GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE, ptr, osg::Image::USE_NEW_DELETE,1); + + image->setMipmapLevels(mipmapData); + + s = size; + for(i=0; s>0; s>>=1, ++i) + { + fillSpotLightImage(ptr, centerColour, backgroudColour, s, power); + ptr += s*s*4; + } + + return image; +#endif +} + + } diff --git a/src/osgParticle/PrecipitationEffect.cpp b/src/osgParticle/PrecipitationEffect.cpp index 361a073d6..56d24f9b6 100644 --- a/src/osgParticle/PrecipitationEffect.cpp +++ b/src/osgParticle/PrecipitationEffect.cpp @@ -27,6 +27,7 @@ #include #include #include +#include using namespace osgParticle; @@ -34,77 +35,6 @@ using namespace osgParticle; static float random(float min,float max) { return min + (max-min)*(float)rand()/(float)RAND_MAX; } -static void fillSpotLightImage(unsigned char* ptr, const osg::Vec4& centerColour, const osg::Vec4& backgroudColour, unsigned int size, float power) -{ - if (size==1) - { - float r = 0.5f; - osg::Vec4 color = centerColour*r+backgroudColour*(1.0f-r); - *ptr++ = (unsigned char)((color[0])*255.0f); - *ptr++ = (unsigned char)((color[1])*255.0f); - *ptr++ = (unsigned char)((color[2])*255.0f); - *ptr++ = (unsigned char)((color[3])*255.0f); - return; - } - - float mid = (float(size)-1.0f)*0.5f; - float div = 2.0f/float(size); - for(unsigned int r=0;rdata(0,r,0); - for(unsigned int c=0;cdata(0,0,0); - fillSpotLightImage(ptr, centerColour, backgroudColour, size, power); - - return image; -#else - osg::Image* image = new osg::Image; - osg::Image::MipmapDataType mipmapData; - unsigned int s = size; - unsigned int totalSize = 0; - unsigned i; - for(i=0; s>0; s>>=1, ++i) - { - if (i>0) mipmapData.push_back(totalSize); - totalSize += s*s*4; - } - - unsigned char* ptr = new unsigned char[totalSize]; - image->setImage(size, size, size, GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE, ptr, osg::Image::USE_NEW_DELETE,1); - - image->setMipmapLevels(mipmapData); - - s = size; - for(i=0; s>0; s>>=1, ++i) - { - fillSpotLightImage(ptr, centerColour, backgroudColour, s, power); - ptr += s*s*4; - } - - return image; -#endif -} - - PrecipitationEffect::PrecipitationEffect(): _previousFrameTime(FLT_MAX) {