opengl.py

lunaengine/backend/opengl.py

OpenGL-based hardware-accelerated renderer for LunaEngine
- GPU-accelerated particles via instancing (CPU physics)
- Full filter system (post-processing)
- 2D drawing primitives (lines, circles, polygons, etc.)
- Rounded rectangles with per-corner radii
- Texture and surface rendering

This version uses the reliable CPU particle system and only uses OpenGL
for rendering - no compute shaders, no GPU physics.
v0.2.3

class FilterType

Description

No documentation

Attributes
NONE: Any = 'none'
VIGNETTE: Any = 'vignette'
BLUR: Any = 'blur'
SEPIA: Any = 'sepia'
GRAYSCALE: Any = 'grayscale'
INVERT: Any = 'invert'
TEMPERATURE_WARM: Any = 'temperature_warm'
TEMPERATURE_COLD: Any = 'temperature_cold'
NIGHT_VISION: Any = 'night_vision'
CRT: Any = 'crt'
PIXELATE: Any = 'pixelate'
BLOOM: Any = 'bloom'
EDGE_DETECT: Any = 'edge_detect'
EMBOSS: Any = 'emboss'
SHARPEN: Any = 'sharpen'
POSTERIZE: Any = 'posterize'
NEON: Any = 'neon'
RADIAL_BLUR: Any = 'radial_blur'
FISHEYE: Any = 'fisheye'
TWIRL: Any = 'twirl'
Methods

No methods defined.

class FilterRegionType

Description

No documentation

Attributes
FULLSCREEN: Any = 'fullscreen'
RECTANGLE: Any = 'rectangle'
CIRCLE: Any = 'circle'
Methods

No methods defined.

class Filter

Description

Simple filter class with all parameters

Methods
def __init__(self: Any, filter_type: FilterType = FilterType.NONE, intensity: float = 1.0, region_type: FilterRegionType = FilterRegionType.FULLSCREEN, region_pos: Tuple[float, float] = (0, 0), region_size: Tuple[float, float] = (100, 100), radius: float = 50.0, feather: float = 10.0, blend_mode: str = 'normal') -> Any
Initialize a filter with all parameters.

Args:
   filter_type: Type of filter effect
   intensity: Filter strength (0.0 to 1.0)
   region_type: Shape of filter region
   region_pos: Position of region (top-left for rect, center for circle)
   region_size: Size of region (width, height)
   radius: Radius for circular regions
   feather: Edge softness in pixels
   blend_mode: How filter blends with background
def update(self: Any, dt: float) -> Any
Update filter for animation
def copy(self: Any) -> 'Filter'
Create a copy of this filter

class ShaderProgram

Description

Base class for all shader programs with uniform caching.

Attributes
vertex_source: str | None = None
fragment_source: str | None = None
geometry_source: str | None = None
Methods
def get_source(self: Any, source: str | None, type: Literal['vertex', 'fragment', 'geometry']) -> Any
No documentation
def __init__(self: Any, vertex_source: str, fragment_source: str, geometry_source: Optional[str] = None) -> Any
No documentation
def _get_uniform_location(self: Any, name: str) -> int
Get cached uniform location.
def _create_program(self: Any, vertex_source: str, fragment_source: str, geometry_source: Optional[str] = None) -> Any
Compile and link shaders.
def use(self: Any) -> Any
Activate the shader program.
def unuse(self: Any) -> Any
Deactivate the shader program.
def _setup_geometry(self: Any) -> Any
Set up vertex arrays - to be overridden by subclasses.

class ParticleShader

Description

Instanced particle shader.

Methods
def __init__(self: Any) -> Any
No documentation
def _setup_geometry(self: Any) -> Any
Create a dummy VAO with one vertex (for instancing).

class SimpleShader

Description

Simple shader for solid color rectangles and shapes.

Methods
def __init__(self: Any) -> Any
No documentation
def _setup_geometry(self: Any) -> Any
Setup a unit quad (0,0 to 1,1).

class TextureShader

Description

Shader for textured rendering.

Methods
def __init__(self: Any) -> Any
No documentation
def _setup_geometry(self: Any) -> Any
Setup textured quad with UVs.

