我正在尝试使用Qt3D库将.obj文件加载到Qt中。到目前为止,我的代码主要是从以下一个示例中复制而来:
import random
print("You are in a dark room in a mysterious castle.")
print("In front of you are four doors. You must choose one.")
playerChoice = input("Choose 1, 2, 3, or 4...")
if playerChoice == "1":
print("You found a room full of tresure. YOU'RE RICH!!!")
print("GAME OVER, YOU WIN!")
elif playerChoice == "2":
print("The door opens and an angry ogre hits you with his club.")
print("GAME OVER, YOU DIED!")
elif playerChoice == "3":
print("You encounter a sleeping dragon.")
print("You can do either:")
print("1) Try to steal some of the dragon's gold")
print("2) Sneak around the dragon to the exit")
dragonChoice = input("type 1 or 2...")
if dragonChoice == "1":
print("The dragon wakes up and eats you. You are delicious.")
print("GAME OVER, YOU WERE EATEN ALIVE.")
elif dragonChoice == "2":
print("You sneak around the dragon and escape the castle, blinking in the sunshine.")
print("GAME OVER, YOU ESCAPED THE CASTLE.")
else:
print("Sorry, you didn't enter 1 or 2.")
elif playerChoice == "4":
print("You enter a room with a sphinx.")
print("It asks you to guess what number it is thinking of, betwwen 1 to 10.")
number = int(input("What number do you choose?"))
if number == random.randint (1, 10):
print("The sphinx hisses in dissapointment. You guessed correctly.")
print("It must let you go free.")
print("GAME OVER, YOU WIN.")
else:
print("The sphinx tells you that your guess is incorrect.")
print("You are now it's prisoner forever.")
print("GAME OVER, YOU LOSE.")
else:
print("sorry, you didn't enter 1, 2, 3, or 4...")
print("YOU TURN BACK AND LEAVE (YOU COWARD)")
到目前为止,应用程序可以渲染对象并显示它。但是,我必须将.obj文件附加到qrc文件,以便它能够找到并呈现它。当我尝试使用来自任何目录的绝对路径设置Qt3DCore::QEntity *createScene()
{
// Root entity
Qt3DCore::QEntity *rootEntity = new Qt3DCore::QEntity;
// Material
Qt3DRender::QMaterial *material = new Qt3DExtras::QPhongMaterial(rootEntity);
// Chest Entity
Qt3DCore::QEntity *chestEntity = new Qt3DCore::QEntity(rootEntity);
Qt3DRender::QMesh *chestMesh = new Qt3DRender::QMesh(rootEntity);
chestMesh->setSource(QUrl("qrc:/PT18E.obj"));
chestEntity->addComponent(chestMesh);
chestEntity->addComponent(material);
return rootEntity;
}
int main(int argc, char* argv[])
{
QGuiApplication app(argc, argv);
Qt3DExtras::Qt3DWindow view;
Qt3DCore::QEntity *scene = createScene();
// Camera
Qt3DRender::QCamera *camera = view.camera();
camera->lens()->setPerspectiveProjection(45.0f, 16.0f/9.0f, 0.1f, 1000.0f);
camera->setPosition(QVector3D(0, 0, 1));
camera->setViewCenter(QVector3D(0, 0, 0));
// For camera controls
Qt3DExtras::QOrbitCameraController *camController = new Qt3DExtras::QOrbitCameraController(scene);
camController->setLinearSpeed( 10.0f );
camController->setLookSpeed( 180.0f );
camController->setCamera(camera);
view.setRootEntity(scene);
view.show();
return app.exec();
}
的源时,例如,
chestMesh
它不起作用。什么都没有呈现,我在控制台中看到错误
chestMesh->setSource(QURL(C:/Users/username/Documents/Qt/simple-cpp/PT18E.obj))
我尝试将完全相同的路径添加到QFSFileEngine::open: No file name specified
对象,然后使用QFile
函数查看路径是否正确,确定路径是否正确。但由于某种原因,网格不能使用不在qrc文件中的源。
如何在Qt中从文件系统加载和渲染任何.obj文件(无需将其放入qrc中)?
答案 0 :(得分:2)
为了编写指向文件的正确url,我们可以尝试静态调用QUrl::fromLocalFile。或者给出以上代码:
chestMesh->setSource(QUrl::fromLocalFile(winPath2ObjFile));