Files
oak-editor/app/widget/multicam/multicamdisplay.cpp
T
Mike-Solar 0aa5879f35 app: migrate all engine access to the C ABI facade (nm U _ZN5olive = 0)
Every app module now reaches liboakengine exclusively through
oakengine_* C calls, EngineEventBridge subscriptions and app-side
handle headers (cliphandle/keyframehandle/nodevaluehandle/oakvaluehelper).
Direct C++ command construction, engine signal connect()s, and engine
type usage in MOC-visible signatures are gone: 557 -> 0 undefined
olive:: symbols in oak-editor.
2026-07-26 22:43:21 +08:00

203 lines
5.3 KiB
C++

/***
Olive - Non-Linear Video Editor
Copyright (C) 2022 Olive Team
Modifications Copyright (C) 2025 mikesolar
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
***/
#include "multicamdisplay.h"
#include "oakengine/display.h"
#include "oakengine/node.h"
namespace olive
{
#define super ViewerDisplayWidget
MulticamDisplay::MulticamDisplay(QWidget *parent)
: super(parent)
, node_(nullptr)
, rows_(0)
, cols_(0)
{
}
void MulticamDisplay::on_paint()
{
super::on_paint();
if (node_) {
QPainter p(paint_device());
p.setPen(QPen(Qt::yellow, fontMetrics().height() / 4));
p.setBrush(Qt::NoBrush);
int rows, cols;
oakengine_multicam_get_rows_and_columns(
oakengine_multicam_get_source_count(
reinterpret_cast<OakEngineNode *>(node_)),
&rows, &cols);
int multi = std::max(rows, cols);
int cell_width = width() / multi;
int cell_height = height() / multi;
int col, row;
int current_source = oakengine_multicam_get_current_source(
reinterpret_cast<OakEngineNode *>(node_));
oakengine_multicam_index_to_row_cols(
current_source, rows, cols, &row, &col);
QRect r(cell_width * col, cell_height * row, cell_width, cell_height);
p.drawRect(generate_world_transform().mapRect(r));
}
}
void MulticamDisplay::on_destroy()
{
shader_ = QVariant();
}
TexturePtr MulticamDisplay::load_custom_texture_from_frame(const QVariant &v)
{
if (v.canConvert<QVector<TexturePtr>>()) {
QVector<TexturePtr> tex = v.value<QVector<TexturePtr>>();
TexturePtr main;
const VideoParams main_params = this->get_viewport_params();
oakengine_display_renderer_create_texture(renderer(), &main_params,
nullptr, 0, &main);
int rows, cols;
oakengine_multicam_get_rows_and_columns(tex.size(), &rows, &cols);
if (shader_.isNull() || rows_ != rows || cols_ != cols) {
if (!shader_.isNull()) {
renderer()->destroy_native_shader(shader_);
}
shader_ = renderer()->create_native_shader(
ShaderCode(generate_shader_code(rows, cols)));
rows_ = rows;
cols_ = cols;
}
ShaderJob job;
for (int i = 0; i < tex.size(); i++) {
int c, r;
oakengine_multicam_index_to_row_cols(i, rows, cols, &r, &c);
job.insert(QStringLiteral("tex_%1_%2")
.arg(QString::number(r), QString::number(c)),
NodeValue(NodeValue::k_texture, tex.at(i)));
}
renderer()->blit_to_texture(shader_, job, main.get());
return main;
} else {
return super::load_custom_texture_from_frame(v);
}
}
QString dbl_to_glsl(double d)
{
return QString::number(d, 'f');
}
QString MulticamDisplay::generate_shader_code(int rows, int cols)
{
int multiplier = std::max(cols, rows);
QStringList shader;
shader.append(QStringLiteral("in vec2 ove_texcoord;"));
shader.append(QStringLiteral("out vec4 frag_color;"));
for (int x = 0; x < cols; x++) {
for (int y = 0; y < rows; y++) {
shader.append(QStringLiteral("uniform sampler2D tex_%1_%2;")
.arg(QString::number(y), QString::number(x)));
shader.append(QStringLiteral("uniform bool tex_%1_%2_enabled;")
.arg(QString::number(y), QString::number(x)));
}
}
shader.append(QStringLiteral("void main() {"));
for (int x = 0; x < cols; x++) {
if (x > 0) {
shader.append(QStringLiteral(" else"));
}
if (x == cols - 1) {
shader.append(QStringLiteral(" {"));
} else {
shader.append(
QStringLiteral(" if (ove_texcoord.x < %1) {")
.arg(dbl_to_glsl(double(x + 1) / double(multiplier))));
}
for (int y = 0; y < rows; y++) {
if (y > 0) {
shader.append(QStringLiteral(" else"));
}
if (y == rows - 1) {
shader.append(QStringLiteral(" {"));
} else {
shader.append(
QStringLiteral(" if (ove_texcoord.y < %1) {")
.arg(dbl_to_glsl(double(y + 1) / double(multiplier))));
}
QString input = QStringLiteral("tex_%1_%2")
.arg(QString::number(y), QString::number(x));
shader.append(
QStringLiteral(
" vec2 coord = vec2((ove_texcoord.x+%1)*%2, (ove_texcoord.y+%3)*%4);")
.arg(dbl_to_glsl(-double(x) / double(multiplier)),
dbl_to_glsl(multiplier),
dbl_to_glsl(-double(y) / double(multiplier)),
dbl_to_glsl(multiplier)));
shader.append(
QStringLiteral(
" if (%1_enabled && coord.x >= 0.0 && coord.x < 1.0 && coord.y >= 0.0 && coord.y < 1.0) {")
.arg(input));
shader.append(
QStringLiteral(" frag_color = texture(%1, coord);")
.arg(input));
shader.append(QStringLiteral(" } else {"));
shader.append(QStringLiteral(" discard;"));
shader.append(QStringLiteral(" }"));
shader.append(QStringLiteral(" }"));
}
shader.append(QStringLiteral(" }"));
}
shader.append(QStringLiteral("}"));
return shader.join('\n');
}
void MulticamDisplay::set_multicam_node(MultiCamNode *n)
{
node_ = n;
}
}