Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to render a triangle using Qt3D
QtWS25 Last Chance

How to render a triangle using Qt3D

Scheduled Pinned Locked Moved Unsolved General and Desktop
c++triangleqt3d
7 Posts 3 Posters 5.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    cjsb
    wrote on 27 Jul 2016, 19:24 last edited by
    #1

    Hi,

    how can I render a triangle using Qt3D (C++) without loading a obj file?

    ? 1 Reply Last reply 27 Jul 2016, 19:52
    0
    • C cjsb
      27 Jul 2016, 19:24

      Hi,

      how can I render a triangle using Qt3D (C++) without loading a obj file?

      ? Offline
      ? Offline
      A Former User
      wrote on 27 Jul 2016, 19:52 last edited by
      #2

      @cjsb Hi, welcome to the Qt forum! You can create your own Qt3D::QGeometry-derived class. There is an example for this, although I must admit, the example is quite complicated. See Qt3D: Tessellation Modes QML Example, especially tessellatedquadmesh.cpp Example File.

      1 Reply Last reply
      1
      • C Offline
        C Offline
        cjsb
        wrote on 27 Jul 2016, 19:58 last edited by
        #3

        Thanks Wieland,

        I will try to create the QGeometry. With the new QGeometry done, how can I tell the application to render it? I am not using the QML based code =x

        1 Reply Last reply
        0
        • ? Offline
          ? Offline
          A Former User
          wrote on 27 Jul 2016, 20:14 last edited by
          #4

          Once you got that first thing complete, you need to build a class that inherits from Qt3DRender::QGeometryRenderer. Here is some code I wrote a while ago that demonstrates that:

          header

          #ifndef FEMMESH_H
          #define FEMMESH_H
          
          #include <Qt3DRender/qgeometryrenderer.h>
          
          class MeshData;
          
          class FemMesh : public Qt3DRender::QGeometryRenderer
          {
              Q_OBJECT
          public:
              explicit FemMesh(Qt3DCore::QNode *parent = 0);
              ~FemMesh();
          
          public slots:
              void setFromMeshData(MeshData *meshData);
              void clear();
          
              void init();
          };
          
          #endif // FEMMESH_H
          

          source

          #include "femmesh.h"
          
          #include "meshdata.h"
          #include "femmeshgeometry.h"
          
          #include "meshdataloader.h"
          
          FemMesh::FemMesh(QNode *parent)
              : Qt3DRender::QGeometryRenderer(parent)
          {
          }
          
          
          FemMesh::~FemMesh()
          {
              QNode::cleanup();
          }
          
          
          void FemMesh::setFromMeshData(MeshData *meshData)
          {
              clear();
              FemMeshGeometry *geom = new FemMeshGeometry(this);
              geom->setFromMeshData(meshData);
              setInstanceCount(1);
              setBaseVertex(0);
              setBaseInstance(0);
              setPrimitiveType(Qt3DRender::QGeometryRenderer::Triangles);
              // setPrimitiveCount has a misleading name.
              // It takes this: (number of triangles) * (number of vertices per triangle)
              setPrimitiveCount(meshData->triangleCount() * 3);
              setGeometry(geom);
          }
          
          
          void FemMesh::clear()
          {
              if (geometry())
                  geometry()->deleteLater();
              setGeometry(Q_NULLPTR);
          }
          
          
          void FemMesh::init()
          {
              MeshDataLoader mdl;
              MeshData * meshData = mdl.load(QUrl(QStringLiteral("qrc:/assets/a.off")));
              setFromMeshData(meshData);
            //  meshData->dbg(); // print mesh data to debug output
              delete meshData;
          }
          

          With the two classes at hand you can write a complete Qt3D application in C++, like in the Qt 3D: Simple C++ Example.

          1 Reply Last reply
          2
          • C Offline
            C Offline
            cjsb
            wrote on 27 Jul 2016, 20:24 last edited by
            #5

            Great, I will try to create one.

            Can I have access to the meshdata, femmeshgeometry and meshdataloader? Because in the end, I want to render a mesh and those code might help a lot.

            ? 1 Reply Last reply 27 Jul 2016, 20:30
            0
            • C cjsb
              27 Jul 2016, 20:24

              Great, I will try to create one.

              Can I have access to the meshdata, femmeshgeometry and meshdataloader? Because in the end, I want to render a mesh and those code might help a lot.

              ? Offline
              ? Offline
              A Former User
              wrote on 27 Jul 2016, 20:30 last edited by
              #6

              @cjsb I've sent you a chat message with a download link. Also, there is a thread related to that code: https://forum.qt.io/topic/64012/mesh-color

              1 Reply Last reply
              0
              • I Offline
                I Offline
                iLya84a
                wrote on 5 Dec 2017, 15:31 last edited by
                #7

                Hi @cjsb! I've created two small examples.
                The first example is pure C++. It is exactly you are looking for - https://github.com/iLya84a/qt3d/tree/master/custom-mesh-cpp
                The second one is a Qml-based app - https://github.com/iLya84a/qt3d/tree/master/custom-mesh-qml

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved