
    3jL                        S r SSKJr  SSKJr  SSKJr  SSKJr  SSK	J
r
JrJr  SSKrSSKr\R                  " S5      rSS	K	JrJr  SSKrS
r " S S\5      r\R.                  rSSKJr  Sr\R4                  (       a   SSKJrJrJrJr  \c,   " S S\5      r " S S\5      r " S S\5      r " S S\5      r0 r"S r#SS jr$g! \ a  r \RC                  S5         Sr C NVSr C ff = f)a  VertexBufferObject helper class

Basic usage:

    my_data = numpy.array( data, 'f')
    my_vbo = vbo.VBO( my_data )
    ...
    my_vbo.bind()
    try:
        ...
        glVertexPointer( my_vbo, ... )
        ...
        glNormalPointer( my_vbo + 12, ... )
    finally:
        my_vbo.unbind()
    
    or 
    
    with my_vbo:
        ...
        glVertexPointer( my_vbo, ... )
        ...
        glNormalPointer( my_vbo + 12, ... )        

See the OpenGLContext shader tutorials for a gentle introduction on the 
usage of VBO objects:

    http://pyopengl.sourceforge.net/context/tutorials/shader_intro.xhtml

This implementation will choose either the ARB or Core (OpenGL 1.5) 
implementation of the VBO functions.
    )ArrayDatatype)FormatHandler)_types)error)bytesunicodeas_8_bitNzOpenGL.arrays.vbo)longinteger_types)VBO
