
    iA                     "   d Z ddlZddlZddlZ	 ddlmZ ddlmZ 	 ddlmZmZmZmZmZmZ dZdZdZd	Zd
ZddZd Zd Zd Z G d d      Zd Zd Zd Zd Z d Z!e"dk(  r e!        yy# e	$ r  e
d       ew xY w# e	$ r  e
d       ew xY w)a   pygame.examples.glcube

Draw a cube on the screen.



Amazing.

Every frame we orbit the camera around a small amount
creating the illusion of a spinning object.

First we setup some points of a multicolored cube. Then we then go through
a semi-unoptimized loop to draw the cube points onto the screen.

OpenGL does all the hard work for us. :]


Keyboard Controls
-----------------

* ESCAPE key to quit
* f key to toggle fullscreen.

    Nz=pyopengl missing. The GLCUBE example requires: pyopengl numpy)arraydoteyezerosfloat32uint32z:numpy missing. The GLCUBE example requires: pyopengl numpyT))      ?      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   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   c                     t        ddd|gddd|gddd|gg dg| j                        j                  }t        | |      | d<   | S )ao  
    Translate (move) a matrix in the x, y and z axes.

    :param matrix: Matrix to translate.
    :param x: direction and magnitude to translate in x axis. Defaults to 0.
    :param y: direction and magnitude to translate in y axis. Defaults to 0.
    :param z: direction and magnitude to translate in z axis. Defaults to 0.
    :return: The translated matrix.
          ?        )r   r   r   r   dtype.)r   r   Tr   )matrixxyztranslation_matrixs        `/home/obispo/Crisostomo_bridge/mision_env/lib/python3.12/site-packages/pygame/examples/glcube.py	translater    e   se     #sA#sA#sA 		
 ll a  f01F3KM    c                     t        dt              }d|z  || z
  z  |d<   || z   || z
  z  |d<   d|z  ||z
  z  |d<   ||z   ||z
  z  |d<   ||z    ||z
  z  |d<   d	|z  |z  ||z
  z  |d
<   d|d<   |S )a  
    Build a perspective matrix from the clipping planes, or camera 'frustrum'
    volume.

    :param left: left position of the near clipping plane.
    :param right: right position of the near clipping plane.
    :param bottom: bottom position of the near clipping plane.
    :param top: top position of the near clipping plane.
    :param znear: z depth of the near clipping plane.
    :param zfar: z depth of the far clipping plane.

    :return: A perspective matrix.
    )r   r   r          @)r   r   )r   r   )r   r   )r   r   )r   r   g       r   r   g      r   )r   r   )leftrightbottomtopznearzfarperspective_matrixs          r   frustumr,   |   s     vW5#e|ut|<t %>t#e|sV|<t #fv>t!%$,?t#e|d2dUlCt#tr!   c                     t        j                  | dz  t         j                  z        |z  }||z  }t        | || |||      S )aT  
    Build a perspective matrix from field of view, aspect ratio and depth
    planes.

    :param fovy: the field of view angle in the y axis.
    :param aspect: aspect ratio of our view port.
    :param znear: z depth of the near clipping plane.
    :param zfar: z depth of the far clipping plane.

    :return: A perspective matrix.
    g     v@)mathtanpir,   )fovyaspectr)   r*   hws         r   perspectiver5      sF     	'(50A	F
AA2q1"a--r!   c                 0   t         j                  |z  dz  }t        j                  |      t        j                  |      }}t        j                  ||z  ||z  z   ||z  z         }||z  ||z  ||z  }}}d|z
  |z  d|z
  |z  d|z
  |z  }
}	}t        ||z  |z   |	|z  ||z  z
  |
|z  ||z  z   dg||z  ||z  z   |	|z  |z   |
|z  ||z  z
  dg||z  ||z  z
  |	|z  ||z  z   |
|z  |z   dgg dg| j                        j                  }t        | |      | d<   | S )a  
    Rotate a matrix around an axis.

    :param matrix: The matrix to rotate.
    :param angle: The angle to rotate by.
    :param x: x of axis to rotate around.
    :param y: y of axis to rotate around.
    :param z: z of axis to rotate around.

    :return: The rotated matrix
       r   r   r   r   r   r   r   .)	r.   r0   cossinsqrtr   r   r   r   )r   angler   r   r   csncxcyczrotation_matrixs               r   rotaterD      sP    GGeOc!E88E?DHHUOqA		!a%!a%-!a%'(A!eQUAE!qAa%1q1ukAEQ;BB!VaZa!a%a!a%;!Va!e^R!VaZa!a%;!Va!e^R!Va!e^R!VaZ;		
 ll a  fo.F3KMr!   c                       e Zd ZdZd Zy)Rotationz?
    Data class that stores rotation angles in three axes.
    c                 .    d| _         d| _        d| _        y )N   (      )thetaphipsi)selfs    r   __init__zRotation.__init__   s    
r!   N)__name__
__module____qualname____doc__rO    r!   r   rF   rF      s    r!   rF   c                  .   t        t        t        t                    } t	        j
                  t        j                         t        D ];  }|D ]4  }| |   \  }}t	        j                  |       t	        j                  |       6 = t	        j                          t	        j                  ddd       t	        j
                  t        j                         t        D ]&  }|D ]  }| |   \  }}t	        j                  |       ! ( t	        j                          y)zK
    Draw the cube using the old open GL methods pre 3.2 core context.
    r   N)listzipCUBE_POINTSCUBE_COLORSGLglBeginGL_QUADSCUBE_QUAD_VERTS
glColor3fvglVertex3fvglEnd	glColor3fGL_LINES
CUBE_EDGES)	allpointsfacevertposcolorlines         r   drawcube_oldrj      s     Sk23IJJr{{   	 D"4JCMM% NN3	  
 HHJLLc3JJr{{   	 D"4JCNN3	  
 HHJr!   c                  F   t        j                  t         j                         t        j                  t         j                         t        j
                          t        j                  dddd       t        j                  ddd       t        j                  ddd	d	       y
)z7
    Initialise open GL, prior to core context 3.2
         F@gUUUUUU?g?      Y@r   g      rJ   r   r   N)
rZ   glEnableGL_DEPTH_TESTglMatrixModeGL_PROJECTIONglLoadIdentityGLUgluPerspectiveglTranslatef	glRotatefrT   r!   r   init_gl_stuff_oldrw      sl     KK  ! OOB$$%t[#u5OOCd#LLQ1r!   c           	      8   d}d}t        j                         }t        j                  t         j                        }t        j                  t         j                        }t        j
                  ||       t        j                  |       t        j                  |      }t        |t              r|j                         }|j                  d      D ]  }t        |        t        j                  ||       t        j
                  ||       t        j                  |       t        j                  |      }t        |t              r|j                         }|j                  d      D ]  }t        |        t        j                  ||       t        j                  |       t        j                  |       t        j                   ||       t        j                   ||       t        j"                  |       t%        ddt&        dfdt&        dfg      }g d	g d
g dg dg dg dg dg dg|d<   g dg dg dg dg dg dg dg dg|d<   t)        g dt*              }	t)        g dt*              }
i i d}t        j,                  t        j.                  d             t        j0                  d      |d   d<   t        j2                  t         j4                  |d   d          t        j6                  t         j4                  |j8                  |t         j:                         |j<                  d    }t?        j@                  d       }t        jB                  |d      }t        jD                  |       t        jF                  |dt         jH                  d!||       t?        j@                  |jJ                  d   jL                        }t        jB                  |d      }t        jD                  |       t        jF                  |dt         jH                  d!||       t        j0                  d      |d   d"<   t        j2                  t         jN                  |d   d"          t        j6                  t         jN                  |	j8                  |	t         jP                         t        j0                  d      |d   d#<   t        j2                  t         jN                  |d   d#          t        j6                  t         jN                  |
j8                  |
t         jP                         t        jR                  |d$      |d%   d$<   t        jT                  |d%   d$   dd!tW        d             t        jR                  |d&      |d%   d&<   tY        tW        d      d'(      }t        jT                  |d%   d&   dd!|       t        jR                  |d)      |d%   d)<   t        jT                  |d%   d)   dd!tW        d             t        jR                  |d*      |d%   d*<   t        jZ                  |d%   d*   dddd       t        jR                  |d+      |d%   d+<   t        jZ                  |d%   d+   d d d d        t        j\                  d d d d        t        j^                  dd       t        j`                  t         jb                         t        jd                  t         jf                  t         jh                         t        jj                  t         jl                         t        jn                  t         jp                  t         jr                         t        jt                  d,       tw        d-| d    ty        | d         z  d.d/      }t        jT                  |d%   d)   dd!|       ||	|
fS )0z
    Initialise open GL in the 'modern' open GL style for open GL versions
    greater than 3.1.

    :param display_size: Size of the window/viewport.
    a  

    #version 150
    uniform mat4   model;
    uniform mat4   view;
    uniform mat4   projection;

    uniform vec4   colour_mul;
    uniform vec4   colour_add;

    in vec4 vertex_colour;         // vertex colour in
    in vec3 vertex_position;

    out vec4   vertex_color_out;            // vertex colour out
    void main()
    {
        vertex_color_out = (colour_mul * vertex_colour) + colour_add;
        gl_Position = projection * view * model * vec4(vertex_position, 1.0);
    }

    z
    #version 150
    in vec4 vertex_color_out;  // vertex colour from vertex shader
    out vec4 fragColor;
    void main()
    {
        fragColor = vertex_color_out;
    }
    
   vertex_positionr   vertex_colourr   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   r8   )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   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   )buffer	constantsr   r~   verticesr   Ffilledoutlinemodelr   viewi)r   
