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

« back to all changes in this revision

Viewing changes to src/components/ogre/environment/caelum/src/Sun.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) 2006-2007 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 "Sun.h"
 
23
#include "SkyLight.h"
 
24
 
 
25
namespace caelum {
 
26
 
 
27
//========================================================================================================================
 
28
// Sphere Sun - deprecated, use Sprite Sun instead
 
29
//========================================================================================================================
 
30
 
 
31
const Ogre::String SphereSun::SUN_MATERIAL_NAME = "CaelumSphereSun";
 
32
 
 
33
SphereSun::SphereSun (Ogre::SceneManager *sceneMgr, Ogre::SceneNode *caelumRootNode, const Ogre::String &meshName) : BaseSkyLight(sceneMgr, caelumRootNode) {
 
34
    mSunMaterial = Ogre::MaterialManager::getSingletonPtr()->getByName(SUN_MATERIAL_NAME);
 
35
        mSunMaterial = mSunMaterial->clone(SUN_MATERIAL_NAME + Ogre::StringConverter::toString((size_t)this));
 
36
    mSunMaterial->load();
 
37
        mSunEntity = sceneMgr->createEntity ("CaelumSun", meshName);
 
38
        mSunEntity->setMaterialName (mSunMaterial->getName());
 
39
        mSunEntity->setCastShadows (false);
 
40
        mSunEntity->setRenderQueueGroup (CAELUM_RENDER_QUEUE_SUN);
 
41
        mNode->attachObject (mSunEntity);
 
42
}
 
43
 
 
44
SphereSun::~SphereSun () {
 
45
        if (mNode) {
 
46
                mNode->detachObject (mSunEntity);
 
47
        }
 
48
        if (mSunEntity) {
 
49
                mSunEntity->_getManager ()->destroyEntity (mSunEntity);
 
50
                mSunEntity = 0;
 
51
        }
 
52
        Ogre::MaterialManager::getSingletonPtr()->remove(mSunMaterial->getHandle());
 
53
}
 
54
 
 
55
void SphereSun::setBodyColour (const Ogre::ColourValue &colour) {
 
56
        BaseSkyLight::setBodyColour(colour);
 
57
 
 
58
        // Set sun material colour.
 
59
        mSunMaterial->setSelfIllumination (colour);
 
60
}
 
61
 
 
62
void SphereSun::notifyCameraChanged (Ogre::Camera *cam) {
 
63
    // This calls setFarRadius
 
64
    CameraBoundElement::notifyCameraChanged(cam);
 
65
 
 
66
    // Set sun position.
 
67
    Ogre::Real sunDistance = mRadius - mRadius * Ogre::Math::Tan (Ogre::Degree (0.01));
 
68
        mNode->setPosition(-mDirection * sunDistance);
 
69
 
 
70
    // Set sun scaling  in [1.6(6) ~ 2.0] range.
 
71
    float factor = 2 - mBodyColour.b / 3;
 
72
    float scale = factor * sunDistance * Ogre::Math::Tan (Ogre::Degree (0.01));
 
73
    mNode->setScale (Ogre::Vector3::UNIT_SCALE * scale);
 
74
}
 
75
 
 
76
//========================================================================================================================
 
77
// Sprite Sun
 
78
//========================================================================================================================
 
79
 
 
80
const Ogre::String SpriteSun::SUN_MATERIAL_NAME = "CaelumSpriteSun";
 
81
 
 
82
SpriteSun::SpriteSun (  Ogre::SceneManager *sceneMgr,
 
83
                                                Ogre::SceneNode *caelumRootNode, 
 
84
                                                const Ogre::String &sunTextureName,
 
85
                                                const Ogre::Degree& sunTextureAngularSize) 
 
86
        : BaseSkyLight(sceneMgr, caelumRootNode)
 
87
        , mSunTextureAngularSize(sunTextureAngularSize)
 
88
{
 
89
    mSunMaterial = Ogre::MaterialManager::getSingletonPtr()->getByName(SUN_MATERIAL_NAME);
 
90
        mSunMaterial = mSunMaterial->clone(SUN_MATERIAL_NAME + Ogre::StringConverter::toString((size_t)this));
 
91
    mSunMaterial->load();
 
92
    assert(!mSunMaterial.isNull());
 
93
    setSunTexture(sunTextureName);
 
94
 
 
95
        mSunBillboardSet = sceneMgr->createBillboardSet("CaelumSpriteSun", 2);
 
96
        mSunBillboardSet->setMaterialName (mSunMaterial->getName());
 
97
        mSunBillboardSet->setCastShadows (false);
 
98
        mSunBillboardSet->setRenderQueueGroup (CAELUM_RENDER_QUEUE_SUN);
 
99
        mSunBillboardSet->setDefaultDimensions(1.0f, 1.0f);
 
100
        mSunBillboardSet->createBillboard(Ogre::Vector3::ZERO);
 
101
 
 
102
        mNode->attachObject (mSunBillboardSet);
 
103
}
 
104
 
 
105
SpriteSun::~SpriteSun () {
 
106
        if (mNode) {
 
107
                mNode->detachObject (mSunBillboardSet);
 
108
        }
 
109
        if (mSunBillboardSet) {
 
110
                mSunBillboardSet->_getManager ()->destroyBillboardSet (mSunBillboardSet);
 
111
                mSunBillboardSet = 0;
 
112
        }
 
113
        Ogre::MaterialManager::getSingletonPtr()->remove(mSunMaterial->getHandle());
 
114
}
 
115
 
 
116
void SpriteSun::setBodyColour (const Ogre::ColourValue &colour) {
 
117
        BaseSkyLight::setBodyColour(colour);
 
118
 
 
119
        // Set sun material colour.
 
120
        mSunBillboardSet->getBillboard(0)->setColour(colour);
 
121
}
 
122
 
 
123
void SpriteSun::setSunTexture (const Ogre::String &textureName) {
 
124
        // Update the sun material
 
125
        assert(mSunMaterial->getBestTechnique ());
 
126
        assert(mSunMaterial->getBestTechnique ()->getPass (0));
 
127
        assert(mSunMaterial->getBestTechnique ()->getPass (0)->getTextureUnitState (0));
 
128
        mSunMaterial->getBestTechnique ()->getPass (0)->getTextureUnitState (0)->setTextureName (textureName);
 
129
}
 
130
 
 
131
void SpriteSun::setSunTextureAngularSize(const Ogre::Degree& sunTextureAngularSize){
 
132
        mSunTextureAngularSize = sunTextureAngularSize;
 
133
}
 
134
 
 
135
void SpriteSun::notifyCameraChanged (Ogre::Camera *cam) {
 
136
    // This calls setFarRadius
 
137
    BaseSkyLight::notifyCameraChanged(cam);
 
138
 
 
139
    // Set sun position.
 
140
    Ogre::Real sunDistance = mRadius - mRadius * Ogre::Math::Tan(mSunTextureAngularSize);
 
141
        mNode->setPosition(-mDirection * sunDistance);
 
142
 
 
143
    // Set sun scaling in [1.0 ~ 1.2] range
 
144
    float factor = 1.2f - mBodyColour.b * 0.2f;
 
145
    float scale = factor * sunDistance * Ogre::Math::Tan(mSunTextureAngularSize);
 
146
    mNode->setScale (Ogre::Vector3::UNIT_SCALE * scale);
 
147
}
 
148
 
 
149
} // namespace caelum