Используя OpenGL, чтобы отобразить текстуру в пользовательском QQuickItem, текстура выглядит неполной в окне QML [закрытC++

Программы на C++. Форум разработчиков
Ответить Пред. темаСлед. тема
Anonymous
 Используя OpenGL, чтобы отобразить текстуру в пользовательском QQuickItem, текстура выглядит неполной в окне QML [закрыт

Сообщение Anonymous »

Я хочу, чтобы изображение было полностью отображено в окне. Я отображаю свое собственное изображение, унаследовав QquickItem и используя OpenGL. Как и в случае QT https://doc.qt.io/qt-6.5/qtquick-cenegr ... ample.html.
Я подозреваю Координаты вершины, которые приводят к тому, что текстура скрыта другими элементами в окне. Но я не знаю, как обработать его, чтобы завершить отображение моего изображения. Вот мой код инициализации OpenGL и код для визуализации изображения. < /P>
//Initialize the OpenGL
QString vertexSource = R"(
#version 330 core
layout(location = 0) in vec3 vPosition;
layout(location = 1) in vec2 vTexCoord;
out vec2 TexCoord;
uniform mat4 zoomMatrix;
void main() {
gl_Position = zoomMatrix * vec4(vPosition, 1.0);
TexCoord = vTexCoord;
}
)";

QString fragSource = R"(
#version 330 core
in vec2 TexCoord;
out vec4 FragColor;
uniform sampler2D frameTexture;
uniform bool IsTextureValid;
void main() {
if(!IsTextureValid)
{
FragColor = vec4(0.0,0.0,0.0,1.0);
} else {
FragColor = texture(frameTexture, TexCoord);
}
}
)";

if (this->shader_program != nullptr)
return;

QSGRendererInterface *rif = this->quick_window->rendererInterface();
Q_ASSERT(rif->graphicsApi() == QSGRendererInterface::OpenGL);

initializeOpenGLFunctions();

this->shader_program = new QOpenGLShaderProgram();
if (!this->shader_program->addShaderFromSourceCode(QOpenGLShader::Vertex, vertexSource))
qDebug() shader_program->log();
if (!this->shader_program->addShaderFromSourceCode(QOpenGLShader::Fragment, fragSource))
qDebug() shader_program->log();
if (!this->shader_program->link())
qDebug() shader_program->log();

QVector vertices = {
// positions // texture coords
-1.0f, 1.0f, 0.0f, 0.0f, 1.0f, // top left
-1.0f, -1.0f, 0.0f, 0.0f, 0.0f, // bottom left
1.0f, -1.0f, 0.0f, 1.0f, 0.0f, // bottom right
1.0f, 1.0f, 0.0f, 1.0f, 1.0f, // top right
};

QVector indices = {
0, 1, 2, 2, 3, 0,
};

this->vao = new QOpenGLVertexArrayObject();
this->vbo = new QOpenGLBuffer(QOpenGLBuffer::VertexBuffer);
this->ebo = new QOpenGLBuffer(QOpenGLBuffer::IndexBuffer);

this->vao->create();
this->vbo->create();
this->ebo->create();

this->vao->bind();
this->vbo->bind();
this->ebo->bind();

this->vbo->allocate(vertices.constData(), vertices.size() * sizeof(GLfloat));
this->ebo->allocate(indices.constData(), indices.size() * sizeof(GLuint));

this->shader_program->bind();

this->shader_program->setAttributeBuffer("vPosition", GL_FLOAT, 0, 3, 5 * sizeof(GLfloat));
this->shader_program->enableAttributeArray(0);
this->shader_program->setAttributeBuffer("vTexCoord", GL_FLOAT, 3 * sizeof(GLfloat), 2, 5 * sizeof(GLfloat));
this->shader_program->enableAttributeArray(1);

this->vao->release();

this->video_frame_texture = new QOpenGLTexture(QOpenGLTexture::Target2D);
this->video_frame_texture->setMagnificationFilter(QOpenGLTexture::Linear);
this->video_frame_texture->setMinificationFilter(QOpenGLTexture::Linear);
this->video_frame_texture->setWrapMode(QOpenGLTexture::Repeat);
< /code>
//paint texture code
bool isTextureValid;
QMatrix4x4 zoom;

this->quick_window->beginExternalCommands();

glDisable(GL_DEPTH_TEST);

glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);

