Sophie

Sophie

distrib > Mageia > 7 > armv7hl > media > core-release > by-pkgid > c936229ef0138f42857f36beadbeda30 > files > 608

qt3d5-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" />
<!-- wireframe.qdoc -->
  <title>Qt 3D: Wireframe QML Example | Qt 3D 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="qt3d-index.html">Qt 3D</a></td><td >Qt 3D: Wireframe QML Example</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right"><a href="qt3d-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="#running-the-example">Running the Example</a></li>
<li class="level1"><a href="#creating-entities">Creating Entities</a></li>
<li class="level1"><a href="#specifying-transformations">Specifying Transformations</a></li>
<li class="level1"><a href="#loading-dynamic-per-vertex-data">Loading Dynamic Per-Vertex Data</a></li>
<li class="level1"><a href="#aggregating-components">Aggregating Components</a></li>
<li class="level1"><a href="#rendering-from-cameras">Rendering from Cameras</a></li>
<li class="level1"><a href="#mapping-materials">Mapping Materials</a></li>
<li class="level1"><a href="#using-animation-elements">Using Animation Elements</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">Qt 3D: Wireframe QML Example</h1>
<span class="subtitle"></span>
<!-- $$$wireframe-brief -->
<p>A Qt 3D QML application that implements a single-pass wireframe rendering method.</p>
<!-- @@@wireframe -->
<!-- $$$wireframe-description -->
<div class="descr"> <a name="details"></a>
<p class="centerAlign"><img src="images/qt3d-wireframe-rendering.png" alt="" /></p><p><i>Qt 3D Wireframe Rendering</i> illustrates how to draw a single entity (a trefoil knot) using a custom set of shaders to implement a single-pass wireframe rendering method.</p>
<a name="running-the-example"></a>
<h2 id="running-the-example">Running the Example</h2>
<p>To run the example from Qt Creator, open the <b>Welcome</b> mode and select the example from <b>Examples</b>. For more information, visit Building and Running an Example.</p>
<a name="creating-entities"></a>
<h2 id="creating-entities">Creating Entities</h2>
<p>The renderer aspect looks for entities that have some geometry, a material, and optionally a transformation. These are all specified in the form of subclasses of QComponent that have been exported to the QML engine in the form of <a href="qml-qt3d-render-mesh.html">Mesh</a>, <a href="qml-qt3d-render-material.html">Material</a>, and <a href="qml-qt3d-core-transform.html">Transform</a>. We use these components to create a custom QML item in <i>TrefoilKnot.qml</i>.</p>
<p>We start off by importing the <code>Qt3D 2.0</code> module that provides the <a href="qml-qt3d-core-entity.html">Entity</a> type and value type helpers, such as Qt.vector3d(). We also import the <code>Qt3D.Renderer</code> module that provides the components and other types picked up by the renderer aspect:</p>
<pre class="cpp">

  import Qt3D.Core 2.0
  import Qt3D.Render 2.0

