Rigs of Rods 2023.09
Soft-body Physics Simulation
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Loading...
Searching...
No Matches
DecalsManager.cpp
Go to the documentation of this file.
1/*
2--------------------------------------------------------------------------------
3This source file is part of Hydrax.
4Visit ---
5
6Copyright (C) 2008 Xavier Vergu�n Gonz�lez <xavierverguin@hotmail.com>
7 <xavyiy@gmail.com>
8
9This program is free software; you can redistribute it and/or modify it under
10the terms of the GNU Lesser General Public License as published by the Free Software
11Foundation; either version 2 of the License, or (at your option) any later
12version.
13
14This program is distributed in the hope that it will be useful, but WITHOUT
15ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
17
18You should have received a copy of the GNU Lesser General Public License along with
19this program; if not, write to the Free Software Foundation, Inc., 59 Temple
20Place - Suite 330, Boston, MA 02111-1307, USA, or go to
21http://www.gnu.org/copyleft/lesser.txt.
22--------------------------------------------------------------------------------
23*/
24
25#include <DecalsManager.h>
26
27#include <Hydrax.h>
28
29namespace 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
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 {
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}
Decal class.
Ogre::Frustum * mProjector
Decal projector.
Ogre::Real mTransparency
Transparency.
bool mVisible
Is decal visible?
Ogre::Vector2 mPosition
Position.
void setPosition(const Ogre::Vector2 &Position)
Set decal position.
void setVisible(const bool &Visible)
Set decal visibile or not.
void setOrientation(const Ogre::Radian &Orientation)
Set decal orientation.
Ogre::Pass * mRegisteredPass
Registered pass.
Decal(Hydrax *h, const Ogre::String &TextureName, const int &Id)
Constructor.
Ogre::Radian mOrientation
Orientation.
void registerPass(Ogre::Pass *_Pass)
Register the decal int the specified pass.
Hydrax * mHydrax
Hydrax parent pointer.
Ogre::String mTextureName
Decal texture name.
void unregister()
Unregister from current technique.
~Decal()
Destructor.
Ogre::SceneNode * mSceneNode
Decal scene node.
void setTransparency(const Ogre::Real &Transparency)
Set decal transparency.
Ogre::Vector2 mSize
Size.
void setSize(const Ogre::Vector2 &Size)
Set decal size.
void remove(const int &Id)
Remove decal.
Ogre::Real mWaterStrength
Water strength (For decals culling)
Ogre::Quaternion mLastOrientation
void update()
Update decal manager.
DecalsManager(Hydrax *h)
Constructor.
Ogre::Vector3 mLastPosition
Last camera position, orientation, underwater.
Decal * get(const int &Id)
Get decal.
Decal * add(const Ogre::String &TextureName)
Add decal.
Hydrax * mHydrax
Hydrax parent pointer.
void removeAll()
Remove all decals.
void registerAll()
Register all decals.
void _forceToUpdate()
Call to force to update decals.
std::vector< Decal * > mDecals
Decals std::vector.
std::vector< Decal * >::iterator DecalIt
Decal iterator.
Ogre::Camera * getCamera()
Get rendering camera.
Definition Hydrax.h:293
const Ogre::Vector3 & getPosition() const
Get water position.
Definition Hydrax.h:413
MaterialManager * getMaterialManager()
Get Hydrax::MaterialManager.
Definition Hydrax.h:325
Ogre::SceneManager * getSceneManager()
Get scene manager.
Definition Hydrax.h:309
DecalsManager * getDecalsManager()
Get Hydrax::DecalsManager.
Definition Hydrax.h:357
const bool & _isCurrentFrameUnderwater() const
Is current frame underwater?
Definition Hydrax.h:621
Ogre::MaterialPtr & getMaterial(const MaterialType &Material)
Get material.
const bool & isCreated() const
Is createMaterials() already called?
Struct wich contains an especific width and height value.
Definition Help.h:41