<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <!-- basicshapes.qdoc --> <title>Qt 3D: Basic Shapes C++ Example | Qt 3D 5.9</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.9</td><td ><a href="qt3d-index.html">Qt 3D</a></td><td >Qt 3D: Basic Shapes C++ Example</td></tr></table><table class="buildversion"><tr> <td id="buildversion" width="100%" align="right">Qt 5.9.4 Reference Documentation</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="#setting-up-a-torus-mesh">Setting Up a Torus Mesh</a></li> </ul> </div> <div class="sidebar-content" id="sidebar-content"></div></div> <h1 class="title">Qt 3D: Basic Shapes C++ Example</h1> <span class="subtitle"></span> <!-- $$$basicshapes-cpp-description --> <div class="descr"> <a name="details"></a> <p class="centerAlign"><img src="images/basicshapes-cpp-example.jpg" alt="" /></p><p><i>Basic Shapes</i> shows four basic shapes that Qt 3D offers: a torus, a cylinder, a cube, and a sphere. The example also shows how to embed a Qt 3D scene into a widget and connect with other widgets.</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="setting-up-a-torus-mesh"></a> <h2 id="setting-up-a-torus-mesh">Setting Up a Torus Mesh</h2> <p>As an example, we go through how to set up a torus mesh. First, we instantiate the <code>QTorusMesh</code>, and then we set the mesh specific parameters, which for torus are radius, minor radius, and the number of rings and slices.</p> <pre class="cpp"> m_torus <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qt3dextras-module.html">Qt3DExtras</a></span><span class="operator">::</span><span class="type">QTorusMesh</span>(); m_torus<span class="operator">-</span><span class="operator">></span>setRadius(<span class="number">1.0f</span>); m_torus<span class="operator">-</span><span class="operator">></span>setMinorRadius(<span class="number">0.4f</span>); m_torus<span class="operator">-</span><span class="operator">></span>setRings(<span class="number">100</span>); m_torus<span class="operator">-</span><span class="operator">></span>setSlices(<span class="number">20</span>); </pre> <p>The size and position of the torus can be adjusted with transform components. We create scale, translation, and rotation components and add them into the <code>QTransform</code> component.</p> <pre class="cpp"> <span class="type"><a href="qt3dcore-module.html">Qt3DCore</a></span><span class="operator">::</span><span class="type">QTransform</span> <span class="operator">*</span>torusTransform <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qt3dcore-module.html">Qt3DCore</a></span><span class="operator">::</span><span class="type">QTransform</span>(); torusTransform<span class="operator">-</span><span class="operator">></span>setScale(<span class="number">2.0f</span>); torusTransform<span class="operator">-</span><span class="operator">></span>setRotation(<span class="type">QQuaternion</span><span class="operator">::</span>fromAxisAndAngle(QVector3D(<span class="number">0.0f</span><span class="operator">,</span> <span class="number">1.0f</span><span class="operator">,</span> <span class="number">0.0f</span>)<span class="operator">,</span> <span class="number">25.0f</span>)); torusTransform<span class="operator">-</span><span class="operator">></span>setTranslation(QVector3D(<span class="number">5.0f</span><span class="operator">,</span> <span class="number">4.0f</span><span class="operator">,</span> <span class="number">0.0f</span>)); </pre> <p>To change the diffuse color of the mesh, we create a <code>QPhongMaterial</code> and set its diffuse color.</p> <pre class="cpp"> <span class="type"><a href="qt3dextras-module.html">Qt3DExtras</a></span><span class="operator">::</span><span class="type">QPhongMaterial</span> <span class="operator">*</span>torusMaterial <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qt3dextras-module.html">Qt3DExtras</a></span><span class="operator">::</span><span class="type">QPhongMaterial</span>(); torusMaterial<span class="operator">-</span><span class="operator">></span>setDiffuse(<span class="type">QColor</span>(<span class="type">QRgb</span>(<span class="number">0xbeb32b</span>))); </pre> <p>The final step is to add the torus into an entity tree, and we do that by creating a <code>QEntity</code> with a parent entity and adding the previously created mesh, material, and transform components into it.</p> <pre class="cpp"> m_torusEntity <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qt3dcore-module.html">Qt3DCore</a></span><span class="operator">::</span><span class="type">QEntity</span>(m_rootEntity); m_torusEntity<span class="operator">-</span><span class="operator">></span>addComponent(m_torus); m_torusEntity<span class="operator">-</span><span class="operator">></span>addComponent(torusMaterial); m_torusEntity<span class="operator">-</span><span class="operator">></span>addComponent(torusTransform); </pre> <p>We can control the visibility of the entity by defining whether it has a parent or not. That is, whether it is part of an entity tree or not.</p> <pre class="cpp"> <span class="type">void</span> SceneModifier<span class="operator">::</span>enableTorus(bool enabled) { m_torusEntity<span class="operator">-</span><span class="operator">></span>setEnabled(enabled); } </pre> <p>Files:</p> <ul> <li><a href="qt3d-basicshapes-cpp-scenemodifier-cpp.html">basicshapes-cpp/scenemodifier.cpp</a></li> <li><a href="qt3d-basicshapes-cpp-scenemodifier-h.html">basicshapes-cpp/scenemodifier.h</a></li> <li><a href="qt3d-basicshapes-cpp-main-cpp.html">basicshapes-cpp/main.cpp</a></li> <li><a href="qt3d-basicshapes-cpp-basicshapes-cpp-pro.html">basicshapes-cpp/basicshapes-cpp.pro</a></li> </ul> </div> <!-- @@@basicshapes-cpp --> </div> </div> </div> </div> </div> <div class="footer"> <p> <acronym title="Copyright">©</acronym> 2017 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>