#include "stdafx.h"
#include "ApplicationOpenGLConfiguration.h"

ApplicationOpenGLConfiguration::ApplicationOpenGLConfiguration(QObject *parent)
    : QObject(parent)
{
}

ApplicationOpenGLConfiguration::~ApplicationOpenGLConfiguration()
{
}

void ApplicationOpenGLConfiguration::initialize()
{
    //
    // The following is required to prevent display artifacts, including clipping of graph data,
    // when Windows 10, Settings, Display, Scale and Layout is set to anything other than 100%.
    //

    QGuiApplication::setAttribute (Qt::ApplicationAttribute::AA_EnableHighDpiScaling);
    QGuiApplication::setHighDpiScaleFactorRoundingPolicy (Qt::HighDpiScaleFactorRoundingPolicy::PassThrough);

    //
    // Enable multisampling for QOpenGLWidgets
    //
    QSurfaceFormat surfaceFormat;
    surfaceFormat.setSamples(16);
    surfaceFormat.setOption(QSurfaceFormat::DebugContext);

    const QOpenGLContext::OpenGLModuleType moduleType = QOpenGLContext::openGLModuleType();

    // Request OpenGL 3.3 core or OpenGL ES 3.0.
    if (moduleType == QOpenGLContext::LibGL)
    {
        qDebug("Requesting OpenGL 3.3 core context.");
        surfaceFormat.setVersion(3, 3);
        surfaceFormat.setProfile(QSurfaceFormat::CoreProfile);
    }
    else
    {
        qDebug("Requesting OpenGL ES 3.0 context.");
        surfaceFormat.setVersion(3, 0);
    }

    QSurfaceFormat::setDefaultFormat(surfaceFormat);
}

void ApplicationOpenGLConfiguration::setHighDpiScalingAndRoundDown ()
{
    //
    // The following is required to prevent display artifacts, including clipping of graph data,
    // when Windows 10, Settings, Display, Scale and Layout is set to anything other than 100%.
    //

    QGuiApplication::setAttribute (Qt::ApplicationAttribute::AA_EnableHighDpiScaling);
    QGuiApplication::setHighDpiScaleFactorRoundingPolicy (Qt::HighDpiScaleFactorRoundingPolicy::PassThrough);
}
