Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SDL3 fails to create OpenGL ES 2.0 context?

I am working on an OpenGL project using the SDL3 and OpenGL ES 2.0. For the moment, I have a basic code which creates a window with the drawing of a triangle. The issue here is that the glCreateShader(GL_VERTEX_SHADER) function always returns 0.

The reason of this is that the context is not created, or not being used. For example, if I run the following code:

#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;
}

I get the following output:

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

glGetString(GL_VERSION) printing (null) means that it couldn't check the version of OpenGL since there is no current context, despite there are no errors shown. I know SDL_GL_MakeCurrent() is not a necessary line, but I have included to make sure the context is being used, which does not seem to be the case.

My directory structure is the following:

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

Here is the content of my Makefile (I compile everything in Windows with mingw32-make:

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

I have tried in a separate project using SDL2 instead of SDL3, or compiling and then running my code in another system. None of this fixed this issue. Also, I made sure the libraries included are compatible with the architecture of my computer.

like image 986
Josué Pedrajas Pérez Avatar asked Dec 01 '25 18:12

Josué Pedrajas Pérez


1 Answers

SDL_GL_CreateContext - Remarks

Windows users new to OpenGL should note that, for historical reasons, GL functions added after OpenGL version 1.1 are not available by default. Those functions must be loaded at run-time, either with an OpenGL extension-handling library or with SDL_GL_GetProcAddress() and its related functions.

If that is not the issue, try to run the example on the same page (example), which uses legacy opengl, which should run without loading the library functions.

See also: OpenGL Wiki - Load OpenGL Functions

like image 127
Erdal Küçük Avatar answered Dec 04 '25 07:12

Erdal Küçük



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!