if (frame != nullptr) {
QMutexLocker locker(&mutex);

QImage img(frame->data[0], frame->width, frame->height, QImage::Format_BGR888);
if (img.isNull()) {
spdlog::error("QImage is null");
return;
}

video_frame_texture->destroy();
video_frame_texture->create();

video_frame_texture->setData(img.mirrored());

isTextureValid = true;

// Calculate the target size at 16:9 ratio
float targetWidth = this->window_size.width();
float targetHeight = targetWidth * 9.0f / 16.0f;

// If the calculated height exceeds the height of the window, the width is calculated by the height
if (targetHeight > window_size.height()) {
targetHeight = window_size.height();
targetWidth = targetHeight * 16.0f / 9.0f;
}

// Calculated scaling
float scaleX = targetWidth / this->window_size.width();
float scaleY = targetHeight / this->window_size.height();

zoom.scale(scaleX, scaleY, 0.0f);

} else {
isTextureValid = false;
}

this->shader_program->bind();
this->shader_program->setUniformValue("IsTextureValid", isTextureValid);

this->vao->bind();
if (isTextureValid) {

this->video_frame_texture->bind(0);
}
this->shader_program->setUniformValue("zoomMatrix", zoom);

glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);

this->vao->release();
this->shader_program->release();
if (isTextureValid)
this->video_frame_texture->release();

this->quick_window->endExternalCommands();
< /code>
My UI layout is as follows
ApplicationWindow {
id: appWindow

objectName: "qmlAppWindow"
width: 1280
height: 720
visible: true
title: qsTr("AVPlayer")

menuBar: MenuBar {
Menu {
title: "&File"
Action {
text: "&Open"
icon.name: "document-open"
shortcut: StandardKey.Open

onTriggered: fdialog.open()
}
}
}

footer: Rectangle {
implicitHeight: 20
RowLayout {
anchors.fill: parent
Label {
text: "state bar"
}
}
}
/*
.......
*/

ColumnLayout {
anchors.fill: parent
anchors.margins: 5

//VideoWidget is a subclass that I inherited from my QQuickItem implementation to display my images
VideoWidget {
id: videoView
objectName: "videoView"

anchors.left: parent.left
anchors.right: parent.right
anchors.top: appWindow.menuBar.bottom
anchors.bottom: playDrawer.top
}

PlayDrawer {
id: playDrawer

anchors.bottom: appWindow.footer.top
anchors.left: parent.left
anchors.right: parent.right

implicitHeight: 80
}
}
< /code>
The location of the picture displayed is strange.The image is from a frame in the video that should have filled the display area (the black part of the image below) and now it is displayed up, causing the top half of the image to be invisible.
Изображение

The complete picture should look like this.
Изображение

I expect the normal display of the picture is complete, like the following, this is the result after I zoom out, when the height of the window is greater than the width of the window, the display of the picture is normal, otherwise, as I said above, the picture is rendered in a strange position.
Изображение

In addition, when the width is greater than the height (1280x720 resolution), when scaled, the top and bottom of the displayed picture feel obscured by the menu bar and the lower playback control group in the window.
The top and bottom of the picture are obscured
I found that OpenGL seemed to use the entire window as the rendering area when rendering. When I turned on the depth test, the image could be displayed completely, but the window turned into the following.The image appears in front of the window, obscuring other elements in the QML window behind it.
Изображение


Подробнее здесь: https://stackoverflow.com/questions/794 ... ppears-inc
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение
  • Использование OpenGL для визуализации текстуры в пользовательском QQuickItem, текстура выглядит неполной в окне QML
    Anonymous » » в форуме C++
    0 Ответы
    11 Просмотры
    Последнее сообщение Anonymous
  • Как получить доступ к элементам из файла .ui.qml в родительском файле .qml и взаимодействовать с ними? QT-QML
    Anonymous » » в форуме C++
    0 Ответы
    46 Просмотры
    Последнее сообщение Anonymous
  • SFML Sprite чертеж пустой текстура текстура
    Anonymous » » в форуме C++
    0 Ответы
    43 Просмотры
    Последнее сообщение Anonymous
  • Отображение QML в пользовательском окне в PyQt6
    Anonymous » » в форуме Python
    0 Ответы
    21 Просмотры
    Последнее сообщение Anonymous
  • Обработанные изображения OpenGL не могут отображаться в окне QML
    Anonymous » » в форуме C++
    0 Ответы
    4 Просмотры
    Последнее сообщение Anonymous

Вернуться в «C++»