osgQt
osgQt copied to clipboard
I tried the demo in the project, and the correct aspect ratio cannot be displayed.
Hello there! This project is great because it is implemented from QOpenGLWidget integration. When I tried its own example, osgviewerQt, I found that there was a problem with the aspect ratio of the example. By adding a line of code: widget.resize (200, 600); The aspect ratio displayed is even more strange, it seems that this example does not set the correct viewport or crop. How can I adjust the code to display the correct aspect ratio? Best regards
osgQt: master version osg: 3.6.4 env: Windows 10 with vs 2017
Hi @fafa1899 do you happen to have any screenshots?
Hi @mathieu
At first, after building osgQt through CMake and compiling VS2017, I directly run it in CMD through the compiled osgviewerQt program:
osgviewerQt.exe simple.earth
The rendered earth is slightly deformed:
So, to verify this problem, I added the following line of code to the end of osgviewerQt:
widget.resize(200, 600);
widget.show();
return app.exec();
"Widget.resize (200, 600);" is the code I added. This time I directly display the cow.osg that comes with OSG:
Looks like there is a problem with the aspect ratio, or should the code be adjusted to get normal results?
Thanks Reply.
Same here. The demo shows correct proportions when it calls widget.show(), but adding the same widget through QMainWindow::setCentralWidget and then calling mainwindow.show() over-stretches the cow horizontally on my system (Qt5 v5.9.7).
~~Calling QWidget::setFixedSize before ::show() can hide this issue. Not suggesting it's a fix.~~
See #42
Should this issue be closed? @fafa1899 @mathieu
Should this issue be closed? @fafa1899 @mathieu
yes. The correct answer is to set the correct aspect ratio when setting up the perspective projection, for example:
#include <QApplication>
#include <QSurfaceFormat>
#include <iostream>
#include <osgDB/ReadFile>
#include <osgGA/AnimationPathManipulator>
#include <osgGA/StateSetManipulator>
#include <osgGA/TrackballManipulator>
#include <osgQOpenGL/osgQOpenGLWidget>
#include <osgUtil/Optimizer>
#include <osgViewer/Viewer>
#include <osgViewer/ViewerEventHandlers>
int main(int argc, char* argv[]) {
QSurfaceFormat format = QSurfaceFormat::defaultFormat();
#ifdef OSG_GL3_AVAILABLE
format.setVersion(3, 2);
format.setProfile(QSurfaceFormat::CoreProfile);
format.setRenderableType(QSurfaceFormat::OpenGL);
format.setOption(QSurfaceFormat::DebugContext);
#else
format.setVersion(2, 0);
format.setProfile(QSurfaceFormat::CompatibilityProfile);
format.setRenderableType(QSurfaceFormat::OpenGL);
format.setOption(QSurfaceFormat::DebugContext);
#endif
format.setDepthBufferSize(24);
// format.setAlphaBufferSize(8);
format.setSamples(8);
format.setStencilBufferSize(8);
format.setSwapBehavior(QSurfaceFormat::DoubleBuffer);
QSurfaceFormat::setDefaultFormat(format);
QApplication app(argc, argv);
osgQOpenGLWidget widget;
QObject::connect(&widget, &osgQOpenGLWidget::initialized, [&widget] {
// set up the camera manipulators.
widget.getOsgViewer()->setCameraManipulator(
new osgGA::TrackballManipulator());
// add the state manipulator
widget.getOsgViewer()->addEventHandler(new osgGA::StateSetManipulator(
widget.getOsgViewer()->getCamera()->getOrCreateStateSet()));
// add the thread model handler
widget.getOsgViewer()->addEventHandler(new osgViewer::ThreadingHandler);
// add the window size toggle handler
widget.getOsgViewer()->addEventHandler(new osgViewer::WindowSizeHandler);
// add the stats handler
widget.getOsgViewer()->addEventHandler(new osgViewer::StatsHandler);
// add the record camera path handler
widget.getOsgViewer()->addEventHandler(
new osgViewer::RecordCameraPathHandler);
// add the LOD Scale handler
widget.getOsgViewer()->addEventHandler(new osgViewer::LODScaleHandler);
// add the screen capture handler
widget.getOsgViewer()->addEventHandler(new osgViewer::ScreenCaptureHandler);
// load the data
std::string filename = "C:/Data/001/010137001.obj";
osg::ref_ptr<osg::Node> loadedModel = osgDB::readRefNodeFile(filename);
// optimize the scene graph, remove redundant nodes and state etc.
osgUtil::Optimizer optimizer;
optimizer.optimize(loadedModel);
widget.getOsgViewer()->setSceneData(loadedModel);
//Increase aspect ratio setting
QSize size = widget.size();
float aspectRatio =
static_cast<float>(size.width()) / static_cast<float>(size.height());
widget.getOsgViewer()->getCamera()->setProjectionMatrixAsPerspective(
60.f, aspectRatio, 1.f, 1000.f);
return 0;
});
widget.resize(200, 600);
widget.show();
return app.exec();
}
Should this issue be closed? @fafa1899 @mathieu
yes. The correct answer is to set the correct aspect ratio when setting up the perspective projection, for example:
#include <QApplication> #include <QSurfaceFormat> #include <iostream> #include <osgDB/ReadFile> #include <osgGA/AnimationPathManipulator> #include <osgGA/StateSetManipulator> #include <osgGA/TrackballManipulator> #include <osgQOpenGL/osgQOpenGLWidget> #include <osgUtil/Optimizer> #include <osgViewer/Viewer> #include <osgViewer/ViewerEventHandlers> int main(int argc, char* argv[]) { QSurfaceFormat format = QSurfaceFormat::defaultFormat(); #ifdef OSG_GL3_AVAILABLE format.setVersion(3, 2); format.setProfile(QSurfaceFormat::CoreProfile); format.setRenderableType(QSurfaceFormat::OpenGL); format.setOption(QSurfaceFormat::DebugContext); #else format.setVersion(2, 0); format.setProfile(QSurfaceFormat::CompatibilityProfile); format.setRenderableType(QSurfaceFormat::OpenGL); format.setOption(QSurfaceFormat::DebugContext); #endif format.setDepthBufferSize(24); // format.setAlphaBufferSize(8); format.setSamples(8); format.setStencilBufferSize(8); format.setSwapBehavior(QSurfaceFormat::DoubleBuffer); QSurfaceFormat::setDefaultFormat(format); QApplication app(argc, argv); osgQOpenGLWidget widget; QObject::connect(&widget, &osgQOpenGLWidget::initialized, [&widget] { // set up the camera manipulators. widget.getOsgViewer()->setCameraManipulator( new osgGA::TrackballManipulator()); // add the state manipulator widget.getOsgViewer()->addEventHandler(new osgGA::StateSetManipulator( widget.getOsgViewer()->getCamera()->getOrCreateStateSet())); // add the thread model handler widget.getOsgViewer()->addEventHandler(new osgViewer::ThreadingHandler); // add the window size toggle handler widget.getOsgViewer()->addEventHandler(new osgViewer::WindowSizeHandler); // add the stats handler widget.getOsgViewer()->addEventHandler(new osgViewer::StatsHandler); // add the record camera path handler widget.getOsgViewer()->addEventHandler( new osgViewer::RecordCameraPathHandler); // add the LOD Scale handler widget.getOsgViewer()->addEventHandler(new osgViewer::LODScaleHandler); // add the screen capture handler widget.getOsgViewer()->addEventHandler(new osgViewer::ScreenCaptureHandler); // load the data std::string filename = "C:/Data/001/010137001.obj"; osg::ref_ptr<osg::Node> loadedModel = osgDB::readRefNodeFile(filename); // optimize the scene graph, remove redundant nodes and state etc. osgUtil::Optimizer optimizer; optimizer.optimize(loadedModel); widget.getOsgViewer()->setSceneData(loadedModel); //Increase aspect ratio setting QSize size = widget.size(); float aspectRatio = static_cast<float>(size.width()) / static_cast<float>(size.height()); widget.getOsgViewer()->getCamera()->setProjectionMatrixAsPerspective( 60.f, aspectRatio, 1.f, 1000.f); return 0; }); widget.resize(200, 600); widget.show(); return app.exec(); }
Thank you very much @fafa1899