Show
Ignore:
Timestamp:
04/07/06 21:54:44 (8 years ago)
Author:
robert
Message:

Added mipmap generation.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • OpenSceneGraph/trunk/examples/osgprecipitation/osgprecipitation.cpp

    r5078 r5079  
    8282    osg::ref_ptr<osg::Uniform> _uniform;     
    8383}; 
     84 
     85void 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 
     108osg::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 
    84149 
    85150osg::Node* createRainEffect(const osg::BoundingBox& bb, const osg::Vec3& velocity, unsigned int numParticles, bool useShaders) 
     
    164229        stateset->addUniform(baseTextureSampler); 
    165230         
    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); 
    167234        stateset->setTextureAttribute(0, texture); 
    168235 
     
    254321 
    255322    osg::BoundingBox bb(0.0, 0.0, 0.0, 100.0, 100.0, 100.0); 
    256     osg::Vec3 velocity(0.0,0.0,-2.0); 
     323    osg::Vec3 velocity(0.0,2.0,-5.0); 
    257324    unsigned int numParticles = 150000; 
    258325