tahoma2d/toonz/sources/stdfx/particlesmanager.cpp

135 lines
4.4 KiB
C++
Raw Normal View History

2016-03-19 06:57:51 +13:00
#include "trenderer.h"
#include <QMutexLocker>
#include "particlesmanager.h"
/*
EXPLANATION:
2016-06-15 18:43:10 +12:00
ParticlesManager improves the old particles system as follows - the particles
manager stores the
2016-03-19 06:57:51 +13:00
last particles configuration which had some particle rendered by a thread.
2016-06-15 18:43:10 +12:00
Under normal cicumstances, this means that every thread has the particles
configuration that rendered
2016-03-19 06:57:51 +13:00
last. In case a trail was set, such frame is that beyond the trail.
2016-06-15 18:43:10 +12:00
This managemer works well on the assumption that each thread builds particle in
an incremental timeline.
2016-03-19 06:57:51 +13:00
*/
//--------------------------------------------------------------------------------------------------
DEFINE_CLASS_CODE(ParticlesManager::FxData, 110)
//--------------------------------------------------------------------------------------------------
typedef std::map<double, ParticlesManager::FrameData> FramesMap;
//************************************************************************************************
// Preliminaries
//************************************************************************************************
class ParticlesManagerGenerator final : public TRenderResourceManagerGenerator {
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
ParticlesManagerGenerator() : TRenderResourceManagerGenerator(true) {}
2016-03-19 06:57:51 +13:00
2016-06-20 14:23:05 +12:00
TRenderResourceManager *operator()(void) override {
return new ParticlesManager;
}
2016-03-19 06:57:51 +13:00
};
MANAGER_FILESCOPE_DECLARATION(ParticlesManager, ParticlesManagerGenerator);
//************************************************************************************************
// FrameData implementation
//************************************************************************************************
ParticlesManager::FrameData::FrameData(FxData *fxData)
2016-06-15 18:43:10 +12:00
: m_fxData(fxData)
, m_frame((std::numeric_limits<int>::min)())
, m_calculated(false)
, m_maxTrail(-1)
, m_totalParticles(0) {
m_fxData->addRef();
2016-03-19 06:57:51 +13:00
}
//-------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
ParticlesManager::FrameData::~FrameData() { m_fxData->release(); }
2016-03-19 06:57:51 +13:00
//-------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void ParticlesManager::FrameData::buildMaxTrail() {
// Store the maximum trail of each particle
std::list<Particle>::iterator it;
for (it = m_particles.begin(); it != m_particles.end(); ++it)
m_maxTrail = std::max(m_maxTrail, it->trail);
2016-03-19 06:57:51 +13:00
}
//-------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void ParticlesManager::FrameData::clear() {
m_frame = (std::numeric_limits<int>::min)();
m_particles.clear();
m_random = TRandom();
m_calculated = false;
m_maxTrail = -1;
m_totalParticles = 0;
2016-03-19 06:57:51 +13:00
}
//************************************************************************************************
// FxData implementation
//************************************************************************************************
2016-06-15 18:43:10 +12:00
ParticlesManager::FxData::FxData() : TSmartObject(m_classCode) {}
2016-03-19 06:57:51 +13:00
//************************************************************************************************
// ParticlesContainer implementation
//************************************************************************************************
2016-06-15 18:43:10 +12:00
ParticlesManager *ParticlesManager::instance() {
return static_cast<ParticlesManager *>(
ParticlesManager::gen()->getManager(TRenderer::renderId()));
2016-03-19 06:57:51 +13:00
}
//-------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
ParticlesManager::ParticlesManager() : m_renderStatus(-1) {}
2016-03-19 06:57:51 +13:00
//-------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
ParticlesManager::~ParticlesManager() {
// Release all fxDatas
std::map<unsigned long, FxData *>::iterator it, end = m_fxs.end();
for (it = m_fxs.begin(); it != end; ++it) it->second->release();
2016-03-19 06:57:51 +13:00
}
//-------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void ParticlesManager::onRenderStatusStart(int renderStatus) {
m_renderStatus = renderStatus;
2016-03-19 06:57:51 +13:00
}
//-------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
ParticlesManager::FrameData *ParticlesManager::data(unsigned long fxId) {
QMutexLocker locker(&m_mutex);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
std::map<unsigned long, FxData *>::iterator it = m_fxs.find(fxId);
if (it == m_fxs.end()) {
it = m_fxs.insert(std::make_pair(fxId, new FxData)).first;
it->second->addRef();
}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
FxData *fxData = it->second;
FrameData *d = fxData->m_frames.localData();
if (!d) {
d = new FrameData(fxData);
fxData->m_frames.setLocalData(d);
}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
return d;
2016-03-19 06:57:51 +13:00
}