How to exclude empty QEntity from bounding volume computation - c++

My company uses Qt3D to display its CAD models. Wee tried to use the function QCamera::viewEntity(Qt3DCore::QEntity *entity) in order to compute the bounding sphere of a given entity and also to fit the entity to the screen.
Now, we stumbled across an unsolvable problem in case of empty QEntity nodes. I'll call a node empty, if it does not contain any vertex/point after all. In this case I expected, that it should be neglected in the computation of bounding volume. Instead it seems, that it will be treated, as it has a bounding sphere with center (0.,0.,0.) and radius 0.
The following code illustrates the issue:
main.cpp
#include <QApplication>
#include <QWidget>
#include <QVBoxLayout>
#include <QFrame>
#include <Qt3DRender/QRenderSettings>
#include <Qt3DCore/QTransform>
#include <Qt3DRender/QCamera>
#include <Qt3DExtras/QSphereMesh>
#include <Qt3DExtras/QDiffuseSpecularMaterial>
#include <Qt3DExtras/QForwardRenderer>
#include <Qt3DExtras/Qt3DWindow>
#include <Qt3DRender/QCamera>
#include <Qt3DRender/QCameraLens>
#include <QPushButton>
Qt3DCore::QEntity* createSphereMesh()
{
auto sphereMat = new Qt3DExtras::QDiffuseSpecularMaterial;
sphereMat->setDiffuse(QColor(Qt::blue));
auto mesh = new Qt3DExtras::QSphereMesh();
mesh->setRadius(0.5);
auto meshEntity = new Qt3DCore::QEntity;
meshEntity->addComponent(mesh);
meshEntity->addComponent(sphereMat);
return meshEntity;
}
int main(int argc, char* argv[])
{
QApplication a(argc, argv);
auto view = new Qt3DExtras::Qt3DWindow();
view->defaultFrameGraph()->setClearColor(QColor(127, 127, 127));
auto settings = view->renderSettings();
settings->setActiveFrameGraph(view->activeFrameGraph());
auto rootEntity = new Qt3DCore::QEntity();
view->setRootEntity(rootEntity);
auto sphere1 = createSphereMesh();
sphere1->setParent(rootEntity);
auto trafo1 = new Qt3DCore::QTransform;
trafo1->setTranslation(QVector3D(20, 10, 0));
sphere1->addComponent(trafo1);
auto sphere2 = createSphereMesh();
sphere2->setParent(rootEntity);
auto trafo2 = new Qt3DCore::QTransform;
trafo2->setTranslation(QVector3D(20, -10, 0));
sphere2->addComponent(trafo2);
QObject::connect(view->camera()->lens(), &Qt3DRender::QCameraLens::viewSphere, [&](const QVector3D& center, float radius) {
qDebug() << "Bounding Sphere:" << center << radius;
auto boundingSphereEntity = new Qt3DCore::QEntity;
auto sphereMat = new Qt3DExtras::QDiffuseSpecularMaterial;
sphereMat->setAlphaBlendingEnabled(true);
sphereMat->setDiffuse(QColor(255,255,255,80));
auto mesh = new Qt3DExtras::QSphereMesh();
mesh->setRadius(radius);
boundingSphereEntity->addComponent(sphereMat);
boundingSphereEntity->addComponent(mesh);
auto trafoAll = new Qt3DCore::QTransform;
trafoAll->setTranslation(center);
boundingSphereEntity->addComponent(trafoAll);
boundingSphereEntity->addComponent(mesh);
boundingSphereEntity->addComponent(sphereMat);
boundingSphereEntity->setParent(rootEntity);
});
auto container = QWidget::createWindowContainer(view);
auto viewAllBtn = new QPushButton("View All");
QObject::connect(viewAllBtn, &QPushButton::clicked, [&]() {
view->camera()->viewAll();
});
QFrame frame;
frame.setFixedSize(500, 500);
frame.setLayout(new QVBoxLayout);
frame.layout()->addWidget(container);
frame.layout()->addWidget(viewAllBtn);
frame.show();
return a.exec();
}
I have two blue spheres with radius 0.5 being at distance 20 from each other. I'm expecting a bounding sphere with center (20,0,0) and radius (10.5).
Instead the program prints:
Bounding Sphere: QVector3D(12.1432, 2.14455, -1.32699e-08) 14.9644
It seems that the value 2.14455 comes really from nowhere and that my add bounding volume sphere is somewhat unpredictable.
If I will replace the translation QVector3D(20, 10, 0) by QVector3D(0,10,0) the result will be like expected.
How, can I exclude the root entity, from my bounding volume computation?

Related

QRenderPassFilter hides plane mesh even if QFilterKey matches

