我想用 Qt3D 创建一个具有自定义图像作为纹理的实体。我遇到了QPaintedTextureImage(链接指向 Qt 5.9 版本以获取详细信息。这里是 5.8 的 ist doc),它可以用 QPainter 编写,但我不明白如何编写。首先,这就是我想象的实体的样子:
[编辑]:代码已编辑并现在可以使用!
planeEntity = new Qt3DCore::QEntity(rootEntity);
planeMesh = new Qt3DExtras::QPlaneMesh;
planeMesh->setWidth(2);
planeMesh->setHeight(2);
image = new TextureImage; //see below
image->setSize(QSize(100,100));
painter = new QPainter;
image->paint(painter)
planeMaterial = new Qt3DExtras::QDiffuseMapMaterial;
planeMaterial->diffuse()->addTextureImage(image);
planeEntity->addComponent(planeMesh);
planeEntity->addComponent(planeMaterial);
TextureImage 是具有绘画功能的子类 QPaintedTextureImage:
class TextureImage : public Qt3DRender::QPaintedTextureImage
{
public:
void paint(QPainter* painter);
};
如果我只想在planeEntity上画一个大圆圈,那么传递给paint函数的QPainter在paint的实现中需要做什么?
[编辑] 实施:
void TextureImage::paint(QPainter* painter)
{
//hardcoded values because there was no device()->width/heigth
painter->fillRect(0, 0, 100, 100, QColor(255, 255, 255));
/* Set pen and brush to whatever you want. */
painter->setPen(QPen(QBrush(QColor(255, 0, 255)) ,10));
painter->setBrush(QColor(0, 0, 255));
/*
* Draw a circle (or an ellipse -- the outcome depends very much on
* the aspect ratio of the bounding rectangle amongst other things).
*/
painter->drawEllipse(0, 0, 100, 100);
}