Source code for picogl.buffers.attributes

"""
Attribute and layout specification handling module.

This module defines data structures for specifying attributes and layout
descriptors in a graphical or geometrical context. These structures are
used to describe how data is stored, accessed, and organized for rendering
or processing purposes.
"""

from dataclasses import dataclass
from typing import List

import numpy as np

from picogl.buffers.vertex.vbo.vbo_class import VBOType


@dataclass
[docs] class AttributeSpec: """Attribute specification."""
[docs] name: str # semantic name ("positions", "colors", "normals", etc.)
[docs] index: int # attribute location
[docs] size: int # number of components (e.g., 3 for vec3)
[docs] type: int # GL_FLOAT, GL_INT, etc.
[docs] normalized: bool
[docs] stride: int
[docs] offset: int # in bytes
[docs] vbo_type: VBOType = VBOType.VBO
@dataclass
[docs] class LayoutDescriptor: """Layout descriptor."""
[docs] attributes: List[AttributeSpec]
[docs] _cache: dict[VBOType, AttributeSpec] | None = None
[docs] def __getitem__(self, vbo_type: VBOType) -> AttributeSpec: try: return self.as_dict()[vbo_type] except KeyError: raise KeyError(f"{vbo_type} not defined in layout")
[docs] def get_attr(self, vbo_type: VBOType) -> AttributeSpec: try: return self.as_dict()[vbo_type] except KeyError: raise KeyError(f"{vbo_type} not defined in layout")
[docs] def has_attr(self, vbo_type: VBOType) -> bool: return vbo_type in self.as_dict()
[docs] def as_dict(self) -> dict[VBOType, AttributeSpec]: if self._cache is None: self._cache = {attr.vbo_type: attr for attr in self.attributes} return self._cache
@property
[docs] def attr_dict(self) -> dict[VBOType, AttributeSpec]: """dict""" return self.as_dict()