#include "LBBCEntityTextureAtlasViewMode.h" namespace LBBC { EntityTextureAtlasViewMode::EntityTextureAtlasViewMode(Ogre::RenderWindow* win, unsigned int ogreFrameListenerModeHandle, bool useBufferedInputKeys, bool useBufferedInputMouse) :OBA::OgreFrameListenerMode(win, ogreFrameListenerModeHandle, useBufferedInputKeys, useBufferedInputMouse) { mTextureAtlasGenerated = false; mFrame = 0; showDebugOverlay(false); } EntityTextureAtlasViewMode::~EntityTextureAtlasViewMode() { // Gametools -- BUG: 06/04/2006 //destroyScene(); } void EntityTextureAtlasViewMode::setTextureAtlasFolder(Ogre::String textureAtlasFolder) { mTextureAtlasFolder = textureAtlasFolder; } void EntityTextureAtlasViewMode::setTextureName(Ogre::String textureName) { mTextureName = textureName; } void EntityTextureAtlasViewMode::setTextureAtlasName(Ogre::String textureAtlasName) { mTextureAtlasName = textureAtlasName; } void EntityTextureAtlasViewMode::setTextureAtlasSize(unsigned int size) { mTextureAtlasSize = size; } void EntityTextureAtlasViewMode::setTextureAtlasNumSamples(unsigned int numSamples) { mNumSamples = numSamples; } void EntityTextureAtlasViewMode::setTextureAtlasBitRange(unsigned int bitRange) { mBitRange = bitRange; } bool EntityTextureAtlasViewMode::frameStarted(const Ogre::FrameEvent& evt, Ogre::InputReader *inputReader) { mWindow->getViewport(0)->setBackgroundColour(Ogre::ColourValue(1.0, 1.0, 1.0, 1.0)); if (mFrame == 1) { mTextureAtlas->getTextureAtlasSceneNode()->setVisible(false); mTextureAtlas->update(); mTextureAtlas->getTextureAtlasSceneNode()->setVisible(true); } if (mFrame == 2) { if (!mTextureAtlasGenerated) { if (mBitRange == 32) { mDestPixelFormat = Ogre::PF_FLOAT32_RGBA; } else if (mBitRange == 16) { mDestPixelFormat = Ogre::PF_FLOAT16_RGBA; } else // mBitRange == 8 { mDestPixelFormat = Ogre::PF_BYTE_RGBA; } mTextureAtlas->update(); //mTextureAtlas->debug(); mTextureAtlas->save(mTextureAtlasFolder, mTextureAtlasName, mDestPixelFormat); mTextureAtlasGenerated = true; } } mFrame++; return OgreFrameListenerMode::frameStarted(evt, inputReader); } void EntityTextureAtlasViewMode::createScene() { IMG::TexturePtr texturePtr ( new IMG::Texture() ); texturePtr->bind(mTextureName); Ogre::ColourValue avgColour = texturePtr->getAvgColour(); mWindow->getViewport(0)->setBackgroundColour(Ogre::ColourValue(1.0, 1.0, 1.0, 1.0)); mTextureAtlasNode = mSceneMgr->getRootSceneNode()->createChildSceneNode(); mTextureAtlas = IMG::TextureAtlasPtr( new IMG::TextureAtlas() ); if (mBitRange == 32) { mSrcPixelFormat = Ogre::PF_FLOAT32_RGBA; } else if (mBitRange == 16) { mSrcPixelFormat = Ogre::PF_FLOAT16_RGBA; } else // mBitRange == 8 { mSrcPixelFormat = Ogre::PF_A8R8G8B8; } mTextureAtlas->create(mTextureAtlasName, mTextureAtlasSize, mTextureAtlasSize, mSrcPixelFormat, mCamera, avgColour); mTextureAtlas->setTextureAtlasSceneNode(mTextureAtlasNode); unsigned int numSubTextures = mNumSamples; Ogre::Vector2 maxTexCoords(1.0, 1.0); unsigned int irow = 1; unsigned int icol = 1; unsigned int nrow = Ogre::Math::Sqrt(BBC::Util::nextPowerOf2(numSubTextures)); Ogre::Real step = 2.0 / nrow; Ogre::Real degreeStep = 360.0 / numSubTextures; Ogre::Real degree = 0.0; Ogre::MaterialPtr materialPtr = Ogre::MaterialManager::getSingleton().create("TextureAtlas" + Ogre::StringConverter::toString(numSubTextures), Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME); materialPtr->getTechnique(0)->getPass(0)->setAlphaRejectSettings(Ogre::CMPF_GREATER_EQUAL, 10); materialPtr->getTechnique(0)->getPass(0)->createTextureUnitState("transparent.png"); materialPtr->getTechnique(0)->getPass(0)->getTextureUnitState(0)->setTextureFiltering(Ogre::FO_NONE, Ogre::FO_NONE, Ogre::FO_NONE); materialPtr->getTechnique(0)->getPass(0)->getTextureUnitState(0)->setTextureAddressingMode(Ogre::TextureUnitState::TAM_CLAMP); materialPtr->getTechnique(0)->getPass(0)->setLightingEnabled(false); mTextureAtlas->setMaterial(materialPtr.getPointer()); mTextureAtlas->setCorners(-1.0, 1.0, 1.0, -1.0); for (unsigned int iSubTexture = 0; iSubTexture < numSubTextures; iSubTexture++) { IMG::TexturePtr texturePtr = IMG::TexturePtr( new IMG::Texture() ); Ogre::MaterialPtr materialPtr = Ogre::MaterialManager::getSingleton().create("TextureAtlas" + Ogre::StringConverter::toString(iSubTexture), Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME); //materialPtr->getTechnique(0)->getPass(0)->setAmbient(Ogre::Math::RangeRandom(1.0, 0.0), 1.0, 0.0); //materialPtr->getTechnique(0)->getPass(0)->setDiffuse(1.0, 1.0, 1.0, 1.0); materialPtr->getTechnique(0)->getPass(0)->setLightingEnabled(false); materialPtr->getTechnique(0)->getPass(0)->setAlphaRejectSettings(Ogre::CMPF_GREATER_EQUAL, 10); materialPtr->getTechnique(0)->getPass(0)->createTextureUnitState(mTextureName, 0); materialPtr->getTechnique(0)->getPass(0)->getTextureUnitState(0)->setTextureFiltering(Ogre::FO_NONE, Ogre::FO_NONE, Ogre::FO_NONE); materialPtr->getTechnique(0)->getPass(0)->getTextureUnitState(0)->setTextureAddressingMode(Ogre::TextureUnitState::TAM_CLAMP); materialPtr->getTechnique(0)->getPass(0)->getTextureUnitState(0)->setTextureRotate(Ogre::Radian(Ogre::Degree(degree))); texturePtr->setMaterial(materialPtr.getPointer()); texturePtr->setCorners(maxTexCoords[0] - step, maxTexCoords[1], maxTexCoords[0], maxTexCoords[1] - step); mTextureAtlas->addTexture(texturePtr); if (irow == nrow) { irow = 1; icol++; maxTexCoords = Ogre::Vector2(1.0, maxTexCoords[1] - step); } else { irow++; maxTexCoords = Ogre::Vector2(maxTexCoords[0] - step, maxTexCoords[1]); } degree = degree + degreeStep; } } void EntityTextureAtlasViewMode::destroyScene() { mTextureAtlasNode->detachAllObjects(); } }