
    kj7                     *   d dl mZmZmZ d dlZd dlZd dlm Z d dlZ	d dl
mZ ej        dej        dej        dej        dej        dej        d	ej        d
ej        dej        dej        di
Z G d d          Z G d d          Z G d de          ZdS )    )AnyOptionalUnionN)types	TRIANGLESTRIANGLE_FANTRIANGLE_STRIPTRIANGLES_ADJACENCYTRIANGLE_STRIP_ADJACENCYPOINTSLINES
LINE_STRIP	LINE_LOOPLINES_ADJACENCYc            	           e Zd ZdZg dfdej        dedee         defdZ	e
defd	            Zdee         deeed
f                  fdZdedefdZdS )
BufferInfoz/Container for a vbo with additional informationFbufferbuffer_format
attributesper_instancec                 :   || _         t          j        |          | _        || _        || _        | j         j        | j        z  dk    r;t          d	                    || j        | j         j        | j        z                      | j         j        | j        z  | _
        dS )z_
        :param buffer: The vbo object
        :param format: The format of the buffer
        r   z@Buffer with type {} has size not aligning with {}. Remainder: {}N)r   r   parse_attribute_formatsattrib_formatsr   r   sizevertex_sizeVAOErrorformatvertices)selfr   r   r   r   s        U/home/agentuser/manim-venv/lib/python3.11/site-packages/moderngl_window/opengl/vao.py__init__zBufferInfo.__init__   s     #;MJJ$( ;d..!33RYY!4#3T[5EHX5X    (D,<<    returnc                 >    t          d | j        D                       S )Nc              3   $   K   | ]}|j         V  d S N)bytes_total).0fs     r    	<genexpr>z)BufferInfo.vertex_size.<locals>.<genexpr>8   s$      >>Q1=>>>>>>r"   )sumr   r   s    r    r   zBufferInfo.vertex_size6   s"    >>$*=>>>>>>r"   .c                    g }g }t          | j        | j                  D ]u\  }}||vr(|                    |                                           1|                    |j                   |                    |           |                    |           vt          |          dk    rdS | j        d                    d	                    |          | j
        rdnd          g|R S )zBuild content tuple for the buffer

        Returns
            The first value is the moderngl buffer
            From the third to the end, it is the attributes of the classr   Nz{}{} z/i )zipr   r   appendpad_strr   removelenr   joinr   )r   r   formatsattrsattrib_formatattribs         r    contentzBufferInfo.content:   s     %()<do%N%N 	& 	&!M6Z''}4466777NN=/000LL   f%%%%u::??4 KMM#((7++T5F-NTTBOO
 
 
 	
r"   namec                     || j         v S r&   r   )r   r;   s     r    has_attributezBufferInfo.has_attributeU   s    t&&r"   N)__name__
__module____qualname____doc__modernglBufferstrlistboolr!   propertyintr   r   tupleobjectr:   r>    r"   r    r   r      s        99 !#"= == = I	=
 = = = =4 ?S ? ? ? X?
$s) 
vs{9K0L 
 
 
 
6'# '$ ' ' ' ' ' 'r"   r   c                   >   e Zd ZdZdej        fdedefdZe	dej
        fd            Z	 	 	 	 d dej        dee         dedededdfdZ	 	 d!d
ddej        dej        dee         dededdfdZ	 	 	 	 d dej        dej        dee         dedededdfdZdeej        ej        e         ef         dedeee         ef         dej        fdZ	 d"deej        ej        e         ef         deddfdZdej        dej        fdZd#deddfdZdedee         fdZdS )$VAOa  
    Represents a vertex array object.

    This is a wrapper class over ``moderngl.VertexArray`` to make interactions
    with programs/shaders simpler. Named buffers are added corresponding with
    attribute names in a vertex shader. When rendering the VAO an internal
    ``moderngl.VertextArray`` is created automatically mapping the named buffers
    compatible with the supplied program. This program is cached internally.

    The shader program doesn't need to use all the buffers registered in
    this wrapper. When a subset is used only the used buffers are mapped
    and the appropriate padding is calculated when interleaved data is used.

    You are not required to use this class, but most methods in the
    system creating vertexbuffers will return this type. You can obtain
    a single ``moderngl.VertexBuffer`` instance by calling :py:meth:`VAO.instance`
    method if you prefer to work directly on moderngl instances.

    Example::

        # Separate buffers
        vao = VAO(name="test", mode=moderngl.POINTS)
        vao.buffer(positions, '3f', ['in_position'])
        vao.buffer(velocities, '3f', ['in_velocities'])

        # Interleaved
        vao = VAO(name="test", mode=moderngl.POINTS)
        vao.buffer(interleaved_data, '3f 3f', ['in_position', 'in_velocities'])

    .. code:: glsl

        # GLSL vertex shader in attributes
        in vec3 in_position;
        in vec3 in_velocities;

    r/   r;   modec                    || _         || _        	 t          | j                  nG# t          $ r: t	          d                    t                                                              w xY wg | _        d| _        d| _	        d| _
        i | _        dS )a  Create and empty VAO with a name and default render mode.

        Example::

            VAO(name="cube", mode=moderngl.TRIANGLES)

        Keyword Args:
            name (str): Optional name for debug purposes
            mode (int): Default draw mode
        z!Invalid draw mode. Options are {}Nr   )r;   rO   
