Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I tried the demo in the project, and the correct aspect ratio cannot be displayed. #36

Open
fafa1899 opened this issue Apr 4, 2020 · 7 comments

Comments

@fafa1899
Copy link

fafa1899 commented Apr 4, 2020

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

@mathieu
Copy link
Collaborator

mathieu commented Apr 6, 2020

Hi @fafa1899 do you happen to have any screenshots?

@fafa1899
Copy link
Author

fafa1899 commented Apr 7, 2020

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:
bug-img1

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:

bug-img2

Looks like there is a problem with the aspect ratio, or should the code be adjusted to get normal results?

Thanks Reply.

@gramnation
Copy link

gramnation commented Aug 21, 2020

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.

@gramnation
Copy link

See #42

@HorrZzz
Copy link

HorrZzz commented Mar 9, 2024

Should this issue be closed? @fafa1899 @mathieu

@fafa1899
Copy link
Author

fafa1899 commented Mar 11, 2024

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();
}

@HorrZzz
Copy link

HorrZzz commented Mar 11, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants