opengl.py

lunaengine/backend/opengl.py

OpenGL-based hardware-accelerated renderer for LunaEngine - DYNAMIC PARTICLE BUFFERS & FILTER SYSTEM

class FilterType

Description

Enumeration of available filter types

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

Enumeration of filter region shapes

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

Generic shader program for 2D rendering with caching

Methods
def __init__(self: Any, vertex_source: Any, fragment_source: Any) -> Any
No documentation
def _get_uniform_location(self: Any, name: Any) -> Any
Get cached uniform location
def _create_shaders(self: Any, vertex_source: Any, fragment_source: Any) -> Any
Compile shaders with error handling

class ParticleShader

Description

OPTIMIZED shader for particle rendering with instancing

Methods
def __init__(self: Any) -> Any
No documentation
def _setup_geometry(self: Any) -> Any
Setup instanced particle geometry for maximum performance

class SimpleShader

Description

Simple shader for solid color rendering

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

class TextureShader

Description

Shader for textured rendering

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

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 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 OpenGLRenderer

Description

No documentation

Attributes
camera_position: pygame.math.Vector2 = 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) -> Any
Initialize OpenGL context and shaders
def _initialize_filter_framebuffer(self: Any) -> Any
Initialize framebuffer for filter rendering
def add_filter(self: Any, filter_obj: Filter) -> Any
Add a filter to be applied during rendering.

Args:
   filter_obj: Filter object with settings
def remove_filter(self: Any, filter_obj: Filter) -> Any
Remove a filter.

Args:
   filter_obj: Filter object to remove
def clear_filters(self: Any) -> Any
Remove all filters
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
Quick helper to create and add a filter in one call.

Args:
   filter_type: Type of filter
   intensity: Filter strength (0.0 to 1.0)
   x, y: Position (top-left for rect, center for circle)
   width, height: Size (defaults to screen size)
   radius: Radius for circular regions
   feather: Edge softness
   
Returns:
   Filter: Created filter object
def apply_vignette(self: Any, intensity: float = 0.7, feather: float = 100.0) -> Filter
Quick vignette filter (darkens edges)
def apply_blur(self: Any, intensity: float = 0.5, x: float = 0, y: float = 0, width: float = None, height: float = None) -> Filter
Quick blur filter
def apply_sepia(self: Any, intensity: float = 1.0) -> Filter
Quick sepia (old photo) filter
def apply_grayscale(self: Any, intensity: float = 1.0) -> Filter
Quick black and white filter
def apply_invert(self: Any, intensity: float = 1.0) -> Filter
Quick color inversion filter
def apply_warm_temperature(self: Any, intensity: float = 0.5) -> Filter
Quick warm temperature filter
def apply_cold_temperature(self: Any, intensity: float = 0.5) -> Filter
Quick cold temperature filter
def apply_night_vision(self: Any, intensity: float = 0.9) -> Filter
Quick night vision (green) effect
def apply_crt_effect(self: Any, intensity: float = 0.8) -> Filter
Quick CRT (old monitor) effect
def apply_pixelate(self: Any, intensity: float = 0.7) -> Filter
Quick pixelation effect
def apply_bloom(self: Any, intensity: float = 0.5) -> Filter
Quick bloom (glow) effect
def apply_edge_detect(self: Any, intensity: float = 0.8) -> Filter
Quick edge detection filter
def apply_emboss(self: Any, intensity: float = 0.7) -> Filter
Quick emboss effect
def apply_sharpen(self: Any, intensity: float = 0.5) -> Filter
Quick sharpen filter
def apply_posterize(self: Any, intensity: float = 0.6) -> Filter
Quick posterize effect
def apply_neon(self: Any, intensity: float = 0.7) -> Filter
Quick neon glow effect
def apply_radial_blur(self: Any, intensity: float = 0.5) -> Filter
Quick radial blur (zoom effect)
def apply_fisheye(self: Any, intensity: float = 0.4) -> Filter
Quick fisheye lens effect
def apply_twirl(self: Any, intensity: float = 0.3) -> Filter
Quick twirl distortion effect
def apply_circular_grayscale(self: Any, center_x: float, center_y: float, radius: float = 100.0, intensity: float = 1.0) -> Filter
Apply grayscale filter to a circular region.

Args:
   center_x: X position of circle center
   center_y: Y position of circle center
   radius: Radius of the circle
   intensity: Filter strength
   
Returns:
   Filter: Created filter
def apply_rectangular_blur(self: Any, x: float, y: float, width: float, height: float, intensity: float = 0.5) -> Filter
Apply blur filter to a rectangular region.

Args:
   x, y: Top-left position
   width, height: Size of rectangle
   intensity: Blur strength
   
Returns:
   Filter: Created filter
def begin_frame(self: Any) -> Any
Begin rendering frame
def end_frame(self: Any) -> Any
End rendering frame - apply filters if any
def _apply_filters(self: Any) -> Any
Apply all registered filters to the screen
def get_surface(self: Any) -> pygame.Surface
Get the main screen surface for compatibility with UI elements.

Returns:
   pygame.Surface: The main display surface
def set_surface(self: Any, surface: pygame.Surface) -> Any
Set custom surface for rendering using Framebuffer Objects.

Args:
   surface: Pygame surface to use as render target
def _surface_to_texture(self: Any, surface: pygame.Surface) -> int
Convert pygame surface to OpenGL texture with proper color format.

Args:
   surface: Pygame surface to convert
   
Returns:
   int: OpenGL texture ID
def _convert_color(self: Any, color: Tuple[int, int, int, float]) -> Tuple[float, float, float, float]
Convert color tuple to normalized RGBA values

R(0-255) -> R(0.0-1.0)

B(0-255) -> B(0.0-1.0)

G(0-255) -> G(0.0-1.0)

A(0.0-1.0) -> A(0.0-1.0)
def _ensure_particle_capacity(self: Any, required_count: int) -> Any
Ensure particle buffers are large enough - DYNAMIC RESIZING
def enable_scissor(self: Any, x: int, y: int, width: int, height: int) -> Any
Enable scissor test for clipping region.

Args:
   x (int): X position from left (pygame coordinate system)
   y (int): Y position from top (pygame coordinate system)  
   width (int): Width of scissor region
   height (int): Height of scissor region
def disable_scissor(self: Any) -> Any
Disable scissor test
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: Tuple[int, int, int, int] | int = 0) -> Any
Draw a colored rectangle with optional rounded corners.

Args:
   x: X coordinate of top-left corner
   y: Y coordinate of top-left corner
   width: Rectangle width
   height: Rectangle height
   color: RGB color tuple
   fill: Whether to fill the rectangle
   anchor_point: Anchor point for the rectangle
   border_width: Border width for unfilled rectangles
   surface: Target surface
   corner_radius: Radius of rounded corners in pixels.
               Can be:
               - int: Same radius for all corners (CSS: border-radius: 10px)
               - tuple of 4 ints: (top-left, top-right, bottom-right, bottom-left)
               (CSS: border-radius: 10px 20px 30px 40px)
def _draw_sharp_rect(self: Any, x: int, y: int, width: int, height: int, color: tuple, fill: bool, border_width: int) -> Any
Original sharp-corner rectangle drawing (existing code)
def _draw_rounded_rect(self: Any, x: int, y: int, width: int, height: int, color: tuple, fill: bool, border_width: int, corner_radii: Tuple[int, int, int, int]) -> Any
Draw rectangle with individually rounded corners
def draw_line(self: Any, start_x: int, start_y: int, end_x: int, end_y: int, color: tuple, width: int = 2, surface: Optional[pygame.Surface] = None) -> Any
Draw a line between two points with specified width.

Args:
   start_x: Start X coordinate
   start_y: Start Y coordinate
   end_x: End X coordinate
   end_y: End Y coordinate
   color: RGB color tuple
   width: Line width
   surface: Target surface
def draw_lines(self: Any, points: List[Tuple[Tuple[int, int], Tuple[int, int]]], color: Tuple[int, int, int, float] | Tuple[int, int, int], width: int = 2, surface: Optional[pygame.Surface] = None) -> Any
No documentation
def draw_text(self: Any, text: str, x: int, y: int, color: tuple, font: pygame.font.FontType, surface: Optional[pygame.Surface] = None, anchor_point: Optional[Tuple[int, int]] = (0.0, 0.0)) -> Any
Draw text using pygame font rendering
def _draw_thick_line_optimized(self: Any, start_x: int, start_y: int, end_x: int, end_y: int, color: tuple, width: int) -> Any
Optimized method for drawing thick lines
def draw_circle(self: Any, center_x: int, center_y: int, radius: int, color: tuple, fill: bool = True, border_width: int = 1, surface: Optional[pygame.Surface] = None, anchor_point: Tuple[float, float] = (0.5, 0.5)) -> Any
Draw a circle with specified center, radius, color, and anchor point.

Args:
   center_x: X coordinate where the anchor point will be placed
   center_y: Y coordinate where the anchor point will be placed
   radius: Circle radius
   color: RGB color tuple
   fill: Whether to fill the circle
   border_width: Border width for hollow circles
   surface: Target surface
   anchor_point: Anchor point within the circle's bounding box (0.0-1.0)
               (0, 0) = top-left, (0.5, 0.5) = center, (1, 1) = bottom-right
def _generate_filled_circle_geometry(self: Any, segments: int) -> Any
Generate vertices and indices for a filled circle - FIXED VERSION
def _generate_hollow_circle_geometry(self: Any, segments: int, border_width: int, radius: int) -> Any
Generate vertices and indices for a hollow circle (outline)
def draw_polygon(self: Any, points: List[Tuple[int, int]], color: tuple, fill: bool = True, border_width: int = 1, surface: Optional[pygame.Surface] = None, anchor_point: Tuple[float, float] = (0.0, 0.0)) -> Any
Draw a polygon from a list of points.

Args:
   points: List of (x, y) points defining the polygon
   color: RGB color tuple
   fill: Whether to fill the polygon
   border_width: Border width for hollow polygons,
   surface: Target surface
   anchor_point: Anchor point
def _generate_filled_polygon_geometry(self: Any, points: List[Tuple[int, int]]) -> Any
Generate vertices and indices for a filled polygon using triangle fan
def _generate_hollow_polygon_geometry(self: Any, points: List[Tuple[int, int]], border_width: int) -> Any
Generate vertices and indices for a hollow polygon (outline)
def _upload_geometry(self: Any, vertices: np.ndarray, indices: np.ndarray) -> Any
Upload vertices and indices to GPU, return VAO, VBO, EBO
def render_opengl(self: Any, renderer: Any) -> Any
This function is used just for make it detectable in the profiler that is OpenGL.

! Never Remove it
def draw_surface(self: Any, surface: pygame.Surface, x: int, y: int) -> Any
Draw a pygame surface at specified coordinates.
Uses blit internally for consistency.

Args:
   surface: Pygame surface to draw
   x: X coordinate
   y: Y coordinate
def render_surface(self: Any, surface: pygame.Surface, x: int, y: int) -> Any
Draw a pygame surface as texture
def render_particles(self: Any, particle_data: Dict[str, Any], camera: 'Camera') -> Any
HIGHLY OPTIMIZED particle rendering with CORRECTED coordinate conversion
def blit(self: Any, source_surface: pygame.Surface, dest_rect: pygame.Rect, area: Optional[pygame.Rect] = None, special_flags: int = 0) -> Any
Blit a source surface onto the current render target.
Works similarly to pygame.Surface.blit().

Args:
   source_surface: Surface to blit from
   dest_rect: Destination rectangle (x, y, width, height) or (x, y)
   area: Source area to blit from (None for entire surface)
   special_flags: Additional blitting flags (currently unused)
def blit_surface(self: Any, source_surface: pygame.Surface, dest_pos: Tuple[int, int], area: Optional[pygame.Rect] = None) -> Any
Alternative blit function with position tuple instead of rect.

Args:
   source_surface: Surface to blit from
   dest_pos: Destination position (x, y)
   area: Source area to blit from
def fill_screen(self: Any, color: Tuple[int, int, int, float]) -> Any
No documentation
def cleanup(self: Any) -> Any
Clean up OpenGL resources
Back to Backend Module