问题
I am trying to draw a 3D horizontal bar chart using Qt 3D.I am trying to achieve this by drawing consecutive cuboids having the same x , y and rotation and translating them to different depths.But it seemed that changing the depth only is not working as both x and y are also changing and I can't figure it out.
Update:
I found out that the x and y attributes should also be changed by specific values. I hard-coded it and it works fine but I can't figure out an equation to do this automatic with varying dimensions.
The code:
SceneModifier::SceneModifier(Qt3DCore::QEntity *rootEntity)
: m_rootEntity(rootEntity)
{
// Green cuboid
//Cuboid shape data
cuboid = new Qt3DExtras::QCuboidMesh();
cuboid->setXExtent(8);
cuboid->setYExtent(2);
cuboid->setZExtent(4);
// CuboidMesh Transform
Qt3DCore::QTransform *cuboidTransform = new Qt3DCore::QTransform();
cuboidTransform->setRotation(QQuaternion::fromAxisAndAngle(QVector3D(0.1f, -0.1f, 0.0f), 45.0f));
Qt3DExtras::QPhongMaterial *cuboidMaterial = new Qt3DExtras::QPhongMaterial();
cuboidMaterial->setDiffuse(QColor(QRgb(0x66423)));
cuboidMaterial->setAmbient(QColor(QRgb(0x35423)));
//Cuboid
m_cuboidEntity = new Qt3DCore::QEntity(m_rootEntity);
m_cuboidEntity->addComponent(cuboid);
m_cuboidEntity->addComponent(cuboidMaterial);
m_cuboidEntity->addComponent(cuboidTransform);
// Red Cuboid shape data
cuboid2 = new Qt3DExtras::QCuboidMesh();
cuboid2->setXExtent(8);
cuboid2->setYExtent(2);
cuboid2->setZExtent(2);
// CuboidMesh Transform
cuboidTransform2 = new Qt3DCore::QTransform();
cuboidTransform2->setRotation(QQuaternion::fromAxisAndAngle(QVector3D(0.1f, -0.1f, 0.0f), 45.0f));
/*An equation is needed to calculate the translation values*/
cuboidTransform2->setTranslation(QVector3D(6*qAsin(45.0/180), 6*qAsin(45.0/180),-2.1f));
Qt3DExtras::QPhongMaterial *cuboidMaterial2 = new Qt3DExtras::QPhongMaterial();
cuboidMaterial2->setDiffuse(QColor(QRgb(0xff0000)));
//Cuboid
m_cuboidEntity2 = new Qt3DCore::QEntity(m_rootEntity);
m_cuboidEntity2->addComponent(cuboid2);
m_cuboidEntity2->addComponent(cuboidMaterial2);
m_cuboidEntity2->addComponent(cuboidTransform2);
}
回答1:
I think the problem you are having is that the translation happens after the rotation. Probably what you want is instead of setting the transform on each cuboid individually, you set the transform on their parent. So don't rotate either cuboid; just translate them normally as if you are viewing directly from above. But then add a rotation transform to m_rootEntity
.
来源:https://stackoverflow.com/questions/37799092/qt-3d-horizontal-bar-chart