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 "SkyDome.h"
23 #include "CaelumExceptions.h"
24 #include "InternalUtilities.h"
25 
26 namespace Caelum
27 {
28     const Ogre::String SkyDome::SPHERIC_DOME_NAME = "CaelumSphericDome";
29     const Ogre::String SkyDome::SKY_DOME_MATERIAL_NAME = "CaelumSkyDomeMaterial";
30 
SkyDome(Ogre::SceneManager * sceneMgr,Ogre::SceneNode * caelumRootNode)31     SkyDome::SkyDome (Ogre::SceneManager *sceneMgr, Ogre::SceneNode *caelumRootNode)
32     {
33         String uniqueSuffix = "/" + InternalUtilities::pointerToString(this);
34 
35         // First clone material
36         mMaterial.reset(InternalUtilities::checkLoadMaterialClone(SKY_DOME_MATERIAL_NAME, SKY_DOME_MATERIAL_NAME + uniqueSuffix));
37 
38         // Determine if the shader technique works.
39         mShadersEnabled = mMaterial->getBestTechnique()->getPass(0)->isProgrammable();
40 
41         // Force setting haze, ensure mHazeEnabled != value.
42         mHazeEnabled = true;
43         setHazeEnabled(false);
44 
45         sceneMgr->getRenderQueue()->getQueueGroup(CAELUM_RENDER_QUEUE_SKYDOME)->setShadowsEnabled(false);
46 
47         // Generate dome entity.
48         InternalUtilities::generateSphericDome (SPHERIC_DOME_NAME, 32, InternalUtilities::DT_SKY_DOME);
49         mEntity.reset(sceneMgr->createEntity ("Caelum/SkyDome/Entity" + uniqueSuffix, SPHERIC_DOME_NAME));
50         mEntity->setMaterialName (mMaterial->getName());
51         mEntity->setRenderQueueGroup (CAELUM_RENDER_QUEUE_SKYDOME);
52         mEntity->setCastShadows (false);
53 
54         mNode.reset(caelumRootNode->createChildSceneNode ("Caelum/SkyDome/Node" + uniqueSuffix));
55         mNode->attachObject (mEntity.get());
56     }
57 
~SkyDome()58     SkyDome::~SkyDome () {
59     }
60 
notifyCameraChanged(Ogre::Camera * cam)61     void SkyDome::notifyCameraChanged (Ogre::Camera *cam) {
62         CameraBoundElement::notifyCameraChanged (cam);
63     }
64 
setFarRadius(Ogre::Real radius)65     void SkyDome::setFarRadius (Ogre::Real radius) {
66         CameraBoundElement::setFarRadius(radius);
67         mNode->setScale (Ogre::Vector3::UNIT_SCALE * radius);
68     }
69 
setSunDirection(const Ogre::Vector3 & sunDir)70     void SkyDome::setSunDirection (const Ogre::Vector3& sunDir) {
71         float elevation = sunDir.dotProduct (Ogre::Vector3::UNIT_Y);
72         elevation = elevation * 0.5 + 0.5;
73         Ogre::Pass* pass = mMaterial->getBestTechnique()->getPass(0);
74         if (mShadersEnabled) {
75             mParams.sunDirection.set(mParams.vpParams, sunDir);
76             mParams.offset.set(mParams.fpParams, elevation);
77         } else {
78             Ogre::TextureUnitState* gradientsTus = pass->getTextureUnitState(0);
79             gradientsTus->setTextureUScroll (elevation);
80         }
81     }
82 
setHazeColour(const Ogre::ColourValue & hazeColour)83     void SkyDome::setHazeColour (const Ogre::ColourValue& hazeColour) {
84         if (mShadersEnabled && mHazeEnabled) {
85             mParams.hazeColour.set(mParams.fpParams, hazeColour);
86         }
87     }
88 
setSkyGradientsImage(const Ogre::String & gradients)89     void SkyDome::setSkyGradientsImage (const Ogre::String& gradients)
90     {
91         Ogre::TextureUnitState* gradientsTus =
92                 mMaterial->getTechnique (0)->getPass (0)->getTextureUnitState(0);
93 
94         gradientsTus->setTextureAddressingMode (Ogre::TextureUnitState::TAM_CLAMP);
95 
96         // Per 1.4 compatibility. Not tested with recent svn.
97         #if OGRE_VERSION < ((1 << 16) | (3 << 8))
98             gradientsTus->setTextureName (gradients, Ogre::TEX_TYPE_2D, -1, true);
99         #else
100             gradientsTus->setTextureName (gradients, Ogre::TEX_TYPE_2D);
101             gradientsTus->setIsAlpha (true);
102         #endif
103     }
104 
setAtmosphereDepthImage(const Ogre::String & atmosphereDepth)105     void SkyDome::setAtmosphereDepthImage (const Ogre::String& atmosphereDepth)
106     {
107         if (!mShadersEnabled) {
108             return;
109         }
110 
111         Ogre::TextureUnitState* atmosphereTus =
112                 mMaterial->getTechnique (0)->getPass (0)->getTextureUnitState(1);
113 
114         atmosphereTus->setTextureName (atmosphereDepth, Ogre::TEX_TYPE_1D);
115         atmosphereTus->setTextureAddressingMode (Ogre::TextureUnitState::TAM_CLAMP, Ogre::TextureUnitState::TAM_WRAP, Ogre::TextureUnitState::TAM_WRAP);
116     }
117 
getHazeEnabled() const118     bool SkyDome::getHazeEnabled () const {
119         return mHazeEnabled;
120     }
121 
setHazeEnabled(bool value)122     void SkyDome::setHazeEnabled (bool value)
123     {
124         if (mHazeEnabled == value) {
125             return;
126         }
127         mHazeEnabled = value;
128 
129         if (!mShadersEnabled) {
130             return;
131         }
132 
133         Ogre::Pass *pass = mMaterial->getTechnique (0)->getPass (0);
134         if (value) {
135             pass->setFragmentProgram("CaelumSkyDomeFP");
136         } else {
137             pass->setFragmentProgram("CaelumSkyDomeFP_NoHaze");
138         }
139         mParams.setup(
140                 pass->getVertexProgramParameters(),
141                 pass->getFragmentProgramParameters());
142     }
143 
setup(Ogre::GpuProgramParametersSharedPtr vpParams,Ogre::GpuProgramParametersSharedPtr fpParams)144     void SkyDome::Params::setup(Ogre::GpuProgramParametersSharedPtr vpParams, Ogre::GpuProgramParametersSharedPtr fpParams)
145     {
146         this->fpParams = fpParams;
147         this->vpParams = vpParams;
148         sunDirection.bind(vpParams, "sunDirection");
149         offset.bind(fpParams, "offset");
150         hazeColour.bind(fpParams, "hazeColour");
151     }
152 }
153