Caelum
Sun.cpp
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 "InternalUtilities.h"
24 
25 namespace Caelum
26 {
27  const Ogre::String SphereSun::SUN_MATERIAL_NAME = "CaelumSphereSun";
28 
30  (
31  Ogre::SceneManager *sceneMgr,
32  Ogre::SceneNode *caelumRootNode,
33  const Ogre::String &meshName
34  ):
35  BaseSkyLight(sceneMgr, caelumRootNode)
36  {
37  Ogre::String uniqueSuffix = "/" + InternalUtilities::pointerToString (this);
38  mSunMaterial.reset (InternalUtilities::checkLoadMaterialClone (SUN_MATERIAL_NAME, SUN_MATERIAL_NAME + uniqueSuffix));
39 
40  mSunEntity.reset (sceneMgr->createEntity ("Caelum/SphereSun" + uniqueSuffix, meshName));
41  mSunEntity->setMaterialName (mSunMaterial->getName ());
42  mSunEntity->setCastShadows (false);
43  mSunEntity->setRenderQueueGroup (CAELUM_RENDER_QUEUE_SUN);
44 
45  mNode->attachObject (mSunEntity.get ());
46  }
47 
49 
50  void SphereSun::setBodyColour (const Ogre::ColourValue &colour) {
52 
53  // Set sun material colour.
54  mSunMaterial->setSelfIllumination (colour);
55  }
56 
57  void SphereSun::notifyCameraChanged (Ogre::Camera *cam) {
58  // This calls setFarRadius
60 
61  // Set sun position.
62  Ogre::Real sunDistance = mRadius - mRadius * Ogre::Math::Tan (Ogre::Degree (0.01));
63  mNode->setPosition(-mDirection * sunDistance);
64 
65  // Set sun scaling in [1.6(6) ~ 2.0] range.
66  float factor = 2 - mBodyColour.b / 3;
67  float scale = factor * sunDistance * Ogre::Math::Tan (Ogre::Degree (0.01));
68  mNode->setScale (Ogre::Vector3::UNIT_SCALE * scale);
69  }
70 
71  const Ogre::String SpriteSun::SUN_MATERIAL_NAME = "CaelumSpriteSun";
72 
74  Ogre::SceneManager *sceneMgr,
75  Ogre::SceneNode *caelumRootNode,
76  const Ogre::String &sunTextureName,
77  const Ogre::Degree& sunTextureAngularSize
78  ):
79  BaseSkyLight(sceneMgr, caelumRootNode),
80  mSunTextureAngularSize(sunTextureAngularSize)
81  {
82  Ogre::String uniqueSuffix = "/" + InternalUtilities::pointerToString (this);
83 
85  setSunTexture (sunTextureName);
86 
87  mSunBillboardSet.reset (sceneMgr->createBillboardSet ("Caelum/SpriteSun" + uniqueSuffix, 2));
88  mSunBillboardSet->setMaterialName (mSunMaterial->getName());
89  mSunBillboardSet->setCastShadows (false);
90  mSunBillboardSet->setRenderQueueGroup (CAELUM_RENDER_QUEUE_SUN);
91  mSunBillboardSet->setDefaultDimensions (1.0f, 1.0f);
92  mSunBillboardSet->createBillboard (Ogre::Vector3::ZERO);
93 
94  mNode->attachObject (mSunBillboardSet.get ());
95  }
96 
98 
99  void SpriteSun::setBodyColour (const Ogre::ColourValue &colour) {
101 
102  // Set sun material colour.
103  mSunBillboardSet->getBillboard (0)->setColour (colour);
104  }
105 
106  void SpriteSun::setSunTexture (const Ogre::String &textureName) {
107  // Update the sun material
108  assert(mSunMaterial->getBestTechnique ());
109  assert(mSunMaterial->getBestTechnique ()->getPass (0));
110  assert(mSunMaterial->getBestTechnique ()->getPass (0)->getTextureUnitState (0));
111  mSunMaterial->getBestTechnique ()->getPass (0)->getTextureUnitState (0)->setTextureName (textureName);
112  }
113 
114  void SpriteSun::setSunTextureAngularSize(const Ogre::Degree& sunTextureAngularSize){
115  mSunTextureAngularSize = sunTextureAngularSize;
116  }
117 
118  void SpriteSun::notifyCameraChanged (Ogre::Camera *cam) {
119  // This calls setFarRadius
121 
122  // Set sun position.
123  Ogre::Real sunDistance = mRadius - mRadius * Ogre::Math::Tan(mSunTextureAngularSize);
124  mNode->setPosition (-mDirection * sunDistance);
125 
126  // Set sun scaling in [1.0 ~ 1.2] range
127  float factor = 1.2f - mBodyColour.b * 0.2f;
128  float scale = factor * sunDistance * Ogre::Math::Tan(mSunTextureAngularSize);
129  mNode->setScale (Ogre::Vector3::UNIT_SCALE * scale);
130  }
131 }
void setBodyColour(const Ogre::ColourValue &colour)
Sets the sun sphere colour.
Definition: Sun.cpp:50
virtual void notifyCameraChanged(Ogre::Camera *cam)
Handle camera change.
Definition: Sun.cpp:57
virtual void notifyCameraChanged(Ogre::Camera *cam)
Handle camera change.
Definition: Sun.cpp:118
SpriteSun(Ogre::SceneManager *sceneMgr, Ogre::SceneNode *caelumRootNode, const Ogre::String &sunTextureName="sun_disc.png", const Ogre::Degree &sunTextureAngularSize=Ogre::Degree(3.77f))
Constructor.
Definition: Sun.cpp:73
virtual void notifyCameraChanged(Ogre::Camera *cam)=0
Notify new camera conditions.
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.
static Ogre::MaterialPtr checkLoadMaterialClone(const Ogre::String &originalName, const Ogre::String &cloneName)
Creates a private clone of a material from a script.
virtual ~SpriteSun()
Destructor.
Definition: Sun.cpp:97
virtual void setBodyColour(const Ogre::ColourValue &colour)
Sets the colour to draw the light&#39;s body with.
Definition: SkyLight.cpp:89
void setBodyColour(const Ogre::ColourValue &colour)
Sets the sun sphere colour.
Definition: Sun.cpp:99
PrivateBillboardSetPtr mSunBillboardSet
The sun sprite / billboard.
Definition: Sun.h:95
PrivateMaterialPtr mSunMaterial
The sun material.
Definition: Sun.h:92
static const Ogre::String SUN_MATERIAL_NAME
Name of the sun material.
Definition: Sun.h:44
Ogre::ColourValue mBodyColour
Body sphere colour, as set by setBodyColour.
Definition: SkyLight.h:47
void setSunTextureAngularSize(const Ogre::Degree &sunTextureAngularSize)
Updates the sun size.
Definition: Sun.cpp:114
virtual ~SphereSun()
Destructor.
Definition: Sun.cpp:48
Ogre::Degree mSunTextureAngularSize
The sun sprite visible angle.
Definition: Sun.h:98
Ogre::Vector3 mDirection
The latest normalised sun direction.
Definition: SkyLight.h:44
static const Ogre::String SUN_MATERIAL_NAME
Name of the sun material.
Definition: Sun.h:88
void reset(const InnerPointerType &newInner=TraitsT::getNullValue())
Change the inner value.
Definition: PrivatePtr.h:94
void setSunTexture(const Ogre::String &textureName)
Updates the sun material.
Definition: Sun.cpp:106
Caelum namespace.
Definition: Astronomy.cpp:24
SphereSun(Ogre::SceneManager *sceneMgr, Ogre::SceneNode *caelumRootNode, const Ogre::String &meshName="sphere.mesh")
Constructor.
Definition: Sun.cpp:30