void ResourceGroupReloader::updateOnEveryRenderable() { //1/ get all the available object type (entity, light, user defined types ...) std::vector<std::string> allAvailableTypes; Ogre::Root::MovableObjectFactoryIterator iterFactory = Ogre::Root::getSingleton().getMovableObjectFactoryIterator(); for(;iterFactory.hasMoreElements();) { Ogre::MovableObjectFactory* factory = iterFactory.getNext(); allAvailableTypes.push_back(factory->getType()); } UpdateMaterialRenderableVisitor lRenderableVisitor; //2/ for each scene manager, lets visit renderables! // unfortunately that does not cover all renderables type... (overlays...) Ogre::SceneManagerEnumerator::SceneManagerIterator iterSceneManager = Ogre::Root::getSingleton().getSceneManagerIterator(); for(;iterSceneManager.hasMoreElements();) { Ogre::SceneManager * scMgr = iterSceneManager.getNext(); std::vector<std::string>::iterator iterMovableType = allAvailableTypes.begin(); std::vector<std::string>::iterator iterMovableTypeEnd = allAvailableTypes.end(); for(;iterMovableType!=iterMovableTypeEnd;iterMovableType++) { Ogre::SceneManager::MovableObjectIterator iterMovable = scMgr->getMovableObjectIterator(*iterMovableType); for(;iterMovable.hasMoreElements();) { Ogre::MovableObject * movable = iterMovable.getNext(); movable->visitRenderables(&lRenderableVisitor,false); } } } // 3 / visit overlays! { Ogre::OverlayManager::OverlayMapIterator iterOverlay = Ogre::OverlayManager::getSingleton().getOverlayIterator(); for(;iterOverlay.hasMoreElements();) { Ogre::Overlay* lOverlay = iterOverlay.getNext(); // get the first level of OverlayContainer in the Overlay Ogre::Overlay::Overlay2DElementsIterator iterOverlayElem = lOverlay->get2DElementsIterator(); for(;iterOverlayElem.hasMoreElements();) { Ogre::OverlayContainer * lOverlayCont = iterOverlayElem.getNext(); visitRecursivelyRenderablesFrom(lOverlayCont,lRenderableVisitor, false); } } } }
void Player::stopAnimation(std::string anim) { Ogre::SceneManager::MovableObjectIterator iterator = SceneManager()->getMovableObjectIterator("Entity"); while(iterator.hasMoreElements()) { Ogre::Entity* e = static_cast<Ogre::Entity*>(iterator.getNext()); if (e->hasSkeleton()) { Ogre::AnimationState *animation = e->getAnimationState(anim); animation->setEnabled(false); animation->setTimePosition(0); } } }
MapCameraLightningInstance::MapCameraLightningInstance(MapCameraLightning& lightning) : mLightning(lightning) { Ogre::SceneManager::MovableObjectIterator iterator = lightning.getSceneManager().getMovableObjectIterator(Ogre::LightFactory::FACTORY_TYPE_NAME); while (iterator.hasMoreElements()) { Ogre::MovableObject* light = iterator.getNext(); if (light && light != mLightning.getLight()) { if (light->getVisible()) { mVisibleLights.push_back(light); light->setVisible(false); } } } mLightning.getLight()->setVisible(true); mAmbientColour = mLightning.getSceneManager().getAmbientLight(); mLightning.getSceneManager().setAmbientLight(mLightning.getLight()->getDiffuseColour()); }
// Manage physics from this loop // Please don't alter the Ogre scene manager directly. Instead use the designated methods (setPositionX, setPositionY, setPositionZ, setRoll, setPitch, setYaw, animate) void loop(Ogre::SceneManager* smgr){ int dir = 1; int timeStep = 10; while(true){ // If the message array has any elements, the render loop is applying changes from physics loop // If so, abort this timestep if((*::message).size() == 1){ continue; } Sleep(timeStep); Ogre::SceneManager::MovableObjectIterator iterator = smgr->getMovableObjectIterator("Entity"); while(iterator.hasMoreElements()){ // If the message array has any elements, the render loop is applying changes from physics loop // If so, abort this timestep if((*::message).size() == 1){ continue; } Ogre::Entity* entity = static_cast<Ogre::Entity*>(iterator.getNext()); // <>< <>< Make the cute fishy swim <>< <>< // This only moves fish if(entity->hasAnimationState("swim")){ Ogre::SceneNode* sceneNode = entity->getParentSceneNode(); // Update position Ogre::Vector3 pos = sceneNode->getPosition(); if(pos.x > 120){ dir = -1; setYaw(entity, 180); } if(pos.x < -120){ dir = 1; setYaw(entity, 180); } setPositionX(entity, sceneNode->getPosition().x + dir); animate(entity, "swim"); } } } }
void Player::disableAnimations() { Ogre::SceneManager::MovableObjectIterator iterator = SceneManager()->getMovableObjectIterator("Entity"); while(iterator.hasMoreElements()) { Ogre::Entity* e = static_cast<Ogre::Entity*>(iterator.getNext()); if (e->hasSkeleton()) { Ogre::AnimationStateIterator iter = e->getAllAnimationStates()->getAnimationStateIterator(); while(iter.hasMoreElements()) { Ogre::AnimationState *animState = iter.getNext(); animState->setEnabled(false); } } } }
/* crouch_idle | crouch_to_stand | crouch_walk | fall_idle | fall_to_roll * gangname_style | idle0 | idle1 | idle2 | jump | turnLeft | turnLeft90 * turnRight | turnRight90 | run | run_to_stop | samba | strafeLeft * strafeRight | the_running_man | walkBackward | walkForward * (Example: playAnimation("jump", time);) */ void Player::playAnimation(std::string anim, float time) { disableAnimations(); Ogre::SceneManager::MovableObjectIterator iterator = SceneManager()->getMovableObjectIterator("Entity"); while(iterator.hasMoreElements()) { Ogre::Entity* e = static_cast<Ogre::Entity*>(iterator.getNext()); if (e->hasSkeleton()) { Ogre::AnimationState *animation = e->getAnimationState(anim); /*if (animation->getAnimationName().compare("jump") == 0) finishAnimation(animation, time); */ animation->setEnabled("true"); animation->addTime(time); } } }
void ReflectionTextureListener::preRenderTargetUpdate(const RenderTargetEvent& evt) { // Hide plane and objects below the water mWater->setVisible(false); /*list<Ogre::Entity*> entList = Level::getSingleton()->getEntityList(); list<Ogre::Entity*>::iterator it; for (it = entList.begin();it != entList.end();it++){ if ((*it)->isVisible()){ Ogre::AxisAlignedBox box = (*it)->getWorldBoundingBox(); if (box.getMinimum().y < mWater->getSceneNode()->_getDerivedPosition().y){ (*it)->setVisible(false); belowWaterEnts.push_back((*it)); } } }*/ /*std::vector<Entity*>::iterator i, iend; iend = belowWaterEnts.end(); for (i = belowWaterEnts.begin(); i != iend; ++i) { (*i)->setVisible(false); }*/ Ogre::SceneManager::MovableObjectIterator it = Level::getSingleton()->getSceneManager()->getMovableObjectIterator("Entity"); while(it.hasMoreElements()){ Ogre::MovableObject *m = it.getNext(); if (m->isVisible() && m->getParentSceneNode()->_getDerivedPosition().y < mWater->getSceneNode()->_getDerivedPosition().y){ m->setVisible(false); belowWaterEnts.push_back(m); } } Ogre::Camera* cam = Level::getSingleton()->getCurrentSegment()->getActiveCamera()->getCamera(); cam->enableReflection(mWater->getReflectionPlane()); }
bool MiniMapMaker::outputTextures(void) { // 如果需要(纹理大小改变了或第一次输出文件时),就重建render texture if (mNeedRecreate) { destroy(); init(); } mTempOutputFileNames.clear(); static const String TEMP_GROUP_NAME = "#TEMP#"; // 创建临时的资源组 Ogre::ResourceGroupManager& rgm = Ogre::ResourceGroupManager::getSingleton(); rgm.addResourceLocation(mPath, "FileSystem", TEMP_GROUP_NAME, false); // 合并所有物体的包围盒 Ogre::AxisAlignedBox aabb; Ogre::SceneManager::MovableObjectIterator itm = mManipulator->getSceneManager()->getMovableObjectIterator(Ogre::EntityFactory::FACTORY_TYPE_NAME); while (itm.hasMoreElements()) { Ogre::MovableObject* movable = itm.getNext(); aabb.merge(movable->getWorldBoundingBox(true)); } mCamera->setFarClipDistance(mCamera->getNearClipDistance() + 2 * (aabb.getMaximum().y - aabb.getMinimum().y )); mCamera->setNearClipDistance(mTileSize/2); // 设置摄像机的高度 Real yPos = mCamera->getNearClipDistance() + aabb.getMaximum().y; TerrainData* terrainData = mManipulator->getTerrainData(); assert (terrainData); float terrainHeight = terrainData->mMaxZ - terrainData->mMinZ; float terrainWidth = terrainData->mMaxX - terrainData->mMinX; // 投影的真正面积 Real projectSize = 0.0f; // 最终切割成小块纹理的块数 int xIndex = 0; int zIndex = 0; Ogre::Vector3 originPoint(Ogre::Vector3::ZERO); if (mUseRealCameraAngle) { float outerSquareWidth = 0.0f; float outerSquareHeight = 0.0f; Ogre::Radian alphaAngle = Ogre::Math::ATan( Ogre::Math::Abs(mMoveZDir.z / mMoveZDir.x) ); switch (mCameraDirQuadrant) { case WestNorth : { float leftWidth = Ogre::Math::Sin(alphaAngle) * terrainHeight; float rightWidth = Ogre::Math::Cos(alphaAngle) * terrainWidth; outerSquareWidth = leftWidth + rightWidth; float topHeight = Ogre::Math::Cos(alphaAngle) * terrainHeight; float bottomHeight = Ogre::Math::Sin(alphaAngle) * terrainWidth; outerSquareHeight = topHeight + bottomHeight; originPoint = Ogre::Vector3(terrainData->mMinX,0,terrainData->mMinZ) + (-mMoveZDir * leftWidth); float projectOffset = yPos / Ogre::Math::Tan(mCamDirAngle); originPoint.x += (mInvertCameraDir * projectOffset ).x; originPoint.z += (mInvertCameraDir * projectOffset ).z; break; } case EastNorth : { float leftWidth = Ogre::Math::Cos(alphaAngle) * terrainWidth; float rightWidth = Ogre::Math::Sin(alphaAngle) * terrainHeight; outerSquareWidth = leftWidth + rightWidth; float topHeight = Ogre::Math::Cos(alphaAngle) * terrainHeight; float bottomHeight = Ogre::Math::Sin(alphaAngle) * terrainWidth; outerSquareHeight = topHeight + bottomHeight; originPoint = Ogre::Vector3(terrainData->mMaxX,0,terrainData->mMinZ) + (-mMoveZDir * leftWidth); float projectOffset = yPos / Ogre::Math::Tan(mCamDirAngle); originPoint.x += (mInvertCameraDir * projectOffset ).x; originPoint.z += (mInvertCameraDir * projectOffset ).z; break; } case EastSouth : { float leftWidth = Ogre::Math::Sin(alphaAngle) * terrainHeight; float rightWidth = Ogre::Math::Cos(alphaAngle) * terrainWidth; outerSquareWidth = leftWidth + rightWidth; float topHeight = Ogre::Math::Sin(alphaAngle) * terrainWidth; float bottomHeight = Ogre::Math::Cos(alphaAngle) * terrainHeight; outerSquareHeight = topHeight + bottomHeight; originPoint = Ogre::Vector3(terrainData->mMaxX,0,terrainData->mMaxZ) + (-mMoveZDir * topHeight); float projectOffset = yPos / Ogre::Math::Tan(mCamDirAngle); originPoint.x += (mInvertCameraDir * projectOffset ).x; originPoint.z += (mInvertCameraDir * projectOffset ).z; break; } case WestSouth : { float leftWidth = Ogre::Math::Sin(alphaAngle) * terrainHeight; float rightWidth = Ogre::Math::Cos(alphaAngle) * terrainWidth; outerSquareWidth = leftWidth + rightWidth; float topHeight = Ogre::Math::Sin(alphaAngle) * terrainWidth; float bottomHeight = Ogre::Math::Cos(alphaAngle) * terrainHeight; outerSquareHeight = topHeight + bottomHeight; originPoint = Ogre::Vector3(terrainData->mMinX,0,terrainData->mMaxZ) + (-mMoveZDir * rightWidth); float projectOffset = yPos / Ogre::Math::Tan(mCamDirAngle); originPoint.x += (mInvertCameraDir * projectOffset ).x; originPoint.z += (mInvertCameraDir * projectOffset ).z; break; } default: { OGRE_EXCEPT(Ogre::Exception::ERR_INTERNAL_ERROR, " wrong camera dir " + Ogre::StringConverter::toString(mCameraDir), "MiniMapMaker::outputTextures"); break; } } // 计算投影的长度 Real factor = Ogre::Math::Sin(mCamDirAngle); if (factor > 0.0f && factor != 1.0f) projectSize = mTileSize / factor; // 根据当前场景的大小,计算需要的分块数 xIndex = Ogre::Math::Ceil( (outerSquareWidth) / mTileSize ) + 1; zIndex = Ogre::Math::Ceil( (outerSquareHeight) / projectSize ) + 1; } else { xIndex = Ogre::Math::Ceil( (terrainData->mMaxX - terrainData->mMinX) / mTileSize ) + 1; zIndex = Ogre::Math::Ceil( (terrainData->mMaxZ - terrainData->mMinZ) / mTileSize ) + 1; originPoint.x = terrainData->mMinX; originPoint.z = terrainData->mMinZ; } // 计算最终的mini map的大小 uint miniMapWidth = xIndex * mTexWidth; uint miniMapHeight = zIndex * mTexHeight; if ( miniMapWidth > 10000 || miniMapHeight > 10000 ) { mLastErrorString = "texture size is out of range!"; return false; } // 创建mini map所需的内存空间 uchar* miniMapData = new uchar[miniMapWidth * miniMapHeight * Ogre::PixelUtil::getNumElemBytes(mOutPutFormat)]; //// 初始的摄像机位置 Real xPos = originPoint.x; Real zPos = originPoint.z; for ( int i=0; i<xIndex; ++i ) { for ( int j=0; j<zIndex; ++j ) { // 设置摄像机位置,并更新render texture的内容 mCamera->setPosition(xPos, yPos, zPos); mRenderTexture->update(); String fileName = mPath + mSceneBaseName + Ogre::StringConverter::toString(i) + "_" + Ogre::StringConverter::toString(j) + "." + mTexExtension; // 输出小纹理文件 mRenderTexture->writeContentsToFile(fileName); mTempOutputFileNames.push_back(fileName); // 读取刚创建的纹理 Ogre::Image* tempImage = new Ogre::Image; tempImage->load(mSceneBaseName + Ogre::StringConverter::toString(i) + "_" + Ogre::StringConverter::toString(j) + "." + mTexExtension, TEMP_GROUP_NAME); // 获取render texture中的内容 uchar* tempImageData = tempImage->getData(); // 定位在mini map中的左上角 uint miniMapIndex = ( j * mTexHeight * miniMapWidth + i * mTexWidth ) * Ogre::PixelUtil::getNumElemBytes(mOutPutFormat); uchar* startData = miniMapData + miniMapIndex; for ( size_t height = 0; height < tempImage->getHeight(); ++height ) { for ( size_t width = 0; width < tempImage->getWidth(); ++width ) { memcpy(startData, tempImageData, Ogre::PixelUtil::getNumElemBytes(mOutPutFormat)); startData += Ogre::PixelUtil::getNumElemBytes(mOutPutFormat); tempImageData += Ogre::PixelUtil::getNumElemBytes( tempImage->getFormat() ); } startData += (miniMapWidth - tempImage->getWidth()) * Ogre::PixelUtil::getNumElemBytes(mOutPutFormat); } delete tempImage; // 移动摄像机的z坐标 if (mUseRealCameraAngle) { zPos += (mInvertCameraDir * (projectSize)).z; xPos += (mInvertCameraDir * (projectSize)).x; } else zPos += mTileSize; } if (mUseRealCameraAngle) { xPos = originPoint.x; zPos = originPoint.z; xPos += (mMoveZDir * (mTileSize) * (i+1)).x; zPos += (mMoveZDir * (mTileSize) * (i+1)).z; } else { // 操作完每一列之后,重置摄像机的z坐标 zPos = terrainData->mMinZ; // 移动摄像机的x坐标 xPos += mTileSize; } } // 保存mini map并输出 Ogre::Image* miniMapImage = new Ogre::Image; miniMapImage->loadDynamicImage(miniMapData, miniMapWidth, miniMapHeight, 1, mOutPutFormat, true); miniMapImage->save(mPath + mOutFileName + "." + mTexExtension); delete miniMapImage; rgm.destroyResourceGroup(TEMP_GROUP_NAME); return true; }
void ImpostorTexture::renderTextures(bool force) { #ifdef IMPOSTOR_FILE_SAVE TexturePtr renderTexture; #else TexturePtr renderTexture(texture); //if we're not using a file image we need to set up a resource loader, so that the texture is regenerated if it's ever unloaded (such as switching between fullscreen and the desktop in win32) loader = std::auto_ptr<ImpostorTextureResourceLoader>(new ImpostorTextureResourceLoader(*this)); #endif RenderTexture *renderTarget; Camera *renderCamera; Viewport *renderViewport; SceneNode *camNode; //Set up RTT texture uint32 textureSize = ImpostorPage::impostorResolution; if (renderTexture.isNull()) { renderTexture = TextureManager::getSingleton().createManual(getUniqueID("ImpostorTexture"), "Impostors", TEX_TYPE_2D, textureSize * IMPOSTOR_YAW_ANGLES, textureSize * IMPOSTOR_PITCH_ANGLES, 0, PF_A8R8G8B8, TU_RENDERTARGET, loader.get()); } renderTexture->setNumMipmaps(MIP_UNLIMITED); //Set up render target renderTarget = renderTexture->getBuffer()->getRenderTarget(); renderTarget->setAutoUpdated(false); //Set up camera camNode = sceneMgr->getSceneNode("ImpostorPage::cameraNode"); renderCamera = sceneMgr->createCamera(getUniqueID("ImpostorCam")); camNode->attachObject(renderCamera); renderCamera->setLodBias(1000.0f); renderViewport = renderTarget->addViewport(renderCamera); renderViewport->setOverlaysEnabled(false); renderViewport->setClearEveryFrame(true); renderViewport->setShadowsEnabled(false); renderViewport->setBackgroundColour(ImpostorPage::impostorBackgroundColor); //Set up scene node SceneNode* node = sceneMgr->getSceneNode("ImpostorPage::renderNode"); Ogre::SceneNode* oldSceneNode = entity->getParentSceneNode(); if (oldSceneNode) { oldSceneNode->detachObject(entity); } node->attachObject(entity); node->setPosition(-entityCenter); //Set up camera FOV const Real objDist = entityRadius * 100; const Real nearDist = objDist - (entityRadius + 1); const Real farDist = objDist + (entityRadius + 1); renderCamera->setAspectRatio(1.0f); renderCamera->setFOVy(Math::ATan(entityDiameter / objDist)); renderCamera->setNearClipDistance(nearDist); renderCamera->setFarClipDistance(farDist); //Disable mipmapping (without this, masked textures look bad) MaterialManager *mm = MaterialManager::getSingletonPtr(); FilterOptions oldMinFilter = mm->getDefaultTextureFiltering(FT_MIN); FilterOptions oldMagFilter = mm->getDefaultTextureFiltering(FT_MAG); FilterOptions oldMipFilter = mm->getDefaultTextureFiltering(FT_MIP); mm->setDefaultTextureFiltering(FO_POINT, FO_LINEAR, FO_NONE); //Disable fog FogMode oldFogMode = sceneMgr->getFogMode(); ColourValue oldFogColor = sceneMgr->getFogColour(); Real oldFogDensity = sceneMgr->getFogDensity(); Real oldFogStart = sceneMgr->getFogStart(); Real oldFogEnd = sceneMgr->getFogEnd(); sceneMgr->setFog(Ogre::FOG_EXP2, Ogre::ColourValue(0,0,0,0), 0.0f, 0.0f, 0.0f); //Ember change //We need to disable all lightning and render it full bright Ogre::ColourValue oldAmbientColour = sceneMgr->getAmbientLight(); sceneMgr->setAmbientLight(ColourValue::White); std::vector<Ogre::MovableObject*> lightStore; Ogre::SceneManager::MovableObjectIterator lightIterator = sceneMgr->getMovableObjectIterator(Ogre::LightFactory::FACTORY_TYPE_NAME); while (lightIterator.hasMoreElements()) { Ogre::MovableObject* light = lightIterator.getNext(); if (light) { if (light->getVisible()) { lightStore.push_back(light); light->setVisible(false); } } } // Get current status of the queue mode Ogre::SceneManager::SpecialCaseRenderQueueMode OldSpecialCaseRenderQueueMode = sceneMgr->getSpecialCaseRenderQueueMode(); //Only render the entity sceneMgr->setSpecialCaseRenderQueueMode(Ogre::SceneManager::SCRQM_INCLUDE); sceneMgr->addSpecialCaseRenderQueue(group->geom->getRenderQueue() + 1); uint8 oldRenderQueueGroup = entity->getRenderQueueGroup(); entity->setRenderQueueGroup(group->geom->getRenderQueue() + 1); bool oldVisible = entity->getVisible(); entity->setVisible(true); float oldMaxDistance = entity->getRenderingDistance(); entity->setRenderingDistance(0); bool needsRegen = true; #ifdef IMPOSTOR_FILE_SAVE //Calculate the filename hash used to uniquely identity this render String strKey = entityKey; char key[32] = {0}; uint32 i = 0; for (String::const_iterator it = entityKey.begin(); it != entityKey.end(); ++it) { key[i] ^= *it; i = (i+1) % sizeof(key); } for (i = 0; i < sizeof(key); ++i) key[i] = (key[i] % 26) + 'A'; String tempdir = this->group->geom->getTempdir(); ResourceGroupManager::getSingleton().addResourceLocation(tempdir, "FileSystem", "BinFolder"); String fileNamePNG = "Impostor." + String(key, sizeof(key)) + '.' + StringConverter::toString(textureSize) + ".png"; String fileNameDDS = "Impostor." + String(key, sizeof(key)) + '.' + StringConverter::toString(textureSize) + ".dds"; //Attempt to load the pre-render file if allowed needsRegen = force; if (!needsRegen){ try{ texture = TextureManager::getSingleton().load(fileNameDDS, "BinFolder", TEX_TYPE_2D, MIP_UNLIMITED); } catch (...){ try{ texture = TextureManager::getSingleton().load(fileNamePNG, "BinFolder", TEX_TYPE_2D, MIP_UNLIMITED); } catch (...){ needsRegen = true; } } } #endif if (needsRegen){ //If this has not been pre-rendered, do so now const float xDivFactor = 1.0f / IMPOSTOR_YAW_ANGLES; const float yDivFactor = 1.0f / IMPOSTOR_PITCH_ANGLES; for (int o = 0; o < IMPOSTOR_PITCH_ANGLES; ++o){ //4 pitch angle renders #ifdef IMPOSTOR_RENDER_ABOVE_ONLY Radian pitch = Degree((90.0f * o) * yDivFactor); //0, 22.5, 45, 67.5 #else Radian pitch = Degree((180.0f * o) * yDivFactor - 90.0f); #endif for (int i = 0; i < IMPOSTOR_YAW_ANGLES; ++i){ //8 yaw angle renders Radian yaw = Degree((360.0f * i) * xDivFactor); //0, 45, 90, 135, 180, 225, 270, 315 //Position camera camNode->setPosition(0, 0, 0); camNode->setOrientation(Quaternion(yaw, Vector3::UNIT_Y) * Quaternion(-pitch, Vector3::UNIT_X)); camNode->translate(Vector3(0, 0, objDist), Node::TS_LOCAL); //Render the impostor renderViewport->setDimensions((float)(i) * xDivFactor, (float)(o) * yDivFactor, xDivFactor, yDivFactor); renderTarget->update(); } } #ifdef IMPOSTOR_FILE_SAVE //Save RTT to file with respecting the temp dir renderTarget->writeContentsToFile(tempdir + fileNamePNG); //Load the render into the appropriate texture view texture = TextureManager::getSingleton().load(fileNamePNG, "BinFolder", TEX_TYPE_2D, MIP_UNLIMITED); #else texture = renderTexture; #endif } entity->setVisible(oldVisible); entity->setRenderQueueGroup(oldRenderQueueGroup); entity->setRenderingDistance(oldMaxDistance); sceneMgr->removeSpecialCaseRenderQueue(group->geom->getRenderQueue() + 1); // Restore original state sceneMgr->setSpecialCaseRenderQueueMode(OldSpecialCaseRenderQueueMode); //Re-enable mipmapping mm->setDefaultTextureFiltering(oldMinFilter, oldMagFilter, oldMipFilter); //Re-enable fog sceneMgr->setFog(oldFogMode, oldFogColor, oldFogDensity, oldFogStart, oldFogEnd); //Re-enable both scene lightning and disabled individual lights sceneMgr->setAmbientLight(oldAmbientColour); for (std::vector<Ogre::MovableObject*>::const_iterator I = lightStore.begin(); I != lightStore.end(); ++I) { (*I)->setVisible(true); } //Delete camera renderTarget->removeViewport(0); renderCamera->getSceneManager()->destroyCamera(renderCamera); //Delete scene node node->detachAllObjects(); if (oldSceneNode) { oldSceneNode->attachObject(entity); } #ifdef IMPOSTOR_FILE_SAVE //Delete RTT texture assert(!renderTexture.isNull()); String texName2(renderTexture->getName()); renderTexture.setNull(); if (TextureManager::getSingletonPtr()) TextureManager::getSingleton().remove(texName2); #endif }
//----------------------------------------------------------------------------------------- bool CTerrainGroupEditor::load(bool async) { if(mLoaded->get()) return true; if(!getParent()->load()) return false; Ogre::ResourceGroupManager *mngr = Ogre::ResourceGroupManager::getSingletonPtr(); Ogre::String terrainDir = OgitorsRoot::getSingletonPtr()->GetProjectOptions()->TerrainDirectory; terrainDir = mOgitorsRoot->GetProjectFile()->getFileSystemName() + "::/" + terrainDir + "/"; mngr->addResourceLocation(terrainDir + "textures/normalheight", "Ofs", "TerrainGroupNormalHeight"); mngr->initialiseResourceGroup("TerrainGroupNormalHeight"); mngr->addResourceLocation(terrainDir + "textures/diffusespecular", "Ofs", "TerrainGroupDiffuseSpecular"); mngr->initialiseResourceGroup("TerrainGroupDiffuseSpecular"); mngr->addResourceLocation(terrainDir + "plants", "Ofs", "TerrainGroupPlants"); mngr->initialiseResourceGroup("TerrainGroupPlants"); OgitorsRoot::getSingletonPtr()->PrepareTerrainResources(); OgitorsRoot::getSingletonPtr()->ReloadUserResources(); mDecalFrustum = OGRE_NEW Ogre::Frustum(); mDecalNode = getSceneManager()->getRootSceneNode()->createChildSceneNode("OgitorTerrainDecalNode"); mDecalNode->setPosition(99999, -99999, 99999); mDecalNode->attachObject(mDecalFrustum); mDecalFrustum->setProjectionType(Ogre::PT_ORTHOGRAPHIC); mDecalNode->setOrientation(Ogre::Quaternion(Ogre::Degree(-90), Ogre::Vector3::UNIT_X)); mDecalFrustum->setFOVy(Ogre::Degree(45)); mDecalFrustum->setNearClipDistance(10); mDecalFrustum->setOrthoWindow(10, 10); mDecalFrustum->setVisible(false); mDecalTexture = Ogre::TextureManager::getSingletonPtr()->createManual("OgitorDecalTexture", "TerrainResources", Ogre::TEX_TYPE_2D, 256, 256, 1, 1, Ogre::PF_A8R8G8B8, Ogre::TU_DYNAMIC_WRITE_ONLY, this); mBrushData = OGRE_ALLOC_T(float, BRUSH_DATA_SIZE * BRUSH_DATA_SIZE, Ogre::MEMCATEGORY_GEOMETRY); mTerrainGlobalOptions->setMaxPixelError(mMaxPixelError->get()); mTerrainGlobalOptions->setCompositeMapSize(mCompositeMapTextureSize->get()); mTerrainGlobalOptions->setCompositeMapDistance(mCompositeMapDistance->get()); mTerrainGlobalOptions->setLightMapSize(mLightMapTextureSize->get()); mTerrainGlobalOptions->setLayerBlendMapSize(mBlendMapTextureSize->get()); mTerrainGlobalOptions->setUseVertexCompressionWhenAvailable(false); mTerrainGlobalOptions->setSkirtSize(mSkirtSize->get()); mTerrainGlobalOptions->setUseRayBoxDistanceCalculation(mUseRayBoxDistanceCalculation->get()); if(mMaterialGeneratorType->get() == 1) { Ogre::TerrainMaterialGeneratorPtr matGenPtr(OGRE_NEW Ogre::TerrainMaterialGeneratorC(this, mDecalFrustum)); mTerrainGlobalOptions->setDefaultMaterialGenerator(matGenPtr); mMaxLayersAllowed = 10; } else { Ogre::TerrainMaterialGeneratorPtr matGenPtr(OGRE_NEW Ogre::TerrainMaterialGeneratorB(this, mDecalFrustum)); mTerrainGlobalOptions->setDefaultMaterialGenerator(matGenPtr); mMaxLayersAllowed = 6; } CSceneManagerEditor *mSceneMgr = static_cast<CSceneManagerEditor*>(mOgitorsRoot->GetSceneManagerEditor()); if(mSceneMgr->getShadowsEnabled()) { if(mMaterialGeneratorType->get() == 1) { Ogre::TerrainMaterialGeneratorC::SM2Profile* matProfile = static_cast<Ogre::TerrainMaterialGeneratorC::SM2Profile*>(mTerrainGlobalOptions->getDefaultMaterialGenerator()->getActiveProfile()); matProfile->setReceiveDynamicShadowsEnabled(true); matProfile->setReceiveDynamicShadowsLowLod(false); matProfile->setReceiveDynamicShadowsDepth(true); matProfile->setReceiveDynamicShadowsPSSM(static_cast<Ogre::PSSMShadowCameraSetup*>(mSceneMgr->getPSSMSetup().get())); } else { Ogre::TerrainMaterialGeneratorB::SM2Profile* matProfile = static_cast<Ogre::TerrainMaterialGeneratorB::SM2Profile*>(mTerrainGlobalOptions->getDefaultMaterialGenerator()->getActiveProfile()); matProfile->setReceiveDynamicShadowsEnabled(true); matProfile->setReceiveDynamicShadowsLowLod(false); matProfile->setReceiveDynamicShadowsDepth(true); matProfile->setReceiveDynamicShadowsPSSM(static_cast<Ogre::PSSMShadowCameraSetup*>(mSceneMgr->getPSSMSetup().get())); } } CONNECT_PROPERTY_MEMFN(mSceneMgr, "shadows::enabled", CTerrainGroupEditor, onShadowsChange, mShadowsConnection[0]); CONNECT_PROPERTY_MEMFN(mSceneMgr, "shadows::technique", CTerrainGroupEditor, onShadowsTechniqueChange, mShadowsConnection[1]); mHandle = OGRE_NEW Ogre::TerrainGroup(mOgitorsRoot->GetSceneManager(), Ogre::Terrain::ALIGN_X_Z, mMapSize->get(), mWorldSize->get()); mHandle->setOrigin(Ogre::Vector3::ZERO); mHandle->setResourceGroup("TerrainResources"); mHandle->setFilenameConvention(mPageNamePrefix->get(), "ogt"); StaticGroupPtr = mHandle; mPGHandle = new PagedGeometry(mOgitorsRoot->GetViewport()->getCameraEditor()->getCamera(), mPGPageSize->get()); mPGHandle->addDetailLevel<GrassPage>(mPGDetailDistance->get()); //Create a GrassLoader object mGrassLoaderHandle = new GrassLoader(mPGHandle); mGrassLoaderHandle->setVisibilityFlags(1 << mLayer->get()); //Assign the "grassLoader" to be used to load geometry for the PagedGrass instance mPGHandle->setPageLoader(mGrassLoaderHandle); //Supply a height function to GrassLoader so it can calculate grass Y values mGrassLoaderHandle->setHeightFunction(OgitorTerrainGroupHeightFunction); Ogre::Vector3 vDir; Ogre::ColourValue cDiffuse; Ogre::SceneManager::MovableObjectIterator mit = mOgitorsRoot->GetSceneManager()->getMovableObjectIterator("Light"); while(mit.hasMoreElements()) { Ogre::Light *l = static_cast<Ogre::Light*>(mit.getNext()); if(l->getType() == Ogre::Light::LT_DIRECTIONAL && l->getCastShadows()) { vDir = l->getDerivedDirection(); cDiffuse = l->getDiffuseColour(); break; } } mTerrainGlobalOptions->setLightMapDirection(vDir); mTerrainGlobalOptions->setCompositeMapAmbient(mOgitorsRoot->GetSceneManager()->getAmbientLight()); mTerrainGlobalOptions->setCompositeMapDiffuse(cDiffuse); terrainDir = OgitorsRoot::getSingletonPtr()->GetProjectOptions()->TerrainDirectory + "/terrain/"; OFS::FileList TGAList = mOgitorsRoot->GetProjectFile()->listFiles(terrainDir.c_str(), OFS::OFS_FILE); for(unsigned int t = 0; t < TGAList.size(); t++) { int pos = TGAList[t].name.find("_density.tga"); if(pos > 0) { Ogre::Image _img; Ogre::String sLoc = terrainDir + TGAList[t].name; // Block to ensure streams are freed when exiting the block { OFS::OFSHANDLE *iHandle = new OFS::OFSHANDLE(); mOgitorsRoot->GetProjectFile()->openFile( *iHandle, sLoc.c_str() ); Ogre::DataStreamPtr img_stream = Ogre::DataStreamPtr(OGRE_NEW OfsDataStream(mOgitorsRoot->GetProjectFile(), iHandle)); _img.load(img_stream); } Ogre::String nLoc = terrainDir + TGAList[t].name.substr(0, pos); nLoc += "_density.png"; OgitorsUtils::SaveImageOfs( _img, nLoc ); mOgitorsRoot->GetProjectFile()->deleteFile( sLoc.c_str() ); } } registerForUpdates(); mLoaded->set(true); return true; }
void OgreRecast::update() { // Fully rebuild static geometry after a reset (when tiles should be removed) if(m_rebuildSg) { m_sg->reset(); // Add navmesh tiles (polys) to static geometry Ogre::SceneManager::MovableObjectIterator iterator = m_pSceneMgr->getMovableObjectIterator("Entity"); while(iterator.hasMoreElements()) { Ogre::Entity* ent = static_cast<Ogre::Entity*>(iterator.getNext()); // Add all navmesh poly debug entities if(Ogre::StringUtil::startsWith(ent->getName(), "ent_recastmowalk_")) m_sg->addEntity(ent, Ogre::Vector3::ZERO); } m_sg->build(); // Batch all lines together in one single manualObject (since we cannot use staticGeometry for lines) if(m_pSceneMgr->hasManualObject("AllNeighbourLines")) { m_pRecastSN->detachObject("AllNeighbourLines") ; m_pSceneMgr->destroyManualObject("AllNeighbourLines"); } if(m_pSceneMgr->hasManualObject("AllBoundaryLines")) { m_pRecastSN->detachObject("AllBoundaryLines") ; m_pSceneMgr->destroyManualObject("AllBoundaryLines"); } Ogre::ManualObject *allNeighbourLines = m_pSceneMgr->createManualObject("AllNeighbourLines"); allNeighbourLines->begin("recastdebug", Ogre::RenderOperation::OT_LINE_LIST); allNeighbourLines->colour(m_navmeshNeighbourEdgeCol); Ogre::ManualObject *allBoundaryLines = m_pSceneMgr->createManualObject("AllBoundaryLines"); allBoundaryLines->begin("recastdebug", Ogre::RenderOperation::OT_LINE_LIST); allBoundaryLines->colour(m_navmeshOuterEdgeCol); iterator = m_pSceneMgr->getMovableObjectIterator("ManualObject"); while(iterator.hasMoreElements()) { Ogre::ManualObject* man = static_cast<Ogre::ManualObject*>(iterator.getNext()); if(Ogre::StringUtil::startsWith(man->getName(), "recastmoneighbour_")) { std::vector<Ogre::Vector3> verts = getManualObjectVertices(man); for(std::vector<Ogre::Vector3>::iterator iter = verts.begin(); iter != verts.end(); iter++) { allNeighbourLines->position(*iter); } } else if(Ogre::StringUtil::startsWith(man->getName(), "recastmoboundary_")) { std::vector<Ogre::Vector3> verts = getManualObjectVertices(man); for(std::vector<Ogre::Vector3>::iterator iter = verts.begin(); iter != verts.end(); iter++) { allBoundaryLines->position(*iter); } } } allNeighbourLines->end(); allBoundaryLines->end(); m_pRecastSN->attachObject(allNeighbourLines); m_pRecastSN->attachObject(allBoundaryLines); m_rebuildSg = false; } }
void saveAsDotScene(const QString& path, QFile& file, Ogre::SceneManager* sceneManager) { Ogre::MeshSerializer* mMeshSerializer = new Ogre::MeshSerializer(); Ogre::MaterialSerializer* mMaterialSerializer = new Ogre::MaterialSerializer(); int idCounter = 3; if (!file.open(QIODevice::WriteOnly)) { /* show wrror message if not able to open file */ QMessageBox::warning(0, "Read only", "The file is in read only mode"); } else { Ogre::SceneManager::MovableObjectIterator iterator = sceneManager->getMovableObjectIterator("Entity"); QXmlStreamWriter* xmlWriter = new QXmlStreamWriter(); xmlWriter->setAutoFormatting(true); xmlWriter->setDevice(&file); xmlWriter->writeStartElement("scene"); xmlWriter->writeAttribute("formatVersion",""); xmlWriter->writeStartElement("nodes"); while(iterator.hasMoreElements()) { Ogre::Entity* e = static_cast<Ogre::Entity*>(iterator.getNext()); Ogre::Any any = e->getParentNode()->getUserAny(); Ogre::String widgetType(""); if(!any.isEmpty()){ widgetType = any_cast<Ogre::String>(any); } Ogre::String tmp(widgetType + ":" + e->getParentNode()->getName()); QString nodeName(tmp.c_str()); xmlWriter->writeStartElement("node"); xmlWriter->writeAttribute("name", nodeName); xmlWriter->writeAttribute("id", QString::number(idCounter++)); xmlWriter->writeStartElement("position"); xmlWriter->writeAttribute("x", QString::number(e->getParentNode()->getPosition().x)); xmlWriter->writeAttribute("y", QString::number(e->getParentNode()->getPosition().y)); xmlWriter->writeAttribute("z", QString::number(e->getParentNode()->getPosition().z)); xmlWriter->writeEndElement(); xmlWriter->writeStartElement("scale"); xmlWriter->writeAttribute("x", QString::number(e->getParentNode()->getScale().x)); xmlWriter->writeAttribute("y", QString::number(e->getParentNode()->getScale().y)); xmlWriter->writeAttribute("z", QString::number(e->getParentNode()->getScale().z)); xmlWriter->writeEndElement(); xmlWriter->writeStartElement("entity"); xmlWriter->writeAttribute("name", nodeName); xmlWriter->writeAttribute("meshFile", nodeName.toLower() + QString(".mesh") ); xmlWriter->writeAttribute("static", QString("false")); xmlWriter->writeEndElement(); const Mesh* mesh = e->getMesh().getPointer(); mMeshSerializer->exportMesh(mesh,String(path.toStdString() + nodeName.toLower().toStdString() + ".mesh" )); std::cout << "numeber" << mesh->getNumSubMeshes() << std::endl; for(int i = 0; i < e->getNumSubEntities(); i++){ Ogre::Material *mat = static_cast<Ogre::Material*> (Ogre::MaterialManager::getSingletonPtr()->getByName(e->getSubEntity(i)->getMaterialName()).getPointer()); //e->getMesh().get()->getSubMesh() if(mat->getTechnique(0)->getPass(0)->getNumTextureUnitStates() !=0){ Ogre::String str = mat->getTechnique(0)->getPass(0)->getTextureUnitState(0)->getTextureName(); Ogre::MaterialPtr mMatPtr =e->getSubEntity(i)->getMaterial() ; mMaterialSerializer->exportMaterial(mMatPtr , String(path.toStdString() + nodeName.toLower().toStdString() + QString::number(i).toStdString() + ".material" )); Ogre::TexturePtr* mTexPtr = new Ogre::TexturePtr(Ogre::TextureManager::getSingletonPtr()->getByName(str)); Ogre::Texture* mTex = mTexPtr->getPointer(); Ogre::PixelFormat pxf = mTex->getFormat(); Ogre::Image mImage; mTex->convertToImage(mImage); std::cout << str << std::endl; mImage.save(String(path.toStdString() + str)); } } //material file merge for(int i = 0; i < e->getNumSubEntities(); i++){ Ogre::Material *mat = static_cast<Ogre::Material*> (Ogre::MaterialManager::getSingletonPtr()->getByName(e->getSubEntity(i)->getMaterialName()).getPointer()); QString mMatFilePath = QString((path.toStdString() + nodeName.toLower().toStdString() + ".material").c_str()) ; QFile mFile(mMatFilePath); if (!mFile.open(QIODevice::Append)) { /* show wrror message if not able to open file */ QMessageBox::warning(0, "Read only", "The file is in read only mode"); } else{ QTextStream out(&mFile); QString mTempMatPath = QString((path + nodeName.toLower() + QString::number(i) + ".material")); QFile mTempMatFile(mTempMatPath); mTempMatFile.open(QIODevice::ReadOnly); QTextStream src(&mTempMatFile); mFile.write(src.readAll().toStdString().c_str()); mTempMatFile.remove(); } } xmlWriter->writeEndElement(); } xmlWriter->writeEndElement(); xmlWriter->writeEndDocument(); delete xmlWriter; } delete mMeshSerializer; delete mMaterialSerializer; }
void OgreInfo::diagnose(std::ostream& outputStream) { Ogre::SceneManagerEnumerator::SceneManagerIterator sceneManagerI = Ogre::Root::getSingleton().getSceneManagerIterator(); while (sceneManagerI.hasMoreElements()) { Ogre::SceneManager* sceneManager = sceneManagerI.getNext(); outputStream << "Scenemanager(" << sceneManager->getTypeName() << ") " << sceneManager->getName() << std::endl; outputStream << " Number of scene nodes: " << countNodes(sceneManager->getRootSceneNode()) << std::endl; outputStream << " Movable objects:" << std::endl; unsigned int movableObjectCounter = 0; Ogre::Root::MovableObjectFactoryIterator movableObjectFactoryI = Ogre::Root::getSingleton().getMovableObjectFactoryIterator(); while (movableObjectFactoryI.hasMoreElements()) { Ogre::MovableObjectFactory* factory = movableObjectFactoryI.getNext(); std::string type(factory->getType()); { Ogre::SceneManager::MovableObjectIterator I = sceneManager->getMovableObjectIterator(type); while (I.hasMoreElements()) { movableObjectCounter++; Ogre::MovableObject* movable = I.getNext(); if (movable->getMovableType() == "Light") { Ogre::Light* light = static_cast<Ogre::Light*> (movable); outputStream << " * Light " << light->getName() << "(" << (light->isInScene() ? "in scene" : "not in scene") << ")" << std::endl; outputStream << " Pos: " << light->getDerivedPosition() << std::endl; outputStream << " Direction: " << light->getDerivedDirection() << std::endl; } else { std::stringstream ssPosAndOrientation; if (movable->getParentSceneNode() && movable->isInScene()) { ssPosAndOrientation << " pos: " << movable->getParentSceneNode()->getPosition() << " orientation: " << movable->getParentSceneNode()->getOrientation(); } outputStream << " * " << type << " " << movable->getName() << "(" << (movable->isInScene() ? "in scene" : "not in scene") << ")" << ssPosAndOrientation.str() << std::endl; // outputStream << " Pos: " << light->getDerivedPosition() << std::endl; // outputStream << " Direction: " << light->getDerivedDirection() << std::endl; } } } } outputStream << " Number of movable objects: " << movableObjectCounter << std::endl; outputStream << " Cameras:" << std::endl; { Ogre::SceneManager::CameraIterator I = sceneManager->getCameraIterator(); while (I.hasMoreElements()) { Ogre::Camera* camera = I.getNext(); outputStream << " Camera " << camera->getName() << "(" << (camera->isInScene() ? "in scene" : "not in scene") << ")" << std::endl; outputStream << " Pos: " << camera->getDerivedPosition() << std::endl; outputStream << " Direction: " << camera->getDerivedDirection() << std::endl; outputStream << " Clip distances: " << camera->getNearClipDistance() << " - " << camera->getFarClipDistance() << std::endl; } } } size_t resourceMemoryUsage = 0; outputStream << "Resource Managers:" << std::endl; Ogre::ResourceGroupManager::ResourceManagerIterator I = Ogre::ResourceGroupManager::getSingleton().getResourceManagerIterator(); while (I.hasMoreElements()) { std::string name = I.peekNextKey(); Ogre::ResourceManager* manager = I.getNext(); outputStream << " Resource Manager: " << name << std::endl; if (manager->getMemoryBudget() == std::numeric_limits<size_t>::max()) { outputStream << " Memory budget: not set" << std::endl; } else { outputStream << " Memory budget: " << manager->getMemoryBudget() << " bytes" << std::endl; } outputStream << " Memory usage: " << manager->getMemoryUsage() << " bytes" << std::endl; resourceMemoryUsage += manager->getMemoryUsage(); Ogre::ResourceManager::ResourceMapIterator resourceI = manager->getResourceIterator(); if (resourceI.hasMoreElements()) { outputStream << " Resources: " << std::endl; int resourceCount = 0; int loadedResourceCount = 0; while (resourceI.hasMoreElements()) { Ogre::ResourcePtr resource = resourceI.getNext(); if (resource->isLoaded()) { std::string reloadable = resource->isReloadable() ? " reloadable" : ""; outputStream << " " << resource->getName() << " ( " << resource->getSize() << " bytes)" << reloadable; Ogre::Texture* texture = dynamic_cast<Ogre::Texture*>(resource.get()); if (texture) { outputStream << texture->getWidth() << "x" << texture->getHeight() << " "; } outputStream << std::endl; loadedResourceCount++; } resourceCount++; } outputStream << " Total number of resources: " << resourceCount << std::endl; outputStream << " Number of loaded resources: " << loadedResourceCount << std::endl; } } outputStream << "Total memory usage for all resource manager: " << resourceMemoryUsage << " bytes" << std::endl; outputStream << std::flush; }