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 / VERSION / GL_2_0.py < prev    next >
Encoding:
Python Source  |  2008-12-07  |  6.0 KB  |  191 lines

  1. '''OpenGL extension VERSION.GL_2_0
  2.  
  3. This module customises the behaviour of the 
  4. OpenGL.raw.GL.VERSION.GL_2_0 to provide a more 
  5. Python-friendly API
  6. '''
  7. from OpenGL import platform, constants, constant, arrays
  8. from OpenGL import extensions, wrapper
  9. from OpenGL.GL import glget
  10. import ctypes
  11. from OpenGL.raw.GL.VERSION.GL_2_0 import *
  12. ### END AUTOGENERATED SECTION
  13. import OpenGL
  14. from OpenGL.raw.GL.ARB.shader_objects import GL_OBJECT_COMPILE_STATUS_ARB as GL_OBJECT_COMPILE_STATUS
  15. from OpenGL.raw.GL.ARB.shader_objects import GL_OBJECT_LINK_STATUS_ARB as GL_OBJECT_LINK_STATUS
  16. from OpenGL.GL.ARB.shader_objects import glGetInfoLogARB as glGetInfoLog
  17. from OpenGL.lazywrapper import lazy
  18.  
  19. from OpenGL import converters, error
  20. GL_INFO_LOG_LENGTH = constant.Constant( 'GL_INFO_LOG_LENGTH', 0x8B84 )
  21.  
  22. glShaderSource = platform.createBaseFunction( 
  23.     'glShaderSource', dll=platform.GL,
  24.     resultType=None, 
  25.     argTypes=(constants.GLhandle, constants.GLsizei, ctypes.POINTER(ctypes.c_char_p), arrays.GLintArray,),
  26.     doc = 'glShaderSource( GLhandle(shaderObj), str( string) ) -> None',
  27.     argNames = ('shaderObj', 'count', 'string', 'length',),
  28. )
  29. conv = converters.StringLengths( name='string' )
  30. glShaderSource = wrapper.wrapper(
  31.     glShaderSource
  32. ).setPyConverter(
  33.     'count' # number of strings
  34. ).setPyConverter( 
  35.     'length' # lengths of strings
  36. ).setPyConverter(
  37.     'string', conv.stringArray
  38. ).setCResolver(
  39.     'string', conv.stringArrayForC,
  40. ).setCConverter(
  41.     'length', conv,
  42. ).setCConverter(
  43.     'count', conv.totalCount,
  44. )
  45. del conv
  46.  
  47. for size in (1,2,3,4):
  48.     for format,arrayType in (
  49.         ('f',arrays.GLfloatArray),
  50.         ('i',arrays.GLintArray),
  51.     ):
  52.         name = 'glUniform%(size)s%(format)sv'%globals()
  53.         globals()[name] = arrays.setInputArraySizeType(
  54.             globals()[name],
  55.             size,
  56.             arrayType, 
  57.             'value',
  58.         )
  59.         del format, arrayType
  60.     del size
  61.  
  62. @lazy( glGetShaderiv )
  63. def glGetShaderiv( baseOperation, shader, pname ):
  64.     """Retrieve the integer parameter for the given shader"""
  65.     status = arrays.GLintArray.zeros( (1,))
  66.     status[0] = 1 
  67.     baseOperation(
  68.         shader, pname, status
  69.     )
  70.     return status[0]
  71. @lazy( glGetProgramiv )
  72. def glGetProgramiv( baseOperation, program, pname, params=None ):
  73.     """Will automatically allocate params if not provided"""
  74.     if params is None:
  75.         params = arrays.GLintArray.zeros( (1,))
  76.         baseOperation( program, pname, params )
  77.         return params[0]
  78.     else:
  79.         baseOperation( program,pname, params )
  80.         return params
  81.  
  82. def _afterCheck( key ):
  83.     """Generate an error-checking function for compilation operations"""
  84.     if key == GL_OBJECT_COMPILE_STATUS:
  85.         getter = glGetShaderiv
  86.     else:
  87.         getter = glGetProgramiv
  88.     def GLSLCheckError( 
  89.         result,
  90.         baseOperation=None,
  91.         cArguments=None,
  92.         *args
  93.     ):
  94.         result = error.glCheckError( result, baseOperation, cArguments, *args )
  95.         status = ctypes.c_int()
  96.         getter( cArguments[0], key, ctypes.byref(status))
  97.         status = status.value
  98.  
  99.  
  100.         if not status:
  101.             raise error.GLError( 
  102.                 result = result,
  103.                 baseOperation = baseOperation,
  104.                 cArguments = cArguments,
  105.                 description= glGetInfoLog( cArguments[0] )
  106.             )
  107.         return result
  108.     return GLSLCheckError
  109.  
  110. if OpenGL.ERROR_CHECKING:
  111.     glCompileShader.errcheck = _afterCheck( GL_OBJECT_COMPILE_STATUS )
  112. if OpenGL.ERROR_CHECKING:
  113.     glLinkProgram.errcheck = _afterCheck( GL_OBJECT_LINK_STATUS )
  114. ## Not sure why, but these give invalid operation :(
  115. ##if glValidateProgram and OpenGL.ERROR_CHECKING:
  116. ##    glValidateProgram.errcheck = _afterCheck( GL_OBJECT_VALIDATE_STATUS )
  117.  
  118. @lazy( glGetShaderInfoLog )
  119. def glGetShaderInfoLog( baseOperation, obj ):
  120.     """Retrieve the shader's error messages as a Python string
  121.     
  122.     returns string which is '' if no message
  123.     """
  124.     length = int(glGetShaderiv(obj, GL_INFO_LOG_LENGTH))
  125.     if length > 0:
  126.         log = ctypes.create_string_buffer(length)
  127.         baseOperation(obj, length, None, log)
  128.         return log.value.strip('\000') # null-termination
  129.     return ''
  130. @lazy( glGetProgramInfoLog )
  131. def glGetProgramInfoLog( baseOperation, obj ):
  132.     """Retrieve the shader program's error messages as a Python string
  133.     
  134.     returns string which is '' if no message
  135.     """
  136.     length = int(glGetProgramiv(obj, GL_INFO_LOG_LENGTH))
  137.     if length > 0:
  138.         log = ctypes.create_string_buffer(length)
  139.         baseOperation(obj, length, None, log)
  140.         return log.value.strip('\000') # null-termination
  141.     return ''
  142.  
  143. @lazy( glGetAttachedShaders )
  144. def glGetAttachedShaders( baseOperation, obj ):
  145.     """Retrieve the attached objects as an array of GLhandle instances"""
  146.     length= glGetProgramiv( obj, GL_ATTACHED_SHADERS )
  147.     if length > 0:
  148.         storage = arrays.GLuintArray.zeros( (length,))
  149.         baseOperation( obj, length, None, storage )
  150.         return storage
  151.     return arrays.GLuintArray.zeros( (0,))
  152.  
  153.  
  154. @lazy( glGetShaderSource )
  155. def glGetShaderSource( baseOperation, obj ):
  156.     """Retrieve the program/shader's source code as a Python string
  157.     
  158.     returns string which is '' if no source code
  159.     """
  160.     length = int(glGetShaderiv(obj, GL_OBJECT_SHADER_SOURCE_LENGTH))
  161.     if length > 0:
  162.         source = ctypes.create_string_buffer(length)
  163.         baseOperation(obj, length, None, source)
  164.         return source.value.strip('\000') # null-termination
  165.     return ''
  166.  
  167. @lazy( glGetActiveUniform )
  168. def glGetActiveUniform(baseOperation,program, index):
  169.     """Retrieve the name, size and type of the uniform of the index in the program"""
  170.     max_index = int(glGetShaderiv( program, GL_OBJECT_ACTIVE_UNIFORMS ))
  171.     length = int(glGetShaderiv( program, GL_OBJECT_ACTIVE_UNIFORM_MAX_LENGTH))
  172.     if index < max_index and index >= 0:
  173.         if length > 0:
  174.             name = ctypes.create_string_buffer(length)
  175.             size = arrays.GLintArray.zeros( (1,))
  176.             gl_type = arrays.GLuintArray.zeros( (1,))
  177.             baseOperation(program, index, length, None, size, gl_type, name)
  178.             return name.value[:int(size[0])], size[0], gl_type[0]
  179.         raise ValueError( """No currently specified uniform names""" )
  180.     raise IndexError, 'Index %s out of range 0 to %i' % (index, max_index - 1, )
  181.  
  182. @lazy( glGetUniformLocation )
  183. def glGetUniformLocation( baseOperation, program, name ):
  184.     """Check that name is a string with a null byte at the end of it"""
  185.     if not name:
  186.         raise ValueError( """Non-null name required""" )
  187.     elif name[-1] != '\000':
  188.         name = name + '\000'
  189.     return baseOperation( program, name )
  190.  
  191.