SDL2 Concise Tutorial (5): OpenGL Drawing

Series Article Directory

SDL2 Concise Tutorial (1): Using Cmake and Conan to Build an SDL2 Programming Environment

SDL2 brief tutorial (2): Create an empty window

SDL2 brief tutorial (3): display pictures

SDL2 Concise Tutorial (4): Importing pictures with the SDL_IMAGE library


Drawing with OpenGL API in SDL

Using OpenGL in SDL is not complicated, the steps are:

  1. SDL_CreateWindowWhen creating the window, flagaddSDL_WINDOW_OPENGL
  2. SDL_GL_CreateContextCreate a GL Context with
  3. Use to set the GL Context SDL_GL_MakeCurrentfor the current window. But in fact, this step has been done SDL_GL_CreateContextin , you can ignore calling this function without switching the GL Context
  4. At this point, the OpenGL Context is set up, and you only need to call the OpenGL API to draw.

About how to use OpenGL, please refer to

Next, the code example is explained. In the following example, a triangle is drawn using OpenGL
. The complete code you can find in opengl_sdl2_example.cpp

//
// Created by user on 2/22/23.
//
#if defined(__cplusplus)
extern "C" {
    
    
#endif

#define GL_GLEXT_PROTOTYPES

#include <SDL.h>
#include <SDL_opengl.h>

#if defined(__cplusplus)
};
#endif

int main() {
    
    
    SDL_Init(SDL_INIT_VIDEO);

    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
    SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1);
    SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
    SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);

    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);

    static const int width = 800;
    static const int height = 600;
    SDL_Window *window =
        SDL_CreateWindow("", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
                         width, height, SDL_WINDOW_OPENGL | SDL_WINDOW_ALLOW_HIGHDPI);

    SDL_GLContext gl_context = SDL_GL_CreateContext(window);
    SDL_GL_MakeCurrent(window, gl_context);
    SDL_GL_SetSwapInterval(1); // Enable vsync

    // opengl operations
    // create shaders and program
    // ....

    // set up vertex data(and buffer(s)) and configure vertex attributes
    float vertices[] = {
    
    
        -0.5f, -0.5f, 0.0f, // left
        0.5f, -0.5f, 0.0f,  // right
        0.0f, 0.5f, 0.0f,   // top
    };

    GLuint VBO{
    
    0};
    GLuint VAO{
    
    0};
    glGenVertexArrays(1, &VAO);
    glGenBuffers(1, &VBO);

    // bind the vertex array object first, then bind and set vertex buffers
    // and then configure vertex attributes
    glBindVertexArray(VAO);

    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *)0);
    glEnableVertexAttribArray(0);

    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindVertexArray(0);

    bool done = false;
    for (; !done;) {
    
    

        SDL_Event event;
        while (SDL_PollEvent(&event)) {
    
    
            if (event.type == SDL_QUIT)
                done = true;
            if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_CLOSE && event.window.windowID == SDL_GetWindowID(window))
                done = true;
        }

        // render
        // -----
        glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);

        // draw out first triangle
        glUseProgram(shaderProgram);
        glBindVertexArray(VAO);
        glDrawArrays(GL_TRIANGLES, 0, 3);

        SDL_GL_SwapWindow(window);
        SDL_Delay(1);
    }

    SDL_DestroyWindow(window);
    SDL_Quit();
    return 0;
}
  • Before include <SDL_opengl.h>, define GL_GLEXT_PROTOTYPES, so that you can use some interfaces in OpenGL 3, such as glGenBuffers
  • SDL_GL_SetAttributeSet some parameters of OpenGL, among which the setting of OpenGL Version is very important. In the code, we specify to use 3.2 core
  • SDL_CreateWindowTo create a window, it needs to be givenSDL_WINDOW_OPENGL
  • SDL_GL_CreateContextCreate an OpenGL Context and SDL_GL_MakeCurrentset the context for the current window
  • Next, all operations related to OpenGL API, including shader compilation, vao and vbo settings, etc.
  • In forthe loop , perform OpenGL drawing and handle SDL events at the same time

insert image description here

Guess you like

Origin blog.csdn.net/weiwei9363/article/details/129178828