Qt 6.2.0, Qt3D. Cannot apply a texture to an object
Unsolved
Game Development
-
Hello,
I do not know how to apply a texture to an object. I tried like this but it does not work:
Qt3DRender::QTextureLoader *textureLoader = new Qt3DRender::QTextureLoader(objectEntity); Qt3DExtras::QTextureMaterial *material = new Qt3DExtras::QTextureMaterial(objectEntity); textureLoader->setSource(QUrl("qrc:/Models/GreenBox.png")); material->setTexture(textureLoader); objectEntity->addComponent(material);
I do not have any error messages.
I tried DAE-object:
GreenBox.dae
<?xml version="1.0" encoding="utf-8"?> <COLLADA xmlns="http://www.collada.org/2005/11/COLLADASchema" version="1.4.1"> <asset> <contributor> <author>Blender User</author> <authoring_tool>Blender 2.67.0 r57141</authoring_tool> </contributor> <created>2021-08-21T16:56:51</created> <modified>2021-08-21T16:56:51</modified> <unit name="meter" meter="1"/> <up_axis>Z_UP</up_axis> </asset> <library_images/> <library_effects> <effect id="Material-effect"> <profile_COMMON> <technique sid="common"> <phong> <emission> <color sid="emission">0 0 0 1</color> </emission> <ambient> <color sid="ambient">0 0 0 1</color> </ambient> <diffuse> <color sid="diffuse">0.64 0.64 0.64 1</color> </diffuse> <specular> <color sid="specular">0.5 0.5 0.5 1</color> </specular> <shininess> <float sid="shininess">50</float> </shininess> <index_of_refraction> <float sid="index_of_refraction">1</float> </index_of_refraction> </phong> </technique> <extra> <technique profile="GOOGLEEARTH"> <double_sided>1</double_sided> </technique> </extra> </profile_COMMON> <extra><technique profile="MAX3D"><double_sided>1</double_sided></technique></extra> </effect> </library_effects> <library_materials> <material id="Material-material" name="Material"> <instance_effect url="#Material-effect"/> </material> </library_materials> <library_geometries> <geometry id="Cube-mesh" name="Cube"> <mesh> <source id="Cube-mesh-positions"> <float_array id="Cube-mesh-positions-array" count="24">1 1 -1 1 -1 -1 -1 -0.9999998 -1 -0.9999997 1 -1 1 0.9999995 1 0.9999994 -1.000001 1 -1 -0.9999997 1 -1 1 1</float_array> <technique_common> <accessor source="#Cube-mesh-positions-array" count="8" stride="3"> <param name="X" type="float"/> <param name="Y" type="float"/> <param name="Z" type="float"/> </accessor> </technique_common> </source> <source id="Cube-mesh-normals"> <float_array id="Cube-mesh-normals-array" count="36">0 0 -1 0 0 1 1 -5.66244e-7 -2.38419e-7 0 -1 -2.98023e-7 -1 2.38419e-7 -1.19209e-7 2.08616e-7 1 2.38419e-7 0 0 -1 0 0 1 1 0 3.27826e-7 -4.76837e-7 -1 0 -1 2.08616e-7 -1.49012e-7 2.68221e-7 1 1.78814e-7</float_array> <technique_common> <accessor source="#Cube-mesh-normals-array" count="12" stride="3"> <param name="X" type="float"/> <param name="Y" type="float"/> <param name="Z" type="float"/> </accessor> </technique_common> </source> <source id="Cube-mesh-map-0"> <float_array id="Cube-mesh-map-0-array" count="72">0.3991582 0.6954766 0.3991585 0.4875001 0.6071346 0.6954768 0.3991591 0.07154691 0.6071359 0.07154715 0.6071354 0.2795239 0.3991582 0.6954766 0.3991582 0.903453 0.1911818 0.9034531 0.3991585 0.4875001 0.399159 0.2795235 0.607135 0.4875004 0.815111 0.6954768 0.815111 0.9034532 0.6071346 0.6954768 0.3991582 0.903453 0.3991582 0.6954766 0.6071346 0.6954768 0.3991585 0.4875001 0.607135 0.4875004 0.6071346 0.6954768 0.399159 0.2795235 0.3991591 0.07154691 0.6071354 0.2795239 0.1911819 0.6954766 0.3991582 0.6954766 0.1911818 0.9034531 0.399159 0.2795235 0.6071354 0.2795239 0.607135 0.4875004 0.815111 0.9034532 0.6071346 0.9034532 0.6071346 0.6954768 0.6071346 0.9034532 0.3991582 0.903453 0.6071346 0.6954768</float_array> <technique_common> <accessor source="#Cube-mesh-map-0-array" count="36" stride="2"> <param name="S" type="float"/> <param name="T" type="float"/> </accessor> </technique_common> </source> <vertices id="Cube-mesh-vertices"> <input semantic="POSITION" source="#Cube-mesh-positions"/> </vertices> <polylist material="Material-material" count="12"> <input semantic="VERTEX" source="#Cube-mesh-vertices" offset="0"/> <input semantic="NORMAL" source="#Cube-mesh-normals" offset="1"/> <input semantic="TEXCOORD" source="#Cube-mesh-map-0" offset="2" set="0"/> <vcount>3 3 3 3 3 3 3 3 3 3 3 3 </vcount> <p>0 0 0 1 0 1 3 0 2 4 1 3 7 1 4 6 1 5 0 2 6 4 2 7 5 2 8 1 3 9 5 3 10 2 3 11 2 4 12 6 4 13 3 4 14 4 5 15 0 5 16 3 5 17 1 6 18 2 6 19 3 6 20 5 7 21 4 7 22 6 7 23 1 8 24 0 8 25 5 8 26 5 9 27 6 9 28 2 9 29 6 10 30 7 10 31 3 10 32 7 11 33 4 11 34 3 11 35</p> </polylist> </mesh> <extra><technique profile="MAYA"><double_sided>1</double_sided></technique></extra> </geometry> </library_geometries> <library_controllers/> <library_visual_scenes> <visual_scene id="Scene" name="Scene"> <node id="Cube" name="Cube" type="NODE"> <matrix sid="transform">1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1</matrix> <instance_geometry url="#Cube-mesh"> <bind_material> <technique_common> <instance_material symbol="Material-material" target="#Material-material"/> </technique_common> </bind_material> </instance_geometry> </node> </visual_scene> </library_visual_scenes> <scene> <instance_visual_scene url="#Scene"/> </scene> </COLLADA>
and OBJ-object:
GreenBox.obj
# Blender v2.93.5 OBJ File: '' # www.blender.org mtllib GreenBox.mtl o Cube_Cube.001 v 1.000000 -1.000000 -1.000000 v 1.000000 -1.000000 1.000000 v -1.000000 -1.000000 1.000000 v -1.000000 -1.000000 -1.000000 v 1.000000 1.000000 -1.000000 v 0.999999 1.000000 1.000001 v -1.000000 1.000000 1.000000 v -1.000000 1.000000 -1.000000 vt 0.399158 0.695477 vt 0.399159 0.487500 vt 0.607135 0.695477 vt 0.399159 0.071547 vt 0.607136 0.071547 vt 0.607135 0.279524 vt 0.399158 0.903453 vt 0.191182 0.903453 vt 0.399159 0.279523 vt 0.607135 0.487500 vt 0.815111 0.695477 vt 0.815111 0.903453 vt 0.191182 0.695477 vt 0.607135 0.903453 vn 0.0000 -1.0000 0.0000 vn 0.0000 1.0000 0.0000 vn 1.0000 -0.0000 0.0000 vn -0.0000 -0.0000 1.0000 vn -1.0000 -0.0000 -0.0000 vn 0.0000 0.0000 -1.0000 usemtl Material.001 s off f 1/1/1 2/2/1 4/3/1 f 5/4/2 8/5/2 7/6/2 f 1/1/3 5/7/3 6/8/3 f 2/2/4 6/9/4 3/10/4 f 3/11/5 7/12/5 4/3/5 f 5/7/6 1/1/6 4/3/6 f 2/2/1 3/10/1 4/3/1 f 6/9/2 5/4/2 7/6/2 f 2/13/3 1/1/3 6/8/3 f 6/9/4 7/6/4 3/10/4 f 7/12/5 8/14/5 4/3/5 f 8/14/6 5/7/6 4/3/6
GreenBox.mtl
# Blender MTL File: 'None' # Material Count: 1 newmtl Material.001 Ns 225.000000 Ka 1.000000 1.000000 1.000000 Kd 0.640000 0.640000 0.640000 Ks 0.500000 0.500000 0.500000 Ke 0.000000 0.000000 0.000000 Ni 1.000000 d 1.000000 illum 2
This is my texture:
My source code:
.pro
QT += 3dcore 3dlogic 3dextras 3dinput
I have all code in one
main.cpp
file:main.cpp
#ifdef _WIN32 #include <windows.h> extern "C" __declspec(dllexport) DWORD NvOptimusEnablement = 0x00000001; extern "C" __declspec(dllexport) DWORD AmdPowerXpressRequestHighPerformance = 0x00000001; #endif #include <QtCore/QDebug> #include <QtGui/QGuiApplication> #include <Qt3DExtras/Qt3DWindow> #include <Qt3DExtras/QOrbitCameraController> #include <Qt3DExtras/QTextureMaterial> #include <Qt3DCore/QEntity> #include <Qt3DCore/QTransform> #include <Qt3DRender/QCamera> #include <Qt3DRender/QPointLight> #include <Qt3DRender/QSceneLoader> #include <Qt3DRender/QTexture> class Window : public Qt3DExtras::Qt3DWindow { public: Window() { setTitle("Texture for 3D model"); resize(400, 400); Qt3DCore::QEntity *rootEntity = new Qt3DCore::QEntity(); // Lighting Qt3DCore::QEntity *lightEntity = new Qt3DCore::QEntity(rootEntity); Qt3DRender::QPointLight *pointLight = new Qt3DRender::QPointLight(lightEntity); Qt3DCore::QTransform *lightTransform = new Qt3DCore::QTransform(lightEntity); lightTransform->setTranslation(QVector3D(20.f, 25.f, 30.f)); lightEntity->addComponent(pointLight); lightEntity->addComponent(lightTransform); // 3D Model Qt3DCore::QEntity *objectEntity = new Qt3DCore::QEntity(rootEntity); Qt3DRender::QSceneLoader *sceneLoader = new Qt3DRender::QSceneLoader(objectEntity); // sceneLoader->setSource(QUrl("qrc:/Models/GreenBox.obj")); sceneLoader->setSource(QUrl("qrc:/Models/GreenBox.dae")); objectEntity->addComponent(sceneLoader); Qt3DCore::QTransform *objectTransform = new Qt3DCore::QTransform(objectEntity); objectTransform->setScale(10.f); objectEntity->addComponent(objectTransform); Qt3DRender::QTextureLoader *textureLoader = new Qt3DRender::QTextureLoader(objectEntity); Qt3DExtras::QTextureMaterial *material = new Qt3DExtras::QTextureMaterial(objectEntity); textureLoader->setSource(QUrl("qrc:/Models/GreenBox.png")); material->setTexture(textureLoader); objectEntity->addComponent(material); // Camera Qt3DRender::QCamera *camera = this->camera(); camera->lens()->setPerspectiveProjection( 60.f, (float) this->width() / this->height(), 0.1f, 500.0f); camera->setPosition(QVector3D(30.0f, 30.0f, 30.f)); camera->setViewCenter(QVector3D(0.f, 0.f, 0.f)); Qt3DExtras::QOrbitCameraController *cameraController = new Qt3DExtras::QOrbitCameraController(rootEntity); cameraController->setCamera(camera); cameraController->setLookSpeed(180.f); cameraController->setLinearSpeed(50.f); this->setRootEntity(rootEntity); } }; int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); Window w; w.show(); return app.exec(); }