From 65977b910e017bbe4007fc1c71d47201c3bb02f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20=C4=8C=C3=AD=C5=BE?= Date: Thu, 12 Oct 2017 15:28:54 +0200 Subject: [PATCH] wrap weather around in all directions --- apps/openmw/mwrender/sky.cpp | 92 +++++++++++++++++----------------- components/nifosg/particle.cpp | 5 -- components/nifosg/particle.hpp | 1 - 3 files changed, 47 insertions(+), 51 deletions(-) diff --git a/apps/openmw/mwrender/sky.cpp b/apps/openmw/mwrender/sky.cpp index fb3e477bb..eaa93ab88 100644 --- a/apps/openmw/mwrender/sky.cpp +++ b/apps/openmw/mwrender/sky.cpp @@ -18,6 +18,8 @@ #include #include +#include + #include #include #include @@ -39,7 +41,6 @@ #include #include #include -#include #include "../mwbase/environment.hpp" #include "../mwbase/world.hpp" @@ -47,12 +48,8 @@ #include "vismask.hpp" #include "renderbin.hpp" -#define RAIN_WIDTH 600.0 -#define RAIN_HEIGHT 600.0 - namespace { - osg::ref_ptr createAlphaTrackingUnlitMaterial() { osg::ref_ptr mat = new osg::Material; @@ -1344,60 +1341,69 @@ protected: class WeatherParticleDrawCallback : public osg::Drawable::DrawCallback { public: - WeatherParticleDrawCallback(osg::Vec3 wrapRange) : osg::Drawable::DrawCallback() + WeatherParticleDrawCallback(osg::Vec3 &wrapRange) : osg::Drawable::DrawCallback() { mWrapRange = wrapRange; } virtual void drawImplementation(osg::RenderInfo& renderInfo, const osg::Drawable *drawable) const { + osgParticle::ParticleSystem *ps = (osgParticle::ParticleSystem *) drawable; + + osgParticle::ParticleSystem::ScopedReadLock *lock = new osgParticle::ParticleSystem::ScopedReadLock(*ps->getReadWriteMutex()); + osg::Vec3 cameraPos = renderInfo.getCurrentCamera()->getInverseViewMatrix().getTrans(); osg::Vec3 cameraOffset = osg::Vec3( fmod(cameraPos.x(), mWrapRange.x()), fmod(cameraPos.y(), mWrapRange.y()), - 0.0); + fmod(cameraPos.z(), mWrapRange.z())); std::vector positionBackups; osg::Matrix toWorld, toLocal; - toWorld.makeIdentity(); - toLocal.makeIdentity(); - std::vector worldMatrices = drawable->getWorldMatrices(); if (!worldMatrices.empty()) - { - toWorld = worldMatrices[0]; - toLocal.invert(toWorld); - } + { + toWorld = worldMatrices[0]; + toLocal.invert(toWorld); + } for (int i = 0; i < ps->numParticles(); i++) - { - osgParticle::Particle *particle = ps->getParticle(i); - positionBackups.push_back(particle->getPosition()); - particle->setPosition(toWorld.preMult(particle->getPosition())); - particle->setPosition(particle->getPosition() - cameraOffset); - - if (particle->getPosition().x() > mWrapRange.x() / 2.0) // wrap-around effect - particle->setPosition(particle->getPosition() - osg::Vec3(mWrapRange.x(),0,0)); - else if (particle->getPosition().x() < -mWrapRange.x() / 2.0) - particle->setPosition(particle->getPosition() + osg::Vec3(mWrapRange.x(),0,0)); - - if (particle->getPosition().y() > mWrapRange.y() / 2.0) - particle->setPosition(particle->getPosition() - osg::Vec3(0,mWrapRange.y(),0)); - else if (particle->getPosition().y() < -mWrapRange.y() / 2.0) - particle->setPosition(particle->getPosition() + osg::Vec3(0,mWrapRange.y(),0)); - - particle->setPosition(toLocal.preMult(particle->getPosition())); + { + osgParticle::Particle *particle = ps->getParticle(i); + positionBackups.push_back(particle->getPosition()); + particle->setPosition(toWorld.preMult(particle->getPosition())); + particle->setPosition(particle->getPosition() - cameraOffset); + + for (int j = 0; j < 3; j++) // wrap-around effect in all 3 directions + { + osg::Vec3 newPosition = particle->getPosition(); + + if (particle->getPosition()[j] > mWrapRange[j] / 2.0) + newPosition[j] -= mWrapRange[j]; + else if (particle->getPosition()[j] < -mWrapRange[j] / 2.0) + newPosition[j] += mWrapRange[j]; + + particle->setPosition(newPosition); } + particle->setPosition(toLocal.preMult(particle->getPosition())); + } + + delete lock; // unlock the mutex as ps will try to lock it in the following command + ps->drawImplementation(renderInfo); + lock = new osgParticle::ParticleSystem::ScopedReadLock(*ps->getReadWriteMutex()); + for (int i = 0; i < ps->numParticles(); i++) // restore positions ps->getParticle(i)->setPosition(positionBackups[i]); + + delete lock; } protected: @@ -1412,7 +1418,8 @@ void SkyManager::createRain() mRainNode = new osg::Group; mRainParticleSystem = new osgParticle::ParticleSystem; - mRainParticleSystem->setDrawCallback(new WeatherParticleDrawCallback(osg::Vec3(RAIN_WIDTH,RAIN_WIDTH,RAIN_HEIGHT))); + osg::Vec3 rainRange = osg::Vec3(600,600,600); + mRainParticleSystem->setDrawCallback(new WeatherParticleDrawCallback(rainRange)); mRainParticleSystem->setParticleAlignment(osgParticle::ParticleSystem::FIXED); mRainParticleSystem->setAlignVectorX(osg::Vec3f(0.1,0,0)); @@ -1440,8 +1447,8 @@ void SkyManager::createRain() emitter->setParticleSystem(mRainParticleSystem); osg::ref_ptr placer (new osgParticle::BoxPlacer); - placer->setXRange(-RAIN_WIDTH / 2, RAIN_WIDTH / 2); // Rain_Diameter - placer->setYRange(-RAIN_WIDTH / 2, RAIN_WIDTH / 2); + placer->setXRange(-rainRange.x() / 2, rainRange.x() / 2); // Rain_Diameter + placer->setYRange(-rainRange.y() / 2, rainRange.y() / 2); placer->setZRange(300, 300); emitter->setPlacer(placer); @@ -1631,19 +1638,14 @@ void SkyManager::setWeather(const WeatherResult& weather) SceneUtil::DisableFreezeOnCullVisitor disableFreezeOnCullVisitor; mParticleEffect->accept(disableFreezeOnCullVisitor); - SceneUtil::FindByClassVisitor findEmitterVisitor(std::string("Emitter")); - mParticleEffect->accept(findEmitterVisitor); + SceneUtil::FindByClassVisitor findPSVisitor(std::string("ParticleSystem")); + mParticleEffect->accept(findPSVisitor); - for (unsigned int i = 0; i < findEmitterVisitor.mFoundNodes.size(); i++) + for (unsigned int i = 0; i < findPSVisitor.mFoundNodes.size(); i++) { - NifOsg::Emitter *emitter = (NifOsg::Emitter *) findEmitterVisitor.mFoundNodes[i]; - NifOsg::ParticleSystem *ps = (NifOsg::ParticleSystem *) emitter->getParticleSystem(); - - osg::BoundingBox box = ps->getBoundingBox(); - - osg::Vec3 wrapRange = osg::Vec3(box.xMax() - box.xMin(),box.yMax() - box.yMin(),box.zMax() - box.zMin()); - - ps->setDrawCallback(new WeatherParticleDrawCallback(wrapRange)); + osgParticle::ParticleSystem *ps = static_cast(findPSVisitor.mFoundNodes[i]); + osg::Vec3 weatherRange = osg::Vec3(1024,1024,800); + ps->setDrawCallback(new WeatherParticleDrawCallback(weatherRange)); } } } diff --git a/components/nifosg/particle.cpp b/components/nifosg/particle.cpp index 547b71034..62360b9d6 100644 --- a/components/nifosg/particle.cpp +++ b/components/nifosg/particle.cpp @@ -250,11 +250,6 @@ void Emitter::setPlacer(osgParticle::Placer *placer) mPlacer = placer; } -osgParticle::Placer *Emitter::getPlacer() -{ - return mPlacer; -} - void Emitter::setCounter(osgParticle::Counter *counter) { mCounter = counter; diff --git a/components/nifosg/particle.hpp b/components/nifosg/particle.hpp index df93d5ced..7a2377f9d 100644 --- a/components/nifosg/particle.hpp +++ b/components/nifosg/particle.hpp @@ -225,7 +225,6 @@ namespace NifOsg void setShooter(osgParticle::Shooter* shooter); void setPlacer(osgParticle::Placer* placer); - osgParticle::Placer *getPlacer(); void setCounter(osgParticle::Counter* counter); private: