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 / GL / exceptional.py < prev    next >
Encoding:
Python Source  |  2008-12-07  |  9.6 KB  |  387 lines

  1. """Exceptional cases that need some extra wrapping"""
  2. from OpenGL.platform import GL,GLU,createBaseFunction
  3. from OpenGL import arrays, error, wrapper
  4. from OpenGL.arrays.arraydatatype import GLfloatArray, GLdoubleArray
  5. from OpenGL import constants as data_types
  6. from OpenGL.lazywrapper import lazy
  7. from OpenGL.raw import GL as simple
  8. from OpenGL.raw.GL import constants
  9. from OpenGL.raw.GL import annotations
  10. import OpenGL
  11. import ctypes
  12.  
  13. __all__ = [
  14.     'glBegin', 
  15.     'glCallLists', 
  16.     'glColor', 
  17.     #'glColorTableParameterfv', 
  18.     'glDeleteTextures', 
  19.     'glEdgeFlagv', 
  20.     'glEnd', 
  21.     'glGenTextures', 
  22.     'glIndexdv', 
  23.     'glIndexfv', 
  24.     'glIndexsv',
  25.     'glIndexubv', 
  26.     'glMap1d', 
  27.     'glMap1f',
  28.     'glMap2d',
  29.     'glMap2f',
  30.     'glMaterial', 
  31.     'glRasterPos', 
  32.     'glRectfv', 
  33.     'glRectiv', 
  34.     'glRectsv', 
  35.     'glTexGenfv', 
  36.     'glTexParameter', 
  37.     'glVertex', 
  38.     'glAreTexturesResident',
  39. ]
  40.  
  41. glRasterPosDispatch = {
  42.     2: simple.glRasterPos2dv,
  43.     3: simple.glRasterPos3dv,
  44.     4: simple.glRasterPos4dv,
  45. }
  46.  
  47. if OpenGL.ERROR_CHECKING:
  48.     @lazy( simple.glBegin )
  49.     def glBegin( baseFunction, mode ):
  50.         """Begin GL geometry-definition mode, disable automatic error checking"""
  51.         error.onBegin( )
  52.         return baseFunction( mode )
  53.     @lazy( simple.glEnd )
  54.     def glEnd( baseFunction ):
  55.         """Finish GL geometry-definition mode, re-enable automatic error checking"""
  56.         error.onEnd( )
  57.         return baseFunction( )
  58. else:
  59.     glBegin = simple.glBegin
  60.     glEnd = simple.glEnd
  61.  
  62. @lazy( simple.glDeleteTextures )
  63. def glDeleteTextures( baseFunction, array ):
  64.     """Delete specified set of textures"""
  65.     ptr = arrays.GLuintArray.asArray( array )
  66.     size = arrays.GLuintArray.arraySize( ptr )
  67.     return baseFunction( size, ptr )
  68.  
  69.  
  70. def glMap2( baseFunction, arrayType ):
  71.     def glMap2( target, u1, u2, v1, v2, points):
  72.         """glMap2(target, u1, u2, v1, v2, points[][][]) -> None
  73.         
  74.         This is a completely non-standard signature which doesn't allow for most 
  75.         of the funky uses with strides and the like, but it has been like this for
  76.         a very long time...
  77.         """
  78.         ptr = arrayType.asArray( points )
  79.         uorder,vorder,vstride = arrayType.dimensions( ptr )
  80.         ustride = vstride*vorder
  81.         return baseFunction(
  82.             target, 
  83.             u1, u2, 
  84.             ustride, uorder, 
  85.             v1, v2, 
  86.             vstride, vorder, 
  87.             ptr
  88.         )
  89.     glMap2.__name__ = baseFunction.__name__
  90.     glMap2.baseFunction = baseFunction
  91.     return glMap2
  92. glMap2d = glMap2( simple.glMap2d, arrays.GLdoubleArray )
  93. glMap2f = glMap2( simple.glMap2f, arrays.GLfloatArray )
  94. del glMap2
  95.  
  96. def glMap1( baseFunction, arrayType ):
  97.     def glMap1(target,u1,u2,points):
  98.         """glMap1(target, u1, u2, points[][][]) -> None
  99.         
  100.         This is a completely non-standard signature which doesn't allow for most 
  101.         of the funky uses with strides and the like, but it has been like this for
  102.         a very long time...
  103.         """
  104.         ptr = arrayType.asArray( points )
  105.         dims = arrayType.dimensions( ptr )
  106.         uorder = dims[0]
  107.         ustride = dims[1]
  108.         return baseFunction( target, u1,u2,ustride,uorder, ptr )
  109.     glMap1.__name__ == baseFunction.__name__
  110.     glMap1.baseFunction = baseFunction
  111.     return glMap1
  112. glMap1d = glMap1( simple.glMap1d, arrays.GLdoubleArray )
  113. glMap1f = glMap1( simple.glMap1f, arrays.GLfloatArray )
  114. del glMap1
  115.  
  116. def glRasterPos( *args ):
  117.     """Choose glRasterPosX based on number of args"""
  118.     if len(args) == 1:
  119.         # v form...
  120.         args = args[0]
  121.     value = GLdoubleArray.asArray( args, constants.GL_DOUBLE )
  122.     return glRasterPosDispatch[ len(args) ]( value )
  123.  
  124. glVertexDispatch = {
  125.     2: simple.glVertex2dv,
  126.     3: simple.glVertex3dv,
  127.     4: simple.glVertex4dv,
  128. }
  129. def glVertex( *args ):
  130.     """Choose glVertexX based on number of args"""
  131.     if len(args) == 1:
  132.         # v form...
  133.         args = args[0]
  134.     value = GLdoubleArray.asArray( args, constants.GL_DOUBLE )
  135.     return glVertexDispatch[ len(args) ]( value )
  136.  
  137. @lazy( simple.glCallLists )
  138. def glCallLists( baseFunction, lists ):
  139.     """glCallLists( str( lists ) or lists[] ) -> None 
  140.     
  141.     Restricted version of glCallLists, takes a string or a GLuint compatible
  142.     array data-type and passes into the base function.
  143.     """
  144.     if isinstance( lists, str ):
  145.         return baseFunction(
  146.             len(lists),
  147.             constants.GL_UNSIGNED_BYTE,
  148.             ctypes.c_void_p(arrays.GLubyteArray.dataPointer( lists )),
  149.         )
  150.     ptr = arrays.GLuintArray.asArray( lists )
  151.     size = arrays.GLuintArray.arraySize( ptr )
  152.     return baseFunction( 
  153.         size, 
  154.         constants.GL_UNSIGNED_INT, 
  155.         ctypes.c_void_p( arrays.GLuintArray.dataPointer(ptr))
  156.     )
  157.  
  158. def glTexParameter( target, pname, parameter ):
  159.     """Set a texture parameter, choose underlying call based on pname and parameter"""
  160.     if isinstance( parameter, float ):
  161.         return simple.glTexParameterf( target, pname, parameter )
  162.     elif isinstance( parameter, int ):
  163.         return simple.glTexParameteri( target, pname, parameter )
  164.     else:
  165.         value = GLfloatArray.asArray( parameter, constants.GL_FLOAT )
  166.         return simple.glTexParameterfv( target, pname, value )
  167.  
  168. @lazy( simple.glGenTextures )
  169. def glGenTextures( baseFunction, count, textures=None ):
  170.     """Generate count new texture names
  171.     
  172.     Note: for compatibility with PyOpenGL 2.x and below,
  173.     a count of 1 will return a single integer, rather than 
  174.     an array of integers.
  175.     """
  176.     if count <= 0:
  177.         raise ValueError( """Can't generate 0 or fewer textures""" )
  178.     elif count == 1:
  179.         # this traditionally returned a single int/long, so we'll continue to
  180.         # do so, even though it would be easier not to bother.
  181.         textures = simple.GLuint( 0 )
  182.         baseFunction( count, textures)
  183.         return textures.value
  184.     else:
  185.         textures = arrays.GLuintArray.zeros( (count,))
  186.         baseFunction( count, textures)
  187.         return textures
  188.  
  189. def glMaterial( faces, constant, *args ):
  190.     """glMaterial -- convenience function to dispatch on argument type
  191.     
  192.     If passed a single argument in args, calls:
  193.         glMaterialfv( faces, constant, args[0] )
  194.     else calls:
  195.         glMaterialf( faces, constant, *args )
  196.     """
  197.     if len(args) == 1:
  198.         arg = GLfloatArray.asArray( args[0] )
  199.         if arg is None:
  200.             raise ValueError( """Null value in glMaterial: %s"""%(args,) )
  201.         return simple.glMaterialfv( faces, constant, arg )
  202.     else:
  203.         return simple.glMaterialf( faces, constant, *args )
  204.  
  205. glColorDispatch = {
  206.     3: annotations.glColor3dv,
  207.     4: annotations.glColor4dv,
  208. }
  209.  
  210. def glColor( *args ):
  211.     """glColor*f* -- convenience function to dispatch on argument type
  212.  
  213.     dispatches to glColor3f, glColor2f, glColor4f, glColor3f, glColor2f, glColor4f
  214.     depending on the arguments passed...
  215.     """
  216.     arglen = len(args)
  217.     if arglen == 1:
  218.         arg = arrays.GLfloatArray.asArray( args[0] )
  219.         function = glColorDispatch[arrays.GLfloatArray.arraySize( arg )]
  220.         return function( arg )
  221.     elif arglen == 2:
  222.         return simple.glColor2d( *args )
  223.     elif arglen == 3:
  224.         return simple.glColor3d( *args )
  225.     elif arglen == 4:
  226.         return simple.glColor4d( *args )
  227.     else:
  228.         raise ValueError( """Don't know how to handle arguments: %s"""%(args,))
  229.  
  230.  
  231. # Rectagle coordinates,
  232. glRectfv = arrays.setInputArraySizeType(
  233.         arrays.setInputArraySizeType(
  234.         simple.glRectfv,
  235.         2,
  236.         arrays.GLfloatArray,
  237.         'v1',
  238.     ),
  239.     2,
  240.     arrays.GLfloatArray,
  241.     'v2',
  242. )
  243. glRectiv = arrays.setInputArraySizeType(
  244.     arrays.setInputArraySizeType(
  245.         simple.glRectiv,
  246.         2,
  247.         arrays.GLintArray,
  248.         'v1',
  249.     ),
  250.     2,
  251.     arrays.GLintArray,
  252.     'v2',
  253. )
  254. glRectsv = arrays.setInputArraySizeType(
  255.     arrays.setInputArraySizeType(
  256.         simple.glRectsv,
  257.         2,
  258.         arrays.GLshortArray,
  259.         'v1',
  260.     ),
  261.     2,
  262.     arrays.GLshortArray,
  263.     'v2',
  264. )
  265.  
  266.  
  267. glIndexsv = arrays.setInputArraySizeType(
  268.     simple.glIndexsv,
  269.     1,
  270.     arrays.GLshortArray,
  271.     'c',
  272. )
  273. glIndexdv = arrays.setInputArraySizeType(
  274.     simple.glIndexdv,
  275.     1,
  276.     arrays.GLdoubleArray,
  277.     'c',
  278. )
  279. glIndexfv = arrays.setInputArraySizeType(
  280.     simple.glIndexfv,
  281.     1,
  282.     arrays.GLfloatArray,
  283.     'c',
  284. )
  285. glIndexubv = arrays.setInputArraySizeType(
  286.     simple.glIndexubv,
  287.     1,
  288.     arrays.GLbyteArray,
  289.     'c',
  290. )
  291. glEdgeFlagv = arrays.setInputArraySizeType(
  292.     simple.glEdgeFlagv,
  293.     1,
  294.     arrays.GLubyteArray,
  295.     'flag',
  296. )
  297. glTexGenfv = arrays.setInputArraySizeType(
  298.     simple.glTexGenfv,
  299.     None,
  300.     arrays.GLfloatArray,
  301.     'params',
  302. )
  303.  
  304. #'glAreTexturesResident',
  305. @lazy( simple.glAreTexturesResident )
  306. def glAreTexturesResident( baseFunction, *args ):
  307.     """Allow both Pythonic and C-style calls to glAreTexturesResident
  308.     
  309.         glAreTexturesResident( arrays.GLuintArray( textures) )
  310.     
  311.     or 
  312.     
  313.         glAreTexturesResident( int(n), arrays.GLuintArray( textures), arrays.GLuboolean( output) )
  314.     
  315.     or 
  316.     
  317.         glAreTexturesResident( int(n), arrays.GLuintArray( textures) )
  318.         
  319.     returns the output arrays.GLubooleanArray
  320.     """
  321.     if len(args) == 1:
  322.         # Pythonic form...
  323.         textures = args[0]
  324.         textures = arrays.GLuintArray.asArray( textures )
  325.         n = arrays.GLuintArray.arraySize(textures)
  326.         output = arrays.GLbooleanArray.zeros( (n,))
  327.     elif len(args) == 2:
  328.         try:
  329.             n = int( args[0] )
  330.         except TypeError, err:
  331.             textures = args[0]
  332.             textures = arrays.GLuintArray.asArray( textures )
  333.  
  334.             n = arrays.GLuintArray.arraySize(textures)
  335.             output = args[1]
  336.             output = arrays.GLbooleanArray.asArray( output )
  337.         else:
  338.             textures = args[1]
  339.             textures = arrays.GLuintArray.asArray( textures )
  340.  
  341.             output = arrays.GLbooleanArray.zeros( (n,))
  342.     elif len(args) == 3:
  343.         n,textures,output = args 
  344.         textures = arrays.GLuintArray.asArray( textures )
  345.         output = arrays.GLbooleanArray.asArray( output )
  346.     else:
  347.         raise TypeError( """Expected 1 to 3 arguments to glAreTexturesResident""" )
  348.     texturePtr = arrays.GLuintArray.typedPointer( textures )
  349.     outputPtr = arrays.GLbooleanArray.typedPointer( output )
  350.     result = baseFunction( n, texturePtr, outputPtr )
  351.     if result:
  352.         # weirdness of the C api, doesn't produce values if all are true
  353.         for i in range(len(output)):
  354.             output[i] = 1
  355.     return output
  356.  
  357. #glMap2f
  358. #glMap2d
  359. #glMap1f
  360. #glMap1d
  361. #glPixelMapusv
  362. #glTexGenfv
  363. #glLightfv
  364. #glFeedbackBuffer
  365. #glDrawRangeElements
  366. #glSelectBuffer
  367. #glAreTexturesResident
  368. #glPixelMapfv
  369. #glTexGeniv
  370. #glClipPlane
  371. #glTexParameterfv
  372. #glTexParameteriv
  373. #glReadPixels
  374. #glConvolutionParameterfv
  375. #glPolygonStipple
  376. #glFogiv
  377. #glTexEnviv
  378. #glRectdv
  379. #glMaterialiv
  380. #glColorTable
  381. #glColorTableParameteriv
  382. #glIndexiv
  383. #glLightModeliv
  384. #glDrawElements
  385. #glConvolutionFilter1D
  386. #glCallLists
  387.