def draw(self, transformation, **kwargs): transformation.scale(self.width, self.height, 1) color = self.color + (self.opacity,) gl.glColor4fv((gl.GLfloat * 4)(*color)) gl.glEnableClientState(gl.GL_VERTEX_ARRAY) gl.glVertexPointer(2, gl.GL_FLOAT, 0, self.vertices) gl.glDrawArrays(gl.GL_TRIANGLE_STRIP, 0, 4)
def on_draw(self): gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) gl.glMatrixMode(gl.GL_MODELVIEW) gl.glEnableClientState(gl.GL_VERTEX_ARRAY) if self.app.show_fps: self.fps_display.draw() self.app.on_draw()
def __init__(self): pts = generate_cyl_points().T colors = np.zeros_like(pts) colors[:,1]=1.0 # all green n_pts = len(pts) pts = map(float,pts.flat) # convert to flat list of floats colors = map(float, colors.flat) # Create ctypes arrays of the lists vertices = (gl.GLfloat * (n_pts*3))(*pts) colors = (gl.GLfloat * (n_pts*3))(*colors) # Create a list of triangle indices. indices = range(n_pts) indices = (gl.GLuint * n_pts)(*indices) # Compile a display list self.list = gl.glGenLists(1) gl.glNewList(self.list, gl.GL_COMPILE) gl.glPushClientAttrib(gl.GL_CLIENT_VERTEX_ARRAY_BIT) gl.glEnableClientState(gl.GL_VERTEX_ARRAY) gl.glVertexPointer(3, gl.GL_FLOAT, 0, vertices) gl.glEnableClientState(gl.GL_COLOR_ARRAY) gl.glColorPointer(3, gl.GL_FLOAT, 0, colors) gl.glDrawElements(gl.GL_LINES, len(indices), gl.GL_UNSIGNED_INT, indices) gl.glPopClientAttrib() gl.glEndList()
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_rects(shape_list, vertex_vbo_id, texture_coord_vbo_id): """ Draw a set of rectangles using vertex buffers. This is more efficient than drawing them individually. """ GL.glEnable(GL.GL_BLEND) GL.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA) GL.glEnable(GL.GL_TEXTURE_2D) GL.glHint(GL.GL_POLYGON_SMOOTH_HINT, GL.GL_NICEST) GL.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST) # GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT) # GL.glMatrixMode(GL.GL_MODELVIEW) # GL.glDisable(GL.GL_BLEND) GL.glBindBuffer(GL.GL_ARRAY_BUFFER, vertex_vbo_id) GL.glEnableClientState(GL.GL_TEXTURE_COORD_ARRAY) GL.glEnableClientState(GL.GL_VERTEX_ARRAY) GL.glVertexPointer(2, GL.GL_FLOAT, 0, 0) GL.glBindBuffer(GL.GL_ARRAY_BUFFER, texture_coord_vbo_id) offset = 0 for shape in shape_list: if shape.can_cache: texture_coord_vbo_id = None GL.glColor4f(1, 1, 1, shape.alpha) _render_rect_filled(shape, offset, shape.texture.texture_id, texture_coord_vbo_id) offset += 4 else: shape.draw()
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 on_draw(self): """ Render the screen. """ start = time.time() float_size = ctypes.sizeof(ctypes.c_float) record_len = 10 * float_size GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT) GL.glMatrixMode(GL.GL_MODELVIEW) GL.glEnableClientState(GL.GL_VERTEX_ARRAY) GL.glColor4ub(255, 0, 0, 255) GL.glBindBuffer(GL.GL_ARRAY_BUFFER, self.rect_vbo.vbo_id) GL.glVertexPointer(2, GL.GL_FLOAT, record_len, 0) for i in range(len(self.shape_list)): shape = self.shape_list[i] GL.glLoadIdentity() GL.glTranslatef(shape.x, shape.y, 0) GL.glDrawArrays(GL.GL_QUADS, i * 8, 8) # GL.glDrawArrays(GL.GL_QUADS, # 0, # self.rect_vbo.size) elapsed = time.time() - start print(elapsed)
def on_draw(self): """ Render the screen. """ start = time.time() GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT) GL.glMatrixMode(GL.GL_MODELVIEW) GL.glDisable(GL.GL_BLEND) GL.glBindBuffer(GL.GL_ARRAY_BUFFER, self.vertex_vbo_id) GL.glEnableClientState(GL.GL_VERTEX_ARRAY) GL.glVertexPointer(2, GL.GL_FLOAT, 0, 0) GL.glBindBuffer(GL.GL_ARRAY_BUFFER, self.color_vbo_id) GL.glEnableClientState(GL.GL_COLOR_ARRAY) GL.glColorPointer(3, GL.GL_FLOAT, 0, 0) offset = 0 for shape in self.shape_list: render_rect_filled(shape, offset) offset += 4 elapsed = time.time() - start print(elapsed)
def __init__(self): pts = generate_cyl_points().T colors = np.zeros_like(pts) colors[:,1]=1.0 # all green n_pts = len(pts) pts = map(float,pts.flat) # convert to flat list of floats colors = map(float, colors.flat) # Create ctypes arrays of the lists vertices = (gl.GLfloat * (n_pts*3))(*pts) colors = (gl.GLfloat * (n_pts*3))(*colors) # Create a list of triangle indices. indices = range(n_pts) indices = (gl.GLuint * n_pts)(*indices) # Compile a display list self.list = gl.glGenLists(1) gl.glNewList(self.list, gl.GL_COMPILE) gl.glPushClientAttrib(gl.GL_CLIENT_VERTEX_ARRAY_BIT) gl.glEnableClientState(gl.GL_VERTEX_ARRAY) gl.glVertexPointer(3, gl.GL_FLOAT, 0, vertices) gl.glEnableClientState(gl.GL_COLOR_ARRAY) gl.glColorPointer(3, gl.GL_FLOAT, 0, colors) gl.glDrawElements(gl.GL_POINTS, len(indices), gl.GL_UNSIGNED_INT, indices) gl.glPopClientAttrib() gl.glEndList()
def draw_bounding_box(self, x, y, screen_height): bb = self.get_bounding_box() bb = [bb['min_x'], bb['min_y'], bb['max_x'], bb['max_y']] vertices = () for _ in bb: vertices += (_.x,) vertices += (screen_height - _.y,) # get opengl vertices of type GLfloat vertices_gl = (GLfloat * len(vertices))(*vertices) # set the color glColor4ub(0, 255, 0, 255); glPolygonMode( GL_FRONT_AND_BACK, GL_LINE ); # turn on blend for alpha channel glEnable(GL_BLEND) # tell open GL were passing a vertex array glEnableClientState(GL_VERTEX_ARRAY) # create a pointer to vertices_gl glVertexPointer(2, GL_FLOAT, 0, vertices_gl) # draw the array glDrawArrays(GL_POLYGON, 0, len(vertices) // 2) glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
def start_render(): """ Get set up to render. Required to be called before drawing anything to the screen. """ gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) gl.glMatrixMode(gl.GL_MODELVIEW) gl.glEnableClientState(gl.GL_VERTEX_ARRAY)
def initializeGL(self): gl.glClearColor(0, 0, 0, 0) gl.glPointSize(10.0) gl.glEnable(gl.GL_POINT_SMOOTH) gl.glEnable(gl.GL_LINE_SMOOTH) gl.glEnable(gl.GL_BLEND) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) gl.glEnableClientState(gl.GL_VERTEX_ARRAY)
def __init__(self, radius, inner_radius, slices, inner_slices): # Create the vertex and normal arrays. vertices = [] normals = [] u_step = 2 * math.pi / (slices - 1) v_step = 2 * math.pi / (inner_slices - 1) u = 0. for i in range(slices): cos_u = math.cos(u) sin_u = math.sin(u) v = 0. for j in range(inner_slices): cos_v = math.cos(v) sin_v = math.sin(v) d = (radius + inner_radius * cos_v) x = d * cos_u y = d * sin_u z = inner_radius * sin_v nx = cos_u * cos_v ny = sin_u * cos_v nz = sin_v vertices.extend([x, y, z]) normals.extend([nx, ny, nz]) v += v_step u += u_step # Create ctypes arrays of the lists vertices = (gl.GLfloat * len(vertices))(*vertices) normals = (gl.GLfloat * len(normals))(*normals) # Create a list of triangle indices. indices = [] for i in range(slices - 1): for j in range(inner_slices - 1): p = i * inner_slices + j indices.extend([p, p + inner_slices, p + inner_slices + 1]) indices.extend([p, p + inner_slices + 1, p + 1]) indices = (gl.GLuint * len(indices))(*indices) # Compile a display list self.list = gl.glGenLists(1) gl.glNewList(self.list, gl.GL_COMPILE) gl.glPushClientAttrib(gl.GL_CLIENT_VERTEX_ARRAY_BIT) gl.glEnableClientState(gl.GL_VERTEX_ARRAY) gl.glEnableClientState(gl.GL_NORMAL_ARRAY) gl.glVertexPointer(3, gl.GL_FLOAT, 0, vertices) gl.glNormalPointer(gl.GL_FLOAT, 0, normals) gl.glDrawElements(gl.GL_TRIANGLES, len(indices), gl.GL_UNSIGNED_INT, indices) gl.glPopClientAttrib() gl.glEndList()
def __init__(self, radius, inner_radius, slices, inner_slices): # Create the vertex and normal arrays. vertices = [] normals = [] u_step = 2 * pi / (slices - 1) v_step = 2 * pi / (inner_slices - 1) u = 0. for i in range(slices): cos_u = cos(u) sin_u = sin(u) v = 0. for j in range(inner_slices): cos_v = cos(v) sin_v = sin(v) d = (radius + inner_radius * cos_v) x = d * cos_u y = d * sin_u z = inner_radius * sin_v nx = cos_u * cos_v ny = sin_u * cos_v nz = sin_v vertices.extend([x, y, z]) normals.extend([nx, ny, nz]) v += v_step u += u_step # Create ctypes arrays of the lists vertices = (GLfloat * len(vertices))(*vertices) normals = (GLfloat * len(normals))(*normals) # Create a list of triangle indices. indices = [] for i in range(slices - 1): for j in range(inner_slices - 1): p = i * inner_slices + j indices.extend([p, p + inner_slices, p + inner_slices + 1]) indices.extend([p, p + inner_slices + 1, p + 1]) indices = (GLuint * len(indices))(*indices) # Compile a display list self.list = glGenLists(1) glNewList(self.list, GL_COMPILE) glPushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT) glEnableClientState(GL_VERTEX_ARRAY) glEnableClientState(GL_NORMAL_ARRAY) glVertexPointer(3, GL_FLOAT, 0, vertices) glNormalPointer(GL_FLOAT, 0, normals) glDrawElements(GL_TRIANGLES, len(indices), GL_UNSIGNED_INT, indices) glPopClientAttrib() glEndList()
def render(shape): gl.glClear(gl.GL_COLOR_BUFFER_BIT) gl.glLoadIdentity() gl.glEnableClientState(gl.GL_VERTEX_ARRAY) gl.glEnableClientState(gl.GL_COLOR_ARRAY) gl.glBindBuffer(gl.GL_ARRAY_BUFFER, shape.vbo_id) gl.glVertexPointer(2, gl.GL_FLOAT, 0, None) gl.glBindBuffer(gl.GL_ARRAY_BUFFER, shape.vbo_color_id) gl.glColorPointer(4, gl.GL_FLOAT, 0, None) gl.glDrawArrays(shape.draw_mode, 0, shape.size)
def display(self, mode_2d=False): glPushMatrix() glTranslatef(self.offset_x, self.offset_y, 0) glEnableClientState(GL_VERTEX_ARRAY) glEnableClientState(GL_COLOR_ARRAY) self._display_movements(mode_2d) glDisableClientState(GL_COLOR_ARRAY) glDisableClientState(GL_VERTEX_ARRAY) glPopMatrix()
def __enter__(self): for idx in self.state: texuint = 0 if hasattr(idx, "__len__"): texunit = idx[1] idx = idx[0] if idx in self.ClientState: if idx == gl.GL_TEXTURE_COORD_ARRAY: gl.glClientActiveTexture(GL_TEXTURE0 + texunit) gl.glEnableClientState(idx) else: gl.glEnable(idx)
def draw(self): gl.glClearColor(0.2, 0.4, 0.5, 1.0) gl.glBlendFunc (gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) gl.glEnable (gl.GL_BLEND) gl.glEnable (gl.GL_LINE_SMOOTH); gl.glEnableClientState(gl.GL_VERTEX_ARRAY) gl.glEnableClientState(gl.GL_COLOR_ARRAY) #TODO verts._buffer.ctypes.data is awkward gl.glVertexPointer(2, self.vert_dtype.gl, 0, self.verts._buffer.ctypes.data) gl.glColorPointer(3, self.color_dtype.gl, 0, self.colors._buffer.ctypes.data) gl.glDrawArrays(gl.GL_TRIANGLE_STRIP, 0, len(self.verts._buffer)//2)
def update_display(render_verts, colors): gl.glClearColor(0.2, 0.4, 0.5, 1.0) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) gl.glEnable(gl.GL_BLEND) gl.glEnable(gl.GL_LINE_SMOOTH) gl.glEnableClientState(gl.GL_VERTEX_ARRAY) gl.glEnableClientState(gl.GL_COLOR_ARRAY) n = len(render_verts[:]) # TODO verts._buffer.ctypes.data is awkward gl.glVertexPointer(3, vert_dtype.gl, 0, render_verts[:].ctypes.data) gl.glColorPointer(3, color_dtype.gl, 0, colors[:].ctypes.data) gl.glDrawArrays(gl.GL_TRIANGLE_STRIP, 0, n)
def update_display(render_verts, colors): gl.glClearColor(0.2, 0.4, 0.5, 1.0) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) gl.glEnable(gl.GL_BLEND) gl.glEnable(gl.GL_LINE_SMOOTH) gl.glEnableClientState(gl.GL_VERTEX_ARRAY) gl.glEnableClientState(gl.GL_COLOR_ARRAY) n = len(render_verts[:]) #TODO verts._buffer.ctypes.data is awkward gl.glVertexPointer(3, vert_dtype.gl, 0, render_verts[:].ctypes.data) gl.glColorPointer(3, color_dtype.gl, 0, colors[:].ctypes.data) gl.glDrawArrays(gl.GL_TRIANGLE_STRIP, 0, n)
def draw_hud(self): ''' Draw any display items overlaid on the world, such as FPS counter ''' self.projection.set_screen() self.modelview.set_identity() gl.glEnableClientState(gl.GL_VERTEX_ARRAY) gl.glEnableClientState(gl.GL_COLOR_ARRAY) self.clock_display.draw() gl.glDisableClientState(gl.GL_VERTEX_ARRAY) gl.glDisableClientState(gl.GL_COLOR_ARRAY)
def draw(verts,colors): '''draw the numpy arrays `verts` and `colors`.''' gl.glClearColor(0.2, 0.4, 0.5, 1.0) gl.glBlendFunc (gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) gl.glEnable (gl.GL_BLEND) gl.glEnable (gl.GL_LINE_SMOOTH); gl.glEnableClientState(gl.GL_VERTEX_ARRAY) gl.glEnableClientState(gl.GL_COLOR_ARRAY) gl.glVertexPointer(2, vert_dtype.gl_type, 0, verts.ctypes.data) gl.glColorPointer(3, color_dtype.gl_type, 0, colors.ctypes.data) gl.glDrawArrays(gl.GL_TRIANGLE_STRIP, 0, len(verts)//2) fps_display.draw()
def draw(verts, colors): '''draw the numpy arrays `verts` and `colors`.''' gl.glClearColor(0.2, 0.4, 0.5, 1.0) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) gl.glEnable(gl.GL_BLEND) gl.glEnable(gl.GL_LINE_SMOOTH) gl.glEnableClientState(gl.GL_VERTEX_ARRAY) gl.glEnableClientState(gl.GL_COLOR_ARRAY) gl.glVertexPointer(2, vert_dtype.gl_type, 0, verts.ctypes.data) gl.glColorPointer(3, color_dtype.gl_type, 0, colors.ctypes.data) gl.glDrawArrays(gl.GL_TRIANGLE_STRIP, 0, len(verts) // 2) fps_display.draw()
def render_points(self): renderPoints = [] for point in self.points: renderPoints.extend(point.position.vector) self.ctPoints = (gl.GLfloat * len(renderPoints))(*renderPoints) point_ptr = ct.cast(self.ctPoints, ct.c_void_p) gl.glColor3f(1.0, 1.0, 1.0) gl.glEnableClientState(gl.GL_VERTEX_ARRAY) gl.glVertexPointer(2, gl.GL_FLOAT, 0, point_ptr) gl.glDrawArrays(gl.GL_POINTS, 0, len(renderPoints)//2) gl.glDisableClientState(gl.GL_VERTEX_ARRAY)
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 drawlines( self, verts, colors, idxs ): 'helper to draw lines from numpy arrays of verts/colors/indexes' vptr = vecutil.numpy2pointer(verts) iptr = vecutil.numpy2pointer(idxs) if colors is not None: cptr = vecutil.numpy2pointer(colors) gl.glEnableClientState(gl.GL_COLOR_ARRAY) gl.glColorPointer(3, gl.GL_FLOAT, 0, cptr) gl.glEnableClientState(gl.GL_VERTEX_ARRAY) gl.glVertexPointer(3, gl.GL_FLOAT, 0, vptr) gl.glDrawElements(gl.GL_LINES, len(idxs), gl.GL_UNSIGNED_INT, iptr) gl.glDisableClientState(gl.GL_VERTEX_ARRAY) gl.glDisableClientState(gl.GL_COLOR_ARRAY)
def on_draw(self): """ Render the screen. """ start = time.time() GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT) GL.glMatrixMode(GL.GL_MODELVIEW) GL.glEnableClientState(GL.GL_VERTEX_ARRAY) for shape in self.shape_list: shape.draw() elapsed = time.time() - start print(elapsed)
def start_render(): """ Get set up to render. Required to be called before drawing anything to the screen. Args: None Returns: None Raises: None """ GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT) GL.glMatrixMode(GL.GL_MODELVIEW) GL.glEnableClientState(GL.GL_VERTEX_ARRAY)
def render_road(self): if self.initialized < 3: vertices = [] colors = [] def vert(a, b, c): for v in (a, b, c): vertices.append(v) for c in color: colors.append(c) color = (0.4, 0.8, 0.4, 1.0) vert(-PLAYFIELD, +PLAYFIELD, 0) vert(+PLAYFIELD, +PLAYFIELD, 0) vert(+PLAYFIELD, -PLAYFIELD, 0) vert(-PLAYFIELD, -PLAYFIELD, 0) color = (0.4, 0.9, 0.4, 1.0) k = PLAYFIELD / 20.0 for x in range(-20, 20, 2): for y in range(-20, 20, 2): vert(k * x + k, k * y + 0, 0) vert(k * x + 0, k * y + 0, 0) vert(k * x + 0, k * y + k, 0) vert(k * x + k, k * y + k, 0) for poly, col in self.road_poly: color = (col[0], col[1], col[2], 1) for p in poly: vert(p[0], p[1], 0) self.initialized += 1 gl.glEnableClientState(gl.GL_VERTEX_ARRAY) gl_vertices = (gl.GLfloat * len(vertices))(*vertices) gl.glBindBuffer(gl.GL_ARRAY_BUFFER_ARB, self.vertex_vbo_id) gl.glBufferData(gl.GL_ARRAY_BUFFER_ARB, len(vertices) * 4, gl_vertices, gl.GL_STATIC_DRAW) gl_colors = (gl.GLfloat * len(colors))(*colors) gl.glBindBuffer(gl.GL_ARRAY_BUFFER_ARB, self.color_vbo_id) gl.glBufferData(gl.GL_ARRAY_BUFFER_ARB, len(colors) * 4, gl_colors, gl.GL_STATIC_DRAW) self.numdraw = len(vertices) // 3 gl.glBindBuffer(gl.GL_ARRAY_BUFFER_ARB, self.vertex_vbo_id) gl.glVertexPointer(3, gl.GL_FLOAT, 0, 0) gl.glEnableClientState(gl.GL_COLOR_ARRAY) gl.glBindBuffer(gl.GL_ARRAY_BUFFER_ARB, self.color_vbo_id) gl.glColorPointer(4, gl.GL_FLOAT, 0, 0) gl.glDrawArrays(gl.GL_QUADS, 0, self.numdraw)
def draw(self): """ Draw everything in the list. """ # gl.glClear(gl.GL_COLOR_BUFFER_BIT) gl.glEnableClientState(gl.GL_VERTEX_ARRAY) gl.glLoadIdentity() gl.glTranslatef(self.center_x, self.center_y, 0) if self.angle: gl.glRotatef(self.angle, 0, 0, 1) last_color = None last_line_width = None gl.glEnable(gl.GL_BLEND) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) gl.glEnable(gl.GL_LINE_SMOOTH) gl.glHint(gl.GL_LINE_SMOOTH_HINT, gl.GL_NICEST) gl.glHint(gl.GL_POLYGON_SMOOTH_HINT, gl.GL_NICEST) for shape in self.shape_list: if shape.vbo_color_id is not None: gl.glEnableClientState(gl.GL_COLOR_ARRAY) else: gl.glDisableClientState(gl.GL_COLOR_ARRAY) if last_color is None or last_color != shape.color: last_color = shape.color if len(shape.color) == 4: gl.glColor4ub(shape.color[0], shape.color[1], shape.color[2], shape.color[3]) gl.glEnable(gl.GL_BLEND) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) elif len(shape.color) == 3: gl.glDisable(gl.GL_BLEND) gl.glColor4ub(shape.color[0], shape.color[1], shape.color[2], 255) if shape.line_width and last_line_width != shape.line_width: last_line_width = shape.line_width gl.glLineWidth(shape.line_width) if shape.vbo_color_id is None: stripped_render(shape) else: stripped_render_with_colors(shape)
def display(self, mode_2d=False): glPushMatrix() glTranslatef(self.offset_x, self.offset_y, 0) glEnableClientState(GL_VERTEX_ARRAY) has_vbo = isinstance(self.vertex_buffer, VertexBufferObject) self._display_travels(has_vbo) glEnableClientState(GL_COLOR_ARRAY) self._display_movements(has_vbo) glDisableClientState(GL_COLOR_ARRAY) glDisableClientState(GL_VERTEX_ARRAY) glPopMatrix()
def on_draw(self): """ Render the screen. """ start = time.time() GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT) GL.glMatrixMode(GL.GL_MODELVIEW) GL.glEnableClientState(GL.GL_VERTEX_ARRAY) offset = 0 for shape in self.shape_list: render_rect_filled(self.vbo.vbo_id, shape, offset) offset += 4 elapsed = time.time() - start print(elapsed)
def draw_hud(self, items): ''' Draw any display items overlaid on the world, such as FPS counter ''' self.camera.hud_projection() rabbyt.render_unsorted( items ) gl.glEnableClientState(gl.GL_VERTEX_ARRAY) gl.glEnableClientState(gl.GL_COLOR_ARRAY) if self.options.fps: if self.clock_display is None: self.clock_display = clock.ClockDisplay() self.clock_display.draw() gl.glDisableClientState(gl.GL_VERTEX_ARRAY) gl.glDisableClientState(gl.GL_COLOR_ARRAY)
def on_draw(self): """ Render the screen. """ # This command has to happen before we start drawing gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) gl.glMatrixMode(gl.GL_MODELVIEW) gl.glEnableClientState(gl.GL_VERTEX_ARRAY) gl.glLoadIdentity() gl.glTranslatef(self.center_x, self.center_y, 0) for shape in self.shape_list: gl.glVertexPointer(2, gl.GL_FLOAT, 0, 0) gl.glBindBuffer(gl.GL_ARRAY_BUFFER, shape.vbo_id) gl.glDrawArrays(gl.GL_QUADS, 0, shape.size) gl.glColor4ub(shape.color[0], shape.color[1], shape.color[2], 255)
def draw(self, offset_x, offset_y, screen_height): vertices = self.get_screen_relative_vertices(offset_x, offset_y, screen_height) # get opengl vertices of type GLfloat vertices_gl = (GLfloat * len(vertices))(*vertices) # set the color glColor4ub(*self.color); # turn on blend for alpha channel glEnable(GL_BLEND) # tell open GL were passing a vertex array glEnableClientState(GL_VERTEX_ARRAY) # create a pointer to vertices_gl glVertexPointer(2, GL_FLOAT, 0, vertices_gl) # draw the array glDrawArrays(GL_POLYGON, 0, len(vertices) // 2)
def _draw_rects(shape_list: Iterable[Sprite], vertex_vbo_id: gl.GLuint, texture_coord_vbo_id: gl.GLuint): """ Draw a set of rectangles using vertex buffers. This is more efficient than drawing them individually. """ gl.glEnable(gl.GL_BLEND) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) gl.glEnable(gl.GL_TEXTURE_2D) # As soon as this happens, can't use drawing commands gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_WRAP_S, gl.GL_CLAMP_TO_EDGE) gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_WRAP_T, gl.GL_CLAMP_TO_EDGE) gl.glTexParameterf(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MIN_FILTER, gl.GL_NEAREST) gl.glTexParameterf(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER, gl.GL_NEAREST) gl.glHint(gl.GL_POLYGON_SMOOTH_HINT, gl.GL_NICEST) gl.glHint(gl.GL_PERSPECTIVE_CORRECTION_HINT, gl.GL_NICEST) # gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) # gl.glMatrixMode(gl.GL_MODELVIEW) # gl.glDisable(gl.GL_BLEND) gl.glBindBuffer(gl.GL_ARRAY_BUFFER, vertex_vbo_id) gl.glEnableClientState(gl.GL_TEXTURE_COORD_ARRAY) gl.glEnableClientState(gl.GL_VERTEX_ARRAY) gl.glVertexPointer(2, gl.GL_FLOAT, 0, 0) gl.glBindBuffer(gl.GL_ARRAY_BUFFER, texture_coord_vbo_id) offset = 0 for shape in shape_list: if shape.can_cache: texture_coord_vbo_id = None gl.glColor4f(1, 1, 1, shape.alpha) _render_rect_filled(shape, offset, shape.texture.texture_id, texture_coord_vbo_id) offset += 4 else: shape.draw() gl.glDisable(gl.GL_TEXTURE_2D)
def paintGL(self): gl.glClear(gl.GL_COLOR_BUFFER_BIT) gl.glLoadIdentity() #draw lines gl.glEnableClientState(gl.GL_COLOR_ARRAY) self.lineVerts = (self.spl.segments + self.centre).flatten() self.lineVerts_gl = (gl.GLfloat * len(self.lineVerts))(*self.lineVerts) self.lineColors = (self.spl.segColor).flatten() self.lineColors_gl = (gl.GLfloat * len(self.lineColors))(*self.lineColors) gl.glVertexPointer(2, gl.GL_FLOAT, 0, self.lineVerts_gl) gl.glColorPointer(4, gl.GL_FLOAT, 0, self.lineColors_gl) gl.glDrawArrays(gl.GL_LINES, 0, len(self.lineVerts) // 2) gl.glDisableClientState(gl.GL_COLOR_ARRAY) #draw points gl.glColor3f(1.0, 1.0, 1.0) vertPoints = (self.spl.state[:, :2] + self.centre).flatten().astype( ctypes.c_float) vertices_gl = vertPoints.ctypes.data_as(ctypes.POINTER(ctypes.c_float)) gl.glVertexPointer(2, gl.GL_FLOAT, 0, vertices_gl) gl.glDrawArrays(gl.GL_POINTS, 0, len(vertPoints) // 2) #overdraw selected point if self.spl.clicked != -1: gl.glColor3f(1.0, 0.0, 0.0) point = (self.spl.clickedState[:2] + self.centre).astype( ctypes.c_float) point_gl = point.ctypes.data_as(ctypes.POINTER(ctypes.c_float)) gl.glVertexPointer(2, gl.GL_FLOAT, 0, point_gl) gl.glDrawArrays(gl.GL_POINTS, 0, 1) pyglet.text.Label("Epoch: %s" % self.secStruct.epochCount, x=5, y=10, font_name='Times New Roman', font_size=20).draw()
def display(self, mode_2d=False): with self.lock: glPushMatrix() glTranslatef(self.offset_x, self.offset_y, 0) glEnableClientState(GL_VERTEX_ARRAY) has_vbo = isinstance(self.vertex_buffer, VertexBufferObject) if self.display_travels: self._display_travels(has_vbo) glEnable(GL_LIGHTING) glEnableClientState(GL_NORMAL_ARRAY) glEnableClientState(GL_COLOR_ARRAY) glMaterialfv(GL_FRONT, GL_SPECULAR, vec(1, 1, 1, 1)) glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, vec(0, 0, 0, 0)) glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 50) glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE) self._display_movements(has_vbo) glDisable(GL_LIGHTING) glDisableClientState(GL_COLOR_ARRAY) glDisableClientState(GL_VERTEX_ARRAY) glDisableClientState(GL_NORMAL_ARRAY) glPopMatrix()
def drawnvc( self, normals, points, colors, idxs ): '''draw tri mesh using glDrawElements using input normals points colors and indexes''' n = 1 for dim in idxs.shape: n *= dim iptr = vecutil.numpy2pointer(idxs) nptr = vecutil.numpy2pointer(normals) vptr = vecutil.numpy2pointer(points) cptr = vecutil.numpy2pointer(colors) mode = self.ui.fillmodes[self.ui.fillmode] gl.glPolygonMode( gl.GL_FRONT, mode ) gl.glEnableClientState(gl.GL_VERTEX_ARRAY) gl.glEnableClientState(gl.GL_NORMAL_ARRAY) gl.glEnableClientState(gl.GL_COLOR_ARRAY) gl.glVertexPointer(3, gl.GL_FLOAT, 0, vptr) gl.glNormalPointer( gl.GL_FLOAT, 0, nptr) gl.glColorPointer(3, gl.GL_FLOAT, 0, cptr) gl.glDrawElements(gl.gl.GL_TRIANGLES, n, gl.GL_UNSIGNED_INT, iptr) gl.glDisableClientState(gl.GL_VERTEX_ARRAY) gl.glDisableClientState(gl.GL_NORMAL_ARRAY) gl.glDisableClientState(gl.GL_COLOR_ARRAY) gl.glPolygonMode( gl.GL_FRONT, gl.GL_FILL )
def draw_background(self): varray = [] carray = [] x = self.top_left['x'] y = self.top_left['y'] for _ in self.background: basePosition = _[0] depth = _[1] color = _[2] #parallax scrolling and wrapping realPosition = (basePosition[0] + x * depth, basePosition[1] + y * depth) wrappedPosition = ( realPosition[0] % self.background_width, realPosition[1] % self.background_height) varray += [wrappedPosition[0], wrappedPosition[1], 0] carray += color # # # needs to be commented and understood.. # glEnable(GL_BLEND) # glEnable(GL_VERTEX_PROGRAM_POINT_SIZE) # point_size = GLfloat(10.0) # glGetFloatv(GL_POINT_SIZE_MAX_ARB, point_size) # glPointSize(point_size) # glPointParameterfvARB(GL_POINT_DISTANCE_ATTENUATION_ARB, (GLfloat * 3)(0, 0, 5)) # glPointParameterfARB(GL_POINT_SIZE_MIN_ARB, 5) varray = (GLfloat * len(varray))(*varray) carray = (GLfloat * len(carray))(*carray) glVertexPointer(3, GL_FLOAT, 0, varray) glColorPointer(4, GL_FLOAT, 0, carray) glEnableClientState(GL_VERTEX_ARRAY) glEnableClientState(GL_COLOR_ARRAY) glDrawArrays(GL_POINTS, 0, len(varray)//3) glDisableClientState(GL_VERTEX_ARRAY); glDisableClientState(GL_COLOR_ARRAY);
def _draw_rects(shape_list: Iterable[Sprite], vertex_vbo_id: gl.GLuint, texture_coord_vbo_id: gl.GLuint): """ Draw a set of rectangles using vertex buffers. This is more efficient than drawing them individually. """ gl.glEnable(gl.GL_BLEND) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) gl.glEnable(gl.GL_TEXTURE_2D ) # As soon as this happens, can't use drawing commands gl.glHint(gl.GL_POLYGON_SMOOTH_HINT, gl.GL_NICEST) gl.glHint(gl.GL_PERSPECTIVE_CORRECTION_HINT, gl.GL_NICEST) # gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) # gl.glMatrixMode(gl.GL_MODELVIEW) # gl.glDisable(gl.GL_BLEND) gl.glBindBuffer(gl.GL_ARRAY_BUFFER, vertex_vbo_id) gl.glEnableClientState(gl.GL_TEXTURE_COORD_ARRAY) gl.glEnableClientState(gl.GL_VERTEX_ARRAY) gl.glVertexPointer(2, gl.GL_FLOAT, 0, 0) gl.glBindBuffer(gl.GL_ARRAY_BUFFER, texture_coord_vbo_id) offset = 0 for shape in shape_list: if shape.can_cache: texture_coord_vbo_id = None gl.glColor4f(1, 1, 1, shape.alpha) _render_rect_filled(shape, offset, shape.texture.texture_id, texture_coord_vbo_id) offset += 4 else: shape.draw() gl.glDisable(gl.GL_TEXTURE_2D)
def render_bounding_box(self): gl.glLineWidth(1.0) self.ctPoints = self.ctPointT( self.rect.min.x, self.rect.min.y, self.rect.max.x, self.rect.min.y, self.rect.max.x, self.rect.min.y, self.rect.max.x, self.rect.max.y, self.rect.max.x, self.rect.max.y, self.rect.min.x, self.rect.max.y, self.rect.min.x, self.rect.max.y, self.rect.min.x, self.rect.min.y, ) point_ptr = ct.cast(self.ctPoints, ct.c_void_p) gl.glColor3f(*self.bbColor) gl.glEnableClientState(gl.GL_VERTEX_ARRAY) gl.glVertexPointer(2, gl.GL_FLOAT, 0, point_ptr) gl.glDrawArrays(gl.GL_LINES, 0, 8) gl.glDisableClientState(gl.GL_VERTEX_ARRAY)
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 draw(self): if self.buffers["vertex"] is not None: gl.glEnableClientState(gl.GL_VERTEX_ARRAY) self.vertex() if self.buffers["color"] is not None: gl.glEnableClientState(gl.GL_COLOR_ARRAY) self.color() if self.buffers["normal"] is not None: gl.glEnableClientState(gl.GL_NORMAL_ARRAY) self.normal() if self.buffers["texture_coords"] is not None: gl.glEnableClientState(gl.GL_TEXTURE_COORD_ARRAY) gl.glEnable(gl.GL_TEXTURE_2D) gl.glBindTexture(texture.target, texture.id) self.texture() gl.glDrawArrays(gl.GL_QUADS, 0, self.vertex_count)
def _display_movements(self, mode_2d=False): self.vertex_buffer.bind() has_vbo = isinstance(self.vertex_buffer, VertexBufferObject) if has_vbo: glVertexPointer(3, GL_FLOAT, 0, None) else: glVertexPointer(3, GL_FLOAT, 0, self.vertex_buffer.ptr) self.vertex_color_buffer.bind() if has_vbo: glColorPointer(4, GL_FLOAT, 0, None) else: glColorPointer(4, GL_FLOAT, 0, self.vertex_color_buffer.ptr) start = 0 if self.num_layers_to_draw <= self.max_layers: end_prev_layer = self.layer_stops[self.num_layers_to_draw - 1] else: end_prev_layer = -1 end = self.layer_stops[min(self.num_layers_to_draw, self.max_layers)] glDisableClientState(GL_COLOR_ARRAY) glColor4f(*self.color_printed) # Draw printed stuff until end or end_prev_layer cur_end = min(self.printed_until, end) if not self.only_current: if 0 <= end_prev_layer <= cur_end: glDrawArrays(GL_LINES, start, end_prev_layer) elif cur_end >= 0: glDrawArrays(GL_LINES, start, cur_end) glEnableClientState(GL_COLOR_ARRAY) # Draw nonprinted stuff until end_prev_layer start = max(cur_end, 0) if end_prev_layer >= start: if not self.only_current: glDrawArrays(GL_LINES, start, end_prev_layer - start) cur_end = end_prev_layer # Draw current layer if end_prev_layer >= 0: glDisableClientState(GL_COLOR_ARRAY) # Backup & increase line width orig_linewidth = (GLfloat)() glGetFloatv(GL_LINE_WIDTH, orig_linewidth) glLineWidth(2.0) glColor4f(*self.color_current_printed) if cur_end > end_prev_layer: glDrawArrays(GL_LINES, end_prev_layer, cur_end - end_prev_layer) glColor4f(*self.color_current) if end > cur_end: glDrawArrays(GL_LINES, cur_end, end - cur_end) # Restore line width glLineWidth(orig_linewidth) glEnableClientState(GL_COLOR_ARRAY) # Draw non printed stuff until end (if not ending at a given layer) start = max(self.printed_until, 0) end = end - start if end_prev_layer < 0 and end > 0 and not self.only_current: glDrawArrays(GL_LINES, start, end) self.vertex_buffer.unbind() self.vertex_color_buffer.unbind()
def _display_movements(self, has_vbo): self.vertex_buffer.bind() if has_vbo: glVertexPointer(3, GL_FLOAT, 0, None) else: glVertexPointer(3, GL_FLOAT, 0, self.vertex_buffer.ptr) self.vertex_color_buffer.bind() if has_vbo: glColorPointer(4, GL_FLOAT, 0, None) else: glColorPointer(4, GL_FLOAT, 0, self.vertex_color_buffer.ptr) self.index_buffer.bind() start = 1 layer_selected = self.num_layers_to_draw <= self.max_layers if layer_selected: end_prev_layer = self.layer_stops[self.num_layers_to_draw - 1] else: end_prev_layer = 0 end = self.layer_stops[min(self.num_layers_to_draw, self.max_layers)] glDisableClientState(GL_COLOR_ARRAY) glColor4f(*self.color_printed) # Draw printed stuff until end or end_prev_layer cur_end = min(self.printed_until, end) if not self.only_current: if 1 <= end_prev_layer <= cur_end: self._draw_elements(1, end_prev_layer) elif cur_end >= 1: self._draw_elements(1, cur_end) glEnableClientState(GL_COLOR_ARRAY) # Draw nonprinted stuff until end_prev_layer start = max(cur_end, 1) if end_prev_layer >= start: if not self.only_current: self._draw_elements(start, end_prev_layer) cur_end = end_prev_layer # Draw current layer if layer_selected: glDisableClientState(GL_COLOR_ARRAY) # Backup & increase line width orig_linewidth = (GLfloat)() glGetFloatv(GL_LINE_WIDTH, orig_linewidth) glLineWidth(2.0) glColor4f(*self.color_current_printed) if cur_end > end_prev_layer: self._draw_elements(end_prev_layer + 1, cur_end) glColor4f(*self.color_current) if end > cur_end: self._draw_elements(cur_end + 1, end) # Restore line width glLineWidth(orig_linewidth) glEnableClientState(GL_COLOR_ARRAY) # Draw non printed stuff until end (if not ending at a given layer) start = max(self.printed_until, 1) if not layer_selected and end >= start: self._draw_elements(start, end) self.vertex_buffer.unbind() self.vertex_color_buffer.unbind()
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 start_render(): gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) gl.glMatrixMode(gl.GL_MODELVIEW) gl.glEnableClientState(gl.GL_VERTEX_ARRAY)
def _display_movements(self, has_vbo): self.vertex_buffer.bind() glVertexPointer(3, GL_FLOAT, 0, self.vertex_buffer.ptr) self.vertex_color_buffer.bind() glColorPointer(3, GL_FLOAT, 0, self.vertex_color_buffer.ptr) self.vertex_normal_buffer.bind() glNormalPointer(GL_FLOAT, 0, self.vertex_normal_buffer.ptr) self.index_buffer.bind() # Prevent race condition by using the number of currently loaded layers max_layers = self.layers_loaded start = 1 layer_selected = self.num_layers_to_draw <= max_layers if layer_selected: end_prev_layer = self.layer_stops[self.num_layers_to_draw - 1] else: end_prev_layer = 0 end = self.layer_stops[min(self.num_layers_to_draw, max_layers)] glDisableClientState(GL_COLOR_ARRAY) glColor3f(*self.color_printed[:-1]) # Draw printed stuff until end or end_prev_layer cur_end = min(self.printed_until, end) if not self.only_current: if 1 <= end_prev_layer <= cur_end: self._draw_elements(1, end_prev_layer) elif cur_end >= 1: self._draw_elements(1, cur_end) glEnableClientState(GL_COLOR_ARRAY) # Draw nonprinted stuff until end_prev_layer start = max(cur_end, 1) if end_prev_layer >= start: if not self.only_current: self._draw_elements(start, end_prev_layer) cur_end = end_prev_layer # Draw current layer if layer_selected: glDisableClientState(GL_COLOR_ARRAY) glColor3f(*self.color_current_printed[:-1]) if cur_end > end_prev_layer: self._draw_elements(end_prev_layer + 1, cur_end) glColor3f(*self.color_current[:-1]) if end > cur_end: self._draw_elements(cur_end + 1, end) glEnableClientState(GL_COLOR_ARRAY) # Draw non printed stuff until end (if not ending at a given layer) start = max(self.printed_until, 1) if not layer_selected and end >= start: self._draw_elements(start, end) self.index_buffer.unbind() self.vertex_buffer.unbind() self.vertex_color_buffer.unbind() self.vertex_normal_buffer.unbind()
def bind(self): gl.glEnableClientState(self.type) gl.glBindBuffer(gl.GL_ARRAY_BUFFER, self.id) gl.glTexCoordPointer(2, gl.GL_FLOAT, 0, 0)
def on_draw(): gl.glPointSize(H.pointsize) gl.glEnableClientState(gl.GL_VERTEX_ARRAY) drawArray(H.field)
def draw(self): gl.glPushMatrix() self.transform() # color preserve - at least nvidia 6150SE needs that gl.glPushAttrib(gl.GL_CURRENT_BIT) # glPointSize(self.get_scaled_particle_size()) gl.glEnable(gl.GL_TEXTURE_2D) gl.glEnable(gl.GL_PROGRAM_POINT_SIZE) # glBindTexture(GL_TEXTURE_2D, self.texture.id) gl.glEnable(gl.GL_POINT_SPRITE) gl.glTexEnvi(gl.GL_POINT_SPRITE, gl.GL_COORD_REPLACE, gl.GL_TRUE) gl.glEnableClientState(gl.GL_VERTEX_ARRAY) vertex_ptr = PointerToNumpy(self.particle_pos) gl.glVertexPointer(2, gl.GL_FLOAT, 0, vertex_ptr) gl.glEnableClientState(gl.GL_COLOR_ARRAY) color_ptr = PointerToNumpy(self.particle_color) gl.glColorPointer(4, gl.GL_FLOAT, 0, color_ptr) gl.glEnableVertexAttribArray(self.particle_size_idx) size_ptr = PointerToNumpy(self.particle_size_scaled) gl.glVertexAttribPointer(self.particle_size_idx, 1, gl.GL_FLOAT, False, 0, size_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) # mode = GLint() # glTexEnviv( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, mode ) # # if self.color_modulate: # glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE ) # else: # glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE ) self.sprite_shader.install() self.sprite_shader.usetTex('sprite_texture', 0, gl.GL_TEXTURE_2D, self.texture.id) gl.glDrawArrays(gl.GL_POINTS, 0, self.total_particles) self.sprite_shader.uninstall() # un -blend gl.glPopAttrib() # color restore gl.glPopAttrib() # restore env mode # glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, mode) # disable states gl.glDisableVertexAttribArray(self.particle_size_idx) gl.glDisableClientState(gl.GL_COLOR_ARRAY) gl.glDisableClientState(gl.GL_VERTEX_ARRAY) gl.glDisable(gl.GL_POINT_SPRITE) gl.glDisable(gl.GL_PROGRAM_POINT_SIZE) gl.glDisable(gl.GL_TEXTURE_2D) gl.glPopMatrix()
def _draw_rects(shape_list: List[Sprite], vertex_vbo_id: gl.GLuint, texture_coord_vbo_id: gl.GLuint, change_x: float, change_y: float): """ Draw a set of rectangles using vertex buffers. This is more efficient than drawing them individually. """ if len(shape_list) == 0: return gl.glEnable(gl.GL_BLEND) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) gl.glEnable(gl.GL_TEXTURE_2D ) # As soon as this happens, can't use drawing commands # gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_WRAP_S, gl.GL_CLAMP_TO_EDGE) # gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_WRAP_T, gl.GL_CLAMP_TO_EDGE) gl.glTexParameterf(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MIN_FILTER, gl.GL_NEAREST) gl.glTexParameterf(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER, gl.GL_NEAREST) gl.glHint(gl.GL_POLYGON_SMOOTH_HINT, gl.GL_NICEST) gl.glHint(gl.GL_PERSPECTIVE_CORRECTION_HINT, gl.GL_NICEST) # gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) # gl.glMatrixMode(gl.GL_MODELVIEW) # gl.glDisable(gl.GL_BLEND) gl.glBindBuffer(gl.GL_ARRAY_BUFFER, vertex_vbo_id) gl.glEnableClientState(gl.GL_TEXTURE_COORD_ARRAY) gl.glEnableClientState(gl.GL_VERTEX_ARRAY) gl.glVertexPointer(2, gl.GL_FLOAT, 0, 0) gl.glBindBuffer(gl.GL_ARRAY_BUFFER, texture_coord_vbo_id) last_alpha = shape_list[0].alpha gl.glColor4f(1, 1, 1, last_alpha) gl.glLoadIdentity() # gl.glLoadIdentity() gl.glTranslatef(change_x, change_y, 0) # Ideally, we want to draw these in "batches." # We seek to find groups of squares with the same texture. Then draw # them all at once. last_texture_id = None last_alpha = 1 batch_count = 0 offset = 0 batch_offset = 0 texture_coord_vbo_id = None for shape in shape_list: if shape.texture.texture_id != last_texture_id or shape.alpha != last_alpha: # Ok, if the 'if' triggered above, we are now looking at a different # texture than we looked at with the last loop. So draw the last # "batch" of squares. We'll start a new batch with the current # square but not draw it yet if batch_count > 0: gl.glColor4f(1, 1, 1, last_alpha) _render_rect_filled(batch_offset, last_texture_id, texture_coord_vbo_id, batch_count) batch_count = 0 batch_offset = offset last_texture_id = shape.texture.texture_id last_alpha = shape.alpha batch_count += 4 offset += 4 # Draw the last batch, if it exists _render_rect_filled(batch_offset, last_texture_id, texture_coord_vbo_id, batch_count) gl.glDisable(gl.GL_TEXTURE_2D)
def render(self, scene): """ Add a ring to the view. :param scene: The view to render the model into :type scene: pyglet_helper.objects.View """ if self.degenerate: return # The number of subdivisions around the hoop's radial direction. if self.thickness: band_coverage = scene.pixel_coverage(self.pos, self.thickness) else: band_coverage = scene.pixel_coverage(self.pos, self.radius * 0.1) if band_coverage < 0: band_coverage = 1000 bands = sqrt(band_coverage * 4.0) bands = clamp(4, bands, 40) # The number of subdivisions around the hoop's tangential direction. ring_coverage = scene.pixel_coverage(self.pos, self.radius) if ring_coverage < 0: ring_coverage = 1000 rings = sqrt(ring_coverage * 4.0) rings = clamp(4, rings, 80) slices = int(rings) inner_slices = int(bands) radius = self.radius inner_radius = self.thickness # Create the vertex and normal arrays. vertices = [] normals = [] outer_angle_step = 2 * pi / (slices - 1) inner_angle_step = 2 * pi / (inner_slices - 1) outer_angle = 0. for i in range(slices): cos_outer_angle = cos(outer_angle) sin_outer_angle = sin(outer_angle) inner_angle = 0. for j in range(inner_slices): cos_inner_angle = cos(inner_angle) sin_inner_angle = sin(inner_angle) diameter = (radius + inner_radius * cos_inner_angle) vertex_x = diameter * cos_outer_angle vertex_y = diameter * sin_outer_angle vertex_z = inner_radius * sin_inner_angle normal_x = cos_outer_angle * cos_inner_angle normal_y = sin_outer_angle * cos_inner_angle normal_z = sin_inner_angle vertices.extend([vertex_x, vertex_y, vertex_z]) normals.extend([normal_x, normal_y, normal_z]) inner_angle += inner_angle_step outer_angle += outer_angle_step # Create ctypes arrays of the lists vertices = (gl.GLfloat *len(vertices))(*vertices) normals = (gl.GLfloat * len(normals))(*normals) # Create a list of triangle indices. indices = [] for i in range(slices - 1): for j in range(inner_slices - 1): pos = i * inner_slices + j indices.extend([pos, pos + inner_slices, pos + inner_slices + 1]) indices.extend([pos, pos + inner_slices + 1, pos + 1]) indices = (gl.GLuint * len(indices))(*indices) # Compile a display list self.list = gl.glGenLists(1) gl.glNewList(self.list, gl.GL_COMPILE) self.color.gl_set(self.opacity) gl.glPushClientAttrib(gl.GL_CLIENT_VERTEX_ARRAY_BIT) gl.glEnableClientState(gl.GL_VERTEX_ARRAY) gl.glEnableClientState(gl.GL_NORMAL_ARRAY) self.model_world_transform(scene.gcf, Vector([self.radius, self.radius, self.radius])).gl_mult() gl.glVertexPointer(3, gl.GL_FLOAT, 0, vertices) gl.glNormalPointer(gl.GL_FLOAT, 0, normals) gl.glDrawElements(gl.GL_TRIANGLES, len(indices), gl.GL_UNSIGNED_INT, indices) gl.glPopClientAttrib() gl.glEndList() gl.glCallList(self.list)