1
0
Fork 1
mirror of https://github.com/TES3MP/openmw-tes3mp.git synced 2025-01-16 12:49:56 +00:00
openmw-tes3mp/apps/openmw/mwrender/sky.cpp

875 lines
29 KiB
C++
Raw Normal View History

#include "sky.hpp"
#include <OgreCamera.h>
#include <OgreRenderWindow.h>
#include <OgreSceneNode.h>
#include <OgreMesh.h>
#include <OgreSubMesh.h>
#include <OgreSceneManager.h>
#include <OgreHardwareVertexBuffer.h>
#include <OgreHighLevelGpuProgramManager.h>
#include <OgreParticle.h>
#include <OgreParticleSystem.h>
#include <OgreEntity.h>
#include <OgreSubEntity.h>
#include <OgreTechnique.h>
#include <OgreControllerManager.h>
#include <OgreMeshManager.h>
2012-07-11 07:08:55 +00:00
#include <boost/lexical_cast.hpp>
#include <openengine/misc/rng.hpp>
2013-02-24 21:52:23 +00:00
#include <components/nifogre/ogrenifloader.hpp>
#include <components/misc/resourcehelpers.hpp>
2012-07-13 07:13:12 +00:00
#include <extern/shiny/Platforms/Ogre/OgreMaterial.hpp>
#include "../mwbase/environment.hpp"
#include "../mwbase/world.hpp"
2013-03-17 13:22:30 +00:00
#include "../mwworld/fallback.hpp"
2012-04-03 13:13:47 +00:00
#include "renderconst.hpp"
2012-04-06 15:21:56 +00:00
#include "renderingmanager.hpp"
2012-03-16 18:02:33 +00:00
using namespace MWRender;
using namespace Ogre;
namespace
{
void setAlpha (NifOgre::ObjectScenePtr scene, Ogre::MovableObject* movable, float alpha)
{
Ogre::MaterialPtr mat = scene->mMaterialControllerMgr.getWritableMaterial(movable);
Ogre::Material::TechniqueIterator techs = mat->getTechniqueIterator();
while(techs.hasMoreElements())
{
Ogre::Technique *tech = techs.getNext();
Ogre::Technique::PassIterator passes = tech->getPassIterator();
while(passes.hasMoreElements())
{
Ogre::Pass *pass = passes.getNext();
Ogre::ColourValue diffuse = pass->getDiffuse();
diffuse.a = alpha;
pass->setDiffuse(diffuse);
}
}
}
void setAlpha (NifOgre::ObjectScenePtr scene, float alpha)
{
for(size_t i = 0; i < scene->mParticles.size(); ++i)
setAlpha(scene, scene->mParticles[i], alpha);
for(size_t i = 0; i < scene->mEntities.size(); ++i)
{
if (scene->mEntities[i] != scene->mSkelBase)
setAlpha(scene, scene->mEntities[i], alpha);
}
}
}
BillboardObject::BillboardObject( const String& textureName,
2012-02-25 15:36:45 +00:00
const float initialSize,
const Vector3& position,
2012-07-13 07:13:12 +00:00
SceneNode* rootNode,
const std::string& material)
: mVisibility(1.0f)
{
2012-07-13 07:13:12 +00:00
SceneManager* sceneMgr = rootNode->getCreator();
Vector3 finalPosition = position.normalisedCopy() * 1000.f;
static unsigned int bodyCount=0;
mMaterial = sh::Factory::getInstance().createMaterialInstance ("BillboardMaterial"+StringConverter::toString(bodyCount), material);
mMaterial->setProperty("texture", sh::makeProperty(new sh::StringValue(textureName)));
2013-05-07 17:15:28 +00:00
static Ogre::Mesh* plane = MeshManager::getSingleton().createPlane("billboard",
ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, Ogre::Plane(Ogre::Vector3(0,0,1), 0), 1, 1, 1, 1, true, 1, 1, 1, Vector3::UNIT_Y).get();
plane->_setBounds(Ogre::AxisAlignedBox::BOX_INFINITE);
2013-05-07 17:15:28 +00:00
mEntity = sceneMgr->createEntity("billboard");
mEntity->setMaterialName("BillboardMaterial"+StringConverter::toString(bodyCount));
mEntity->setVisibilityFlags(RV_Sky);
mEntity->setCastShadows(false);
2012-07-13 07:13:12 +00:00
mNode = rootNode->createChildSceneNode();
mNode->setPosition(finalPosition);
mNode->attachObject(mEntity);
2013-05-14 18:34:40 +00:00
mNode->setScale(Ogre::Vector3(450.f*initialSize));
mNode->setOrientation(Ogre::Vector3::UNIT_Z.getRotationTo(-position.normalisedCopy()));
2012-07-13 07:13:12 +00:00
2012-07-20 15:08:15 +00:00
sh::Factory::getInstance().getMaterialInstance ("BillboardMaterial"+StringConverter::toString(bodyCount))->setListener(this);
2012-07-13 07:13:12 +00:00
bodyCount++;
}
2012-07-20 15:08:15 +00:00
void BillboardObject::requestedConfiguration (sh::MaterialInstance* m, const std::string& configuration)
{
}
void BillboardObject::createdConfiguration (sh::MaterialInstance* m, const std::string& configuration)
{
setVisibility(mVisibility);
setColour(mColour);
}
void BillboardObject::setVisible(const bool visible)
{
mEntity->setVisible(visible);
}
2012-02-26 19:46:09 +00:00
void BillboardObject::setSize(const float size)
{
2013-05-14 18:34:40 +00:00
mNode->setScale(450.f*size, 450.f*size, 450.f*size);
2012-02-26 19:46:09 +00:00
}
void BillboardObject::setVisibility(const float visibility)
{
2012-07-20 15:08:15 +00:00
mVisibility = visibility;
2012-07-13 07:13:12 +00:00
Ogre::MaterialPtr m = static_cast<sh::OgreMaterial*>(mMaterial->getMaterial ())->getOgreMaterial ();
for (int i=0; i<m->getNumTechniques(); ++i)
{
Ogre::Technique* t = m->getTechnique(i);
if (t->getNumPasses ())
t->getPass(0)->setDiffuse (0,0,0, visibility);
}
2012-02-26 19:46:09 +00:00
}
void BillboardObject::setPosition(const Vector3& pPosition)
{
Vector3 normalised = pPosition.normalisedCopy();
2012-03-18 19:32:50 +00:00
Vector3 finalPosition = normalised * 1000.f;
mNode->setOrientation(Ogre::Vector3::UNIT_Z.getRotationTo(-normalised));
mNode->setPosition(finalPosition);
}
2012-02-26 19:46:09 +00:00
Vector3 BillboardObject::getPosition() const
{
return mNode->getPosition();
2012-02-26 19:46:09 +00:00
}
2012-04-06 13:17:54 +00:00
void BillboardObject::setVisibilityFlags(int flags)
{
mEntity->setVisibilityFlags(flags);
2012-04-06 13:17:54 +00:00
}
void BillboardObject::setColour(const ColourValue& pColour)
{
2012-07-20 15:08:15 +00:00
mColour = pColour;
2012-07-13 07:13:12 +00:00
Ogre::MaterialPtr m = static_cast<sh::OgreMaterial*>(mMaterial->getMaterial ())->getOgreMaterial ();
for (int i=0; i<m->getNumTechniques(); ++i)
{
Ogre::Technique* t = m->getTechnique(i);
if (t->getNumPasses ())
t->getPass(0)->setSelfIllumination (pColour);
}
}
void BillboardObject::setRenderQueue(unsigned int id)
{
mEntity->setRenderQueueGroup(id);
}
SceneNode* BillboardObject::getNode()
{
return mNode;
}
Moon::Moon( const String& textureName,
2012-02-25 15:36:45 +00:00
const float initialSize,
const Vector3& position,
2012-07-13 07:13:12 +00:00
SceneNode* rootNode,
const std::string& material)
: BillboardObject(textureName, initialSize, position, rootNode, material)
, mType(Type_Masser)
{
2012-02-21 16:38:15 +00:00
setVisibility(1.0);
2012-02-29 09:13:25 +00:00
mMaterial->setProperty("alphatexture", sh::makeProperty(new sh::StringValue(textureName + "_alpha")));
2012-02-21 16:38:15 +00:00
mPhase = Moon::Phase_Full;
}
void Moon::setType(const Moon::Type& type)
{
mType = type;
}
void Moon::setPhase(const Moon::Phase& phase)
{
2012-03-15 19:35:23 +00:00
// Colour texture
2012-02-21 16:38:15 +00:00
Ogre::String textureName = "textures\\tx_";
2012-02-21 16:38:15 +00:00
if (mType == Moon::Type_Secunda) textureName += "secunda_";
else textureName += "masser_";
2012-02-21 16:38:15 +00:00
if (phase == Moon::Phase_New) textureName += "new";
else if (phase == Moon::Phase_WaxingCrescent) textureName += "one_wax";
else if (phase == Moon::Phase_WaxingHalf) textureName += "half_wax";
else if (phase == Moon::Phase_WaxingGibbous) textureName += "three_wax";
else if (phase == Moon::Phase_WaningCrescent) textureName += "one_wan";
else if (phase == Moon::Phase_WaningHalf) textureName += "half_wan";
else if (phase == Moon::Phase_WaningGibbous) textureName += "three_wan";
else if (phase == Moon::Phase_Full) textureName += "full";
2012-02-21 16:38:15 +00:00
textureName += ".dds";
2012-07-13 07:13:12 +00:00
if (mType == Moon::Type_Secunda)
{
2012-07-13 07:13:12 +00:00
sh::Factory::getInstance ().setTextureAlias ("secunda_texture", textureName);
sh::Factory::getInstance ().setTextureAlias ("secunda_texture_alpha", "textures\\tx_mooncircle_full_s.dds");
}
2012-07-13 07:13:12 +00:00
else
{
2012-07-13 07:13:12 +00:00
sh::Factory::getInstance ().setTextureAlias ("masser_texture", textureName);
sh::Factory::getInstance ().setTextureAlias ("masser_texture_alpha", "textures\\tx_mooncircle_full_m.dds");
}
2012-02-29 09:13:25 +00:00
2012-02-21 16:38:15 +00:00
mPhase = phase;
}
unsigned int Moon::getPhaseInt() const
{
if (mPhase == Moon::Phase_New) return 0;
else if (mPhase == Moon::Phase_WaxingCrescent) return 1;
else if (mPhase == Moon::Phase_WaningCrescent) return 1;
else if (mPhase == Moon::Phase_WaxingHalf) return 2;
else if (mPhase == Moon::Phase_WaningHalf) return 2;
else if (mPhase == Moon::Phase_WaxingGibbous) return 3;
else if (mPhase == Moon::Phase_WaningGibbous) return 3;
else if (mPhase == Moon::Phase_Full) return 4;
2012-02-29 09:13:25 +00:00
2012-02-21 16:38:15 +00:00
return 0;
}
2013-04-29 12:50:40 +00:00
SkyManager::SkyManager(Ogre::SceneNode *root, Ogre::Camera *pCamera)
2015-04-25 18:37:42 +00:00
: mCreated(false)
, mMoonRed(false)
, mIsStorm(false)
, mHour(0.0f)
, mDay(0)
, mMonth(0)
2015-04-25 18:37:42 +00:00
, mCloudAnimationTimer(0.f)
, mSun(NULL)
, mSunGlare(NULL)
, mMasser(NULL)
, mSecunda(NULL)
, mCamera(pCamera)
, mRootNode(NULL)
, mSceneMgr(NULL)
, mAtmosphereDay(NULL)
, mAtmosphereNight(NULL)
, mCloudNode(NULL)
2015-04-25 18:37:42 +00:00
, mParticleNode(NULL)
, mRainTimer(0)
, mStormDirection(0,-1,0)
, mClouds()
, mNextClouds()
, mCloudBlendFactor(0.0f)
, mCloudOpacity(0.0f)
, mCloudSpeed(0.0f)
, mStarsOpacity(0.0f)
, mLightning(NULL)
, mRemainingTransitionTime(0.0f)
, mGlare(0.0f)
2015-04-25 18:37:42 +00:00
, mGlareFade(0.0f)
, mRainEnabled(false)
, mRainSpeed(0)
, mRainFrequency(1)
, mEnabled(true)
, mSunEnabled(true)
, mMasserEnabled(true)
, mSecundaEnabled(true)
{
2013-02-26 13:01:10 +00:00
mSceneMgr = root->getCreator();
mRootNode = mSceneMgr->getRootSceneNode()->createChildSceneNode();
}
2012-02-29 09:13:25 +00:00
void SkyManager::create()
{
2012-07-23 16:19:34 +00:00
assert(!mCreated);
2012-07-11 07:08:55 +00:00
sh::Factory::getInstance().setSharedParameter ("cloudBlendFactor",
sh::makeProperty<sh::FloatValue>(new sh::FloatValue(0)));
sh::Factory::getInstance().setSharedParameter ("cloudOpacity",
sh::makeProperty<sh::FloatValue>(new sh::FloatValue(1)));
sh::Factory::getInstance().setSharedParameter ("cloudColour",
sh::makeProperty<sh::Vector3>(new sh::Vector3(1,1,1)));
sh::Factory::getInstance().setSharedParameter ("cloudAnimationTimer",
sh::makeProperty<sh::FloatValue>(new sh::FloatValue(0)));
sh::Factory::getInstance().setSharedParameter ("nightFade",
sh::makeProperty<sh::FloatValue>(new sh::FloatValue(0)));
sh::Factory::getInstance().setSharedParameter ("atmosphereColour", sh::makeProperty<sh::Vector4>(new sh::Vector4(0,0,0,1)));
sh::Factory::getInstance().setSharedParameter ("horizonColour", sh::makeProperty<sh::Vector4>(new sh::Vector4(0,0,0,1)));
2012-07-11 07:08:55 +00:00
sh::Factory::getInstance().setTextureAlias ("cloud_texture_1", "");
sh::Factory::getInstance().setTextureAlias ("cloud_texture_2", "");
2012-07-23 16:19:34 +00:00
// Create light used for thunderstorm
2012-07-13 07:13:12 +00:00
mLightning = mSceneMgr->createLight();
mLightning->setType (Ogre::Light::LT_DIRECTIONAL);
mLightning->setDirection (Ogre::Vector3(0.3f, -0.7f, 0.3f));
2012-07-13 07:13:12 +00:00
mLightning->setVisible (false);
mLightning->setDiffuseColour (ColourValue(3,3,3));
2013-03-18 07:29:40 +00:00
const MWWorld::Fallback* fallback=MWBase::Environment::get().getWorld()->getFallback();
mSecunda = new Moon("secunda_texture", fallback->getFallbackFloat("Moons_Secunda_Size")/100, Vector3(-0.4f, 0.4f, 0.5f), mRootNode, "openmw_moon");
2012-02-25 15:36:45 +00:00
mSecunda->setType(Moon::Type_Secunda);
2012-04-04 16:53:40 +00:00
mSecunda->setRenderQueue(RQG_SkiesEarly+4);
2012-02-29 09:13:25 +00:00
mMasser = new Moon("masser_texture", fallback->getFallbackFloat("Moons_Masser_Size")/100, Vector3(-0.4f, 0.4f, 0.5f), mRootNode, "openmw_moon");
2012-04-04 16:53:40 +00:00
mMasser->setRenderQueue(RQG_SkiesEarly+3);
2012-02-25 15:36:45 +00:00
mMasser->setType(Moon::Type_Masser);
2012-02-29 09:13:25 +00:00
mSun = new BillboardObject("textures\\tx_sun_05.dds", 1, Vector3(0.4f, 0.4f, 0.4f), mRootNode, "openmw_sun");
2012-04-04 16:53:40 +00:00
mSun->setRenderQueue(RQG_SkiesEarly+4);
mSunGlare = new BillboardObject("textures\\tx_sun_flash_grey_05.dds", 3, Vector3(0.4f, 0.4f, 0.4f), mRootNode, "openmw_sun");
2012-04-04 16:53:40 +00:00
mSunGlare->setRenderQueue(RQG_SkiesLate);
2012-07-19 20:23:07 +00:00
mSunGlare->setVisibilityFlags(RV_NoReflection);
Ogre::AxisAlignedBox aabInf = Ogre::AxisAlignedBox::BOX_INFINITE;
// Stars
mAtmosphereNight = mRootNode->createChildSceneNode();
NifOgre::ObjectScenePtr objects;
if (Ogre::ResourceGroupManager::getSingleton().resourceExistsInAnyGroup("meshes\\sky_night_02.nif"))
objects = NifOgre::Loader::createObjects(mAtmosphereNight, "meshes\\sky_night_02.nif");
else
objects = NifOgre::Loader::createObjects(mAtmosphereNight, "meshes\\sky_night_01.nif");
for(size_t i = 0, matidx = 0;i < objects->mEntities.size();i++)
2012-03-13 18:57:33 +00:00
{
Entity* night1_ent = objects->mEntities[i];
night1_ent->setRenderQueueGroup(RQG_SkiesEarly+1);
night1_ent->setVisibilityFlags(RV_Sky);
night1_ent->setCastShadows(false);
night1_ent->getMesh()->_setBounds (aabInf);
2012-02-29 09:13:25 +00:00
for (unsigned int j=0; j<night1_ent->getNumSubEntities(); ++j)
{
std::string matName = "openmw_stars_" + boost::lexical_cast<std::string>(matidx++);
sh::MaterialInstance* m = sh::Factory::getInstance().createMaterialInstance(matName, "openmw_stars");
2012-07-11 07:08:55 +00:00
std::string textureName = sh::retrieveValue<sh::StringValue>(
sh::Factory::getInstance().getMaterialInstance(night1_ent->getSubEntity(j)->getMaterialName())->getProperty("diffuseMap"), NULL).get();
2012-02-29 09:13:25 +00:00
m->setProperty("texture", sh::makeProperty<sh::StringValue>(new sh::StringValue(textureName)));
night1_ent->getSubEntity(j)->setMaterialName(matName);
}
}
mObjects.push_back(objects);
// Atmosphere (day)
mAtmosphereDay = mRootNode->createChildSceneNode();
2013-04-07 08:52:35 +00:00
objects = NifOgre::Loader::createObjects(mAtmosphereDay, "meshes\\sky_atmosphere.nif");
for(size_t i = 0;i < objects->mEntities.size();i++)
{
Entity* atmosphere_ent = objects->mEntities[i];
atmosphere_ent->setCastShadows(false);
atmosphere_ent->setRenderQueueGroup(RQG_SkiesEarly);
atmosphere_ent->setVisibilityFlags(RV_Sky);
2013-01-10 14:35:06 +00:00
for(unsigned int j = 0;j < atmosphere_ent->getNumSubEntities();j++)
atmosphere_ent->getSubEntity (j)->setMaterialName("openmw_atmosphere");
// Using infinite AAB here to prevent being clipped by the custom near clip plane used for reflections/refractions
atmosphere_ent->getMesh()->_setBounds (aabInf);
}
mObjects.push_back(objects);
2012-02-29 09:13:25 +00:00
// Clouds
mCloudNode = mRootNode->createChildSceneNode();
objects = NifOgre::Loader::createObjects(mCloudNode, "meshes\\sky_clouds_01.nif");
for(size_t i = 0;i < objects->mEntities.size();i++)
{
Entity* clouds_ent = objects->mEntities[i];
clouds_ent->setVisibilityFlags(RV_Sky);
clouds_ent->setRenderQueueGroup(RQG_SkiesEarly+5);
2013-01-10 14:35:06 +00:00
for(unsigned int j = 0;j < clouds_ent->getNumSubEntities();j++)
clouds_ent->getSubEntity(j)->setMaterialName("openmw_clouds");
clouds_ent->setCastShadows(false);
// Using infinite AAB here to prevent being clipped by the custom near clip plane used for reflections/refractions
clouds_ent->getMesh()->_setBounds (aabInf);
}
mObjects.push_back(objects);
mCreated = true;
}
SkyManager::~SkyManager()
{
2014-06-25 16:20:21 +00:00
clearRain();
delete mSun;
delete mSunGlare;
delete mMasser;
delete mSecunda;
}
2012-02-21 16:38:15 +00:00
int SkyManager::getMasserPhase() const
{
if (!mCreated) return 0;
2012-02-21 16:38:15 +00:00
return mMasser->getPhaseInt();
}
int SkyManager::getSecundaPhase() const
{
if (!mCreated) return 0;
2012-02-21 16:38:15 +00:00
return mSecunda->getPhaseInt();
}
2014-06-25 16:20:21 +00:00
void SkyManager::clearRain()
{
for (std::map<Ogre::SceneNode*, NifOgre::ObjectScenePtr>::iterator it = mRainModels.begin(); it != mRainModels.end();)
{
it->second.setNull();
mSceneMgr->destroySceneNode(it->first);
mRainModels.erase(it++);
}
}
void SkyManager::updateRain(float dt)
{
// Move existing rain
// Note: if rain gets disabled, we let the existing rain drops finish falling down.
float minHeight = 200;
for (std::map<Ogre::SceneNode*, NifOgre::ObjectScenePtr>::iterator it = mRainModels.begin(); it != mRainModels.end();)
{
Ogre::Vector3 pos = it->first->getPosition();
pos.z -= mRainSpeed * dt;
it->first->setPosition(pos);
if (pos.z < -minHeight
// Here we might want to add a "splash" effect later
|| MWBase::Environment::get().getWorld()->isUnderwater(
MWBase::Environment::get().getWorld()->getPlayerPtr().getCell(), it->first->_getDerivedPosition()))
2014-06-25 16:20:21 +00:00
{
it->second.setNull();
mSceneMgr->destroySceneNode(it->first);
mRainModels.erase(it++);
}
else
++it;
}
// Spawn new rain
float rainFrequency = mRainFrequency;
if (mRainEnabled)
{
mRainTimer += dt;
if (mRainTimer >= 1.f/rainFrequency)
{
mRainTimer = 0;
// TODO: handle rain settings from Morrowind.ini
2014-06-25 16:20:21 +00:00
const float rangeRandom = 100;
float xOffs = OEngine::Misc::Rng::rollProbability() * rangeRandom - (rangeRandom / 2);
float yOffs = OEngine::Misc::Rng::rollProbability() * rangeRandom - (rangeRandom / 2);
2014-06-25 16:20:21 +00:00
// Create a separate node to control the offset, since a node with setInheritOrientation(false) will still
// consider the orientation of the parent node for its position, just not for its orientation
2014-09-26 15:12:48 +00:00
float startHeight = 700;
Ogre::Vector3 worldPos = mParticleNode->_getDerivedPosition();
worldPos += Ogre::Vector3(xOffs, yOffs, startHeight);
if (MWBase::Environment::get().getWorld()->isUnderwater(
MWBase::Environment::get().getWorld()->getPlayerPtr().getCell(), worldPos))
return;
Ogre::SceneNode* offsetNode = mParticleNode->createChildSceneNode(Ogre::Vector3(xOffs,yOffs,startHeight));
2014-06-25 16:20:21 +00:00
// Spawn a new rain object for each instance.
// TODO: this is inefficient. We could try to use an Ogre::ParticleSystem instead, but then we would need to make assumptions
// about the rain meshes being Quads and their dimensions.
// Or we could clone meshes into one vertex buffer manually.
2014-06-25 16:20:21 +00:00
NifOgre::ObjectScenePtr objects = NifOgre::Loader::createObjects(offsetNode, mRainEffect);
2014-06-26 12:26:53 +00:00
for (unsigned int i=0; i<objects->mEntities.size(); ++i)
{
objects->mEntities[i]->setRenderQueueGroup(RQG_Alpha);
objects->mEntities[i]->setVisibilityFlags(RV_Sky);
}
for (unsigned int i=0; i<objects->mParticles.size(); ++i)
{
objects->mParticles[i]->setRenderQueueGroup(RQG_Alpha);
objects->mParticles[i]->setVisibilityFlags(RV_Sky);
}
2014-06-25 16:20:21 +00:00
mRainModels[offsetNode] = objects;
}
}
}
void SkyManager::update(float duration)
2012-02-26 17:21:11 +00:00
{
if (!mEnabled) return;
2013-03-18 07:29:40 +00:00
const MWWorld::Fallback* fallback=MWBase::Environment::get().getWorld()->getFallback();
2012-08-15 11:17:35 +00:00
if (!mParticle.isNull())
{
for (unsigned int i=0; i<mParticle->mControllers.size(); ++i)
mParticle->mControllers[i].update();
for (unsigned int i=0; i<mParticle->mParticles.size(); ++i)
{
Ogre::ParticleSystem* psys = mParticle->mParticles[i];
Ogre::ParticleIterator pi = psys->_getIterator();
while (!pi.end())
{
Ogre::Particle *p = pi.getNext();
#if OGRE_VERSION >= (1 << 16 | 10 << 8 | 0)
Ogre::Vector3 pos = p->mPosition;
Ogre::Real& timeToLive = p->mTimeToLive;
#else
Ogre::Vector3 pos = p->position;
Ogre::Real& timeToLive = p->timeToLive;
#endif
if (psys->getKeepParticlesInLocalSpace() && psys->getParentNode())
pos = psys->getParentNode()->convertLocalToWorldPosition(pos);
if (MWBase::Environment::get().getWorld()->isUnderwater(
MWBase::Environment::get().getWorld()->getPlayerPtr().getCell(), pos))
timeToLive = 0;
}
}
if (mIsStorm)
mParticleNode->setOrientation(Ogre::Vector3::UNIT_Y.getRotationTo(mStormDirection));
}
if (mIsStorm)
mCloudNode->setOrientation(Ogre::Vector3::UNIT_Y.getRotationTo(mStormDirection));
else
mCloudNode->setOrientation(Ogre::Quaternion::IDENTITY);
2014-06-25 16:20:21 +00:00
updateRain(duration);
// UV Scroll the clouds
mCloudAnimationTimer += duration * mCloudSpeed;
2012-07-11 07:08:55 +00:00
sh::Factory::getInstance().setSharedParameter ("cloudAnimationTimer",
sh::makeProperty<sh::FloatValue>(new sh::FloatValue(mCloudAnimationTimer)));
2012-02-29 09:13:25 +00:00
2012-02-26 19:46:09 +00:00
/// \todo improve this
mMasser->setPhase( static_cast<Moon::Phase>( (int) ((mDay % 32)/4.f)) );
mSecunda->setPhase ( static_cast<Moon::Phase>( (int) ((mDay % 32)/4.f)) );
2012-02-29 09:13:25 +00:00
2013-03-17 13:22:30 +00:00
mSecunda->setColour ( mMoonRed ? fallback->getFallbackColour("Moons_Script_Color") : ColourValue(1,1,1,1));
2012-07-13 07:13:12 +00:00
mMasser->setColour (ColourValue(1,1,1,1));
2012-02-26 19:46:09 +00:00
if (mSunEnabled)
{
2012-07-11 07:08:55 +00:00
// take 1/10 sec for fading the glare effect from invisible to full
if (mGlareFade > mGlare)
{
2012-07-11 07:08:55 +00:00
mGlareFade -= duration*10;
if (mGlareFade < mGlare) mGlareFade = mGlare;
}
else if (mGlareFade < mGlare)
{
2012-07-11 07:08:55 +00:00
mGlareFade += duration*10;
if (mGlareFade > mGlare) mGlareFade = mGlare;
}
// increase the strength of the sun glare effect depending
// on how directly the player is looking at the sun
2012-02-26 19:46:09 +00:00
Vector3 sun = mSunGlare->getPosition();
Vector3 cam = mCamera->getRealDirection();
2012-02-26 19:46:09 +00:00
const Degree angle = sun.angleBetween( cam );
float val = 1- (angle.valueDegrees() / 180.f);
val = (val*val*val*val)*6;
mSunGlare->setSize(val * mGlareFade);
2012-02-26 19:46:09 +00:00
}
2012-02-29 09:13:25 +00:00
mSunGlare->setVisible(mSunEnabled);
2012-02-26 17:21:11 +00:00
mSun->setVisible(mSunEnabled);
mMasser->setVisible(mMasserEnabled);
mSecunda->setVisible(mSecundaEnabled);
2012-03-15 18:49:15 +00:00
2012-03-16 18:02:33 +00:00
// rotate the stars by 360 degrees every 4 days
mAtmosphereNight->roll(Degree(MWBase::Environment::get().getWorld()->getTimeScaleFactor()*duration*360 / (3600*96.f)));
}
void SkyManager::enable()
{
if (!mCreated)
create();
2014-06-25 16:20:21 +00:00
if (mParticleNode)
mParticleNode->setVisible(true);
mRootNode->setVisible(true);
mEnabled = true;
}
void SkyManager::disable()
{
2014-06-25 16:20:21 +00:00
if (mParticleNode)
mParticleNode->setVisible(false);
clearRain();
mRootNode->setVisible(false);
2014-06-25 16:20:21 +00:00
mEnabled = false;
}
void SkyManager::setMoonColour (bool red)
{
2012-07-13 07:13:12 +00:00
mMoonRed = red;
2012-02-21 21:11:41 +00:00
}
void SkyManager::setWeather(const MWWorld::WeatherResult& weather)
{
if (!mCreated) return;
2012-07-06 08:31:48 +00:00
2014-06-25 16:20:21 +00:00
mRainEffect = weather.mRainEffect;
mRainEnabled = !mRainEffect.empty();
mRainFrequency = weather.mRainFrequency;
mRainSpeed = weather.mRainSpeed;
mIsStorm = weather.mIsStorm;
2014-06-25 16:20:21 +00:00
if (mCurrentParticleEffect != weather.mParticleEffect)
{
mCurrentParticleEffect = weather.mParticleEffect;
if (mCurrentParticleEffect.empty())
{
mParticle.setNull();
}
else
{
mParticle = NifOgre::Loader::createObjects(mParticleNode, mCurrentParticleEffect);
for(size_t i = 0; i < mParticle->mParticles.size(); ++i)
{
ParticleSystem* particle = mParticle->mParticles[i];
particle->setRenderQueueGroup(RQG_Alpha);
particle->setVisibilityFlags(RV_Sky);
}
for (size_t i = 0; i < mParticle->mControllers.size(); ++i)
{
if (mParticle->mControllers[i].getSource().isNull())
mParticle->mControllers[i].setSource(Ogre::ControllerManager::getSingleton().getFrameTimeSource());
}
}
}
if (mClouds != weather.mCloudTexture)
{
sh::Factory::getInstance().setTextureAlias ("cloud_texture_1", Misc::ResourceHelpers::correctTexturePath(weather.mCloudTexture));
mClouds = weather.mCloudTexture;
}
2012-02-29 09:13:25 +00:00
if (mNextClouds != weather.mNextCloudTexture)
{
sh::Factory::getInstance().setTextureAlias ("cloud_texture_2", Misc::ResourceHelpers::correctTexturePath(weather.mNextCloudTexture));
mNextClouds = weather.mNextCloudTexture;
}
2012-02-29 09:13:25 +00:00
if (mCloudBlendFactor != weather.mCloudBlendFactor)
{
mCloudBlendFactor = weather.mCloudBlendFactor;
2012-07-11 07:08:55 +00:00
sh::Factory::getInstance().setSharedParameter ("cloudBlendFactor",
sh::makeProperty<sh::FloatValue>(new sh::FloatValue(weather.mCloudBlendFactor)));
}
2012-02-29 09:13:25 +00:00
if (mCloudOpacity != weather.mCloudOpacity)
{
mCloudOpacity = weather.mCloudOpacity;
2012-07-11 07:08:55 +00:00
sh::Factory::getInstance().setSharedParameter ("cloudOpacity",
sh::makeProperty<sh::FloatValue>(new sh::FloatValue(weather.mCloudOpacity)));
}
2012-02-29 09:13:25 +00:00
if (mCloudColour != weather.mSunColor)
{
ColourValue clr( weather.mSunColor.r*0.7f + weather.mAmbientColor.r*0.7f,
weather.mSunColor.g*0.7f + weather.mAmbientColor.g*0.7f,
weather.mSunColor.b*0.7f + weather.mAmbientColor.b*0.7f);
2012-02-29 09:13:25 +00:00
2012-07-11 07:08:55 +00:00
sh::Factory::getInstance().setSharedParameter ("cloudColour",
sh::makeProperty<sh::Vector3>(new sh::Vector3(clr.r, clr.g, clr.b)));
mCloudColour = weather.mSunColor;
}
2012-02-29 09:13:25 +00:00
if (mSkyColour != weather.mSkyColor)
2012-02-29 09:13:25 +00:00
{
mSkyColour = weather.mSkyColor;
2012-07-09 18:14:07 +00:00
sh::Factory::getInstance().setSharedParameter ("atmosphereColour", sh::makeProperty<sh::Vector4>(new sh::Vector4(
2012-07-11 07:08:55 +00:00
weather.mSkyColor.r, weather.mSkyColor.g, weather.mSkyColor.b, weather.mSkyColor.a)));
}
2012-02-29 09:13:25 +00:00
if (mFogColour != weather.mFogColor)
{
mFogColour = weather.mFogColor;
sh::Factory::getInstance().setSharedParameter ("horizonColour", sh::makeProperty<sh::Vector4>(new sh::Vector4(
weather.mFogColor.r, weather.mFogColor.g, weather.mFogColor.b, weather.mFogColor.a)));
}
2012-07-11 07:08:55 +00:00
mCloudSpeed = weather.mCloudSpeed;
2012-02-29 09:13:25 +00:00
if (weather.mNight && mStarsOpacity != weather.mNightFade)
{
2012-03-03 11:26:08 +00:00
if (weather.mNightFade == 0)
mAtmosphereNight->setVisible(false);
else
{
mAtmosphereNight->setVisible(true);
2012-07-11 07:08:55 +00:00
sh::Factory::getInstance().setSharedParameter ("nightFade",
sh::makeProperty<sh::FloatValue>(new sh::FloatValue(weather.mNightFade)));
2012-03-03 11:26:08 +00:00
mStarsOpacity = weather.mNightFade;
}
}
2012-02-26 19:46:09 +00:00
float strength;
float timeofday_angle = std::abs(mSunGlare->getPosition().z/mSunGlare->getPosition().length());
if (timeofday_angle <= 0.44)
strength = timeofday_angle/0.44f;
else
strength = 1.f;
2012-02-29 09:13:25 +00:00
mSunGlare->setVisibility(weather.mGlareView * mGlareFade * strength);
mSun->setVisibility(weather.mGlareView * strength);
2012-02-29 09:13:25 +00:00
mAtmosphereNight->setVisible(weather.mNight && mEnabled);
if (mParticle.get())
setAlpha(mParticle, weather.mEffectFade);
for (std::map<Ogre::SceneNode*, NifOgre::ObjectScenePtr>::iterator it = mRainModels.begin(); it != mRainModels.end(); ++it)
setAlpha(it->second, weather.mEffectFade);
}
void SkyManager::setGlare(const float glare)
{
mGlare = glare;
}
Vector3 SkyManager::getRealSunPos()
{
if (!mCreated) return Vector3(0,0,0);
return mSun->getNode()->getPosition() + mCamera->getRealPosition();
}
void SkyManager::sunEnable()
{
mSunEnabled = true;
}
void SkyManager::sunDisable()
{
mSunEnabled = false;
}
void SkyManager::setStormDirection(const Vector3 &direction)
{
mStormDirection = direction;
}
2015-02-07 01:21:40 +00:00
void SkyManager::setSunDirection(const Vector3& direction, bool is_night)
{
if (!mCreated) return;
mSun->setPosition(direction);
mSunGlare->setPosition(direction);
2012-07-19 20:23:07 +00:00
float height = direction.z;
float fade = is_night ? 0.0f : (( height > 0.5) ? 1.0f : height * 2);
2012-07-19 20:23:07 +00:00
sh::Factory::getInstance ().setSharedParameter ("waterSunFade_sunHeight", sh::makeProperty<sh::Vector2>(new sh::Vector2(fade, height)));
}
2012-02-25 12:46:17 +00:00
2012-02-25 15:36:45 +00:00
void SkyManager::setMasserDirection(const Vector3& direction)
{
if (!mCreated) return;
2012-02-25 15:36:45 +00:00
mMasser->setPosition(direction);
}
void SkyManager::setSecundaDirection(const Vector3& direction)
{
if (!mCreated) return;
2012-02-25 15:36:45 +00:00
mSecunda->setPosition(direction);
}
void SkyManager::masserEnable()
{
mMasserEnabled = true;
}
void SkyManager::secundaEnable()
{
mSecundaEnabled = true;
}
void SkyManager::masserDisable()
{
mMasserEnabled = false;
}
void SkyManager::secundaDisable()
{
mSecundaEnabled = false;
}
2012-07-13 07:13:12 +00:00
void SkyManager::setLightningStrength(const float factor)
2012-02-25 12:46:17 +00:00
{
if (!mCreated) return;
2012-02-25 12:46:17 +00:00
if (factor > 0.f)
{
2012-07-13 07:13:12 +00:00
mLightning->setDiffuseColour (ColourValue(2*factor, 2*factor, 2*factor));
mLightning->setVisible(true);
2012-02-25 12:46:17 +00:00
}
else
2012-07-13 07:13:12 +00:00
mLightning->setVisible(false);
}
2012-02-25 15:36:45 +00:00
void SkyManager::setMasserFade(const float fade)
{
2012-04-11 16:53:13 +00:00
if (!mCreated) return;
2012-02-25 15:36:45 +00:00
mMasser->setVisibility(fade);
}
void SkyManager::setSecundaFade(const float fade)
{
2012-04-11 16:53:13 +00:00
if (!mCreated) return;
2012-02-25 15:36:45 +00:00
mSecunda->setVisibility(fade);
}
void SkyManager::setHour(double hour)
{
mHour = static_cast<float>(hour);
}
void SkyManager::setDate(int day, int month)
{
mDay = day;
mMonth = month;
}
Ogre::SceneNode* SkyManager::getSunNode()
{
if (!mCreated) return 0;
return mSun->getNode();
}
2012-04-05 13:30:55 +00:00
2012-07-19 20:23:07 +00:00
void SkyManager::setGlareEnabled (bool enabled)
{
if (!mCreated || !mEnabled)
2012-07-19 20:23:07 +00:00
return;
mSunGlare->setVisible (mSunEnabled && enabled);
}
void SkyManager::attachToNode(SceneNode *sceneNode)
{
if (!mParticleNode)
{
mParticleNode = sceneNode->createChildSceneNode();
mParticleNode->setInheritOrientation(false);
}
else
{
sceneNode->addChild(mParticleNode);
}
}