Source code for picogl.renderer.base

"""
RendererBase is a base class for rendering operations, providing foundational
methods for initializing rendering states, managing OpenGL features, and
dispatching rendering tasks.

This module implements essential rendering functionality and acts as a
common base for custom rendering logic. It primarily revolves around
OpenGL-based rendering pipelines and provides interfaces for model drawing
and rendering state initialization.
"""

from typing import Callable, Optional

import numpy as np
from OpenGL.raw.GL.VERSION.GL_1_0 import (
    GL_DEPTH_TEST,
    GL_LINE_SMOOTH,
    glFlush,
    glLineWidth,
)

from picogl.backend.gl.wrappers.enable import gl_enable, gl_disable
from picogl.renderer.abstract import AbstractRenderer


[docs] class RendererBase(AbstractRenderer): """Base Renderer Class"""
[docs] def __init__(self, parent=None): """ Initialize the renderer. :param state: Application state object for accessing shared data. """ super().__init__()
[docs] self.line_width = 2.0
[docs] self.show_model = False
[docs] self.parent = parent
[docs] def _set_gl_state(self): """Set the line width and disable depth test.""" glLineWidth(self.line_width) gl_disable(GL_DEPTH_TEST) gl_enable(GL_LINE_SMOOTH)
[docs] def _restore_gl_state(self): """Restore the original line width and depth test state.""" gl_enable(GL_DEPTH_TEST)
@property
[docs] def dispatch_list(self): dispatch_list: list[tuple[bool, Callable]] = [ (self.show_model, self._draw_model), # Add more conditions and corresponding draw functions as needed ] return dispatch_list
[docs] def _do_initialize(self) -> None: """ Initialize OpenGL resources (shaders, atoms_buffers, etc.). """ pass
@property
[docs] def initialized(self) -> bool: return self._initialized
[docs] def render(self, mvp_matrix: Optional[np.ndarray] = None) -> None: """ render dispatcher :return: None """ for condition, draw_fn in self.dispatch_list: if condition: draw_fn() if hasattr(self, "_has_selection") and self._has_selection(): self._draw_selection() self._finalize_render()
[docs] def initialize_rendering_buffers(self): """For back compatibility""" self.initialize()
[docs] def _finalize_render(self): """ Finalize the rendering (e.g., flush or swap atoms_buffers). """ glFlush()
[docs] def _draw_model(self): """ draw_model """ raise NotImplementedError("Subclasses must implement the method.")
[docs] def _draw_selection(self): """ draw_selection """ raise NotImplementedError("Subclasses must implement the method.")
[docs] def set_visibility(self, visible: bool) -> None: """Set the visibility of the object.""" pass