From 8114126a62ecd4904f3c92fd0d5667d874700a2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20=C4=8C=C3=AD=C5=BE?= Date: Thu, 5 Oct 2017 12:58:56 +0200 Subject: [PATCH] make use of renderinfo --- apps/openmw/mwrender/renderingmanager.cpp | 2 +- apps/openmw/mwrender/sky.cpp | 63 ++++++++++++++--------- apps/openmw/mwrender/sky.hpp | 7 +-- 3 files changed, 41 insertions(+), 31 deletions(-) diff --git a/apps/openmw/mwrender/renderingmanager.cpp b/apps/openmw/mwrender/renderingmanager.cpp index 37abb1ec3..3826fd5f6 100644 --- a/apps/openmw/mwrender/renderingmanager.cpp +++ b/apps/openmw/mwrender/renderingmanager.cpp @@ -255,7 +255,7 @@ namespace MWRender mRootNode->getOrCreateStateSet()->addUniform(mUniformRainIntensity); - mSky.reset(new SkyManager(sceneRoot, resourceSystem->getSceneManager(), this)); + mSky.reset(new SkyManager(sceneRoot, resourceSystem->getSceneManager())); source->setStateSetModes(*mRootNode->getOrCreateStateSet(), osg::StateAttribute::ON); diff --git a/apps/openmw/mwrender/sky.cpp b/apps/openmw/mwrender/sky.cpp index cce2e1b1d..81494d8e2 100644 --- a/apps/openmw/mwrender/sky.cpp +++ b/apps/openmw/mwrender/sky.cpp @@ -1094,9 +1094,8 @@ private: } }; -SkyManager::SkyManager(osg::Group* parentNode, Resource::SceneManager* sceneManager, MWRender::RenderingManager *renderingManager) +SkyManager::SkyManager(osg::Group* parentNode, Resource::SceneManager* sceneManager) : mSceneManager(sceneManager) - , mRendering(renderingManager) , mAtmosphereNightRoll(0.f) , mCreated(false) , mIsStorm(false) @@ -1344,40 +1343,50 @@ protected: class WeatherParticleDrawCallback : public osg::Drawable::DrawCallback { public: - WeatherParticleDrawCallback(MWRender::RenderingManager *renderingManager, float rangeX, float rangeY) : osg::Drawable::DrawCallback() + WeatherParticleDrawCallback(float rangeX, float rangeY) : osg::Drawable::DrawCallback() { - mRendering = renderingManager; mRangeX = rangeX; mRangeY = rangeY; } virtual void drawImplementation(osg::RenderInfo& renderInfo, const osg::Drawable *drawable) const { - osg::Vec3 cameraPos = mRendering->getCameraPosition(); + osgParticle::ParticleSystem *ps = (osgParticle::ParticleSystem *) drawable; + osg::Vec3 cameraPos = renderInfo.getCurrentCamera()->getInverseViewMatrix().getTrans(); + osg::Vec3 cameraOffset = osg::Vec3( - mRangeX - fmod(cameraPos.x(), mRangeX / 2), - mRangeY - fmod(cameraPos.y(), mRangeY / 2), - 0); + fmod(cameraPos.x(), mRangeX), + fmod(cameraPos.y(), mRangeY), + 0.0); - osgParticle::ParticleSystem *ps = (osgParticle::ParticleSystem *) drawable; + std::vector positionBackups; - for (int xOff = 0; xOff < 3; xOff++) - for (int yOff = 0; yOff < 3; yOff++) - { - osg::Vec3 offset = cameraOffset + osg::Vec3(-1 * xOff * mRangeX, -1 * yOff * mRangeY,0); + for (int i = 0; i < ps->numParticles(); i++) + { + osgParticle::Particle *particle = ps->getParticle(i); - for(int i = 0; i < ps->numParticles(); i++) - ps->getParticle(i)->setPosition(ps->getParticle(i)->getPosition() + offset); + positionBackups.push_back(particle->getPosition()); - ps->drawImplementation(renderInfo); + particle->setPosition(particle->getPosition() - cameraOffset); - for(int i = 0; i < ps->numParticles(); i++) - ps->getParticle(i)->setPosition(ps->getParticle(i)->getPosition() - offset); - } + if (particle->getPosition().x() > mRangeX / 2.0) // wrap-around effect + particle->setPosition(particle->getPosition() - osg::Vec3(mRangeX,0,0)); + else if (particle->getPosition().x() < -mRangeX / 2.0) + particle->setPosition(particle->getPosition() + osg::Vec3(mRangeX,0,0)); + + if (particle->getPosition().y() > mRangeY / 2.0) + particle->setPosition(particle->getPosition() - osg::Vec3(0,mRangeY,0)); + else if (particle->getPosition().y() < -mRangeY / 2.0) + particle->setPosition(particle->getPosition() + osg::Vec3(0,mRangeY,0)); + } + + ps->drawImplementation(renderInfo); + + for (int i = 0; i < ps->numParticles(); i++) // restore positions + ps->getParticle(i)->setPosition(positionBackups[i]); } protected: - MWRender::RenderingManager *mRendering; float mRangeX, mRangeY; }; @@ -1389,11 +1398,12 @@ void SkyManager::createRain() mRainNode = new osg::Group; mRainParticleSystem = new osgParticle::ParticleSystem; - mRainParticleSystem->setDrawCallback(new WeatherParticleDrawCallback(mRendering,RAIN_WIDTH,RAIN_WIDTH)); + mRainParticleSystem->setDrawCallback(new WeatherParticleDrawCallback(RAIN_WIDTH,RAIN_WIDTH)); mRainParticleSystem->setParticleAlignment(osgParticle::ParticleSystem::FIXED); mRainParticleSystem->setAlignVectorX(osg::Vec3f(0.1,0,0)); mRainParticleSystem->setAlignVectorY(osg::Vec3f(0,0,1)); + mRainParticleSystem->setCullingActive(false); osg::ref_ptr stateset (mRainParticleSystem->getOrCreateStateSet()); @@ -1422,7 +1432,8 @@ void SkyManager::createRain() emitter->setPlacer(placer); osg::ref_ptr counter (new osgParticle::ConstantRateCounter); - counter->setNumberOfParticlesPerSecondToCreate(600.0); +// counter->setNumberOfParticlesPerSecondToCreate(600.0); + counter->setNumberOfParticlesPerSecondToCreate(2000); emitter->setCounter(counter); osg::ref_ptr shooter (new RainShooter); @@ -1496,6 +1507,9 @@ void SkyManager::update(float duration) osg::Quat quat; quat.makeRotate(osg::Vec3f(0,1,0), mStormDirection); + if (mParticleNode) + mParticleNode->setAttitude(quat); + mCloudNode->setAttitude(quat); } else @@ -1587,7 +1601,7 @@ void SkyManager::setWeather(const WeatherResult& weather) { if (!mParticleNode) { - mParticleNode = new osg::Group; + mParticleNode = new osg::PositionAttitudeTransform; mParticleNode->addCullCallback(mUnderwaterSwitch); mParticleNode->setNodeMask(Mask_WeatherParticles); mRootNode->addChild(mParticleNode); @@ -1621,12 +1635,11 @@ void SkyManager::setWeather(const WeatherResult& weather) } } - SceneUtil::FindByClassVisitor findPSVisitor(std::string("ParticleSystem")); mParticleEffect->accept(findPSVisitor); if (findPSVisitor.mFoundNode) - ((osgParticle::ParticleSystem *) findPSVisitor.mFoundNode)->setDrawCallback(new WeatherParticleDrawCallback(mRendering,rangeX,rangeY)); + ((osgParticle::ParticleSystem *) findPSVisitor.mFoundNode)->setDrawCallback(new WeatherParticleDrawCallback(rangeX,rangeY)); } } diff --git a/apps/openmw/mwrender/sky.hpp b/apps/openmw/mwrender/sky.hpp index fdcd27e5a..59a8ddc4e 100644 --- a/apps/openmw/mwrender/sky.hpp +++ b/apps/openmw/mwrender/sky.hpp @@ -9,8 +9,6 @@ #include #include -#include - namespace osg { class Group; @@ -110,7 +108,7 @@ namespace MWRender class SkyManager { public: - SkyManager(osg::Group* parentNode, Resource::SceneManager* sceneManager, MWRender::RenderingManager *renderingManager); + SkyManager(osg::Group* parentNode, Resource::SceneManager* sceneManager); ~SkyManager(); void update(float duration); @@ -172,12 +170,11 @@ namespace MWRender void updateRainParameters(); Resource::SceneManager* mSceneManager; - MWRender::RenderingManager *mRendering; osg::ref_ptr mRootNode; osg::ref_ptr mEarlyRenderBinRoot; - osg::ref_ptr mParticleNode; + osg::ref_ptr mParticleNode; osg::ref_ptr mParticleEffect; std::vector > mParticleFaders; osg::ref_ptr mUnderwaterSwitch;