def _alt( base, name ): if hasattr( GL, base ): root = getattr( GL, base ) if callable(root): globals()[base] = alternate( getattr(GL,base),getattr(module,name)) __all__.append( base ) else: globals()[base] = root __all__.append( base ) return True return False
def _alt( base, name ): if hasattr( GL, base ): root = getattr( GL, base ) if hasattr(root,'__call__'): globals()[base] = alternate( getattr(GL,base), getattr(module,name) ) __all__.append( base ) else: globals()[base] = root __all__.append( base ) return True return False
def detect(): glutInit([]) glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH) glutInitWindowSize(200, 200) glutCreateWindow("Detection") functions = ( glDrawArraysInstanced, glDrawArraysInstancedARB, glDrawArraysInstancedEXT, #glDrawArraysInstancedNV, is GLES only ) for function in functions: print(function.__name__, bool(function)) any = alternate(*functions) print(any, bool(any)) try: if fgDeinitialize: fgDeinitialize(False) except NameError: pass # Older PyOpenGL, you may see a seg-fault here...
def detect(): glutInit([]) glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH) glutInitWindowSize(200,200) window = glutCreateWindow("Detection") functions = ( glDrawArraysInstanced, glDrawArraysInstancedARB, glDrawArraysInstancedEXT, #glDrawArraysInstancedNV, is GLES only ) for function in functions: print(function.__name__, bool(function)) any = alternate( *functions ) print(any, bool(any)) try: if fgDeinitialize: fgDeinitialize(False) except NameError as err: pass # Older PyOpenGL, you may see a seg-fault here...
#! /usr/bin/env python '''Tests operation of the OpenGL1.5/ARB Occlusion Query extension ''' from OpenGLContext import testingcontext BaseContext = testingcontext.getInteractive() from OpenGLContext.scenegraph import imagetexture, shape, material, appearance, box from OpenGL.GL import * from OpenGL.GL.ARB.occlusion_query import * from OpenGL.extensions import alternate from OpenGLContext.arrays import array import string, time, sys glBeginQuery = alternate( glBeginQuery, glBeginQueryARB ) glDeleteQueries = alternate( glDeleteQueries, glDeleteQueriesARB ) glEndQuery = alternate( glEndQuery, glEndQueryARB ) glGenQueries = alternate( glGenQueries, glGenQueriesARB ) glGetQueryObjectiv = alternate( glGetQueryObjectiv, glGetQueryObjectivARB ) glGetQueryObjectuiv = alternate( glGetQueryObjectiv, glGetQueryObjectuivARB ) images = [ "nehe_glass.bmp", "pimbackground_FR.jpg", "nehe_wall.bmp", ] sizes = [ (.5,2,.25), (1,1,1), (2,2,2), (3,2,3), (4,3,3),
handedness.append(1) bitanBuffer = [] for i in range(len(V)): n = rasterizer.np.array(nBuffer[i]) t = rasterizer.np.array(tanBuffer[i]) bitangent = rasterizer.np.cross(n, t) * handedness[i] bitanBuffer.append(bitangent.tolist()) return vBuffer, tBuffer, nBuffer, tanBuffer, bitanBuffer, cBuffer, iBuffer, T '''We set up alternate objects that will use whichever function is available at run-time.''' glMultiTexCoord2f = alternate(glMultiTexCoord2f, glMultiTexCoord2fARB) glActiveTexture = alternate( glActiveTexture, glActiveTextureARB, ) class TestContext(BaseContext): """Multi-texturing demo """ initialPosition = (0, 0, 0) rotation = 0 theta = 360 - 45 phi = 30 def OnInit(self):
the Flat renderer. """ from OpenGLContext.arrays import * from OpenGL.GL import * from OpenGL.GL.ARB.occlusion_query import * from OpenGL.GL.HP.occlusion_test import * from OpenGL.GLUT import glutSolidCube from vrml.vrml97 import nodetypes from vrml import node, field, protofunctions, cache from OpenGLContext import frustum, utilities, doinchildmatrix from OpenGL.extensions import alternate import exceptions import logging log = logging.getLogger(__name__) glBeginQuery = alternate(glBeginQuery, glBeginQueryARB) glDeleteQueries = alternate(glDeleteQueries, glDeleteQueriesARB) glEndQuery = alternate(glEndQuery, glEndQueryARB) glGenQueries = alternate(glGenQueries, glGenQueriesARB) glGetQueryObjectiv = alternate(glGetQueryObjectiv, glGetQueryObjectivARB) glGetQueryObjectuiv = alternate(glGetQueryObjectiv, glGetQueryObjectuivARB) try: from vrml.arrays import frustcullaccel except ImportError: frustcullaccel = None class UnboundedObject(exceptions.ValueError): """Error raised when an object does not support bounding volumes"""
a "light map" modulating the base texture. ''' from OpenGLContext import testingcontext BaseContext = testingcontext.getInteractive() from OpenGLContext import texture from OpenGL.GL import * from OpenGL.GL.ARB.multitexture import * from OpenGLContext.arrays import array import sys from OpenGLContext.events.timer import Timer from OpenGL.extensions import alternate '''We set up alternate objects that will use whichever function is available at run-time.''' glMultiTexCoord2f = alternate( glMultiTexCoord2f, glMultiTexCoord2fARB ) glActiveTexture = alternate( glActiveTexture, glActiveTextureARB, ) class TestContext( BaseContext ): """Multi-texturing demo """ initialPosition = (0,0,0) rotation = 0 def OnInit( self ): """Do all of our setup functions...""" if not glMultiTexCoord2f:
from collections import defaultdict # Requires Python 2.5 from numpy import array, zeros, float32 from OpenGL.GL import * from OpenGL.extensions import alternate from OpenGL.GL.EXT.multi_draw_arrays import glMultiDrawArraysEXT glMultiDrawArrays = alternate(glMultiDrawArrays, glMultiDrawArraysEXT) from clutterdef import ClutterDef, COL_SELECTED, COL_UNPAINTED # one per texture per layer class DrawBucket: def __init__(self): self.first = [] self.count = [] self.afirst = self.acount = None def add(self, first, count): self.first.append(first) self.count.append(count) self.afirst = self.acount = None def draw(self, glstate): if glstate.multi_draw_arrays: if self.afirst is None: self.afirst = array(self.first, GLint) self.acount = array(self.count, GLsizei) glMultiDrawArrays(GL_TRIANGLES, self.afirst, self.acount, len(self.count)) else: for first, count in zip(self.first, self.count):
# Part of the PsychoPy library # Copyright (C) 2015 Jonathan Peirce # Distributed under the terms of the GNU General Public License (GPL). from OpenGL.GL import * from OpenGL.GL.ARB.shader_objects import * from OpenGL.GL.ARB.fragment_shader import * from OpenGL.GL.ARB.vertex_shader import * import time, sys from OpenGL.extensions import alternate glCreateShader = alternate("glCreateShader", glCreateShader, glCreateShaderObjectARB) glShaderSource = alternate("glShaderSource", glShaderSource, glShaderSourceARB) glCompileShader = alternate("glCompileShader", glCompileShader, glCompileShaderARB) glCreateProgram = alternate("glCreateProgram", glCreateProgram, glCreateProgramObjectARB) glAttachShader = alternate("glAttachShader", glAttachShader, glAttachObjectARB) glValidateProgram = alternate("glValidateProgram", glValidateProgram, glValidateProgramARB) glLinkProgram = alternate("glLinkProgram", glLinkProgram, glLinkProgramARB) glDeleteShader = alternate("glDeleteShader", glDeleteShader, glDeleteObjectARB) glUseProgram = alternate("glUseProgram", glUseProgram, glUseProgramObjectARB) def compileShader(source, shaderType): """Compile shader source of given type""" shader = glCreateShader(shaderType) glShaderSource(shader, source) glCompileShader(shader) return shader def compileProgram(vertexSource=None, fragmentSource=None):
"""Convenience API for using Frame Buffer Objects""" from OpenGL.extensions import alternate from OpenGL.GL.ARB.framebuffer_object import * from OpenGL.GL.EXT.framebuffer_object import * from OpenGL.GL.EXT.framebuffer_multisample import * from OpenGL.GL.EXT.framebuffer_blit import * glBindFramebuffer = alternate(glBindFramebuffer,glBindFramebufferEXT) glBindRenderbuffer = alternate( glBindRenderbuffer, glBindRenderbufferEXT ) glCheckFramebufferStatus = alternate( glCheckFramebufferStatus, glCheckFramebufferStatusEXT ) glDeleteFramebuffers = alternate( glDeleteFramebuffers, glDeleteFramebuffersEXT ) glDeleteRenderbuffers = alternate( glDeleteRenderbuffers, glDeleteRenderbuffersEXT ) glFramebufferRenderbuffer = alternate( glFramebufferRenderbuffer, glFramebufferRenderbufferEXT ) glFramebufferTexture1D = alternate( glFramebufferTexture1D, glFramebufferTexture1DEXT ) glFramebufferTexture2D = alternate( glFramebufferTexture2D, glFramebufferTexture2DEXT ) glFramebufferTexture3D = alternate( glFramebufferTexture3D, glFramebufferTexture3DEXT ) glGenFramebuffers = alternate( glGenFramebuffers, glGenFramebuffersEXT ) glGenRenderbuffers = alternate( glGenRenderbuffers, glGenRenderbuffersEXT ) glGenerateMipmap = alternate( glGenerateMipmap, glGenerateMipmapEXT ) glGetFramebufferAttachmentParameteriv = alternate( glGetFramebufferAttachmentParameteriv, glGetFramebufferAttachmentParameterivEXT ) glGetRenderbufferParameteriv = alternate( glGetRenderbufferParameteriv, glGetRenderbufferParameterivEXT ) glIsFramebuffer = alternate( glIsFramebuffer, glIsFramebufferEXT ) glIsRenderbuffer = alternate( glIsRenderbuffer, glIsRenderbufferEXT ) glRenderbufferStorage = alternate( glRenderbufferStorage, glRenderbufferStorageEXT ) glBlitFramebuffer = alternate( glBlitFramebuffer, glBlitFramebufferEXT ) glRenderbufferStorageMultisample = alternate( glRenderbufferStorageMultisample, glRenderbufferStorageMultisampleEXT ) # this entry point is new to the ARB version of the extensions #glFramebufferTextureLayer = alternate( glFramebufferTextureLayer, glFramebufferTextureLayerEXT )
PRIMITIVE_RESTART_NV = 2 # Primitive restart type supported (filled in in probe_extensions) primitive_restart_mode = PRIMITIVE_RESTART_NONE gl_types = { PRIM_TRIANGLES: GL_TRIANGLES, PRIM_TRIANGLE_STRIP: GL_TRIANGLE_STRIP } PRIMITIVE_RESTART_INDEX = 65535 # extension alternates for GL <2.0 from OpenGL.GL.ARB.vertex_shader import * from OpenGL.GL.ARB.vertex_buffer_object import * from OpenGL.GL.ARB.vertex_program import * glGetAttribLocation = alternate('glGetAttribLocation', glGetAttribLocation, glGetAttribLocationARB) glEnableVertexAttribArray = alternate('glEnableVertexAttribArray', glEnableVertexAttribArray, glEnableVertexAttribArrayARB) glDisableVertexAttribArray = alternate('glDisableVertexAttribArray', glDisableVertexAttribArray, glDisableVertexAttribArrayARB) glVertexAttribPointer = alternate('glVertexAttribPointer', glVertexAttribPointer, glVertexAttribPointerARB) glGenBuffers = alternate('glGenBuffers', glGenBuffers, glGenBuffersARB) glBindBuffer = alternate('glBindBuffer', glBindBuffer, glBindBufferARB) glBufferData = alternate('glBufferData', glBufferData, glBufferDataARB) def reshape(w, h): global width, height width = w height = h arcball.place([w/2, h/2], h/2) def draw(): glViewport(0, 0, width, height)
"""Geometry type for "point-arrays" w/ colour support""" from OpenGL.GL import * from vrml.vrml97 import basenodes from OpenGLContext.scenegraph import coordinatebounded from OpenGLContext.arrays import array from OpenGL.extensions import alternate from OpenGL.GL.ARB.point_parameters import * from OpenGL.GL.EXT.point_parameters import * import logging log = logging.getLogger(__name__) glPointParameterf = alternate(glPointParameterf, glPointParameterfARB, glPointParameterfEXT) glPointParameterfv = alternate(glPointParameterfv, glPointParameterfvARB, glPointParameterfEXT) RESET_ATTENUATION = array([1, 0, 0], 'f') class PointSet(coordinatebounded.CoordinateBounded, basenodes.PointSet): """VRML97-style Point-Set object http://www.web3d.org/x3d/specifications/vrml/ISO-IEC-14772-IS-VRML97WithAmendment1/part1/nodesRef.html#PointSet """ def render( self, visible=1, # can skip normals and textures if not lit=1, # can skip normals if not textured=1, # can skip textureCoordinates if not transparent=0, # need to sort triangle geometry... mode=None, # the renderpass object ):
from collections import defaultdict # Requires Python 2.5 from numpy import array, zeros, float32 from OpenGL.GL import * from OpenGL.extensions import alternate from OpenGL.GL.EXT.multi_draw_arrays import glMultiDrawArraysEXT glMultiDrawArrays = alternate(glMultiDrawArrays, glMultiDrawArraysEXT) from clutterdef import ClutterDef, COL_SELECTED, COL_UNPAINTED # one per texture per layer class DrawBucket: def __init__(self): self.first=[] self.count=[] self.afirst = self.acount = None def add(self, first, count): self.first.append(first) self.count.append(count) self.afirst = self.acount = None def draw(self, glstate): if glstate.multi_draw_arrays: if self.afirst is None: self.afirst = array(self.first, GLint) self.acount = array(self.count, GLsizei) glMultiDrawArrays(GL_TRIANGLES, self.afirst, self.acount, len(self.count)) else: for first,count in zip(self.first, self.count):
for suffix in ('ObjectARB', '_ARB', 'ARB'): if name.endswith(suffix): found = False base = name[:-(len(suffix))] if base not in _excludes: if _alt(base, name): found = True break if found is False: log.debug( '''Found no alternate for: %s.%s''', module.__name__, name, ) glAttachShader = alternate(GL.glAttachShader, shader_objects.glAttachObjectARB) glDetachShader = alternate(GL.glDetachShader, shader_objects.glDetachObjectARB) glDeleteShader = alternate(GL.glDeleteShader, shader_objects.glDeleteObjectARB) glGetAttachedShaders = alternate(GL.glGetAttachedShaders, shader_objects.glGetAttachedObjectsARB) glGetProgramInfoLog = alternate(GL.glGetProgramInfoLog, shader_objects.glGetInfoLogARB) glGetShaderInfoLog = alternate(GL.glGetShaderInfoLog, shader_objects.glGetInfoLogARB) glGetShaderiv = alternate(GL.glGetShaderiv, shader_objects.glGetObjectParameterivARB) glGetProgramiv = alternate(GL.glGetProgramiv, shader_objects.glGetObjectParameterivARB)
from ngsgui import _debug from OpenGL.GL.ARB import debug_output from OpenGL.extensions import alternate import ctypes, ngsolve, numpy, cmath, math import qtpy from qtpy import QtCore, QtGui from ngsgui.shader import locations as shaderpaths _DEVELOP=True glGetDebugMessageLog = alternate( 'glGetDebugMessageLog', GL.glGetDebugMessageLog, debug_output.glGetDebugMessageLogARB) # the size specifications of some debug_output tokens seem to be missing so # we add them manually even though it does not seem to be a nice way to do it # GL.glget.GL_GET_SIZES[debug_output.GL_MAX_DEBUG_MESSAGE_LENGTH_ARB] = (1,) # GL.glget.GL_GET_SIZES[debug_output.GL_DEBUG_LOGGED_MESSAGES_ARB] = (1,) def get_constant(value, namespace): """Get symbolic constant. value - the (integer) token value to look for namespace - the module object to search in """
finally: GL.glPopAttrib() listCount = 0 @classmethod def glGenLists(cls, n): cls.listCount += n return GL.glGenLists(n) @classmethod def glDeleteLists(cls, base, n): cls.listCount -= n return GL.glDeleteLists(base, n) glActiveTexture = alternate(GL.glActiveTexture, multitexture.glActiveTextureARB) allDisplayLists = [] class DisplayList(object): def __init__(self): self._list = None self.dirty = True def _delete(r): allDisplayLists.remove(r) allDisplayLists.append(weakref.ref(self, _delete)) @classmethod def deallocAllLists(self):
for name in dir(module): found = None for suffix in ('ObjectARB','_ARB','ARB'): if name.endswith( suffix ): found = False base = name[:-(len(suffix))] if base not in _excludes: if _alt( base, name ): found = True break if found is False: log.debug( '''Found no alternate for: %s.%s''', module.__name__,name, ) glAttachShader = alternate( GL.glAttachShader,shader_objects.glAttachObjectARB ) glDetachShader = alternate( GL.glDetachShader,shader_objects.glDetachObjectARB ) glDeleteShader = alternate( GL.glDeleteShader,shader_objects.glDeleteObjectARB ) glGetAttachedShaders = alternate( GL.glGetAttachedShaders, shader_objects.glGetAttachedObjectsARB ) glGetProgramInfoLog = alternate( GL.glGetProgramInfoLog, shader_objects.glGetInfoLogARB ) glGetShaderInfoLog = alternate( GL.glGetShaderInfoLog, shader_objects.glGetInfoLogARB ) glGetShaderiv = alternate( GL.glGetShaderiv, shader_objects.glGetObjectParameterivARB ) glGetProgramiv = alternate( GL.glGetProgramiv, shader_objects.glGetObjectParameterivARB ) GL_VALIDATE_STATUS = GL.GL_VALIDATE_STATUS GL_COMPILE_STATUS = GL.GL_COMPILE_STATUS GL_LINK_STATUS = GL.GL_LINK_STATUS GL_FALSE = GL.GL_FALSE GL_TRUE = GL.GL_TRUE
# good, should have got this error pass else: raise RuntimeError( """Did not catch invalid context!""" ) from OpenGL import constants, error from OpenGL.GLU import * from OpenGL.arrays import arraydatatype import OpenGL from OpenGL.extensions import alternate import ctypes from OpenGL.GL.framebufferobjects import * from OpenGL.GL.EXT.multi_draw_arrays import * from OpenGL.GL.ARB.imaging import * glMultiDrawElements = alternate( glMultiDrawElementsEXT, glMultiDrawElements, ) class Tests( unittest.TestCase ): evaluator_ctrlpoints = [[[ -1.5, -1.5, 4.0], [-0.5, -1.5, 2.0], [0.5, -1.5, -1.0], [1.5, -1.5, 2.0]], [[-1.5, -0.5, 1.0], [-0.5, -0.5, 3.0], [0.5, -0.5, 0.0], [1.5, -0.5, -1.0]], [[-1.5, 0.5, 4.0], [-0.5, 0.5, 0.0], [0.5, 0.5, 3.0], [1.5, 0.5, 4.0]], [[-1.5, 1.5, -2.0], [-0.5, 1.5, -2.0], [0.5, 1.5, 0.0], [1.5, 1.5, -1.0]]] width = height = 300 def setUp( self ): """Set up the operation""" self.screen = pygame.display.set_mode( (self.width,self.height),
# Part of the PsychoPy library # Copyright (C) 2015 Jonathan Peirce # Distributed under the terms of the GNU General Public License (GPL). from OpenGL.GL import * from OpenGL.GL.ARB.shader_objects import * from OpenGL.GL.ARB.fragment_shader import * from OpenGL.GL.ARB.vertex_shader import * import time, sys from OpenGL.extensions import alternate glCreateShader = alternate('glCreateShader', glCreateShader, glCreateShaderObjectARB) glShaderSource = alternate('glShaderSource', glShaderSource, glShaderSourceARB) glCompileShader = alternate('glCompileShader', glCompileShader, glCompileShaderARB) glCreateProgram = alternate('glCreateProgram', glCreateProgram, glCreateProgramObjectARB) glAttachShader = alternate('glAttachShader', glAttachShader, glAttachObjectARB) glValidateProgram = alternate('glValidateProgram', glValidateProgram, glValidateProgramARB) glLinkProgram = alternate('glLinkProgram', glLinkProgram, glLinkProgramARB) glDeleteShader = alternate('glDeleteShader', glDeleteShader, glDeleteObjectARB) glUseProgram = alternate('glUseProgram', glUseProgram, glUseProgramObjectARB) def compileShader(source, shaderType): """Compile shader source of given type""" shader = glCreateShader(shaderType) glShaderSource(shader, source) glCompileShader(shader)
pass else: print('WARNING: Failed to catch invalid context') #raise RuntimeError( """Did not catch invalid context!""" ) #from OpenGL import error from OpenGL.GLU import * #from OpenGL.arrays import arraydatatype import OpenGL from OpenGL.extensions import alternate #import ctypes from OpenGL.GL.framebufferobjects import * from OpenGL.GL.EXT.multi_draw_arrays import * from OpenGL.GL.ARB.imaging import * glMultiDrawElements = alternate( glMultiDrawElementsEXT, glMultiDrawElements, ) class BaseTest(unittest.TestCase): width = height = 300 def setUp(self): """Set up the operation""" self.screen = pygame.display.set_mode( (self.width, self.height), pygame.OPENGL | pygame.DOUBLEBUF, ) pygame.display.set_caption('Testing system')
"""Geometry type for "point-arrays" w/ colour support""" from OpenGL.GL import * from vrml.vrml97 import basenodes from OpenGLContext.scenegraph import coordinatebounded from OpenGLContext.arrays import array from OpenGL.extensions import alternate from OpenGL.GL.ARB.point_parameters import * from OpenGL.GL.EXT.point_parameters import * import logging log = logging.getLogger( __name__ ) glPointParameterf = alternate( glPointParameterf, glPointParameterfARB,glPointParameterfEXT ) glPointParameterfv = alternate( glPointParameterfv, glPointParameterfvARB, glPointParameterfEXT ) RESET_ATTENUATION = array( [1,0,0],'f') class PointSet( coordinatebounded.CoordinateBounded, basenodes.PointSet ): """VRML97-style Point-Set object http://www.web3d.org/x3d/specifications/vrml/ISO-IEC-14772-IS-VRML97WithAmendment1/part1/nodesRef.html#PointSet """ def render ( self, visible = 1, # can skip normals and textures if not lit = 1, # can skip normals if not