From b97b3d7b71b07538657edd924976e5b294c4bf01 Mon Sep 17 00:00:00 2001
From: scrawl <scrawl@baseoftrash.de>
Date: Wed, 15 Aug 2012 17:56:24 +0200
Subject: [PATCH] fix sky position when reflection is enabled, fix delay in sky
 reflection

---
 apps/openmw/mwrender/sky.cpp   | 10 ++++++----
 apps/openmw/mwrender/water.cpp |  4 +---
 2 files changed, 7 insertions(+), 7 deletions(-)

diff --git a/apps/openmw/mwrender/sky.cpp b/apps/openmw/mwrender/sky.cpp
index 3eb42a05e..eba605d0c 100644
--- a/apps/openmw/mwrender/sky.cpp
+++ b/apps/openmw/mwrender/sky.cpp
@@ -280,7 +280,7 @@ SkyManager::SkyManager (SceneNode* pMwRoot, Camera* pCamera)
     , mMoonRed(false)
 {
     mSceneMgr = pMwRoot->getCreator();
-    mRootNode = mCamera->getParentSceneNode()->createChildSceneNode();
+    mRootNode = mSceneMgr->getRootSceneNode()->createChildSceneNode();
     mRootNode->pitch(Degree(-90)); // convert MW to ogre coordinates
     mRootNode->setInheritOrientation(false);
 }
@@ -405,7 +405,8 @@ void SkyManager::update(float duration)
 {
     if (!mEnabled) return;
 
-    mRootNode->setPosition(mCamera->getPosition());
+    mCamera->getParentSceneNode ()->needUpdate ();
+    mRootNode->setPosition(mCamera->getDerivedPosition());
 
     // UV Scroll the clouds
     mCloudAnimationTimer += duration * mCloudSpeed * (MWBase::Environment::get().getWorld()->getTimeScaleFactor()/30.f);
@@ -668,12 +669,13 @@ Ogre::SceneNode* SkyManager::getSunNode()
 
 void SkyManager::setSkyPosition(const Ogre::Vector3& position)
 {
-    mRootNode->_setDerivedPosition(position);
+    mRootNode->setPosition(position);
 }
 
 void SkyManager::resetSkyPosition()
 {
-    mRootNode->setPosition(0,0,0);
+    mCamera->getParentSceneNode ()->needUpdate ();
+    mRootNode->setPosition(mCamera->getDerivedPosition());
 }
 
 void SkyManager::scaleSky(float scale)
diff --git a/apps/openmw/mwrender/water.cpp b/apps/openmw/mwrender/water.cpp
index d5b93b7cb..46678f2bc 100644
--- a/apps/openmw/mwrender/water.cpp
+++ b/apps/openmw/mwrender/water.cpp
@@ -207,6 +207,7 @@ void Water::preRenderTargetUpdate(const RenderTargetEvent& evt)
 {
     if (evt.source == mReflectionTarget)
     {
+        mCamera->getParentSceneNode ()->needUpdate ();
         mReflectionCamera->setOrientation(mCamera->getDerivedOrientation());
         mReflectionCamera->setPosition(mCamera->getDerivedPosition());
         mReflectionCamera->setNearClipDistance(mCamera->getNearClipDistance());
@@ -215,11 +216,9 @@ void Water::preRenderTargetUpdate(const RenderTargetEvent& evt)
         mReflectionCamera->setFOVy(mCamera->getFOVy());
         mReflectionRenderActive = true;
 
-        /// \todo the reflection render (and probably all renderingmanager-updates) lag behind 1 camera frame for some reason
         Vector3 pos = mCamera->getRealPosition();
         pos.y = mTop*2 - pos.y;
         mSky->setSkyPosition(pos);
-        mSky->scaleSky(mCamera->getFarClipDistance() / 50.f);
         mReflectionCamera->enableReflection(mWaterPlane);
     }
 }
@@ -229,7 +228,6 @@ void Water::postRenderTargetUpdate(const RenderTargetEvent& evt)
     if (evt.source == mReflectionTarget)
     {
         mSky->resetSkyPosition();
-        mSky->scaleSky(1);
         mReflectionCamera->disableReflection();
         mReflectionCamera->disableCustomNearClipPlane();
         mReflectionRenderActive = false;