We are aiming to replace our previous 3D Engine with Qt3D. As a last obstacle we need to correctly implement a pixel correct transparency. We are now trying to implement depth peeling as a possible approach to make pixel correct transparency workable. For this algorithm one has to do perform a deferred (multipass) rendering, which can be achieved with QRenderPassFilter and QFilterKey inside an effect.
Now, I already had big problems to make the combination of QRenderPassFilter and QFilterKey together with the material QDiffuseSpecularMaterial going to work correctly. Even if there is just one pass.
This is my source code:
#include <QApplication>
#include <QWidget>
#include <QVBoxLayout>
#include <QFrame>
#include <Qt3DCore/QTransform>
#include <Qt3DRender/QSortPolicy>
#include <Qt3DRender/QRenderSettings>
#include <Qt3DRender/QRenderSurfaceSelector>
#include <Qt3DRender/QViewport>
#include <Qt3DRender/QCamera>
#include <Qt3DRender/QCameraSelector>
#include <Qt3DRender/QClearBuffers>
#include <Qt3DRender/QDirectionalLight>
#include <Qt3DRender/QTexture>
#include <Qt3DExtras/QPlaneMesh>
#include <Qt3DExtras/QDiffuseSpecularMaterial>
#include <Qt3DExtras/Qt3DWindow>
#include <Qt3DRender/QFilterKey>
#include <Qt3DRender/QParameter>
#include <Qt3DRender/QRenderPass>
#include <Qt3DRender/QRenderPassFilter>
#include <Qt3DRender/QTechnique>
#include <QDebug>
int main(int argc, char* argv[])
{
QApplication a(argc, argv);
auto view = new Qt3DExtras::Qt3DWindow();
auto mClearBuffers = new Qt3DRender::QClearBuffers;
auto mMainCameraSelector = new Qt3DRender::QCameraSelector;
mMainCameraSelector->setCamera(view->camera());
auto mRenderSurfaceSelector = new Qt3DRender::QRenderSurfaceSelector;
auto mMainViewport = new Qt3DRender::QViewport;
auto renderPassFilter = new Qt3DRender::QRenderPassFilter;
{
auto filterKey = new Qt3DRender::QFilterKey(renderPassFilter);
filterKey->setName(QStringLiteral("renderingStyle"));
filterKey->setValue(QStringLiteral("forward"));
// Adding the filterKey to the renderPassFilter hides the plane
// Name and Value of filterKey matches the FilterKey inside the QDiffuseSpecularMaterial
renderPassFilter->addMatch(filterKey); // Removing this lines shows the plane mesh
mClearBuffers->setClearColor(Qt::lightGray);
mClearBuffers->setBuffers(Qt3DRender::QClearBuffers::BufferType::ColorDepthBuffer);
mMainCameraSelector->setParent(mClearBuffers);
mClearBuffers->setParent(renderPassFilter);
}
renderPassFilter->setParent(mRenderSurfaceSelector);
mRenderSurfaceSelector->setParent(mMainViewport);
view->setActiveFrameGraph(mMainViewport);
view->activeFrameGraph()->dumpObjectTree();
auto rootEntity = new Qt3DCore::QEntity();
view->setRootEntity(rootEntity);
view->camera()->lens()->setPerspectiveProjection(45.0f, 1., 0.1f, 10000.0f);
view->camera()->setPosition(QVector3D(0, 2, 0));
view->camera()->setUpVector(QVector3D(0, 1, 0));
view->camera()->setViewCenter(QVector3D(0, 0, 0));
auto planeEntity = new Qt3DCore::QEntity(rootEntity);
auto meshMaterial = new Qt3DExtras::QDiffuseSpecularMaterial;
meshMaterial->setDiffuse(QColor("#ff00ff"));
planeEntity->addComponent(meshMaterial);
auto mesh = new Qt3DExtras::QPlaneMesh;
mesh->setWidth(0.3);
mesh->setHeight(0.3);
planeEntity->addComponent(mesh);
auto container = QWidget::createWindowContainer(view);
QFrame frame;
frame.setLayout(new QVBoxLayout);
frame.layout()->addWidget(container);
frame.resize(QSize(400, 300));
frame.show();
return a.exec();
}
The console outputs my framegraph as:
Qt3DRender::QViewport::
Qt3DRender::QRenderSurfaceSelector::
Qt3DRender::QRenderPassFilter::
Qt3DRender::QFilterKey::
Qt3DRender::QClearBuffers::
Qt3DRender::QCameraSelector::
Now, if I remove the line
renderPassFilter->addMatch(filterKey);
everything works as expected and I see my simple plane mesh.
However, adding the line, which should not filter anything the plane mesh is no longer displayed.
I'm really running out of ideas, what I'm possibly doing wrong here. How, can I make my small program with my renderPassFilter going to work and what are my errors?
I also didn't really understood, what are purposes of the settings name and value in the QFilterKey, which of both is necessary to filter out certain effects?
After carefully studying my application and particularly the QDiffuseSpecularMaterial I figured out, that the QFilterKey inside the QDiffuseSpecularMaterial is not added to the QRenderPass Object, but moreover added to the QTechnique, which I found rather obscure.
Now, adding a QTechniqueFilter instead of a QRenderPassFilter made the program working as expected. Changing the string forward to something different e.g. xxx hides the plane as expected.
Adding the line
meshMaterial->dumpObjectTree();
indeed gave me the clue
Qt3DExtras::QDiffuseSpecularMaterial::
Qt3DRender::QShaderProgramBuilder::
Qt3DRender::QShaderProgram::
Qt3DRender::QShaderProgramBuilder::
Qt3DRender::QShaderProgram::
Qt3DRender::QFilterKey::
Qt3DRender::QEffect::
Qt3DRender::QTechnique::
Qt3DRender::QRenderPass::
Qt3DRender::QNoDepthMask::
Qt3DRender::QBlendEquationArguments::
Qt3DRender::QBlendEquation::
Qt3DRender::QTechnique::
Qt3DRender::QRenderPass::
Qt3DRender::QTechnique::
Qt3DRender::QRenderPass::
Qt3DRender::QParameter::
Qt3DRender::QParameter::
Qt3DRender::QParameter::
Qt3DRender::QParameter::
Qt3DRender::QParameter::
So dumpObjectTree() seems to a good of the shelf debugging tool, when dealing with Qt3D and Qt in general.
#include <QApplication>
#include <QWidget>
#include <QVBoxLayout>
#include <QFrame>
#include <Qt3DCore/QTransform>
#include <Qt3DRender/QSortPolicy>
#include <Qt3DRender/QRenderSettings>
#include <Qt3DRender/QRenderSurfaceSelector>
#include <Qt3DRender/QViewport>
#include <Qt3DRender/QCamera>
#include <Qt3DRender/QCameraSelector>
#include <Qt3DRender/QClearBuffers>
#include <Qt3DRender/QTechniqueFilter>
#include <Qt3DRender/QDirectionalLight>
#include <Qt3DRender/QTexture>
#include <Qt3DExtras/QPlaneMesh>
#include <Qt3DExtras/QDiffuseSpecularMaterial>
#include <Qt3DExtras/Qt3DWindow>
#include <Qt3DRender/QFilterKey>
#include <Qt3DRender/QParameter>
#include <Qt3DRender/QRenderPass>
#include <Qt3DRender/QRenderPassFilter>
#include <Qt3DRender/QTechnique>
#include <QDebug>
int main(int argc, char* argv[])
{
QApplication a(argc, argv);
auto view = new Qt3DExtras::Qt3DWindow();
auto mClearBuffers = new Qt3DRender::QClearBuffers;
auto mMainCameraSelector = new Qt3DRender::QCameraSelector;
mMainCameraSelector->setCamera(view->camera());
auto mRenderSurfaceSelector = new Qt3DRender::QRenderSurfaceSelector;
auto mMainViewport = new Qt3DRender::QViewport;
auto renderPassFilter = new Qt3DRender::QTechniqueFilter;
{
auto filterKey = new Qt3DRender::QFilterKey(renderPassFilter);
filterKey->setName(QStringLiteral("renderingStyle"));
filterKey->setValue(QStringLiteral("forward"));
// Adding the filterKey to the renderPassFilter hides the plane
// Name and Value of filterKey matches the FilterKey inside the QDiffuseSpecularMaterial
renderPassFilter->addMatch(filterKey); // Removing this lines shows the plane mesh
mClearBuffers->setClearColor(Qt::lightGray);
mClearBuffers->setBuffers(Qt3DRender::QClearBuffers::BufferType::ColorDepthBuffer);
mMainCameraSelector->setParent(mClearBuffers);
mClearBuffers->setParent(renderPassFilter);
}
renderPassFilter->setParent(mRenderSurfaceSelector);
mRenderSurfaceSelector->setParent(mMainViewport);
view->setActiveFrameGraph(mMainViewport);
view->activeFrameGraph()->dumpObjectTree();
auto rootEntity = new Qt3DCore::QEntity();
view->setRootEntity(rootEntity);
view->camera()->lens()->setPerspectiveProjection(45.0f, 1., 0.1f, 10000.0f);
view->camera()->setPosition(QVector3D(0, 2, 0));
view->camera()->setUpVector(QVector3D(0, 1, 0));
view->camera()->setViewCenter(QVector3D(0, 0, 0));
auto planeEntity = new Qt3DCore::QEntity(rootEntity);
auto meshMaterial = new Qt3DExtras::QDiffuseSpecularMaterial;
meshMaterial->setDiffuse(QColor("#ff00ff"));
planeEntity->addComponent(meshMaterial);
auto mesh = new Qt3DExtras::QPlaneMesh;
mesh->setWidth(0.3);
mesh->setHeight(0.3);
planeEntity->addComponent(mesh);
auto container = QWidget::createWindowContainer(view);
QFrame frame;
frame.setLayout(new QVBoxLayout);
frame.layout()->addWidget(container);
frame.resize(QSize(400, 300));
frame.show();
return a.exec();
}

