
    kjJ                         d dl Z d dlmZmZmZ d dlZd dlmZmZmZ d dl	m
Z
 d dlmZ d dlmZmZ dZdZd	Zd
ZdZdZd ZdZdZ G d d          Z G d de          Z G d de          ZdS )    N)AnyOptionalUnion)cosradianssin)BaseKeys)Projection3D)QWERTYKeyMapFactory                  c            	          e Zd ZdZ	 d!dedededefd	Zed
efd            Zdededed
dfdZ	deded
dfdZ
ed
efd            Zej        ded
dfd            Zed
efd            Zej        ded
dfd            Zed
ej        fd            Zd"dZ	 d#deej                 deeeeef                  d
ej        fdZdej        dej        dej        d
ej        fd ZdS )$CameraaV  Simple camera class containing projection.

    .. code:: python

        # create a camera
        camera = Camera(fov=60.0, aspect_ratio=1.0, near=1.0, far=100.0)

        # Get the current camera matrix as numpy array
        print(camera.matrix)

        # Get projection matrix as numpy array
        print(camera.projection.matrix)
          N@      ?      Y@fovaspect_rationearfarc                 ^   t          j        ddd          | _        t          j        ddd          | _        t          j        ddd          | _        t          j        ddd          | _        d| _        d| _        t          j        ddd          | _        t          ||||          | _
        dS )zInitialize camera using a specific projection

        Keyword Args:
            fov (float): Field of view
            aspect_ratio (float): Aspect ratio
            near (float): Near plane
            far (float): Far plane
                r   g      g     VN)glmvec3positionuprightdir_yaw_pitch_upr
   _projection)selfr   r   r   r   s        W/home/agentuser/manim-venv/lib/python3.11/site-packages/moderngl_window/scene/camera.py__init__zCamera.__init__(   s     c3//(3S))Xc3,,
8Cd++	 8Cc** (c4EE    returnc                     | j         S )zN:py:class:`~moderngl_window.opengl.projection.Projection3D`: The 3D projection)r'   r(   s    r)   
projectionzCamera.projectionB   s     r+   xyzNc                 <    t          j        |||          | _        dS )zSet the 3D position of the camera.

        Args:
            x (float): x position
            y (float): y position
            z (float): z position
        N)r   r   r    )r(   r0   r1   r2   s       r)   set_positionzCamera.set_positionG   s     Aq))r+   yawpitchc                 J    || _         || _        |                                  dS )zSet the rotation of the camera.

        Args:
            yaw (float): yaw rotation
            pitch (float): pitch rotation
        N)r%   r$   _update_yaw_and_pitch)r(   r5   r6   s      r)   set_rotationzCamera.set_rotationQ   s*     	""$$$$$r+   c                     | j         S )zfloat: The current yaw angle.)r$   r.   s    r)   r5   z
Camera.yaw\   s     yr+   valuec                 <    || _         |                                  d S N)r$   r8   r(   r;   s     r)   r5   z
Camera.yawa   s!    	""$$$$$r+   c                     | j         S )zfloat: The current pitch angle.)r%   r.   s    r)   r6   zCamera.pitchf   s     {r+   c                 <    || _         |                                  d S r=   )r%   r8   r>   s     r)   r6   zCamera.pitchk   s!    ""$$$$$r+   c                     |                                   |                     | j        | j        | j        z   | j                  S )0glm.mat4: The current view matrix for the camera)r8   _gl_look_atr    r#   r&   r.   s    r)   matrixzCamera.matrixp   s;     	""$$$t}tx/GRRRr+   c                    t          j        ddd          }t          t          | j                            t          t          | j                            z  |_        t          t          | j                            |_        t          t          | j                            t          t          | j                            z  |_	        t          j
        |          | _        t          j
        t          j        | j        | j                            | _        t          j
        t          j        | j        | j                            | _        dS )z=Updates the camera vectors based on the current yaw and pitchr   N)r   r   r   r   r5   r6   r0   r   r1   r2   	normalizer#   crossr&   r"   r!   )r(   fronts     r)   r8   zCamera._update_yaw_and_pitchv   s    c3''gdh''((3wtz/B/B+C+CCgdj))**gdh''((3wtz/B/B+C+CC='']39TXtx#@#@AA