DRAW_MODESKeyErrorr   r   values_buffers_index_buffer_index_element_sizevertex_countvaos)r   r;   rO   s      r    r!   zVAO.__init__   s     			\ty!!! 	\ 	\ 	\>EEjFWFWFYFYZZ[[[	\ +-8<26 57			s
   # AA'r#   c                 (    t          j                    S )z-moderngl.Context: The active moderngl context)mglwctxr,   s    r    r[   zVAO.ctx   s     xzzr"   Nr      programr   first	instancesc                 t    |                      |          }|| j        }|                    ||||           dS )a  Render the VAO.

        An internal ``moderngl.VertexBuffer`` with compatible buffer bindings
        is automatically created on the fly and cached internally.

        Args:
            program: The ``moderngl.Program``
        Keyword Args:
            mode: Override the draw mode (``TRIANGLES`` etc)
            vertices (int): The number of vertices to transform
            first (int): The index of the first vertex to start with
            instances (int): The number of instances
        N)r   r_   r`   )instancerO   render)r   r^   rO   r   r_   r`   vaos          r    rc   z
VAO.render   sA    * mmG$$<9D

4(%9
MMMMMr"   )r_   r   countc                t    |                      |          }|| j        }|                    ||||           dS )aA  
        The render primitive (mode) must be the same as the input primitive of the
        GeometryShader.

        The draw commands are 5 integers:
        (count, instanceCount, firstIndex, baseVertex, baseInstance).

        Args:
            program: The ``moderngl.Program``
            buffer: The ``moderngl.Buffer`` containing indirect draw commands
        Keyword Args:
            mode (int): By default :py:data:`TRIANGLES` will be used.
            count (int): The number of draws.
            first (int): The index of the first indirect draw command.
        N)rO   re   r_   )rb   rO   render_indirect)r   r^   r   rO   re   r_   rd   s          r    rg   zVAO.render_indirect   sD    0 mmG$$<9DFU%HHHHHr"   c                 v    |                      |          }|| j        }|                    |||||           dS )a  Transform vertices. Stores the output in a single buffer.

        Args:
            program: The ``moderngl.Program``
            buffer: The ``moderngl.buffer`` to store the output
        Keyword Args:
            mode: Draw mode (for example ``moderngl.POINTS``)
            vertices (int): The number of vertices to transform
            first (int): The index of the first vertex to start with
            instances (int): The number of instances
        N)rO   r   r_   r`   )rb   rO   	transform)r   r^   r   rO   r   r_   r`   rd   s           r    ri   zVAO.transform   sD    ( mmG$$<9Df4(%S\]]]]]r"   r   attribute_namesc                    t          |t                    s|g}t          |          t          j        t
          j        t          fvr/t          d	                    t          |                              t          |t
          j                  r,| j
                            |                                          }t          |t                    r| j
                            |          }|                                }t          |          t          |          k    r#t          d	                    ||                    | j                            t#          |||                     | j        d         j        | _        |S )at  Register a buffer/vbo for the VAO. This can be called multiple times.
        adding multiple buffers (interleaved or not).

        Args:
            buffer:
                The buffer data. Can be ``numpy.array``, ``moderngl.Buffer`` or ``bytes``.
            buffer_format (str):
                The format of the buffer. (eg. ``3f 3f`` for interleaved positions and normals).
            attribute_names:
                A list of attribute names this buffer should map to.
        Returns:
            The ``moderngl.Buffer`` instance object. This is handy when providing ``bytes``
            and ``numpy.array``.
        zSbuffer parameter must be a moderngl.Buffer, numpy.ndarray or bytes instance(not {})dataz+Format '{}' does not describe attributes {}r\   )