class RoundedRectShader

Description

Shader for drawing rectangles with per-corner rounded corners.

Methods
def __init__(self: Any) -> Any
No documentation
def _setup_geometry(self: Any) -> Any
Setup quad geometry for rectangle.

class FilterShader

Description

Shader for applying post-processing filters.

Methods
def __init__(self: Any) -> Any
No documentation
def _setup_geometry(self: Any) -> Any
Setup fullscreen quad for filter rendering.

class OpenGLRenderer

Description

No documentation

Attributes
camera_position: Any = pygame.math.Vector2(0, 0)
Methods
def __init__(self: Any, width: int, height: int) -> Any
No documentation
def max_particles(self: Any) -> int
No documentation
def max_particles(self: Any, value: int) -> Any
No documentation
def initialize(self: Any) -> bool
Initialize OpenGL context and shaders.
def _initialize_filter_framebuffer(self: Any) -> bool
Create framebuffer for off-screen filter rendering.
def add_filter(self: Any, filter_obj: Filter) -> Any
No documentation
def remove_filter(self: Any, filter_obj: Filter) -> Any
No documentation
def clear_filters(self: Any) -> Any
No documentation
def create_quick_filter(self: Any, filter_type: FilterType, intensity: float = 1.0, x: float = 0, y: float = 0, width: float = None, height: float = None, radius: float = 50.0, feather: float = 10.0) -> Filter
No documentation
def apply_vignette(self: Any, intensity: Any = 0.7, feather: Any = 100.0) -> Filter
No documentation
def apply_blur(self: Any, intensity: Any = 0.5, x: Any = 0, y: Any = 0, width: Any = None, height: Any = None) -> Filter
No documentation
def apply_sepia(self: Any, intensity: Any = 1.0) -> Filter
No documentation
def apply_grayscale(self: Any, intensity: Any = 1.0) -> Filter
No documentation
def apply_invert(self: Any, intensity: Any = 1.0) -> Filter
No documentation
def apply_warm_temperature(self: Any, intensity: Any = 0.5) -> Filter
No documentation
def apply_cold_temperature(self: Any, intensity: Any = 0.5) -> Filter
No documentation
def apply_night_vision(self: Any, intensity: Any = 0.9) -> Filter
No documentation
def apply_crt_effect(self: Any, intensity: Any = 0.8) -> Filter
No documentation
def apply_pixelate(self: Any, intensity: Any = 0.7) -> Filter
No documentation
def apply_bloom(self: Any, intensity: Any = 0.5) -> Filter
No documentation
def apply_edge_detect(self: Any, intensity: Any = 0.8) -> Filter
No documentation
def apply_emboss(self: Any, intensity: Any = 0.7) -> Filter
No documentation
def apply_sharpen(self: Any, intensity: Any = 0.5) -> Filter
No documentation
def apply_posterize(self: Any, intensity: Any = 0.6) -> Filter
No documentation
def apply_neon(self: Any, intensity: Any = 0.7) -> Filter
No documentation
def apply_radial_blur(self: Any, intensity: Any = 0.5) -> Filter
No documentation
def apply_fisheye(self: Any, intensity: Any = 0.4) -> Filter
No documentation
def apply_twirl(self: Any, intensity: Any = 0.3) -> Filter
No documentation
def apply_circular_grayscale(self: Any, center_x: Any, center_y: Any, radius: Any = 100.0, intensity: Any = 1.0) -> Filter
No documentation
def apply_rectangular_blur(self: Any, x: Any, y: Any, width: Any, height: Any, intensity: Any = 0.5) -> Filter
No documentation
def begin_frame(self: Any) -> Any
Start rendering a new frame.
def end_frame(self: Any) -> Any
Finish rendering and apply filters.
def _apply_filters(self: Any) -> Any
Apply stacked filters to the screen.
def get_surface(self: Any) -> pygame.Surface
No documentation
def set_surface(self: Any, surface: Optional[pygame.Surface]) -> Any
Render to a custom surface using framebuffer.
def _surface_to_texture(self: Any, surface: pygame.Surface) -> int
Convert a pygame Surface to an OpenGL texture.
def _surface_to_texture_cached(self: Any, surface: pygame.Surface) -> int
Cached version of surface-to-texture.
def _convert_color(self: Any, color: Tuple[int, int, int, float]) -> Tuple[float, float, float, float]
Convert color tuple to normalized RGBA.
def enable_scissor(self: Any, x: int, y: int, width: int, height: int) -> Any
No documentation
def disable_scissor(self: Any) -> Any
No documentation
def draw_rect(self: Any, x: int, y: int, width: int, height: int, color: tuple, fill: bool = True, anchor_point: tuple = (0.0, 0.0), border_width: int = 1, surface: Optional[pygame.Surface] = None, corner_radius: Union[int, Tuple[int, int, int, int]] = 0) -> Any
No documentation
def _draw_sharp_rect(self: Any, x: Any, y: Any, width: Any, height: Any, color: Any, fill: Any, border_width: Any) -> Any
Draw a sharp rectangle, with proper outline handling.
def _draw_rounded_rect(self: Any, x: Any, y: Any, w: Any, h: Any, color: Any, fill: Any, border_width: Any, radii: Any) -> Any
No documentation
def draw_line(self: Any, start_x: Any, start_y: Any, end_x: Any, end_y: Any, color: Any, width: Any = 2, surface: Any = None) -> Any
No documentation
def _draw_thick_line(self: Any, x1: Any, y1: Any, x2: Any, y2: Any, color: Any, width: Any) -> Any
No documentation
def draw_lines(self: Any, points: List[Tuple[Tuple[int, int], Tuple[int, int]]], color: tuple, width: int = 2, surface: Any = None) -> Any
No documentation
def draw_circle(self: Any, center_x: Any, center_y: Any, radius: Any, color: Any, fill: Any = True, border_width: Any = 1, surface: Any = None, anchor_point: Any = (0.5, 0.5)) -> Any
No documentation
def _generate_filled_circle_geometry(self: Any, segments: Any) -> Any
No documentation
def _generate_hollow_circle_geometry(self: Any, segments: int, border_width: float | int, radius: float | int) -> Any
No documentation
def _upload_geometry(self: Any, vertices: Any, indices: Any) -> Any
No documentation
def draw_polygon(self: Any, points: Any, color: Any, fill: Any = True, border_width: Any = 1, surface: Any = None, anchor_point: Any = (0.0, 0.0)) -> Any
No documentation
def _generate_filled_polygon_geometry(self: Any, points: Any) -> Any
No documentation
def _generate_hollow_polygon_geometry(self: Any, points: Any, border_width: Any) -> Any
No documentation
def draw_text(self: Any, text: str, x: int, y: int, color: tuple, font: Any, surface: Any = None, anchor_point: Any = (0.0, 0.0)) -> Any
No documentation
def draw_surface(self: Any, surface: pygame.Surface, x: int, y: int) -> Any
No documentation
def blit(self: Any, source: pygame.Surface, dest: Union[Tuple[int, int], pygame.Rect], area: Optional[pygame.Rect] = None, special_flags: int = 0) -> Any
No documentation
def fill_screen(self: Any, color: Tuple[int, int, int, float]) -> Any
No documentation
def clear(self: Any) -> Any
No documentation
def render_particles(self: Any, particle_data: Dict[str, Any], camera: Any) -> Any
Render particles using instancing.
particle_data should contain:
   - active_count: int
   - positions: np.ndarray (N,2) world positions
   - sizes: np.ndarray (N,) sizes in world units (will be zoomed by camera)
   - colors: np.ndarray (N,3) uint8 RGB
   - alphas: np.ndarray (N,) uint8 alpha
def _ensure_particle_capacity(self: Any, required: int) -> Any
Dynamically resize instance buffers if needed.
def cleanup(self: Any) -> Any
No documentation
def set_blend_mode(self: Any, mode: Literal['normal', 'add', 'multiply', 'screen']) -> Any
Set the OpenGL blend mode.
mode can be 'normal', 'add', 'multiply', etc.
Back to Backend Module