Merged in animate_review_rebased_bit (pull request #59)

Animated Rendering

Approved-by: Matt Feemster
This commit is contained in:
Seth 2022-02-28 06:01:51 +00:00 committed by Matt Feemster
commit d4174ef92a
7 changed files with 191 additions and 26 deletions

View File

@ -2,6 +2,8 @@
#include "FractoriumPch.h"
template <typename T> class FractoriumEmberController;
/// <summary>
/// EmberTreeWidgetItem
/// </summary>
@ -14,6 +16,12 @@
class EmberTreeWidgetItemBase : public QTreeWidgetItem
{
public:
friend FractoriumEmberController<float>;
#ifdef DO_DOUBLE
friend FractoriumEmberController<double>;
#endif
/// <summary>
/// Constructor that takes a pointer to a QTreeWidget as a parent widget.
/// This is meant to be a root level item.
@ -47,16 +55,22 @@ public:
/// <param name="height">The height of the image in pixels</param>
void SetImage(vector<byte>& v, uint width, uint height)
{
constexpr auto size = 64;
constexpr auto size = PREVIEW_SIZE;
m_Image = QImage(width, height, QImage::Format_RGBA8888);
memcpy(m_Image.scanLine(0), v.data(), SizeOf(v));//Memcpy the data in.
m_Pixmap = QPixmap::fromImage(m_Image).scaled(QSize(size, size), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);//Create a QPixmap out of the QImage, scaled to size.
setData(0, Qt::DecorationRole, m_Pixmap);
}
void SetRendered()
{
m_Rendered = true;
}
protected:
QImage m_Image;
QPixmap m_Pixmap;
bool m_Rendered;
};
/// <summary>

View File

@ -1236,6 +1236,8 @@ void Fractorium::SetTabOrders()
w = SetTabOrder(this, w, ui.SequenceRenderButton);
w = SetTabOrder(this, w, ui.SequenceSaveButton);
w = SetTabOrder(this, w, ui.SequenceOpenButton);
w = SetTabOrder(this, w, ui.SequenceAnimateButton);
w = SetTabOrder(this, w, ui.SequenceClearButton);
w = SetTabOrder(this, w, ui.SequenceTree);
w = SetTabOrder(this, ui.CurrentXformCombo, ui.AddXformButton);//Xforms.
w = SetTabOrder(this, w, ui.AddLinkedXformButton);

View File

@ -208,6 +208,8 @@ public slots:
void OnSequenceRenderButtonClicked(bool checked);
void OnSequenceSaveButtonClicked(bool checked);
void OnSequenceOpenButtonClicked(bool checked);
void OnSequenceAnimateButtonClicked(bool checked);
void OnSequenceClearButtonClicked(bool checked);
void OnSequenceRandomizeStaggerCheckBoxStateChanged(int state);
void OnSequenceRandomizeFramesPerRotCheckBoxStateChanged(int state);
void OnSequenceRandomizeRotationsCheckBoxStateChanged(int state);
@ -391,7 +393,7 @@ public slots:
void ShowCritical(const QString& title, const QString& text, bool invokeRequired = false);
//Can't have a template function be a slot.
void SetLibraryTreeItemData(EmberTreeWidgetItemBase* item, vv4F& v, uint w, uint h);
void SetTreeItemData(EmberTreeWidgetItemBase* item, vv4F& v, uint w, uint h);
public:
//template<typename spinType, typename valType>//See below.

View File

@ -8118,6 +8118,33 @@
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="SequenceAnimateButtonsHLayout" stretch="0,0,0,0">
<property name="spacing">
<number>2</number>
</property>
<item>
<widget class="QPushButton" name="SequenceAnimateButton">
<property name="toolTip">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Animate the sequence of generated thumbnails.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
<property name="text">
<string>Animate</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="SequenceClearButton">
<property name="toolTip">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Clear the sequence of generated thumbnails.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
<property name="text">
<string>Clear</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="LibraryTreeWidget" name="SequenceTree">

View File

@ -18,7 +18,12 @@ FractoriumEmberControllerBase::FractoriumEmberControllerBase(Fractorium* fractor
m_RenderTimer->setInterval(0);
m_Fractorium->connect(m_RenderTimer.get(), SIGNAL(timeout()), SLOT(IdleTimer()));
m_RenderRestartTimer = make_unique<QTimer>(m_Fractorium);
m_AnimateTimer = make_unique<QTimer>(m_Fractorium);
m_AnimateTimer->stop();
m_Fractorium->connect(m_RenderRestartTimer.get(), &QTimer::timeout, [&]() { m_Fractorium->StartRenderTimer(false); });//It's ok to pass false for the first shot because creating the controller will start the preview renders.
// XXX: why not SLOT(SequenceAnimateNextFrame())?
m_Fractorium->connect(m_AnimateTimer.get(), &QTimer::timeout, [&]() { SequenceAnimateNextFrame(); });
}
/// <summary>
@ -30,6 +35,7 @@ FractoriumEmberControllerBase::~FractoriumEmberControllerBase()
StopRenderTimer(true);
m_RenderTimer->stop();
m_RenderRestartTimer->stop();
m_AnimateTimer->stop();
}
/// <summary>
@ -379,7 +385,8 @@ void TreePreviewRenderer<T>::PreviewRenderFunc(uint start, uint end)
m_PreviewRenderer.YAxisUp(f->m_Settings->YAxisUp());
m_PreviewRenderer.ThreadCount(std::max(1u, Timing::ProcessorCount() - 1));//Leave one processor free so the GUI can breathe.
if (const auto top = m_Tree->topLevelItem(0))
// Possible animate item at index 0
if (const auto top = m_Tree->topLevelItem(m_Tree->topLevelItemCount() - 1))
{
size_t i = start;
@ -402,11 +409,12 @@ void TreePreviewRenderer<T>::PreviewRenderFunc(uint start, uint end)
//until the update is complete.
if (m_PreviewRun)
{
QMetaObject::invokeMethod(f, "SetLibraryTreeItemData", Qt::DirectConnection,
QMetaObject::invokeMethod(f, "SetTreeItemData", Qt::DirectConnection,
Q_ARG(EmberTreeWidgetItemBase*, treeItem),
Q_ARG(vv4F&, m_PreviewFinalImage),
Q_ARG(uint, PREVIEW_SIZE),
Q_ARG(uint, PREVIEW_SIZE));
treeItem->SetRendered();
}
}
}

