summaryrefslogtreecommitdiffstats
path: root/src/Runtime/api/studio3d/q3dsimagesequencegeneratorthread.cpp
blob: 0968e3f197f8cf24a90baf0d5d6e842292c3d797 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/****************************************************************************
**
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt 3D Studio.
**
** $QT_BEGIN_LICENSE:GPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 or (at your option) any later version
** approved by the KDE Free Qt Foundation. The licenses are as published by
** the Free Software Foundation and appearing in the file LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "q3dsimagesequencegeneratorthread_p.h"
#include "q3dssurfaceviewer.h"
#include "q3dspresentation.h"
#include "q3dsviewersettings.h"

#include <QtGui/qopenglcontext.h>
#include <QtGui/qopenglfunctions.h>
#include <QtGui/qoffscreensurface.h>
#include <QtGui/qopenglframebufferobject.h>
#include <QtCore/qfileinfo.h>
#include <QtCore/qdir.h>
#include <QtCore/qdebug.h>
#include <QtCore/qmath.h>

bool Q3DSImageSequenceGeneratorThread::initialize(
        const QString &presentation, qreal start, qreal end, qreal fps, qreal frameInterval,
        int width, int height, const QString &outPath, const QString &outFile)
{
    QFileInfo fileInfo(presentation);
    if (!fileInfo.exists()) {
        QString error = QObject::tr("File not found: '%1'").arg(presentation);
        qWarning() << "Generating image sequence failed -" << error;
        Q_EMIT generationFinished(false, error);
        return false;
    }

    m_outputFileName = QStringLiteral("%2/%1_%3.png");
    if (outFile.isEmpty()) {
        m_outputFileName = m_outputFileName.arg(fileInfo.baseName())
                .arg(outPath).arg(QStringLiteral("%1"));
    } else {
        m_outputFileName = m_outputFileName.arg(outFile).arg(outPath).arg(QStringLiteral("%1"));
    }

    m_sourceUrl = QUrl::fromLocalFile(fileInfo.absoluteFilePath());

    QSurfaceFormat format;
    format.setDepthBufferSize(24);
    format.setStencilBufferSize(8);

    m_context = new QOpenGLContext;
    m_context->setFormat(format);
    if (!m_context->create()) {
        QString error = QObject::tr("Failed to create context");
        qWarning() << "Generating image sequence failed -" << error;
        Q_EMIT generationFinished(false, error);
        return false;
    }

    m_surface = new QOffscreenSurface;
    m_surface->setFormat(m_context->format());
    m_surface->create();

    m_mainThread = QThread::currentThread();
    m_surface->moveToThread(this);
    m_context->moveToThread(this);

    m_start = start;
    m_end = end;
    m_fps = fps;
    m_frameInterval = frameInterval;
    m_width = width;
    m_height = height;

    return true;
}

Q3DSImageSequenceGeneratorThread::Q3DSImageSequenceGeneratorThread()
    : m_start(0)
    , m_end(10000)
    , m_fps(60)
    , m_frameInterval(16.666667)
    , m_width(1920)
    , m_height(1080)
    , m_surface(nullptr)
    , m_context(nullptr)
{
}

Q3DSImageSequenceGeneratorThread::~Q3DSImageSequenceGeneratorThread() {
    delete m_context;
    delete m_surface;
}

void Q3DSImageSequenceGeneratorThread::run() {
    if (!m_context->makeCurrent(m_surface)) {
        QString error = QObject::tr("Couldn't make context current.");
        qWarning() << "Generating image sequence failed -" << error;
        Q_EMIT generationFinished(false, error);
        cleanup();
        return;
    }

    const QSize size(m_width, m_height);
    QOpenGLFramebufferObject fbo(size, QOpenGLFramebufferObject::CombinedDepthStencil);

    Q3DSSurfaceViewer viewer;
    viewer.presentation()->setSource(m_sourceUrl);
    viewer.settings()->setScaleMode(Q3DSViewerSettings::ScaleModeFill);

    viewer.setUpdateInterval(-1);
    viewer.setAutoSize(false);
    viewer.setSize(size);

    if (!viewer.create(m_surface, m_context, fbo.handle())) {
        QString error = QObject::tr("Viewer initialization failed.");
        qWarning() << "Generating image sequence failed -" << error;
        Q_EMIT generationFinished(false, error);
        cleanup();
        return;
    }

    if (m_frameInterval <= 0)
        m_frameInterval = 1000.0 / m_fps;

    // Presentations always assume you want to start animating at time zero and set a local
    // offset to global time when they render the first frame. This means we need to always
    // render a frame at global time zero first.
    if (qRound(m_start) != 0) {
        viewer.presentation()->setGlobalAnimationTime(0);
        viewer.update();
    }

    // Add a bit of time to the end time to ensure we don't lose the last frame to rounding errors
    m_end += m_frameInterval / 10000.0;

    // Ensure directory exists
    QFileInfo fi(m_outputFileName);
    QDir dir = fi.absoluteDir();
    dir.mkpath(".");

    int frameCount = 0;
    int totalFrames = qCeil((m_end - m_start) / m_frameInterval);
    for (qreal t = m_start; t <= m_end; t += m_frameInterval) {
        ++frameCount;
        viewer.presentation()->setGlobalAnimationTime(qRound64(t));
        viewer.update();
        if (!fbo.toImage().save(m_outputFileName.arg(frameCount))) {
            QString error = QObject::tr("Failed to write output file: '%1'")
                    .arg(m_outputFileName.arg(frameCount));
            qWarning() << "Generating image sequence failed -" << error;
            Q_EMIT generationFinished(false, error);
            cleanup();
            return;
        }
        Q_EMIT progress(totalFrames, frameCount);
    }

    Q_EMIT generationFinished(true, m_outputFileName.arg("*"));
    cleanup();
}

void Q3DSImageSequenceGeneratorThread::cleanup()
{
    m_context->doneCurrent();
    delete m_context;
    m_context = nullptr;
    // Surface needs to be deleted in the thread it was created in
    m_surface->moveToThread(m_mainThread);
    deleteLater();
}