去官方的github下载vsgQt的源码,通过它你可以很简单的创建出嵌入到Qt界面的渲染窗口。
https://github.com/vsg-dev/vsgQt
如果项目是cmake项目可以直接放入源码进行编译,这里我采用的是vs的解决方案。
下载下来之后,通过cmake进行编译,编译完成得到我们想要的静态库和动态库。
右键项目属性,找到c/c++栏,在“附加包含目录”添加vsgQt的include文件夹;
在“链接器”->"附加库目录"添加vsgQt的lib文件夹;
在“连接器”->"输入"->“附加依赖项”添加编译的静态库,如果是debug模式添加vsgQtd.lib,如果是release模式添加vsgQt.lib。

设置完成之后,就可以使用vsgQt来创建我们的渲染窗口了,代码里头有3个例子可以参考,这里我们使用vsgqtwindows这个例子进行创建。
在界面类中增加CreateVsgWindow方法,然后把例子中的代码拷过来,参考代码如下:
//mainwindow.h
vsgQt::Window* CreateVsgWindow(vsg::ref_ptr<vsgQt::Viewer> viewer, vsg::ref_ptr<vsg::WindowTraits> traits, vsg::ref_ptr<vsg::Node> vsg_scene, QWindow* parent, const QString& title);
//mainwindow.cpp
vsgQt::Window* BimMainWindow::CreateVsgWindow(vsg::ref_ptr<vsgQt::Viewer> viewer, vsg::ref_ptr<vsg::WindowTraits> traits, vsg::ref_ptr<vsg::Node> vsg_scene, QWindow* parent, const QString& title = {})
{
auto window = new vsgQt::Window(viewer, traits, parent);
window->setTitle(title);
window->initializeWindow();
// if this is the first window to be created, use its device for future window creation.
if (!traits->device) traits->device = window->windowAdapter->getOrCreateDevice();
// compute the bounds of the scene graph to help position camera
vsg::ComputeBounds computeBounds;
vsg_scene->accept(computeBounds);
//vsg::dvec3 centre = (computeBounds.bounds.min + computeBounds.bounds.max) * 0.5;
vsg::dvec3 center(0, 0, 0);
//double radius = vsg::length(computeBounds.bounds.max - computeBounds.bounds.min) * 0.6;
double radius = 3000;
double nearPlane = 0.1;
double farPlane = 50000;
uint32_t width = window->traits->width;
uint32_t height = window->traits->height;
vsg::ref_ptr<vsg::EllipsoidModel> ellipsoidModel(vsg_scene->getObject<vsg::EllipsoidModel>("EllipsoidModel"));
vsg::ref_ptr<vsg::Camera> camera;
{
// set up the camera
auto lookAt = vsg::LookAt::create(center + vsg::dvec3(0.0, -radius * 3.5, 0.0), center, vsg::dvec3(0.0, 0.0, 1.0));
vsg::ref_ptr<vsg::ProjectionMatrix> perspective;
/*if (ellipsoidModel)
{
perspective = vsg::EllipsoidPerspective::create(
lookAt, ellipsoidModel, 30.0,
static_cast<double>(width) /
static_cast<double>(height),
nearFarRatio, false);
}
else*/
{
perspective = vsg::Perspective::create(
30.0,
static_cast<double>(width) /
static_cast<double>(height),
nearPlane, farPlane);
}
camera = vsg::Camera::create(perspective, lookAt, vsg::ViewportState::create(VkExtent2D{ width, height }));
}
auto trackball = vsg::Trackball::create(camera, ellipsoidModel);
trackball->addWindow(*window);
viewer->addEventHandler(trackball);
auto renderGraph = vsg::RenderGraph::create(window->windowAdapter);
VkClearValue colorClear{};
colorClear.color = { {0.2f, 0.2f, 0.2f, 1.0f} };
VkClearValue depthClear{};
depthClear.depthStencil = { 1.0f, 0 }; // 深度 = 1.0
renderGraph->clearValues = { colorClear };
auto commandGraph = vsg::createCommandGraphForView(*window, camera, vsg_scene);
//commandGraph->addChild(renderGraph);
viewer->addRecordAndSubmitTaskAndPresentation({ commandGraph });
return window;
}构造函数中调用创建窗口的方法,参考代码:
//mainwindow.h
vsg::ref_ptr<vsg::Group> m_vsgScene;
vsg::ref_ptr<vsg::Options> m_vsgOptions;
vsg::ref_ptr<vsgQt::Viewer> m_vsgViewer;
vsg::ref_ptr<vsg::ShaderStage> m_vertexShader;
vsg::ref_ptr<vsg::ShaderStage> m_fragmentShader;
//mainwindow.cpp
m_vsgOptions = vsg::Options::create(vsgXchange::all::create());
m_vsgOptions->fileCache = vsg::getEnv("VSG_FILE_CACHE");
m_vsgOptions->paths = vsg::getEnvPaths("VSG_FILE_PATH");
m_vsgOptions->sharedObjects = vsg::SharedObjects::create();
auto windowTraits = vsg::WindowTraits::create();
windowTraits->windowTitle = "vsgQt viewer";
windowTraits->swapchainPreferences.imageCount = 2; // 最少双缓冲
windowTraits->swapchainPreferences.presentMode = VK_PRESENT_MODE_FIFO_KHR; // 避免 tear
windowTraits->depthFormat = VK_FORMAT_D32_SFLOAT;
//m_vsgScene = vsg::read_cast<vsg::Node>("C:/Users/kdyonly/Desktop/555.gltf",options);
m_vsgScene = vsg::Group::create();
if (!m_vsgScene)
{
return;
}
// create the viewer that will manage all the rendering of the views
m_vsgViewer = vsgQt::Viewer::create();
auto window = CreateVsgWindow(m_vsgViewer, windowTraits, m_vsgScene, nullptr, "First Window");
auto widget = QWidget::createWindowContainer(window, ui.frameopengl);
widget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
// 设置布局
QVBoxLayout* layout = new QVBoxLayout(ui.frameopengl);
//layout->setContentsMargins(0, 0, 0, 0);
layout->setSpacing(0);
layout->addWidget(widget);
m_vsgViewer->addEventHandler(vsg::CloseHandler::create(m_vsgViewer));
m_vsgViewer->setInterval(8);
m_vsgViewer->continuousUpdate = true;
m_vsgViewer->compile();如果你成功了,你将得到类似这样的嵌入到界面的一个渲染窗口。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。