How to add rendersetting component to my root entity in qt3d?

I am working on some codes about qt3d. I create a Qt3DWindow(named view).
Try to add a root entity(named rootEntity) in it.
Put a cube entity like this:
m_cubeEntity = new Qt3DCore::QEntity;
...
Qt3DExtras::QCuboidMesh *cubeMesh = new Qt3DExtras::QCuboidMesh;
Qt3DRender::QMaterial *cubeMaterial = new Qt3DRender::QMaterial;
Qt3DCore::QTransform *cubeTransform = new Qt3DCore::QTransform;
Qt3DRender::QObjectPicker *objectPicker = new Qt3DRender::QObjectPicker;
...
m_cubeEntity.addComponent(cubeMesh);
m_cubeEntity.addComponent(cubeMaterial);
m_cubeEntity.addComponent(cubeTransform);
m_cubeEntity.adComponent(objectPicker);
m_cubeEntity.setParent(m_rootEntity);
Everything works fine.
And then I find using PickingSettings.BoundingVolumePicking to pick my cube entity is inaccuracy. I want the PickingSettings.TrianglePicking.
====================================================================/
So then i do it like this:
m_renderSettings = new Qt3DRender::QRenderSettings();
m_renderSettings->pickingSettings()->setPickMethod(Qt3DRender::QPickingSettings::TrianglePicking);
m_renderSettings->pickingSettings()->setPickResultMode(Qt3DRender::QPickingSettings::AllPicks);
m_renderer = new Qt3DExtras::QForwardRenderer();
m_renderer->setClearColor(Qt::lightGray);
m_renderSettings->setActiveFrameGraph(m_renderer);
m_rootEntity->addComponent(m_renderSettings);
But now nothing is rendered. If i remove "m_renderSettings" from rootEntity, everything returns correct.
How to set rendersetting correctly for the root entity?
Most likely you just set a bad framegraph. You can easily display the standard framegraph using dumpObjectTree(), which tends to be a very useful function in debugging the framegraph of Qt3D.
For your simple use case it suffices to just use the renderSettings already contained in the activeFrameGraph() of the Qt3DWindow.
Just try the following simple app, that contains your desired QObjectPicker.
#include <QApplication>
#include <Qt3DCore/QTransform>
#include <Qt3DRender/QPickEvent>
#include <Qt3DRender/QCamera>
#include <Qt3DRender/QFrameGraphNode>
#include <Qt3DRender/QObjectPicker>
#include <Qt3DExtras/QDiffuseSpecularMaterial>
#include <Qt3DExtras/QCuboidMesh>
#include <Qt3DExtras/Qt3DWindow>
#include <Qt3DRender/QRenderSettings>
#include <QDebug>
int main(int argc, char* argv[])
{
QApplication a(argc, argv);
auto view = new Qt3DExtras::Qt3DWindow();
auto rootEntity = new Qt3DCore::QEntity();
view->setRootEntity(rootEntity);
// Shows your framegraph! Simple forward renderer!
view->activeFrameGraph()->dumpObjectTree();
auto rendersettings=view->renderSettings();
rendersettings->pickingSettings()->setPickMethod(Qt3DRender::QPickingSettings::TrianglePicking);
rendersettings->pickingSettings()->setPickResultMode(Qt3DRender::QPickingSettings::AllPicks);
auto cameraEntity = view->camera();
cameraEntity->lens()->setPerspectiveProjection(45.0f, 1., 0.1f, 10000.0f);
cameraEntity->setPosition(QVector3D(0, 2, 0));
cameraEntity->setUpVector(QVector3D(0, 1, 0));
cameraEntity->setViewCenter(QVector3D(0, 0, 0));
auto cubeEntity = new Qt3DCore::QEntity(rootEntity);
auto cubeMesh = new Qt3DExtras::QCuboidMesh;
cubeMesh->setXExtent(1.);
cubeMesh->setYExtent(1.);
auto cubeMaterial = new Qt3DExtras::QDiffuseSpecularMaterial;
auto objectPicker = new Qt3DRender::QObjectPicker;
QObject::connect(objectPicker, &Qt3DRender::QObjectPicker::clicked, [](Qt3DRender::QPickEvent* pick) {
qDebug() << pick;
});
cubeEntity->addComponent(cubeMesh);
cubeEntity->addComponent(cubeMaterial);
cubeEntity->addComponent(objectPicker);
view->show();
return a.exec();
}

