SDL2 简明教程(五):OpenGL 绘制

系列文章目录

SDL2 简明教程(一):使用 Cmake 和 Conan 构建 SDL2 编程环境

SDL2 简明教程(二):创建一个空的窗口

SDL2 简明教程(三):显示图片

SDL2 简明教程(四):用 SDL_IMAGE 库导入图片


SDL 中使用 OpenGL API 进行绘制

在 SDL 中使用 OpenGL 并不复杂,步骤为:

  1. SDL_CreateWindow 创建窗口时,flag 添加 SDL_WINDOW_OPENGL
  2. 使用 SDL_GL_CreateContext 创建 GL Context
  3. 使用 SDL_GL_MakeCurrent 为当前窗口设置 GL Context。 但其实 SDL_GL_CreateContext 中已经做了这个步骤,不切换 GL Context 的情况下可以忽略调用该函数
  4. 至此,OpenGL Context 设置好了,你只需要调用 OpenGL API 进行绘制即可。

关于 OpenGL 如何使用请参考

接下来进行代码示例说明,下面的示例中使用 OpenGL 绘制了一个 三角形
。完整代码你可以在 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;
}
  • 在 include <SDL_opengl.h> 之前,定义 GL_GLEXT_PROTOTYPES,这样才可以使用 OpenGL 3 中的一些接口,例如 glGenBuffers
  • SDL_GL_SetAttribute 设置 OpenGL 的一些参数,其中对于 OpenGL Version 的设置很重要。代码中,我们指定使用 3.2 core
  • SDL_CreateWindow 创建 window,需要给定 SDL_WINDOW_OPENGL
  • SDL_GL_CreateContext 创建 OpenGL Context,SDL_GL_MakeCurrent 为当前 window 设置 context
  • 接来下,都是 OpenGL API 相关的操作了,包括 shader 编译、vao 和 vbo 的设置等等
  • for 循环中,进行 OpenGL 绘制,同时处理 SDL 事件

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weiwei9363/article/details/129178828