RigsofRods
Soft-body Physics Simulation
MoonManager.cpp
Go to the documentation of this file.
1 /*
2 --------------------------------------------------------------------------------
3 This source file is part of SkyX.
4 Visit http://www.paradise-studios.net/products/skyx/
5 
6 Copyright (C) 2009-2012 Xavier Verguín González <xavyiy@gmail.com>
7 
8 This program is free software; you can redistribute it and/or modify it under
9 the terms of the GNU Lesser General Public License as published by the Free Software
10 Foundation; either version 2 of the License, or (at your option) any later
11 version.
12 
13 This program is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
16 
17 You should have received a copy of the GNU Lesser General Public License along with
18 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19 Place - Suite 330, Boston, MA 02111-1307, USA, or go to
20 http://www.gnu.org/copyleft/lesser.txt.
21 --------------------------------------------------------------------------------
22 */
23 
24 #include "MoonManager.h"
25 
26 #include "SkyX.h"
27 
28 namespace SkyX
29 {
31  : mSkyX(s)
32  , mMoonBillboard(0)
33  , mMoonSceneNode(0)
34  , mCreated(false)
35  , mMoonSize(0.225f)
36  , mMoonHaloIntensity(0.4f)
37  , mMoonHaloStrength(0.9f)
38  , mMoonMaterial(Ogre::MaterialPtr())
39  {
40  }
41 
43  {
44  remove();
45  }
46 
48  {
49  if (mCreated)
50  {
51  return;
52  }
53 
54  mMoonMaterial = static_cast<Ogre::MaterialPtr>(Ogre::MaterialManager::getSingleton().getByName("SkyX_Moon"));
55 
56  if (mMoonMaterial.isNull())
57  {
58  SkyXLOG("Error while creating SkyX::MoonManager, material not found");
59  return;
60  }
61 
62  mMoonSceneNode = mSkyX->getSceneManager()->getRootSceneNode()->createChildSceneNode();
63 
64  mMoonBillboard = mSkyX->getSceneManager()->createBillboardSet("SkyXMoonBillboardSet", 1);
65  mMoonBillboard->setMaterialName(mSkyX->getGPUManager()->getMoonMaterialName());
66  mMoonBillboard->setBillboardType(Ogre::BBT_ORIENTED_COMMON);
67  mMoonBillboard->setRenderQueueGroup(mSkyX->getRenderQueueGroups().skydome+1);
68  mMoonBillboard->setCastShadows(false);
69 
70  mMoonBillboard->createBillboard(Ogre::Vector3(0,0,0));
71 
72  mMoonSceneNode->attachObject(mMoonBillboard);
73 
74  mCreated = true;
75  }
76 
78  {
79  if (!mCreated)
80  {
81  return;
82  }
83 
84  mMoonSceneNode->detachAllObjects();
85  mMoonSceneNode->getParentSceneNode()->removeAndDestroyChild(mMoonSceneNode);
86  mMoonSceneNode = 0;
87 
88  mSkyX->getSceneManager()->destroyBillboardSet(mMoonBillboard);
89  mMoonBillboard = 0;
90 
91  mMoonMaterial.setNull();
92 
93  mCreated = false;
94  }
95 
96  void MoonManager::updateMoonPhase(const Ogre::Real& phase)
97  {
98  Ogre::Real center = 0, radius = 0, radius_add = 0, interpolation = 0, halo_flip = 0;
99 
100  Ogre::Vector3 halo1, halo2;
101 
102  // [-1, 0]
103  if (phase < 0)
104  {
105  // [-1, -0.5]
106  if (phase < -0.5)
107  {
108  center = (1+phase)/2;
109  radius = 0.25;
110 
111  interpolation = center*4;
112 
113  if (interpolation < 1.0f/3)
114  {
115  interpolation /= 1.0f/3;
116  halo1 = Ogre::Vector3(0.25, 0.5, (1-interpolation)*interpolation);
117  halo2 = Ogre::Vector3(0.25, 0.5, interpolation);
118  }
119  else if (interpolation < 2.0f/3)
120  {
121  interpolation -= 1.0f/3;
122  interpolation /= 1.0f/3;
123  halo1 = Ogre::Vector3(0.25, 0.5, 1-interpolation);
124  halo2 = Ogre::Vector3(0.0, 0.5, interpolation);
125  }
126  else
127  {
128  interpolation -= 2.0f/3;
129  interpolation /= 1.0f/3;
130  halo1 = Ogre::Vector3(0.0, 0.5, 1-interpolation);
131  halo2 = Ogre::Vector3(0.75, 0.0, interpolation);
132  }
133 
134  radius_add = 0.1*center/(0.25001-center);
135 
136  radius += radius_add;
137  center += radius_add;
138  }
139  // [-0.5, 0]
140  else
141  {
142  center = (-phase)/2;
143  radius = 0.25;
144 
145  interpolation = 1-center*4;
146 
147  if (interpolation < 1.0f/3)
148  {
149  interpolation /= 1.0f/3;
150  halo1 = Ogre::Vector3(0.75, 0.0, 1-interpolation);
151  halo2 = Ogre::Vector3(0.5, 0.0, interpolation);
152  }
153  else if (interpolation < 2.0f/3)
154  {
155  interpolation -= 1.0f/3;
156  interpolation /= 1.0f/3;
157  halo1 = Ogre::Vector3(0.5, 0.0, 1-interpolation);
158  halo2 = Ogre::Vector3(0.25, 0.0, interpolation);
159  }
160  else
161  {
162  interpolation -= 2.0f/3;
163  interpolation /= 1.0f/3;
164  halo1 = Ogre::Vector3(0.25, 0.0, 1-interpolation);
165  halo2 = Ogre::Vector3(0.00, 0.0, interpolation);
166  }
167 
168  radius_add = 0.1*center/(0.25001-center);
169 
170  radius += radius_add;
171  center += radius_add;
172 
173  radius = -radius;
174  center = -center;
175  }
176  }
177  // [0, 1]
178  else
179  {
180  halo_flip = 1;
181 
182  // [0, 0.5]
183  if (phase < 0.5)
184  {
185  center = phase/2;
186  radius = 0.25;
187 
188  interpolation = center*4;
189 
190  if (interpolation < 1.0f/3)
191  {
192  interpolation /= 1.0f/3;
193  halo1 = Ogre::Vector3(0.00, 0.0, 1-interpolation);
194  halo2 = Ogre::Vector3(0.25, 0.0, interpolation);
195  }
196  else if (interpolation < 2.0f/3)
197  {
198  interpolation -= 1.0f/3;
199  interpolation /= 1.0f/3;
200  halo1 = Ogre::Vector3(0.25, 0.0, 1-interpolation);
201  halo2 = Ogre::Vector3(0.5, 0.0, interpolation);
202  }
203  else
204  {
205  interpolation -= 2.0f/3;
206  interpolation /= 1.0f/3;
207  halo1 = Ogre::Vector3(0.5, 0.0, 1-interpolation);
208  halo2 = Ogre::Vector3(0.75, 0.0, interpolation);
209  }
210 
211  radius_add = 0.1*center/(0.25001-center);
212 
213  radius += radius_add;
214  center += radius_add;
215 
216  radius = -radius;
217  center = center;
218  }
219  // [0.5, 1]
220  else
221  {
222  center = (1-phase)/2;
223  radius = 0.25;
224 
225  interpolation = 1-center*4;
226 
227  if (interpolation < 1.0f/3)
228  {
229  interpolation /= 1.0f/3;
230  halo1 = Ogre::Vector3(0.75, 0.0, 1-interpolation);
231  halo2 = Ogre::Vector3(0.0, 0.5, interpolation);
232  }
233  else if (interpolation < 2.0f/3)
234  {
235  interpolation -= 1.0f/3;
236  interpolation /= 1.0f/3;
237  halo1 = Ogre::Vector3(0.0, 0.5, 1-interpolation);
238  halo2 = Ogre::Vector3(0.25, 0.5, interpolation);
239  }
240  else
241  {
242  interpolation -= 2.0f/3;
243  interpolation /= 1.0f/3;
244  halo1 = Ogre::Vector3(0.25, 0.5, 1-interpolation);
245  halo2 = Ogre::Vector3(0.25, 0.5, (1-interpolation)*interpolation);
246  }
247 
248  radius_add = 0.1*center/(0.25001-center);
249 
250  radius += radius_add;
251  center += radius_add;
252 
253  center = -center;
254  }
255  }
256 
257  mMoonMaterial->getTechnique(0)->getPass(0)->getFragmentProgramParameters()->setNamedConstant("uMoonPhase", Ogre::Vector3(radius, center + 0.5f, mMoonHaloStrength));
258 
259  halo1.z *= mMoonHaloIntensity;
260  halo2.z *= mMoonHaloIntensity;
261 
262  mMoonMaterial->getTechnique(0)->getPass(0)->getFragmentProgramParameters()->setNamedConstant("uMoonHalo1", halo1);
263  mMoonMaterial->getTechnique(0)->getPass(0)->getFragmentProgramParameters()->setNamedConstant("uMoonHalo2", halo2);
264  mMoonMaterial->getTechnique(0)->getPass(0)->getFragmentProgramParameters()->setNamedConstant("uMoonHaloFlip", halo_flip);
265  }
266 
267  void MoonManager::updateGeometry(Ogre::Camera* c)
268  {
269  if (!mCreated)
270  {
271  return;
272  }
273 
274  float radius = mSkyX->getMeshManager()->getSkydomeRadius(c)*0.95f,
275  size = radius*mMoonSize;
276 
277  mMoonBillboard->setCommonDirection((mSkyX->getController()->getMoonDirection()).normalisedCopy().perpendicular());
278 
279  Ogre::Vector3 moonRelativePos = mSkyX->getController()->getMoonDirection()*
280  Ogre::Math::Cos(Ogre::Math::ASin((size/2)/radius))*radius;
281 
282  mMoonSceneNode->setPosition(c->getDerivedPosition() + moonRelativePos);
283 
284  if (moonRelativePos.y < -size/2)
285  {
286  mMoonSceneNode->setVisible(false);
287  }
288  else
289  {
290  mMoonSceneNode->setVisible(mSkyX->isVisible());
291 
292  mMoonMaterial->getTechnique(0)->getPass(0)
293  ->getVertexProgramParameters()->setNamedConstant("uSkydomeCenter", c->getDerivedPosition());
294  }
295 
296  if (mMoonBillboard->getBoundingBox().getMaximum().x != size)
297  {
299  }
300  }
301 
302  void MoonManager::_updateMoonBounds(Ogre::Camera* c)
303  {
304  float radius = mSkyX->getMeshManager()->getSkydomeRadius(c)*0.95f,
305  size = radius*mMoonSize;
306 
307  mMoonBillboard->setDefaultDimensions(size, size);
308  mMoonBillboard->setBounds(Ogre::AxisAlignedBox(-size/2, -size/2, -size/2,
309  size/2, size/2, size/2), 1);
310  mMoonSceneNode->_updateBounds();
311  }
312 }
SkyX::SkyX::getRenderQueueGroups
const RenderQueueGroups & getRenderQueueGroups() const
Get render queue groups.
Definition: SkyX.h:257
SkyX::MoonManager::mMoonBillboard
Ogre::BillboardSet * mMoonBillboard
Moon billboard.
Definition: MoonManager.h:142
SkyX::MoonManager::mMoonSceneNode
Ogre::SceneNode * mMoonSceneNode
Moon scene node.
Definition: MoonManager.h:144
SkyX::SkyX::getController
Controller * getController() const
Get current controller.
Definition: SkyX.h:244
SkyX::MoonManager::MoonManager
MoonManager(SkyX *s)
Constructor.
Definition: MoonManager.cpp:30
SkyX::SkyX::getSceneManager
Ogre::SceneManager * getSceneManager()
Get scene manager.
Definition: SkyX.h:315
SkyX
Definition: AtmosphereManager.cpp:30
SkyX::MoonManager::remove
void remove()
Remove all resources.
Definition: MoonManager.cpp:77
SkyX::SkyX::getMeshManager
MeshManager * getMeshManager()
Get mesh manager.
Definition: SkyX.h:183
SkyX::MeshManager::getSkydomeRadius
const float getSkydomeRadius(Ogre::Camera *c) const
Get skydome radius.
Definition: MeshManager.cpp:360
SkyX::Controller::getMoonDirection
virtual Ogre::Vector3 getMoonDirection()=0
Get moon direction.
SkyX::MoonManager::mMoonHaloIntensity
Ogre::Real mMoonHaloIntensity
Moon halo intensity.
Definition: MoonManager.h:152
SkyX.h
SkyX::MoonManager::mMoonMaterial
Ogre::MaterialPtr mMoonMaterial
Moon material.
Definition: MoonManager.h:157
SkyX::SkyX::getGPUManager
GPUManager * getGPUManager()
Get GPU manager.
Definition: SkyX.h:199
SkyX::MoonManager::mSkyX
SkyX * mSkyX
SkyX parent pointer.
Definition: MoonManager.h:160
SkyX::MoonManager::mMoonSize
Ogre::Real mMoonSize
Moon size.
Definition: MoonManager.h:150
SkyX::MoonManager::create
void create()
Create all resources.
Definition: MoonManager.cpp:47
SkyX::MoonManager::updateGeometry
void updateGeometry(Ogre::Camera *c)
Update geometry.
Definition: MoonManager.cpp:267
SkyX::MoonManager::~MoonManager
~MoonManager()
Destructor.
Definition: MoonManager.cpp:42
MoonManager.h
SkyX::MoonManager::_updateMoonBounds
void _updateMoonBounds(Ogre::Camera *c)
Update moon bounds.
Definition: MoonManager.cpp:302
SkyX::MoonManager::updateMoonPhase
void updateMoonPhase(const Ogre::Real &phase)
Update moon phase.
Definition: MoonManager.cpp:96
SkyX::GPUManager::getMoonMaterialName
const Ogre::String getMoonMaterialName() const
Get moon material name.
Definition: GPUManager.h:102
SkyXLOG
#define SkyXLOG(msg)
Include external headers.
Definition: Prerequisites.h:34
Ogre
Definition: ExtinguishableFireAffector.cpp:35
SkyX::SkyX::RenderQueueGroups::skydome
Ogre::uint8 skydome
Skydome render queue group (Note: Moon = skydome_render_queue+1)
Definition: SkyX.h:80
SkyX::SkyX::isVisible
const bool & isVisible() const
Is SkyX visible?
Definition: SkyX.h:156
SkyX::MoonManager::mMoonHaloStrength
Ogre::Real mMoonHaloStrength
Moon halo strength.
Definition: MoonManager.h:154
SkyX::MoonManager::mCreated
bool mCreated
Is moon manager created?
Definition: MoonManager.h:147