Caelum
Moon.cpp
1 /*
2 This file is part of Caelum.
3 See http://www.ogre3d.org/wiki/index.php/Caelum
4 
5 Copyright (c) 2008 Caelum team. See Contributors.txt for details.
6 
7 Caelum is free software: you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published
9 by the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11 
12 Caelum 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 Lesser General Public License for more details.
16 
17 You should have received a copy of the GNU Lesser General Public License
18 along with Caelum. If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 #include "CaelumPrecompiled.h"
22 #include "CaelumExceptions.h"
23 #include "InternalUtilities.h"
24 #include "Moon.h"
25 #include <memory>
26 
27 namespace Caelum
28 {
29  const Ogre::String Moon::MOON_MATERIAL_NAME = "Caelum/PhaseMoon";
30  const Ogre::String Moon::MOON_BACKGROUND_MATERIAL_NAME = "Caelum/MoonBackground";
31 
33  Ogre::SceneManager *sceneMgr,
34  Ogre::SceneNode *caelumRootNode,
35  const Ogre::String& moonTextureName,
36  Ogre::Degree angularSize
37  ):
38  BaseSkyLight(sceneMgr, caelumRootNode),
39  mAngularSize(angularSize)
40  {
41  Ogre::String uniqueSuffix = "/" + InternalUtilities::pointerToString(this);
42 
43  // Clone materials
46 
47  assert (!mMoonMaterial.isNull ());
48  assert (mMoonMaterial->getTechnique (0));
49  assert (mMoonMaterial->getTechnique (0)->getPass (0));
50  assert (mMoonMaterial->getTechnique (0)->getPass( 0)->hasFragmentProgram ());
51  mParams.setup(mMoonMaterial->getBestTechnique ()->getPass (0)->getFragmentProgramParameters ());
52 
53  setMoonTexture(moonTextureName);
54 
55  mMoonBB.reset(sceneMgr->createBillboardSet("Caelum/Moon/MoonBB" + uniqueSuffix, 1));
56  mMoonBB->setMaterialName (mMoonMaterial->getName());
57  mMoonBB->setCastShadows (false);
58  mMoonBB->setRenderQueueGroup (CAELUM_RENDER_QUEUE_MOON);
59  mMoonBB->setDefaultDimensions (1.0f, 1.0f);
60  mMoonBB->createBillboard (Ogre::Vector3::ZERO);
61 
62  mBackBB.reset(sceneMgr->createBillboardSet("Caelum/Moon/BackBB" + uniqueSuffix, 1));
63  mBackBB->setMaterialName (mBackMaterial->getName());
64  mBackBB->setCastShadows (false);
65  mBackBB->setRenderQueueGroup (CAELUM_RENDER_QUEUE_MOON_BACKGROUND);
66  mBackBB->setDefaultDimensions (1.0f, 1.0f);
67  mBackBB->createBillboard (Ogre::Vector3::ZERO);
68 
69  mNode->attachObject (mMoonBB.get());
70  mNode->attachObject (mBackBB.get());
71  }
72 
73  Moon::~Moon () {
74  }
75 
76  void Moon::setBodyColour (const Ogre::ColourValue &colour) {
78 
79  // Set moon material colour.
80  mMoonBB->getBillboard(0)->setColour(colour);
81  }
82 
83  void Moon::setMoonTexture (const Ogre::String &textureName)
84  {
85  // Update the moon material
86  assert(mMoonMaterial->getBestTechnique ());
87  assert(mMoonMaterial->getBestTechnique ()->getPass (0));
88  assert(mMoonMaterial->getBestTechnique ()->getPass (0)->getTextureUnitState (0));
89  mMoonMaterial->getBestTechnique ()->getPass (0)->getTextureUnitState (0)->setTextureName (textureName);
90  mBackMaterial->getBestTechnique ()->getPass (0)->getTextureUnitState (0)->setTextureName (textureName);
91  }
92 
93  void Moon::setMoonTextureAngularSize(const Ogre::Degree& moonTextureAngularSize){
94  mAngularSize = moonTextureAngularSize;
95  }
96 
97  void Moon::notifyCameraChanged (Ogre::Camera *cam) {
98  // This calls setFarRadius
100 
101  // Set moon position.
102  Ogre::Real moonDistance = mRadius - mRadius * Ogre::Math::Tan(mAngularSize);
103  mNode->setPosition(-mDirection * moonDistance);
104 
105  // Set moon scaling in [1.0 ~ 1.2] range
106  float factor = 1.2f - mBodyColour.b * 0.2f;
107  float scale = factor * moonDistance * Ogre::Math::Tan(mAngularSize);
108  mNode->setScale (Ogre::Vector3::UNIT_SCALE * scale);
109  }
110 
111  void Moon::Params::setup(Ogre::GpuProgramParametersSharedPtr fpParams)
112  {
113  this->fpParams = fpParams;
114  this->phase.bind(fpParams, "phase");
115  }
116 
117  void Moon::setPhase (Ogre::Real phase) {
118  mParams.phase.set(mParams.fpParams, phase);
119  }
120 
121  void Moon::setMoonNorthPoleDirection(const Ogre::Vector3& moonNorthPoleDir)
122  {
123  mMoonBB->setBillboardType(Ogre::BBT_ORIENTED_COMMON);
124  mMoonBB->setCommonDirection(moonNorthPoleDir);
125  }
126 
127  void Moon::setQueryFlags (uint flags) {
128  mMoonBB->setQueryFlags (flags);
129  mBackBB->setQueryFlags (flags);
130  }
131 
132  uint Moon::getQueryFlags () const {
133  assert (mMoonBB->getQueryFlags () == mBackBB->getQueryFlags ());
134  return mMoonBB->getQueryFlags ();
135  }
136 
137  void Moon::setVisibilityFlags (uint flags) {
138  mMoonBB->setVisibilityFlags (flags);
139  mBackBB->setVisibilityFlags (flags);
140  }
141 
142  uint Moon::getVisibilityFlags () const {
143  assert (mMoonBB->getVisibilityFlags () == mBackBB->getVisibilityFlags ());
144  return mMoonBB->getVisibilityFlags ();
145  }
146 }
static const Ogre::String MOON_BACKGROUND_MATERIAL_NAME
Name of the moon background material.
Definition: Moon.h:43
virtual void notifyCameraChanged(Ogre::Camera *cam)
Handle camera change.
Definition: Moon.cpp:97
virtual void notifyCameraChanged(Ogre::Camera *cam)=0
Notify new camera conditions.
void setMoonNorthPoleDirection(const Ogre::Vector3 &moonNorthPoleDir)
Set Moon`s north pole direction.
Definition: Moon.cpp:121
float mRadius
Base distance of the light.
Definition: SkyLight.h:41
Base class for sky lights (sun and moon).
Definition: SkyLight.h:31
Ogre::SceneNode * mNode
The sun scene node.
Definition: SkyLight.h:38
static const Ogre::String pointerToString(void *pointer)
Quickly format a pointer as a string; in hex.
void setBodyColour(const Ogre::ColourValue &colour)
Sets the moon sphere colour.
Definition: Moon.cpp:76
static Ogre::MaterialPtr checkLoadMaterialClone(const Ogre::String &originalName, const Ogre::String &cloneName)
Creates a private clone of a material from a script.
void setPhase(Ogre::Real phase)
Set the moon&#39;s phase.
Definition: Moon.cpp:117
virtual void setBodyColour(const Ogre::ColourValue &colour)
Sets the colour to draw the light&#39;s body with.
Definition: SkyLight.cpp:89
static const Ogre::String MOON_MATERIAL_NAME
Name of the moon material.
Definition: Moon.h:40
void setMoonTextureAngularSize(const Ogre::Degree &moonTextureAngularSize)
Updates the moon size.
Definition: Moon.cpp:93
bool isNull() const
Check if this is null.
Definition: PrivatePtr.h:142
Ogre::ColourValue mBodyColour
Body sphere colour, as set by setBodyColour.
Definition: SkyLight.h:47
void setMoonTexture(const Ogre::String &textureName)
Updates the moon material.
Definition: Moon.cpp:83
Ogre::Vector3 mDirection
The latest normalised sun direction.
Definition: SkyLight.h:44
void reset(const InnerPointerType &newInner=TraitsT::getNullValue())
Change the inner value.
Definition: PrivatePtr.h:94
Caelum namespace.
Definition: Astronomy.cpp:24
Moon(Ogre::SceneManager *sceneMgr, Ogre::SceneNode *caelumRootNode, const Ogre::String &moonTextureName="moon_disc.dds", Ogre::Degree angularSize=Ogre::Degree(3.77f))
Constructor.
Definition: Moon.cpp:32