~ubuntu-branches/debian/sid/ember/sid

« back to all changes in this revision

Viewing changes to src/components/ogre/environment/caelum/src/Moon.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michael Koch
  • Date: 2009-07-23 07:46:40 UTC
  • Revision ID: james.westby@ubuntu.com-20090723074640-wh0ukzis0kda36qv
Tags: upstream-0.5.6
ImportĀ upstreamĀ versionĀ 0.5.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 "Moon.h"
 
24
 
 
25
namespace caelum
 
26
{
 
27
    const Ogre::String Moon::MOON_MATERIAL_NAME = "Caelum/PhaseMoon";
 
28
 
 
29
    const Ogre::String Moon::MOON_BACKROUND_MATERIAL_NAME = "Caelum/MoonBackground";
 
30
 
 
31
    Moon::Moon (
 
32
                    Ogre::SceneManager *sceneMgr,
 
33
                    Ogre::SceneNode *caelumRootNode, 
 
34
                    const Ogre::String& moonTextureName,
 
35
                    Ogre::Degree angularSize
 
36
    ):
 
37
                    BaseSkyLight(sceneMgr, caelumRootNode),
 
38
                    mAngularSize(angularSize)
 
39
    {
 
40
        // Clone moon face material.
 
41
        mMoonMaterial = Ogre::MaterialManager::getSingletonPtr()->getByName(MOON_MATERIAL_NAME);
 
42
        if (mMoonMaterial.isNull()) {
 
43
            CAELUM_THROW_UNSUPPORED_EXCEPTION("Can't find moon material", "Moon");
 
44
        }
 
45
            mMoonMaterial = mMoonMaterial->clone(mMoonMaterial->getName() + Ogre::StringConverter::toString((size_t)this));
 
46
        mMoonMaterial->load();
 
47
        if (mMoonMaterial->getBestTechnique() == NULL) {
 
48
            CAELUM_THROW_UNSUPPORED_EXCEPTION("Moon material not supported", "Moon");
 
49
        }
 
50
 
 
51
        // Clone moon back material.
 
52
        mBackMaterial = Ogre::MaterialManager::getSingletonPtr()->getByName(MOON_BACKROUND_MATERIAL_NAME);
 
53
        if (mBackMaterial.isNull()) {
 
54
            CAELUM_THROW_UNSUPPORED_EXCEPTION("Can't find moon background material", "Moon");
 
55
        }
 
56
            mBackMaterial = mBackMaterial->clone(mBackMaterial->getName() + Ogre::StringConverter::toString((size_t)this));
 
57
        mBackMaterial->load();
 
58
        if (mBackMaterial->getBestTechnique() == NULL) {
 
59
            CAELUM_THROW_UNSUPPORED_EXCEPTION("Moon background material not supported", "Moon");
 
60
        }
 
61
 
 
62
            getFpParams()->setIgnoreMissingParams(true);
 
63
        setMoonTexture(moonTextureName);
 
64
 
 
65
            mMoonBB = sceneMgr->createBillboardSet(
 
66
                "Caelum/Moon/MoonBB/" + Ogre::StringConverter::toString((size_t)this), 1);
 
67
            mMoonBB->setMaterialName (mMoonMaterial->getName());
 
68
            mMoonBB->setCastShadows (false);
 
69
            mMoonBB->setRenderQueueGroup (CAELUM_RENDER_QUEUE_MOON);
 
70
            mMoonBB->setDefaultDimensions (1.0f, 1.0f);
 
71
            mMoonBB->createBillboard (Ogre::Vector3::ZERO);
 
72
 
 
73
            mBackBB = sceneMgr->createBillboardSet(
 
74
                "Caelum/Moon/BackBB/" + Ogre::StringConverter::toString((size_t)this), 1);
 
75
            mBackBB->setMaterialName (mBackMaterial->getName());
 
76
            mBackBB->setCastShadows (false);
 
77
            mBackBB->setRenderQueueGroup (CAELUM_RENDER_QUEUE_MOON_BACKGROUND);
 
78
            mBackBB->setDefaultDimensions (1.0f, 1.0f);
 
79
            mBackBB->createBillboard (Ogre::Vector3::ZERO);
 
80
 
 
81
            mNode->attachObject (mMoonBB);
 
82
            mNode->attachObject (mBackBB);
 
83
    }
 
84
 
 
85
    Moon::~Moon () {
 
86
            if (mMoonBB) {
 
87
                    mMoonBB->_getManager ()->destroyBillboardSet (mMoonBB);
 
88
                    mMoonBB = 0;
 
89
            }
 
90
            if (mBackBB) {
 
91
                    mBackBB->_getManager ()->destroyBillboardSet (mBackBB);
 
92
                    mBackBB = 0;
 
93
            }
 
94
            Ogre::MaterialManager::getSingletonPtr()->remove(mMoonMaterial->getHandle());
 
95
            Ogre::MaterialManager::getSingletonPtr()->remove(mBackMaterial->getHandle());
 
96
    }
 
97
 
 
98
    void Moon::setBodyColour (const Ogre::ColourValue &colour) {
 
99
            BaseSkyLight::setBodyColour(colour);
 
100
 
 
101
            // Set moon material colour.
 
102
            mMoonBB->getBillboard(0)->setColour(colour);
 
103
    }
 
104
 
 
105
    void Moon::setMoonTexture (const Ogre::String &textureName)
 
106
    {
 
107
            // Update the moon material
 
108
            assert(mMoonMaterial->getBestTechnique ());
 
109
            assert(mMoonMaterial->getBestTechnique ()->getPass (0));
 
110
            assert(mMoonMaterial->getBestTechnique ()->getPass (0)->getTextureUnitState (0));
 
111
            mMoonMaterial->getBestTechnique ()->getPass (0)->getTextureUnitState (0)->setTextureName (textureName);
 
112
            mBackMaterial->getBestTechnique ()->getPass (0)->getTextureUnitState (0)->setTextureName (textureName);
 
113
    }
 
114
 
 
115
    void Moon::setMoonTextureAngularSize(const Ogre::Degree& moonTextureAngularSize){
 
116
            mAngularSize = moonTextureAngularSize;
 
117
    }
 
118
 
 
119
    void Moon::notifyCameraChanged (Ogre::Camera *cam) {
 
120
        // This calls setFarRadius
 
121
        BaseSkyLight::notifyCameraChanged(cam);
 
122
 
 
123
        // Set moon position.
 
124
        Ogre::Real moonDistance = mRadius - mRadius * Ogre::Math::Tan(mAngularSize);
 
125
            mNode->setPosition(-mDirection * moonDistance);
 
126
 
 
127
        // Set moon scaling in [1.0 ~ 1.2] range
 
128
        float factor = 1.2f - mBodyColour.b * 0.2f;
 
129
        float scale = factor * moonDistance * Ogre::Math::Tan(mAngularSize);
 
130
        mNode->setScale (Ogre::Vector3::UNIT_SCALE * scale);
 
131
    }
 
132
 
 
133
    Ogre::GpuProgramParametersSharedPtr Moon::getFpParams () {
 
134
        assert (!mMoonMaterial.isNull ());
 
135
        assert (mMoonMaterial->getBestTechnique ());
 
136
        assert (mMoonMaterial->getBestTechnique ()->getPass (0));
 
137
        assert (mMoonMaterial->getBestTechnique ()->getPass( 0)->hasFragmentProgram ());
 
138
            return mMoonMaterial->getBestTechnique ()->getPass (0)->getFragmentProgramParameters ();
 
139
    }
 
140
 
 
141
    void Moon::setPhase (Ogre::Real phase) {
 
142
            getFpParams ()->setNamedConstant ("phase", phase);  
 
143
    }
 
144
}