</pre>
<p>To use components from other aspects, we would need to import the corresponding QML module, too.</p>
<p>We then use an <a href="qml-qt3d-core-entity.html">Entity</a> type as the root element of the custom QML type exposing some custom properties just as you would with any other type in QML:</p>
<pre class="cpp">

  Entity {
      id: root

      property real x: 0.0
      property real y: 0.0
      property real z: 0.0
      property real scale: 1.0
      property real theta: 0.0
      property real phi: 0.0
      property Material material

</pre>
<p>In addition to aggregating components, the <a href="qml-qt3d-core-entity.html">Entity</a> type can be used to group child objects together. This is analogous to how the Item type is used in Qt Quick 2.</p>
<a name="specifying-transformations"></a>
<h2 id="specifying-transformations">Specifying Transformations</h2>
<p>We instantiate a <a href="qml-qt3d-core-transform.html">Transform</a> component and a <a href="qml-qt3d-render-mesh.html">Mesh</a> component. The <a href="qml-qt3d-core-transform.html">Transform</a> component specifies how the renderer should transform the geometry when it is drawn with the OpenGL pipeline. We combine an ordered set of transformations into a single <a href="qml-qt3d-core-transform.html">Transform</a> component. This information will be automatically available to our shaders through standard named uniform variables:</p>
<pre class="cpp">

      Transform {
          id: transform
          translation: Qt.vector3d(root.x, root.y, root.z)
          rotation: fromEulerAngles(theta, phi, 0)
          scale: root.scale
      }

</pre>
<a name="loading-dynamic-per-vertex-data"></a>
<h2 id="loading-dynamic-per-vertex-data">Loading Dynamic Per-Vertex Data</h2>
<p>The <a href="qml-qt3d-render-mesh.html">Mesh</a> component is very simple. We use its source property to load in a static set of geometry (such as vertex positions, normal vectors, and texture coordinates) from a file in the Wavefront Obj format. This data was exported from the Blender application.</p>
<pre class="cpp">

      Mesh {
          id: mesh
          source: "assets/obj/trefoil.obj"
      }

</pre>
<p>In addition to the <a href="qml-qt3d-render-mesh.html">Mesh</a> element, Qt 3D also enables dynamic generation of per-vertex attribute data through C++ hooks that are called by the task-based engine.</p>
<a name="aggregating-components"></a>
<h2 id="aggregating-components">Aggregating Components</h2>
<p>Simply instantiating components is not enough, however. In order for them to imbue special behavior on an entity, the entity must aggregate the components by means of its components property:</p>
<pre class="cpp">

      components: [ transform, mesh, root.material ]

</pre>
<p>This allows components to be shared between multiple entities very easily. In this example, we have components for the transform and mesh that are contained within the TrefoilKnot custom type. The final component, of type <a href="qml-qt3d-render-material.html">Material</a>, is provided by a property of the TrefoilKnot custom type. We will later customize the appearance of the entity.</p>
<a name="rendering-from-cameras"></a>
<h2 id="rendering-from-cameras">Rendering from Cameras</h2>
<p>We use the TrefoilKnot custom type in <i>main.qml</i> to draw the trefoil knot on the screen.</p>
<p>We use the same import statements as in <i>TrefoilKnot.qml</i>, with the addition of a namespaced import for the Qt Quick module that we will need for animations:</p>
<pre class="cpp">

  import QtQuick 2.1 as QQ2
  import Qt3D.Core 2.0
  import Qt3D.Render 2.0

</pre>
<p>We use an <a href="qml-qt3d-core-entity.html">Entity</a> type as the root type simply to act as a parent for its children. In this sense, the <a href="qml-qt3d-core-entity.html">Entity</a> type is much like the Item type:</p>
<pre class="cpp">

  import Qt3D.Input 2.0
  import Qt3D.Extras 2.0

  Entity {
      id: root

</pre>
<p>The RendererSettings component uses the <a href="qml-qt3d-extras-forwardrenderer.html">ForwardRenderer</a> type to completely configure the renderer without touching any C++ code:</p>
<pre class="cpp">

      // Render from the mainCamera
      components: [
          RenderSettings {
              activeFrameGraph: ForwardRenderer {
                  id: renderer
                  camera: mainCamera
              }
          },
          // Event Source will be set by the Qt3DQuickWindow
          InputSettings { }
      ]

</pre>
<p>The BasicCamera type is a trivial wrapper around the built-in <a href="qml-qt3d-render-camera.html">Camera</a> type that represents a virtual camera. It has properties for such things as the near and far planes, field of view, aspect ratio, projection type, position, and orientation:</p>
<pre class="cpp">

      BasicCamera {
          id: mainCamera
          position: Qt.vector3d( 0.0, 0.0, 15.0 )
      }

</pre>
<p>The Configuration type provides a temporary workaround for having mouse control of the camera while the proper implementation that uses aspects and components is being completed:</p>
<pre class="cpp">

      FirstPersonCameraController { camera: mainCamera }

</pre>
<p>It is trivial to use multiple cameras and choose between them using the framegraph for all or part of the scene rendering.</p>
<a name="mapping-materials"></a>
<h2 id="mapping-materials">Mapping Materials</h2>
<p>Qt 3D has a robust and very flexible <a href="qt3d-overview.html#materials">material system</a> that allows multiple levels of customization. We use the WireframeMaterial custom type to wrap the <a href="qml-qt3d-render-material.html">Material</a> type:</p>
<pre class="cpp">

      WireframeMaterial {
          id: wireframeMaterial
          effect: WireframeEffect {}
          ambient: Qt.rgba( 0.2, 0.0, 0.0, 1.0 )
          diffuse: Qt.rgba( 0.8, 0.0, 0.0, 1.0 )

</pre>
<p>We then instantiate the TrefoilKnot type and set the material on it:</p>
<pre class="cpp">

      TrefoilKnot {
          id: trefoilKnot
          material: wireframeMaterial
      }

</pre>
<p>The Qt 3D engine in conjunction with the renderer aspect now has enough information to finally render our mesh using the material we specified.</p>
<a name="using-animation-elements"></a>
<h2 id="using-animation-elements">Using Animation Elements</h2>
<p>We use the animation elements provided by Qt Quick 2 to animate the properties of the TrefoilKnot and WireframeMaterial types. The properties of the components of a type are updated by using the QML property binding mechanism:</p>
<pre class="cpp">

          QQ2.SequentialAnimation {
              loops: QQ2.Animation.Infinite
              running: true

              QQ2.NumberAnimation {
                  target: wireframeMaterial;
                  property: "lineWidth";
                  duration: 1000;
                  from: 0.8
                  to: 1.8
              }

              QQ2.NumberAnimation {
                  target: wireframeMaterial;
                  property: "lineWidth";
                  duration: 1000;
                  from: 1.8
                  to: 0.8
              }

              QQ2.PauseAnimation { duration: 1500 }
          }

</pre>
<p>The property updates are noticed by the <a href="qt3dcore-qnode.html">QNode</a> base class and automatically sent through to the corresponding objects in the renderer aspect. The renderer then takes care of translating the property updates to new values for uniform variables in the GLSL shader programs.</p>
<p>Run the example to view the trefoil knot with the width of the wireframe lines pulsing. All the heavy lifting is being done by the GPU. The CPU only has to run the property animations and to translate the scenegraph and framegraph into raw OpenGL calls.</p>
<p>It is also possible to animate on the GPU via a custom shader program and material.</p>
<p>Files:</p>
<ul>
<li><a href="qt3d-wireframe-basiccamera-qml.html">wireframe/BasicCamera.qml</a></li>
<li><a href="qt3d-wireframe-trefoilknot-qml.html">wireframe/TrefoilKnot.qml</a></li>
<li><a href="qt3d-wireframe-wireframeeffect-qml.html">wireframe/WireframeEffect.qml</a></li>
<li><a href="qt3d-wireframe-wireframematerial-qml.html">wireframe/WireframeMaterial.qml</a></li>
<li><a href="qt3d-wireframe-main-cpp.html">wireframe/main.cpp</a></li>
<li><a href="qt3d-wireframe-main-qml.html">wireframe/main.qml</a></li>
<li><a href="qt3d-wireframe-shaders-robustwireframe-geom.html">wireframe/shaders/robustwireframe.geom</a></li>
<li><a href="qt3d-wireframe-shaders-robustwireframe-vert.html">wireframe/shaders/robustwireframe.vert</a></li>
<li><a href="qt3d-wireframe-wireframe-pro.html">wireframe/wireframe.pro</a></li>
<li><a href="qt3d-wireframe-wireframe-qrc.html">wireframe/wireframe.qrc</a></li>
</ul>
</div>
<!-- @@@wireframe -->
        </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>