Skip to main content
LibGL is SerenityOS’s implementation of OpenGL 1.5, providing hardware-accelerated 3D graphics rendering capabilities. It implements the standard OpenGL API and integrates with the GPU subsystem through LibGPU.

Architecture

LibGL implements the OpenGL fixed-function pipeline with support for modern features:
  • GLContext: Central context management for all OpenGL state
  • GPU Integration: Hardware-accelerated rendering via LibGPU device drivers
  • Shader Pipeline: Vertex and fragment shader compilation and linking
  • Buffer Objects: VBOs and element array buffers for efficient geometry handling

Core Components

GLContext

The main context class that manages all OpenGL state and operations.
GLContext
class
Central OpenGL context managing rendering state, matrices, textures, and shaders.Key Members:
  • Model-view and projection matrix stacks
  • Texture units and texture objects
  • Lighting and material state
  • Shader programs and buffer objects
  • Blending, depth, and stencil configuration

Matrix Operations

LibGL maintains separate matrix stacks for different transformation types:
void gl_matrix_mode(GLenum mode);  // GL_MODELVIEW, GL_PROJECTION, GL_TEXTURE
void gl_push_matrix();
void gl_pop_matrix();
void gl_load_identity();
void gl_load_matrix(FloatMatrix4x4 const& matrix);
void gl_mult_matrix(FloatMatrix4x4 const& matrix);
Stack Limits:
  • Modelview: 64 matrices
  • Projection: 8 matrices
  • Texture: 8 matrices per unit

Rendering Pipeline

Immediate mode and vertex array rendering:
// Immediate mode
void gl_begin(GLenum mode);  // GL_TRIANGLES, GL_QUADS, etc.
void gl_vertex(GLfloat x, GLfloat y, GLfloat z, GLfloat w);
void gl_color(GLfloat r, GLfloat g, GLfloat b, GLfloat a);
void gl_tex_coord(GLfloat s, GLfloat t, GLfloat r, GLfloat q);
void gl_normal(GLfloat nx, GLfloat ny, GLfloat nz);
void gl_end();

// Vertex arrays
void gl_enable_client_state(GLenum cap);
void gl_vertex_pointer(GLint size, GLenum type, GLsizei stride, void const* pointer);
void gl_draw_arrays(GLenum mode, GLint first, GLsizei count);
void gl_draw_elements(GLenum mode, GLsizei count, GLenum type, void const* indices);

Texture Management

LibGL supports 2D textures with multiple texture units.
gl_gen_textures
function
Generate texture object names.
void gl_gen_textures(GLsizei n, GLuint* textures);
gl_bind_texture
function
Bind a texture to a target.
void gl_bind_texture(GLenum target, GLuint texture);
// target: GL_TEXTURE_1D, GL_TEXTURE_2D, GL_TEXTURE_3D, GL_TEXTURE_CUBE_MAP
gl_tex_image_2d
function
Specify a two-dimensional texture image.
void gl_tex_image_2d(GLenum target, GLint level, GLint internal_format,
                     GLsizei width, GLsizei height, GLint border,
                     GLenum format, GLenum type, GLvoid const* data);

Shader Support

OpenGL 2.0 programmable pipeline support.
// Create and compile shaders
GLuint gl_create_shader(GLenum shader_type);  // GL_VERTEX_SHADER, GL_FRAGMENT_SHADER
void gl_shader_source(GLuint shader, GLsizei count, GLchar const** string, GLint const* length);
void gl_compile_shader(GLuint shader);
void gl_get_shader(GLuint shader, GLenum pname, GLint* params);
void gl_delete_shader(GLuint shader);
// Link shader programs
GLuint gl_create_program();
void gl_attach_shader(GLuint program, GLuint shader);
void gl_link_program(GLuint program);
void gl_use_program(GLuint program);
void gl_get_program(GLuint program, GLenum pname, GLint* params);
void gl_delete_program(GLuint program);

Lighting

Fixed-function lighting with up to 8 light sources.
gl_lightfv
function
Set light source parameters.
void gl_lightfv(GLenum light, GLenum pname, GLfloat const* params);
// light: GL_LIGHT0 through GL_LIGHT7
// pname: GL_AMBIENT, GL_DIFFUSE, GL_SPECULAR, GL_POSITION, etc.
gl_materialfv
function
Set material properties.
void gl_materialfv(GLenum face, GLenum pname, GLfloat const* params);
// face: GL_FRONT, GL_BACK, GL_FRONT_AND_BACK
// pname: GL_AMBIENT, GL_DIFFUSE, GL_SPECULAR, GL_EMISSION, GL_SHININESS

Buffer Objects

Vertex Buffer Objects (VBOs) for efficient geometry storage.
void gl_gen_buffers(GLsizei n, GLuint* buffers);
void gl_bind_buffer(GLenum target, GLuint buffer);
// target: GL_ARRAY_BUFFER, GL_ELEMENT_ARRAY_BUFFER

void gl_buffer_data(GLenum target, GLsizeiptr size, void const* data, GLenum usage);
// usage: GL_STATIC_DRAW, GL_DYNAMIC_DRAW, GL_STREAM_DRAW

void gl_buffer_sub_data(GLenum target, GLintptr offset, GLsizeiptr size, void const* data);
void gl_delete_buffers(GLsizei n, GLuint const* buffers);

State Management

LibGL maintains extensive state for the OpenGL fixed-function pipeline:
  • Depth testing and depth buffer configuration
  • Stencil testing with separate front/back face operations
  • Alpha testing and blending modes
  • Culling and polygon offset
  • Scissor testing and clipping planes
  • Color masking and depth masking

Enable/Disable Capabilities

void gl_enable(GLenum cap);
void gl_disable(GLenum cap);
GLboolean gl_is_enabled(GLenum cap);
Common Capabilities:
  • GL_DEPTH_TEST: Depth buffer testing
  • GL_BLEND: Alpha blending
  • GL_CULL_FACE: Backface culling
  • GL_LIGHTING: Fixed-function lighting
  • GL_TEXTURE_2D: 2D texturing
  • GL_STENCIL_TEST: Stencil buffer testing
  • GL_SCISSOR_TEST: Scissor testing
  • GL_ALPHA_TEST: Alpha testing

Display Lists

Compiled display lists for efficient repeated rendering.
GLuint gl_gen_lists(GLsizei range);
void gl_new_list(GLuint list, GLenum mode);  // GL_COMPILE, GL_COMPILE_AND_EXECUTE
void gl_end_list();
void gl_call_list(GLuint list);
void gl_delete_lists(GLuint list, GLsizei range);

Error Handling

gl_get_error
function
Retrieve the current error code.
GLenum gl_get_error();
Error Codes:
  • GL_NO_ERROR: No error
  • GL_INVALID_ENUM: Invalid enumeration value
  • GL_INVALID_VALUE: Invalid parameter value
  • GL_INVALID_OPERATION: Invalid operation
  • GL_STACK_OVERFLOW: Matrix stack overflow
  • GL_STACK_UNDERFLOW: Matrix stack underflow
  • GL_OUT_OF_MEMORY: Out of memory

Source Location

Directory: Userland/Libraries/LibGL/ Key Files:
  • GLContext.h / GLContext.cpp: Main context implementation
  • GL/gl.h: OpenGL API definitions
  • Buffer/Buffer.h: Buffer object management
  • Shaders/Program.h: Shader program handling
  • Tex/Texture.h: Texture object implementation

Build docs developers (and LLMs) love