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 
Moon(Ogre::SceneManager * sceneMgr,Ogre::SceneNode * caelumRootNode,const Ogre::String & moonTextureName,Ogre::Degree angularSize)32     Moon::Moon (
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
44         mMoonMaterial.reset(InternalUtilities::checkLoadMaterialClone(MOON_MATERIAL_NAME, MOON_MATERIAL_NAME + uniqueSuffix));
45         mBackMaterial.reset(InternalUtilities::checkLoadMaterialClone(MOON_BACKGROUND_MATERIAL_NAME, MOON_BACKGROUND_MATERIAL_NAME + uniqueSuffix));
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 
~Moon()73     Moon::~Moon () {
74     }
75 
setBodyColour(const Ogre::ColourValue & colour)76     void Moon::setBodyColour (const Ogre::ColourValue &colour) {
77 	    BaseSkyLight::setBodyColour(colour);
78 
79 	    // Set moon material colour.
80 	    mMoonBB->getBillboard(0)->setColour(colour);
81     }
82 
setMoonTexture(const Ogre::String & textureName)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 
setMoonTextureAngularSize(const Ogre::Degree & moonTextureAngularSize)93     void Moon::setMoonTextureAngularSize(const Ogre::Degree& moonTextureAngularSize){
94 	    mAngularSize = moonTextureAngularSize;
95     }
96 
notifyCameraChanged(Ogre::Camera * cam)97     void Moon::notifyCameraChanged (Ogre::Camera *cam) {
98         // This calls setFarRadius
99         BaseSkyLight::notifyCameraChanged(cam);
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 
setup(Ogre::GpuProgramParametersSharedPtr fpParams)111     void Moon::Params::setup(Ogre::GpuProgramParametersSharedPtr fpParams)
112     {
113         this->fpParams = fpParams;
114         this->phase.bind(fpParams, "phase");
115     }
116 
setPhase(Ogre::Real phase)117     void Moon::setPhase (Ogre::Real phase) {
118         mParams.phase.set(mParams.fpParams, phase);
119     }
120 
setMoonNorthPoleDirection(const Ogre::Vector3 & moonNorthPoleDir)121     void Moon::setMoonNorthPoleDirection(const Ogre::Vector3& moonNorthPoleDir)
122     {
123         mMoonBB->setBillboardType(Ogre::BBT_ORIENTED_COMMON);
124         mMoonBB->setCommonDirection(moonNorthPoleDir);
125     }
126 
setQueryFlags(uint flags)127     void Moon::setQueryFlags (uint flags) {
128         mMoonBB->setQueryFlags (flags);
129         mBackBB->setQueryFlags (flags);
130     }
131 
getQueryFlags() const132     uint Moon::getQueryFlags () const {
133         assert (mMoonBB->getQueryFlags () == mBackBB->getQueryFlags ());
134         return mMoonBB->getQueryFlags ();
135     }
136 
setVisibilityFlags(uint flags)137     void Moon::setVisibilityFlags (uint flags) {
138         mMoonBB->setVisibilityFlags (flags);
139         mBackBB->setVisibilityFlags (flags);
140     }
141 
getVisibilityFlags() const142     uint Moon::getVisibilityFlags () const {
143         assert (mMoonBB->getVisibilityFlags () == mBackBB->getVisibilityFlags ());
144         return mMoonBB->getVisibilityFlags ();
145     }
146 }
147