o
    OZŽhÜA  ã                   @   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r‰e!ƒ  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ÚzZtranslation_matrix© r   úE/var/www/auris/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ÚzfarZ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'   )ZfovyZaspectr%   r&   ÚhÚwr   r   r   Úperspective•   s   r-   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   Zangler   r   r   ÚcÚsÚnZcxÚcyÚczZrotation_matrixr   r   r   Úrotate¦   s"   (***üùø	r8   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__rA   r   r   r   r   r9   Ä   s    r9   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ÚGLZglBeginZGL_QUADSÚCUBE_QUAD_VERTSZ
glColor3fvZglVertex3fvZglEndZ	glColor3fÚGL_LINESÚ
CUBE_EDGES)Z	allpointsZfaceZvertÚposÚcolorÚliner   r   r   Údrawcube_oldÏ   s"   
ýþrQ   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      Àr<   r
   r   N)
rJ   ÚglEnableÚGL_DEPTH_TESTZglMatrixModeZGL_PROJECTIONZglLoadIdentityÚGLUZgluPerspectiveZglTranslatefÚ	glRotatefr   r   r   r   Úinit_gl_stuff_oldç   s   rX   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;
    }
    Ú
é   Zvertex_positionr   Z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
   r/   )r   r
   r   r
   )r
   r
   r   r
   )r
   r
   r
   r
   )r
   r   r
   r
   )r
   r   r   r
   )$r   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r
   r
   r   r   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )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   rR   r   rS   )=rJ   ZglCreateProgramZglCreateShaderZGL_VERTEX_SHADERZGL_FRAGMENT_SHADERZglShaderSourceZglCompileShaderZglGetShaderInfoLogÚ
isinstanceÚbytesÚdecodeÚsplitÚprintZglAttachShaderZglValidateProgramZglLinkProgramZglDetachShaderZglUseProgramr   r   r   r   ZglBindVertexArrayZglGenVertexArraysZglGenBuffersÚglBindBufferZGL_ARRAY_BUFFERZglBufferDataÚnbytesZGL_DYNAMIC_DRAWÚstridesÚctypesZc_void_pZglGetAttribLocationZglEnableVertexAttribArrayZglVertexAttribPointerZGL_FLOATr   ÚitemsizeÚGL_ELEMENT_ARRAY_BUFFERZGL_STATIC_DRAWZglGetUniformLocationÚglUniformMatrix4fvr   r   ÚglUniform4fZglClearColorZglPolygonOffsetrT   ZGL_LINE_SMOOTHZglBlendFuncZGL_SRC_ALPHAZGL_ONE_MINUS_SRC_ALPHAZglDepthFuncZGL_LESSZglHintZGL_LINE_SMOOTH_HINTZ	GL_NICESTZglLineWidthr-   Úfloat)Údisplay_sizeZvertex_codeZfragment_codeÚprogramZvertexÚfragmentÚlogrP   r^   Úfilled_cube_indicesÚoutline_cube_indicesÚshader_dataZstrideÚoffsetÚlocrb   rc   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.
    r]   rd   r
   re   r   r   r\   r_   Nr   r`   r   r   ra   F)rJ   ÚglClearÚGL_COLOR_BUFFER_BITÚGL_DEPTH_BUFFER_BITZ	glDisableZGL_BLENDrT   rU   ZGL_POLYGON_OFFSET_FILLrr   rk   rp   ZglDrawElementsZGL_TRIANGLESÚlenZGL_UNSIGNED_INTrL   r>   r   r   r8   r?   rq   )rz   rx   ry   Úrotationra   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}|rêt 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¾|sŒt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r»t|ƒ\}}}tƒ }qTtƒ  qTtrÉt||||ƒ 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ÚdisplayZgl_set_attributeZGL_CONTEXT_MAJOR_VERSIONZGL_CONTEXT_MINOR_VERSIONZGL_CONTEXT_PROFILE_MASKZGL_CONTEXT_PROFILE_COREÚset_modeZOPENGLZ	DOUBLEBUFZ	RESIZABLEr}   r9   rX   ÚeventÚgetÚtypeZQUITZKEYDOWNÚkeyZK_ESCAPEZK_frj   Z
FULLSCREENrƒ   rJ   r~   r   r€   rW   rQ   ÚflipÚtimeÚwaitÚquit)
Z
gl_versionZ
fullscreenrt   ZgpuZ	f_indicesZ	o_indicesr‚   ZgoingÚeventsrŠ   r   r   r   Úmain  s\   ÿ
ÿ$€
Ý%r“   Ú__main__)r   r   r   )#rE   r(   rn   Zpygamer…   Z	OpenGL.GLrJ   Z
OpenGL.GLUrV   ÚImportErrorrj   Ú
SystemExitÚnumpyr   r   r   r   r   r   r‡   rH   rI   rK   rM   r   r'   r-   r8   r9   rQ   rX   r}   rƒ   r“   rB   r   r   r   r   Ú<module>   sH   þ$þ	
 i+F
ÿ