openglsdlsdl-2glkit

SDL2 OpenGL Point rendering


I'd like to render randomly positioned points on the screen with SDL2 and OpenGL on Mac. As part of the setup a window is opened an rendered in blue (code not shown) but nothing beside that is rendered.

I have set this attributes:

SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1)

And this is the rendering method with SCREEN_WIDTH set to 640, SCREEN_HEIGHT is 480 which is also the size of the window.

void RunGame()
    {
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluOrtho2D(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0);

        glColor3f(1.0, 1.0, 1.0);

        SDL_Event event;
        while (true)
        {
            SDL_PollEvent(&event);
            if (event.type == SDL_QUIT)
                break;

            int sx = rand() % SCREEN_WIDTH;
            int sy = rand() % SCREEN_HEIGHT;

            glBegin(GL_POINTS);
            glVertex2d(sx, sy);
            glEnd();
            SDL_GL_SwapWindow(mainWindow);
        }
    }

As put in the comment below my context is not the latest OpenGL API but rather getting text-book examples running with minimal changes.


Solution

  • glBegin/glEnd sequences are deprecated and not supported by a cor profile Context.
    You would've to create a Shader, a Vertex Buffer Object and a Vertex Array Object.

    If you want to use Legacy OpenGL and to draw by glBegin/glEnd sequencis, then you've to use a compatibility profile context (SDL_GL_CONTEXT_PROFILE_COMPATIBILITY).

    So the minimal fix, to solve the issue is:

    SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_COMPATIBILITY);