o
    tVgA                     @   s  d Z ddlZddlZddlZzddlmZ ddlmZ W n e	y)   e
d ew zddlmZmZmZmZmZmZ W n e	yI   e
d ew dZdZdZd	Zd
Zd!ddZdd Zdd Zdd ZG dd dZdd Zdd Zdd Zdd Z dd Z!e"d kre!  dS dS )"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                 C   sF   t ddd|gddd|gddd|gg dg| jd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   r   dtype.)r   r   Tr   )matrixxyztranslation_matrix r   J/home/ubuntu/.local/lib/python3.10/site-packages/pygame/examples/glcube.py	translatee   s   



	r   c                 C   s   t dtd}d| ||   |d< ||  ||   |d< d| ||  |d< || ||  |d< ||  ||  |d< d	| | ||  |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_matrixr   r   r   frustum|   s   r)   c                 C   s6   t | d t j | }|| }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wr   r   r   perspective   s   r1   c                 C   s,  t j| d }t |t |}}t || ||  ||  }|| || || }}}d| | d| | d| | }}	}
t|| | |	| ||  |
| ||  dg|| ||  |	| | |
| ||  dg|| ||  |	| ||  |
| | dgg dg| jd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*   r,   cossinsqrtr   r   r   r   )r   angler   r   r   csncxcyczrotation_matrixr   r   r   rotate   s"   (***	r?   c                   @   s   e Zd ZdZdd ZdS )Rotationz?
    Data class that stores rotation angles in three axes.
    c                 C   s   d| _ d| _d| _d S )N   (      )thetaphipsi)selfr   r   r   __init__   s   
zRotation.__init__N)__name__
__module____qualname____doc__rH   r   r   r   r   r@      s    r@   c                  C   s   t ttt} ttj tD ]}|D ]}| | \}}t| t	| qqt
  tddd ttj tD ]}|D ]}| | \}}t	| q>q:t
  dS )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liner   r   r   drawcube_old   s"   
ra   c                   C   sR   t t j t t j t   tdddd t ddd t 	ddd	d	 d
S )z7
    Initialise open GL, prior to core context 3.2
         F@gUUUUUU?g?      Y@r   g      rC   r
   r   N)
rQ   glEnableGL_DEPTH_TESTglMatrixModeGL_PROJECTIONglLoadIdentityGLUgluPerspectiveglTranslatef	glRotatefr   r   r   r   init_gl_stuff_old   s   rm   c              	   C   s  d}d}t  }t t j}t t j}t || t | t |}t|t	r-|
 }|dD ]}t| q2t || t || t | t |}t|t	rX|
 }|dD ]}t| q]t || t | t | t || t || t | 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d}	tg dtd}
i i d}t t d t d|d d< t t j|d d  t t j|j|t j |jd  }t d }t !|d}t "| t #|dt j$d!|| t |j%d j&}t !|d}t "| t #|dt j$d!|| t d|d d"< t t j'|d d"  t t j'|	j|	t j( t d|d d#< t t j'|d d#  t t j'|
j|
t j( t )|d$|d% d$< t *|d% d$ dd!t+d t )|d&|d% d&< t,t+dd'd(}t *|d% d& dd!| t )|d)|d% d)< t *|d% d) dd!t+d t )|d*|d% d*< t -|d% d* dddd t )|d+|d% d+< t -|d% d+ d d d d  t .d d d d  t /dd t 0t j1 t 2t j3t j4 t 5t j6 t 7t j8t j9 t :d, t;d-| d  t<| d  d.d/}t *|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
   )rr   rr   r
   )r
   rr   r
   )r
   rr   rr   )r
   r
   rr   )rr   r
   rr   )rr   rr   rr   )r   r
   r
   r
   )r   r   r
   r
   r3   )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
   rs   verticesr   Ffilledoutlinemodelrt   viewi)r   
projection
colour_mul
colour_addr   rb   r    rc   )=rQ   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glPolygonOffsetrd   GL_LINE_SMOOTHglBlendFuncGL_SRC_ALPHAGL_ONE_MINUS_SRC_ALPHAglDepthFuncGL_LESSglHintGL_LINE_SMOOTH_HINT	GL_NICESTglLineWidthr1   float)display_sizevertex_codefragment_codeprogramvertexfragmentlogr`   ru   filled_cube_indicesoutline_cube_indicesshader_datastrideoffsetlocry   rz   r   r   r   init_gl_modern   s   












&*






r   c                 C   sl  t t jt jB  t t j t t j t t j t 	| d d dddd t 	| d d dddd t 
t j| d d  t t jt|t jd	 t t j t t j t 	| d d dddd t 	| d d dddd
 t 
t j| d d  t t jt|t jd	 | jd
7  _tdtd}t||jddd t||jddd t | d d dd| d	S )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.
    rt   r{   r
   r|   r   r   rs   rv   Nr   rw   r   r   rx   F)rQ   glClearGL_COLOR_BUFFER_BITGL_DEPTH_BUFFER_BIT	glDisableGL_BLENDrd   re   GL_POLYGON_OFFSET_FILLr   r   r   glDrawElementsGL_TRIANGLESlenGL_UNSIGNED_INTrY   rE   r   r   r?   rF   r   )r   r   r   rotationrx   r   r   r   draw_cube_modern  s*   r   c            
      C   s  t   d} tr'd} t jt j| d  t jt j| d  t jt jt j d}d}t j	|t j
t jB t jB  trFt|\}}}t }nt  d}|rt j }|D ]j}	|	jt jksh|	jt jkrk|	jt jkrkd}qT|	jt jkr|	jt jkr|std t j	dt j
t jB t jB  ntd	 t j	dt j
t jB  | }| d d
ks| d dkr| d dkrt|\}}}t }qTt  qTtrt|||| nttjtjB  tdddd t   t j!  t j"#d |sMt $  dS )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   r@   rm   eventgettypeQUITKEYDOWNkeyK_ESCAPEK_fr   
FULLSCREENr   rQ   r   r   r   rl   ra   fliptimewaitquit)

gl_version
fullscreenr   gpu	f_indices	o_indicesr   goingeventsr   r   r   r   main  s\   
$
%r   __main__)r   r   r   )#rL   r*   r   pygamer   	OpenGL.GLrQ   
OpenGL.GLUri   ImportErrorr   
SystemExitnumpyr   r   r   r   r   r   r   rO   rP   rT   rZ   r   r)   r1   r?   r@   ra   rm   r   r   r   rI   r   r   r   r   <module>   sH   $	
 i+F
