Sophie

Sophie

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

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" />
<!-- jsonmodels.qdoc -->
  <title>JSON Models 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 >JSON Models 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="#json-model-loader">JSON Model Loader</a></li>
<li class="level1"><a href="#loading-the-models">Loading the Models</a></li>
<li class="level1"><a href="#loading-the-textures">Loading the Textures</a></li>
<li class="level1"><a href="#input-handling">Input Handling</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">JSON Models Example</h1>
<span class="subtitle"></span>
<!-- $$$jsonmodels-brief -->
<p>Load and display several JSON models.</p>
<!-- @@@jsonmodels -->
<!-- $$$jsonmodels-description -->
<div class="descr"> <a name="details"></a>
<p>The JSON Models Example demonstrates loading and displaying more than one JSON model and more than one texture. It also implements simple mouse input handling to allow rotating the scene and zooming into it.</p>
<p class="centerAlign"><img src="images/jsonmodels-example.png" alt="" /></p><a name="json-model-loader"></a>
<h2 id="json-model-loader">JSON Model Loader</h2>
<p>First we include a JSON model parser, which handles parsing the JSON models into our internal models:</p>
<pre class="js">

  Qt.include("ThreeJSLoader.js")

</pre>
<p>The <code>ThreeJSLoader.js</code> includes a reimplementation of the JSON parser in <code>three.js</code>, but we will not go into its implementation details.</p>
<a name="loading-the-models"></a>
<h2 id="loading-the-models">Loading the Models</h2>
<p>First we need to initialize all array buffers for the models:</p>
<pre class="js">

  function initBuffers() {
      modelOne.verticesVBO = gl.createBuffer();
      modelOne.verticesVBO.name = "modelOne.verticesVBO";
      modelOne.normalsVBO  = gl.createBuffer();
      modelOne.normalsVBO.name = "modelOne.normalsVBO";
      modelOne.texCoordVBO = gl.createBuffer();
      modelOne.texCoordVBO.name = "modelOne.texCoordVBO";
      modelOne.indexVBO    = gl.createBuffer();
      modelOne.indexVBO.name = "modelOne.indexVBO";

      modelTwo.verticesVBO = gl.createBuffer();
      modelTwo.verticesVBO.name = "modelTwo.verticesVBO";
      modelTwo.normalsVBO  = gl.createBuffer();
      modelTwo.normalsVBO.name = "modelTwo.normalsVBO";
      modelTwo.texCoordVBO = gl.createBuffer();
      modelTwo.texCoordVBO.name = "modelTwo.texCoordVBO";
      modelTwo.indexVBO    = gl.createBuffer();
      modelTwo.indexVBO.name = "modelTwo.indexVBO";
      ...

</pre>
<p>Then we request the models to be loaded:</p>
<pre class="js">

  function loadJSONModels() {
      // Load the first model
      var request = new XMLHttpRequest();
      request.open("GET", "gold.json");
      request.onreadystatechange = function () {
          if (request.readyState === XMLHttpRequest.DONE) {
              handleLoadedModel(JSON.parse(request.responseText));
          }
      }
      request.send();
      log("   XMLHttpRequest sent for model one")

      // Load the second model
      var request2 = new XMLHttpRequest();
      request2.open("GET", "woodbox.json");
      request2.onreadystatechange = function () {
          if (request2.readyState === XMLHttpRequest.DONE) {
              handleLoadedModel(JSON.parse(request2.responseText));
          }
      }
      request2.send();
      log("   XMLHttpRequest sent for model two")
      ...

</pre>
<p>Then, when the load requests return, we handle the models:</p>
<pre class="js">

  function handleLoadedModel(jsonObj) {
      log("handleLoadedModel...");
      var modelData = parseJSON3DModel(jsonObj, "");

      if (modelOne.count === 0)
          fillModel(modelData, modelOne);
      else if (modelTwo.count === 0)
          fillModel(modelData, modelTwo);
      ...

</pre>
<p>Each buffer is bound and filled with the data parsed from the json models:</p>
<pre class="js">

  function fillModel(modelData, model) {
      log("   fillModel...");
      log("   "+model.verticesVBO.name);
      gl.bindBuffer(gl.ARRAY_BUFFER, model.verticesVBO);
      gl.bufferData(gl.ARRAY_BUFFER,
                    new Float32Array(modelData.vertices),
                    gl.STATIC_DRAW);
      log("   "+model.normalsVBO.name);
      if (isLogEnabled && stateDumpExt)
          log("GL STATE DUMP:\n"+stateDumpExt.getGLStateDump(stateDumpExt.DUMP_VERTEX_ATTRIB_ARRAYS_BIT || stateDumpExt.DUMP_VERTEX_ATTRIB_ARRAYS_CONTENTS_BIT));

      gl.bindBuffer(gl.ARRAY_BUFFER, model.normalsVBO);
      gl.bufferData(gl.ARRAY_BUFFER,
                    new Float32Array(modelData.normals),
                    gl.STATIC_DRAW);

      log("   "+model.texCoordVBO.name);
      gl.bindBuffer(gl.ARRAY_BUFFER, model.texCoordVBO);
      gl.bufferData(gl.ARRAY_BUFFER,
                    new Float32Array(modelData.texCoords[0]),
                    gl.STATIC_DRAW);

      log("   "+model.indexVBO.name);
      gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, model.indexVBO);
      gl.bufferData(gl.ELEMENT_ARRAY_BUFFER,
                    new Uint16Array(modelData.indices),
                    gl.STATIC_DRAW);

      model.count = modelData.indices.length;
      log("   ...fillModel");
  }

</pre>
<a name="loading-the-textures"></a>
<h2 id="loading-the-textures">Loading the Textures</h2>
<p>First we create the TextureImage objects for each of the images we are going to load and register handlers for the <code>imageLoaded</code> and <code>imageLoadingFailed</code> signals. In the <code>imageLoaded</code> signal handlers we create the OpenGL textures:</p>
<pre class="js">

  function loadTextures() {
      // Load the first texture
      var goldImage = TextureImageFactory.newTexImage();
      goldImage.name = "goldImage";
      goldImage.imageLoaded.connect(function() {
          log("    creating model one texture");
          modelOneTexture = gl.createTexture();
          modelOneTexture.name = "modelOneTexture";
          gl.bindTexture(gl.TEXTURE_2D, modelOneTexture);
          gl.texImage2D(gl.TEXTURE_2D,    // target
                        0,                // level
                        gl.RGBA,          // internalformat
                        gl.RGBA,          // format
                        gl.UNSIGNED_BYTE, // type
                        goldImage);       // pixels
          gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
          gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_NEAREST);
          gl.generateMipmap(gl.TEXTURE_2D);
      });
      goldImage.imageLoadingFailed.connect(function() {
          console.log("Texture load FAILED, "+goldImage.errorString);
      });
      goldImage.src = "qrc:///gold.jpg";
      log("   texture one source set")

      // Load the second texture
      var woodBoxImage = TextureImageFactory.newTexImage();
      woodBoxImage.name = "woodBoxImage";
      woodBoxImage.imageLoaded.connect(function() {
          log("    creating model two texture");
          modelTwoTexture = gl.createTexture();
          modelTwoTexture.name = "modelTwoTexture";
          gl.bindTexture(gl.TEXTURE_2D, modelTwoTexture);
          gl.texImage2D(gl.TEXTURE_2D,    // target
                        0,                // level
                        gl.RGBA,          // internalformat
                        gl.RGBA,          // format
                        gl.UNSIGNED_BYTE, // type
                        woodBoxImage);    // pixels
          gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
          gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_NEAREST);
          gl.generateMipmap(gl.TEXTURE_2D);
      });
      woodBoxImage.imageLoadingFailed.connect(function() {
          console.log("Texture load FAILED, "+woodBoxImage.errorString);
      });
      woodBoxImage.src = "qrc:///woodbox.jpg";
      log("   texture two source set")
      ...

</pre>
<a name="input-handling"></a>
<h2 id="input-handling">Input Handling</h2>
<p>First we add a MouseArea to fill the Canvas3D:</p>
<pre class="qml">

  MouseArea {
      anchors.fill: parent
      ...

</pre>
<p>Before adding some functionality on it, we need to add properties to the canvas with initial values set:</p>
<pre class="qml">

  property double xRot: 0.0
  property double yRot: 45.0
  property double distance: 2.0

</pre>
<p>After that, we add rotation on mouse movement when the left mouse button is pressed:</p>
<pre class="qml">

  onMouseXChanged: {
      // Do not rotate if we don't have previous value
      if (previousY !== 0)
          canvas3d.yRot += mouseY - previousY
      previousY = mouseY
      // Limit the rotation to -90...90 degrees
      if (canvas3d.yRot > 90)
          canvas3d.yRot = 90
      if (canvas3d.yRot < -90)
          canvas3d.yRot = -90
  }
  onMouseYChanged: {
      // Do not rotate if we don't have previous value
      if (previousX !== 0)
          canvas3d.xRot += mouseX - previousX
      previousX = mouseX
      // Wrap the rotation around
      if (canvas3d.xRot > 180)
          canvas3d.xRot -= 360
      if (canvas3d.xRot < -180)
          canvas3d.xRot += 360
  }
  onReleased: {
      // Reset previous mouse positions to avoid rotation jumping
      previousX = 0
      previousY = 0
  }

</pre>
<p>We need to keep the previous x and y values to avoid rotation jumping when the mouse button is released and pressed again. We store them in these properties:</p>
<pre class="qml">

  property int previousY: 0
  property int previousX: 0

</pre>
<p>Then we add zooming by mouse wheel:</p>
<pre class="qml">

  onWheel: {
      canvas3d.distance -= wheel.angleDelta.y / 1000.0
      // Limit the distance to 0.5...10
      if (canvas3d.distance < 0.5)
          canvas3d.distance = 0.5
      if (canvas3d.distance > 10)
          canvas3d.distance = 10
  }

</pre>
<p>These properties are then used in the JavaScript side when calculating eye/camera movement:</p>
<pre class="js">

  // Get the view matrix
  mat4.identity(vMatrix);
  eye = moveEye(canvas.xRot, canvas.yRot, canvas.distance);
  mat4.lookAt(vMatrix, eye, [0, 0, 0], [0, 1, 0]);

</pre>
<p>Converting the rotation values into movement is done as follows:</p>
<pre class="js">

  function moveEye(xRot, yRot, distance) {
      var xAngle = degToRad(xRot);
      var yAngle = degToRad(yRot);

      var zPos = distance * Math.cos(xAngle) * Math.cos(yAngle);
      var xPos = distance * Math.sin(xAngle) * Math.cos(yAngle);
      var yPos = distance * Math.sin(yAngle);

      return [-xPos, yPos, zPos];
  }

</pre>
<p>Files:</p>
<ul>
<li><a href="qtcanvas3d-jsonmodels-jsonmodels-pro.html">jsonmodels/jsonmodels.pro</a></li>
<li><a href="qtcanvas3d-jsonmodels-main-cpp.html">jsonmodels/main.cpp</a></li>
<li><a href="qtcanvas3d-jsonmodels-qml-qrc.html">jsonmodels/qml.qrc</a></li>
<li><a href="qtcanvas3d-jsonmodels-qml-jsonmodels-jsonmodels-js.html">jsonmodels/qml/jsonmodels/jsonmodels.js</a></li>
<li><a href="qtcanvas3d-jsonmodels-qml-jsonmodels-jsonmodels-qml.html">jsonmodels/qml/jsonmodels/jsonmodels.qml</a></li>
</ul>
<p>Images:</p>
<ul>
<li><a href="images/used-in-examples/jsonmodels/qml/jsonmodels/bush.png">jsonmodels/qml/jsonmodels/bush.png</a></li>
<li><a href="images/used-in-examples/jsonmodels/qml/jsonmodels/gold.jpg">jsonmodels/qml/jsonmodels/gold.jpg</a></li>
<li><a href="images/used-in-examples/jsonmodels/qml/jsonmodels/pallet.jpg">jsonmodels/qml/jsonmodels/pallet.jpg</a></li>
<li><a href="images/used-in-examples/jsonmodels/qml/jsonmodels/rock.jpg">jsonmodels/qml/jsonmodels/rock.jpg</a></li>
<li><a href="images/used-in-examples/jsonmodels/qml/jsonmodels/woodbox.jpg">jsonmodels/qml/jsonmodels/woodbox.jpg</a></li>
</ul>
</div>
<!-- @@@jsonmodels -->
        </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>