| 1 |
// Copyright (C) 2017 The Qt Company Ltd. |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only |
| 3 |
|
| 4 |
/*! |
| 5 |
\example planets-qml |
| 6 |
\title Qt 3D: Planets QML Example |
| 7 |
\ingroup qt3d-examples-qml |
| 8 |
\brief Demonstrates combining Qt 3D rendering and Qt Quick 2 elements. |
| 9 |
|
| 10 |
\image planets-qml-example.jpg |
| 11 |
|
| 12 |
\e Planets demonstrates how to implement an application that combines the use of |
| 13 |
Qt 3D rendering with Qt Quick 2D elements. The example shows the eight planets of our Solar |
| 14 |
System with the Sun. |
| 15 |
|
| 16 |
Planet texture maps are Copyright (c) by James Hastings-Trew |
| 17 |
\l{http://planetpixelemporium.com/planets.html}{http://planetpixelemporium.com/planets.html} |
| 18 |
used with permission. |
| 19 |
|
| 20 |
The planets are rotating around the Sun based on their orbit at a given time. The rotation |
| 21 |
starts at 2000 Jan 0.0 UT. The planet positions are calculated based on the formulas found here: |
| 22 |
\l {http://www.stjarnhimlen.se/comp/ppcomp.html}{http://www.stjarnhimlen.se/comp/ppcomp.html} |
| 23 |
and \l {http://www.davidcolarusso.com/astro/}{http://www.davidcolarusso.com/astro/}. |
| 24 |
|
| 25 |
\include examples-run.qdocinc |
| 26 |
|
| 27 |
\section1 Qt Quick 2D Implementation |
| 28 |
|
| 29 |
The Qt Quick Implementation in \c {planets-qml/PlanetsMain.qml} of the |
| 30 |
example renders the 3D content using the \c Scene3D type. |
| 31 |
|
| 32 |
\snippet planets-qml/PlanetsMain.qml 0 |
| 33 |
|
| 34 |
The planet related information is stored into a \c{ListModel}. The selection buttons for the |
| 35 |
planets and the information sheet are created based on the model. The 2D elements, selection |
| 36 |
buttons and sliders, are implemented in \c {planets-qml/PlanetsMain.qml}. |
| 37 |
|
| 38 |
The selection buttons change the \c{focusedPlanet} property of the \c{mainview}. As the property |
| 39 |
changes, the planet information is updated, and the camera is animated to the new position. |
| 40 |
|
| 41 |
\snippet planets-qml/PlanetsMain.qml 1 |
| 42 |
|
| 43 |
The camera position and the camera look at point are updated based on values that are animated |
| 44 |
in \c {planets-qml/SolarSystem.qml}, triggered from the |
| 45 |
\c{changePlanetFocus()} function. |
| 46 |
|
| 47 |
\snippet planets-qml/SolarSystem.qml 0 |
| 48 |
|
| 49 |
The sliders are used to adjust the rotation speed, the planet size, and the viewing distance. |
| 50 |
When a slider value changes, a JavaScript function in \c {planets-qml/SolarSystem.qml} |
| 51 |
is called to adjust the given property. For example, changing the value of |
| 52 |
the \e{Viewing Distance} slider calls the \c{changeCameraDistance()} method. |
| 53 |
|
| 54 |
\snippet planets-qml/PlanetsMain.qml 2 |
| 55 |
|
| 56 |
\section1 Qt 3D Implementation |
| 57 |
|
| 58 |
The main part of the implementation, including the movement and rotation maths for the planets, |
| 59 |
is done in \c {planets-qml/SolarSystem.qml}. |
| 60 |
|
| 61 |
First, a \c Camera, a \c{Light}, and a \c Configuration are added, followed by \c{Effect}s for |
| 62 |
the planet \c{Material}s, and finally the planets themselves. For example, Earth is constructed |
| 63 |
as follows: |
| 64 |
|
| 65 |
\snippet planets-qml/SolarSystem.qml 1 |
| 66 |
|
| 67 |
Planet data, which is needed for the movement and rotation calculations, among other things, is |
| 68 |
constructed with JavaScript in \c {planets-qml/planets.js} by calling |
| 69 |
\c{loadPlanetData()} as the component completes. Other initializations, such as inserting the |
| 70 |
planets into an array for easier handling, calculating the ring radii for Saturn and Uranus |
| 71 |
rings, and setting the default scale, speed, and camera offset, are done as well: |
| 72 |
|
| 73 |
\snippet planets-qml/SolarSystem.qml 2 |
| 74 |
|
| 75 |
The scene is animated by calling the \c{animate()} function. That is also the place where the |
| 76 |
time is advanced, and the new positions for all of the planets are calculated. The planets are |
| 77 |
rotated in the \c{positionPlanet()} function based on their axial tilt and their sidereal |
| 78 |
rotation period. Finally, the new camera position is calculated in the \c{updateCamera()} |
| 79 |
function. |
| 80 |
|
| 81 |
\snippet planets-qml/SolarSystem.qml 3 |
| 82 |
*/ |