qt3d自定义生成mesh图形
qt中提供的基础的QMesh
- 方形体 - Qt3DExtras::QConeMesh
- 圆锥体 - Qt3DExtras::QCuboidMesh
- 圆柱体 - Qt3DExtras::QCylinderMesh
- 文本 - Qt3DExtras::QExtrudedTextMesh
- 平面 - Qt3DExtras::QPlaneMesh
- 球体 - Qt3DExtras::QSphereMesh
- 圆环 - Qt3DExtras::QTorusMesh
- 外部加载 - Qt3DRender::QMesh 从各种格式的外部文件加载网格数据。
都继承于QGeometryRenderer类。而QGeometryRenderer继承于Qt3DCore::QComponent。很容易知道mesh网格数据是作为组件增加到渲染中的。
红色上面是qt自带绘制出来的3d模型。下面部分是自定义生成空心圆柱体的3d模型,涉及到重写这些类。
查看qt自带mesh分别怎么用的,和提供了什么
SceneModifier::SceneModifier(Qt3DCore::QEntity *rootEntity): m_rootEntity(rootEntity)
{// Torus shape data//! [0]m_torus = new Qt3DExtras::QTorusMesh();m_torus->setRadius(1.0f);m_torus->setMinorRadius(0.4f);m_torus->setRings(100);m_torus->setSlices(20);//! [0]// TorusMesh Transform//! [1]Qt3DCore::QTransform *torusTransform = new Qt3DCore::QTransform();torusTransform->setScale(1.0f);torusTransform->setRotation(QQuaternion::fromAxisAndAngle(QVector3D(0.0f, 1.0f, 0.0f), 25.0f));torusTransform->setTranslation(QVector3D(5.0f, 4.0f, 0.0f));//! [1]//! [2]Qt3DExtras::QPhongMaterial *torusMaterial = new Qt3DExtras::QPhongMaterial();torusMaterial->setDiffuse(QColor(QRgb(0xbeb32b)));//! [2]// Torus//! [3]m_torusEntity = new Qt3DCore::QEntity(m_rootEntity);m_torusEntity->addComponent(m_torus);m_torusEntity->addComponent(torusMaterial);m_torusEntity->addComponent(torusTransform);Qt3DRender::QObjectPicker *pickerTorus = new Qt3DRender::QObjectPicker(m_torusEntity);m_torusEntity->addComponent(pickerTorus);connect(pickerTorus, &Qt3DRender::QObjectPicker::clicked, this, &SceneModifier::handlePickerClicked);//! [3]// Cone shape dataQt3DExtras::QConeMesh *cone = new Qt3DExtras::QConeMesh();cone->setTopRadius(0.5);cone->setBottomRadius(1);cone->setLength(3);cone->setRings(50);cone->setSlices(20);// ConeMesh TransformQt3DCore::QTransform *coneTransform = new Qt3DCore::QTransform();coneTransform->setScale(1.5f);coneTransform->setRotation(QQuaternion::fromAxisAndAngle(QVector3D(1.0f, 0.0f, 0.0f), 45.0f));coneTransform->setTranslation(QVector3D(0.0f, 4.0f, -1.5));Qt3DExtras::QPhongMaterial *coneMaterial = new Qt3DExtras::QPhongMaterial();coneMaterial->setDiffuse(QColor(QRgb(0x928327)));// Conem_coneEntity = new Qt3DCore::QEntity(m_rootEntity);m_coneEntity->addComponent(cone);m_coneEntity->addComponent(coneMaterial);m_coneEntity->addComponent(coneTransform);// Cylinder shape dataQt3DExtras::QCylinderMesh *cylinder = new Qt3DExtras::QCylinderMesh();cylinder->setRadius(1);cylinder->setLength(3);cylinder->setRings(24);cylinder->setSlices(24);// CylinderMesh TransformQt3DCore::QTransform *cylinderTransform = new Qt3DCore::QTransform();cylinderTransform->setScale(1.5f);cylinderTransform->setRotation(QQuaternion::fromAxisAndAngle(QVector3D(1.0f, 0.0f, 0.0f), 45.0f));cylinderTransform->setTranslation(QVector3D(-5.0f, 4.0f, -1.5));Qt3DExtras::QPhongMaterial *cylinderMaterial = new Qt3DExtras::QPhongMaterial();cylinderMaterial->setDiffuse(QColor(QRgb(0x928327)));// Cylinderm_cylinderEntity = new Qt3DCore::QEntity(m_rootEntity);m_cylinderEntity->addComponent(cylinder);m_cylinderEntity->addComponent(cylinderMaterial);m_cylinderEntity->addComponent(cylinderTransform);// Cuboid shape dataQt3DExtras::QCuboidMesh *cuboid = new Qt3DExtras::QCuboidMesh();// CuboidMesh TransformQt3DCore::QTransform *cuboidTransform = new Qt3DCore::QTransform();cuboidTransform->setScale(2.0f);cuboidTransform->setTranslation(QVector3D(5.0f, -4.0f, 0.0f));Qt3DExtras::QPhongMaterial *cuboidMaterial = new Qt3DExtras::QPhongMaterial();cuboidMaterial->setDiffuse(QColor(QRgb(0x665423)));//Cuboidm_cuboidEntity = new Qt3DCore::QEntity(m_rootEntity);m_cuboidEntity->addComponent(cuboid);m_cuboidEntity->addComponent(cuboidMaterial);m_cuboidEntity->addComponent(cuboidTransform);Qt3DRender::QObjectPicker *pickerCuboid = new Qt3DRender::QObjectPicker(m_cuboidEntity);m_cuboidEntity->addComponent(pickerCuboid);connect(pickerCuboid, &Qt3DRender::QObjectPicker::clicked, this, &SceneModifier::handlePickerClicked);// Plane shape dataQt3DExtras::QPlaneMesh *planeMesh = new Qt3DExtras::QPlaneMesh();planeMesh->setWidth(2);planeMesh->setHeight(2);// Plane mesh transformQt3DCore::QTransform *planeTransform = new Qt3DCore::QTransform();planeTransform->setScale(1.3f);planeTransform->setRotation(QQuaternion::fromAxisAndAngle(QVector3D(1.0f, 0.0f, 0.0f), 45.0f));planeTransform->setTranslation(QVector3D(0.0f, -4.0f, 0.0f));Qt3DExtras::QPhongMaterial *planeMaterial = new Qt3DExtras::QPhongMaterial();planeMaterial->setDiffuse(QColor(QRgb(0xa69929)));// Planem_planeEntity = new Qt3DCore::QEntity(m_rootEntity);m_planeEntity->addComponent(planeMesh);m_planeEntity->addComponent(planeMaterial);m_planeEntity->addComponent(planeTransform);// Sphere shape dataQt3DExtras::QSphereMesh *sphereMesh = new Qt3DExtras::QSphereMesh();sphereMesh->setRings(20);sphereMesh->setSlices(20);sphereMesh->setRadius(2);// Sphere mesh transformQt3DCore::QTransform *sphereTransform = new Qt3DCore::QTransform();sphereTransform->setScale(1.3f);sphereTransform->setTranslation(QVector3D(-5.0f, -4.0f, 0.0f));Qt3DExtras::QPhongMaterial *sphereMaterial = new Qt3DExtras::QPhongMaterial();sphereMaterial->setDiffuse(QColor(QRgb(0xa69929)));// Spherem_sphereEntity = new Qt3DCore::QEntity(m_rootEntity);m_sphereEntity->addComponent(sphereMesh);m_sphereEntity->addComponent(sphereMaterial);m_sphereEntity->addComponent(sphereTransform);//textMeshQt3DExtras::QExtrudedTextMesh* textMesh = new Qt3DExtras::QExtrudedTextMesh();textMesh->setDepth(.1f);textMesh->setText("Qt3d自带的shapes");Qt3DCore::QTransform *textTransform = new Qt3DCore::QTransform();textTransform->setScale(1.5f);textTransform->setRotation(QQuaternion::fromAxisAndAngle(QVector3D(1.0f, 0.0f, 0.0f), 45.0f));textTransform->setTranslation(QVector3D(-6.0f, 8.0f, 0.0f));Qt3DExtras::QPhongMaterial *textMaterial = new Qt3DExtras::QPhongMaterial();textMaterial->setDiffuse(QColor(QRgb(0x928327)));//Qt3DCore::QEntity* textEntity = new Qt3DCore::QEntity(m_rootEntity);textEntity->addComponent(textMesh);textEntity->addComponent(textMaterial);textEntity->addComponent(textTransform);//==============================================================================//DHollowCylinderMesh 自定义空心圆柱meshQt3DExtras::DHollowCylinderMesh* dcylinder = new Qt3DExtras::DHollowCylinderMesh();dcylinder->setSlices(100);dcylinder->setRings(100);dcylinder->setLength(3);dcylinder->setOuterRadius(5);dcylinder->setInnerRadius(2.5f);Qt3DCore::QTransform *dcylinderTransform = new Qt3DCore::QTransform();dcylinderTransform->setScale(1.5f);dcylinderTransform->setRotation(QQuaternion::fromAxisAndAngle(QVector3D(1.0f, 0.0f, 0.0f), 45.0f));dcylinderTransform->setTranslation(QVector3D(0.0f, -15.0f, 0.0f));Qt3DExtras::QPhongMaterial *dcylinderMaterial = new Qt3DExtras::QPhongMaterial();dcylinderMaterial->setDiffuse(QColor(QRgb(0x928327)));Qt3DCore::QEntity* dcylinderEntity = new Qt3DCore::QEntity(m_rootEntity);dcylinderEntity->addComponent(dcylinder);dcylinderEntity->addComponent(dcylinderMaterial);dcylinderEntity->addComponent(dcylinderTransform);
}
自定义图形 (空心圆柱体)
DHollowCylinderMesh.h
#ifndef DHOLLOWCYLINDERMESH_H
#define DHOLLOWCYLINDERMESH_H#include <Qt3DExtras/qt3dextras_global.h>
#include <Qt3DRender/qgeometryrenderer.h>QT_BEGIN_NAMESPACE
namespace Qt3DExtras {
class Q_3DEXTRASSHARED_EXPORT DHollowCylinderMesh : public Qt3DRender::QGeometryRenderer
{Q_OBJECTQ_PROPERTY(int rings READ rings WRITE setRings NOTIFY ringsChanged)Q_PROPERTY(int slices READ slices WRITE setSlices NOTIFY slicesChanged)Q_PROPERTY(float outerRadius READ outerRadius WRITE setOuterRadius NOTIFY outerRadiusChanged);Q_PROPERTY(float innerRadius READ innerRadius WRITE setInnerRadius NOTIFY innerRadiusChanged);Q_PROPERTY(float length READ length WRITE setLength NOTIFY lengthChanged)
public:explicit DHollowCylinderMesh(Qt3DCore::QNode *parent = nullptr);~DHollowCylinderMesh();int rings() const;int slices() const;float length() const;float outerRadius() const;float innerRadius() const;
public Q_SLOTS:void setRings(int rings);void setSlices(int slices);void setLength(float length);void setOuterRadius(float radius);void setInnerRadius(float radius);
Q_SIGNALS:void ringsChanged(int rings);void slicesChanged(int slices);void lengthChanged(float length);void outerRadiusChanged(float radius);void innerRadiusChanged(float radius);
private:// As this is a default provided geometry renderer, no one should be able// to modify the QGeometryRenderer's propertiesvoid setInstanceCount(int instanceCount);void setVertexCount(int vertexCount);void setIndexOffset(int indexOffset);void setFirstInstance(int firstInstance);void setRestartIndexValue(int index);void setPrimitiveRestartEnabled(bool enabled);void setGeometry(Qt3DRender::QGeometry *geometry);void setPrimitiveType(PrimitiveType primitiveType);
};
} // namespace Qt3DRender
QT_END_NAMESPACE#endif // DHOLLOWCYLINDERMESH_H
DHollowCylinderMesh.cpp
#include "dhollowcylindermesh.h"
#include "dhollowcylindergeometry.h"#ifndef _USE_MATH_DEFINES
# define _USE_MATH_DEFINES // For MSVC
#endif#include <Qt3DRender/qbuffer.h>
#include <Qt3DRender/qbufferdatagenerator.h>
#include <Qt3DRender/qattribute.h>
#include <QtGui/QVector3D>
#include <qmath.h>QT_BEGIN_NAMESPACE
using namespace Qt3DRender;
namespace Qt3DExtras {
/*!* \class Qt3DExtras::DHollowCylinderMesh\ingroup qt3d-extras-geometries* \inheaderfile Qt3DExtras/DHollowCylinderMesh* \inmodule Qt3DExtras** \inherits Qt3DRender::QGeometryRenderer** \brief A hollowCylinderMesh mesh.*/
/*!* Constructs a new DHollowCylinderMesh with \a parent.*/
DHollowCylinderMesh::DHollowCylinderMesh(QNode *parent): QGeometryRenderer(parent)
{DHollowCylinderGeometry *geometry = new DHollowCylinderGeometry(this);QObject::connect(geometry, &DHollowCylinderGeometry::outerRadiusChanged, this, &DHollowCylinderMesh::outerRadiusChanged);QObject::connect(geometry, &DHollowCylinderGeometry::innerRadiusChanged, this, &DHollowCylinderMesh::innerRadiusChanged);QObject::connect(geometry, &DHollowCylinderGeometry::ringsChanged, this, &DHollowCylinderMesh::ringsChanged);QObject::connect(geometry, &DHollowCylinderGeometry::slicesChanged, this, &DHollowCylinderMesh::slicesChanged);QObject::connect(geometry, &DHollowCylinderGeometry::lengthChanged, this, &DHollowCylinderMesh::lengthChanged);QGeometryRenderer::setGeometry(geometry);
}DHollowCylinderMesh::~DHollowCylinderMesh()
{
}
void DHollowCylinderMesh::setRings(int rings)
{static_cast<DHollowCylinderGeometry *>(geometry())->setRings(rings);
}
void DHollowCylinderMesh::setSlices(int slices)
{static_cast<DHollowCylinderGeometry *>(geometry())->setSlices(slices);
}void DHollowCylinderMesh::setLength(float length)
{static_cast<DHollowCylinderGeometry *>(geometry())->setLength(length);
}
void DHollowCylinderMesh::setOuterRadius(float radius)
{static_cast<DHollowCylinderGeometry *>(geometry())->setOuterRadius(radius);
}
void DHollowCylinderMesh::setInnerRadius(float radius)
{static_cast<DHollowCylinderGeometry *>(geometry())->setInnerRadius(radius);
}int DHollowCylinderMesh::rings() const
{return static_cast<DHollowCylinderGeometry *>(geometry())->rings();
}int DHollowCylinderMesh::slices() const
{return static_cast<DHollowCylinderGeometry *>(geometry())->slices();
}float DHollowCylinderMesh::length() const
{return static_cast<DHollowCylinderGeometry *>(geometry())->length();
}float DHollowCylinderMesh::outerRadius() const
{return static_cast<DHollowCylinderGeometry *>(geometry())->outerRadius();
}float DHollowCylinderMesh::innerRadius() const
{return static_cast<DHollowCylinderGeometry *>(geometry())->outerRadius();
}} // namespace Qt3DExtras
QT_END_NAMESPACE
DHollowCylinderGeometry.h
#ifndef DHOLLOWCYLINDERGEOMETRY_H
#define DHOLLOWCYLINDERGEOMETRY_H#include <Qt3DExtras/qt3dextras_global.h>
#include <Qt3DRender/qgeometry.h>QT_BEGIN_NAMESPACE
namespace Qt3DRender {
class QAttribute;
class QBuffer;
} // namespace Qt3DRender
namespace Qt3DExtras {class Q_3DEXTRASSHARED_EXPORT DHollowCylinderGeometry : public Qt3DRender::QGeometry
{Q_OBJECTQ_PROPERTY(int rings READ rings WRITE setRings NOTIFY ringsChanged)Q_PROPERTY(int slices READ slices WRITE setSlices NOTIFY slicesChanged)Q_PROPERTY(float innerRadius READ innerRadius WRITE setInnerRadius NOTIFY innerRadiusChanged)Q_PROPERTY(float outerRadius READ outerRadius WRITE setOuterRadius NOTIFY outerRadiusChanged)Q_PROPERTY(float length READ length WRITE setLength NOTIFY lengthChanged)Q_PROPERTY(Qt3DRender::QAttribute *positionAttribute READ positionAttribute CONSTANT)Q_PROPERTY(Qt3DRender::QAttribute *normalAttribute READ normalAttribute CONSTANT)Q_PROPERTY(Qt3DRender::QAttribute *texCoordAttribute READ texCoordAttribute CONSTANT)Q_PROPERTY(Qt3DRender::QAttribute *indexAttribute READ indexAttribute CONSTANT)
public:explicit DHollowCylinderGeometry(QNode *parent = nullptr);~DHollowCylinderGeometry();void updateVertices();void updateIndices();int rings() const;int slices() const;float innerRadius() const;float outerRadius() const;float length() const;Qt3DRender::QAttribute *positionAttribute() const;Qt3DRender::QAttribute *normalAttribute() const;Qt3DRender::QAttribute *texCoordAttribute() const;Qt3DRender::QAttribute *indexAttribute() const;
public Q_SLOTS:void setRings(int rings);void setSlices(int slices);void setLength(float length);void setOuterRadius(float radius);void setInnerRadius(float radius);Q_SIGNALS:void radiusChanged(float radius);void ringsChanged(int rings);void slicesChanged(int slices);void lengthChanged(float length);void outerRadiusChanged(float radius);void innerRadiusChanged(float radius);private://Q_DECLARE_PRIVATE(QCylinderGeometry)void init();int m_rings;int m_slices;float m_length;float m_outerRadius;float m_innerRadius;Qt3DRender::QAttribute *m_positionAttribute;Qt3DRender::QAttribute *m_normalAttribute;Qt3DRender::QAttribute *m_texCoordAttribute;Qt3DRender::QAttribute *m_indexAttribute;Qt3DRender::QBuffer *m_vertexBuffer;Qt3DRender::QBuffer *m_indexBuffer;
};
} // Qt3DExtras
QT_END_NAMESPACE#endif // DHOLLOWCYLINDERGEOMETRY_H