繁体   English   中英

Qt qml应用下的OpenGL场景

[英]OpenGL scene under Qt qml application

这应该是如何将自定义 opengl 添加到 qml 应用程序的最佳方式。

http://qt-project.org/doc/qt-5/qtquick-scenegraph-openglunderqml-example.html

问题是,我不想在整个窗口上绘画,而只是在我的 opengl 自定义 qt 快速项目占用的矩形中绘画。 我以为我可以使用适当的参数调用 glViewport,所以 opengl 只会绘制我的项目的矩形。

实际上,这对我不起作用。

qml:

import QtQuick 2.2
import QtQuick.Controls 1.1
import ge.components 1.0

ApplicationWindow {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")
    color: "red"

    menuBar: MenuBar {
        Menu {
            title: qsTr("Filxe")
            MenuItem {
                text: qsTr("Exit")
                onTriggered: Qt.quit();
            }
        }
    }

    GLViewer {
        width: 200
        height: 200
        x: 100
        y: 100
    }
}

qt快速项目:在paint方法中,我首先用ApplicationWindow的颜色填充整个窗口,然后我想用黑色填充我的项目占据的矩形。 实际上它用黑色填满了整个窗口,为什么???

#include "glviewer.h"
#include <QQuickWindow>
#include <iostream>
#include <QColor>

using namespace std;

GLViewer::GLViewer(QQuickItem *parent) :
    QQuickItem(parent)
{
    connect(this, SIGNAL(windowChanged(QQuickWindow*)), this, SLOT(handleWindowChanged(QQuickWindow*)));
}

void GLViewer::handleWindowChanged(QQuickWindow *win)
{
    if (win) {
        connect(win, SIGNAL(beforeRendering()), this, SLOT(paint()), Qt::DirectConnection);
        win->setClearBeforeRendering(false);
    }
}

void GLViewer::paint() {

    QColor color = window()->color();
    glClearColor(color.red(), color.green(), color.blue(), color.alpha());
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    cout << "X: " << x() << ", Y: " << y() << ", W: " << width() << ", H: " << height() << endl;

    glViewport(x(), y(), width(), height());
    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);

}

您的代码有两个问题。 首先,ApplicationWindow没有颜色属性,则在设置时

color: "red"

在此组件中,您不会设置任何颜色(即颜色为黑色)。 您可以为GLViewer之前的Rectangle组件添加ApplicationWindow的背景色,如下所示

Rectangle {
    width: parent.width
    height: parent.height
    anchors.centerIn: parent
    color: "red"
}

第二个,您是在主窗口GL上下文中绘制,然后,即使正确设置了视口,也需要以下几行代码

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

将清除整个窗口。 如果您只想清除窗口的一部分,则必须使用glScissor

glViewport(x, y, w, h);

glEnable(GL_SCISSOR_TEST);
glScissor(x,y,w,h);

glClearColor(0, 0, 0, 1);
glClear(GL_COLOR_BUFFER_BIT);

glDisable(GL_SCISSOR_TEST);

您可以在github上找到一个完整的示例(基于您的链接)。

你如何初始化你的 GLViewer ? 也许,你应该在你的 QQuickWindow 中创建一个 QQuickItem 并将这个项目投射到你的 GLViewer

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM