OpenGL Multisampling

Hello,

I have a problem with multisampling. This is what I do:

Before calling “SDL_SetVideoMode(… SDL_OPENGL)” I set the appropriate
OpenGl-attributes like this:
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 4);

After video mode is set using the call mentioned above I say
glEnable(GL_MULTISAMPLE_ARB);

From then on all my rendering appears smooth, just like I expected it.
Accordingly quering various states gives me:
SDL_GL_GetAttribute(SDL_GL_MULTISAMPLEBUFFERS, …) --> 1
SDL_GL_GetAttribute(SDL_GL_MULTISAMPLESAMPLES, …) --> 4
glGet(SDL_SAMPLE_BUFFERS_ARB, …) --> 1
glGet(SDL_SAMPLES_ARB, …) --> 4

Well, no problems so far.

When I try to do this in fullscreen mode, i.e. calling
SDL_SetVideoMode(… SDL_OPENGL | SDL_FULLSCREEN);
I see the well-known jagged edges, what means I don’t have multisampling.
States then are:
SDL_GL_GetAttribute(SDL_GL_MULTISAMPLEBUFFERS, …) --> 1 ???
SDL_GL_GetAttribute(SDL_GL_MULTISAMPLESAMPLES, …) --> 4 ???
glGet(SDL_SAMPLE_BUFFERS_ARB, …) --> 0 !!!
glGet(SDL_SAMPLES_ARB, …) --> 0 !!!
Furthermore, I am not able to achieve multisampling in windowed mode any more
after having tried the fullscreen multisampling.
After restarting X multisampling in windowed mode is possible again.

When I start in windowed mode and switch to fullscreen mode later using
SDL_WM_ToggleFullScreen(…) I even get multisampling in fullscreen mode, but
only once. After quiting the program and starting again no multisampling is
available until restarting X.

Any ideas?

BTW: It’s a linux system with an NVIDIA GeForce3 using the NVIDIA drivers in
the version 1.0-5341.

Thanks in advance,
Christian

P.S. Sorry for the long mail …

1 Like

Christian wrote:

Hello,

I have a problem with multisampling. This is what I do:

Before calling “SDL_SetVideoMode(… SDL_OPENGL)” I set the appropriate
OpenGl-attributes like this:
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 4);

After video mode is set using the call mentioned above I say
glEnable(GL_MULTISAMPLE_ARB);

From then on all my rendering appears smooth, just like I expected it.
Accordingly quering various states gives me:
SDL_GL_GetAttribute(SDL_GL_MULTISAMPLEBUFFERS, …) --> 1
SDL_GL_GetAttribute(SDL_GL_MULTISAMPLESAMPLES, …) --> 4
glGet(SDL_SAMPLE_BUFFERS_ARB, …) --> 1
glGet(SDL_SAMPLES_ARB, …) --> 4

Well, no problems so far.

When I try to do this in fullscreen mode, i.e. calling
SDL_SetVideoMode(… SDL_OPENGL | SDL_FULLSCREEN);
I see the well-known jagged edges, what means I don’t have multisampling.
States then are:
SDL_GL_GetAttribute(SDL_GL_MULTISAMPLEBUFFERS, …) --> 1 ???
SDL_GL_GetAttribute(SDL_GL_MULTISAMPLESAMPLES, …) --> 4 ???
glGet(SDL_SAMPLE_BUFFERS_ARB, …) --> 0 !!!
glGet(SDL_SAMPLES_ARB, …) --> 0 !!!
Furthermore, I am not able to achieve multisampling in windowed mode any more
after having tried the fullscreen multisampling.
After restarting X multisampling in windowed mode is possible again.

When I start in windowed mode and switch to fullscreen mode later using
SDL_WM_ToggleFullScreen(…) I even get multisampling in fullscreen mode, but
only once. After quiting the program and starting again no multisampling is
available until restarting X.

Any ideas?

BTW: It’s a linux system with an NVIDIA GeForce3 using the NVIDIA drivers in
the version 1.0-5341.

What is your glxinfo output ? At the end it lists the supported modes,
could you post this info ?

Stephane

I spent several days to find this solution. Many thanks!

Note! It’s important to call this function before SDL_CreateWindow:

image

pip install numpy PyOpenGL PySDL2 pysdl2-dll

main.py

import ctypes

import numpy as np
from OpenGL.GL import *
from OpenGL.GL.shaders import *
from sdl2 import *

window = None
maxFPS = 5

vertexShaderSource = """
    attribute vec2 aPosition;
    void main()
    {
        gl_Position = vec4(aPosition, 0.0, 1.0);
    }
"""

fragmentShaderSource = """
    void main()
    {
        gl_FragColor = vec4(0.5, 0.8, 0.7, 1.0);
    }
"""

def fatalError(message):
    print(message)
    if window:
        SDL_DestroyWindow(window)
    SDL_Quit()
    exit(-1)

if __name__ == "__main__":
    if SDL_Init(SDL_INIT_VIDEO) < 0:
        fatalError(SDL_GetError())

    # SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, b"2")
    SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 4)

    winW, winH = 350, 350
    window = SDL_CreateWindow(b"OpenGL21, SDL2, Python",
        SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
        winW, winH, SDL_WINDOW_OPENGL)
    if not window:
        fatalError(SDL_GetError())

    context = SDL_GL_CreateContext(window)
    if not context:
        fatalError(SDL_GetError())

    glClearColor(0.2, 0.2, 0.2, 1)
    glViewport(0, 0, winW, winH)

    program = compileProgram(
        compileShader(vertexShaderSource, GL_VERTEX_SHADER),
        compileShader(fragmentShaderSource, GL_FRAGMENT_SHADER))
    glUseProgram(program)

    vertPositions = np.array([
        -0.5, -0.5,
        0.5, -0.5,
        0.0, 0.5
    ], dtype=np.float32)
    vertPosBuffer = glGenBuffers(1)
    glBindBuffer(GL_ARRAY_BUFFER, vertPosBuffer)
    glBufferData(GL_ARRAY_BUFFER, len(vertPositions) * 4,
        vertPositions, GL_STATIC_DRAW)
    aPositionLocation = glGetAttribLocation(program, "aPosition")
    glVertexAttribPointer(aPositionLocation, 2, GL_FLOAT, GL_FALSE,
        0, ctypes.c_void_p(0))
    glEnableVertexAttribArray(aPositionLocation)

    event = SDL_Event()
    running = True
    while running:
        while SDL_PollEvent(ctypes.byref(event)) != 0:
            if event.type == SDL_QUIT:
                running = False
        startTicks = SDL_GetTicks()

        glClear(GL_COLOR_BUFFER_BIT)
        glDrawArrays(GL_TRIANGLES, 0, 3)
        SDL_GL_SwapWindow(window)

        # Limit the FPS to the max FPS
        frameTicks = SDL_GetTicks() - startTicks
        if 1000 / maxFPS > frameTicks:
            SDL_Delay(int(1000 / maxFPS - frameTicks))

    SDL_GL_DeleteContext(context)
    SDL_DestroyWindow(window)
    SDL_Quit()