Qt3D纹理参数

时间:2019-01-31 00:26:00

标签: c++ qt3d

我正在使用Qt3D(5.11),并且在尝试设置供自定义片段着色器使用的QParameter值时遇到断言。这是一段似乎无效的代码:

auto entity = new Qt3DRender::QEntity( mRootEntity );
auto material = new Qt3DRender::QMaterial( entity );

// Set up the custom geometry and material for the entity, which works
// fine in tests as long as the fragment shader does not use texture mapping

auto image = new Qt3DRender::QTextureImage( entity );
image->setSource( QString( "qrc:/image.png" ) );

auto texture = new Qt3DRender::QTexture2D( entity );
texture->addTextureImage( image );

material->addParameter( new QParameter( "imageTexture", texture, entity ) );

我只包含了此问题所需的一些代码:

这是设置简单纹理参数的有效方法吗?如果没有,我想设置一个简单的图像是什么?

请注意,qrc:/image.png是我在该项目的其他地方使用的256x256图像,没有问题。

代码可以很好地编译,但是当我运行它时,我得到一个带有以下消息的断言:ASSERT: "texture" in file texture\textureimage.cpp, line 94

我正在Windows 10和Qt 5.11上使用VS2017。

1 个答案:

答案 0 :(得分:1)

我偶然发现了这个问题。将QTextureImage设为entity会导致断言。完全放弃父项(将其有效地设置为nullptr)或将其父项设置为texture可以解决此问题。

以下是一些工作代码:

auto texture = new Qt3DRender::QTexture2D( entity );
auto image = new Qt3DRender::QTextureImage( texture );
image->setSource( QString( "qrc:/image.png" ) );
texture->addTextureImage( image );

material->addParameter( new QParameter( "imageTexture", texture, entity ) );

这可能是一个错误?如果有人知道为什么QTextureImage不能安全地作为entity的父母,请添加注释。

相关问题