You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SDL3无法创建OpenGL ES 2.0上下文问题求助

SDL3 + OpenGL ES 2.0 上下文无效问题:glGetString返回null、glCreateShader返回0

我正在用SDL3搭配OpenGL ES 2.0开发OpenGL项目,编写绘制三角形的基础代码时,glCreateShader(GL_VERTEX_SHADER)始终返回0。排查后发现是上下文未创建或未生效——运行测试代码时,glGetString(GL_VERSION)返回(null),尽管SDL_GL_MakeCurrent()返回1且没有SDL错误提示。我已经尝试改用SDL2、更换运行系统,也确认了库与电脑架构兼容,但问题仍未解决。

测试代码

#include <stdio.h>
#include <stdlib.h>
#include <SDL3/SDL.h>
#include <SDL3/SDL_main.h>
#include <SDL3/SDL_opengles2.h>

int main(int argc, char* argv[]) {
    // Initialize SDL2

    if (!SDL_Init(SDL_INIT_VIDEO)) {
        printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
        return -1;
    }

    SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);

    // Create an SDL window with OpenGL ES context
    SDL_Window* window = SDL_CreateWindow("OpenGL ES 2.0 Triangle", 800, 600, SDL_WINDOW_OPENGL);
    if (!window) {
        printf("Window could not be created! SDL_Error: %s\n", SDL_GetError());
        SDL_Quit();
        return -1;
    }

    SDL_GLContext context = SDL_GL_CreateContext(window);

    if (!context) {
        printf("OpenGL context could not be created! SDL_Error: %s\n", SDL_GetError());
        SDL_DestroyWindow(window);
        SDL_Quit();
        return -1;
    }

    int isMadeCurrent = SDL_GL_MakeCurrent(window, context);

    printf("Current context? %d\n", isMadeCurrent);
    printf("Context error: %s\n", SDL_GetError());
    printf("Context version: %s\n", glGetString(GL_VERSION));

    SDL_GL_DestroyContext(context);
    SDL_DestroyWindow(window);
    SDL_Quit();

    printf("Exiting...\n");

    return 0;
}

运行输出

Current context? 1
Context error:
Context version: (null)
Exiting...

目录结构

- src
  - include
    - GLES2
    - SDL3
  - lib
    - libGLESv2.lib
    - libSDL3.dll.a
- libGLESv2.dll
- main.c
- main.exe
- Makefile
- mozglue.dll
- SDL3.dll

Makefile内容(Windows下使用mingw32-make编译)

all:
    gcc -Isrc/include -L src/lib -o main main.c -lGLESv2 -lSDL3

内容的提问来源于stack exchange,提问作者Josué Pedrajas Pérez

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 07:16:06