Disable all light sources in Qt 3d

In my company there is a shift from an old 3D engine to Qt3d. One goal of this undertaking is to compare the rendered view of the old 3D engine with the Qt3d rendering.
For this I wrote a small example application, where I can compare old and new rendering. There are still plenty differences. My first idea was to switch of all light sources in both engines and compare the silhouettes of both renderings.
Now, there is some thing, that I really don't understand and this has to do with the Qt3d lighting model.
In my small example app I define a simple sphere mesh and a camera and a check box, which can disable a point light source. The sphere is lighted by the phong reflection model.
No, if I switch out mylight, I'm expecting a simple black sphere in my viewer, as there is really no light. Instead there is still some lighting (from a different source). I think, that there is some other light source, that is activated by default.
How, can I disable all light sources in Qt3d? As a side question I'm also wondering why meshMaterial->setAmbient(QColor(255, 0, 0)); has no visible effect on the material after all. It really doesn't matter what you will enter here.
#include <QApplication>
#include <QWidget>
#include <QCheckBox>
#include <QVBoxLayout>
#include <QFrame>
#include <Qt3DCore/QTransform.h>
#include <Qt3DRender/QCamera.h>
#include <Qt3DRender/QRenderSettings.h>
#include <Qt3DRender/QPointLight.h>
#include <Qt3DExtras/QSphereMesh>
#include <Qt3DExtras/QPhongMaterial>
#include <Qt3DExtras/qforwardrenderer.h>
#include <Qt3DExtras/Qt3DWindow.h>
#include <Qt3DExtras/QFirstPersonCameraController.h>
int main(int argc, char* argv[])
{
QApplication a(argc, argv);
auto view = new Qt3DExtras::Qt3DWindow();
view->defaultFrameGraph()->setClearColor(QColor(255,255,255));
auto rootEntity = new Qt3DCore::QEntity();
view->setRootEntity(rootEntity);
auto cameraEntity = view->camera();
cameraEntity->lens()->setPerspectiveProjection(45.0f, 1., 0.1f, 10000.0f);
cameraEntity->setPosition(QVector3D(5, 5, 5));
cameraEntity->setUpVector(QVector3D(0, 1, 0));
cameraEntity->setViewCenter(QVector3D(0, 0, 0));
auto lightEntity = new Qt3DCore::QEntity(rootEntity);
auto light = new Qt3DRender::QPointLight(lightEntity);
light->setColor("white");
light->setIntensity(1);
lightEntity->addComponent(light);
auto lightTransform = new Qt3DCore::QTransform(lightEntity);
lightTransform->setTranslation(cameraEntity->position());
lightEntity->addComponent(lightTransform);
lightEntity->setEnabled(false);
// For camera controls
auto camController = new Qt3DExtras::QFirstPersonCameraController(rootEntity);
camController->setCamera(cameraEntity);
auto mesh = new Qt3DExtras::QSphereMesh();
mesh->setRadius(1.);
auto meshMaterial = new Qt3DExtras::QPhongMaterial();
meshMaterial->setDiffuse(QColor(0, 255, 0));
meshMaterial->setAmbient(QColor(255, 0, 0));
meshMaterial->setSpecular(QColor(0,0,255));
meshMaterial->setShininess(23);
auto meshEntity = new Qt3DCore::QEntity(rootEntity);
meshEntity->addComponent(mesh);
meshEntity->addComponent(meshMaterial);
meshEntity->setEnabled(true);
auto disableLight = new QCheckBox();
auto container = QWidget::createWindowContainer(view);
QFrame frame;
frame.setLayout(new QVBoxLayout);
frame.layout()->addWidget(container);
frame.layout()->addWidget(disableLight);
QObject::connect(disableLight, &QCheckBox::stateChanged, [lightEntity](auto state) {
lightEntity->setEnabled(state == Qt::CheckState::Checked);
});
frame.setFixedSize(500, 500);
frame.show();
return a.exec();
}
If no light entities are created in your Qt3D scene Qt3D will add one for you. This is to prevent users not seeing anything without light.
Once you add a light yourself the default one is omitted
You can workaround this default behaviour by adding a light with intensity set to zero:
DirectionalLight {
worldDirection: Qt.vector3d(-1, 1, -1)
intensity: 0.0
}
This will give you the following effect:
test with cuboid and sphere mesh with PhongMaterial:
So tinkering with the intensity property of the light might give what you want.

