翻譯|使用教程|編輯:鮑佳佳|2021-07-19 10:58:45.153|閱讀 535 次
概述:本文演示了如何實現將 Qt 3D 渲染與 Qt Quick 2D 元素結合使用的應用程序。該示例顯示了太陽系的八顆行星與太陽。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
Qt組件推薦:
演示結合Qt 3D渲染和Qt Quick 2元素。
本文演示了如何實現 Qt 3D 渲染與 Qt Quick 2D 元素結合使用的應用程序。該示例顯示了太陽系的八顆行星與太陽。
行星紋理貼圖由 James Hastings-Trew 版權所有 (c) //planetpixelemporium.com/planets.html經許可使用。
行星在給定時間根據它們的軌道圍繞太陽旋轉。輪換從 2000 Jan 0.0 UT 開始。行星位置是根據此處找到的公式計算的:http : //www.stjarnhimlen.se/comp/ppcomp.html和//www.davidcolarusso.com/astro/。
要從Qt Creator運行示例,請打開welcome模式并從Examples選擇示例。有關更多信息,請訪問構建和運行示例。
planets-qml/PlanetsMain.qml示例中的 Qt Quick Implementation使用Scene3D類型呈現 3D 內容。
Scene3D { anchors.fill: parent aspects: ["render", "logic", "input"] SolarSystem { id: solarsystem } }
行星相關信息存儲在一個ListModel. 行星的選擇按鈕和信息表是基于模型創建的。2D 元素、選擇按鈕和滑塊在planets-qml/PlanetsMain.qml.
選擇按鈕會更改 的focusedPlanet屬性mainview。隨著屬性的變化,行星信息會更新,并且相機會動畫到新的位置。
onFocusedPlanetChanged: { if (focusedPlanet == 100) { info.opacity = 0 updatePlanetInfo() } else { updatePlanetInfo() info.opacity = 1 } solarsystem.changePlanetFocus(oldPlanet, focusedPlanet) oldPlanet = focusedPlanet }
相機位置和相機觀察點根據 中動畫的值更新planets-qml/SolarSystem.qml,由changePlanetFocus()函數觸發。
QQ2.NumberAnimation { id: lookAtOffsetAnimation target: sceneRoot properties: "xLookAtOffset, yLookAtOffset, zLookAtOffset" to: 0 easing.type: Easing.InOutQuint duration: 1250 } QQ2.NumberAnimation { id: cameraOffsetAnimation target: sceneRoot properties: "xCameraOffset, yCameraOffset, zCameraOffset" to: 0 easing.type: Easing.InOutQuint duration: 2500 }
滑塊用于調整旋轉速度、行星大小和觀看距離。當滑塊值發生變化時,planets-qml/SolarSystem.qml會調用一個 JavaScript 函數來調整給定的屬性。例如,更改觀看距離滑塊的值會調用該changeCameraDistance()方法。
onValueChanged: solarsystem.changeCameraDistance(value)
實現的主要部分,包括行星的運動和旋轉數學,在planets-qml/SolarSystem.qml.
首先,添加 a Camera、 aLight和 a Configuration,然后是Effects 代表行星Materials,最后是行星本身。例如,地球的構造如下:
Entity { id: earthEntity Planet { id: earth tilt: planetData[Planets.EARTH].tilt } PlanetMaterial { id: materialEarth effect: effectDSB ambientLight: ambientStrengthPlanet diffuseMap: "qrc:/images/solarsystemscope/earthmap2k.jpg" specularMap: "qrc:/images/solarsystemscope/earthspec2k.jpg" normalMap: "qrc:/images/solarsystemscope/earthnormal2k.jpg" shininess: shininessSpecularMap } property Transform transformEarth: Transform { matrix: { var m = Qt.matrix4x4() m.translate(Qt.vector3d(earth.x, earth.y, earth.z)) m.rotate(earth.tilt, tiltAxis) m.rotate(earth.roll, rollAxis) m.scale(earth.r) return m } } components: [ earth, materialEarth, transformEarth ] }
移動和旋轉計算等所需的行星數據是planets-qml/planets.js通過loadPlanetData()在組件完成時調用的JavaScript 來構建的。其他初始化,例如將行星插入數組以便于處理,計算土星環和天王星環的環半徑,以及設置默認比例、速度和相機偏移,也已完成:
QQ2.Component.onCompleted: { planetData = Planets.loadPlanetData() // Push in the correct order planets.push(sun) planets.push(mercury) planets.push(venus) planets.push(earth) planets.push(mars) planets.push(jupiter) planets.push(saturn) planets.push(uranus) planets.push(neptune) planets.push(moon) // TODO: Once support for creating meshes from arrays is implemented take these into use //saturnRing.makeRing() //uranusRing.makeRing() saturnRingOuterRadius = planetData[Planets.SATURN].radius + Planets.saturnOuterRadius saturnRingInnerRadius = planetData[Planets.SATURN].radius + 0.006630 uranusRingOuterRadius = planetData[Planets.URANUS].radius + Planets.uranusOuterRadius uranusRingInnerRadius = planetData[Planets.URANUS].radius + 0.002 ready = true changeScale(1200) changeSpeed(0.2) setLookAtOffset(Planets.SUN) }
場景通過調用該animate()函數進行動畫處理。這也是時間提前的地方,計算所有行星的新位置。行星positionPlanet()根據它們的軸向傾斜和它們的恒星自轉周期在函數中旋轉。最后,在updateCamera()函數中計算新的相機位置。
function animate(focusedPlanet) { if (!ready) return advanceTime(focusedPlanet) for (var i = 0; i <= Planets.NUM_SELECTABLE_PLANETS; i++) positionPlanet(i) updateCamera(focusedPlanet) }
====================================================
想要了解或購買Qt正版授權的朋友,歡迎
Qt技術交流交流群開通,QQ搜索群號“765444821”或者掃描二維碼加入
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自: