Ignore:
Timestamp:
04/06/06 20:48:19 (18 years ago)
Author:
igarcia
Message:
 
File:
1 edited

Legend:

Unmodified
Added
Removed
  • GTP/trunk/Lib/Illum/IBRBillboardCloudTrees/OGRE/src/IMGTextureAtlas.cpp

    r721 r731  
    44namespace IMG { 
    55 
    6     TextureAtlas::TextureAtlas() 
     6TextureAtlas::TextureAtlas(): references(0) // initialize references to 0 
     7{ 
     8        mRectangle2D->setBoundingBox(Ogre::AxisAlignedBox(-100000.0 * Ogre::Vector3::UNIT_SCALE, 100000.0 * Ogre::Vector3::UNIT_SCALE)); 
     9        mRectangle2D->setRenderQueueGroup(Ogre::RENDER_QUEUE_BACKGROUND); 
     10} 
     11 
     12TextureAtlas::~TextureAtlas() 
     13{ 
     14        mTextureList.clear(); 
     15} 
     16 
     17unsigned int TextureAtlas::getReference() 
     18{ 
     19        return references; 
     20} 
     21 
     22void TextureAtlas::setTextureAtlasName(Ogre::String textureAtlasName) 
     23{ 
     24        mTextureAtlasName = textureAtlasName; 
     25} 
     26 
     27Ogre::String TextureAtlas::getTextureAtlasName() 
     28{ 
     29        return mTextureAtlasName; 
     30} 
     31 
     32void TextureAtlas::addTexture(TexturePtr texture) 
     33{ 
     34        mTextureList.push_back(texture); 
     35        mTextureAtlasSceneNode->attachObject(texture->getRectangle2D()); 
     36} 
     37 
     38TexturePtr TextureAtlas::getTexture(unsigned int iTexture) 
     39{ 
     40        return mTextureList[iTexture]; 
     41} 
     42 
     43void TextureAtlas::removeTexture(unsigned int iTexture) 
     44{ 
     45        mTextureList.erase(mTextureList.begin()+iTexture); 
     46} 
     47 
     48unsigned int TextureAtlas::getNumTextures() 
     49{ 
     50        return mTextureList.size(); 
     51} 
     52 
     53void TextureAtlas::setTextureAtlasSceneNode(Ogre::SceneNode *textureAtlasSceneNode) 
     54{ 
     55        mTextureAtlasSceneNode = textureAtlasSceneNode;  
     56        mTextureAtlasSceneNode->attachObject(mRectangle2D); 
     57} 
     58 
     59Ogre::SceneNode* TextureAtlas::getTextureAtlasSceneNode() 
     60{ 
     61        return mTextureAtlasSceneNode; 
     62} 
     63 
     64void TextureAtlas::setCamera(Ogre::Camera *camera) 
     65{ 
     66        mCamera = camera; 
     67} 
     68 
     69void TextureAtlas::update() 
     70{ 
     71        mTexture->getBuffer(0, 0)->getRenderTarget()->update(); 
     72} 
     73 
     74void TextureAtlas::save(Ogre::String folderName, Ogre::String fileName, Ogre::PixelFormat pixelFormat) 
     75{        
     76        unsigned char* pBufferBox = new unsigned char[mTexture->getBuffer(0, 0)->getSizeInBytes()]; 
     77        unsigned char* pBufferDest = new unsigned char[mTexture->getBuffer(0, 0)->getSizeInBytes()]; 
     78 
     79    // Read pixels       
     80        Ogre::PixelBox pBox(mWidth, mHeight, 1, mPixelFormat, pBufferBox); 
     81        Ogre::PixelBox pDest(mWidth, mHeight, 1, pixelFormat, pBufferDest);      
     82        mTexture->getBuffer(0, 0)->blitToMemory(pBox); 
     83        Ogre::PixelUtil::bulkPixelConversion(pBox.data,mPixelFormat,pDest.data,pixelFormat,pBox.getWidth()*pBox.getHeight()); 
     84 
     85        //if (pixelFormat == Ogre::PF_FLOAT32_RGBA) 
     86        //{ 
     87        //      imdebug("rgba w=%d h=%d %p b=32f",pDest.getWidth(),pDest.getHeight(),pDest.data); 
     88        //} 
     89 
     90        // copyToMemory 
     91        Ogre::ImageCodec::ImageData *imgData = new Ogre::ImageCodec::ImageData(); 
     92     
     93    imgData->width = mWidth; 
     94    imgData->height = mHeight; 
     95        imgData->depth = 1;      
     96    imgData->format = pixelFormat; 
     97 
     98        // Wrap buffer in a chunk 
     99        Ogre::MemoryDataStreamPtr stream(new Ogre::MemoryDataStream(pBufferDest, mTexture->getBuffer(0, 0)->getSizeInBytes(), false)); 
     100 
     101    // Get codec  
     102    size_t pos = fileName.find_last_of("."); 
     103    Ogre::String extension; 
     104    if( pos == Ogre::String::npos ) 
    7105        { 
     106        OGRE_EXCEPT(Ogre::Exception::ERR_INVALIDPARAMS, "Unable to determine image type for '" + fileName + "' - invalid extension.", "GLRenderTexture::writeContentsToFile" ); 
    8107        } 
    9108 
    10     virtual TextureAtlas::~TextureAtlas() 
     109    while( pos != fileName.length() - 1 ) 
    11110        { 
     111        extension += fileName[++pos]; 
    12112        } 
    13113 
    14     void TextureAtlas::setRenderSourceName(Ogre::String renderSourceName) 
    15         { 
    16                 mRenderSourceName = renderSourceName; 
    17         } 
     114    // Get the codec 
     115    Ogre::Codec * pCodec = Ogre::Codec::getCodec(extension); 
    18116 
    19     Ogre::String TextureAtlas::getRenderSourceName() 
    20         { 
    21                 return mRenderSourceName; 
    22         } 
     117    // Write out 
     118    Ogre::Codec::CodecDataPtr codecDataPtr(imgData); 
    23119 
    24         void TextureAtlas::setRenderSourceName(Ogre::String renderSourceName) 
    25         { 
    26                 mRenderSourceName = renderSourceName; 
    27         } 
     120        pCodec->codeToFile(stream,folderName + fileName, codecDataPtr); 
    28121 
    29         void TextureAtlas::addTexture(Texture *texture) 
    30         { 
    31                 mTextureList.push_back(texture); 
    32         } 
     122        delete [] pBufferDest; 
     123        delete [] pBufferBox; 
     124} 
    33125 
    34         void TextureAtlas::getTexture(unsigned int iTexture) 
    35         { 
    36                 return mTextureList[iTexture]; 
    37         } 
     126void TextureAtlas::setFormat(Ogre::PixelFormat pixelFormat) 
     127{ 
     128        mPixelFormat = pixelFormat; 
     129} 
    38130 
    39         void TextureAtlas::removeTexture(unsigned int iTexture) 
    40         { 
    41                 Texture *texture = mTextureList[iTexture]; 
    42                 mTextureList.erase(mTextureList.begin()+iTexture); 
    43                 delete texture; 
    44         } 
     131void TextureAtlas::create(Ogre::String fileName, unsigned int width, unsigned int height, Ogre::PixelFormat pixelFormat, Ogre::Camera *camera) 
     132{ 
     133        mCamera = camera; 
     134        mWidth = width; 
     135        mHeight = height; 
     136        mPixelFormat = pixelFormat; 
     137        mTexture = Ogre::TextureManager::getSingleton().createManual(fileName, "General", Ogre::TEX_TYPE_2D, mWidth, mHeight, 0, mPixelFormat, Ogre::TU_RENDERTARGET).getPointer(); 
     138        mTexture->getBuffer(0, 0)->getRenderTarget(); 
     139        Ogre::RenderTexture *rt = mTexture->getBuffer()->getRenderTarget(); 
     140        rt->setAutoUpdated(false); 
     141        Ogre::Viewport *v = rt->addViewport(mCamera); 
     142        v->setClearEveryFrame(true); 
     143        v->setBackgroundColour(Ogre::ColourValue(1.0f, 1.0f, 1.0f, 0.0f)); 
     144        v->setOverlaysEnabled(false); 
     145        v->setDimensions(0,0,1.0,1.0);   
     146        Ogre::Root::getSingleton().getRenderSystem()->clearFrameBuffer(Ogre::FBT_COLOUR, Ogre::ColourValue(1.0f,1.0f,1.0f,0.0f) ); 
     147} 
    45148 
    46         unsigned int TextureAtlas::getNumTextures() 
    47         { 
    48                 return mTextureList.size(); 
    49         } 
    50149} 
Note: See TracChangeset for help on using the changeset viewer.