Reverse axis of QPolarChart

I need to modify a QPolarChart to create a Satellite SkyPlot.
For this, I need to reverse the radial axis (elevation axis) so that 90 is placed on the origin and 0 on the outer ring.
In the list of members for QValueAxis I found the method setReverse, which seems to do what I need.
However, doing setReverse(true) on the elevation axis does not flip the axis as I was expecting.
How can I reverse the axis then?
This is the minimal code required to run this example:
#include <QtWidgets/QApplication>
#include <QtWidgets/QMainWindow>
#include <QtCharts/QChartView>
#include <QtCharts/QPolarChart>
#include <QtCharts/QValueAxis>
#include <QScatterSeries>
QT_CHARTS_USE_NAMESPACE
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QtCharts::QPolarChart *chart = new QtCharts::QPolarChart();
chart->legend()->hide();
QScatterSeries *series = new QScatterSeries();
for (int i = 0; i < 360; i += 10) {
series->append(i, i);
}
chart->addSeries(series);
QValueAxis *azimuthAxis = new QValueAxis();
azimuthAxis->setRange(0, 360);
azimuthAxis->setTickCount(9);
azimuthAxis->setLabelFormat("%d");
azimuthAxis->setLabelsVisible(true);
chart->addAxis(azimuthAxis, QPolarChart::PolarOrientationAngular);
QValueAxis *elevationAxis = new QValueAxis();
elevationAxis->setRange(0, 90);
elevationAxis->setTickCount(7);
elevationAxis->setLabelFormat("%d");
elevationAxis->setLabelsVisible(true);
elevationAxis->setReverse(true); // <-- REVERSE THE AXIS
chart->addAxis(elevationAxis, QPolarChart::PolarOrientationRadial);
QtCharts::QChartView *chartView = new QtCharts::QChartView();
chartView->setChart(chart);
chartView->setRenderHint(QPainter::Antialiasing);
QMainWindow window;
window.setCentralWidget(chartView);
window.resize(800, 600);
window.show();
return app.exec();
}
UPDATE:
After reading Damien's comment:
You may check with isReverse if the command setReverse was taken into account
I made the test by putting qDebug() << elevationAxis->isReverse(); immediately before and immediately after elevationAxis->setReverse(true);, and it prints out false and true, respectively.
qDebug() << elevationAxis->isReverse(); // <-- Prints out "false"
elevationAxis->setReverse(true); // <-- REVERSE THE AXIS
qDebug() << elevationAxis->isReverse(); // <-- Prints out "true"
So the reverse property is indeed changed. However, this change is not reflected visually in the axis.
The documentation for the reverse property reads:
This property holds whether a reverse axis is used.
By default, the value is false.
The reverse axis is supported with a line, spline, and scatter series,
as well as an area series with a cartesian chart. All axes of the same
orientation attached to the same series must be reversed if one is
reversed or the behavior is undefined.
A polar chart is certainly NOT a cartesian chart, so that might be the root of the problem.
I couldn't make the setReversed(true) work whatever the way I tried to do it.
So I found another solution: Do it manually.
I printed the series in reversed way:
QScatterSeries *series = new QScatterSeries();
for (int i = 0; i < 360; i += 10) {
series->append(i, MAX_ELEVATION - i);
}
MAX_ELEVATION equals to 90.
Then I reversed the labels names. For that I had to replace QValueAxis by its derived QCategoryAxis.
The full code
#include <QtWidgets/QApplication>
#include <QtWidgets/QMainWindow>
#include <QtCharts/QChartView>
#include <QtCharts/QPolarChart>
#include <QtCharts/QValueAxis>
#include <QScatterSeries>
#include <QtCharts/QCategoryAxis>
QT_CHARTS_USE_NAMESPACE
#define MAX_ELEVATION 90
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QtCharts::QPolarChart *chart = new QtCharts::QPolarChart();
chart->legend()->hide();
QScatterSeries *series = new QScatterSeries();
for (int i = 0; i < 360; i += 10) {
series->append(i, MAX_ELEVATION - i);
}
chart->addSeries(series);
QValueAxis *azimuthAxis = new QValueAxis();
azimuthAxis->setRange(0, 360);
azimuthAxis->setTickCount(9);
azimuthAxis->setLabelFormat("%d");
azimuthAxis->setLabelsVisible(true);
chart->addAxis(azimuthAxis, QPolarChart::PolarOrientationAngular);
/*
QValueAxis *elevationAxis = new QValueAxis();
elevationAxis->setRange(0, 90);
elevationAxis->setTickCount(7);
elevationAxis->setLabelFormat("%d");
elevationAxis->setLabelsVisible(true);
elevationAxis->setReverse(true); // <-- REVERSE THE AXIS
chart->addAxis(elevationAxis, QPolarChart::PolarOrientationRadial);
*/
QCategoryAxis *elevationAxis = new QCategoryAxis;
elevationAxis->setRange(0, MAX_ELEVATION);
for(unsigned int i = 0; i <= MAX_ELEVATION; i += 15)
elevationAxis->append(QString::number(MAX_ELEVATION-i), i);
elevationAxis->setLabelsPosition(QCategoryAxis::AxisLabelsPositionOnValue);
elevationAxis->setLabelsVisible(true);
chart->addAxis(elevationAxis, QPolarChart::PolarOrientationRadial);
QtCharts::QChartView *chartView = new QtCharts::QChartView();
chartView->setChart(chart);
chartView->setRenderHint(QPainter::Antialiasing);
QMainWindow window;
window.setCentralWidget(chartView);
window.resize(800, 600);
window.show();
return app.exec();
}
It is equivalent to what setReversed(true) should do. It will display your series exactly the way you want without modifying anything of the way the data are computed.
Here you can see what it looks like:
It worked successfully for me.
Hope it can help.

