1

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.

jmkg
  • 1,499
  • 1
  • 11
  • 10
  • 1
    Possible duplicate of [List of deprecated OpenGL functionalities](https://stackoverflow.com/questions/6573398/list-of-deprecated-opengl-functionalities) – Nicol Bolas Sep 22 '19 at 01:21
  • 1
    If you want to use [Legacy OpenGL](https://www.khronos.org/opengl/wiki/Legacy_OpenGL) and to draw by `glBegin`/`glEnd` sequencis, then you've to use a compatibility profile context (`SDL_GL_CONTEXT_PROFILE_COMPATIBILITY`). Else you would've to create a [Shader](https://www.khronos.org/opengl/wiki/Shader), to create a [Vertex Buffer Object](https://www.khronos.org/opengl/wiki/Vertex_Specification#Vertex_Buffer_Object) and a [Vertex Array Object](https://www.khronos.org/opengl/wiki/Vertex_Specification#Vertex_Array_Object). But to explain this is far to broad for a single question. – Rabbid76 Sep 22 '19 at 08:07
  • 1
    Hi @Rabbid76 - I'm on my way through a relatively old book on 3d computer graphics just for fun and interest in the algorithm. My focus is getting the examples running (with minimal tweeks as possible) and not (yet) the latest API. And for this the compatibility context you've mentioned works perfect. Thanks a lot!! – jmkg Sep 22 '19 at 09:57

1 Answers1

1

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);
Rabbid76
  • 202,892
  • 27
  • 131
  • 174