我的任务:计算3D网格的像素坐标(例如制作快照),从特定的摄像机角度找到该网格的2D形状.
我目前正在使用带有QGeometryRenderer的Qt3D将包含网格的场景渲染到QWidget,它可以正常工作.我尝试使用QWidget :: render()将QWidget的内容呈现为Pixmap,如本文所建议的如何创建QWidget的屏幕截图?.将像素图保存为.jpg会产生具有默认背景颜色的空白图像,这是有意义的,因为QWidget本身并未保存网格对象.
以下是我在mainwindow.cpp中设置场景的方法
// sets the scene objects, camera, lights,...
void MainWindow::setScene() {
scene = custommesh->createScene(mesh->getVertices(),
mesh->getVerticesNormals(),
mesh->getFaceNormals(),
mesh->getVerticesIndex(),
mesh->getFacesIndex()); // QEntity*
custommesh->setMaterial(scene); // CustomMeshRenderer object
camera = custommesh->setCamera(view);
custommesh->setLight(scene, camera);
custommesh->setCamController(scene, camera);
view->setRootEntity(scene); // Qt3DExtras::Qt3DWindow object
// Setting up a QWiget working as a container for the view
QWidget *container = QWidget::createWindowContainer(view);
container->setMinimumSize(QSize(500, 500));
QSizePolicy policy = QSizePolicy(QSizePolicy::Policy(5), QSizePolicy::Policy(5));
policy.setHorizontalStretch(1);
policy.setVerticalStretch(1);
container->setSizePolicy(policy);
container->setObjectName("meshWidget");
this->ui->meshLayout->insertWidget(0, container);
}
Run Code Online (Sandbox Code Playgroud)
至于渲染,这里是custommeshrenderer类,其中定义了QGeometryRenderer,并在初始化网格时返回QEntity*.
#include "custommeshrenderer.h"
#include <Qt3DRender/QAttribute>
#include <Qt3DExtras>
#include <Qt3DRender/QGeometryRenderer> …
Run Code Online (Sandbox Code Playgroud)