def update_display(verts,tex_coords,texture=bird_texture): gl.glClearColor(0.2, 0.4, 0.5, 1.0) gl.glEnable(texture.target) gl.glBindTexture(texture.target, texture.id) gl.glPushAttrib(gl.GL_COLOR_BUFFER_BIT) gl.glEnable(gl.GL_ALPHA_TEST) gl.glAlphaFunc (gl.GL_GREATER, .1) #gl.glEnable(gl.GL_BLEND) #gl.glBlendFunc (gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) gl.glEnable(gl.GL_DEPTH_TEST) gl.glEnableClientState(gl.GL_VERTEX_ARRAY) gl.glEnableClientState(gl.GL_TEXTURE_COORD_ARRAY) n=len(verts[:]) #TODO verts._buffer.ctypes.data is awkward gl.glVertexPointer(3, vert_dtype.gl, 0, verts[:].ctypes.data) gl.glTexCoordPointer(3, tex_dtype.gl, 0, tex_coords[:].ctypes.data) gl.glDrawArrays(gl.GL_TRIANGLE_STRIP, 0, n) #unset state gl.glPopAttrib() gl.glDisable(texture.target)
def update_display(verts, tex_coords, texture=bird_texture): gl.glClearColor(0.2, 0.4, 0.5, 1.0) gl.glEnable(texture.target) gl.glBindTexture(texture.target, texture.id) gl.glPushAttrib(gl.GL_COLOR_BUFFER_BIT) gl.glEnable(gl.GL_ALPHA_TEST) gl.glAlphaFunc(gl.GL_GREATER, .1) #gl.glEnable(gl.GL_BLEND) #gl.glBlendFunc (gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) gl.glEnable(gl.GL_DEPTH_TEST) gl.glEnableClientState(gl.GL_VERTEX_ARRAY) gl.glEnableClientState(gl.GL_TEXTURE_COORD_ARRAY) n = len(verts[:]) #TODO verts._buffer.ctypes.data is awkward gl.glVertexPointer(3, vert_dtype.gl, 0, verts[:].ctypes.data) gl.glTexCoordPointer(3, tex_dtype.gl, 0, tex_coords[:].ctypes.data) gl.glDrawArrays(gl.GL_TRIANGLE_STRIP, 0, n) #unset state gl.glPopAttrib() gl.glDisable(texture.target)
def draw(self): """Draw the text to the back buffer""" # Border width self.box.setLineWidth( self.palette['lineWidth']) # Use 1 as base if border width is none #self.borderWidth = self.box.lineWidth # Border colour self.box.setLineColor(self.palette['lineColor'], colorSpace='rgb') #self.borderColor = self.box.lineColor # Background self.box.setFillColor(self.palette['fillColor'], colorSpace='rgb') #self.fillColor = self.box.fillColor if self._needVertexUpdate: #print("Updating vertices...") self._updateVertices() if self.fillColor is not None or self.borderColor is not None: self.box.draw() # self.boundingBox.draw() # could draw for debug purposes gl.glPushMatrix() self.win.setScale('pix') gl.glActiveTexture(gl.GL_TEXTURE0) gl.glBindTexture(gl.GL_TEXTURE_2D, self.glFont.textureID) gl.glEnable(gl.GL_TEXTURE_2D) gl.glDisable(gl.GL_DEPTH_TEST) gl.glEnableClientState(gl.GL_VERTEX_ARRAY) gl.glEnableClientState(gl.GL_COLOR_ARRAY) gl.glEnableClientState(gl.GL_TEXTURE_COORD_ARRAY) gl.glEnableClientState(gl.GL_VERTEX_ARRAY) gl.glVertexPointer(2, gl.GL_DOUBLE, 0, self.verticesPix.ctypes) gl.glColorPointer(4, gl.GL_DOUBLE, 0, self._colors.ctypes) gl.glTexCoordPointer(2, gl.GL_DOUBLE, 0, self._texcoords.ctypes) self.shader.bind() self.shader.setInt('texture', 0) self.shader.setFloat('pixel', [1.0 / 512, 1.0 / 512]) nVerts = len(self._text) * 4 gl.glDrawArrays(gl.GL_QUADS, 0, nVerts) self.shader.unbind() # removed the colors and font texture gl.glDisableClientState(gl.GL_COLOR_ARRAY) gl.glDisableClientState(gl.GL_TEXTURE_COORD_ARRAY) gl.glDisableVertexAttribArray(1) gl.glDisableClientState(gl.GL_VERTEX_ARRAY) gl.glActiveTexture(gl.GL_TEXTURE0) gl.glBindTexture(gl.GL_TEXTURE_2D, 0) gl.glDisable(gl.GL_TEXTURE_2D) if self.hasFocus: # draw caret line self.caret.draw() gl.glPopMatrix()
def draw_fallback(self): """Called instead of :meth:`draw` when quads are used instead of Point Sprite. """ self.make_delta_pos_to_vertex() self.update_vertexs_from_pos() self.update_per_vertex_colors() gl.glPushMatrix() self.transform() # color preserve - at least intel 945G needs that gl.glPushAttrib(gl.GL_CURRENT_BIT) gl.glEnable(gl.GL_TEXTURE_2D) gl.glBindTexture(gl.GL_TEXTURE_2D, self.texture.id) gl.glEnableClientState(gl.GL_VERTEX_ARRAY) vertexs_ptr = PointerToNumpy(self.vertexs) gl.glVertexPointer(2, gl.GL_FLOAT, 0, vertexs_ptr) gl.glEnableClientState(gl.GL_COLOR_ARRAY) color_ptr = PointerToNumpy(self.per_vertex_colors) # gl.glColorPointer(4, gl.GL_UNSIGNED_BYTE, 0, color_ptr) gl.glColorPointer(4, gl.GL_FLOAT, 0, color_ptr) gl.glEnableClientState(gl.GL_TEXTURE_COORD_ARRAY) tex_coord_ptr = PointerToNumpy(self.tex_coords) gl.glTexCoordPointer(2, gl.GL_FLOAT, 0, tex_coord_ptr) gl.glPushAttrib(gl.GL_COLOR_BUFFER_BIT) gl.glEnable(gl.GL_BLEND) if self.blend_additive: gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE) else: gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) gl.glDrawArrays(gl.GL_QUADS, 0, len(self.vertexs) * 4) # un -blend gl.glPopAttrib() # color restore gl.glPopAttrib() # disable states gl.glDisableClientState(gl.GL_TEXTURE_COORD_ARRAY) gl.glDisableClientState(gl.GL_COLOR_ARRAY) gl.glDisableClientState(gl.GL_VERTEX_ARRAY) gl.glDisable(gl.GL_TEXTURE_2D) gl.glPopMatrix()
def _render_rect_filled(shape, offset, texture_id, texture_coord_vbo): """ Render the rectangle at the right spot. """ # Set color GL.glLoadIdentity() GL.glTranslatef(shape.center_x, shape.center_y, 0) if shape.angle != 0: GL.glRotatef(shape.angle, 0, 0, 1) GL.glBindTexture(GL.GL_TEXTURE_2D, texture_id) GL.glTexCoordPointer(2, GL.GL_FLOAT, 0, texture_coord_vbo) GL.glDrawArrays(GL.GL_QUADS, offset, 4)
def _render_rect_filled(offset: int, texture_id: str, texture_coord_vbo: gl.GLuint, batch_count): """ Render the rectangle at the right spot. """ # Set color # gl.glLoadIdentity() # gl.glTranslatef(shape.center_x, shape.center_y, 0) # if shape.angle != 0: # gl.glRotatef(shape.angle, 0, 0, 1) gl.glBindTexture(gl.GL_TEXTURE_2D, texture_id) gl.glTexCoordPointer(2, gl.GL_FLOAT, 0, texture_coord_vbo) gl.glDrawArrays(gl.GL_QUADS, offset, batch_count)
def _render_rect_filled(shape: Sprite, offset: int, texture_id: str, texture_coord_vbo: gl.GLuint): """ Render the rectangle at the right spot. """ # Set color gl.glLoadIdentity() gl.glTranslatef(shape.center_x, shape.center_y, 0) if shape.angle != 0: gl.glRotatef(shape.angle, 0, 0, 1) gl.glBindTexture(gl.GL_TEXTURE_2D, texture_id) gl.glTexCoordPointer(2, gl.GL_FLOAT, 0, texture_coord_vbo) gl.glDrawArrays(gl.GL_QUADS, offset, 4)
def draw_arrays(primitive, verts=None, colors=None, tc0=None, tc1=None, tc2=None, tc3=None): states = [] if verts is not None: verts = ascont(verts) assert verts.ndim >= 2 and verts.shape[-1] <= 4 gl.glVertexPointer( verts.shape[-1], arrayToGLType(verts), verts.strides[-2], verts.ctypes.data) states.append( gl.GL_VERTEX_ARRAY ) if colors is not None: colors = ascont(colors) gl.glColorPointer( colors.shape[-1], arrayToGLType(colors), colors.strides[-2], colors.ctypes.data) states.append( gl.GL_COLOR_ARRAY ) protect = [] for i, tc in enumerate([tc0, tc1, tc2, tc3]): if tc is not None: tc = ascont(tc) protect.append(tc) gl.glClientActiveTexture(GL_TEXTURE0 + i) gl.glTexCoordPointer(tc.shape[-1], arrayToGLType(tc), tc.strides[-2], tc.ctypes.data) states.append( (GL_TEXTURE_COORD_ARRAY, i) ) with glstate(*states): gl.glDrawArrays( primitive, 0, np.prod(verts.shape[:-1]) )
def texture(self): gl.glBindBuffer(gl.GL_ARRAY_BUFFER, self.buffers["texture_coords"]) gl.glTexCoordPointer(3, gl.GL_FLOAT, 0, 0)
def bind(self): gl.glEnableClientState(self.type) gl.glBindBuffer(gl.GL_ARRAY_BUFFER, self.id) gl.glTexCoordPointer(2, gl.GL_FLOAT, 0, 0)
def createVAO(vertexBuffer, textureCoordBuffer=None, normalBuffer=None, colorBuffer=None): """Create a Vertex Array Object (VAO) with specified Vertex Buffer Objects. VAOs store buffer binding states, reducing binding overhead when drawing objects with vertext data stored in VBOs. Parameters ---------- vertexBuffer : :obj:`VertexBufferObject` Vertex buffer descriptor, must have 'bufferType' as GL_VERTEX_ARRAY. textureCoordBuffer : :obj:`VertexBufferObject` or None, optional Vertex buffer descriptor of texture coordinates, must have 'bufferType' as GL_TEXTURE_COORD_ARRAY. normalBuffer : :obj:`VertexBufferObject` or None, optional Vertex buffer descriptor of normals, must have 'bufferType' as GL_NORMAL_ARRAY. colorBuffer :obj:`VertexBufferObject` or None, optional Vertex buffer descriptor of colors, must have 'bufferType' as GL_COLOR_ARRAY. Returns ------- VertexArrayObject A descriptor with vertex buffer information. Examples -------- # create a VAO vaoDesc = createVAO(vboVerts, vboTexCoords, vboNormals) # draw the VAO, renders the mesh drawVAO(vaoDesc, GL.GL_TRIANGLES) """ # create a vertex buffer ID vaoId = GL.GLuint() GL.glGenVertexArrays(1, ctypes.byref(vaoId)) GL.glBindVertexArray(vaoId) # must have a vertex pointer assert vertexBuffer.bufferType == GL.GL_VERTEX_ARRAY # bind and set the vertex pointer, this is must be bound GL.glBindBuffer(GL.GL_ARRAY_BUFFER, vertexBuffer.id) GL.glVertexPointer(vertexBuffer.vertexSize, vertexBuffer.dtype, 0, None) GL.glEnableClientState(vertexBuffer.bufferType) # texture coordinates if textureCoordBuffer is not None: if vertexBuffer.indices != textureCoordBuffer.indices: raise RuntimeError( "Texture and vertex buffer indices do not match!") GL.glBindBuffer(GL.GL_ARRAY_BUFFER, textureCoordBuffer.id) GL.glTexCoordPointer(textureCoordBuffer.vertexSize, textureCoordBuffer.dtype, 0, None) GL.glEnableClientState(GL.GL_TEXTURE_COORD_ARRAY) # normals if normalBuffer is not None: if vertexBuffer.indices != normalBuffer.indices: raise RuntimeError( "Normal and vertex buffer indices do not match!") GL.glBindBuffer(GL.GL_ARRAY_BUFFER, normalBuffer.id) GL.glNormalPointer(normalBuffer.dtype, 0, None) GL.glEnableClientState(GL.GL_NORMAL_ARRAY) # colors if colorBuffer is not None: if vertexBuffer.indices != colorBuffer.indices: raise RuntimeError("Color and vertex buffer indices do not match!") GL.glBindBuffer(GL.GL_ARRAY_BUFFER, colorBuffer.id) GL.glColorPointer(colorBuffer.vertexSize, colorBuffer.dtype, 0, None) GL.glEnableClientState(GL.GL_COLOR_ARRAY) GL.glBindVertexArray(0) return VertexArrayObject(vaoId, vertexBuffer.indices, dict())
def drawVertexbuffers(vertexBuffer, *args, mode=GL.GL_TRIANGLES, flush=True): """Draw a vertex buffer using glDrawArrays. This method does not require shaders. Parameters ---------- vertexBuffer : :obj:`Vertexbuffer` Vertex buffer descriptor, must have 'bufferType' as GL_VERTEX_ARRAY. Optional vertex buffer descriptors can be passed as seperate arguments, they must have 'bufferTypes' as GL_TEXTURE_COORD_ARRAY, GL_NORMAL_ARRAY or GL_COLOR_ARRAY. mode : :obj:`int` Drawing mode to use (e.g. GL_TRIANGLES, GL_QUADS, GL_POINTS, etc.) flush : :obj:`bool` Flush queued drawing commands before returning. Returns ------- None Examples -------- # vertices of a triangle verts = [ 1.0, 1.0, 0.0, # v0 0.0, -1.0, 0.0, # v1 -1.0, 1.0, 0.0] # v2 # triangle vertex colors colors = [1.0, 0.0, 0.0, # v0 0.0, 1.0, 0.0, # v1 0.0, 0.0, 1.0] # v2 # load vertices to graphics device, return a descriptor vertexBuffer = createVertexbuffer(verts, 3) colorBuffer = createVertexbuffer(c, 3, GL.GL_COLOR_ARRAY) # draw the VBO drawVertexbuffer(vertexBuffer, colorBuffer, GL.GL_TRIANGLES) """ # must have a vertex pointer assert vertexBuffer.bufferType == GL.GL_VERTEX_ARRAY # bind and set the vertex pointer GL.glBindBuffer(GL.GL_ARRAY_BUFFER, vertexBuffer.id) GL.glVertexPointer(vertexBuffer.vertexSize, vertexBuffer.dtype, 0, None) GL.glEnableClientState(vertexBuffer.bufferType) # handle additional buffers if args: for buffer in args: # check if the number of indicies are the same if vertexBuffer.indices != buffer.indices: raise RuntimeError("Vertex buffer indices do not match!") GL.glBindBuffer(GL.GL_ARRAY_BUFFER, buffer.id) if buffer.bufferType == GL.GL_TEXTURE_COORD_ARRAY: GL.glTexCoordPointer(buffer.dtype, 0, None) elif buffer.bufferType == GL.GL_NORMAL_ARRAY: GL.glNormalPointer(buffer.dtype, 0, None) elif buffer.bufferType == GL.GL_COLOR_ARRAY: GL.glColorPointer(buffer.vertexSize, buffer.dtype, 0, None) GL.glEnableClientState(buffer.bufferType) GL.glDrawArrays(mode, 0, vertexBuffer.indices) # draw arrays # reset GL.glBindBuffer(GL.GL_ARRAY_BUFFER, 0) GL.glDisableClientState(vertexBuffer.bufferType) if args: for vbo in args: GL.glDisableClientState(vbo.bufferType) if flush: GL.glFlush()