4

I'm new to Qt 3D (C++), and while I'm training with my first 3D app, I couldn't achieve the following:

Creating a cube with 6 different textures on each face... Until now, this is what I've tried:

// Cuboid shape data
    Qt3DExtras::QCuboidMesh *cuboid = new Qt3DExtras::QCuboidMesh();

    // CuboidMesh Transform
    Qt3DCore::QTransform *cuboidTransform = new Qt3DCore::QTransform();
    cuboidTransform->setScale(2.0f);

   Qt3DExtras::QTextureMaterial *textureMaterial = new Qt3DExtras::QTextureMaterial();
   Qt3DRender::QTextureCubeMap *cubMap = new Qt3DRender::QTextureCubeMap();

    Qt3DRender::QTextureImage *f1 = new Qt3DRender::QTextureImage();
    Qt3DRender::QTextureImage *f2 = new Qt3DRender::QTextureImage();
    Qt3DRender::QTextureImage *f3 = new Qt3DRender::QTextureImage();
    Qt3DRender::QTextureImage *f4 = new Qt3DRender::QTextureImage();
    Qt3DRender::QTextureImage *f5 = new Qt3DRender::QTextureImage();
    Qt3DRender::QTextureImage *f6 = new Qt3DRender::QTextureImage();

    f1->setSource(QUrl("qrc:/rc/images/cubemap1.png"));
    f1->setFace(Qt3DRender::QAbstractTexture::CubeMapNegativeX);
    f2->setSource(QUrl("qrc:/rc/images/cubemap2.png"));
    f2->setFace(Qt3DRender::QAbstractTexture::CubeMapPositiveX);
    f3->setSource(QUrl("qrc:/rc/images/cubemap3.png"));
    f3->setFace(Qt3DRender::QAbstractTexture::CubeMapNegativeY);
    f4->setSource(QUrl("qrc:/rc/images/cubemap4.png"));
    f4->setFace(Qt3DRender::QAbstractTexture::CubeMapPositiveY);
    f5->setSource(QUrl("qrc:/rc/images/cubemap5.png"));
    f5->setFace(Qt3DRender::QAbstractTexture::CubeMapNegativeZ);
    f6->setSource(QUrl("qrc:/rc/images/cubemap6.png"));
    f6->setFace(Qt3DRender::QAbstractTexture::CubeMapPositiveZ);

    cubMap->addTextureImage(f1);
    cubMap->addTextureImage(f2);
    cubMap->addTextureImage(f3);
    cubMap->addTextureImage(f4);
    cubMap->addTextureImage(f5);
    cubMap->addTextureImage(f6);

    textureMaterial->setTexture(cubMap);

    //Cuboid
    m_cuboidEntity = new Qt3DCore::QEntity(m_rootEntity);
    m_cuboidEntity->addComponent(cuboid);
    m_cuboidEntity->addComponent(textureMaterial);
    m_cuboidEntity->addComponent(cuboidTransform);

But it gives me a black cube. I've googled for an example, but all I find is written in OpenGl, which I'm not familiar with. I believe it's possible using Qt C++ Classes only.

I would appreciate your help.

Osama
  • 324
  • 3
  • 11
  • Telling from a first glance, the code looks alright. Did you check whether the images are loaded correctly? Sometimes there is some output saying that the images could not be found. Maybe try loading the in a `QImage` and check if that contains the data correctly. – Florian Blume Oct 30 '19 at 11:56
  • @FlorianBlume, I don't think so, because they are correctly loaded when I use `QTexture2D` instead of `QTextureCubeMap`, and I'm not getting any output. – Osama Oct 30 '19 at 19:01

2 Answers2

1

This could be related to back face culling.

could you set the following in your renderer?

    activeFrameGraph: ForwardRenderer {
      ...
        frustumCulling: false
    }

When using Skybox (which is basically a CuboidMesh with different images on it), this is mandatory. Could be needed here too.

Eddy Alleman
  • 1,096
  • 2
  • 10
  • 21
  • I tried `m_scene->defaultFrameGraph()->setFrustumCullingEnabled(false)` where `m_scene` is a `Qt3DWindow`... but the cube is still **black**. It's good that you mentioned **Skybox**, because I've tried using `QSkyboxEntity`, and it **works** !! So why isn't that code working right ! – Osama Nov 01 '19 at 12:03
1

Try using f1->setSource(QUrl::fromLocalFile("...")); instead f1->setSource(QUrl("..."));, etc. And remember about Status

avttrue
  • 387
  • 2
  • 12
  • Does it even work with you ? I'm sure it isn't related to the path of the textures, because when I give a wrong one, I get _Failed to open "..."_, and the same paths are working great with `QTexture2D`... And about `QTextureImage::statusChanged`, this signal is never emitted, even with `QTexture2D`. – Osama Nov 15 '19 at 15:02
  • @perrahmaouy This is a tip from my experience on the practice of loading geometries from *.obj files. – avttrue Nov 16 '19 at 03:49