home *** CD-ROM | disk | FTP | other *** search
/ Hackers Magazine 57 / CdHackersMagazineNr57.iso / Software / Multimedia / k3d-setup-0.7.11.0.exe / lib / site-packages / OpenGL / arrays / numbers.py < prev    next >
Encoding:
Python Source  |  2008-12-20  |  3.0 KB  |  92 lines

  1. """Numbers passed as array handling code for PyOpenGL
  2. """
  3. REGISTRY_NAME = 'numbers'
  4. from OpenGL import constants
  5. from OpenGL.arrays import formathandler
  6. import ctypes
  7.  
  8. class NumberHandler( formathandler.FormatHandler ):
  9.     """Allows the user to pass a bald Python float,int, etceteras as an array-of-1"""
  10.     HANDLED_TYPES = (
  11.         int,long,float,
  12.         constants.GLdouble,
  13.         constants.GLfloat,
  14.         constants.GLint,
  15.         constants.GLshort,
  16.         constants.GLuint,
  17.         constants.GLushort, 
  18.         constants.GLclampf,
  19.         constants.GLclampd,
  20.     )
  21.     def from_param( self, value, typeCode=None  ):
  22.         """If it's a ctypes value, pass on, otherwise do asArray"""
  23.         if not value.__class__ in TARGET_TYPES:
  24.             return self.asArray( value, typeCode=typeCode )
  25.         return ctypes.byref(value)
  26.     dataPointer = from_param
  27.     def zeros( self, dims, typeCode=None ):
  28.         """Currently don't allow Number as output types!"""
  29.         raise NotImplemented( """Number data-type not allowed as an output array format""" )
  30.     def ones( self, dims, typeCode=None ):
  31.         """Currently don't allow Number as output types!"""
  32.         raise NotImplemented( """Number data-type not allowed as an output array format""" )
  33.     def arrayToGLType( self, value ):
  34.         """Given a value, guess OpenGL type of the corresponding pointer"""
  35.         if value.__class__ in TARGET_TYPES:
  36.             return TARGET_TYPES[ value.__class__ ]
  37.         else:
  38.             guess = DEFAULT_TYPES.get( value.__class__ )
  39.             if guess is not None:
  40.                 return guess[1]
  41.             raise TypeError( """Can't guess array data-type for %r types"""%(type(value)))
  42.     def arraySize( self, value, typeCode = None ):
  43.         """Given a data-value, calculate ravelled size for the array"""
  44.         return 1
  45.     def asArray( self, value, typeCode=None ):
  46.         """Convert given value to an array value of given typeCode"""
  47.         if value.__class__ in TARGET_TYPES:
  48.             return value
  49.         targetType = CONSTANT_TO_TYPE.get( typeCode )
  50.         if targetType is not None:
  51.             return targetType( value )
  52.         raise TypeError( """Don't know how to convert %r to an array type"""%(
  53.             typeCode,
  54.         ))
  55.     def unitSize( self, value, typeCode=None ):
  56.         """Determine unit size of an array (if possible)"""
  57.         return 1 # there's only 1 possible value in the set...
  58.     def registerEquivalent( self, typ, base ):
  59.         """Register a sub-class for handling as the base-type"""
  60.         global TARGET_TYPE_TUPLE
  61.         for source in (DEFAULT_TYPES, TARGET_TYPES, BYTE_SIZES):
  62.             if source.has_key( base ):
  63.                 source[typ] = source[base]
  64.         if TARGET_TYPES.has_key( base ):
  65.             TARGET_TYPE_TUPLE = TARGET_TYPE_TUPLE + (base,)
  66.  
  67. DEFAULT_TYPES = {
  68.     float: (constants.GLdouble,constants.GL_DOUBLE),
  69.     int: (constants.GLint,constants.GL_INT),
  70.     long: (constants.GLint,constants.GL_INT),
  71. }
  72. TARGET_TYPES = dict([
  73.     (getattr( constants,n),c)
  74.     for (n,c) in constants.ARRAY_TYPE_TO_CONSTANT
  75. ])
  76. TARGET_TYPE_TUPLE = tuple([
  77.     getattr(constants,n) 
  78.     for (n,c) in constants.ARRAY_TYPE_TO_CONSTANT
  79. ])
  80. CONSTANT_TO_TYPE = dict([
  81.     (c,getattr( constants, n))
  82.     for (n,c) in constants.ARRAY_TYPE_TO_CONSTANT
  83. ])
  84.  
  85. BYTE_SIZES = dict([
  86.     ( c, ctypes.sizeof( getattr( constants, n) ) )
  87.     for (n,c) in constants.ARRAY_TYPE_TO_CONSTANT
  88. ])
  89.  
  90. del n,c
  91.  
  92.