isinstancerF   typerC   rD   numpyndarraybytesr   r   r[   r   tobytessplitr4   rT   r1   r   r   rW   )r   r   r   rj   r6   s        r    r   z
VAO.buffer   sM   ( /400 	O <<FFF%vd6ll33   fem,, 	7X__V^^%5%566Ffe$$ 	2X__&_11F%%''w<<3////=DD]Tcdd   	ZOOPPP M"-6r"      index_element_sizec                    t          |          t          j        t          j        t
          fvrt          d          t          |t          j                  r,| j        	                    |
                                          }t          |t
                    r| j        	                    |          }|| _        || _        dS )zSet the index buffer for this VAO.

        Args:
            buffer: ``moderngl.Buffer``, ``numpy.array`` or ``bytes``
        Keyword Args:
            index_element_size (int): Byte size of each element. 1, 2 or 4
        zKbuffer parameter must be a moderngl.Buffer, numpy.ndarray or bytes instancerl   N)ro   rC   rD   rp   rq   rr   r   rn   r[   r   rs   rU   rV   )r   r   rv   s      r    index_bufferzVAO.index_buffer&  s     <<FFF]   fem,, 	7X__V^^%5%566Ffe$$ 	2X__&_11F##5   r"   c                    | j                             |j                  }|t          |t          j                  r|S d |j                                        D             }|D ]\t          fd| j	        D                       s:t          d                    | j        ||d | j	        D                                 ]g }| j	        D ].}|                    |          }|r|                    |           /|r,t          d                    d |D                                 | j        r(| j                            ||| j        | j                  }n| j                            ||          }|| j         |j        <   |S )a  Obtain the ``moderngl.VertexArray`` instance for the program.

        The instance is only created once and cached internally.

        Args:
            program (moderngl.Program): The program

        Returns:
            ``moderngl.VertexArray``: instance
        Nc                     g | ];\  }}t          |t          j                  |j                            d           9|<S )gl_)rn   rC   	Attributer;   
startswith)r(   r;   attrs      r    
<listcomp>z VAO.instance.<locals>.<listcomp>M  s[     
 
 
d$ 233
 =AI<P<PQV<W<W

 
 
r"   c              3   B   K   | ]}|                               V  d S r&   )r>   )r(   r   attrib_names     r    r*   zVAO.instance.<locals>.<genexpr>V  s1      UUVv++K88UUUUUUr"   z[VAO {} doesn't have attribute {} for program {}.
Program attributes: {}.
VAO attributes: {}c                 &    g | ]}|j         D ]}|S rL   r=   )r(   buffr~   s      r    r   z VAO.instance.<locals>.<listcomp>a  s'    TTT$DOTTDTTTTr"   z$Did not find a buffer mapping for {}c                     g | ]}|S rL   rL   )r(   ns     r    r   z VAO.instance.<locals>.<listcomp>p  s    >]>]>]Qq>]>]>]r"   )rX   getglorn   rC   VertexArray_membersitemsr+   rT   r   r   r;   r:   r1   rU   r[   vertex_arrayrV   )r   r^   rd   program_attributesvao_contentr   r:   r   s          @r    rb   zVAO.instance>  s    immGK((?z#x/CDD?J
 
%.4466
 
 
 . 	 	KUUUUt}UUUUU - f	#*TT$-TTT     m 	, 	,Fnn%788G ,""7+++  	6==>]>]J\>]>]>]^^  
  	>(''"(	 CC (''==C!$	'+
r"   Tc                    | j                                         D ]\  }}|                                 i | _         |rC| j        D ]}|j                                         | j        r| j                                         g | _        dS )zDestroy all internally cached vaos and release all buffers.

        Keyword Args:
            buffers (bool): also release buffers
        N)rX   r   releaserT   r   rU   )r   r   _rd   r   s        r    r   zVAO.release  s     ioo'' 	 	FAsKKMMMM	 	- & &##%%%%! -"**,,,r"   c                 4    | j         D ]}||j        v r|c S dS )a  Get the BufferInfo associated with a specific attribute name

        If no buffer is associated with the name `None` will be returned.

        Args:
            name (str): Name of the mapped attribute
        Returns:
            BufferInfo: BufferInfo instance
        N)rT   r   )r   r;   r   s      r    get_buffer_by_namezVAO.get_buffer_by_name  s8     m 	 	Fv((( ) tr"   )Nr\   r   r]   )Nr\   )ru   )T) r?   r@   rA   rB   rC   r   rE   rI   r!   rH   Contextr[   Programr   rc   rD   rg   ri   r   nptNDArrayr   rr   rF   r   rx   r   rb   rG   r   r   r   rL   r"   r    rN   rN   Y   s       # #J $&83E 8 8S 8S 8 8 8 84 X%    X #N N!N smN 	N
 N N 
N N N N@ #I I I I!I I sm	I
 I I 
I I I IF #^ ^!^ ^ sm	^
 ^ ^ ^ 
^ ^ ^ ^60hos{3'7>?0 0 tCy#~.	0
 
0 0 0 0f bc6 6HOS[-=uDE6[^6	6 6 6 60A 0 AX5I A A A AF d d    (s x
/C      r"   rN   c                       e Zd ZdS )r   N)r?   r@   rA   rL   r"   r    r   r     s        Dr"   r   )typingr   r   r   rC   rp   numpy.typingr   moderngl_windowrZ   moderngl_window.openglr   r   r   r	   r
   r   r   r   r   r   r   rQ   r   rN   	Exceptionr   rL   r"   r    <module>r      sN   ' ' ' ' ' ' ' ' ' '             ( ( ( ( ( ( >- "7%'AOXNG/
=' =' =' =' =' =' =' ='@J J J J J J J JZ
	 	 	 	 	y 	 	 	 	 	r"   