简体   繁体   English

Qt qml应用下的OpenGL场景

[英]OpenGL scene under Qt qml application

This should be the best way how to add custom opengl into qml app.这应该是如何将自定义 opengl 添加到 qml 应用程序的最佳方式。

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

The problem is, i dont want to paint over the whole window, but just in rectangle that is occupied by my opengl custom qt quick item.问题是,我不想在整个窗口上绘画,而只是在我的 opengl 自定义 qt 快速项目占用的矩形中绘画。 I thought i can just call glViewport with appropriate parameters, so opengl will paint just rectangle of my item.我以为我可以使用适当的参数调用 glViewport,所以 opengl 只会绘制我的项目的矩形。

Actually, this doesn't work for me.实际上,这对我不起作用。

qml: 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 quick item: In paint method, i first fill whole window with color of ApplicationWindow and then i want to fill just rectangle occupied by my item with black color. qt快速项目:在paint方法中,我首先用ApplicationWindow的颜色填充整个窗口,然后我想用黑色填充我的项目占据的矩形。 Actually it fill the whole window with black, why???实际上它用黑色填满了整个窗口,为什么???

#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);

}

There are two issues with your code. 您的代码有两个问题。 First one, ApplicationWindow has no color attribute then when you set 首先,ApplicationWindow没有颜色属性,则在设置时

color: "red"

in this component you don't set any color (that is color is black). 在此组件中,您不会设置任何颜色(即颜色为黑色)。 You can set a background color for your ApplicationWindow adding a Rectangle component before your GLViewer as follow 您可以为GLViewer之前的Rectangle组件添加ApplicationWindow的背景色,如下所示

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

Second one, you are drawing in main windows GL context then, even if viewport is correctly setted, the following lines of code 第二个,您是在主窗口GL上下文中绘制,然后,即使正确设置了视口,也需要以下几行代码

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

will clear the entire window. 将清除整个窗口。 If you want to clear only a part of the window you have to use glScissor 如果您只想清除窗口的一部分,则必须使用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);

You can find a complete example (based on your link) on github . 您可以在github上找到一个完整的示例(基于您的链接)。

How do you initialize your GLViewer ?你如何初始化你的 GLViewer ? Maybe, you should create a QQuickItem inside your QQuickWindow and cast this item to your GLViewer也许,你应该在你的 QQuickWindow 中创建一个 QQuickItem 并将这个项目投射到你的 GLViewer

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

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