projection
colour_mul
colour_addr   rl   r#   rm   )=rZ   glCreateProgramglCreateShaderGL_VERTEX_SHADERGL_FRAGMENT_SHADERglShaderSourceglCompileShaderglGetShaderInfoLog
isinstancebytesdecodesplitprintglAttachShaderglValidateProgramglLinkProgramglDetachShaderglUseProgramr   r   r   r   glBindVertexArrayglGenVertexArraysglGenBuffersglBindBufferGL_ARRAY_BUFFERglBufferDatanbytesGL_DYNAMIC_DRAWstridesctypesc_void_pglGetAttribLocationglEnableVertexAttribArrayglVertexAttribPointerGL_FLOATr   itemsizeGL_ELEMENT_ARRAY_BUFFERGL_STATIC_DRAWglGetUniformLocationglUniformMatrix4fvr   r    glUniform4fglClearColorglPolygonOffsetrn   GL_LINE_SMOOTHglBlendFuncGL_SRC_ALPHAGL_ONE_MINUS_SRC_ALPHAglDepthFuncGL_LESSglHintGL_LINE_SMOOTH_HINT	GL_NICESTglLineWidthr5   float)display_sizevertex_codefragment_codeprogramvertexfragmentlogri   r   filled_cube_indicesoutline_cube_indicesshader_datastrideoffsetlocr   r   s                    r   init_gl_modernr      s3   K,M   "Gr223F  !6!67Hfk*v 


