def OnInit(self): """Do our testing here...""" weird_value = -701496917 data = arrays.zeros((256, ), 'i') for name in dir(GL): value = getattr(GL, name) if isinstance(value, constant.Constant): if value in glget.GL_GET_SIZES: continue data[:] = weird_value try: GL.glGetIntegerv(value, data) except error.GLError, err: if err.err == 1280: print '# No: %s' % (value.name, ) else: print 'simple.%s: (1,), # TODO: Check size!' % ( value.name, ) else: ordinality = 256 - arrays.sum((data == weird_value)) if ordinality == 16: ordinality = (4, 4) else: ordinality = (ordinality, ) print 'simple.%s: %s,' % (value.name, ordinality)
def glReadPixels( x,y,width,height,format,type, outputType=str ): """Read specified pixels from the current display buffer x,y,width,height -- location and dimensions of the image to read from the buffer format -- pixel format for the resulting data type -- data-format for the resulting data outputType -- default (str) provides string output of the results iff OpenGL.UNSIGNED_BYTE_IMAGES_AS_STRING is True and type == GL_UNSIGNED_BYTE. Any other value will cause output in the default array output format. returns the pixel data array in the format defined by the format, type and outputType """ x,y,width,height = asInt(x),asInt(y),asInt(width),asInt(height) array = images.SetupPixelRead( format, (width,height), type ) arrayType = arrays.GL_CONSTANT_TO_ARRAY_TYPE[ images.TYPE_TO_ARRAYTYPE.get(type,type) ] imageData = arrayType.dataPointer(array) simple.glReadPixels( x,y,width,height, format,type, ctypes.c_void_p( imageData ) ) if outputType is str: return images.returnFormat( array, type ) else: return array
def OnInit( self ): """Do our testing here...""" weird_value = -701496917 data = arrays.zeros( (256,),'i') for name in dir( GL ): value = getattr( GL, name ) if isinstance( value, constant.Constant ): if value in glget.GL_GET_SIZES: continue data[:] = weird_value try: GL.glGetIntegerv( value, data ) except error.GLError, err: if err.err == 1280: print '# No: %s'%( value.name, ) else: print 'simple.%s: (1,), # TODO: Check size!'%( value.name, ) else: ordinality = 256 - arrays.sum( (data == weird_value) ) if ordinality == 16: ordinality = (4,4) else: ordinality = (ordinality,) print 'simple.%s: %s,'%(value.name,ordinality)
def glGetTexImage( target, level,format,type, outputType=str ): """Get a texture-level as an image target -- enum constant for the texture engine to be read level -- the mip-map level to read format -- image format to read out the data type -- data-type into which to read the data outputType -- default (str) provides string output of the results iff OpenGL.UNSIGNED_BYTE_IMAGES_AS_STRING is True and type == GL_UNSIGNED_BYTE. Any other value will cause output in the default array output format. returns the pixel data array in the format defined by the format, type and outputType """ from OpenGL.GL import glget dims = [glget.glGetTexLevelParameteriv( target, level, simple.GL_TEXTURE_WIDTH )] if target != simple.GL_TEXTURE_1D: dims.append( glget.glGetTexLevelParameteriv( target, level, simple.GL_TEXTURE_HEIGHT ) ) if target != simple.GL_TEXTURE_2D: dims.append( glget.glGetTexLevelParameteriv( target, level, simple.GL_TEXTURE_DEPTH ) ) array = images.SetupPixelRead( format, tuple(dims), type ) arrayType = arrays.GL_CONSTANT_TO_ARRAY_TYPE[ images.TYPE_TO_ARRAYTYPE.get(type,type) ] simple.glGetTexImage( target, level, format, type, ctypes.c_void_p( arrayType.dataPointer(array)) ) if outputType is str: return images.returnFormat( array, type ) else: return array
def glSelectBuffer(size, buffer=None): """Create a selection buffer of the given size """ if buffer is None: buffer = arrays.GLuintArray.zeros((size, )) simple.glSelectBuffer(size, buffer) contextdata.setValue(simple.GL_SELECTION_BUFFER_POINTER, buffer) return buffer
def glSelectBuffer( size, buffer = None ): """Create a selection buffer of the given size """ if buffer is None: buffer = arrays.GLuintArray.zeros( (size,) ) simple.glSelectBuffer( size, buffer ) contextdata.setValue( simple.GL_SELECTION_BUFFER_POINTER, buffer ) return buffer
def glTexParameter( target, pname, parameter ): """Set a texture parameter, choose underlying call based on pname and parameter""" if isinstance( parameter, float ): return simple.glTexParameterf( target, pname, parameter ) elif isinstance( parameter, int ): return simple.glTexParameteri( target, pname, parameter ) else: value = GLfloatArray.asArray( parameter, constants.GL_FLOAT ) return simple.glTexParameterfv( target, pname, value )
def glTexParameter(target, pname, parameter): """Set a texture parameter, choose underlying call based on pname and parameter""" if isinstance(parameter, float): return simple.glTexParameterf(target, pname, parameter) elif isinstance(parameter, int): return simple.glTexParameteri(target, pname, parameter) else: value = GLfloatArray.asArray(parameter, constants.GL_FLOAT) return simple.glTexParameterfv(target, pname, value)
def glFeedbackBuffer(size, type, buffer=None): """Create a selection buffer of the given size """ if buffer is None: buffer = arrays.GLfloatArray.zeros((size, )) simple.glFeedbackBuffer(size, type, buffer) contextdata.setValue(simple.GL_FEEDBACK_BUFFER_POINTER, buffer) contextdata.setValue("GL_FEEDBACK_BUFFER_TYPE", type) return buffer
def glFeedbackBuffer( size, type, buffer = None ): """Create a selection buffer of the given size """ if buffer is None: buffer = arrays.GLfloatArray.zeros( (size,) ) simple.glFeedbackBuffer( size, type, buffer ) contextdata.setValue( simple.GL_FEEDBACK_BUFFER_POINTER, buffer ) contextdata.setValue( "GL_FEEDBACK_BUFFER_TYPE", type ) return buffer
def setupDefaultTransferMode( ): """Set pixel transfer mode to assumed internal structure of arrays Basically OpenGL-ctypes (and PyOpenGL) assume that your image data is in non-byte-swapped order, with big-endian ordering of bytes (though that seldom matters in image data). These assumptions are normally correct when dealing with Python libraries which expose byte-arrays. """ simple.glPixelStorei(simple.GL_PACK_SWAP_BYTES, 0) simple.glPixelStorei(simple.GL_PACK_LSB_FIRST, 0)
def setupDefaultTransferMode(): """Set pixel transfer mode to assumed internal structure of arrays Basically OpenGL-ctypes (and PyOpenGL) assume that your image data is in non-byte-swapped order, with big-endian ordering of bytes (though that seldom matters in image data). These assumptions are normally correct when dealing with Python libraries which expose byte-arrays. """ simple.glPixelStorei(simple.GL_PACK_SWAP_BYTES, 0) simple.glPixelStorei(simple.GL_PACK_LSB_FIRST, 0)
def glGetPointerv(constant): """Retrieve a stored pointer constant""" # do we have a cached version of the pointer? # get the base pointer from the underlying operation vp = ctypes.voidp() simple.glGetPointerv(constant, ctypes.byref(vp)) current = contextdata.getValue(constant) if current is not None: if arrays.ArrayDatatype.dataPointer(current) == vp.value: return current # XXX should be coercing to the proper type and converting to an array return vp
def glGetPointerv( constant ): """Retrieve a stored pointer constant""" # do we have a cached version of the pointer? # get the base pointer from the underlying operation vp = ctypes.voidp() simple.glGetPointerv( constant, ctypes.byref(vp) ) current = contextdata.getValue( constant ) if current is not None: if arrays.ArrayDatatype.dataPointer( current ) == vp.value: return current # XXX should be coercing to the proper type and converting to an array return vp
def glGetTexImage( target, level,format,type=type ): """Get a texture-level as an image""" from OpenGL.GL import glget dims = [glget.glGetTexLevelParameteriv( target, level, simple.GL_TEXTURE_WIDTH )] if target != simple.GL_TEXTURE_1D: dims.append( glget.glGetTexLevelParameteriv( target, level, simple.GL_TEXTURE_HEIGHT ) ) if target != simple.GL_TEXTURE_2D: dims.append( glget.glGetTexLevelParameteriv( target, level, simple.GL_TEXTURE_DEPTH ) ) array = images.SetupPixelRead( format, tuple(dims), type ) arrayType = arrays.GL_CONSTANT_TO_ARRAY_TYPE[ images.TYPE_TO_ARRAYTYPE.get(type,type) ] simple.glGetTexImage( target, level, format, type, ctypes.c_void_p( arrayType.dataPointer(array)) ) return array
def glMaterial(faces, constant, *args): """glMaterial -- convenience function to dispatch on argument type If passed a single argument in args, calls: glMaterialfv( faces, constant, args[0] ) else calls: glMaterialf( faces, constant, *args ) """ if len(args) == 1: arg = GLfloatArray.asArray(args[0]) if arg is None: raise ValueError("""Null value in glMaterial: %s""" % (args, )) return simple.glMaterialfv(faces, constant, arg) else: return simple.glMaterialf(faces, constant, *args)
def glMaterial( faces, constant, *args ): """glMaterial -- convenience function to dispatch on argument type If passed a single argument in args, calls: glMaterialfv( faces, constant, args[0] ) else calls: glMaterialf( faces, constant, *args ) """ if len(args) == 1: arg = GLfloatArray.asArray( args[0] ) if arg is None: raise ValueError( """Null value in glMaterial: %s"""%(args,) ) return simple.glMaterialfv( faces, constant, arg ) else: return simple.glMaterialf( faces, constant, *args )
def glReadPixels( x,y,width,height,format,type=type ): """Read specified pixels from the current display buffer This typed version returns data in your specified default array data-type format """ x,y,width,height = asInt(x),asInt(y),asInt(width),asInt(height) array = images.SetupPixelRead( format, (width,height), type ) arrayType = arrays.GL_CONSTANT_TO_ARRAY_TYPE[ images.TYPE_TO_ARRAYTYPE.get(type,type) ] imageData = arrayType.dataPointer(array) simple.glReadPixels( x,y, width, height, format,type, ctypes.c_void_p( imageData ) ) return array
def glReadPixels(x, y, width, height, format, type=type, array=None): """Read specified pixels from the current display buffer This typed version returns data in your specified default array data-type format, or in the passed array, which will be converted to the array-type required by the format. """ x, y, width, height = asInt(x), asInt(y), asInt(width), asInt(height) arrayType = arrays.GL_CONSTANT_TO_ARRAY_TYPE[ images.TYPE_TO_ARRAYTYPE.get(type, type)] if array is None: array = images.SetupPixelRead(format, (width, height), type) else: array = arrayType.asArray(array) imageData = arrayType.voidDataPointer(array) simple.glReadPixels(int(x), int(y), int(width), int(height), format, type, imageData) return array
def glColor( *args ): """glColor*f* -- convenience function to dispatch on argument type dispatches to glColor3f, glColor2f, glColor4f, glColor3f, glColor2f, glColor4f depending on the arguments passed... """ arglen = len(args) if arglen == 1: arg = arrays.GLfloatArray.asArray( args[0] ) function = glColorDispatch[arrays.GLfloatArray.arraySize( arg )] return function( arg ) elif arglen == 2: return simple.glColor2d( *args ) elif arglen == 3: return simple.glColor3d( *args ) elif arglen == 4: return simple.glColor4d( *args ) else: raise ValueError( """Don't know how to handle arguments: %s"""%(args,))
def glColor(*args): """glColor*f* -- convenience function to dispatch on argument type dispatches to glColor3f, glColor2f, glColor4f, glColor3f, glColor2f, glColor4f depending on the arguments passed... """ arglen = len(args) if arglen == 1: arg = arrays.GLfloatArray.asArray(args[0]) function = glColorDispatch[arrays.GLfloatArray.arraySize(arg)] return function(arg) elif arglen == 2: return simple.glColor2d(*args) elif arglen == 3: return simple.glColor3d(*args) elif arglen == 4: return simple.glColor4d(*args) else: raise ValueError("""Don't know how to handle arguments: %s""" % (args, ))
def glReadPixels( x,y,width,height,format,type=type, array=None ): """Read specified pixels from the current display buffer This typed version returns data in your specified default array data-type format, or in the passed array, which will be converted to the array-type required by the format. """ x,y,width,height = asInt(x),asInt(y),asInt(width),asInt(height) arrayType = arrays.GL_CONSTANT_TO_ARRAY_TYPE[ images.TYPE_TO_ARRAYTYPE.get(type,type) ] if array is None: array = images.SetupPixelRead( format, (width,height), type ) else: array = arrayType.asArray( array ) imageData = arrayType.voidDataPointer( array ) simple.glReadPixels( int(x),int(y), int(width), int(height), format,type, imageData ) return array
def process_image(): """ copy image and process using CUDA """ global pycuda_source_pbo,source_pbo,current_size, dest_pbo image_width, image_height = current_size assert source_pbo is not None # tell cuda we are going to get into these buffers pycuda_source_pbo.unregister() # activate destination buffer glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, long(source_pbo)) # read data into pbo. note: use BGRA format for optimal performance import OpenGL.raw.GL as rawgl rawgl.glReadPixels( 0, #start x 0, #start y image_width, #end x image_height, #end y GL_BGRA, #format GL_UNSIGNED_BYTE, #output type ctypes.c_void_p(0)) pycuda_source_pbo = cuda_gl.BufferObject(long(source_pbo)) # run the Cuda kernel process(image_width, image_height) # blit convolved texture onto the screen # download texture from PBO glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, long(dest_pbo)) glBindTexture(GL_TEXTURE_2D, output_texture) rawgl.glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, image_width, image_height, GL_BGRA, GL_UNSIGNED_BYTE, ctypes.c_void_p(0))
def glGenTextures(baseFunction, count, textures=None): """Generate count new texture names Note: for compatibility with PyOpenGL 2.x and below, a count of 1 will return a single integer, rather than an array of integers. """ if count <= 0: raise ValueError("""Can't generate 0 or fewer textures""") elif count == 1: # this traditionally returned a single int/long, so we'll continue to # do so, even though it would be easier not to bother. textures = simple.GLuint(0) baseFunction(count, textures) return textures.value else: textures = arrays.GLuintArray.zeros((count, )) baseFunction(count, textures) return textures
"""Change to the given rendering mode If the current mode is GL_FEEDBACK or GL_SELECT, return the current buffer appropriate to the mode """ # must get the current mode to determine operation... from OpenGL.GL import glGetIntegerv from OpenGL.GL import selection, feedback currentMode = glGetIntegerv( simple.GL_RENDER_MODE ) try: currentMode = currentMode[0] except (TypeError,ValueError,IndexError), err: pass if currentMode in (simple.GL_RENDER,0): # no array needs to be returned... return simple.glRenderMode( newMode ) result = simple.glRenderMode( newMode ) # result is now an integer telling us how many elements were copied... if result < 0: if currentMode == simple.GL_SELECT: raise error.GLError( simple.GL_STACK_OVERFLOW, "glSelectBuffer too small to hold selection results", ) elif currentMode == simple.GL_FEEDBACK: raise error.GLError( simple.GL_STACK_OVERFLOW, "glFeedbackBuffer too small to hold selection results", ) else:
"""Change to the given rendering mode If the current mode is GL_FEEDBACK or GL_SELECT, return the current buffer appropriate to the mode """ # must get the current mode to determine operation... from OpenGL.GL import glGetIntegerv from OpenGL.GL import selection, feedback currentMode = glGetIntegerv(simple.GL_RENDER_MODE) try: currentMode = currentMode[0] except (TypeError, ValueError, IndexError), err: pass if currentMode in (simple.GL_RENDER, 0): # no array needs to be returned... return simple.glRenderMode(newMode) result = simple.glRenderMode(newMode) # result is now an integer telling us how many elements were copied... if result < 0: if currentMode == simple.GL_SELECT: raise error.GLError( simple.GL_STACK_OVERFLOW, "glSelectBuffer too small to hold selection results", ) elif currentMode == simple.GL_FEEDBACK: raise error.GLError( simple.GL_STACK_OVERFLOW, "glFeedbackBuffer too small to hold selection results", ) else: