def glGetTexImage(target, level, format, type, array=None, outputType=bytes): """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 array -- optional array/offset into which to store the value outputType -- default (bytes) 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 """ arrayType = arrays.GL_CONSTANT_TO_ARRAY_TYPE[images.TYPE_TO_ARRAYTYPE.get( type, type)] if array is None: dims = _get_texture_level_dims(target, level) array = imageData = images.SetupPixelRead(format, tuple(dims), type) owned = True else: if isinstance(array, integer_types): imageData = ctypes.c_void_p(array) else: array = arrayType.asArray(array) imageData = arrayType.voidDataPointer(array) owned = False GL_1_1.glGetTexImage(target, level, format, type, imageData) if outputType is bytes: return images.returnFormat(array, type) else: return array
def glGetTexImage(target, level, format, type, array=None, outputType=bytes): """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 array -- optional array/offset into which to store the value outputType -- default (bytes) 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 """ arrayType = arrays.GL_CONSTANT_TO_ARRAY_TYPE[images.TYPE_TO_ARRAYTYPE.get(type, type)] if array is None: dims = _get_texture_level_dims(target, level) array = imageData = images.SetupPixelRead(format, tuple(dims), type) owned = True else: if isinstance(array, integer_types): imageData = ctypes.c_void_p(array) else: array = arrayType.asArray(array) imageData = arrayType.voidDataPointer(array) owned = False GL_1_1.glGetTexImage(target, level, format, type, imageData) if outputType is bytes: return images.returnFormat(array, type) else: return array
def glReadPixels( x,y,width,height,format,type=type, array=None, outputType=bytes ): """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 = imageData = images.SetupPixelRead( format, (width,height), type ) owned = True else: if isinstance( array, integer_types): imageData = ctypes.c_void_p( array ) else: array = arrayType.asArray( array ) imageData = arrayType.voidDataPointer( array ) owned = False GL_1_1.glReadPixels( x,y, width, height, format,type, imageData ) if owned and outputType is bytes: return images.returnFormat( array, type ) else: return array
def glReadPixels( x,y,width,height,format,type, array=None, 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 array -- optional array/offset into which to store the value 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) 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 ) GL_1_1.glReadPixels( x,y,width,height, format,type, imageData ) if outputType is str: return images.returnFormat( array, type ) else: return array
def glGetTexImage(target, level, format, type, array=None, outputType=bytes): """bug fixed overwirte version""" arrayType = arrays.GL_CONSTANT_TO_ARRAY_TYPE[ images.TYPE_TO_ARRAYTYPE.get(type, type)] if array is None: dims = OpenGLContext._get_texture_level_dims(target, level) imageData = images.SetupPixelRead(format, tuple(dims), type) array = imageData else: if isinstance(array, integer_types): imageData = ctypes.c_void_p(array) else: array = arrayType.asArray(array) imageData = arrayType.voidDataPointer(array) GL_1_1.glGetTexImage(target, level, format, type, imageData) if outputType is bytes: return images.returnFormat(array, type) else: return array
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, GL_1_1.GL_TEXTURE_WIDTH )] if target != GL_1_1.GL_TEXTURE_1D: dims.append( glget.glGetTexLevelParameteriv( target, level, GL_1_1.GL_TEXTURE_HEIGHT ) ) if target != GL_1_1.GL_TEXTURE_2D: dims.append( glget.glGetTexLevelParameteriv( target, level, GL_1_2.GL_TEXTURE_DEPTH ) ) array = images.SetupPixelRead( format, tuple(dims), type ) arrayType = arrays.GL_CONSTANT_TO_ARRAY_TYPE[ images.TYPE_TO_ARRAYTYPE.get(type,type) ] GL_1_1.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 glReadPixels(x, y, width, height, format, type=type, array=None, outputType=bytes): """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 = imageData = images.SetupPixelRead(format, (width, height), type) owned = True else: if isinstance(array, integer_types): imageData = ctypes.c_void_p(array) else: array = arrayType.asArray(array) imageData = arrayType.voidDataPointer(array) owned = False GL_1_1.glReadPixels(x, y, width, height, format, type, imageData) if owned and outputType is bytes: 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 glRenderMode( newMode ): """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) as 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: raise error.GLError( simple.GL_STACK_OVERFLOW, "Unknown glRenderMode buffer (%s) too small to hold selection results"%( currentMode, ), ) # Okay, now that the easy cases are out of the way... # Do we have a pre-stored pointer about which the user already knows? context = platform.GetCurrentContext() if context == 0: raise error.Error( """Returning from glRenderMode without a valid context!""" ) arrayConstant, wrapperFunction = { simple.GL_FEEDBACK: (simple.GL_FEEDBACK_BUFFER_POINTER,feedback.parseFeedback), simple.GL_SELECT: (simple.GL_SELECTION_BUFFER_POINTER, selection.GLSelectRecord.fromArray), }[ currentMode ] current = contextdata.getValue( arrayConstant ) # XXX check to see if it's the *same* array we set currently! if current is None: current = glGetPointerv( arrayConstant ) # XXX now, can turn the array into the appropriate wrapper type... if wrapperFunction: current = wrapperFunction( current, result ) return current
def glRenderMode(newMode): """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) as 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: raise error.GLError( simple.GL_STACK_OVERFLOW, "Unknown glRenderMode buffer (%s) too small to hold selection results" % (currentMode, ), ) # Okay, now that the easy cases are out of the way... # Do we have a pre-stored pointer about which the user already knows? context = platform.GetCurrentContext() if context == 0: raise error.Error( """Returning from glRenderMode without a valid context!""") arrayConstant, wrapperFunction = { simple.GL_FEEDBACK: (simple.GL_FEEDBACK_BUFFER_POINTER, feedback.parseFeedback), simple.GL_SELECT: (simple.GL_SELECTION_BUFFER_POINTER, selection.GLSelectRecord.fromArray), }[currentMode] current = contextdata.getValue(arrayConstant) # XXX check to see if it's the *same* array we set currently! if current is None: current = glGetPointerv(arrayConstant) # XXX now, can turn the array into the appropriate wrapper type... if wrapperFunction: current = wrapperFunction(current, result) return current
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 glGetTexImage( target, level,format,type=type ): """Get a texture-level as an image""" from OpenGL.GL import glget dims = [glget.glGetTexLevelParameteriv( target, level, GL_1_1.GL_TEXTURE_WIDTH )] if target != GL_1_1.GL_TEXTURE_1D: dims.append( glget.glGetTexLevelParameteriv( target, level, GL_1_1.GL_TEXTURE_HEIGHT ) ) if target != GL_1_1.GL_TEXTURE_2D: dims.append( glget.glGetTexLevelParameteriv( target, level, GL_1_2.GL_TEXTURE_DEPTH ) ) array = images.SetupPixelRead( format, tuple(dims), type ) arrayType = arrays.GL_CONSTANT_TO_ARRAY_TYPE[ images.TYPE_TO_ARRAYTYPE.get(type,type) ] GL_1_1.glGetTexImage( target, level, format, type, ctypes.c_void_p( arrayType.dataPointer(array)) ) 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) GL_1_1.glReadPixels(x, y, width, height, format, type, imageData) return array
def glReadPixels(x, y, width, height, format, type, array=None, outputType=bytes): """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 array -- optional array/offset into which to store the value outputType -- default (bytes) 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) arrayType = arrays.GL_CONSTANT_TO_ARRAY_TYPE[images.TYPE_TO_ARRAYTYPE.get( type, type)] if array is None: array = imageData = images.SetupPixelRead(format, (width, height), type) owned = True else: if isinstance(array, integer_types): imageData = ctypes.c_void_p(array) else: array = arrayType.asArray(array) imageData = arrayType.voidDataPointer(array) owned = False GL_1_1.glReadPixels(x, y, width, height, format, type, imageData) if owned and outputType is bytes: return images.returnFormat(array, type) else: return array
def glGetTexImage(target, level, format, type=type): """Get a texture-level as an image""" from OpenGL.GL import glget dims = [ glget.glGetTexLevelParameteriv(target, level, GL_1_1.GL_TEXTURE_WIDTH) ] if target != GL_1_1.GL_TEXTURE_1D: dims.append( glget.glGetTexLevelParameteriv(target, level, GL_1_1.GL_TEXTURE_HEIGHT)) if target != GL_1_1.GL_TEXTURE_2D: dims.append( glget.glGetTexLevelParameteriv(target, level, GL_1_2.GL_TEXTURE_DEPTH)) array = images.SetupPixelRead(format, tuple(dims), type) arrayType = arrays.GL_CONSTANT_TO_ARRAY_TYPE[ images.TYPE_TO_ARRAYTYPE.get(type, type)] GL_1_1.glGetTexImage(target, level, format, type, ctypes.c_void_p(arrayType.dataPointer(array))) 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 ) GL_1_1.glReadPixels( x,y, width, height, format,type, imageData ) return array
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, GL_1_1.GL_TEXTURE_WIDTH) ] if target != GL_1_1.GL_TEXTURE_1D: dims.append( glget.glGetTexLevelParameteriv(target, level, GL_1_1.GL_TEXTURE_HEIGHT)) if target != GL_1_1.GL_TEXTURE_2D: dims.append( glget.glGetTexLevelParameteriv(target, level, GL_1_2.GL_TEXTURE_DEPTH)) array = images.SetupPixelRead(format, tuple(dims), type) arrayType = arrays.GL_CONSTANT_TO_ARRAY_TYPE[images.TYPE_TO_ARRAYTYPE.get( type, type)] GL_1_1.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 _get_texture_level_dims(target,level): """Retrieve texture dims for given level and target""" dims = [] dim = _types.GLuint() GL_1_1.glGetTexLevelParameteriv( target, level, GL_1_1.GL_TEXTURE_WIDTH, dim ) dims = [dim.value] if target != GL_1_1.GL_TEXTURE_1D: GL_1_1.glGetTexLevelParameteriv( target, level, GL_1_1.GL_TEXTURE_HEIGHT, dim ) dims.append( dim.value ) if target != GL_1_1.GL_TEXTURE_2D: GL_1_1.glGetTexLevelParameteriv( target, level, GL_1_1.GL_TEXTURE_DEPTH, dim ) dims.append( dim.value ) return dims
def _get_texture_level_dims(target, level): dim = _types.GLuint() GL_1_1.glGetTexLevelParameteriv(target, level, GL_1_1.GL_TEXTURE_WIDTH, dim) dims = [dim.value] if target != GL_1_1.GL_TEXTURE_1D: GL_1_1.glGetTexLevelParameteriv(target, level, GL_1_1.GL_TEXTURE_HEIGHT, dim) dims.append(dim.value) if target != GL_1_1.GL_TEXTURE_2D: # bug fixed : GL_1_1.GL_TEXTURE_DEPTH -> GL_1_2.GL_TEXTURE_DEPTH GL_1_1.glGetTexLevelParameteriv(target, level, GL_1_2.GL_TEXTURE_DEPTH, dim) dims.append(dim.value) return dims
"""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: