From aad8f6605e2f6f9bac0fb87aa8599989d5705305 Mon Sep 17 00:00:00 2001 From: scrawl Date: Fri, 24 Feb 2012 16:12:43 +0100 Subject: [PATCH] send a raycast through the physics engine to check if sun is visible --- apps/openmw/mwrender/renderingmanager.cpp | 5 +++++ apps/openmw/mwrender/renderingmanager.hpp | 1 + apps/openmw/mwrender/sky.cpp | 21 +++++++++++++++++++-- apps/openmw/mwrender/sky.hpp | 7 +++++++ apps/openmw/mwworld/physicssystem.cpp | 11 +++++++++++ apps/openmw/mwworld/physicssystem.hpp | 8 ++++++-- apps/openmw/mwworld/world.cpp | 13 +++++++++++++ 7 files changed, 62 insertions(+), 4 deletions(-) diff --git a/apps/openmw/mwrender/renderingmanager.cpp b/apps/openmw/mwrender/renderingmanager.cpp index 513f009ef..f26acb019 100644 --- a/apps/openmw/mwrender/renderingmanager.cpp +++ b/apps/openmw/mwrender/renderingmanager.cpp @@ -305,4 +305,9 @@ void RenderingManager::setSunDirection(const Ogre::Vector3& direction) if (mSun) mSun->setPosition(direction); } +void RenderingManager::setGlare(bool glare) +{ + mSkyManager->setGlare(glare); +} + } // namespace diff --git a/apps/openmw/mwrender/renderingmanager.hpp b/apps/openmw/mwrender/renderingmanager.hpp index a4449145c..fb7e19997 100644 --- a/apps/openmw/mwrender/renderingmanager.hpp +++ b/apps/openmw/mwrender/renderingmanager.hpp @@ -93,6 +93,7 @@ class RenderingManager: private RenderingInterface { void sunEnable(); void sunDisable(); + void setGlare(bool glare); void skyEnable (); void skyDisable (); void skySetHour (double hour); diff --git a/apps/openmw/mwrender/sky.cpp b/apps/openmw/mwrender/sky.cpp index 959b6880a..a4d02212c 100644 --- a/apps/openmw/mwrender/sky.cpp +++ b/apps/openmw/mwrender/sky.cpp @@ -54,6 +54,11 @@ void BillboardObject::setRenderQueue(unsigned int id) mBBSet->setRenderQueueGroup(id); } +SceneNode* BillboardObject::getNode() +{ + return mNode; +} + void BillboardObject::init(const String& textureName, const unsigned int initialSize, const Vector3& position, @@ -276,8 +281,8 @@ SkyManager::SkyManager (SceneNode* pMwRoot, Camera* pCamera) mViewport->setBackgroundColour(ColourValue(0.87, 0.87, 0.87)); - mSun = new BillboardObject("textures\\tx_sun_05.dds", 1, Vector3(0.4, 0.4, 1.0), mRootNode); - mSunGlare = new BillboardObject("textures\\tx_sun_flash_grey_05.dds", 3, Vector3(0.4, 0.4, 1.0), mRootNode); + mSun = new BillboardObject("textures\\tx_sun_05.dds", 1, Vector3(0.4, 0.4, 0.4), mRootNode); + mSunGlare = new BillboardObject("textures\\tx_sun_flash_grey_05.dds", 3, Vector3(0.4, 0.4, 0.4), mRootNode); mSunGlare->setRenderQueue(RENDER_QUEUE_SKIES_LATE); mMasser = new Moon("textures\\tx_masser_full.dds", 1, Vector3(-0.4, -0.4, 0.5), mRootNode); mSecunda = new Moon("textures\\tx_secunda_full.dds", 0.5, Vector3(0.4, -0.4, 0.5), mRootNode); @@ -442,11 +447,13 @@ void SkyManager::update(float duration) void SkyManager::enable() { mRootNode->setVisible(true); + mEnabled = true; } void SkyManager::disable() { mRootNode->setVisible(false); + mEnabled = false; } void SkyManager::setMoonColour (bool red) @@ -508,3 +515,13 @@ void SkyManager::setWeather(const MWWorld::WeatherResult& weather) mViewport->setBackgroundColour(weather.mFogColor); } + +void SkyManager::setGlare(bool glare) +{ + mSunGlare->setVisible(glare && mEnabled); +} + +Vector3 SkyManager::getRealSunPos() +{ + return mSun->getNode()->_getDerivedPosition(); +} diff --git a/apps/openmw/mwrender/sky.hpp b/apps/openmw/mwrender/sky.hpp index 61439f56b..43b922903 100644 --- a/apps/openmw/mwrender/sky.hpp +++ b/apps/openmw/mwrender/sky.hpp @@ -38,6 +38,8 @@ namespace MWRender void setVisible(const bool visible); void setRenderQueue(unsigned int id); + Ogre::SceneNode* getNode(); + protected: virtual void init(const Ogre::String& textureName, const unsigned int size, @@ -128,6 +130,9 @@ namespace MWRender void setWeather(const MWWorld::WeatherResult& weather); + void setGlare(bool glare); + Ogre::Vector3 getRealSunPos(); + private: BillboardObject* mSun; BillboardObject* mSunGlare; @@ -156,6 +161,8 @@ namespace MWRender float mRemainingTransitionTime; void ModVertexAlpha(Ogre::Entity* ent, unsigned int meshType); + + bool mEnabled; }; } diff --git a/apps/openmw/mwworld/physicssystem.cpp b/apps/openmw/mwworld/physicssystem.cpp index f32cf9703..b0da4524e 100644 --- a/apps/openmw/mwworld/physicssystem.cpp +++ b/apps/openmw/mwworld/physicssystem.cpp @@ -50,6 +50,17 @@ namespace MWWorld return mEngine->rayTest(from,to); } + + bool PhysicsSystem::castRay(const Vector3& from, const Vector3& to) + { + btVector3 _from, _to; + _from = btVector3(from.x, from.y, from.z); + _to = btVector3(to.x, to.y, to.z); + + std::pair result = mEngine->rayTest(_from, _to); + + return !(result.first == ""); + } std::vector< std::pair > PhysicsSystem::doPhysics (float duration, diff --git a/apps/openmw/mwworld/physicssystem.hpp b/apps/openmw/mwworld/physicssystem.hpp index e534ee208..78cbde083 100644 --- a/apps/openmw/mwworld/physicssystem.hpp +++ b/apps/openmw/mwworld/physicssystem.hpp @@ -33,11 +33,15 @@ namespace MWWorld void scaleObject (const std::string& handle, float scale); bool toggleCollisionMode(); - std::pair getFacedHandle (MWWorld::World& world); + + std::pair getFacedHandle (MWWorld::World& world); + + // cast ray, return true if it hit something + bool castRay(const Ogre::Vector3& from, const Ogre::Vector3& to); void insertObjectPhysics(const MWWorld::Ptr& ptr, std::string model); - void insertActorPhysics(const MWWorld::Ptr&, std::string model); + void insertActorPhysics(const MWWorld::Ptr&, std::string model); OEngine::Physic::PhysicEngine* getEngine(); diff --git a/apps/openmw/mwworld/world.cpp b/apps/openmw/mwworld/world.cpp index 3b6782347..a631a14f9 100644 --- a/apps/openmw/mwworld/world.cpp +++ b/apps/openmw/mwworld/world.cpp @@ -24,6 +24,9 @@ #include "globals.hpp" #include "cellfunctors.hpp" +#include +using namespace Ogre; + namespace { template @@ -413,6 +416,8 @@ namespace MWWorld mRendering->skySetDate (day, month); mWeatherManager->setDate (day, month); + + } void World::setMonth (int month) @@ -699,6 +704,14 @@ namespace MWWorld mWorldScene->update (duration); mWeatherManager->update (duration); + + // cast a ray from player to sun to detect if the sun is visible + // this is temporary until we find a better place to put this code + // currently its here because we need to access the physics system + float* p = mPlayer->getPlayer().getRefData().getPosition().pos; + Vector3 sun = mRendering->getSkyManager()->getRealSunPos(); + sun = Vector3(sun.x, -sun.z, sun.y); + mRendering->getSkyManager()->setGlare(!mPhysics->castRay(Ogre::Vector3(p[0], p[1], p[2]), sun)); } bool World::isCellExterior() const