RigsofRods
Soft-body Physics Simulation
SurveyMapTextureCreator.cpp
Go to the documentation of this file.
1 /*
2  This source file is part of Rigs of Rods
3  Copyright 2005-2012 Pierre-Michel Ricordel
4  Copyright 2007-2012 Thomas Fischer
5 
6  For more information, see http://www.rigsofrods.org/
7 
8  Rigs of Rods is free software: you can redistribute it and/or modify
9  it under the terms of the GNU General Public License version 3, as
10  published by the Free Software Foundation.
11 
12  Rigs of Rods is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with Rigs of Rods. If not, see <http://www.gnu.org/licenses/>.
19 */
20 
22 
23 #include "Actor.h"
24 #include "Application.h"
25 #include "GameContext.h"
26 #include "GfxScene.h"
27 #include "IWater.h"
28 #include "Terrain.h"
29 
30 using namespace Ogre;
31 using namespace RoR;
32 
33 static int counter = 0;
34 
35 SurveyMapTextureCreator::SurveyMapTextureCreator(Ogre::Real terrain_height) :
36  mCamera(nullptr),
37  mRttTex(nullptr),
38  mTerrainHeight(Math::Clamp(terrain_height + 100.0f, 150.0f, 2500.0f))
39 {
40  counter++;
41  mTextureName = "MapRttTex-" + TOSTRING(counter);
42 }
43 
45 {
46  if (mRttTex)
47  mRttTex->removeAllViewports();
48  if (mCamera)
49  App::GetGfxScene()->GetSceneManager()->destroyCamera(mCamera);
50  if (mTexture)
51  Ogre::TextureManager::getSingleton().remove(mTexture);
52 }
53 
54 bool SurveyMapTextureCreator::init(int res, int fsaa)
55 {
56  mTexture = Ogre::TextureManager::getSingleton().createManual(mTextureName,
57  Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, Ogre::TEX_TYPE_2D, res, res,
58  Ogre::TU_RENDERTARGET, Ogre::PF_R8G8B8, Ogre::TU_RENDERTARGET, 0, false, fsaa);
59 
60  if (mTexture.isNull())
61  return false;
62 
63  mRttTex = mTexture->getBuffer()->getRenderTarget();
64 
65  if (!mRttTex)
66  return false;
67 
68  mRttTex->addListener(this);
69  mRttTex->setAutoUpdated(false);
70 
71  mCamera = App::GetGfxScene()->GetSceneManager()->createCamera("MapRttCam-" + TOSTRING(counter));
72  mCamera->setFixedYawAxis(false);
73  mCamera->setDirection(-Vector3::UNIT_Y);
74  mCamera->setProjectionType(PT_ORTHOGRAPHIC);
75  mCamera->setNearClipDistance(1.0f);
76  mCamera->setFarClipDistance(0);
77 
78  auto mViewport = mRttTex->addViewport(mCamera);
79  mViewport->setBackgroundColour(ColourValue::Black);
80  mViewport->setOverlaysEnabled(false);
81  mViewport->setShadowsEnabled(false);
82  mViewport->setSkiesEnabled(false);
83 
84  return true;
85 }
86 
87 void SurveyMapTextureCreator::update(Vector2 center, Vector2 size)
88 {
89  if (!mRttTex)
90  return;
91 
92  mCamera->setOrthoWindow(size.x, size.y);
93  mCamera->setPosition(Vector3(center.x, mTerrainHeight, center.y));
94 
95  mRttTex->update();
96 }
97 
98 void SurveyMapTextureCreator::preRenderTargetUpdate(const RenderTargetEvent &evt)
99 {
100  auto water = App::GetGameContext()->GetTerrain()->getWater();
101  if (water)
102  {
103  water->SetStaticWaterHeight(water->GetStaticWaterHeight());
104  water->SetForcedCameraTransform(mCamera->getFOVy(),
105  mCamera->getPosition(), mCamera->getOrientation()); // FIXME: Legacy OGRE API! Use camera node instead!
106  water->UpdateWater();
107  water->ClearForcedCameraTransform();
108  }
109 }
110 
111 void SurveyMapTextureCreator::postRenderTargetUpdate(const RenderTargetEvent &evt)
112 {
113  auto water = App::GetGameContext()->GetTerrain()->getWater();
114  if (water)
115  {
116  water->UpdateWater();
117  }
118 }
119 
120 Ogre::TexturePtr SurveyMapTextureCreator::convertTextureToStatic(const std::string& texName, const std::string& rgName)
121 {
122  Ogre::Image img;
123  mTexture->convertToImage(img);
124  return Ogre::TextureManager::getSingleton().loadImage(texName, rgName, img);
125 }
GameContext.h
Game state manager and message-queue provider.
RoR::SurveyMapTextureCreator::init
bool init(int res, int fsaa)
Definition: SurveyMapTextureCreator.cpp:54
RoR::SurveyMapTextureCreator::mCamera
Ogre::Camera * mCamera
Definition: SurveyMapTextureCreator.h:47
RoR::SurveyMapTextureCreator::preRenderTargetUpdate
void preRenderTargetUpdate(const Ogre::RenderTargetEvent &evt)
Definition: SurveyMapTextureCreator.cpp:98
Actor.h
RoR::GfxScene::GetSceneManager
Ogre::SceneManager * GetSceneManager()
Definition: GfxScene.h:64
RoR::SurveyMapTextureCreator::mTextureName
Ogre::String mTextureName
Definition: SurveyMapTextureCreator.h:45
RoR::IWater::UpdateWater
virtual void UpdateWater()=0
TOSTRING
#define TOSTRING(x)
Definition: Application.h:56
RoR::SurveyMapTextureCreator::update
void update(Ogre::Vector2 center, Ogre::Vector2 size)
Definition: SurveyMapTextureCreator.cpp:87
RoR::SurveyMapTextureCreator::mTexture
Ogre::TexturePtr mTexture
Definition: SurveyMapTextureCreator.h:48
GfxScene.h
Application.h
Central state/object manager and communications hub.
RoR::SurveyMapTextureCreator::mRttTex
Ogre::RenderTarget * mRttTex
Definition: SurveyMapTextureCreator.h:49
RoR::App::GetGameContext
GameContext * GetGameContext()
Definition: Application.cpp:280
RoR::SurveyMapTextureCreator::postRenderTargetUpdate
void postRenderTargetUpdate(const Ogre::RenderTargetEvent &evt)
Definition: SurveyMapTextureCreator.cpp:111
SurveyMapTextureCreator.h
RoR::SurveyMapTextureCreator::convertTextureToStatic
Ogre::TexturePtr convertTextureToStatic(const std::string &texName, const std::string &rgName)
Definition: SurveyMapTextureCreator.cpp:120
IWater.h
RoR::SurveyMapTextureCreator::mTerrainHeight
Ogre::Real mTerrainHeight
Definition: SurveyMapTextureCreator.h:44
Terrain.h
Ogre
Definition: ExtinguishableFireAffector.cpp:35
RoR::IWater::SetStaticWaterHeight
virtual void SetStaticWaterHeight(float value)=0
counter
static int counter
Definition: SurveyMapTextureCreator.cpp:33
RoR
Definition: AppContext.h:36
RoR::App::GetGfxScene
GfxScene * GetGfxScene()
Definition: Application.cpp:276
RoR::Terrain::getWater
IWater * getWater()
Definition: Terrain.h:84
RoR::SurveyMapTextureCreator::~SurveyMapTextureCreator
~SurveyMapTextureCreator()
Definition: SurveyMapTextureCreator.cpp:44
RoR::GameContext::GetTerrain
const TerrainPtr & GetTerrain()
Definition: GameContext.h:117