'C#ujjl		$ d gv&h.x  


)C#ujjl		$ d gx(!Wgv&gx(OOG 	+ow-JKH
 		#H 		!H_  %	
L O(T !P
  b1K--a01(*(:K*%OOB&&H(=j(IJOOB&&2CUCUVa F__QF

 
 *;
<C  %S!R[[%H__X^^,=>GGHF

 
 /
:C  %S!R[[%H&(ooa&8K(#OOB..H0Eh0OPOO
""""
	 (*q'9K)$OOB..H0Ei0PQOO
""##
	 )+(?(?(QKW%+k27;Qs1vN')'>'>w'OKV$SVr"D+k26:AudK-/-D-D.K\* +k2<@!UCPQFS .0-D-D.K\* NN;{+L91aAF .0-D-D.K\* NN;{+L91aAF OOAq!Qq!KK!!"NN2??B$=$=>NN2::IIb$$bll3NN3T<?U<?5K#KSRWXJ+k2<@!UJW+-AAAr!   c                 J   t        j                  t         j                  t         j                  z         t        j                  t         j
                         t        j                  t         j                         t        j                  t         j                         t        j                  | d   d   dddd       t        j                  | d   d   dddd       t        j                  t         j                  | d   d          t        j                  t         j                  t        |      t         j                  d	       t        j                  t         j                         t        j                  t         j
                         t        j                  | d   d   dddd       t        j                  | d   d   dddd
       t        j                  t         j                  | d   d          t        j                  t         j                   t        |      t         j                  d	       |xj"                  d
z  c_        t%        dt&              }t)        ||j"                  ddd       t)        ||j*                  ddd       t        j,                  | d   d   dd|       y	)ao  
    Draw a cube in the 'modern' Open GL style, for post 3.1 versions of
    open GL.

    :param shader_data: compile vertex & pixel shader data for drawing a cube.
    :param filled_cube_indices: the indices to draw the 'filled' cube.
    :param outline_cube_indices: the indices to draw the 'outline' cube.
    :param rotation: the current rotations to apply.
    r   r   r   r   r   r   r~   r   Nr   r   r   r   r   F)rZ   glClearGL_COLOR_BUFFER_BITGL_DEPTH_BUFFER_BIT	glDisableGL_BLENDrn   ro   GL_POLYGON_OFFSET_FILLr   r   r   glDrawElementsGL_TRIANGLESlenGL_UNSIGNED_INTrb   rL   r   r   rD   rM   r   )r   r   r   rotationr   s        r   draw_cube_modernr     s    JJr%%(>(>>? LLKK  !KK))*NN;{+L91aAFNN;{+L91aCHOOB..H0Eh0OP
0123E3Et
 LL**+KKNN;{+L91aCHNN;{+L91aCHOOB..H0Ei0PQbkk3';#<b>P>PRVW LLCL!E
5(,,1a(
5(,,1a(+k27;QuMr!   c                  L   t        j                          d} t        rd} t         j                  j	                  t         j
                  | d          t         j                  j	                  t         j                  | d          t         j                  j	                  t         j                  t         j                         d}d}t         j                  j                  |t         j                  t         j                  z  t         j                  z         t        rt        |      \  }}}t               }n
t                d}|rQt         j                   j#                         }|D ]  }	|	j$                  t         j&                  k(  s:|	j$                  t         j(                  k(  r |	j*                  t         j,                  k(  rd}^|	j$                  t         j(                  k(  s||	j*                  t         j.                  k(  s|s\t1        d       t         j                  j                  dt         j                  t         j                  z  t         j2                  z         nJt1        d	       t         j                  j                  dt         j                  t         j                  z         | }| d   d
k\  s| d   dk(  r#| d   dk\  rt        |      \  }}}t               }xt                 t        rt5               nVt7        j8                  t6        j:                  t6        j<                  z         t7        j>                  dddd       tA                t         j                  jC                          t         jD                  jG                  d       |rQt        jH                          y)zrun the demo)r   r   r$   r   r   F)i  i  TzChanging to FULLSCREENzChanging to windowed moder   r   r   
   N)%pginitUSE_MODERN_GLdisplaygl_set_attributeGL_CONTEXT_MAJOR_VERSIONGL_CONTEXT_MINOR_VERSIONGL_CONTEXT_PROFILE_MASKGL_CONTEXT_PROFILE_COREset_modeOPENGL	DOUBLEBUF	RESIZABLEr   rF   rw   eventgettypeQUITKEYDOWNkeyK_ESCAPEK_fr   
FULLSCREENr   rZ   r   r   r   rv   rj   fliptimewaitquit)

gl_version
fullscreenr   gpu	f_indices	o_indicesr   goingeventsr   s
             r   mainr     sq    GGIJ
 	

##B$?$?AO


##B$?$?AO


##&&(B(B	
 JLJJbii",,&>&MN$2<$@!Y	:E
 	(EzzRWW$

bjj(UYY"++-Erzz)eii266.A!23JJ''"BII$<r}}$L 56JJ''
BII4LM!+^
a=A%*Q-1*<ARSAS0>|0L-CI'zH%')	(, S)YA JJr--0F0FFGLLAq!$N



RG J GGIr!   __main__)r   r   r   )#rS   r.   r   pygamer   	OpenGL.GLrZ   
OpenGL.GLUrs   ImportErrorr   
SystemExitnumpyr   r   r   r   r   r   r   rX   rY   r]   rc   r    r,   r5   rD   rF   rj   rw   r   r   r   rP   rT   r!   r   <module>r     s   0   
== 		
 .2."< 0eBP(NVCL zF Y  	
IJ
  	
FG
s   A+ A> +A;>B