<s> '''<STR_LIT>''' <EOL> __docformat__ = '<STR_LIT>' <EOL> __version__ = '<STR_LIT>' <EOL> import ctypes <EOL> import re <EOL> from pyglet . gl import * <EOL> from pyglet . graphics import vertexbuffer <EOL> _c_types = { <EOL> GL_BYTE : ctypes . c_byte , <EOL> GL_UNSIGNED_BYTE : ctypes . c_ubyte , <EOL> GL_SHORT : ctypes . c_short , <EOL> GL_UNSIGNED_SHORT : ctypes . c_ushort , <EOL> GL_INT : ctypes . c_int , <EOL> GL_UNSIGNED_INT : ctypes . c_uint , <EOL> GL_FLOAT : ctypes . c_float , <EOL> GL_DOUBLE : ctypes . c_double , <EOL> } <EOL> _gl_types = { <EOL> '<STR_LIT:b>' : GL_BYTE , <EOL> '<STR_LIT:B>' : GL_UNSIGNED_BYTE , <EOL> '<STR_LIT:s>' : GL_SHORT , <EOL> '<STR_LIT:S>' : GL_UNSIGNED_SHORT , <EOL> '<STR_LIT:i>' : GL_INT , <EOL> '<STR_LIT:I>' : GL_UNSIGNED_INT , <EOL> '<STR_LIT:f>' : GL_FLOAT , <EOL> '<STR_LIT:d>' : GL_DOUBLE , <EOL> } <EOL> _attribute_format_re = re . compile ( r'''<STR_LIT>''' , re . VERBOSE ) <EOL> _attribute_cache = { } <EOL> def _align ( v , align ) : <EOL> return ( ( v - <NUM_LIT:1> ) & ~ ( align - <NUM_LIT:1> ) ) + align <EOL> def interleave_attributes ( attributes ) : <EOL> '''<STR_LIT>''' <EOL> stride = <NUM_LIT:0> <EOL> max_size = <NUM_LIT:0> <EOL> for attribute in attributes : <EOL> stride = _align ( stride , attribute . align ) <EOL> attribute . offset = stride <EOL> stride += attribute . size <EOL> max_size = max ( max_size , attribute . size ) <EOL> stride = _align ( stride , max_size ) <EOL> for attribute in attributes : <EOL> attribute . stride = stride <EOL> def serialize_attributes ( count , attributes ) : <EOL> '''<STR_LIT>''' <EOL> offset = <NUM_LIT:0> <EOL> for attribute in attributes : <EOL> offset = _align ( offset , attribute . align ) <EOL> attribute . offset = offset <EOL> offset += count * attribute . stride <EOL> def create_attribute ( format ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> cls , args = _attribute_cache [ format ] <EOL> return cls ( * args ) <EOL> except KeyError : <EOL> pass <EOL> match = _attribute_format_re . match ( format ) <EOL> assert match , '<STR_LIT>' % format <EOL> count = int ( match . group ( '<STR_LIT:count>' ) ) <EOL> gl_type = _gl_types [ match . group ( '<STR_LIT:type>' ) ] <EOL> generic_index = match . group ( '<STR_LIT>' ) <EOL> texcoord_texture = match . group ( '<STR_LIT>' ) <EOL> if generic_index : <EOL> normalized = match . group ( '<STR_LIT>' ) <EOL> attr_class = GenericAttribute <EOL> args = int ( generic_index ) , normalized , count , gl_type <EOL> elif texcoord_texture : <EOL> attr_class = MultiTexCoordAttribute <EOL> args = int ( texcoord_texture ) , count , gl_type <EOL> else : <EOL> name = match . group ( '<STR_LIT:name>' ) <EOL> attr_class = _attribute_classes [ name ] <EOL> if attr_class . _fixed_count : <EOL> assert count == attr_class . _fixed_count , '<STR_LIT>' % ( <EOL> name , attr_class . _fixed_count ) <EOL> args = ( gl_type , ) <EOL> else : <EOL> args = ( count , gl_type ) <EOL> _attribute_cache [ format ] = attr_class , args <EOL> return attr_class ( * args ) <EOL> class AbstractAttribute ( object ) : <EOL> '''<STR_LIT>''' <EOL> _fixed_count = None <EOL> def __init__ ( self , count , gl_type ) : <EOL> '''<STR_LIT>''' <EOL> assert count in ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ) , '<STR_LIT>' <EOL> self . gl_type = gl_type <EOL> self . c_type = _c_types [ gl_type ] <EOL> self . count = count <EOL> self . align = ctypes . sizeof ( self . c_type ) <EOL> self . size = count * self . align <EOL> self . stride = self . size <EOL> self . offset = <NUM_LIT:0> <EOL> def enable ( self ) : <EOL> '''<STR_LIT>''' <EOL> raise NotImplementedError ( '<STR_LIT>' ) <EOL> def set_pointer ( self , offset ) : <EOL> '''<STR_LIT>''' <EOL> raise NotImplementedError ( '<STR_LIT>' ) <EOL> def get_region ( self , buffer , start , count ) : <EOL> '''<STR_LIT>''' <EOL> byte_start = self . stride * start <EOL> byte_size = self . stride * count <EOL> array_count = self . count * count <EOL> if self . stride == self . size or not array_count : <EOL> ptr_type = ctypes . POINTER ( self . c_type * array_count ) <EOL> return buffer . get_region ( byte_start , byte_size , ptr_type ) <EOL> else : <EOL> byte_start += self . offset <EOL> byte_size -= self . offset <EOL> elem_stride = self . stride // ctypes . sizeof ( self . c_type ) <EOL> elem_offset = self . offset // ctypes . sizeof ( self . c_type ) <EOL> ptr_type = ctypes . POINTER ( <EOL> self . c_type * ( count * elem_stride - elem_offset ) ) <EOL> region = buffer . get_region ( byte_start , byte_size , ptr_type ) <EOL> return vertexbuffer . IndirectArrayRegion ( <EOL> region , array_count , self . count , elem_stride ) <EOL> def set_region ( self , buffer , start , count , data ) : <EOL> '''<STR_LIT>''' <EOL> if self . stride == self . size : <EOL> byte_start = self . stride * start <EOL> byte_size = self . stride * count <EOL> array_count = self . count * count <EOL> data = ( self . c_type * array_count ) ( * data ) <EOL> buffer . set_data_region ( data , byte_start , byte_size ) <EOL> else : <EOL> region = self . get_region ( buffer , start , count ) <EOL> region [ : ] = data <EOL> class ColorAttribute ( AbstractAttribute ) : <EOL> '''<STR_LIT>''' <EOL> plural = '<STR_LIT>' <EOL> def __init__ ( self , count , gl_type ) : <EOL> assert count in ( <NUM_LIT:3> , <NUM_LIT:4> ) , '<STR_LIT>' <EOL> super ( ColorAttribute , self ) . __init__ ( count , gl_type ) <EOL> def enable ( self ) : <EOL> glEnableClientState ( GL_COLOR_ARRAY ) <EOL> def set_pointer ( self , pointer ) : <EOL> glColorPointer ( self . count , self . gl_type , self . stride , <EOL> self . offset + pointer ) <EOL> class EdgeFlagAttribute ( AbstractAttribute ) : <EOL> '''<STR_LIT>''' <EOL> plural = '<STR_LIT>' <EOL> _fixed_count = <NUM_LIT:1> <EOL> def __init__ ( self , gl_type ) : <EOL> assert gl_type in ( GL_BYTE , GL_UNSIGNED_BYTE , GL_BOOL ) , '<STR_LIT>' <EOL> super ( EdgeFlagAttribute , self ) . __init__ ( <NUM_LIT:1> , gl_type ) <EOL> def enable ( self ) : <EOL> glEnableClientState ( GL_EDGE_FLAG_ARRAY ) <EOL> def set_pointer ( self , pointer ) : <EOL> glEdgeFlagPointer ( self . stride , self . offset + pointer ) <EOL> class FogCoordAttribute ( AbstractAttribute ) : <EOL> '''<STR_LIT>''' <EOL> plural = '<STR_LIT>' <EOL> def __init__ ( self , count , gl_type ) : <EOL> super ( FogCoordAttribute , self ) . __init__ ( count , gl_type ) <EOL> def enable ( self ) : <EOL> glEnableClientState ( GL_FOG_COORD_ARRAY ) <EOL> def set_pointer ( self , pointer ) : <EOL> glFogCoordPointer ( self . count , self . gl_type , self . stride , <EOL> self . offset + pointer ) <EOL> class NormalAttribute ( AbstractAttribute ) : <EOL> '''<STR_LIT>''' <EOL> plural = '<STR_LIT>' <EOL> _fixed_count = <NUM_LIT:3> <EOL> def __init__ ( self , gl_type ) : <EOL> assert gl_type in ( GL_BYTE , GL_SHORT , GL_INT , GL_FLOAT , GL_DOUBLE ) , '<STR_LIT>' <EOL> super ( NormalAttribute , self ) . __init__ ( <NUM_LIT:3> , gl_type ) <EOL> def enable ( self ) : <EOL> glEnableClientState ( GL_NORMAL_ARRAY ) <EOL> def set_pointer ( self , pointer ) : <EOL> glNormalPointer ( self . gl_type , self . stride , self . offset + pointer ) <EOL> class SecondaryColorAttribute ( AbstractAttribute ) : <EOL> '''<STR_LIT>''' <EOL> plural = '<STR_LIT>' <EOL> _fixed_count = <NUM_LIT:3> <EOL> def __init__ ( self , gl_type ) : <EOL> super ( SecondaryColorAttribute , self ) . __init__ ( <NUM_LIT:3> , gl_type ) <EOL> def enable ( self ) : <EOL> glEnableClientState ( GL_SECONDARY_COLOR_ARRAY ) <EOL> def set_pointer ( self , pointer ) : <EOL> glSecondaryColorPointer ( <NUM_LIT:3> , self . gl_type , self . stride , <EOL> self . offset + pointer ) <EOL> class TexCoordAttribute ( AbstractAttribute ) : <EOL> '''<STR_LIT>''' <EOL> plural = '<STR_LIT>' <EOL> def __init__ ( self , count , gl_type ) : <EOL> assert gl_type in ( GL_SHORT , GL_INT , GL_INT , GL_FLOAT , GL_DOUBLE ) , '<STR_LIT>' <EOL> super ( TexCoordAttribute , self ) . __init__ ( count , gl_type ) <EOL> def enable ( self ) : <EOL> glEnableClientState ( GL_TEXTURE_COORD_ARRAY ) <EOL> def set_pointer ( self , pointer ) : <EOL> glTexCoordPointer ( self . count , self . gl_type , self . stride , <EOL> self . offset + pointer ) <EOL> def convert_to_multi_tex_coord_attribute ( self ) : <EOL> '''<STR_LIT>''' <EOL> self . __class__ = MultiTexCoordAttribute <EOL> self . texture = <NUM_LIT:0> <EOL> class MultiTexCoordAttribute ( AbstractAttribute ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , texture , count , gl_type ) : <EOL> assert gl_type in ( GL_SHORT , GL_INT , GL_INT , GL_FLOAT , GL_DOUBLE ) , '<STR_LIT>' <EOL> self . texture = texture <EOL> super ( MultiTexCoordAttribute , self ) . __init__ ( count , gl_type ) <EOL> def enable ( self ) : <EOL> glClientActiveTexture ( GL_TEXTURE0 + self . texture ) <EOL> glEnableClientState ( GL_TEXTURE_COORD_ARRAY ) <EOL> def set_pointer ( self , pointer ) : <EOL> glTexCoordPointer ( self . count , self . gl_type , self . stride , <EOL> self . offset + pointer ) <EOL> class VertexAttribute ( AbstractAttribute ) : <EOL> '''<STR_LIT>''' <EOL> plural = '<STR_LIT>' <EOL> def __init__ ( self , count , gl_type ) : <EOL> assert count > <NUM_LIT:1> , '<STR_LIT>' <EOL> assert gl_type in ( GL_SHORT , GL_INT , GL_INT , GL_FLOAT , GL_DOUBLE ) , '<STR_LIT>' <EOL> super ( VertexAttribute , self ) . __init__ ( count , gl_type ) <EOL> def enable ( self ) : <EOL> glEnableClientState ( GL_VERTEX_ARRAY ) <EOL> def set_pointer ( self , pointer ) : <EOL> glVertexPointer ( self . count , self . gl_type , self . stride , <EOL> self . offset + pointer ) <EOL> class GenericAttribute ( AbstractAttribute ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , index , normalized , count , gl_type ) : <EOL> self . normalized = bool ( normalized ) <EOL> self . index = index <EOL> super ( GenericAttribute , self ) . __init__ ( count , gl_type ) <EOL> def enable ( self ) : <EOL> glEnableVertexAttribArray ( self . index ) <EOL> def set_pointer ( self , pointer ) : <EOL> glVertexAttribPointer ( self . index , self . count , self . gl_type , <EOL> self . normalized , self . stride , <EOL> self . offset + pointer ) <EOL> _attribute_classes = { <EOL> '<STR_LIT:c>' : ColorAttribute , <EOL> '<STR_LIT:e>' : EdgeFlagAttribute , <EOL> '<STR_LIT:f>' : FogCoordAttribute , <EOL> '<STR_LIT:n>' : NormalAttribute , <EOL> '<STR_LIT:s>' : SecondaryColorAttribute , <EOL> '<STR_LIT:t>' : TexCoordAttribute , <EOL> '<STR_LIT:v>' : VertexAttribute , <EOL> } </s>
