How to render a triangle using Qt3D



  • Hi,

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


  • Moderators

    @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.



  • 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


  • Moderators

    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.



  • 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.


  • Moderators

    @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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.