-	$*dh ? ?@@r+   vecposc                     |t          j        |          }|t          d          |                     | j        || j                  S )a  Look at a specific point

        Either ``vec`` or ``pos`` needs to be supplied.

        Keyword Args:
            vec (glm.vec3): position
            pos (tuple/list): list of tuple ``[x, y, x]`` / ``(x, y, x)``
        Returns:
            glm.mat4x4: Camera matrix
        Nzvector or pos must be set)r   r   
ValueErrorrC   r    r&   )r(   rI   rJ   s      r)   look_atzCamera.look_at   sF     ?(3--C;8999sDH===r+   targetr!   c                    t          j        ||z
            }t          j        t          j        t          j        |          |                    }t          j        ||          }t          j                    }|j         |d         d<   |j         |d         d<   |j         |d         d<   t          j                    }|d         |d         d<   |d         |d         d<   |d         |d         d<   |d         |d         d<   |d         |d         d<   |d         |d         d<   |d         |d         d<   |d         |d         d<   |d         |d         d<   ||z  S )zThe standard lookAt method.

        Args:
            pos: current position
            target: target position to look at
            up: direction up
        Returns:
            glm.mat4: The matrix
        r   r   r   r   )r   rF   rG   mat4r0   r1   r2   )	r(   rJ   rN   r!   r2   r0   r1   	translaterotates	            r)   rC   zCamera._gl_look_at   s8    M#,''M#)CM"$5$5q99::IaOOHJJ	5&	!Q5&	!Q5&	!Qtq	!tq	!tq	!tq	!tq	!tq	!tq	!tq	!tq	!	!!r+   )r   r   r   r   )r,   N)NN)__name__
__module____qualname____doc__floatr*   propertyr
   r/   r4   r9   r5   setterr6   r   rP   rD   r8   r   r   tuplerM   rC    r+   r)   r   r      sf         ]bF FF/4FBGFTYF F F F4  L       X *e * *% *D * * * *	% 	%e 	% 	% 	% 	% 	% U    X 	Z% %4 % % % Z% u    X \%5 %T % % % \% S S S S XS
	A 	A 	A 	A [_> >CH%>3;E%PUBU<V3W>	> > > >*"sx " "sx "CH " " " " " "r+   r   c                       e Zd ZdZeddddfdedededed	ed
ef fdZe	defd            Z
e
j        deddfd            Z
e	defd            Zej        deddfd            ZdedededdfdZdeddfdZdeddfdZdeddfdZdeddfdZdeddfdZdeddfdZdededdfdZd ed!eddfd"Ze	dej        fd#            Z xZS )$KeyboardCameraa  Camera controlled by mouse and keyboard.
    The class interacts with the key constants in the
    built in window types.

    Creating a keyboard camera:

    .. code:: python

        camera = KeyboardCamera(
            self.wnd.keys,
            fov=75.0,
            aspect_ratio=self.wnd.aspect_ratio,
            near=0.1,
            far=1000.0,
        )

    We can also interact with the belonging
    :py:class:`~moderngl_window.opengl.projection.Projection3D` instance.

    .. code:: python

        # Update aspect ratio
        camera.projection.update(aspect_ratio=1.0)

        # Get projection matrix in bytes (f4)
        camera.projection.tobytes()
    r   r   r   keyskeymapr   r   r   r   c                     || _          ||          | _        t          | _        t          | _        t          | _        d| _        d| _        d| _        d| _	        t                                          ||||           dS )aw  Initialize the camera

        Args:
            keys (BaseKeys): The key constants for the current window type
        Keyword Args:
            keymap (KeyMapFactory) : The keymap to use. By default QWERTY.
            fov (float): Field of view
            aspect_ratio (float): Aspect ratio
            near (float): near plane
            far (float): far plane
        r         $@g      ?)r   r   r   r   N)r^   r_   STILL_xdir_zdir_ydir
_last_time_last_rot_time	_velocity_mouse_sensitivitysuperr*   )r(   r^   r_   r   r   r   r   	__class__s          r)   r*   zKeyboardCamera.__init__   sw    * 	fTll


