gpt4 book ai didi

c++ - Qt qml应用下的OpenGL场景

转载 作者:搜寻专家 更新时间:2023-10-31 01:04:26 24 4
gpt4 key购买 nike

这应该是将自定义 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"

在这个组件中你没有设置任何颜色(即颜色为黑色)。您可以为 ApplicationWindow 设置背景颜色,在 GLViewer 之前添加一个 Rectangle 组件,如下所示

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

第二,您在主窗口 GL 上下文中绘制,即使正确设置了视口(viewport),以下代码行

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 上找到完整示例(基于您的链接) .

关于c++ - Qt qml应用下的OpenGL场景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23968598/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com