def compile( self, mode=None ): """Compile the box as a display-list""" if vbo.get_implementation(): vb = vbo.VBO( array( list(yieldVertices( self.size )), 'f')) def draw( textured=True,lit=True ): vb.bind() try: glPushClientAttrib(GL_CLIENT_ALL_ATTRIB_BITS) try: glEnableClientState( GL_VERTEX_ARRAY ) if lit: glEnableClientState( GL_NORMAL_ARRAY ) glNormalPointer( GL_FLOAT, 32, vb+8 ) if textured: glEnableClientState( GL_TEXTURE_COORD_ARRAY ) glTexCoordPointer( 2, GL_FLOAT, 32, vb ) glVertexPointer( 3, GL_FLOAT, 32, vb+20 ) glDrawArrays( GL_TRIANGLES, 0, 36 ) finally: glPopClientAttrib() finally: vb.unbind() else: vb = array( list(yieldVertices( self.size )), 'f') def draw(textured=True,lit=True): glPushClientAttrib(GL_CLIENT_ALL_ATTRIB_BITS) try: glInterleavedArrays( GL_T2F_N3F_V3F, 0, vb ) glDrawArrays( GL_TRIANGLES, 0, 36 ) finally: glPopClientAttrib() holder = mode.cache.holder(self, draw) holder.depend( self, protofunctions.getField(self, 'size') ) return draw
def get_vbos(self, mode): """Retrieve vertex buffer object source if we support them or None if not""" if vbo.get_implementation(): vbos = mode.cache.getData(self, key='vbos') if vbos is None: vbos = VBOHolder() # compile our data to a set of vbos holder = mode.cache.holder(self, key="vbos", data=vbos) for field, attr in self.NODE_FIELDS: node = getattr(self, field) value = getattr(node, attr, None) if value is not None: setattr(vbos, field, vbo.VBO(value)) # TODO: this is *way* too general, as it will # cause the *entire* set of VBOs to be discarded # and recreated whenever *anything* changes! holder.depend(node, attr) holder.depend(self, field) holder.depend(self, 'index') else: vbos = Holder() for field, attr in self.NODE_FIELDS: node = getattr(self, field) if node: value = getattr(node, attr, ()) setattr(vbos, field, vbo.VBO(value)) return vbos
def get_vbos( self, mode): """Retrieve vertex buffer object source if we support them or None if not""" if vbo.get_implementation(): vbos = mode.cache.getData(self, key='vbos') if vbos is None: vbos = VBOHolder() # compile our data to a set of vbos holder = mode.cache.holder(self, key = "vbos", data = vbos) for field,attr in self.NODE_FIELDS: node = getattr( self, field ) value = getattr( node, attr, None) if value is not None: setattr( vbos,field, vbo.VBO( value )) # TODO: this is *way* too general, as it will # cause the *entire* set of VBOs to be discarded # and recreated whenever *anything* changes! holder.depend( node, attr ) holder.depend( self, field ) holder.depend( self, 'index' ) else: vbos = Holder() for field,attr in self.NODE_FIELDS: node = getattr( self, field ) if node: value = getattr( node, attr, ()) setattr( vbos,field, vbo.VBO( value )) return vbos
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 for which we compile ): """Render the ArrayGeometry object called by IndexedFaceSet.render to do the actual rendering of the node. """ if not len(self.vertices): return 1 # we are already finished vboAvailable = bool(vbo.get_implementation()) glPushClientAttrib(GL_CLIENT_ALL_ATTRIB_BITS) glPushAttrib(GL_ALL_ATTRIB_BITS) try: glEnableClientState(GL_VERTEX_ARRAY) self.callBound(glVertexPointerf, self.vertices) if visible and self.colours is not None: # make the color field alter the diffuse color, should instead be aware of current material/lighting... glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE) glEnable(GL_COLOR_MATERIAL) glEnableClientState(GL_COLOR_ARRAY) self.callBound(glColorPointerf, self.colours) # else: # glDisableClientState( GL_COLOR_ARRAY ) if lit and self.normals is not None: glEnableClientState(GL_NORMAL_ARRAY) self.callBound(glNormalPointerf, self.normals) glEnable(GL_NORMALIZE) # should do this explicitly eventually else: glDisable(GL_LIGHTING) # glDisableClientState( GL_NORMAL_ARRAY ) if visible and textured and self.textures is not None: glEnableClientState(GL_TEXTURE_COORD_ARRAY) self.callBound(glTexCoordPointerf, self.textures) # else: # glDisableClientState( GL_TEXTURE_COORD_ARRAY ) glFrontFace(self.ccw) if self.solid: # and not transparent: glEnable(GL_CULL_FACE) else: glDisable(GL_CULL_FACE) # do the actual rendering if visible and transparent: self.drawTransparent(mode=mode) else: self.draw() # cleanup the environment finally: glPopAttrib() glPopClientAttrib() return 1
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 for which we compile ): """Render the ArrayGeometry object called by IndexedFaceSet.render to do the actual rendering of the node. """ if not len(self.vertices): return 1 # we are already finished vboAvailable = bool(vbo.get_implementation()) glPushClientAttrib(GL_CLIENT_ALL_ATTRIB_BITS) glPushAttrib(GL_ALL_ATTRIB_BITS) try: glEnableClientState( GL_VERTEX_ARRAY ) self.callBound( glVertexPointerf, self.vertices) if visible and self.colours is not None: # make the color field alter the diffuse color, should instead be aware of current material/lighting... glColorMaterial( GL_FRONT_AND_BACK, GL_DIFFUSE) glEnable( GL_COLOR_MATERIAL ) glEnableClientState( GL_COLOR_ARRAY ) self.callBound( glColorPointerf, self.colours) # else: # glDisableClientState( GL_COLOR_ARRAY ) if lit and self.normals is not None: glEnableClientState( GL_NORMAL_ARRAY ) self.callBound( glNormalPointerf, self.normals) glEnable(GL_NORMALIZE); # should do this explicitly eventually else: glDisable( GL_LIGHTING ) # glDisableClientState( GL_NORMAL_ARRAY ) if visible and textured and self.textures is not None: glEnableClientState( GL_TEXTURE_COORD_ARRAY ) self.callBound( glTexCoordPointerf, self.textures ) # else: # glDisableClientState( GL_TEXTURE_COORD_ARRAY ) glFrontFace( self.ccw) if self.solid:# and not transparent: glEnable( GL_CULL_FACE ) else: glDisable( GL_CULL_FACE ) # do the actual rendering if visible and transparent: self.drawTransparent( mode = mode ) else: self.draw() # cleanup the environment finally: glPopAttrib() glPopClientAttrib() return 1
def test_vbo(self): """Test utility vbo wrapper""" from OpenGL.arrays import vbo if not vbo.get_implementation(): return if not glVertexPointerd or not glDrawElements: return points = array([ [0, 0, 0], [0, 1, 0], [1, .5, 0], [1, 0, 0], [1.5, .5, 0], [1.5, 0, 0], ], dtype='d') indices = array( range(len(points)), ['i', 'I'][bool(OpenGL.ERROR_ON_COPY)], # test coercion if we can ) d = vbo.VBO(points) glDisable(GL_CULL_FACE) glNormal3f(0, 0, 1) glColor3f(1, 1, 1) glEnableClientState(GL_VERTEX_ARRAY) try: for x in range(1, 255, 10): d.bind() try: glVertexPointerd(d) glDrawElements(GL_LINE_LOOP, len(indices), GL_UNSIGNED_INT, indices) finally: d.unbind() lastPoint = array([[1.5, (1 / 255.) * float(x), 0]]) d[-2:-1] = lastPoint glFlush() pygame.display.flip() glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) time.sleep(0.001) finally: glDisableClientState(GL_VERTEX_ARRAY) # bug report from Dan Helfman, delete shouldn't cause # errors if called explicitly d.delete()
def test_vbo(self): """Test utility vbo wrapper""" import numpy from OpenGL.arrays import vbo assert vbo.get_implementation() dt = arraydatatype.GLdoubleArray array = numpy.array([ [0, 0, 0], [0, 1, 0], [1, .5, 0], [1, 0, 0], [1.5, .5, 0], [1.5, 0, 0], ], dtype='d') indices = numpy.array( range(len(array)), 'i', ) d = vbo.VBO(array) glDisable(GL_CULL_FACE) glNormal3f(0, 0, 1) glColor3f(1, 1, 1) glEnableClientState(GL_VERTEX_ARRAY) try: for x in range(1, 255, 10): d.bind() try: glVertexPointerd(d) glDrawElements(GL_LINE_LOOP, len(indices), GL_UNSIGNED_INT, indices) finally: d.unbind() lastPoint = numpy.array([[1.5, (1 / 255.) * float(x), 0]]) d[-2:-1] = lastPoint glFlush() pygame.display.flip() glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) time.sleep(0.001) finally: glDisableClientState(GL_VERTEX_ARRAY) # bug report from Dan Helfman, delete shouldn't cause # errors if called explicitly d.delete()
def drawCube(): """Draw a cube 2,2,2 units centered around the origin""" # draw six faces of a cube global VBO if VBO is None: if vbo.get_implementation(): data = vbo.VBO(array(list(box.yieldVertices((2, 2, 2))), 'f')) def draw(): data.bind() try: glPushClientAttrib(GL_CLIENT_ALL_ATTRIB_BITS) try: glEnable(GL_VERTEX_ARRAY) glEnable(GL_NORMAL_ARRAY) glEnable(GL_TEXTURE_COORD_ARRAY) glVertexPointer(3, GL_FLOAT, 32, data + 20) glNormalPointer(GL_FLOAT, 32, data + 8) glTexCoordPointer(2, GL_FLOAT, 32, data) glDrawArrays(GL_TRIANGLES, 0, 36) finally: glPopClientAttrib() finally: data.unbind() VBO = draw else: data = array(list(yieldVertices((2, 2, 2))), 'f') def draw(): try: glPushClientAttrib(GL_CLIENT_ALL_ATTRIB_BITS) try: # interleaved arrays is not 3.1 compatible, # but this is the old-code path... glInterleavedArrays(GL_T2F_N3F_V3F, 0, data) glDrawArrays(GL_TRIANGLES, 0, 36) finally: glPopClientAttrib() finally: data.unbind() VBO = draw return VBO()
def test_glbuffersubdata_numeric(self): from OpenGL.arrays import vbo assert vbo.get_implementation() points = np.array( [ [0,0,0], [0,1,0], [1,.5,0], [1,0,0], [1.5,.5,0], [1.5,0,0], ], dtype='f') d = vbo.VBO(points) with d: glBufferSubData( d.target, 12, 12, np.array([1,1,1],dtype='f'), )
def test_vbo( self ): """Test utility vbo wrapper""" import numpy from OpenGL.arrays import vbo assert vbo.get_implementation() dt = arraydatatype.GLdoubleArray array = numpy.array( [ [0,0,0], [0,1,0], [1,.5,0], [1,0,0], [1.5,.5,0], [1.5,0,0], ], dtype='d') indices = numpy.array( range(len(array)), 'i', ) d = vbo.VBO(array) glDisable( GL_CULL_FACE ) glNormal3f( 0,0,1 ) glColor3f( 1,1,1 ) glEnableClientState(GL_VERTEX_ARRAY) try: for x in range( 1, 255, 10 ): d.bind() try: glVertexPointerd( d ) glDrawElements( GL_LINE_LOOP, len(indices), GL_UNSIGNED_INT, indices ) finally: d.unbind() lastPoint = numpy.array( [[1.5,(1/255.) * float(x),0]] ) d[-2:-1] = lastPoint glFlush() pygame.display.flip() glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT ) time.sleep( 0.001 ) finally: glDisableClientState( GL_VERTEX_ARRAY ) # bug report from Dan Helfman, delete shouldn't cause # errors if called explicitly d.delete()
def compile(self, mode=None): """Compile the box as a display-list""" if vbo.get_implementation(): vb = vbo.VBO(array(list(yieldVertices(self.size)), 'f')) def draw(textured=True, lit=True): vb.bind() try: glPushClientAttrib(GL_CLIENT_ALL_ATTRIB_BITS) try: glEnableClientState(GL_VERTEX_ARRAY) if lit: glEnableClientState(GL_NORMAL_ARRAY) glNormalPointer(GL_FLOAT, 32, vb + 8) if textured: glEnableClientState(GL_TEXTURE_COORD_ARRAY) glTexCoordPointer(2, GL_FLOAT, 32, vb) glVertexPointer(3, GL_FLOAT, 32, vb + 20) glDrawArrays(GL_TRIANGLES, 0, 36) finally: glPopClientAttrib() finally: vb.unbind() else: vb = array(list(yieldVertices(self.size)), 'f') def draw(textured=True, lit=True): glPushClientAttrib(GL_CLIENT_ALL_ATTRIB_BITS) try: glInterleavedArrays(GL_T2F_N3F_V3F, 0, vb) glDrawArrays(GL_TRIANGLES, 0, 36) finally: glPopClientAttrib() holder = mode.cache.holder(self, draw) holder.depend(self, protofunctions.getField(self, 'size')) return draw
def __init__( self, vertexArray, # array of vertex coordinates to draw colorArray=None, # optional array of vertex colors normalArray=None, # optional array of normals textureCoordinateArray=None, # optional array of texture coordinates objectType=GL_TRIANGLES, # type of primitive, see glDrawArrays, allowed are: #GL_POINTS, GL_LINE_STRIP, GL_LINE_LOOP, GL_LINES, #GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN, GL_TRIANGLES, GL_QUAD_STRIP, #GL_QUADS, and GL_POLYGON startIndex=0, # the index from which to draw, see glDrawArrays count=-1, # by default, render the whole array (len(vertexArray)), see glDrawArrays ccw=1, # determines winding direction solid=1, # whether backspace culling may be enabled ): """Initialize the ArrayGeometry vertexArray -- array of vertex coordinates to draw colorArray = None -- optional array of vertex colors normalArray = None -- optional array of normals textureCoordinateArray = None -- optional array of texture coordinates objectType= GL_TRIANGLES -- type of primitive, see glDrawArrays. Allowed values are: GL_POINTS, GL_LINE_STRIP, GL_LINE_LOOP, GL_LINES, GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN, GL_TRIANGLES, GL_QUAD_STRIP, GL_QUADS, and GL_POLYGON though few of those save points, lines and triangles have actually been tested. Only triangles is currently used. startIndex = 0 -- the index from which to draw, see glDrawArrays count = -1 -- by default, render the whole array (len(vertexArray)), see glDrawArrays ccw = 1 -- determines winding direction see glFrontFace solid = 1 -- whether backspace culling should be enabled see glEnable( GL_CULL_FACE ) """ log.debug('New array geometry node') if FORCE_CONTIGUOUS: if vertexArray is not None and len(vertexArray): vertexArray = contiguous(vertexArray) if colorArray is not None and len(colorArray): colorArray = contiguous(colorArray) if normalArray is not None and len(normalArray): normalArray = contiguous(normalArray) if textureCoordinateArray is not None and len( textureCoordinateArray): textureCoordinateArray = contiguous(textureCoordinateArray) if vbo.get_implementation(): log.debug("VBO implementation available") if vertexArray is not None and len(vertexArray): vertexArray = vbo.VBO(vertexArray) if colorArray is not None and len(colorArray): colorArray = vbo.VBO(colorArray) if normalArray is not None and len(normalArray): normalArray = vbo.VBO(normalArray) if textureCoordinateArray is not None and len( textureCoordinateArray): textureCoordinateArray = vbo.VBO(textureCoordinateArray) if count < 0: count = len(vertexArray) self.vertices = vertexArray self.colours = colorArray self.normals = normalArray self.textures = textureCoordinateArray self.arguments = (objectType, startIndex, count) log.debug(' array geometry: %s, %s', self.arguments, len(self.vertices)) if ccw: self.ccw = GL_CCW else: self.ccw = GL_CW self.solid = solid
def __init__ ( self, vertexArray,# array of vertex coordinates to draw colorArray= None, # optional array of vertex colors normalArray= None, # optional array of normals textureCoordinateArray= None, # optional array of texture coordinates objectType= GL_TRIANGLES, # type of primitive, see glDrawArrays, allowed are: #GL_POINTS, GL_LINE_STRIP, GL_LINE_LOOP, GL_LINES, #GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN, GL_TRIANGLES, GL_QUAD_STRIP, #GL_QUADS, and GL_POLYGON startIndex = 0, # the index from which to draw, see glDrawArrays count = -1, # by default, render the whole array (len(vertexArray)), see glDrawArrays ccw = 1, # determines winding direction solid = 1, # whether backspace culling may be enabled ): """Initialize the ArrayGeometry vertexArray -- array of vertex coordinates to draw colorArray = None -- optional array of vertex colors normalArray = None -- optional array of normals textureCoordinateArray = None -- optional array of texture coordinates objectType= GL_TRIANGLES -- type of primitive, see glDrawArrays. Allowed values are: GL_POINTS, GL_LINE_STRIP, GL_LINE_LOOP, GL_LINES, GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN, GL_TRIANGLES, GL_QUAD_STRIP, GL_QUADS, and GL_POLYGON though few of those save points, lines and triangles have actually been tested. Only triangles is currently used. startIndex = 0 -- the index from which to draw, see glDrawArrays count = -1 -- by default, render the whole array (len(vertexArray)), see glDrawArrays ccw = 1 -- determines winding direction see glFrontFace solid = 1 -- whether backspace culling should be enabled see glEnable( GL_CULL_FACE ) """ log.debug( 'New array geometry node' ) if FORCE_CONTIGUOUS: if vertexArray is not None and len(vertexArray): vertexArray = contiguous( vertexArray ) if colorArray is not None and len(colorArray): colorArray = contiguous( colorArray ) if normalArray is not None and len(normalArray): normalArray = contiguous( normalArray ) if textureCoordinateArray is not None and len(textureCoordinateArray): textureCoordinateArray = contiguous( textureCoordinateArray ) if vbo.get_implementation(): log.debug( "VBO implementation available" ) if vertexArray is not None and len(vertexArray): vertexArray = vbo.VBO( vertexArray ) if colorArray is not None and len(colorArray): colorArray = vbo.VBO( colorArray ) if normalArray is not None and len(normalArray): normalArray = vbo.VBO( normalArray ) if textureCoordinateArray is not None and len(textureCoordinateArray): textureCoordinateArray = vbo.VBO( textureCoordinateArray ) if count < 0: count = len (vertexArray) self.vertices = vertexArray self.colours = colorArray self.normals = normalArray self.textures = textureCoordinateArray self.arguments = (objectType, startIndex,count) log.debug( ' array geometry: %s, %s', self.arguments, len(self.vertices) ) if ccw: self.ccw = GL_CCW else: self.ccw = GL_CW self.solid = solid
import time try: import numpy except: raise MissingModule("Numpy - you can get it from: http://sourceforge.net/projects/numpy/files/") try: from OpenGL.GL.EXT.framebuffer_object import * FBO_AVAILABLE = True except: FBO_AVAILABLE = False try: from OpenGL.arrays import vbo VBO_AVAILABLE = bool(vbo.get_implementation()) except: VBO_AVAILABLE = False try: from OpenGL.GL.EXT.texture_filter_anisotropic import * ANI_AVAILABLE = True except: ANI_AVAILABLE = False try: import Image as PIL TEX_ANI_AVAILABLE = True except: TEX_ANI_AVAILABLE = False print "PIL not found - animated textures not supported!"