Qt3D SceneLoader entity is not rendered

I'm trying to create a Qt3D prototype which is able to render an obj which contains transparent objects. Therefor I need to somehow incorporate QSortPolicy with a custom frame graph. I put together a few examples which I found on the internet (unfortunately there aren't many Qt3D C++ examples). The source of the protoype is shown below. The problem is that I add a QTorusMesh and the content of the QSceneLoader to the root entity, but when rendered only the torus will be shown, the obj (in this case the monkey) is not rendered and I don't know why and also not how to debug it. I dumped all the trees (scene graph and frame graph) and couldn't find any inconsistency. Only thing I can think of that the loaded object somehow needs a different renderer (QGeometryRenderer) which needs to be added to the frame graph? Does someone know what I'm doing wrong?
Examples: Custom frame graph, Scene walker
#include <QGuiApplication>
#include <Qt3DCore/QEntity>
#include <Qt3DCore/QTransform>
#include <Qt3DCore/QAspectEngine>
#include <Qt3DInput/QInputAspect>
#include <Qt3DExtras/Qt3DWindow>
#include <Qt3DExtras/QPhongMaterial>
#include <Qt3DExtras/QOrbitCameraController>
#include <Qt3DExtras/QTorusMesh>
#include <Qt3DRender/QCamera>
#include <Qt3DRender/QRenderAspect>
#include <Qt3DRender/QSceneLoader>
#include <Qt3DRender/QRenderSurfaceSelector>
#include <Qt3DRender/QClearBuffers>
#include <Qt3DRender/QLayerFilter>
#include <Qt3DRender/QViewport>
#include <Qt3DRender/QCameraSelector>
#include <Qt3DRender/QLayer>
#include <Qt3DRender/QRenderSettings>
class SceneWalker : public QObject
{
public:
SceneWalker(Qt3DRender::QSceneLoader* loader):
m_loader(loader)
{}
void onStatusChanged();
private:
void walkEntity(Qt3DCore::QEntity* e, int depth = 0);
Qt3DRender::QSceneLoader* m_loader;
};
void SceneWalker::onStatusChanged()
{
qDebug() << "Status changed:" << m_loader->status();
if (m_loader->status() != Qt3DRender::QSceneLoader::Ready)
return;
// The QSceneLoader instance is a component of an entity. The loaded scene
// tree is added under this entity.
QVector<Qt3DCore::QEntity*> entities = m_loader->entities();
// Technically there could be multiple entities referencing the scene loader
// but sharing is discouraged, and in our case there will be one anyhow.
if (entities.isEmpty())
return;
Qt3DCore::QEntity* root = entities[0];
// Print the tree.
walkEntity(root);
// To access a given node (like a named mesh in the scene), use QObject::findChild().
// The scene structure and names always depend on the asset.
Qt3DCore::QEntity* e = root->findChild<Qt3DCore::QEntity*>(QStringLiteral("PlanePropeller_mesh")); // toyplane.obj
if (e)
qDebug() << "Found propeller node" << e << "with components" << e->components();
}
void SceneWalker::walkEntity(Qt3DCore::QEntity* e, int depth)
{
Qt3DCore::QNodeVector nodes = e->childNodes();
for (int i = 0; i < nodes.count(); ++i)
{
Qt3DCore::QNode* node = nodes[i];
Qt3DCore::QEntity* entity = qobject_cast<Qt3DCore::QEntity*>(node);
if (entity)
{
QString indent;
indent.fill(' ', depth * 2);
qDebug().noquote() << indent << "Entity:" << entity << "Components:" << entity->components();
walkEntity(entity, depth + 1);
}
}
}
int main(int argc, char* argv[])
{
QGuiApplication app(argc, argv);
Qt3DExtras::Qt3DWindow* window = new Qt3DExtras::Qt3DWindow();
// Root
Qt3DCore::QEntity* rootEntity = new Qt3DCore::QEntity();
window->setRootEntity(rootEntity);
Qt3DRender::QRenderSurfaceSelector *renderSurfaceSelector = new Qt3DRender::QRenderSurfaceSelector();
renderSurfaceSelector->setSurface(window);
// clearing the buffers
Qt3DRender::QClearBuffers* clearBuffers = new Qt3DRender::QClearBuffers(renderSurfaceSelector);
clearBuffers->setBuffers(Qt3DRender::QClearBuffers::ColorDepthBuffer);
// Framegraph for objects
Qt3DRender::QLayerFilter* objectsLayerFilter = new Qt3DRender::QLayerFilter(renderSurfaceSelector);
Qt3DRender::QLayer* objectsLayer = new Qt3DRender::QLayer(objectsLayerFilter);
objectsLayerFilter->addLayer(objectsLayer);
Qt3DRender::QViewport* viewport = new Qt3DRender::QViewport(objectsLayer);
Qt3DRender::QCameraSelector* objectsCameraSelector = new Qt3DRender::QCameraSelector(viewport);
Qt3DRender::QCamera* objectsCamera = new Qt3DRender::QCamera(objectsCameraSelector);
objectsCamera->lens()->setPerspectiveProjection(45.f, 16.0f/9.0f, 0.01f, 1000.f);
objectsCamera->setPosition(QVector3D(0, 0, -10));
objectsCamera->setViewCenter(QVector3D(0, 0, 0));
objectsCamera->setUpVector(QVector3D(0, 1, 0));
objectsCameraSelector->setCamera(objectsCamera);
// Set the new framegraph
window->setActiveFrameGraph(renderSurfaceSelector);
window->renderSettings()->setRenderPolicy(Qt3DRender::QRenderSettings::Always);
// camera controls
Qt3DExtras::QOrbitCameraController* camController = new Qt3DExtras::QOrbitCameraController(rootEntity);
camController->setLinearSpeed(50.0f);
camController->setLookSpeed(180.0f);
camController->setCamera(objectsCamera);
// Torus
Qt3DCore::QEntity* torusEntity = new Qt3DCore::QEntity(rootEntity);
Qt3DExtras::QTorusMesh* torusMesh = new Qt3DExtras::QTorusMesh(torusEntity);
torusMesh->setSlices(50.0f);
torusMesh->setRings(50.0f);
torusMesh->setRadius(2.0f);
Qt3DExtras::QPhongMaterial* torusMaterial = new Qt3DExtras::QPhongMaterial(torusEntity);
torusMaterial->setAmbient(Qt::gray);
Qt3DCore::QTransform* torusTransform = new Qt3DCore::QTransform(torusEntity);
torusTransform->setTranslation(QVector3D(0.0f, 0.0f, 10.0f));
torusTransform->setRotationY(50.0f);
torusTransform->setScale(2.0f);
torusEntity->addComponent(torusTransform);
torusEntity->addComponent(torusMesh);
torusEntity->addComponent(torusMaterial);
torusEntity->addComponent(objectsLayer);
// Scene loader
Qt3DCore::QEntity* sceneLoaderEntity = new Qt3DCore::QEntity(rootEntity);
Qt3DRender::QSceneLoader* sceneLoader = new Qt3DRender::QSceneLoader(sceneLoaderEntity);
sceneLoader->setSource(QUrl::fromLocalFile("monkey.obj"));
// Transform
Qt3DCore::QTransform* sceneLoaderTransform = new Qt3DCore::QTransform(sceneLoaderEntity);
sceneLoaderTransform->setScale(2.0f);
sceneLoaderTransform->setTranslation(QVector3D(0.0f, 0.0f, 10.0f));
SceneWalker sceneWalker(sceneLoader);
QObject::connect(sceneLoader, &Qt3DRender::QSceneLoader::statusChanged, &sceneWalker, &SceneWalker::onStatusChanged);
sceneLoaderEntity->addComponent(sceneLoader);
sceneLoaderEntity->addComponent(sceneLoaderTransform);
sceneLoaderEntity->addComponent(objectsLayer);
window->show();
return app.exec();
}
The problem is that you are not actually adding the monkey to the objects layer. QSceneLoader creates its own scene subtree, even when the OBJ file contains a single object. So you are just adding the root entity of the subtree to the objects layer.
The SceneWalker object traverses the QSceneLoader subtree and provides information about its structure. You can just look at the console output, find the names of the entities you want to render, and attach them to the desired layer.
This has to be done asynchronously, using signals and slots. Do this right after creating the QSceneLoader object (assuming your mesh is named "monkey"):
QObject::connect(sceneLoader, &Qt3DRender::QSceneLoader::statusChanged, &app,
[sceneLoader, objectsLayer](Qt3DRender::QSceneLoader::Status s) {
if (s == Qt3DRender::QSceneLoader::Status::Ready)
sceneLoader->entity("monkey")->addComponent(objectsLayer);
});
Since you are using an OBJ file, you can even ditch the SceneWalker entirely. OBJ files are text-based, so you can find the name of the mesh by just opening it with a text editor.
If you succeed with layer filters, you don't need to use QSortPolicy (check out this answer).