Sophie

Sophie

distrib > Mageia > 7 > armv7hl > media > core-release > by-pkgid > 0a1223a3e4bb8a61fd0c6bd9fadbd0af > files > 221

qtcanvas3d5-doc-5.12.2-2.mga7.noarch.rpm

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- oneqt.qdoc -->
  <title>One Qt Example | Qt Canvas 3D (deprecated) 5.12.2</title>
  <link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
  <script type="text/javascript">
    document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
    // loading style sheet breaks anchors that were jumped to before
    // so force jumping to anchor again
    setTimeout(function() {
        var anchor = location.hash;
        // need to jump to different anchor first (e.g. none)
        location.hash = "#";
        setTimeout(function() {
            location.hash = anchor;
        }, 0);
    }, 0);
  </script>
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="main">
    <div class="main-rounded">
      <div class="navigationbar">
        <table><tr>
<td >Qt 5.12</td><td ><a href="qtcanvas3d-index.html">Qt Canvas 3D (deprecated)</a></td><td ><a href="qtcanvas3d-examples.html">Qt Canvas 3D Examples</a></td><td >One Qt Example</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right"><a href="qtcanvas3d-index.html">Qt 5.12.2 Reference Documentation</a></td>
        </tr></table>
      </div>
    </div>
<div class="content">
<div class="line">
<div class="content mainContent">
<div class="sidebar">
<div class="toc">
<h3><a name="toc">Contents</a></h3>
<ul>
<li class="level1"><a href="#main-qml-file">Main QML File</a></li>
<li class="level1"><a href="#the-custom-3d-qml-control">The Custom 3D QML Control</a></li>
<li class="level1"><a href="#the-javascript-code">The JavaScript Code</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">One Qt Example</h1>
<span class="subtitle"></span>
<!-- $$$threejs/oneqt-brief -->
<p>Demonstrates combining Qt Quick and three.js rendering.</p>
<!-- @@@threejs/oneqt -->
<!-- $$$threejs/oneqt-description -->
<div class="descr"> <a name="details"></a>
<p>One Qt example demonstrates how to implement a simple QML 3D control that combines the use of <code>three.js</code> library-based Canvas3D rendering with Qt Quick 2D elements. The example shows a view with various benefits of using Qt with related images picked from <a href="http://qt.io">http://qt.io</a>. The images are displayed on the side of a spinning 3D cube that spins to show the correct image when the tabs at the top of the application are selected. You can also use swipe gestures to spin the cube to navigate between the tabs. The 3D cube control has been implemented as a simple QML type that internaly uses <code>three.js</code> library and <a href="qtcanvas3d-index.html">Qt Canvas 3D</a>.</p>
<p class="centerAlign"><img src="images/oneqt-example.png" alt="" /></p><a name="main-qml-file"></a>
<h2 id="main-qml-file">Main QML File</h2>
<p>In <a href="qtcanvas3d-threejs-oneqt-oneqt-qml.html">oneqt.qml</a>, we build the 2D content as normal in QML. Then we add a custom <code>ImageCube</code> type into the scene behind the text elements. This custom type, implemented using <code>three.js</code> library, handles the painting of the 3D cube.</p>
<pre class="qml">

  ImageCube {
      id: imageCube
      width: 512 * (parent.width / 1280)
      height: 512 * (parent.height / 768)
      anchors.bottom: parent.bottom
      anchors.right: parent.right
      ...

</pre>
<p><b>Note: </b>The <code>ImageCube</code> 3D UI component can be created and anchored just like any other QML type.</p><a name="the-custom-3d-qml-control"></a>
<h2 id="the-custom-3d-qml-control">The Custom 3D QML Control</h2>
<p>The <a href="qtcanvas3d-threejs-oneqt-imagecube-qml.html">ImageCube.qml</a> takes six images that it loads and places to the sides of the cube. In addition, the type has a state that defines which of these images are visible and a <code>backgroundColor</code> property that is used when painting the 3D cube. The <code>angleOffset</code> property can be used to adjust the cube's direction when displaying the selected image. In this example the cube component sits on the right edge of the screen so we twist it slightly to the left so that it appears to be facing the rest of the content. This angle is also used by the 3D light so that the light always illuminates the selected face of the cube.</p>
<pre class="qml">

  state: "image6"
  property color backgroundColor: "#FCFCFC"
  property real angleOffset: -180 / 8.0
  property string image1: ""
      ...

</pre>
<p>The custom type defines six states, one for each side of the cube along with the x-, y-, and z-rotations, that must be set to show the face of the cube corresponding to the state.</p>
<pre class="qml">

  states: [
      State {
          name: "image1"
          PropertyChanges { target: cube; xRotation: 0; }
          PropertyChanges { target: cube; yRotation: 180 * 1.5 + angleOffset; }
          PropertyChanges { target: cube; zRotation: 0 }
      },
      ...

</pre>
<p>We use <code>RotationAnimation</code> to animate the transition between angles. It enables us to get smooth transitions between different cube orientations and to always rotate the cube along the shortest possible angle distance.</p>
<pre class="qml">

  transitions: [
      Transition {
          id: turnTransition
          from: "*"
          to: "*"
          RotationAnimation {
              properties: "xRotation,yRotation,zRotation"
              easing.type: Easing.InOutCubic
              direction: RotationAnimation.Shortest
              duration: 450
          }
      }
  ]
      ...

</pre>
<p>We call the JavaScript code that uses <code>three.js</code> to do the rendering of the cube, calling it on the <code>initializeGL</code>, <code>paintGL</code>, and <code>resizeGL</code> signals.</p>
<pre class="qml">

  onInitializeGL: {
      GLCode.initializeGL(cube);
  }

  onPaintGL: {
      GLCode.paintGL(cube);
  }

  onResizeGL: {
      GLCode.resizeGL(cube);
  }

</pre>
<a name="the-javascript-code"></a>
<h2 id="the-javascript-code">The JavaScript Code</h2>
<p>The JavaScript side of the implementation, <a href="qtcanvas3d-threejs-oneqt-imagecube-js.html">imagecube.js</a>, is done using a version of <code>three.js</code> that is ported for <a href="qtcanvas3d-index.html">Qt Canvas 3D</a>: <a href="https://github.com/tronlec/three.js">three.js</a>.</p>
<p>In <a href="qtcanvas3d-threejs-oneqt-imagecube-js.html">imagecube.js</a>, we start by creating the camera and the scene that contains all the rest of the <code>three.js</code> objects.</p>
<pre class="js">

  camera = new THREE.PerspectiveCamera(50, canvas.width / canvas.height, 1, 2000);
  camera.position.z = 400;
  camera.position.y = 140;

  scene = new THREE.Scene();
      ...

</pre>
<p>Then we start the asynchronous loading of the textures and create a material array for the sides of the cube (note that the cube needs 12 materials as each side consists of two triangles).</p>
<pre class="js">

  // Load textures
  var textureLoader = new THREE.TextureLoader();
  var textureCase1 = textureLoader.load(canvas.image1);
  var textureCase2 = textureLoader.load(canvas.image2);
  var textureCase3 = textureLoader.load(canvas.image3);
  var textureCase4 = textureLoader.load(canvas.image4);
  var textureCase5 = textureLoader.load(canvas.image5);
  var textureCase6 = textureLoader.load(canvas.image6);

  // Materials
  var materials = [];
  materials.push(new THREE.MeshLambertMaterial({ map: textureCase1 }));
  materials.push(new THREE.MeshLambertMaterial({ map: textureCase1 }));
      ...

</pre>
<p>We then create the needed geometry as <code>BoxGeometry</code> binding the created materials to the faces of the cube. We then create a <code>MeshFaceMaterial</code> from the array of materials.</p>
<pre class="js">

  var geometry = new THREE.BoxGeometry(200, 200, 200);
  for (var i = 0, len = geometry.faces.length; i < len; i ++) {
      geometry.faces[ i ].materialIndex = i;
  }
  geometry.materials = materials;
  var faceMaterial = new THREE.MeshFaceMaterial(materials);

</pre>
<p>Finally we create the cube mesh from the geometry and material, position it, and add it to the 3D scene.</p>
<pre class="js">

  cube = new THREE.Mesh(geometry, faceMaterial);
  scene.add(cube);

</pre>
<p>Next we create and add some lights to the scene. <code>AmbientLight</code> defines the surrounding light amount and the directional light is positioned so that it highlights the face of the cube that is currently selected.</p>
<pre class="js">

  scene.add(new THREE.AmbientLight(0x444444));

  var directionalLight = new THREE.DirectionalLight(0xffffff, 1.0);

  directionalLight.position.y = 130;
  directionalLight.position.z = 700;
  directionalLight.position.x = Math.tan(canvas.angleOffset) * directionalLight.position.z;
  directionalLight.position.normalize();
  scene.add(directionalLight);

</pre>
<p>Final step in the initialization phase is to create the <code>Canvas3D</code> renderer and set the initial size and clear color (color of the background) to the renderer.</p>
<pre class="js">

  renderer = new THREE.Canvas3DRenderer(
              { canvas: canvas, antialias: true, devicePixelRatio: canvas.devicePixelRatio });
  renderer.setPixelRatio(canvas.devicePixelRatio);
  renderer.setSize(canvas.width, canvas.height);
  setBackgroundColor(canvas.backgroundColor);

</pre>
<p>When we need to render the scene in response to the <code>paintGL</code> signal from Canvas3D, we just copy the current rotation values from the QML side to the cube mesh in the <code>paintGL()</code> method.</p>
<pre class="js">

  function paintGL(canvas) {
      cube.rotation.x = canvas.xRotation * Math.PI / 180;
      cube.rotation.y = canvas.yRotation * Math.PI / 180;
      cube.rotation.z = canvas.zRotation * Math.PI / 180;
      renderer.render(scene, camera);
  }

</pre>
<p>For more information on how to use <code>three.js</code> the documentation is available here: <a href="http://threejs.org/docs/">three.js/docs</a></p>
<p>Files:</p>
<ul>
<li><a href="qtcanvas3d-threejs-oneqt-imagecube-qml.html">threejs/oneqt/ImageCube.qml</a></li>
<li><a href="qtcanvas3d-threejs-oneqt-infosheet-qml.html">threejs/oneqt/InfoSheet.qml</a></li>
<li><a href="qtcanvas3d-threejs-oneqt-navibutton-qml.html">threejs/oneqt/Navibutton.qml</a></li>
<li><a href="qtcanvas3d-threejs-oneqt-swipearea-qml.html">threejs/oneqt/SwipeArea.qml</a></li>
<li><a href="qtcanvas3d-threejs-oneqt-imagecube-js.html">threejs/oneqt/imagecube.js</a></li>
<li><a href="qtcanvas3d-threejs-oneqt-main-cpp.html">threejs/oneqt/main.cpp</a></li>
<li><a href="qtcanvas3d-threejs-oneqt-oneqt-pro.html">threejs/oneqt/oneqt.pro</a></li>
<li><a href="qtcanvas3d-threejs-oneqt-oneqt-qml.html">threejs/oneqt/oneqt.qml</a></li>
<li><a href="qtcanvas3d-threejs-oneqt-oneqt-qrc.html">threejs/oneqt/oneqt.qrc</a></li>
</ul>
<p>Images:</p>
<ul>
<li><a href="images/used-in-examples/threejs/oneqt/ios/OneQtIcon29x29.png">threejs/oneqt/ios/OneQtIcon29x29.png</a></li>
<li><a href="images/used-in-examples/threejs/oneqt/ios/OneQtIcon29x29@2x.png">threejs/oneqt/ios/OneQtIcon29x29@2x.png</a></li>
<li><a href="images/used-in-examples/threejs/oneqt/ios/OneQtIcon29x29@2x~ipad.png">threejs/oneqt/ios/OneQtIcon29x29@2x~ipad.png</a></li>
<li><a href="images/used-in-examples/threejs/oneqt/ios/OneQtIcon29x29~ipad.png">threejs/oneqt/ios/OneQtIcon29x29~ipad.png</a></li>
<li><a href="images/used-in-examples/threejs/oneqt/ios/OneQtIcon40x40@2x.png">threejs/oneqt/ios/OneQtIcon40x40@2x.png</a></li>
<li><a href="images/used-in-examples/threejs/oneqt/ios/OneQtIcon40x40@2x~ipad.png">threejs/oneqt/ios/OneQtIcon40x40@2x~ipad.png</a></li>
<li><a href="images/used-in-examples/threejs/oneqt/ios/OneQtIcon40x40~ipad.png">threejs/oneqt/ios/OneQtIcon40x40~ipad.png</a></li>
<li><a href="images/used-in-examples/threejs/oneqt/ios/OneQtIcon50x50@2x~ipad.png">threejs/oneqt/ios/OneQtIcon50x50@2x~ipad.png</a></li>
<li><a href="images/used-in-examples/threejs/oneqt/ios/OneQtIcon50x50~ipad.png">threejs/oneqt/ios/OneQtIcon50x50~ipad.png</a></li>
<li><a href="images/used-in-examples/threejs/oneqt/ios/OneQtIcon57x57.png">threejs/oneqt/ios/OneQtIcon57x57.png</a></li>
<li><a href="images/used-in-examples/threejs/oneqt/ios/OneQtIcon57x57@2x.png">threejs/oneqt/ios/OneQtIcon57x57@2x.png</a></li>
<li><a href="images/used-in-examples/threejs/oneqt/ios/OneQtIcon60x60@2x.png">threejs/oneqt/ios/OneQtIcon60x60@2x.png</a></li>
<li><a href="images/used-in-examples/threejs/oneqt/ios/OneQtIcon72x72@2x~ipad.png">threejs/oneqt/ios/OneQtIcon72x72@2x~ipad.png</a></li>
<li><a href="images/used-in-examples/threejs/oneqt/ios/OneQtIcon72x72~ipad.png">threejs/oneqt/ios/OneQtIcon72x72~ipad.png</a></li>
<li><a href="images/used-in-examples/threejs/oneqt/ios/OneQtIcon76x76@2x~ipad.png">threejs/oneqt/ios/OneQtIcon76x76@2x~ipad.png</a></li>
<li><a href="images/used-in-examples/threejs/oneqt/ios/OneQtIcon76x76~ipad.png">threejs/oneqt/ios/OneQtIcon76x76~ipad.png</a></li>
<li><a href="images/used-in-examples/threejs/oneqt/textures/dataviz.jpg">threejs/oneqt/textures/dataviz.jpg</a></li>
<li><a href="images/used-in-examples/threejs/oneqt/textures/devices.png">threejs/oneqt/textures/devices.png</a></li>
<li><a href="images/used-in-examples/threejs/oneqt/textures/embedded.png">threejs/oneqt/textures/embedded.png</a></li>
<li><a href="images/used-in-examples/threejs/oneqt/textures/iot.png">threejs/oneqt/textures/iot.png</a></li>
<li><a href="images/used-in-examples/threejs/oneqt/textures/multiscreen.png">threejs/oneqt/textures/multiscreen.png</a></li>
<li><a href="images/used-in-examples/threejs/oneqt/textures/puzzle-pieces.png">threejs/oneqt/textures/puzzle-pieces.png</a></li>
<li><a href="images/used-in-examples/threejs/oneqt/textures/qtlogo.png">threejs/oneqt/textures/qtlogo.png</a></li>
<li><a href="images/used-in-examples/threejs/oneqt/textures/qtlogosmall.png">threejs/oneqt/textures/qtlogosmall.png</a></li>
</ul>
</div>
<!-- @@@threejs/oneqt -->
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2019 The Qt Company Ltd.
   Documentation contributions included herein are the copyrights of
   their respective owners.<br/>    The documentation provided herein is licensed under the terms of the    <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation    License version 1.3</a> as published by the Free Software Foundation.<br/>    Qt and respective logos are trademarks of The Qt Company Ltd.     in Finland and/or other countries worldwide. All other trademarks are property
   of their respective owners. </p>
</div>
</body>
</html>