日本黄色一级经典视频|伊人久久精品视频|亚洲黄色色周成人视频九九九|av免费网址黄色小短片|黄色Av无码亚洲成年人|亚洲1区2区3区无码|真人黄片免费观看|无码一级小说欧美日免费三级|日韩中文字幕91在线看|精品久久久无码中文字幕边打电话

當(dāng)前位置:首頁(yè) > 芯聞號(hào) > 充電吧
[導(dǎo)讀]Qt Canvas 3D是Qt 基于WebGL的3D內(nèi)容運(yùn)行環(huán)境。由于QtQuick本身就是通過(guò)類(lèi)js引擎執(zhí)行,而且渲染層面基于opengl技術(shù)。故結(jié)合webgL和Qtquick的優(yōu)勢(shì),利用js高效的

Qt Canvas 3D是Qt 基于WebGL的3D內(nèi)容運(yùn)行環(huán)境。由于QtQuick本身就是通過(guò)類(lèi)js引擎執(zhí)行,而且渲染層面基于opengl技術(shù)。故結(jié)合webgL和Qtquick的優(yōu)勢(shì),利用js高效的特點(diǎn),給Qtquick增加了3d功能。而且Qt Canvas 3D還可以利用成熟的web 3d框架,如three.js、gl-matrix.js等,大大方便了利用QtQuick編寫(xiě)3d內(nèi)容。Qt Canvas 3D是由官方支持,比Qt3D模塊較成熟。至于兩者性能上的差異尚待對(duì)比。


最新版QtCreater支持在創(chuàng)建項(xiàng)目時(shí)指定Qt Canvas 3D應(yīng)用,下面我們通過(guò)該方式創(chuàng)建一個(gè)默認(rèn)應(yīng)用,學(xué)習(xí)其結(jié)構(gòu)組成。通過(guò)QtCreater創(chuàng)建一個(gè)使用three.js的應(yīng)用。


main.cpp

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

????QQmlApplicationEngine?engine;
????engine.load(QUrl(QStringLiteral("qrc:/main.qml")));

????return?app.exec();
}


從上面可以看出,Canvas 3D應(yīng)用和普通qml應(yīng)用對(duì)于c++后端要求一致,沒(méi)有額外內(nèi)容,由下面qml代碼可知,Canvas3D即一個(gè)普通的QtQuick Item。

main.qml

import?QtQuick?2.4
import?QtCanvas3D?1.0
import?QtQuick.Window?2.2

import?"glcode.js"?as?GLCode

Window?{
????title:?qsTr("untitled1")
????width:?1280
????height:?768
????visible:?true

????Canvas3D?{
????????id:?canvas3d
????????anchors.fill:?parent
????????focus:?true

????????onInitializeGL:?{
????????????GLCode.initializeGL(canvas3d);
????????}

????????onPaintGL:?{
????????????GLCode.paintGL(canvas3d);
????????}

????????onResizeGL:?{
????????????GLCode.resizeGL(canvas3d);
????????}
????}
}

上述代碼通過(guò)Canvas3D元素定義了3d場(chǎng)景,當(dāng)場(chǎng)景初始化后會(huì)發(fā)送?initializeGL 信號(hào),從而執(zhí)on?initializeGL 槽。實(shí)際的控制邏輯寫(xiě)在了glcode.js中,通過(guò)槽函數(shù)將要操作的場(chǎng)景對(duì)象canvas3d傳給js代碼。

glcode.js首先導(dǎo)入three.js,然后實(shí)現(xiàn)了main.qml中對(duì)應(yīng)的3個(gè)槽對(duì)應(yīng)的函數(shù)功能。其語(yǔ)法和普通的three.js應(yīng)用區(qū)別不大,故一個(gè)基于html的three.js應(yīng)用可以很方便的移植到QtQuick中。


The?Canvas3D?is a QML element that, when placed in your Qt Quick 2 scene, allows you to get a 3D rendering context and call 3D rendering API calls through that context object. Use of the rendering API requires knowledge of OpenGL-like rendering APIs.

There are two functions that are called by the?Canvas3D?implementation:

initializeGL?is emitted before the first frame is rendered, and usually during that you get the 3D context and initialize resources to be used later on during the rendering cycle.

paintGL?is emitted for each frame to be rendered, and usually during that you submit 3D rendering calls to draw whatever 3D content you want to be displayed.

邏輯代碼

Qt.include("three.js")

var?camera,?scene,?renderer;
var?cube;

function?initializeGL(canvas)?{
????scene?=?new?THREE.Scene();
????//custom?begain
????camera?=?new?THREE.PerspectiveCamera(75,?canvas.width?/?canvas.height,?0.1,?1000);
????camera.position.z?=?5;

????var?material?=?new?THREE.MeshBasicMaterial({?color:?0x80c342,
???????????????????????????????????????????????????ambient:?0x000000,
???????????????????????????????????????????????????shading:?THREE.SmoothShading?});
????var?cubeGeometry?=?new?THREE.BoxGeometry(1,?1,?1);
????cube?=?new?THREE.Mesh(cubeGeometry,?material);
????cube.rotation.set(0.785,?0.785,?0.0);
????scene.add(cube);

????//custom?end
????renderer?=?new?THREE.Canvas3DRenderer(
????????????????{?canvas:?canvas,?antialias:?true,?devicePixelRatio:?canvas.devicePixelRatio?});
????renderer.setSize(canvas.width,?canvas.height);
}

function?resizeGL(canvas)?{
????camera.aspect?=?canvas.width?/?canvas.height;
????camera.updateProjectionMatrix();

????renderer.setPixelRatio(canvas.devicePixelRatio);
????renderer.setSize(canvas.width,?canvas.height);
}

function?paintGL(canvas)?{
????renderer.render(scene,?camera);
}

從上述js代碼可以看出,resizeGL和paintGL一般默認(rèn)即可,不需要改動(dòng),需要我們編寫(xiě)的是initializeGL中//custom begain 和//custom end 之間的內(nèi)容。

本站聲明: 本文章由作者或相關(guān)機(jī)構(gòu)授權(quán)發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點(diǎn),本站亦不保證或承諾內(nèi)容真實(shí)性等。需要轉(zhuǎn)載請(qǐng)聯(lián)系該專(zhuān)欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請(qǐng)及時(shí)聯(lián)系本站刪除( 郵箱:macysun@21ic.com )。
換一批
延伸閱讀
關(guān)閉