! "%S|$CPPPPPr+   r,   c                     | j         S )zfloat: Mouse sensitivity (rotation speed).

        This property can also be set::

            camera.mouse_sensitivity = 2.5
        ri   r.   s    r)   mouse_sensitivityz KeyboardCamera.mouse_sensitivity   s     &&r+   r;   Nc                     || _         d S r=   rm   r>   s     r)   rn   z KeyboardCamera.mouse_sensitivity      "'r+   c                     | j         S )zfloat: The speed this camera move based on key inputs

        The property can also be modified::

            camera.velocity = 5.0
        rh   r.   s    r)   velocityzKeyboardCamera.velocity  s     ~r+   c                     || _         d S r=   rr   r>   s     r)   rs   zKeyboardCamera.velocity  s    r+   keyaction	modifiersc                 v   || j         j        k    rP|| j        j        k    r|                     d           dS || j        j        k    r|                     d           dS dS || j         j        k    rP|| j        j        k    r|                     d           dS || j        j        k    r|                     d           dS dS || j         j        k    rN|| j        j        k    r| 	                    d           || j        j        k    r| 	                    d           dS dS || j         j
        k    rN|| j        j        k    r|                     d           || j        j        k    r|                     d           dS dS || j         j        k    rN|| j        j        k    r|                     d           || j        j        k    r|                     d           dS dS || j         j        k    rL|| j        j        k    r|                     d           || j        j        k    r|                     d           dS dS dS )zProcess key inputs and move camera

        Args:
            key: The key
            action: key action release/press
            modifiers: key modifier states such as ctrl or shit
        TFN)r_   RIGHTr^   ACTION_PRESS
move_rightACTION_RELEASELEFT	move_leftFORWARDmove_forwardBACKWARDmove_backwardDOWN	move_downUPmove_up)r(   ru   rv   rw   s       r)   	key_inputzKeyboardCamera.key_input  su    $+###///%%%%%49333&&&&& 43 DK$$$///t$$$$$49333u%%%%% 43 DK'''///!!$'''111!!%((((( 21 DK(((///""4(((111""5))))) 21 DK$$$///t$$$111u%%%%% 21 DKN""///T"""111U#####	 #" 21r+   activatec                 <    |                      t          |           dS )zThe camera should be continiously moving to the left.

        Args:
            activate (bool): Activate or deactivate this state
        N)
