How to render a triangle using Qt3D
-
@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. -
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.
-
@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
-
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