VBOHandlermapVBOc                       \ rS rSrSr/ rSr\S 5       r\S 5       r	SR                  5       rSrS rS	 rS
 r\rS r0 rSrg)Implementation1   zBAbstraction point for the various implementations that can be usedNc                 :    U R                   R                  U 5        g N)IMPLEMENTATION_CLASSESappend)clss    K/home/wildlama/miniconda3/lib/python3.13/site-packages/OpenGL/arrays/vbo.pyregisterImplementation.register7   s    ""))#.    c                     U R                   c9  U R                   H)  nU" 5       nU(       d  M  U[        l           U R                   $    U R                   $ r   )CHOSENr   r   )r   argspossibleimplementations       r   get_implementation!Implementation.get_implementation;   sI    ::66!)8,:N)zz 7
 zzr   a  glGenBuffers
    glBindBuffer 
    glBufferData 
    glBufferSubData 
    glDeleteBuffers
    glMapBuffer
    glUnmapBuffer
    GL_STATIC_DRAW
    GL_STATIC_READ
    GL_STATIC_COPY
    GL_DYNAMIC_DRAW
    GL_DYNAMIC_READ
    GL_DYNAMIC_COPY
    GL_STREAM_DRAW
    GL_STREAM_READ
    GL_STREAM_COPY
    GL_ARRAY_BUFFER
    GL_ELEMENT_ARRAY_BUFFER
    GL_UNIFORM_BUFFER
    GL_TEXTURE_BUFFER
    GL_TRANSFORM_FEEDBACK_BUFFERFc                     UR                  S5      =(       a    UR                  S5      =(       d)    UR                  S5      =(       a    UR                  S5      =(       a    US:g  $ )NglARBGL_glInitVertexBufferObjectARB)
startswithendswithselfnames     r   _arbnameImplementation._arbname\   sQ    __T";t}}U'; A&?4==+?6 44	6r   c                 r    UR                  S5      (       a  US S $ UR                  S5      (       a  US S $ U$ )N_ARBr$   )r(   r)   s     r   basenameImplementation.basenameb   s=    ==  9]]5!!9Kr   c                     U R                   $ r   )	availabler*   s    r   __nonzero__Implementation.__nonzero__j   s    ~~r   c                 d   ^ ^^^^ [         R                  m[        R                  mUUUUU 4S jnU$ )z5Produce a deleter callback to delete the given bufferc                  P  > T(       a5   TR                  5       n T" U5      nT	R                  SU5        T(       a  M5   T	R
                  R                  T5        g ! [        T[        4 a
  n S nAN<S nAff = f! [         a  n S nAMK  S nAff = f! [         a
  n S nAg S nAff = f)N   )popglDeleteBuffersAttributeError	TypeError
IndexError
_DELETERS_KeyError)
r   namedbufferbuferrbuffersgluintkeynfer*   s
        r   doBufferDeletion0Implementation.deleter.<locals>.doBufferDeletionv   s    $[[]F %Vn,,Q4 '##C( +C;  "   s3   A9 A B A61A69
B	B
B% B%)r   NullFunctionErrorr   GLuint)r*   rG   rI   rK   rH   rJ   s   ``` @@r   deleterImplementation.deletero   s+     %%	 	&  r    )__name__
__module____qualname____firstlineno____doc__r   r   classmethodr   r    splitEXPORTED_NAMESr5   r,   r2   r7   __bool__rO   rA   __static_attributes__rQ   r   r   r   r   1   sr    LF/ /  $( %*EG) * I6 H 8 Jr   r   )acceleratesupport)r   	VBOOffsetr   VBOOffsetHandlerz5Unable to load VBO accelerator from OpenGL_acceleratec                       \ rS rSrSrSrSr   SS jrSr\	" \
5      rS rSS jrS	 rS
 rS rS rS rS rS rS rS rS r\rSS jrSrg)r      zInstances can be passed into array-handling routines

You can check for whether VBOs are supported by accessing the implementation:

    if bool(vbo.get_implementation()):
        # vbo version of code
    else:
        # fallback version of code
FTNc                 Z    X l         U R                  X5        X0l        / U l        / U l        g)as  Initialize the VBO object

data -- PyOpenGL-compatible array-data structure, numpy arrays, ctypes arrays, etc.
usage -- OpenGL usage constant describing expected data-flow patterns (this is a hint
    to the GL about where/how to cache the data)

    GL_STATIC_DRAW_ARB
    GL_STATIC_READ_ARB
    GL_STATIC_COPY_ARB
    GL_DYNAMIC_DRAW_ARB
    GL_DYNAMIC_READ_ARB
    GL_DYNAMIC_COPY_ARB
    GL_STREAM_DRAW_ARB
    GL_STREAM_READ_ARB
    GL_STREAM_COPY_ARB

    DRAW constants suggest to the card that the data will be primarily used to draw
    on the card.  READ that the data will be read back into the GL.  COPY means that
    the data will be used both for DRAW and READ operations.

    STATIC suggests that the data will only be written once (or a small number of times).
    DYNAMIC suggests that the data will be used a small number of times before being
    discarded.
    STREAM suggests that the data will be updated approximately every time that it is
    used (that is, it will likely only be used once).

target -- VBO target to which to bind (array or indices)
    GL_ARRAY_BUFFER -- array-data binding
    GL_ELEMENT_ARRAY_BUFFER -- index-data binding
    GL_UNIFORM_BUFFER -- used to pass mid-size arrays of data packed into a buffer
    GL_TEXTURE_BUFFER -- used to pass large arrays of data as a pseudo-texture
    GL_TRANSFORM_FEEDBACK_BUFFER -- used to receive transformed vertices for processing

size -- if not provided, will use arrayByteCount to determine the size of the data-array,
    thus this value (number of bytes) is required when using opaque data-structures,
    (such as ctypes pointers) as the array data-source.
N)usage	set_arraytargetrG   _copy_segments)r*   datarb   rd   sizes        r   __init__VBO.__init__   s*    X JNN4& KDL"$Dr   c                     [        U[        [        45      (       a/  [        U R                  U R                  R                  U5      5      $ U$ )z#Resolve string constant to constant)
isinstancer   r   getattrr   r2   r*   values     r   resolveVBO.resolve   s>    %%!122t22D4G4G4P4PQV4WXXLr   c                     Xl         SU l        Ub  X l        gU R                   b&  [        R                  " U R                   5      U l        gg)ab  Update our entire array with new data

data -- PyOpenGL-compatible array-data structure, numpy arrays, ctypes arrays, etc.
size -- if not provided, will use arrayByteCount to determine the size of the data-array,
    thus this value (number of bytes) is required when using opaque data-structures,
    (such as ctypes pointers) as the array data-source.
FN)rf   copiedrg   r   arrayByteCount)r*   rf   rg   s      r   rc   VBO.set_array   s@     IDK 	&)88C	 'r   c                    UR                   (       a  UR                   S:X  d  [        S5      e[        R                  " U5      nUR                  =(       d    SnUR
                  =(       d    [        U R                  5      nUS:  a%  U[        U R                  5      -  n[        US45      nUS:  a%  U[        U R                  5      -  n[        US45      nX0R                  U'   U R                  (       a  U R                  (       a  XE-
  [        U R                  5      :X  a  SU l	        g[        U5      (       aL  [        R                  " U R                  S   5      nXF-  nXV-  nU R                  R                  XEU-
  U45        gggg)ac  Set slice of data on the array and vbo (if copied already)

slice -- the Python slice object determining how the data should
    be copied into the vbo/array
array -- something array-compatible that will be used as the
    source of the data, note that the data-format will have to
    be the same as the internal data-array to work properly, if
    not, the amount of data copied will be wrong.

This is a reasonably complex operation, it has to have all sorts
of state-aware changes to correctly map the source into the low-level
OpenGL view of the buffer (which is just bytes as far as the GL
is concerned).
r;   z(Don't know how to map stepped arrays yetr   FN)stepNotImplementedErrorr   asArraystartstoplenrf   maxrr   rG   rs   re   r   )r*   slicearrayrf   ry   rz   rg   s          r   __setitem__VBO.__setitem__   s5    zz%**/)B  !((/DKK$1E::/TYYDqyTYY'UAJaxDII&D!9~#IIe{{t||<3tyy>1"'DKYY )77		!ED MELD''..ut/LM 	  ,{r   c                 ,    [        U R                  5      $ )z.Delegate length/truth checks to our data-array)r{   rf   r6   s    r   __len__VBO.__len__,  s    tyy>!r   c                 P    US;  a  [        U R                  U5      $ [        U5      e)z4Delegate failing attribute lookups to our data-array)rf   rb   rd   rG   rr   _I_r   re   )rl   rf   r>   r*   rI   s     r   __getattr__VBO.__getattr__0  s-     	 	 tyy#..$S))r   c           	         U R                   (       a   S5       e[        U R                  R                  S5      5      /U l         U R	                  U R
                  5      U l        U R	                  U R                  5      U l        [        R                  " X R                  R                  U R                   [        U 5      5      5      U R                  R                  [        U 5      '   U R                   $ )zCreate the internal buffer(s)zAlready created the bufferr;   )rG   r
   r   glGenBuffersro   rd   rb   weakrefrefrO   idrA   r6   s    r   create_buffersVBO.create_buffers@  s    ||E%EE# !4!4!A!A!!DEFDL,,t{{3DKdjj1DJ7>{{))11$,,4I8D**2d84 <<r   c                    U R                   (       d   S5       eU R                  (       a  U R                  (       a  U R                  (       ao  U R                  R                  S5      u  pn[        R
                  " U5      nU R                  R                  U R                  XU5        U R                  (       a  Mn  gggU R                  b2  U R                  c%  [        R                  " U R                  5      U l
        U R                  R                  U R                  U R                  U R                  U R                  5        SU l        g)a  Copy our data into the buffer on the GL side (if required)

Ensures that the GL's version of the data in the VBO matches our
internal view of the data, either by copying the entire data-set
over with glBufferData or by updating the already-transferred
data with glBufferSubData.
z)Should do create_buffers before copy_datar   NT)rG   rr   re   r<   r   voidDataPointerr   glBufferSubDatard   rf   rg   rs   glBufferDatarb   )r*   ry   rg   rf   dataptrs        r   	copy_dataVBO.copy_dataK  s     <<P!PP<{{&&--,0,?,?,C,CA,F)T"/"?"?"E++;; KKg --- ' 99(TYY-> - < <TYY GDI##00KKIIIIJJ	 #r   c                 ,   U R                   (       a\  U R                   (       aJ   U R                  R                  SU R                   R                  S5      5        U R                   (       a  MI  ggg! [        [
        R                  4 a
  n SnAN5SnAff = f)zDelete this buffer explicitlyr;   r   N)rG   r   r=   r<   r>   r   rM   )r*   rF   s     r   delete
VBO.deleteg  sk    ||ll++;;At||?O?OPQ?RS lll  +E,C,CD s   5A/ /BBc                 b    U R                   (       d  U R                  5         U R                   S   $ )zGet our VBO idr   )rG   r   r6   s    r   __int__VBO.__int__p  s#    <<##%<<?"r   c                     U R                   (       d  U R                  5         U R                  R                  U R                  U R                   S   5        U R                  5         g)zBind this buffer for use in vertex calls

If we have not yet created our implementation-level VBO, then we
will create it before binding.  Once bound, calls self.copy_data()
r   N)rG   r   r   glBindBufferrd   r   r6   s    r   bindVBO.bindv  sC     <<##%,,T[[$,,q/JNNr   c                 P    U R                   R                  U R                  S5        g)z7Unbind the buffer (make normal array operations active)r   N)r   r   rd   r6   s    r   unbind
VBO.unbind  s    ,,T[[!<r   c                     [        US5      (       a  UR                  n[        U[        5      (       d   S5       e[	        X5      $ )z/Add an integer to this VBO (create a VBOOffset)offsetz)Only know how to add integer/long offsets)hasattrr   rk   r   r]   r*   others     r   __add__VBO.__add__  sI    uh''}  ?>?  T))r   c                 $    U R                  5         g)zContext manager exitF)r   )r*   exc_typeexc_valexc_tbs       r   __exit__VBO.__exit__  s    KKMr   )re   rG   rr   rf   rg   rd   rb   )GL_DYNAMIC_DRAWGL_ARRAY_BUFFERNr   )NNN)rR   rS   rT   rU   rV   rr   
_no_cache_rh   r   propertyr    r   ro   rc   r   r   r   r   r   r   r   r   r   r   	__enter__r   r[   rQ   r   r   r   r      s    	 

 $$0	%d !
		D1	Nf	"	* 		 	#8		#			=	* 		r   r   c                   *    \ rS rSrSrS rS rS rSrg)r]   i  zOffset into a VBO instance

This class is normally instantiated by doing a my_vbo + int operation,
it can be passed to VBO requiring operations and will generate the
appropriate integer offset value to be passed in.
c                     Xl         X l        g)z<Initialize the offset with vbo and offset (unsigned integer)N)vbor   )r*   r   r   s      r   rh   VBOOffset.__init__  s    H Kr   c                 Z    US:w  a  [        U R                  U5      $ [        SU< S35      e)z4Delegate any undefined attribute save vbo to our vbor   zNo z key in VBOOffset)rl   r   r>   r   s     r   r   VBOOffset.__getattr__  s(    e|txx-- S!BCCr   c                     [        US5      (       a  UR                  n[        U R                  U R                  U-   5      $ )zAllow adding integers or other VBOOffset instances

returns a VBOOffset to the this VBO with other.offset + self.offset
or, if other has no offset, returns VBOOffset with self.offset + other
r   )r   r   r]   r   r   s     r   r   VBOOffset.__add__  s4     uh''TXXt{{U':;;r   )r   r   N)	rR   rS   rT   rU   rV   rh   r   r   r[   rQ   r   r   r]   r]     s    		!
	D	<r   r]   c                       \ rS rSrSr\R                  " S5      rS rSS jr	S r
\
rSS jrS	 rS
 rSS jrSS jrSS jrSrg)r   i  zHandles VBO instances passed in as array data

This FormatHandler is registered with PyOpenGL on import of this module
to provide handling of VBO objects as array data-sources
r   c                     g)zeRetrieve data-pointer from the instance's data

Is always NULL, to indicate use of the bound pointer
r   rQ   r*   instances     r   dataPointerVBOHandler.dataPointer  s    
 r   Nc                     U R                   $ )zAlways returns c_void_p(0))vp0r*   r   typeCodes      r   
from_paramVBOHandler.from_param  s    88Or   c                     [        S5      e)zNot implementedz!Don't have VBO output support yet)rw   )r*   dimsr   s      r   zerosVBOHandler.zeros  s    %&MNNr   c                     U$ )z.Given a value, convert to array representationrQ   r*   rn   r   s      r   rx   VBOHandler.asArray  s    Lr   c                 B    [         R                  " UR                  5      $ )z=Given a value, guess OpenGL type of the corresponding pointer)r   arrayToGLTyperf   rm   s     r   r   VBOHandler.arrayToGLType  s     ..uzz::r   c                 B    [         R                  " UR                  5      $ r   )r   rs   rf   rm   s     r   rs   VBOHandler.arrayByteCount  s     //

;;r   c                 B    [         R                  " UR                  5      $ )z6Given a data-value, calculate dimensions for the array)r   	arraySizerf   r   s      r   r   VBOHandler.arraySize  s     **5::66r   c                 B    [         R                  " UR                  5      $ )z-Determine unit size of an array (if possible))r   unitSizerf   r   s      r   r   VBOHandler.unitSize  s     ))%**55r   c                 B    [         R                  " UR                  5      $ )z<Determine dimensions of the passed array value (if possible))r   
dimensionsrf   r   s      r   r   VBOHandler.dimensions  s     ++EJJ77r   rQ   r   )rR   rS   rT   rU   rV   ctypesc_void_pr   r   r   r   onesrx   r   rs   r   r   r   r[   rQ   r   r   r   r     sL    	 ooa 			O 		;	<	7	6	8r   r   c                   (    \ rS rSrSrS rSS jrSrg)r^   i  zHandles VBOOffset instances passed in as array data

Registered on module import to provide support for VBOOffset instances
as sources for array data.
c                     UR                   $ )zIRetrieve data-pointer from the instance's data

returns instance' offset
)r   r   s     r   r   VBOOffsetHandler.dataPointer  s    
 ??"r   Nc                 B    [         R                  " UR                  5      $ )z%Returns a c_void_p( instance.offset ))r   r   r   r   s      r   r   VBOOffsetHandler.from_param  s    ??8??33r   rQ   r   )rR   rS   rT   rU   rV   r   r   r[   rQ   r   r   r^   r^     s    		#	4r   r^   c                    ^  U 4S jnU$ )z=Construct a mapped-array cleaner function to unmap vbo.targetc                    >  [         R                  T5        TR                  R                  TR                  5        g ! [
         a
  n S nAg S nAff = fr   )	_cleanersr<   r   glUnmapBufferrd   	Exception)r   rF   r   s     r   clean_cleaner.<locals>.clean  sC    	9MM# ,,SZZ8  		s   > 
AArQ   )r   r   s   ` r   _cleanerr     s    9 Lr   c                 J   SSK Jn  U R                  R                  U R                  U5      n[
        R                  " U[
        R                  " [
        R                  U R                  -  5      5      nU" US5      n[        R                  " U[        U 5      5      [        U '   U$ )aX  Map the given buffer into a numpy array...

Method taken from:
 http://www.mail-archive.com/numpy-discussion@lists.sourceforge.net/msg01161.html

This should be considered an *experimental* API,
it is not guaranteed to be available in future revisions
of this library!

Simplification to use ctypes cast from comment by 'sashimi' on my blog...
r   )
frombufferB)numpyr   r   glMapBufferrd   r   castPOINTERc_byterg   r   r   r   r   )r   accessr   vpvp_arrayr~   s         r   r   r     st     !				'	'

F	;B {{2v~~fmmchh.FGHHx%E[[6IcNLr   )i  )%rV   OpenGL.arrays.arraydatatyper   OpenGL.arrays.formathandlerr   OpenGL.raw.GLr   OpenGLr   OpenGL._bytesr   r   r	   r   logging	getLogger_logr
   r   r   __all__objectr   r    r\   r   ACCELERATE_AVAILABLEOpenGL_accelerate.vbor]   r   r^   ImportErrorrF   warningr   r   r   rQ   r   r   <module>r     s   B 6 5    2 2 ,- - 
)ZV Zz $66  $
))N	
 	
 ;tf tl<F <:/8] /8b4: 4& 	Y  NLMMNs   >C C%
C  C%