我试图从搅拌器加载一个.obj源文件,并显示它,LibertStatue.obj是与下面的main.cpp文件位于同一个文件夹中的.obj文件。当我运行这段代码时,我会得到一个空白窗口,我认为它应该显示一个自由女神像,以及一个命令提示符,上面写着QObject::connect(opneglcontext,未知)无效的nullptr参数。我在上使用。
我从这里下载了自由女神像的.obj文件:https://free3d.com/3d-model/statue-of-liberty-73656.html。
#include <QGuiApplication>
#include <Qt3DExtras/QPhongMaterial>
#include <Qt3DExtras/Qt3DWindow>
#include <Qt3DExtras/QOrbitCameraController>
#include <Qt3DRender>
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QUrl data = QUrl::fromLocalFile("LibertStatue.obj");
Qt3DExtras::Qt3DWindow view;
Qt3DCore::QEntity *rootEntity = new Qt3DCore::QEntity;
Qt3DCore::QEntity *flyingwedge = new Qt3DCore::QEntity(rootEntity);
Qt3DExtras::QPhongMaterial *material = new Qt3DExtras::QPhongMaterial();
material->setDiffuse(QColor(254, 254, 254));
Qt3DRender::QMesh *flyingwedgeMesh = new Qt3DRender::QMesh;
flyingwedgeMesh->setMeshName("FlyingWedge");
flyingwedgeMesh->setSource(data);
flyingwedge->addComponent(flyingwedgeMesh);
flyingwedge->addComponent(material);
Qt3DRender::QCamera *camera = view.camera();
camera->lens()->setPerspectiveProjection(40.0f, 16.0f/9.0f, 0.1f, 1000.0f);
camera->setPosition(QVector3D(0, 0, 40.0f));
camera->setViewCenter(QVector3D(0, 0, 0));
Qt3DCore::QEntity *lightEntity = new Qt3DCore::QEntity(rootEntity);
Qt3DRender::QPointLight *light = new Qt3DRender::QPointLight(lightEntity);
light->setColor("white");
light->setIntensity(0.8f);
lightEntity->addComponent(light);
Qt3DCore::QTransform *lightTransform = new Qt3DCore::QTransform(lightEntity);
lightTransform->setTranslation(QVector3D(60, 0, 40.0f));
lightEntity->addComponent(lightTransform);
Qt3DExtras::QOrbitCameraController *camController = new Qt3DExtras::QOrbitCameraController(rootEntity);
camController->setCamera(camera);
view.setRootEntity(rootEntity);
view.show();
return app.exec();
}发布于 2020-07-10 08:04:00
问题是setMeshName调用。QMesh C++类的文档具有误导性:setMeshName不是用于为QMesh对象设置名称,而是用于从要加载的OBJ文件中选择哪个几何图形。它的实际行为在相应的QML类型的文档中解释。
因此,您有两个选项:只需删除setMeshName调用,或传递几何学的实际名称,您可以通过使用文本编辑器打开OBJ文件来找到这个名称。
发布于 2020-07-01 06:38:38
检查您的代码,在我看来,Qt3Dwindow不能单独显示,需要创建一个窗口容器来在Widget (QFrame)中显示Qt3DWindow。
用于测试的典型Qt3D应用程序如下所示:
#include <QApplication>
#include <QDebug>
#include <QFrame>
#include <QVBoxLayout>
#include <QWidget>
#include <QTimer>
#include <Qt3DRender/QCamera>
#include <Qt3DRender/QCameraLens>
#include <Qt3DExtras/Qt3DWindow>
#include <Qt3DExtras/QOrbitCameraController>
#include <Qt3DExtras/QDiffuseSpecularMaterial>
#include <Qt3DExtras/QSphereMesh>
int main(int argc, char* argv[])
{
QApplication a(argc, argv);
auto view = new Qt3DExtras::Qt3DWindow();
auto rootEntity = new Qt3DCore::QEntity();
view->setRootEntity(rootEntity);
auto camera = view->camera();
camera->lens()->setPerspectiveProjection(45.0f, 1., 0.1f, 10000.0f);
camera->setPosition(QVector3D(0, 0, 5));
camera->setUpVector(QVector3D(0, 1, 0));
camera->setViewCenter(QVector3D(0, 0, 0));
auto camController = new Qt3DExtras::QOrbitCameraController(rootEntity);
camController->setCamera(camera);
auto sphereMat = new Qt3DExtras::QDiffuseSpecularMaterial;
sphereMat->setDiffuse(QColor(Qt::blue));
auto mesh = new Qt3DExtras::QSphereMesh();
mesh->setRadius(1);
auto sphereEntity = new Qt3DCore::QEntity(rootEntity);
sphereEntity->addComponent(mesh);
sphereEntity->addComponent(sphereMat);
auto container = QWidget::createWindowContainer(view);
QFrame frame;
frame.setLayout(new QVBoxLayout);
frame.layout()->addWidget(container);
frame.resize(QSize(400, 300));
QTimer::singleShot(100, [&]() {
camera->viewAll();
});
frame.show();
return a.exec();
}https://stackoverflow.com/questions/62630717
复制相似问题