Setting Maximum Samples for AntiAliasing with an OpenGL-enabled QWindow ...



  • Hello,

    I'd like to set the maximum number of FSAA anti-aliasing samples in my QWindow upon creation/initialization.

    Currently, the constructor for my QWindow looks like this:

    @
    setSurfaceType(QWindow::OpenGLSurface);
    QSurfaceFormat QTOpenGLProperties;
    QTOpenGLProperties.setProfile(QTOpenGLProperties.CompatibilityProfile);
    /*

    This code errors out because I would need to call initializeOpenGLFunctions() first.

    int MaxSamples = 0;
    glGetIntegerv(GL_MAX_SAMPLES, &MaxSamples);
    QTOpenGLProperties.setSamples(MaxSamples);

    */
    QTOpenGLProperties.setSamples(4);
    setFormat(QTOpenGLProperties);
    setAnimating(true);
    installEventFilter(this);
    @

    What I would like to do is call the setSamples() function with the maximum number of samples the graphics card supports.

    Of course, later in the program I would still need to call glENABLE(GL_MULTISAMPLE) to enable multisampling.

    So how can I set the samples to be the maximum number?

    Thank you.


  • Moderators

    It's a classic OpenGL context chicken and egg problem. To get GL_MAX_SAMPLES you need a context and to create a context you need that value.

    The solution is to create a dummy context, make it current, get the value, destroy the dummy and use the value in your "real" context creation.

    Something like this:
    @
    GLint getMaxSamples() {
    GLint maxSamples = 0;
    QOffscreenSurface sfc;
    sfc.create();
    QOpenGLContext ctx;
    if(ctx.create()) {
    ctx.makeCurrent(&sfc);
    glGetIntegerv(GL_MAX_SAMPLES, &maxSamples);
    ctx.doneCurrent();
    }
    return maxSamples;
    }
    @

    The other way is to set the sample count to some high value (e.g. 1000) and when the context is created it will give you the most it can. You can then check with samples() what value was used. I'm not sure this is a defined behavior (might be, haven't checked) but it works at least on my machine.



  • I see; looks good.

    I will try this and update the post on how it goes.

    Thank you very much.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.