RigsofRods
Soft-body Physics Simulation
DecalsManager.cpp
Go to the documentation of this file.
1 /*
2 --------------------------------------------------------------------------------
3 This source file is part of Hydrax.
4 Visit ---
5 
6 Copyright (C) 2008 Xavier Verguín González <xavierverguin@hotmail.com>
7  <xavyiy@gmail.com>
8 
9 This program is free software; you can redistribute it and/or modify it under
10 the terms of the GNU Lesser General Public License as published by the Free Software
11 Foundation; either version 2 of the License, or (at your option) any later
12 version.
13 
14 This program is distributed in the hope that it will be useful, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
17 
18 You should have received a copy of the GNU Lesser General Public License along with
19 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
20 Place - Suite 330, Boston, MA 02111-1307, USA, or go to
21 http://www.gnu.org/copyleft/lesser.txt.
22 --------------------------------------------------------------------------------
23 */
24 
25 #include <DecalsManager.h>
26 
27 #include <Hydrax.h>
28 
29 namespace Hydrax
30 {
31  Decal::Decal(Hydrax *h, const Ogre::String &TextureName, const int& Id)
32  : mHydrax(h)
33  , mTextureName(TextureName)
34  , mId(Id)
35  , mRegisteredPass(0)
36  , mPosition(Ogre::Vector2(0,0))
37  , mSize(Ogre::Vector2(1,1))
38  , mOrientation(Ogre::Radian(0))
39  , mTransparency(1)
40  , mVisible(true)
41  {
42  mProjector = new Ogre::Frustum();
43  mProjector->setProjectionType(Ogre::PT_ORTHOGRAPHIC);
44 
45  mSceneNode = mHydrax->getSceneManager()->getRootSceneNode()->createChildSceneNode();
46  mSceneNode->attachObject(mProjector);
47  mSceneNode->setPosition(Ogre::Vector3(0,0,0));
48  mSceneNode->setOrientation(Ogre::Quaternion(Ogre::Degree(90), Ogre::Vector3::NEGATIVE_UNIT_X));
49 
51  setSize(mSize);
53  }
54 
56  {
57  unregister();
58 
59  mSceneNode->getParentSceneNode()->removeAndDestroyChild(mSceneNode);
60 
61  delete mProjector;
62  }
63 
64  void Decal::registerPass(Ogre::Pass* _Pass)
65  {
66  unregister();
67 
68  _Pass->setSceneBlending(Ogre::SBT_TRANSPARENT_ALPHA);
69  _Pass->setCullingMode(Ogre::CULL_NONE);
70  _Pass->setDepthBias(1,1);
71  _Pass->setLightingEnabled(false);
72  _Pass->setDepthWriteEnabled(false);
73 
74  Ogre::TextureUnitState *DecalTexture = _Pass->createTextureUnitState(mTextureName);
75  DecalTexture->setProjectiveTexturing(true, mProjector);
76  DecalTexture->setTextureAddressingMode(Ogre::TextureUnitState::TAM_CLAMP);
77  DecalTexture->setTextureFiltering(Ogre::FO_LINEAR, Ogre::FO_LINEAR, Ogre::FO_NONE);
78  DecalTexture->setAlphaOperation(Ogre::LBX_MODULATE, Ogre::LBS_TEXTURE, Ogre::LBS_MANUAL, 1.0, mTransparency);
79 
80  mRegisteredPass = _Pass;
81  }
82 
84  {
85  if (mRegisteredPass)
86  {
87  mRegisteredPass->getParent()->removePass(mRegisteredPass->getIndex());
88  mRegisteredPass = static_cast<Ogre::Pass*>(NULL);
89  }
90  }
91 
92  void Decal::setPosition(const Ogre::Vector2& Position)
93  {
94  mPosition = Position;
95 
96  mSceneNode->setPosition(Position.x, 0, Position.y);
97  }
98 
99  void Decal::setSize(const Ogre::Vector2& Size)
100  {
101  mSize = Size;
102 
103  // This method is only available in the CVS HEAD,
104  // if you have problems compiling, just comment the
105  // following line:
106  mProjector->setOrthoWindow(Size.x, Size.y);
107  }
108 
109  void Decal::setOrientation(const Ogre::Radian& Orientation)
110  {
111  mSceneNode->rotate(Ogre::Vector3::UNIT_Z, -mOrientation + Orientation);
112 
113  mOrientation = Orientation;
114  }
115 
116  void Decal::setTransparency(const Ogre::Real& Transparency)
117  {
118  mTransparency = Transparency;
119 
120  if (mRegisteredPass)
121  {
122  mRegisteredPass->getTextureUnitState(0)
123  ->setAlphaOperation(Ogre::LBX_MODULATE, Ogre::LBS_TEXTURE, Ogre::LBS_MANUAL, 1.0, mTransparency);
124  }
125  }
126 
127  void Decal::setVisible(const bool& Visible)
128  {
129  mVisible = Visible;
130 
131  unregister();
132 
134  }
135 
136  // --------------------------------------------------------------------
137 
139  : mHydrax(h)
140  , mNextId(0)
141  , mLastUnderwater(false)
142  , mWaterStrength(5)
143  , mForceToUpdate(false)
144  {
145  }
146 
148  {
149  for(DecalIt = mDecals.begin(); DecalIt != mDecals.end(); DecalIt++)
150  {
151  delete (*DecalIt);
152  }
153 
154  mDecals.clear();
155  }
156 
158  {
159  if (mHydrax->getCamera()->getDerivedPosition() == mLastPosition &&
160  mHydrax->getCamera()->getDerivedOrientation() == mLastOrientation &&
162  {
163  return;
164  }
165 
166  if (mForceToUpdate)
167  {
168  mForceToUpdate = false;
169  }
170 
171  Ogre::Vector2 DPos;
172  Ogre::Real HHeight = mHydrax->getPosition().y;
173  Ogre::Vector2 DSize;
174  Ogre::AxisAlignedBox DecalBox;
175 
177  {
178  for(DecalIt = mDecals.begin(); DecalIt != mDecals.end(); DecalIt++)
179  {
180  if (!(*DecalIt)->isVisible())
181  {
182  continue;
183  }
184 
185  if ((*DecalIt)->getRegisteredPass())
186  {
187  (*DecalIt)->unregister();
188 
190  {
191  (*DecalIt)->registerPass(
193  getTechnique(0)->createPass());
194  }
195  else
196  {
197  (*DecalIt)->registerPass(
199  getTechnique(0)->createPass());
200  }
201  }
202 
203  }
204  }
205 
206  for(DecalIt = mDecals.begin(); DecalIt != mDecals.end(); DecalIt++)
207  {
208  if (!(*DecalIt)->isVisible())
209  {
210  continue;
211  }
212 
213  DPos = (*DecalIt)->getPosition();
214  DSize = (*DecalIt)->getSize()/2;
215 
216  DecalBox = Ogre::AxisAlignedBox(DPos.x - DSize.x, HHeight - mWaterStrength, DPos.y - DSize.y,
217  DPos.x + DSize.x, HHeight + mWaterStrength, DPos.y + DSize.y);
218 
219  if (mHydrax->getCamera()->isVisible(DecalBox))
220  {
221  if (!(*DecalIt)->getRegisteredPass())
222  {
224  {
225  (*DecalIt)->registerPass(
227  getTechnique(0)->createPass());
228  }
229  else
230  {
231  (*DecalIt)->registerPass(
233  getTechnique(0)->createPass());
234  }
235  }
236  }
237  else
238  {
239  (*DecalIt)->unregister();
240  }
241  }
242 
243  mLastPosition = mHydrax->getCamera()->getDerivedPosition();
244  mLastOrientation = mHydrax->getCamera()->getDerivedOrientation();
246  }
247 
248  Decal* DecalsManager::add(const Ogre::String& TextureName)
249  {
250  Decal* NewDecal = new Decal(mHydrax, TextureName, mNextId);
251 
252  mDecals.push_back(NewDecal);
253 
255  {
256  NewDecal->registerPass(
258  getTechnique(0)->createPass());
259  }
260 
261  mNextId++;
262 
263  return NewDecal;
264  }
265 
266  Decal* DecalsManager::get(const int& Id)
267  {
268  for(DecalIt = mDecals.begin(); DecalIt != mDecals.end(); DecalIt++)
269  {
270  if((*DecalIt)->getId() == Id)
271  {
272  return (*DecalIt);
273  }
274  }
275 
276  return static_cast<Decal*>(NULL);
277  }
278 
279  void DecalsManager::remove(const int& Id)
280  {
281  for(DecalIt = mDecals.begin(); DecalIt != mDecals.end(); DecalIt++)
282  {
283  if((*DecalIt)->getId() == Id)
284  {
285  delete (*DecalIt);
286  mDecals.erase(DecalIt);
287 
288  return;
289  }
290  }
291  }
292 
294  {
295  for(DecalIt = mDecals.begin(); DecalIt != mDecals.end(); DecalIt++)
296  {
297  delete (*DecalIt);
298  }
299 
300  mDecals.clear();
301 
302  mNextId = 0;
303  }
304 
306  {
307  for(DecalIt = mDecals.begin(); DecalIt != mDecals.end(); DecalIt++)
308  {
309  (*DecalIt)->unregister();
310 
312  {
313  (*DecalIt)->registerPass(
315  getTechnique(0)->createPass());
316  }
317  else
318  {
319  (*DecalIt)->registerPass(
321  getTechnique(0)->createPass());
322  }
323  }
324  }
325 }
Hydrax::MaterialManager::MAT_UNDERWATER
@ MAT_UNDERWATER
Definition: MaterialManager.h:57
Hydrax::DecalsManager::update
void update()
Update decal manager.
Definition: DecalsManager.cpp:157
Hydrax::DecalsManager::DecalsManager
DecalsManager(Hydrax *h)
Constructor.
Definition: DecalsManager.cpp:138
Hydrax::DecalsManager::mLastPosition
Ogre::Vector3 mLastPosition
Last camera position, orientation, underwater.
Definition: DecalsManager.h:288
Hydrax::Decal::mTransparency
Ogre::Real mTransparency
Transparency.
Definition: DecalsManager.h:191
Hydrax::Hydrax::getPosition
const Ogre::Vector3 & getPosition() const
Get water position.
Definition: Hydrax.h:413
Hydrax::MaterialManager::getMaterial
Ogre::MaterialPtr & getMaterial(const MaterialType &Material)
Get material.
Definition: MaterialManager.h:221
DecalsManager.h
Hydrax::DecalsManager::mHydrax
Hydrax * mHydrax
Hydrax parent pointer.
Definition: DecalsManager.h:294
Hydrax
Definition: CfgFileManager.cpp:28
Hydrax::Hydrax::getMaterialManager
MaterialManager * getMaterialManager()
Get Hydrax::MaterialManager.
Definition: Hydrax.h:325
Hydrax::Decal::mTextureName
Ogre::String mTextureName
Decal texture name.
Definition: DecalsManager.h:174
Hydrax::Size
Struct wich contains an especific width and height value.
Definition: Help.h:40
Hydrax::DecalsManager::mLastOrientation
Ogre::Quaternion mLastOrientation
Definition: DecalsManager.h:289
Hydrax.h
Hydrax::Decal::mSize
Ogre::Vector2 mSize
Size.
Definition: DecalsManager.h:187
Hydrax::DecalsManager::get
Decal * get(const int &Id)
Get decal.
Definition: DecalsManager.cpp:266
Hydrax::DecalsManager::_forceToUpdate
void _forceToUpdate()
Call to force to update decals.
Definition: DecalsManager.h:271
Hydrax::Decal::mPosition
Ogre::Vector2 mPosition
Position.
Definition: DecalsManager.h:185
Hydrax::Decal::setTransparency
void setTransparency(const Ogre::Real &Transparency)
Set decal transparency.
Definition: DecalsManager.cpp:116
Hydrax::DecalsManager::~DecalsManager
~DecalsManager()
Destructor.
Definition: DecalsManager.cpp:147
Hydrax::Decal
Decal class.
Definition: DecalsManager.h:42
Hydrax::Decal::setOrientation
void setOrientation(const Ogre::Radian &Orientation)
Set decal orientation.
Definition: DecalsManager.cpp:109
Hydrax::Decal::~Decal
~Decal()
Destructor.
Definition: DecalsManager.cpp:55
Hydrax::DecalsManager::removeAll
void removeAll()
Remove all decals.
Definition: DecalsManager.cpp:293
Hydrax::Decal::setVisible
void setVisible(const bool &Visible)
Set decal visibile or not.
Definition: DecalsManager.cpp:127
Hydrax::DecalsManager::mForceToUpdate
bool mForceToUpdate
Definition: DecalsManager.h:291
Hydrax::Hydrax::getSceneManager
Ogre::SceneManager * getSceneManager()
Get scene manager.
Definition: Hydrax.h:309
Hydrax::Hydrax::getCamera
Ogre::Camera * getCamera()
Get rendering camera.
Definition: Hydrax.h:293
Hydrax::Decal::mProjector
Ogre::Frustum * mProjector
Decal projector.
Definition: DecalsManager.h:178
Hydrax::Decal::setSize
void setSize(const Ogre::Vector2 &Size)
Set decal size.
Definition: DecalsManager.cpp:99
Hydrax::Decal::unregister
void unregister()
Unregister from current technique.
Definition: DecalsManager.cpp:83
Hydrax::MaterialManager::isCreated
const bool & isCreated() const
Is createMaterials() already called?
Definition: MaterialManager.h:212
Hydrax::Decal::mVisible
bool mVisible
Is decal visible?
Definition: DecalsManager.h:193
Hydrax::DecalsManager::DecalIt
std::vector< Decal * >::iterator DecalIt
Decal iterator.
Definition: DecalsManager.h:280
Hydrax::DecalsManager::mLastUnderwater
bool mLastUnderwater
Definition: DecalsManager.h:290
Hydrax::DecalsManager::mWaterStrength
Ogre::Real mWaterStrength
Water strength (For decals culling)
Definition: DecalsManager.h:285
Hydrax::Decal::mHydrax
Hydrax * mHydrax
Hydrax parent pointer.
Definition: DecalsManager.h:196
Hydrax::Decal::registerPass
void registerPass(Ogre::Pass *_Pass)
Register the decal int the specified pass.
Definition: DecalsManager.cpp:64
Hydrax::MaterialManager::MAT_WATER
@ MAT_WATER
Definition: MaterialManager.h:53
Ogre
Definition: ExtinguishableFireAffector.cpp:35
Hydrax::DecalsManager::remove
void remove(const int &Id)
Remove decal.
Definition: DecalsManager.cpp:279
Hydrax::DecalsManager::add
Decal * add(const Ogre::String &TextureName)
Add decal.
Definition: DecalsManager.cpp:248
Hydrax::DecalsManager::registerAll
void registerAll()
Register all decals.
Definition: DecalsManager.cpp:305
Hydrax::Hydrax::_isCurrentFrameUnderwater
const bool & _isCurrentFrameUnderwater() const
Is current frame underwater?
Definition: Hydrax.h:621
Hydrax::Decal::Decal
Decal(Hydrax *h, const Ogre::String &TextureName, const int &Id)
Constructor.
Definition: DecalsManager.cpp:31
Hydrax::Decal::mOrientation
Ogre::Radian mOrientation
Orientation.
Definition: DecalsManager.h:189
Hydrax::DecalsManager::mNextId
int mNextId
Next Id.
Definition: DecalsManager.h:282
Hydrax::Decal::mSceneNode
Ogre::SceneNode * mSceneNode
Decal scene node.
Definition: DecalsManager.h:180
Hydrax::Decal::setPosition
void setPosition(const Ogre::Vector2 &Position)
Set decal position.
Definition: DecalsManager.cpp:92
Hydrax::Hydrax::getDecalsManager
DecalsManager * getDecalsManager()
Get Hydrax::DecalsManager.
Definition: Hydrax.h:357
Hydrax::Decal::mRegisteredPass
Ogre::Pass * mRegisteredPass
Registered pass.
Definition: DecalsManager.h:182
Hydrax::DecalsManager::mDecals
std::vector< Decal * > mDecals
Decals std::vector.
Definition: DecalsManager.h:278