Search code examples
c++openglsdl-2opengl-3

SDL2 not creating triangle


I tried to make a triangle, but it wont render when i compile and run the program.

Here is the code:

#include <iostream>
#include <gl/GL.h>
#include <SDL.h>
#include <SDL_opengl.h>

const int windowWidth = 1280, windowHeight = 720;

int main(int argc, char* argv[])
{
    // Create window //
    SDL_Window* window = SDL_CreateWindow("OpenGL Test", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, windowWidth, windowHeight, SDL_WINDOW_OPENGL);

    if (window == NULL)
    {
        return EXIT_FAILURE;
    }

    SDL_Event windowEvent;

    // OpenGL context //
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);

    // Main loop //
    while (true)
    {
        if (SDL_PollEvent(&windowEvent))
        {
            if (windowEvent.type == SDL_QUIT)
            {
                break;
            }
        }
        // Draw //
        glClearColor(0.f, 0.f, 0.f, 1.f);
        glClear(GL_COLOR_BUFFER_BIT);

        glBegin(GL_TRIANGLES);
            glColor3f(1.f, 0.f, 0.f);
            glVertex2f(0, 0.5);
            glColor3f(0.f, 1.f, 0.f);
            glVertex2f(-0.5, -0.5);
            glColor3f(0.f, 0.f, 1.f);
            glVertex2f(0.5, -0.5);
        glEnd();

        glFlush();

        SDL_GL_SwapWindow(window);
    }

    SDL_DestroyWindow(window);
    SDL_Quit();

    return EXIT_SUCCESS;
}

Solution

  • Multiple issues:

    • Call SDL_GL_SetAttribute()s before SDL_CreateWindow().
    • Create a GL context with SDL_GL_CreateContext() before trying to call any GL functions.
    • If you do end up using SDL_GL_CONTEXT_PROFILE_CORE you'll need to completely re-write the rest of your GL code to not use all those removed functions like glBegin()/glVertex()/glColor()/glEnd().

    All together:

    // g++ main.cpp `pkg-config --cflags --libs sdl2 gl`
    #include <SDL.h>
    #include <SDL_opengl.h>
    
    int main(int argc, char* argv[])
    {
        SDL_Window* window = SDL_CreateWindow("OpenGL Test", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, SDL_WINDOW_OPENGL);
        if (window == NULL)
        {
            return EXIT_FAILURE;
        }
    
        SDL_GLContext ctx = SDL_GL_CreateContext( window );
    
        // Main loop //
        while (true)
        {
            SDL_Event windowEvent;
            if (SDL_PollEvent(&windowEvent))
            {
                if (windowEvent.type == SDL_QUIT)
                {
                    break;
                }
            }
    
            // Draw //
            glClearColor(0.f, 0.f, 0.f, 1.f);
            glClear(GL_COLOR_BUFFER_BIT);
    
            glBegin(GL_TRIANGLES);
                glColor3f(1.f, 0.f, 0.f);
                glVertex2f(0, 0.5);
                glColor3f(0.f, 1.f, 0.f);
                glVertex2f(-0.5, -0.5);
                glColor3f(0.f, 0.f, 1.f);
                glVertex2f(0.5, -0.5);
            glEnd();
    
            SDL_GL_SwapWindow(window);
        }
    
        SDL_GL_DeleteContext( ctx );
        SDL_DestroyWindow(window);
        SDL_Quit();
    
        return EXIT_SUCCESS;
    }