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 / GLU / glunurbs.py < prev    next >
Encoding:
Python Source  |  2008-12-07  |  7.7 KB  |  244 lines

  1. """Implementation of GLU Nurbs structure and callback methods
  2.  
  3. Same basic pattern as seen with the gluTess* functions, just need to
  4. add some bookkeeping to the structure class so that we can keep the
  5. Python function references alive during the calling process.
  6. """
  7. from OpenGL.raw import GLU as simple
  8. from OpenGL import platform, converters, wrapper
  9. from OpenGL.GLU import glustruct
  10. from OpenGL.lazywrapper import lazy
  11. from OpenGL import arrays
  12. import ctypes
  13. import weakref
  14. from OpenGL.platform import PLATFORM
  15.  
  16. __all__ = (
  17.     'GLUnurbs',
  18.     'gluNewNurbsRenderer',
  19.     'gluNurbsCallback',
  20.     'gluNurbsCallbackData',
  21.     'gluNurbsCallbackDataEXT',
  22.     'gluNurbsCurve',
  23.     'gluNurbsSurface',
  24.     'gluPwlCurve',
  25. )
  26.  
  27. # /usr/include/GL/glu.h 242
  28. class GLUnurbs(glustruct.GLUStruct, simple.GLUnurbs):
  29.     """GLU Nurbs structure with oor and callback storage support
  30.     
  31.     IMPORTANT NOTE: the texture coordinate callback receives a raw ctypes 
  32.     data-pointer, as without knowing what type of evaluation is being done 
  33.     (1D or 2D) we cannot safely determine the size of the array to convert 
  34.     it.  This is a limitation of the C implementation.  To convert to regular 
  35.     data-pointer, just call yourNurb.ptrAsArray( ptr, size, arrays.GLfloatArray )
  36.     with the size of data you expect.
  37.     """
  38.     FUNCTION_TYPE = PLATFORM.functionTypeFor(PLATFORM.GLU)
  39.     CALLBACK_FUNCTION_REGISTRARS = {
  40.         # mapping from "which" to a function that should take 3 parameters,
  41.         # the nurb, the which and the function pointer...
  42.     }
  43.     CALLBACK_TYPES = {
  44.         # mapping from "which" GLU enumeration to a ctypes function type
  45.         simple.GLU_NURBS_BEGIN: FUNCTION_TYPE( 
  46.             None, simple.GLenum 
  47.         ),
  48.         simple.GLU_NURBS_BEGIN_DATA: FUNCTION_TYPE( 
  49.             None, simple.GLenum, ctypes.POINTER(simple.GLvoid) 
  50.         ),
  51.         simple.GLU_NURBS_VERTEX: FUNCTION_TYPE( 
  52.             None, ctypes.POINTER(simple.GLfloat)
  53.         ),
  54.         simple.GLU_NURBS_VERTEX_DATA: FUNCTION_TYPE( 
  55.             None, ctypes.POINTER(simple.GLfloat), ctypes.POINTER(simple.GLvoid) 
  56.         ),
  57.         simple.GLU_NURBS_NORMAL: FUNCTION_TYPE( 
  58.             None, ctypes.POINTER(simple.GLfloat)
  59.         ),
  60.         simple.GLU_NURBS_NORMAL_DATA: FUNCTION_TYPE( 
  61.             None, ctypes.POINTER(simple.GLfloat), ctypes.POINTER(simple.GLvoid) 
  62.         ),
  63.         simple.GLU_NURBS_COLOR: FUNCTION_TYPE( 
  64.             None, ctypes.POINTER(simple.GLfloat)
  65.         ),
  66.         simple.GLU_NURBS_COLOR_DATA: FUNCTION_TYPE( 
  67.             None, ctypes.POINTER(simple.GLfloat), ctypes.POINTER(simple.GLvoid) 
  68.         ),
  69.         simple.GLU_NURBS_TEXTURE_COORD: FUNCTION_TYPE( 
  70.             None, ctypes.POINTER(simple.GLfloat)
  71.         ),
  72.         simple.GLU_NURBS_TEXTURE_COORD_DATA: FUNCTION_TYPE( 
  73.             None, ctypes.POINTER(simple.GLfloat), ctypes.POINTER(simple.GLvoid) 
  74.         ),
  75.         simple.GLU_NURBS_END:FUNCTION_TYPE( 
  76.             None
  77.         ),
  78.         simple.GLU_NURBS_END_DATA: FUNCTION_TYPE( 
  79.             None, ctypes.POINTER(simple.GLvoid) 
  80.         ),
  81.         simple.GLU_NURBS_ERROR:FUNCTION_TYPE( 
  82.             None, simple.GLenum, 
  83.         ),
  84.     }
  85.     WRAPPER_METHODS = {
  86.         simple.GLU_NURBS_BEGIN: None,
  87.         simple.GLU_NURBS_BEGIN_DATA: '_justOOR',
  88.         simple.GLU_NURBS_VERTEX: '_vec3',
  89.         simple.GLU_NURBS_VERTEX_DATA: '_vec3',
  90.         simple.GLU_NURBS_NORMAL: '_vec3',
  91.         simple.GLU_NURBS_NORMAL_DATA: '_vec3',
  92.         simple.GLU_NURBS_COLOR: '_vec4',
  93.         simple.GLU_NURBS_COLOR_DATA: '_vec4',
  94.         simple.GLU_NURBS_TEXTURE_COORD: '_tex',
  95.         simple.GLU_NURBS_TEXTURE_COORD_DATA: '_tex',
  96.         simple.GLU_NURBS_END: None,
  97.         simple.GLU_NURBS_END_DATA: '_justOOR',
  98.         simple.GLU_NURBS_ERROR: None,
  99.     }
  100.     def _justOOR( self, function ):
  101.         """Just do OOR on the last argument..."""
  102.         def getOOR( *args ):
  103.             args = args[:-1] + (self.originalObject(args[-1]),)
  104.             return function( *args )
  105.         return getOOR
  106.     def _vec3( self, function, size=3 ):
  107.         """Convert first arg to size-element array, do OOR on arg2 if present"""
  108.         def vec( *args ):
  109.             vec = self.ptrAsArray(args[0],size,arrays.GLfloatArray)
  110.             if len(args) > 1:
  111.                 oor = self.originalObject(args[1])
  112.                 return function( vec, oor )
  113.             else:
  114.                 return function( vec )
  115.         return vec
  116.     def _vec4( self, function ):
  117.         """Size-4 vector version..."""
  118.         return self._vec3( function, 4 )
  119.     def _tex( self, function ):
  120.         """Texture coordinate callback 
  121.         
  122.         NOTE: there is no way for *us* to tell what size the array is, you will 
  123.         get back a raw data-point, not an array, as you do for all other callback 
  124.         types!!!
  125.         """
  126.         def oor( *args ):
  127.             if len(args) > 1:
  128.                 oor = self.originalObject(args[1])
  129.                 return function( args[0], oor )
  130.             else:
  131.                 return function( args[0] )
  132.         return oor
  133.  
  134. # XXX yes, this is a side-effect...
  135. simple.gluNewNurbsRenderer.restype = ctypes.POINTER( GLUnurbs )
  136.  
  137. def _callbackWithType( funcType ):
  138.     """Get gluNurbsCallback function with set last arg-type"""
  139.     result =  platform.copyBaseFunction(
  140.         simple.gluNurbsCallback
  141.     )
  142.     result.argtypes = [ctypes.POINTER(GLUnurbs), simple.GLenum, funcType]
  143.     assert result.argtypes[-1] == funcType
  144.     return result
  145.  
  146. for (c,funcType) in GLUnurbs.CALLBACK_TYPES.items():
  147.     cb = _callbackWithType( funcType )
  148.     GLUnurbs.CALLBACK_FUNCTION_REGISTRARS[ c ] = cb
  149.     assert funcType == GLUnurbs.CALLBACK_TYPES[c]
  150.     assert cb.argtypes[-1] == funcType
  151. del c,cb, funcType
  152.  
  153. def gluNurbsCallback( nurb, which, CallBackFunc ):
  154.     """Dispatch to the nurb's addCallback operation"""
  155.     return nurb.addCallback( which, CallBackFunc )
  156.  
  157. @lazy( simple.gluNewNurbsRenderer )
  158. def gluNewNurbsRenderer( baseFunction ):
  159.     """Return a new nurbs renderer for the system (dereferences pointer)"""
  160.     newSet = baseFunction()
  161.     new = newSet[0]
  162.     #new.__class__ = GLUnurbs # yes, I know, ick
  163.     return new
  164.  
  165. @lazy( simple.gluNurbsCallbackData )
  166. def gluNurbsCallbackData( baseFunction, nurb, userData ):
  167.     """Note the Python object for use as userData by the nurb"""
  168.     return baseFunction( 
  169.         nurb, nurb.noteObject( userData ) 
  170.     )
  171.  
  172. @lazy( simple.gluNurbsCallbackDataEXT )
  173. def gluNurbsCallbackDataEXT( baseFunction,nurb, userData ):
  174.     """Note the Python object for use as userData by the nurb"""
  175.     return baseFunction( 
  176.         nurb, nurb.noteObject( userData ) 
  177.     )
  178.  
  179. @lazy( simple.gluNurbsCurve )
  180. def gluNurbsCurve( baseFunction, nurb, knots, control, type ):
  181.     """Pythonic version of gluNurbsCurve
  182.     
  183.     Calculates knotCount, stride, and order automatically
  184.     """
  185.     knots = arrays.GLfloatArray.asArray( knots )
  186.     knotCount = arrays.GLfloatArray.arraySize( knots )
  187.     control = arrays.GLfloatArray.asArray( control )
  188.     length,step = arrays.GLfloatArray.dimensions( control )
  189.     order = knotCount - length
  190.     return baseFunction(
  191.         nurb, knotCount, knots, step, control, order, type,
  192.     )
  193.  
  194. @lazy( simple.gluNurbsSurface )
  195. def gluNurbsSurface( baseFunction, nurb, sKnots, tKnots, control, type ):
  196.     """Pythonic version of gluNurbsSurface
  197.     
  198.     Calculates knotCount, stride, and order automatically
  199.     """
  200.     sKnots = arrays.GLfloatArray.asArray( sKnots )
  201.     sKnotCount = arrays.GLfloatArray.arraySize( sKnots )
  202.     tKnots = arrays.GLfloatArray.asArray( tKnots )
  203.     tKnotCount = arrays.GLfloatArray.arraySize( tKnots )
  204.     control = arrays.GLfloatArray.asArray( control )
  205.  
  206.     length,width,step = arrays.GLfloatArray.dimensions( control )
  207.     sOrder = sKnotCount - length 
  208.     tOrder = tKnotCount - width 
  209.     sStride = width*step
  210.     tStride = step
  211.     
  212.     assert (sKnotCount-sOrder)*(tKnotCount-tOrder) == length*width, (
  213.         nurb, sKnotCount, sKnots, tKnotCount, tKnots,
  214.         sStride, tStride, control,
  215.         sOrder,tOrder,
  216.         type
  217.     )
  218.  
  219.     result = baseFunction(
  220.         nurb, sKnotCount, sKnots, tKnotCount, tKnots,
  221.         sStride, tStride, control,
  222.         sOrder,tOrder,
  223.         type
  224.     )
  225.     return result
  226.  
  227. @lazy( simple.gluPwlCurve )
  228. def gluPwlCurve( baseFunction, nurb, data, type ):
  229.     """gluPwlCurve -- piece-wise linear curve within GLU context
  230.     
  231.     data -- the data-array 
  232.     type -- determines number of elements/data-point
  233.     """
  234.     data = arrays.GLfloatArray.asArray( data )
  235.     if type == simple.GLU_MAP1_TRIM_2:
  236.         divisor = 2
  237.     elif type == simple.GLU_MAP_TRIM_3:
  238.         divisor = 3
  239.     else:
  240.         raise ValueError( """Unrecognised type constant: %s"""%(type))
  241.     size = arrays.GLfloatArray.arraySize( data )
  242.     size = int(size/divisor)
  243.     return baseFunction( nurb, size, data, divisor, type )
  244.