| | 84 | |
| | 85 | void fillSpotLightImage(unsigned char* ptr, const osg::Vec4& centerColour, const osg::Vec4& backgroudColour, unsigned int size, float power) |
| | 86 | { |
| | 87 | float mid = (float(size)-1.0f)*0.5f; |
| | 88 | float div = 2.0f/float(size); |
| | 89 | for(unsigned int r=0;r<size;++r) |
| | 90 | { |
| | 91 | //unsigned char* ptr = image->data(0,r,0); |
| | 92 | for(unsigned int c=0;c<size;++c) |
| | 93 | { |
| | 94 | float dx = (float(c) - mid)*div; |
| | 95 | float dy = (float(r) - mid)*div; |
| | 96 | float r = powf(1.0f-sqrtf(dx*dx+dy*dy),power); |
| | 97 | if (r<0.0f) r=0.0f; |
| | 98 | osg::Vec4 color = centerColour*r+backgroudColour*(1.0f-r); |
| | 99 | *ptr++ = (unsigned char)((color[0])*255.0f); |
| | 100 | *ptr++ = (unsigned char)((color[1])*255.0f); |
| | 101 | *ptr++ = (unsigned char)((color[2])*255.0f); |
| | 102 | *ptr++ = (unsigned char)((color[3])*255.0f); |
| | 103 | } |
| | 104 | } |
| | 105 | } |
| | 106 | |
| | 107 | |
| | 108 | osg::Image* createSpotLightImage(const osg::Vec4& centerColour, const osg::Vec4& backgroudColour, unsigned int size, float power) |
| | 109 | { |
| | 110 | osg::Image* image = new osg::Image; |
| | 111 | |
| | 112 | #if 0 |
| | 113 | |
| | 114 | |
| | 115 | unsigned char* ptr = image->data(0,0,0); |
| | 116 | fillSpotLightImage(ptr, centerColour, backgroudColour, size, power); |
| | 117 | #else |
| | 118 | osg::Image::MipmapDataType mipmapData; |
| | 119 | unsigned int s = size; |
| | 120 | unsigned int totalSize = 0; |
| | 121 | unsigned i; |
| | 122 | for(i=0; s>0; s>>=1, ++i) |
| | 123 | { |
| | 124 | if (i>0) mipmapData.push_back(totalSize); |
| | 125 | totalSize += s*s*4; |
| | 126 | std::cout<<" i= "<<i<<" s="<<s<<" p="<<totalSize<<std::endl; |
| | 127 | } |
| | 128 | |
| | 129 | std::cout<<"Total size ="<<totalSize<<std::endl; |
| | 130 | |
| | 131 | unsigned char* ptr = new unsigned char[totalSize]; |
| | 132 | image->setImage(size, size, size, GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE, ptr, osg::Image::USE_NEW_DELETE,1); |
| | 133 | |
| | 134 | image->setMipmapLevels(mipmapData); |
| | 135 | |
| | 136 | s = size; |
| | 137 | for(i=0; s>0; s>>=1, ++i) |
| | 138 | { |
| | 139 | fillSpotLightImage(ptr, centerColour, backgroudColour, s, power); |
| | 140 | ptr += s*s*4; |
| | 141 | } |
| | 142 | |
| | 143 | #endif |
| | 144 | |
| | 145 | |
| | 146 | return image; |
| | 147 | } |
| | 148 | |
| 166 | | osg::Texture2D* texture = new osg::Texture2D(osgDB::readImageFile("Images/particle.rgb")); |
| | 231 | // osg::Texture2D* texture = new osg::Texture2D(osgDB::readImageFile("Images/particle.rgb")); |
| | 232 | osg::Texture2D* texture = new osg::Texture2D(createSpotLightImage(osg::Vec4(1.0f,1.0f,1.0f,1.0f),osg::Vec4(1.0f,1.0f,1.0f,0.0f),32,1.0)); |
| | 233 | // texture->setFilter(osg::Texture2D::MIN_FILTER,osg::Texture2D::LINEAR); |