On this page
  1. Where in the frame
  2. The batch
  3. Leave the state as you found it
  4. Which OpenGL each target runs
    1. Shaders
  5. Raw OpenGL

rlgl and OpenGL

Under every raylib drawing call is rlgl, raylib's thin layer over OpenGL: a matrix stack, a render batch, blend and depth state, shaders. It is on the include path of every target, it is what raylib's own drawing is made of, and it is the portable way to reach below DrawTexture() — the one that works on every target the framework builds, including the software renderer its tests run on.

Raw OpenGL is possible too, and costs more than it looks: the version is not the same on every target, and the framework hands your code no GL header and no loader.

Where in the frame#

<rlgl.h> is never included for you; a file that uses it says so. What it is safe to do, and where, follows from the frame the framework runs:

WhereWhat is safe there
A scene's _draw()Drawing, in screen space. It is the hook that runs between BeginDrawing() and EndDrawing().
An object's _draw()Drawing, in world space: it runs inside the scene's camera.
A scene's _ready()Creating GPU resources — textures, render textures, shaders. The window, and the GL context with it, exists by then.
A scene's _end()Releasing them. At shutdown every scene is ended before the window closes.
A scene's constructor, or a member initialiserNothing that touches the GPU. The first scene is constructed before the window opens.
_update()Nothing that draws. The frame has not begun.

The constructor is the one that surprises. RMP_GAME builds your first scene as the argument to the call that opens the window, so anything its constructor or its member initialisers load is loaded with no GL context — and comes back empty, with raylib asking in the log whether you are trying to load before InitWindow(). Every scene after the first is constructed while the game runs, which is why the mistake hides until the scene it is in becomes the first one.

The batch#

raylib does not send each drawing call to the GPU as it comes. It queues vertices into a render batch and submits the batch when it is full, when a change of state forces it, and at the end of the frame in EndDrawing(). Everything drawn through raylib and rlgl goes through that one queue, in order, so mixing DrawRectangle() with rlBegin() and rlVertex2f() needs no ceremony.

Two things do need it, and both are about code that reads or writes the GPU around the queue rather than through it: raw OpenGL calls, and reading the framebuffer back. Flush the queue first with rlDrawRenderBatchActive(), or raylib's drawing lands after yours, and the read sees only what was submitted before. The framework's own CI capture, in tests/smoke_test.h, flushes for exactly that reason before it reads a frame back.

The batch is not the same size everywhere: rlgl sizes it at 8192 quads on OpenGL 3.3 and at 2048 on OpenGL ES, 3.0 as well as 2.0, so the same scene submits more often on the web and on Android than on a desktop.

Leave the state as you found it#

Your _draw() is not the last thing in the frame. The interface draws in rmp::ui::end(), the scenes above yours draw after it, and next frame the world draws again with whatever state the GPU was left in. raylib's paired calls put back what they change; a bare rlgl switch stays switched until something switches it back:

float angle = 0;

void _update(float delta) override { angle += 90 * delta; }

void _draw() override {
    // a pair: blending goes back to alpha at EndBlendMode()
    BeginBlendMode(BLEND_ADDITIVE);
    DrawCircleV({ 200, 200 }, 60, Fade(ORANGE, 0.6f));
    DrawCircleV({ 240, 200 }, 60, Fade(SKYBLUE, 0.6f));
    EndBlendMode();

    // the matrix stack: whatever is pushed is popped
    rlPushMatrix();
    rlTranslatef(500, 225, 0);
    rlRotatef(angle, 0, 0, 1);
    DrawRectangle(-40, -40, 80, 80, LIME);
    rlPopMatrix();
}

The same goes for BeginScissorMode(), BeginShaderMode() and BeginTextureMode(), each with its End. Switches such as rlEnableDepthTest() or rlDisableBackfaceCulling() have no pair: turn back on what you turned off before _draw() returns.

Which OpenGL each target runs#

raylib is compiled once per target for one version of OpenGL, and it is not the same version everywhere. This is what the framework's build asks for at its current source:

TargetsOpenGLrlGetVersion()
Windows, Linux (glibc and musl, every architecture), the BSDsOpenGL 3.3, whichever windowing backend is chosenRL_OPENGL_33
macOSOpenGL 3.3 — raylib's build fixes it there, and notes that Apple has marked OpenGL deprecatedRL_OPENGL_33
Linux DRM/KMSOpenGL ES 2.0, through EGL and GBMRL_OPENGL_ES_20
WebOpenGL ES 2.0, which the browser provides as WebGL — with either [web] backendRL_OPENGL_ES_20
AndroidOpenGL ES 2.0, or 3.0 with [android] gl_version = "ES30" (see below)RL_OPENGL_ES_20, or RL_OPENGL_ES_30
iOSOpenGL ES 3.0, through ANGLE, which runs it on Metal. The game is compiled for ES 3.0; raylib itself comes prebuilt from the raylib-iOS fork.RL_OPENGL_ES_30
The software renderer of rmp test render and CI's render checksNone: raylib rasterises on the CPU, into memoryRL_OPENGL_SOFTWARE

[android] gl_version is the one version a game chooses. "ES20", the default, compiles raylib and the game for OpenGL ES 2.0; "ES30" compiles both for OpenGL ES 3.0, links ES 3.0's library and asks EGL for an ES 3.0 context. The manifest requires the same version, so "ES30" also hides the game from the devices without ES 3.0 on the Play Store: it is for a game that needs what raylib's ES 3.0 path adds. See Android.

Ask at run time rather than with a macro: rlGetVersion() is compiled into raylib itself, so it answers for the library the game really linked against.

Shaders#

The version decides which shading language a shader is written in: GLSL 330 for OpenGL 3.3, GLSL 100 for OpenGL ES 2.0, and GLSL 300 es for OpenGL ES 3.0. A game with shaders ships one file for each language its targets report, and picks by version:

Shader glow{};

void _ready() override {
    const char *glsl = "100";                               // ES 2.0: DRM, web, Android
    if (rlGetVersion() == RL_OPENGL_33) glsl = "330";       // the desktops
    if (rlGetVersion() == RL_OPENGL_ES_30) glsl = "300es";  // iOS, and Android on "ES30"
    glow = LoadShader(nullptr, TextFormat(RMP_RESOURCES_PATH "glow%s.fs", glsl));
}

void _end() override { UnloadShader(glow); }

void _draw() override {
    BeginShaderMode(glow);
    DrawRectangle(100, 100, 200, 200, WHITE);
    EndShaderMode();
}

LoadShader() reads its files through the asset pack like every raylib loader the framework hooks, so the shaders ship inside resources.rres with everything else, in a shaders/ folder or at the top of resources/.

Raw OpenGL#

The framework gives your code no OpenGL header and no function loader. raylib loads the GL functions it needs inside rlgl's implementation, for itself. What that means in practice:

  • The versions differ by target, as the table above says, so a GL call that is fine on a desktop's OpenGL 3.3 may not exist on the web's ES 2.0.
  • The software renderer has no GL at all. rmp test render and the render checks in CI build the game against raylib's software renderer, which links no GL library — a direct glClear() in a game's code links on a Linux desktop and fails to link there, with the linker reporting an undefined glClear. Code that calls OpenGL directly has to be left out of that build, for instance with #if !defined(GRAPHICS_API_OPENGL_SOFTWARE) around it.
  • The queue comes first. Call rlDrawRenderBatchActive() before your own GL calls, so raylib's queued drawing reaches the GPU before them.
  • The state goes back. raylib keeps its own idea of what is bound and enabled; a raw call that changes it behind rlgl's back changes it for every draw that follows.

raylib does lend out its loader. rlGetProcAddress() returns the address of a GL function through the same loader raylib used for itself, in a raylib compiled for OpenGL 3.3 or OpenGL ES — 2.0, or 3.0 on Android. Under the software renderer it returns null. On iOS it is the fork's prebuilt raylib that answers, and nothing in the framework calls it there or tests what it returns. So the call has to be checked:

using GetIntegerv = void (*)(unsigned int, int *);
auto get_integerv = reinterpret_cast<GetIntegerv>(rlGetProcAddress("glGetIntegerv"));
if (get_integerv != nullptr) {
    rlDrawRenderBatchActive();
    int largest = 0;
    get_integerv(0x0D33, &largest); // GL_MAX_TEXTURE_SIZE
    TraceLog(LOG_INFO, "largest texture: %d", largest);
}

Everything past that — a GL loader of your own, linking the platform's GL library yourself, the GLES headers of each mobile SDK — is outside what the framework builds and tests on its targets. If rlgl can say it, say it with rlgl.