我正在尝试在Qt C++中导入6个机器人机械臂关节的3D网格。正在显示关节,但它们不会像连接在一起一样旋转。但是每个关节都围绕自己的中心独立旋转。我想通过 c++ 代码旋转每个关节,其他关节也应该旋转。我正在使用Blender设置网格的中心并导出到obj文件。
Qt3DCore::QEntity *root = new Qt3DCore::QEntity();
Qt3DExtras::Qt3DWindow view;
Joint *joint1 = new Joint(root);
joint1->mesh()->setSource(QUrl(QStringLiteral("qrc:/assets/base.obj")));
joint1->transform()->setTranslation(QVector3D(0.0f, 1.81f, 0.0f));
joint1->transform()->setScale(0.01f);
Qt3DExtras::QPhongMaterial *material1 = new Qt3DExtras::QPhongMaterial();
material1->setDiffuse(QColor(204, 91, 70));
material1->setSpecular(QColor(255, 171, 247 ));
material1->setShininess(500.0f);
joint1->addComponent(material1);
Joint *joint2 = new Joint(root);
joint2->mesh()->setSource(QUrl(QStringLiteral("qrc:/assets/j2.obj")));
joint2->transform()->setTranslation(QVector3D(0.0f, 1.9030f, 0.0f));
joint2->transform()->setScale(0.01f);
Qt3DExtras::QPhongMaterial *material2 = new Qt3DExtras::QPhongMaterial();
material2->setDiffuse(QColor(27, 176, 204));
material2->setSpecular(QColor(37, 95, 255 ));
material2->setShininess(500.0f);
joint2->addComponent(material2);
/*
* Rest of the 4 joint meshes are defined in same way as above
*/
view.setRootEntity(root);
我的联合班如下: 关节.cpp
#include "joint.h"
Joint::Joint(Qt3DCore::QNode *parent)
: Qt3DCore::QEntity(parent)
,m_mesh(new Qt3DRender::QMesh())
,m_transform(new Qt3DCore::QTransform())
{
addComponent(m_mesh);
addComponent(m_transform);
}
Joint::~Joint()
{
}
Qt3DRender::QMesh *Joint::mesh() const
{
return m_mesh;
}
Qt3DCore::QTransform *Joint::transform() const
{
return m_transform;
}
我的联合头文件: 关节.h
#ifndef JOINT_H
#define JOINT_H
#include <Qt3DCore/QEntity>
#include <Qt3DCore/QTransform>
#include <Qt3DRender/QMesh>
class Joint : public Qt3DCore::QEntity
{
public:
Joint(Qt3DCore::QNode *parent= 0);
~Joint();
Qt3DRender::QMesh *mesh() const;
Qt3DCore::QTransform *transform() const;
private:
Qt3DRender::QMesh *m_mesh;
Qt3DCore::QTransform *m_transform;
};
#endif //JOINT_H
如果您的关节具有"父子"层次结构,则应将每个父项指定为Joint *joint2 = new Joint(joint1);
。
这样,当您轮换父母时,孩子也会轮换。但这不是互惠的(它在Blender中用作育儿(:旋转或翻译孩子不会影响其任何父母。