View File

@ -32,7 +32,7 @@ class Fractorium;
template <typename T> class PreviewRenderer;
template <typename T> class TreePreviewRenderer;
#define PREVIEW_SIZE 256
#define PREVIEW_SIZE 128
#define UNDO_SIZE 512
/// <summary>
@ -135,9 +135,13 @@ public:
virtual void MoveLibraryItems(const QModelIndexList& items, int destRow) { }
virtual void Delete(const vector<pair<size_t, QTreeWidgetItem*>>& v) { }
virtual void FillSequenceTree() { }
virtual void AddAnimationItem() { }
virtual void SequenceGenerateButtonClicked() { }
virtual void SequenceSaveButtonClicked() { }
virtual void SequenceOpenButtonClicked() { }
virtual void SequenceAnimateButtonClicked() { }
virtual void SequenceAnimateNextFrame() { }
virtual void SequenceClearButtonClicked() { }
//Params.
virtual void ParamsToEmber(Ember<float>& ember, bool imageParamsOnly = false) { };
@ -324,8 +328,10 @@ protected:
Palette<float> m_TempPalette, m_PreviousTempPalette;
std::unique_ptr<QTimer> m_RenderTimer;
std::unique_ptr<QTimer> m_RenderRestartTimer;
std::unique_ptr<QTimer> m_AnimateTimer;
shared_ptr<PaletteList<float>> m_PaletteList;
shared_ptr<OpenCLInfo> m_Info = OpenCLInfo::Instance();
int m_AnimateFrame = 0;
};
/// <summary>
@ -427,9 +433,13 @@ public:
void StopSequencePreviewRender() override;
void StopAllPreviewRenderers() override;
void FillSequenceTree() override;
void AddAnimationItem() override;
void SequenceGenerateButtonClicked() override;
void SequenceSaveButtonClicked() override;
void SequenceOpenButtonClicked() override;
void SequenceAnimateButtonClicked() override;
void SequenceAnimateNextFrame() override;
void SequenceClearButtonClicked() override;
//Params.
void ParamsToEmber(Ember<float>& ember, bool imageParamsOnly = false) override;

