
    kj                    x    d dl mZ 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
mZ erddlmZ  G d d	          ZdS )
    )annotations)TYPE_CHECKINGAnyOptionalN)VAO   )Material)MeshProgramc                      e Zd ZdZddd ej                     ej                    fd-dZ	 d.d/dZd0dZd1dZ	d2d#Z
d3d'Zd4d)Zd5d6d,ZdS )7MeshzMesh info and geometryNnamestrvaoOptional[VAO]materialOptional[Material]
attributesOptional[dict[str, Any]]bbox_minglm.vec3bbox_maxreturnNonec                l    || _         || _        || _        |pi | _        || _        || _        d| _        dS )a;  Initialize mesh.

        Args:
            name (str): name of the mesh
        Keyword Args:
            vao (VAO): geometry
            material (Material): material for the mesh
            attributes (dict): Details info about each mesh attribute (dict)
            bbox_min: xyz min values
            bbox_max: xyz max values

        Attributes example::

            {
                "NORMAL": {"name": "in_normal", "components": 3, "type": GL_FLOAT},
                "POSITION": {"name": "in_position", "components": 3, "type": GL_FLOAT}
            }
        N)r   r   r   r   r   r   mesh_program)selfr   r   r   r   r   r   s          U/home/agentuser/manim-venv/lib/python3.11/site-packages/moderngl_window/scene/mesh.py__init__zMesh.__init__   sA    6 	 $*  59            projection_matrixglm.mat4model_matrixcamera_matrixtimefloatc                V    | j         !| j                             | ||||           dS dS )zDraw the mesh using the assigned mesh program

        Keyword Args:
            projection_matrix (bytes): projection_matrix
            view_matrix (bytes): view_matrix
            camera_matrix (bytes): camera_matrix
        N)r!   r#   r$   r%   )r   draw)r   r!   r#   r$   r%   s        r   r(   z	Mesh.draw5   sN     ("""3)+ #      )(r   proj_matrix
cam_matrixprogrammoderngl.Programr   c                   |d                              |                                           |d                              |                                           |d                              |                                           |d                              | j                                                   |d                              | j                                                   |                    |           dS )a4  Renders the bounding box for this mesh.

        Args:
            proj_matrix: Projection matrix
            model_matrix: View/model matrix
            cam_matrix: Camera matrix
            program: The moderngl.Program rendering the bounding box
            vao: The vao mesh for the bounding box
        m_projm_modelm_cambb_minbb_maxN)writeto_bytesr   r   render)r   r)   r#   r*   r+   r   s         r   	draw_bboxzMesh.draw_bboxL   s    " 	 4 4 6 6777	  !6!6!8!8999z2244555 6 6 8 8999 6 6 8 8999

7r   c                   | j         
J d            |d                             |                                           |d                             |                                           | j                             |           dS )zRender the mesh as wireframe.

        proj_matrix: Projection matrix
        model_matrix: View/model matrix
        program: The moderngl.Program rendering the wireframe
        Nz(Can not draw the wireframe, vao is emptyr.   r/   )r   r3   r4   r5   )r   r)   r#   r+   s       r   draw_wireframezMesh.draw_wireframed   s     x##%O### 4 4 6 6777	  !6!6!8!8999     r   	attr_type
componentsintc                     ||d| j         |<   dS )z
        Add metadata about the mesh
        :param attr_type: POSITION, NORMAL etc
        :param name: The attribute name used in the program
        :param components: Number of floats
        )r   r:   Nr   )r   r9   r   r:   s       r   add_attributezMesh.add_attributer   s     /3*%M%M	"""r   view_matrixglm.vec3 | Nonetuple[glm.vec3, glm.vec3]c                b   t          j        | j        d          }t          j        | j        d          }||z  }||z  }t	          d          D ].}||         ||         z
  dk     r||         ||         c||<   ||<   /||Jt          j        |j        |j        |j                  t          j        |j        |j        |j                  fS t	          d          D ]!}t          ||         ||                   ||<   "t	          d          D ]!}t          ||         ||                   ||<   "||fS )zCalculates the global bounding.

        Args:
            view_matrix: View matrix
            bbox_min: xyz min
            bbox_max: xyz max
        Returns:
            bbox_min, bbox_max: Combined bbox
        g      ?   r   )glmvec4r   r   rangevec3xyzminmax)	r   r?   r   r   bb1bb2bminbmaxis	            r   calc_global_bboxzMesh.calc_global_bbox{   s9    ht}c**ht}c** S S  q 	4 	4AAwa 1$$#'7DG Qax/HTVTVTV44chtvtvtv6V6VWWq 	4 	4Ahqk4733HQKKq 	4 	4Ahqk4733HQKK!!r   boolc                    d| j         v S )zJ
        Returns:
            bool: Does the mesh have a normals?
        NORMALr=   )r   s    r   has_normalszMesh.has_normals   s    
 4?**r   r   layerc                :    d                     |          | j        v S )zT
        Returns:
            bool: Does the mesh have texture coordinates?
        zTEXCOORD_{})formatr   )r   rW   s     r   has_uvszMesh.has_uvs   s    
 ##E**do==r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r    )
r!   r"   r#   r"   r$   r"   r%   r&   r   r   )r)   r"   r#   r"   r*   r"   r+   r,   r   r   r   r   )r)   r"   r#   r"   r+   r,   r   r   )r9   r   r   r   r:   r;   r   r   )r?   r"   r   r@   r   r@   r   rA   )r   rS   )r   )rW   r;   r   rS   )__name__
__module____qualname____doc__rD   rG   r   r(   r6   r8   r>   rR   rV   rZ    r   r   r   r      s          
 "'+/3%SXZZ%SXZZ!: !: !: !: !:P     .   0! ! ! !N N N N"" "" "" ""H+ + + +> > > > > > >r   r   )
__future__r   typingr   r   r   rD   modernglmoderngl_window.opengl.vaor   r   r	   programsr
   r   r_   r   r   <module>re      s    " " " " " " / / / / / / / / / / 



  * * * * * *       &%%%%%%\> \> \> \> \> \> \> \> \> \>r   