1

I would like to create a 3D scene using Qt (version 5.11).

I would like to map a JPG image on nearly-infinite sphere, to support background of the 3D scene.

Here is the related source code:

Qt3DCore::QEntity *aGalaxyBackground = new Qt3DCore::QEntity(rootEntity);
Qt3DExtras::QSphereMesh* aGalaxyBackgroundSphereMesh = new Qt3DExtras::QSphereMesh();
aGalaxyBackgroundSphereMesh->setRadius(100);
aGalaxyBackground->addComponent(aGalaxyBackgroundSphereMesh);

Qt3DRender::QTextureLoader* aGalaxyBackgroundLoader = new Qt3DRender::QTextureLoader(aGalaxyBackground);
Qt3DExtras::QTextureMaterial* aGalaxyBackgroundMaterial = new Qt3DExtras::QTextureMaterial(aGalaxyBackground);
aGalaxyBackgroundLoader->setSource(QUrl::fromLocalFile("D:/Qt/starfield.jpg"));
aGalaxyBackgroundMaterial->setTexture(aGalaxyBackgroundLoader);
aGalaxyBackground->addComponent(aGalaxyBackgroundMaterial);

The problem with this approach is that the JPG is mapped on the "outside" surface of the sphere. When the camera is in the inside of the sphere, the background is not visible.

What is wrong with this approach?

  • Does it have to be a sphere you're mapping the image to or do you generally need a background image? – Florian Blume Jul 19 '18 at 14:14
  • Well, the background needs to be defined in all directions; it should change with the direction of the camera. Do you think a background image could do this? – Pierre Vueghs Jul 20 '18 at 14:45
  • Probably not but if you just wanted a background image I would have redirected you to my background image renderer (https://github.com/Sonnentierchen/Qt3D-BackgroundImage). But I sounds like the Qt skybox example is the right thing for you: http://doc.qt.io/archives/qt-5.5/qt3drenderer-skybox-example.html. – Florian Blume Jul 20 '18 at 17:30
  • If it isn't then you probably have to create your own mesh with faces pointing inwards. Then you need your own technique (and everything else) to push your texture and access it from the renderer. – Florian Blume Jul 20 '18 at 17:31
  • Thanks a lot @HappyFeet! It is clear (in theory). It is the way it is done in some example of Qt, but defined in XML. I would like to do the same exercise in C++. Is there any way to reverse the normal of the faces of a mesh? This kind of service should be proposed by the platform... Anyway, I think I see the way... I guess I have to replace the QSphereMesh with the new mesh (with inverted normals)... The rest of the code should remain the same, isn't it? Thank you for your answer :) – Pierre Vueghs Jul 20 '18 at 21:04
  • That sounds about correct! But what do you mean in XML? If you mean QML (what you probably do) then you can transfer the exmamples into C++ directly! Do you have any links to the examples? – Florian Blume Jul 21 '18 at 07:26
  • Here is an example for a cylinder created using QGeometry: https://doc.qt.io/GammaRay/gammaray-qt3d-geometry-mycylinder-cpp.html. You could add another texture coordinate attribute. You can find more description here: http://doc.qt.io/qt-5/qt3drender-geometry.html. You can also look at the QTextureMaterial at https://doc.qt.io/qt-5.11/qt3dextras-qtexturematerial.html and its shader https://github.com/qt/qt3d/blob/5.11/src/extras/shaders/gl3/unlittexture.vert. It expects a texCoord, i.e. you have to add such an attribute to your geometry. – Florian Blume Jul 21 '18 at 07:50
  • And I'd suggest you add the texture to a resources file to avoid referring to files on your hard drive ;) – Florian Blume Jul 21 '18 at 07:52
  • Hi @HappyFeet, thank you for all your suggestions :) I will try to mimic the case of the cylinder, I think it is the best example. I will be on holidays from tomorrow, with no Internet access... but I kept all links you sent me in memory. I will try to continue working anyway :) Thanks again :) – Pierre Vueghs Jul 21 '18 at 18:58
  • You're welcome :) I always try to help with Qt3D as much as possible because it gave me such a headache when I started to use it ;) Don't forget to post an answer if you find a solution! – Florian Blume Jul 22 '18 at 05:51

1 Answers1

3

You can set the sphere radius to -100 instead of 100, and you'll have normals pointing inwards. The texture will then be visible from inside the sphere, and not outside.

Pascal
  • 31
  • 3