View File

@ -17,6 +17,8 @@ void Fractorium::InitLibraryUI()
connect(ui.SequenceAllButton, SIGNAL(clicked(bool)), this, SLOT(OnSequenceAllButtonClicked(bool)), Qt::QueuedConnection);
connect(ui.SequenceGenerateButton, SIGNAL(clicked(bool)), this, SLOT(OnSequenceGenerateButtonClicked(bool)), Qt::QueuedConnection);
connect(ui.SequenceRenderButton, SIGNAL(clicked(bool)), this, SLOT(OnSequenceRenderButtonClicked(bool)), Qt::QueuedConnection);
connect(ui.SequenceAnimateButton, SIGNAL(clicked(bool)), this, SLOT(OnSequenceAnimateButtonClicked(bool)), Qt::QueuedConnection);
connect(ui.SequenceClearButton, SIGNAL(clicked(bool)), this, SLOT(OnSequenceClearButtonClicked(bool)), Qt::QueuedConnection);
connect(ui.SequenceSaveButton, SIGNAL(clicked(bool)), this, SLOT(OnSequenceSaveButtonClicked(bool)), Qt::QueuedConnection);
connect(ui.SequenceOpenButton, SIGNAL(clicked(bool)), this, SLOT(OnSequenceOpenButtonClicked(bool)), Qt::QueuedConnection);
connect(ui.SequenceRandomizeStaggerCheckBox, SIGNAL(stateChanged(int)), this, SLOT(OnSequenceRandomizeStaggerCheckBoxStateChanged(int)), Qt::QueuedConnection);
@ -127,7 +129,7 @@ vector<pair<size_t, QTreeWidgetItem*>> Fractorium::GetCurrentEmberIndex(bool isC
/// <param name="v">The vector holding the RGBA bitmap</param>
/// <param name="w">The width of the bitmap</param>
/// <param name="h">The height of the bitmap</param>
void Fractorium::SetLibraryTreeItemData(EmberTreeWidgetItemBase* item, vv4F& v, uint w, uint h)
void Fractorium::SetTreeItemData(EmberTreeWidgetItemBase* item, vv4F& v, uint w, uint h)
{
m_PreviewVec.resize(size_t(w) * size_t(h) * 4);
Rgba32ToRgba8(v.data(), m_PreviewVec.data(), w, h, m_Settings->Transparency());
@ -176,18 +178,20 @@ void FractoriumEmberController<T>::SyncLibrary(eLibraryUpdate update)
template <typename T>
void FractoriumEmberController<T>::FillLibraryTree(int selectIndex)
{
uint size = 64;
uint i = 0;
const auto tree = m_Fractorium->ui.LibraryTree;
vector<byte> v(size * size * 4);
StopAllPreviewRenderers();
const uint size = PREVIEW_SIZE;
vector<byte> empy_preview(size * size * 4);
const auto tree = m_Fractorium->ui.LibraryTree;
tree->clear();
auto fileItem = new QTreeWidgetItem(tree);
QFileInfo info(m_EmberFile.m_Filename);
fileItem->setText(0, info.fileName());
fileItem->setToolTip(0, m_EmberFile.m_Filename);
fileItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsSelectable | Qt::ItemIsDropEnabled);
uint i = 0;
for (auto& it : m_EmberFile.m_Embers)
{
auto emberItem = new EmberTreeWidgetItem<T>(&it, fileItem);
@ -198,7 +202,7 @@ void FractoriumEmberController<T>::FillLibraryTree(int selectIndex)
emberItem->setText(0, it.m_Name.c_str());
emberItem->setToolTip(0, emberItem->text(0));
emberItem->SetImage(v, size, size);
emberItem->SetImage(empy_preview, size, size);
}
if (selectIndex != -1)
@ -216,8 +220,8 @@ void FractoriumEmberController<T>::FillLibraryTree(int selectIndex)
template <typename T>
void FractoriumEmberController<T>::UpdateLibraryTree()
{
const uint size = 64;
vector<byte> v(size * size * 4);
const uint size = PREVIEW_SIZE;
vector<byte> empy_preview(size * size * 4);
const auto tree = m_Fractorium->ui.LibraryTree;
if (auto top = tree->topLevelItem(0))
@ -235,7 +239,7 @@ void FractoriumEmberController<T>::UpdateLibraryTree()
emberItem->setText(0, it->m_Name.c_str());
emberItem->setToolTip(0, emberItem->text(0));
emberItem->SetImage(v, size, size);
emberItem->SetImage(empy_preview, size, size);
}
//When adding elements, ensure all indices are sequential.
@ -428,10 +432,10 @@ template <typename T>
void FractoriumEmberController<T>::RenderPreviews(QTreeWidget* tree, TreePreviewRenderer<T>* renderer, EmberFile<T>& file, uint start, uint end)
{
renderer->Stop();
if (start == UINT_MAX && end == UINT_MAX)
{
if (const auto top = tree->topLevelItem(0))
// Animated item might be at index 0, previews go in last item.
if (const auto top = tree->topLevelItem(tree->topLevelItemCount() - 1))
{
const auto childCount = top->childCount();
vector<byte> emptyPreview(PREVIEW_SIZE * PREVIEW_SIZE * 4);
@ -471,6 +475,23 @@ void FractoriumEmberController<T>::StopAllPreviewRenderers()
StopSequencePreviewRender();
}
template <typename T>
void FractoriumEmberController<T>::AddAnimationItem()
{
auto fileItem = new QTreeWidgetItem(m_Fractorium->ui.SequenceTree);
fileItem->setText(0, "Rendered Animation");
fileItem->setToolTip(0, "Rendered frames can be animated here");
fileItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
auto emberItem = new EmberTreeWidgetItemBase(fileItem);
emberItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
emberItem->setToolTip(0, "Animated Frame");
const uint size = PREVIEW_SIZE;
vector<byte> empy_preview(size * size * 4);
emberItem->SetImage(empy_preview, size, size);
}
/// <summary>
/// Fill the sequence tree with the names of the embers in the
/// currently generated sequence.
@ -479,18 +500,23 @@ void FractoriumEmberController<T>::StopAllPreviewRenderers()
template <typename T>
void FractoriumEmberController<T>::FillSequenceTree()
{
const uint size = 64;
uint i = 0;
StopAllPreviewRenderers();
const uint size = PREVIEW_SIZE;
vector<byte> empy_preview(size * size * 4);
const auto tree = m_Fractorium->ui.SequenceTree;
vector<byte> v(size * size * 4);
m_SequencePreviewRenderer->Stop();
tree->clear();
// Add extra TreeWidget for animation at index 0
AddAnimationItem();
auto fileItem = new QTreeWidgetItem(tree);
QFileInfo info(m_SequenceFile.m_Filename);
fileItem->setText(0, info.fileName());
fileItem->setToolTip(0, m_SequenceFile.m_Filename);
fileItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsSelectable);
uint i = 0;
for (auto& it : m_SequenceFile.m_Embers)
{
auto emberItem = new EmberTreeWidgetItemBase(fileItem);
@ -503,10 +529,13 @@ void FractoriumEmberController<T>::FillSequenceTree()
i++;
emberItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
emberItem->setToolTip(0, emberItem->text(0));
emberItem->SetImage(v, size, size);
emberItem->SetImage(empy_preview, size, size);
}
tree->expandAll();
// Hide the animation item
tree->collapseItem(tree->topLevelItem(0));
RenderSequencePreviews(0, uint(m_SequenceFile.Size()));
}
@ -519,7 +548,7 @@ void FractoriumEmberController<T>::FillSequenceTree()
template <typename T>
void FractoriumEmberController<T>::SequenceTreeItemChanged(QTreeWidgetItem* item, int col)
{
if (item == m_Fractorium->ui.SequenceTree->topLevelItem(0))
if (item == m_Fractorium->ui.SequenceTree->topLevelItem(1))
{
auto text = item->text(0);
@ -541,8 +570,13 @@ void Fractorium::OnSequenceTreeItemChanged(QTreeWidgetItem* item, int col)
/// <param name="start">Ignored, render all.</param>
/// <param name="end">Ignored, render all.</param>
template <typename T>
void FractoriumEmberController<T>::RenderSequencePreviews(uint start, uint end) { RenderPreviews(m_Fractorium->ui.SequenceTree, m_SequencePreviewRenderer.get(), m_SequenceFile, start, end); }
void Fractorium::OnSequenceStartPreviewsButtonClicked(bool checked) { m_Controller->RenderSequencePreviews(); }
void FractoriumEmberController<T>::RenderSequencePreviews(uint start, uint end) {
RenderPreviews(m_Fractorium->ui.SequenceTree, m_SequencePreviewRenderer.get(), m_SequenceFile, start, end);
}
void Fractorium::OnSequenceStartPreviewsButtonClicked(bool checked) {
m_Controller->RenderSequencePreviews();
}
/// <summary>
/// Stop rendering the sequence previews.
@ -730,8 +764,6 @@ void FractoriumEmberController<T>::SequenceGenerateButtonClicked()
for (frame = 0; frame < blendFrames; frame++)
{
//if (frame == 43)
// cout << frame << endl;
const auto seqFlag = frame == 0 || (frame == blendFrames - 1);
blend = frame / double(blendFrames);
result.Clear();
@ -773,6 +805,76 @@ void Fractorium::OnSequenceRenderButtonClicked(bool checked)
}
}
/// <summary>
/// Animate the sequence
/// </summary>
template <typename T>
void FractoriumEmberController<T>::SequenceAnimateNextFrame()
{
const auto tree = m_Fractorium->ui.SequenceTree;
if (const auto renders = tree->topLevelItem(1))
{
if (renders->childCount())
{
const auto animate = dynamic_cast<EmberTreeWidgetItemBase*>(tree->topLevelItem(0)->child(0));
const auto frame = m_AnimateFrame++ % renders->childCount();
const auto nth = dynamic_cast<EmberTreeWidgetItemBase*>(renders->child(frame));
if (animate && nth)
{
if (!nth->m_Rendered)
{
m_AnimateFrame = 0;
}
else
{
animate->m_Pixmap = QPixmap(nth->m_Pixmap);
animate->setData(0, Qt::DecorationRole, animate->m_Pixmap);
}
}
}
}
}
/// <summary>
/// Animate the sequence
/// </summary>
template <typename T>
void FractoriumEmberController<T>::SequenceAnimateButtonClicked()
{
if (const auto animation = m_Fractorium->ui.SequenceTree->topLevelItem(0))
{
if (animation->isExpanded())
{
animation->setExpanded(false);
m_AnimateTimer->stop();
}
else
{
animation->setExpanded(true);
m_AnimateFrame = 0;
// TODO Make this a UI Parameter
const auto fps = 30;
m_AnimateTimer->start(1000 / fps);
}
}
}
void Fractorium::OnSequenceAnimateButtonClicked(bool checked) { m_Controller->SequenceAnimateButtonClicked(); }
/// <summary>
/// Clear the sequence.
/// </summary>
template <typename T>
void FractoriumEmberController<T>::SequenceClearButtonClicked()
{
const auto tree = m_Fractorium->ui.SequenceTree;
m_SequencePreviewRenderer->Stop();
tree->clear();
}
void Fractorium::OnSequenceClearButtonClicked(bool checked) { m_Controller->SequenceClearButtonClicked(); }
/// <summary>
/// Save the sequence to a file.
/// </summary>