00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "CaelumPrecompiled.h"
00022 #include "SkyDome.h"
00023 #include "CaelumExceptions.h"
00024 #include "InternalUtilities.h"
00025
00026 namespace Caelum
00027 {
00028 const Ogre::String SkyDome::SPHERIC_DOME_NAME = "CaelumSphericDome";
00029 const Ogre::String SkyDome::SKY_DOME_MATERIAL_NAME = "CaelumSkyDomeMaterial";
00030
00031 SkyDome::SkyDome (Ogre::SceneManager *sceneMgr, Ogre::SceneNode *caelumRootNode)
00032 {
00033 String uniqueSuffix = "/" + InternalUtilities::pointerToString(this);
00034
00035
00036 mMaterial.reset(InternalUtilities::checkLoadMaterialClone(SKY_DOME_MATERIAL_NAME, SKY_DOME_MATERIAL_NAME + uniqueSuffix));
00037
00038
00039 mShadersEnabled = mMaterial->getBestTechnique()->getPass(0)->isProgrammable();
00040
00041
00042 mHazeEnabled = true;
00043 setHazeEnabled(false);
00044
00045 sceneMgr->getRenderQueue()->getQueueGroup(CAELUM_RENDER_QUEUE_SKYDOME)->setShadowsEnabled(false);
00046
00047
00048 InternalUtilities::generateSphericDome (SPHERIC_DOME_NAME, 32, InternalUtilities::DT_SKY_DOME);
00049 mEntity.reset(sceneMgr->createEntity ("Caelum/SkyDome/Entity" + uniqueSuffix, SPHERIC_DOME_NAME));
00050 mEntity->setMaterialName (mMaterial->getName());
00051 mEntity->setRenderQueueGroup (CAELUM_RENDER_QUEUE_SKYDOME);
00052 mEntity->setCastShadows (false);
00053
00054 mNode.reset(caelumRootNode->createChildSceneNode ("Caelum/SkyDome/Node" + uniqueSuffix));
00055 mNode->attachObject (mEntity.get());
00056 }
00057
00058 SkyDome::~SkyDome () {
00059 }
00060
00061 void SkyDome::notifyCameraChanged (Ogre::Camera *cam) {
00062 CameraBoundElement::notifyCameraChanged (cam);
00063 }
00064
00065 void SkyDome::setFarRadius (Ogre::Real radius) {
00066 CameraBoundElement::setFarRadius(radius);
00067 mNode->setScale (Ogre::Vector3::UNIT_SCALE * radius);
00068 }
00069
00070 void SkyDome::setSunDirection (const Ogre::Vector3& sunDir) {
00071 float elevation = sunDir.dotProduct (Ogre::Vector3::UNIT_Y);
00072 elevation = elevation * 0.5 + 0.5;
00073 Ogre::Pass* pass = mMaterial->getBestTechnique()->getPass(0);
00074 if (mShadersEnabled) {
00075 mParams.sunDirection.set(mParams.vpParams, sunDir);
00076 mParams.offset.set(mParams.fpParams, elevation);
00077 } else {
00078 Ogre::TextureUnitState* gradientsTus = pass->getTextureUnitState(0);
00079 gradientsTus->setTextureUScroll (elevation);
00080 }
00081 }
00082
00083 void SkyDome::setHazeColour (const Ogre::ColourValue& hazeColour) {
00084 if (mShadersEnabled && mHazeEnabled) {
00085 mParams.hazeColour.set(mParams.fpParams, hazeColour);
00086 }
00087 }
00088
00089 void SkyDome::setSkyGradientsImage (const Ogre::String& gradients)
00090 {
00091 Ogre::TextureUnitState* gradientsTus =
00092 mMaterial->getTechnique (0)->getPass (0)->getTextureUnitState(0);
00093
00094 gradientsTus->setTextureAddressingMode (Ogre::TextureUnitState::TAM_CLAMP);
00095
00096
00097 #if OGRE_VERSION < ((1 << 16) | (3 << 8))
00098 gradientsTus->setTextureName (gradients, Ogre::TEX_TYPE_2D, -1, true);
00099 #else
00100 gradientsTus->setTextureName (gradients, Ogre::TEX_TYPE_2D);
00101 gradientsTus->setIsAlpha (true);
00102 #endif
00103 }
00104
00105 void SkyDome::setAtmosphereDepthImage (const Ogre::String& atmosphereDepth)
00106 {
00107 if (!mShadersEnabled) {
00108 return;
00109 }
00110
00111 Ogre::TextureUnitState* atmosphereTus =
00112 mMaterial->getTechnique (0)->getPass (0)->getTextureUnitState(1);
00113
00114 atmosphereTus->setTextureName (atmosphereDepth, Ogre::TEX_TYPE_1D);
00115 atmosphereTus->setTextureAddressingMode (Ogre::TextureUnitState::TAM_CLAMP, Ogre::TextureUnitState::TAM_WRAP, Ogre::TextureUnitState::TAM_WRAP);
00116 }
00117
00118 bool SkyDome::getHazeEnabled () const {
00119 return mHazeEnabled;
00120 }
00121
00122 void SkyDome::setHazeEnabled (bool value)
00123 {
00124 if (mHazeEnabled == value) {
00125 return;
00126 }
00127 mHazeEnabled = value;
00128
00129 if (!mShadersEnabled) {
00130 return;
00131 }
00132
00133 Ogre::Pass *pass = mMaterial->getTechnique (0)->getPass (0);
00134 if (value) {
00135 pass->setFragmentProgram("CaelumSkyDomeFP");
00136 } else {
00137 pass->setFragmentProgram("CaelumSkyDomeFP_NoHaze");
00138 }
00139 mParams.setup(
00140 pass->getVertexProgramParameters(),
00141 pass->getFragmentProgramParameters());
00142 }
00143
00144 void SkyDome::Params::setup(Ogre::GpuProgramParametersSharedPtr vpParams, Ogre::GpuProgramParametersSharedPtr fpParams)
00145 {
00146 this->fpParams = fpParams;
00147 this->vpParams = vpParams;
00148 sunDirection.bind(vpParams, "sunDirection");
00149 offset.bind(fpParams, "offset");
00150 hazeColour.bind(fpParams, "hazeColour");
00151 }
00152 }