move_stater}   r(   r   s     r)   r~   zKeyboardCamera.move_leftB       	h'''''r+   c                 <    |                      t          |           dS )zThe camera should be continiously moving to the right.

        Args:
            activate (bool): Activate or deactivate this state
        N)r   ry   r   s     r)   r{   zKeyboardCamera.move_rightJ  s     	x(((((r+   c                 <    |                      t          |           dS )zThe camera should be continiously moving forward.

        Args:
            activate (bool): Activate or deactivate this state
        N)r   r   r   s     r)   r   zKeyboardCamera.move_forwardR  s     	*****r+   c                 <    |                      t          |           dS )zThe camera should be continiously moving backwards.

        Args:
            activate (bool): Activate or deactivate this state
        N)r   r   r   s     r)   r   zKeyboardCamera.move_backwardZ  s     	(+++++r+   c                 <    |                      t          |           dS )zThe camera should be continiously moving up.

        Args:
            activate (bool): Activate or deactivate this state
        N)r   r   r   s     r)   r   zKeyboardCamera.move_upb  s     	H%%%%%r+   c                 <    |                      t          |           dS )zThe camera should be continiously moving down.

        Args:
            activate (bool): Activate or deactivate this state
        N)r   r   r   s     r)   r   zKeyboardCamera.move_downj  r   r+   	directionc                    |t           k    r|rt          nt          | _        dS |t          k    r|rt
          nt          | _        dS |t          k    r|rt
          nt          | _        dS |t          k    r|rt          nt          | _        dS |t          k    r|rt          nt          | _
        dS |t          k    r|rt
          nt          | _
        dS dS )zSet the camera position move state.

        Args:
            direction: What direction to update
            activate: Start or stop moving in the direction
        N)ry   POSITIVErb   rc   r}   NEGATIVEr   rd   r   r   re   r   )r(   r   r   s      r)   r   zKeyboardCamera.move_stater  s     %-85DJJJ$%-85DJJJ'!!%-85DJJJ(""%-85DJJJ"__%-85DJJJ$%-85DJJJ r+   dxdyc                    t          j                     }|| j        z
  }|| _        |dk    r0t          t          |          t          |                    dk    rdS || j        z  }|| j        z  }| xj        |z  c_        | xj        |z  c_        | j        dk    rd| _        | j        dk     rd| _        |                                  dS )a  Update the rotation of the camera.

        This is done by passing in the relative
        mouse movement change on x and y (delta x, delta y).

        In the past this method took the viewport position
        of the mouse. This does not work well when
        mouse exclusivity mode is enabled.

        Args:
            dx: Relative mouse position change on x
            dy: Relative mouse position change on y
        g?r   Ng     @U@g     @U)	timerg   maxabsri   r$   r%   r6   r8   )r(   r   r   nowdeltas        r)   	rot_statezKeyboardCamera.rot_state  s     ikkd))!
 3;;3s2wwB00144F
d%%
d%%		R		r:DJ:DJ""$$$$$r+   c                    t          j                     }t          || j        z
  d          }|| _        | j        t          k    r!| xj        | j        | j        z  |z  z  c_        n0| j        t          k    r | xj        | j        | j        z  |z  z  c_        | j	        t          k    r!| xj        | j
        | j        z  |z  z  c_        n0| j	        t          k    r | xj        | j
        | j        z  |z  z  c_        | j        t          k    r!| xj        | j        | j        z  |z  z  c_        n0| j        t          k    r | xj        | j        | j        z  |z  z  c_        |                     | j        | j        | j
        z   | j                  S )z2glm.mat4x4: The current view matrix for the camerar   )r   r   rf   rc   r   r    r"   rh   r   rd   r#   re   r!   rC   r&   )r(   r   ts      r)   rD   zKeyboardCamera.matrix  sa    ikk do%q)) :!!MMTZ$.81<<MMMZ8##MMTZ$.81<<MM :!!MMTX6::MMMZ8##MMTX6::MM :!!MMTWt~599MMMZ8##MMTWt~599MMt}tx/GRRRr+   )rS   rT   rU   rV   r   r	   r   rW   r*   rX   rn   rY   rs   strr   r   boolr~   r{   r   r   r   r   intr   r   r   rP   rD   __classcell__rk   s   @r)   r]   r]      s        > !'!!Q !Q!Q !Q 	!Q
 !Q !Q !Q !Q !Q !Q !Q !QF '5 ' ' ' X' (u ( ( ( ( ( %    X _e     _-$S -$# -$# -$$ -$ -$ -$ -$^($ (4 ( ( ( ()4 )D ) ) ) )+T +d + + + +,d ,t , , , ,& & & & & &($ (4 ( ( ( (9C 94 9D 9 9 9 9(#%E #%u #% #% #% #% #%J S S S S XS S S S Sr+   r]   c            	           e Zd ZdZ	 	 	 ddeej        eeeef         f         dedeeef         de	f fd	Z
ed
ej        fd            Zed
efd            Zej        ded
dfd            Zed
efd            Zej        ded
dfd            Zed
efd            Zej        ded
dfd            Zed
efd            Zej        ded
dfd            Zdeded
dfdZded
dfdZ xZS )OrbitCameraa  Camera controlled by the mouse to pan around the target.

    The functions :py:function:`~camera.OrbitCamera.rot_state` and
    :py:function:`~camera.OrbitCamera.rot_state` are used to update the rotation and zoom.

    Creating a orbit camera:

    .. code:: python

        camera = OrbitCamera(
            target=(0., 0., 0.),
            radius=2.0
            fov=75.0,
            aspect_ratio=self.wnd.aspect_ratio,
            near=0.1,
            far=1000.0,
        )

    We can also interact with the belonging
    :py:class:`~moderngl_window.opengl.projection.Projection3D` instance.

    .. code:: python

        # Update aspect ratio
        camera.projection.update(aspect_ratio=1.0)

        # Get projection matrix in bytes (f4)
        camera.projection.tobytes()
    r   r   r          @g     F@g     FrN   radiusangleskwargsc                     || _         |\  | _        | _        t          j        |          | _        t          j        ddd          | _        d| _        d| _         t                      j
        di | dS )ao  Initialize the camera

        Keyword Args:
            target (float, float, float): Target point
            radius (float): Radius
            angles (float, float): angle_x and angle_y in degrees
            fov (float): Field of view
            aspect_ratio (float): Aspect ratio
            near (float): near plane
            far (float): far plane
        r   r   Nr[   )r   angle_xangle_yr   r   rN   r!   ri   _zoom_sensitivityrj   r*   )r(   rN   r   r   r   rk   s        r)   r*   zOrbitCamera.__init__  ss    & %+"dlhv&&(3S))"%!$""6"""""r+   r,   c                 &   t          t          | j                            t          t          | j                            z  | j        z  | j        d         z   t          t          | j                            | j        z  | j        d         z   t          t          | j                            t          t          | j                            z  | j        z  | j        d         z   f} | j        |  t          j	        || j        | j
                  S )rB   r   r   r   )r   r   r   r   r   r   rN   r4   r   lookAtr!   )r(   r    s     r)   rD   zOrbitCamera.matrix  s    
 %%&&WT\-B-B)C)CCdkQTXT_`aTbb%%&&4t{1~E%%&&WT\-B-B)C)CCdkQTXT_`aTbb

 	8$$zKG
 
 	
r+   c                     | j         S )ztfloat: camera angle x in degrees.

        This property can also be set::
            camera.angle_x = 45.
        _angle_xr.   s    r)   r   zOrbitCamera.angle_x       }r+   r;   Nc                     || _         dS )z!Set camera rotation_x in degrees.Nr   r>   s     r)   r   zOrbitCamera.angle_x       r+   c                     | j         S )ztfloat: camera angle y in degrees.

        This property can also be set::
            camera.angle_y = 45.
        _angle_yr.   s    r)   r   zOrbitCamera.angle_y$  r   r+   c                     || _         dS )z!Set camera rotation_y in degrees.Nr   r>   s     r)   r   zOrbitCamera.angle_y-  r   r+   c                     | j         S )zfloat: Mouse sensitivity (rotation speed).

        This property can also be set::
            camera.mouse_sensitivity = 2.5
        rm   r.   s    r)   rn   zOrbitCamera.mouse_sensitivity2  s     &&r+   c                     || _         d S r=   rm   r>   s     r)   rn   zOrbitCamera.mouse_sensitivity;  rp   r+   c                     | j         S )zfloat: Mousewheel zooming sensitivity (zoom speed).

        This property can also be set::
            camera.zoom_sensitivity = 2.5
        r   r.   s    r)   zoom_sensitivityzOrbitCamera.zoom_sensitivity?  s     %%r+   c                     || _         d S r=   r   r>   s     r)   r   zOrbitCamera.zoom_sensitivityH  s    !&r+   r   r   c                     | xj         || j        z  dz  z  c_         | xj        || j        z  dz  z  c_        t          t	          | j        d          d          | _        dS )a"  Update the rotation of the camera around the target point.

        This is done by passing relative mouse change in the x and y axis (delta x, delta y)

        Args:
            dx: Relative mouse position change on x axis
            dy: Relative mouse position change on y axis
        ra   g      g     eN)r   rn   r   r   min)r(   r   r   s      r)   r   zOrbitCamera.rot_stateL  sa     	T33d::T33d:: 3t|T22F;;r+   y_offsetc                 j    | xj         || j        z  z  c_         t          d| j                   | _         d S )Nr   )r   r   r   )r(   r   s     r)   
zoom_statezOrbitCamera.zoom_state[  s1    x$"888#t{++r+   )r   r   r   )rS   rT   rU   rV   r   r   r   rZ   rW   r   r*   rX   rP   rD   r   rY   r   rn   r   r   r   r   r   s   @r)   r   r     st        @ ?N&3	# #cheUE&9 ::;# # eUl#	#
 # # # # # #: 
 
 
 
 X
     X ^U t    ^     X ^U t    ^ '5 ' ' ' X' (u ( ( ( ( ( &% & & & X& 'e ' ' ' ' '<E <u < < < < <,5 ,T , , , , , , , ,r+   r   )r   typingr   r   r   r   r   r   r   moderngl_window.context.baser	   !moderngl_window.opengl.projectionr
   moderngl_window.utils.keymapsr   r   ry   r}   r   r   r   r   rb   r   r   r   r]   r   r[   r+   r)   <module>r      sj    ' ' ' ' ' ' ' ' ' ' 



 ! ! ! ! ! ! ! ! ! ! 1 1 1 1 1 1 : : : : : : ? ? ? ? ? ? ? ? 	

 	
[" [" [" [" [" [" [" ["|PS PS PS PS PSV PS PS PSfT, T, T, T, T,& T, T, T, T, T,r+   