4

I am using Qt3D (5.11), and I am experiencing asserts when I try to set a QParameter value to be used by a custom fragment shader. Here is the section of code that doesn't seem to be working:

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 ) );

I've included only the bits of code needed for this question:

Is this a valid way to set a simple texture parameter? If not, what am I missing to set a simple image?

Note that qrc:/image.png is a 256x256 image that I have used elsewhere in this project without a problem.

The code compiles fine, but when I run it I get an assert with the following message: ASSERT: "texture" in file texture\textureimage.cpp, line 94

I am using VS2017 on Windows 10 with Qt 5.11.

Naaff
  • 9,213
  • 3
  • 38
  • 43

1 Answers1

2

I stumbled upon the issue. Parenting the QTextureImage to entity leads to the assert. Leaving off the parent completely (effectively setting it to nullptr) or parenting it to texture fixes the issue.

Here is some working code:

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 ) );

This is probably a bug? If someone knows why the QTextureImage cannot be safely parented to entity, please add a comment.

Naaff
  • 9,213
  • 3
  • 38
  • 43