一个在Ubuntu平台的opengl的例子

本文介绍如何将OpenGL与QML界面结合,通过移植例程SceneGraph-OpenGLUnderQML,在Ubuntu手机平台上实现更强的渲染效果。通过采用QtQuickAppwithQMLUI(qmake)模版,轻松集成C++代码,并展示OpenGL与QML的完美融合。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

OpenGL有很强的渲染效果.我们可以把QML界面和OpenGL结合起来得到更强的渲染效果.在今天的例子中,我们仿照例程"Scene Graph - OpenGL Under QML",并把它移植到我们的Ubuntu手机平台中来.


为了移植的方便,我们采用了我们Ubuntu SDK中的"QtQuick App with QML UI (qmake)"模版.




这个模版的好处是可以很容把我们所需要的C++代码加入到我们的应用中去.我们需要把我们的C++代码加入到我们的.pro文件中:


SOURCES += main.cpp squircle.cpp
HEADERS += squircle.h

另外:

main.cpp


#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQuickView>

#include "squircle.h"

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);

    qmlRegisterType<Squircle>("OpenGLUnderQML", 1, 0, "Squircle");

    QQuickView view;
    view.setSource(QUrl(QStringLiteral("qrc:///Main.qml")));
    view.setResizeMode(QQuickView::SizeRootObjectToView);
    view.show();
    return app.exec();
}

这里,我们把"Squircle"进行注册,并在我们的QML文件中可以实例化.


Main.qml


import QtQuick 2.0
import Ubuntu.Components 1.1

import OpenGLUnderQML 1.0

Item {
    width: 320
    height: 480

    Squircle {
        SequentialAnimation on t {
            NumberAnimation { to: 1; duration: 2500; easing.type: Easing.InQuad }
            NumberAnimation { to: 0; duration: 2500; easing.type: Easing.OutQuad }
            loops: Animation.Infinite
            running: true
        }
    }

    //! [1] //! [2]
    Rectangle {
        color: Qt.rgba(1, 1, 1, 0.7)
        radius: 10
        border.width: 1
        border.color: "white"
        anchors.fill: label
        anchors.margins: -10
    }

    Text {
        id: label
        color: "black"
        wrapMode: Text.WordWrap
        text: "The background here is a squircle rendered with raw OpenGL using the 'beforeRender()' signal in QQuickWindow. This text label and its border is rendered using QML"
        anchors.right: parent.right
        anchors.left: parent.left
        anchors.bottom: parent.bottom
        anchors.margins: 20
    }
}

注意,我们不能再使用MainView及Page了,否则我们不能正确地显示我们的画面.

运行我们的应用:




从上面我们可以看出来,OpenGL可以和QML完美地结合在一起.


整个项目的源码在:https://github.com/liu-xiao-guo/openglqmlexample

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值