def _draw_wireframe_display_list(self, dl): pgl.glPushAttrib(pgl.GL_ENABLE_BIT | pgl.GL_POLYGON_BIT) pgl.glPolygonMode(pgl.GL_FRONT_AND_BACK, pgl.GL_LINE) pgl.glEnable(pgl.GL_POLYGON_OFFSET_LINE) pgl.glPolygonOffset(-0.005, -50.0) pgl.glCallList(dl) pgl.glPopAttrib()
def draw(self, state = [0,0,0]): gl.glEnable (gl.GL_LINE_SMOOTH); gl.glHint (gl.GL_LINE_SMOOTH_HINT, gl.GL_NICEST) gl.glLoadIdentity() gl.glLineWidth(1) gl.glColor3f(.7,.7,.7) draw_rect((-.5,0), (.5,-.01)) draw_rect((-.01,0),(.01,1)) gl.glTranslatef(0,self.r_roller,0); gl.glPushMatrix(); gl.glTranslatef(state[0]*self.r_roller, 0, 0) gl.glColor3f(0,0,0) gl.glLineWidth(3) # gl.glPushMatrix() gl.glRotatef(-R2D*state[0],0,0,1) draw_mass_center(self.r_roller, (0,0)) # gl.glPopMatrix() gl.glRotatef(-R2D*state[1], 0, 0, 1) gl.glTranslatef(0,self.r_roller,0) gl.glPushMatrix() gl.glRotatef(R2D*(state[1]+state[0]),0,0,1) gl.glPushAttrib(gl.GL_ENABLE_BIT); gl.glColor3f(.7,.2,.2) gl.glLineStipple(1, 0xF00F) # [1] gl.glEnable(gl.GL_LINE_STIPPLE) draw_line((0,0),(0,1)) gl.glPopAttrib() gl.glPopMatrix() gl.glTranslatef(-state[1] * self.r_roller,0,0) gl.glColor3f(0,0,0) draw_rect( (-self.l_board/2,0), (self.l_board/2,.02)) gl.glColor3f(.5,.5,.5) draw_rect((-.01,0), (.01,self.h_body)) gl.glPushMatrix() gl.glTranslatef(0, self.h_body, 0) gl.glRotatef(R2D*state[2], 0, 0, 1) gl.glColor3f(0,0,0); draw_mass_center(.1, (0,0)) gl.glPopMatrix() if len(state) >= 8: gl.glPushMatrix() gl.glTranslatef(0, self.h_body, 0) gl.glRotatef(R2D*state[3], 0, 0, 1) gl.glTranslatef(0, -self.h_body+self.r_roller+.05, 0) gl.glColor3f(0,0,0); draw_mass_center(.03, (0,0)) gl.glPopMatrix() gl.glPopMatrix();
def set_state(self): """ Ensure that blending is set. """ gl.glPushAttrib(gl.GL_ENABLE_BIT | gl.GL_CURRENT_BIT) gl.glEnable(gl.GL_BLEND) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA)
def draw(self): if self._render_object: pgl.glPushAttrib(pgl.GL_ENABLE_BIT | pgl.GL_POLYGON_BIT | pgl.GL_DEPTH_BUFFER_BIT) if self._overlay: pgl.glDisable(pgl.GL_DEPTH_TEST) self._render_object.draw() pgl.glPopAttrib()
def draw(self): if self._dirty: self._context = Context() self._parts = [] self.free() self.render() self.build_vbo() self._dirty = False # set gl.glEnable(self._texture.target) gl.glBindTexture(self._texture.target, self._texture.id) gl.glPushAttrib(gl.GL_COLOR_BUFFER_BIT) gl.glEnable(gl.GL_BLEND) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) gl.glPushMatrix() self.transform() # cuadric.begin() self._vertex_list.draw(gl.GL_TRIANGLES) # cuadric.end() # unset gl.glPopMatrix() gl.glPopAttrib() gl.glDisable(self._texture.target)
def after_draw(self, camera): """Called by CocosNode when the texture is already grabbed. The FrameBuffer will be unbound and the texture will be drawn :Parameters: `camera` : `Camera` The target's camera object. """ # capture after drawing self.grabber.after_render(self.texture) # after unbinding # set a 3d projection self._set_3d_projection() # and center the camera camera.locate(force=True) # blit gl.glEnable(self.texture.target) gl.glBindTexture(self.texture.target, self.texture.id) gl.glPushAttrib(gl.GL_COLOR_BUFFER_BIT) self._blit() gl.glPopAttrib() gl.glDisable(self.texture.target)
def _draw(self): corner = c = self.octree.corner w = self.octree.width gl.glPushAttrib( gl.GL_ENABLE_BIT ) gl.glEnable( gl.GL_COLOR_MATERIAL ) if self.octree._child_nodes is None: if self.list_id: gl.glCallList(self.list_id) else: self.list_id = gl.glGenLists(1) gl.glNewList(self.list_id, gl.GL_COMPILE) gl.glColor3f(*self.color) gl.glBegin(gl.GL_LINE_LOOP) gl.glVertex3f(*c) gl.glVertex3f(*(c + (0,w,0))) gl.glVertex3f(*(c + (0,w,w))) gl.glVertex3f(*(c + (0,0,w))) gl.glEnd() c = corner + (w,0,0) gl.glBegin(gl.GL_LINE_LOOP) gl.glVertex3f(*c) gl.glVertex3f(*(c + (0,w,0))) gl.glVertex3f(*(c + (0,w,w))) gl.glVertex3f(*(c + (0,0,w))) gl.glEnd() gl.glBegin(gl.GL_LINES) gl.glVertex3f(*c) gl.glVertex3f(*(c - (w,0,0))) gl.glVertex3f(*(c + (0,w,0))) gl.glVertex3f(*(corner + (0,w,0))) gl.glVertex3f(*(c + (0,w,w))) gl.glVertex3f(*(corner + (0,w,w))) gl.glVertex3f(*(c + (0,0,w))) gl.glVertex3f(*(corner + (0,0,w))) gl.glEnd() gl.glEndList() # This could be optimized of course if self.octree._child_nodes is not None: r = self.color[0] + 0.14 if r < 1.0: r = r % 1.0 else: r = 1.0 b = max((self.color[2] - 0.14), 0) for node in self.octree._child_nodes.values(): if not self._cache.has_key(id(node)): self._cache[id(node)] = OctreeDebug(node, color=(r,0,b)) debugNode = self._cache[id(node)] debugNode._draw() gl.glColor3f(1,1,1) gl.glPopAttrib()
def set_state(self): gl.glPushAttrib(gl.GL_ENABLE_BIT | gl.GL_TRANSFORM_BIT | gl.GL_CURRENT_BIT) gl.glEnable(gl.GL_BLEND) gl.glBlendFuncSeparate(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA, gl.GL_ONE, gl.GL_ONE_MINUS_SRC_ALPHA) # To Allow Normal Rendering when Buffering with FrameBufferObject # Without this option : problem with alpha blending when rendering buffered GUI textures #Also in context.glContext # Disable clipping planes to check culling. gl.glEnable(gl.GL_CLIP_PLANE0) gl.glEnable(gl.GL_CLIP_PLANE1) gl.glEnable(gl.GL_CLIP_PLANE2) gl.glEnable(gl.GL_CLIP_PLANE3) # Left gl.glClipPlane(gl.GL_CLIP_PLANE0, (gl.GLdouble * 4)( 1, 0, 0, -(self._clip_x - 1))) # Top gl.glClipPlane(gl.GL_CLIP_PLANE1, (gl.GLdouble * 4)( 0, -1, 0, self._clip_y)) # Right gl.glClipPlane(gl.GL_CLIP_PLANE2, (gl.GLdouble * 4)( -1, 0, 0, self._clip_x + self._clip_width + 1)) # Bottom gl.glClipPlane(gl.GL_CLIP_PLANE3, (gl.GLdouble * 4)( 0, 1, 0, -(self._clip_y - self._clip_height))) gl.glTranslatef(self.translate_x, self.translate_y, 0)
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 buffer_texture(width, height): id_ = gl.GLuint() gl.glGenTextures(1, byref(id_)) gl.glPushAttrib(gl.GL_ENABLE_BIT | gl.GL_TEXTURE_BIT) gl.glActiveTexture(gl.GL_TEXTURE0) gl.glEnable(gl.GL_TEXTURE_2D) gl.glBindTexture(gl.GL_TEXTURE_2D, id_) gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MIN_FILTER, gl.GL_LINEAR) gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER, gl.GL_LINEAR) gl.glTexImage2D( gl.GL_TEXTURE_2D, 0, gl.GL_RGBA, width, height, 0, gl.GL_RGBA, gl.GL_UNSIGNED_BYTE, (gl.GLubyte * (width*height * 4))(), ) gl.glFlush() gl.glBindTexture(gl.GL_TEXTURE_2D, 0) gl.glPopAttrib() return id_
def set_state(self): gl.glPushAttrib(gl.GL_LINE_BIT | gl.GL_CURRENT_BIT) gl.glLineWidth(2) if self.facet.is_border_facet: colour = self.BORDER_FACET_COLOUR else: colour = self.INNER_FACET_COLOUR gl.glColor4f(*colour)
def draw(self): super(ColorLayer, self).draw() gl.glPushMatrix() self.transform() gl.glPushAttrib(gl.GL_CURRENT_BIT) self._batch.draw() gl.glPopAttrib() gl.glPopMatrix()
def on_draw(self): with reset_matrix(self.w, self.h): gl.glPushAttrib(gl.GL_ENABLE_BIT) gl.glEnable(gl.GL_BLEND) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) super().on_draw() gl.glPopAttrib()
def set_state(self): gl.glPushAttrib(gl.GL_CURRENT_BIT) if self.facet.home_facet_of: colour = self.facet.colour else: if self.facet.is_border_facet: colour = self.BORDER_FACET_COLOUR else: colour = self.INNER_FACET_COLOUR gl.glColor4f(*colour)
def draw_mass_center(r, pt): x,y = pt gl.glPushAttrib(gl.GL_CURRENT_BIT) gl.glColor3f(1,1,1) draw_circle(r, pt, filled=True) gl.glColor3f(0,0,0) draw_circle(r, pt, filled=False) draw_arc((r, r), pt, (0,pi/2)) draw_arc((r, r), pt, (pi,3*pi/2)) gl.glPopAttrib()
def set_state(self): """ Enables a scissor test on our region """ gl.glPushAttrib(gl.GL_ENABLE_BIT | gl.GL_TRANSFORM_BIT | gl.GL_CURRENT_BIT) self.was_scissor_enabled = gl.glIsEnabled(gl.GL_SCISSOR_TEST) gl.glEnable(gl.GL_SCISSOR_TEST) gl.glScissor(int(self.x), int(self.y), int(self.width), int(self.height))
def render(self): from pyglet import gl gl.glPushAttrib(gl.GL_CURRENT_BIT) gl.glColor3f(1.0, 0.0, 0.0) for p in self._polys_top: gl.glBegin(gl.GL_LINE_STRIP) for i, q in enumerate(p): gl.glVertex2f(q[0], q[1]) gl.glVertex2f(p[0][0], p[0][1]) gl.glEnd() gl.glPopAttrib(gl.GL_CURRENT_BIT)
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_special(self, style): """Draws the batch with special OpenGL attributes. Args: style (int): The style with which to draw the batch. """ glPushAttrib(GL_ENABLE_BIT) if style == self.DASHED_LINES: self._set_dashed_lines() self.draw() glPopAttrib()
def illuminate(self): if not self.is_fbo_valid(): self.fbo = FrameBuffer(self.viewport.w, self.viewport.h) #self.vl.vertices = self.viewport_coords() with self.fbo: gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) gl.glPushAttrib(gl.GL_ALL_ATTRIB_BITS) yield gl.glPopAttrib() if self.sh is None: self.sh = self._build_spatial_hash() self.render()
def draw(self, x, y, width, height): """Draw the nine-patch at the given image dimensions.""" width = max(width, self.width + 2) height = max(height, self.height + 2) vertices = self.get_vertices(int(x), int(y), int(width), int(height)) glPushAttrib(GL_ENABLE_BIT) glEnable(GL_BLEND) glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) glEnable(self.texture.target) glBindTexture(self.texture.target, self.texture.id) pyglet.graphics.draw_indexed(16, GL_QUADS, self.indices, ('v2i', vertices), ('t2f', self.tex_coords)) glPopAttrib()
def draw(self): glPushMatrix() glPushAttrib(GL_STENCIL_BUFFER_BIT) glClear(GL_STENCIL_BUFFER_BIT) glTranslatef(self.parent.x, self.parent.y, 0) glDisable(GL_TEXTURE_2D) loader = self.loader if loader.shape == LINE_SHAPE: glEnable(GL_LINE_SMOOTH) glEnable(GL_POINT_SMOOTH) glLineWidth(loader.borderSize) glPointSize(loader.borderSize) self.vertex.draw(GL_LINES) self.vertex.draw(GL_POINTS) elif loader.shape == RECTANGLE_SHAPE: width = self.parent.width height = self.parent.height if loader.fillType != NONE_FILL: # for counters if self.counter_stencil: glEnable(GL_STENCIL_TEST) glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE) glStencilFunc(GL_ALWAYS, 1, 1) glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE) self.counter_stencil.draw(GL_QUADS) glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE) glStencilFunc(GL_EQUAL, 1, 1) self.draw_rectangle() glDisable(GL_STENCIL_TEST) else: self.draw_rectangle() if self.border is not None: self.border.draw(GL_QUADS) elif loader.shape == ELLIPSE_SHAPE: if loader.fillType != NONE_FILL: glEnable(GL_STENCIL_TEST) glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE) glStencilFunc(GL_ALWAYS, 1, 1) glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE) self.stencil.draw(GL_TRIANGLES) glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE) glStencilFunc(GL_EQUAL, 1, 1) self.draw_rectangle(True) glDisable(GL_STENCIL_TEST) if self.border: self.border.draw(GL_QUADS) glPopAttrib() glPopMatrix()
def render(self): from pyglet import gl gl.glPushAttrib(gl.GL_CURRENT_BIT) gl.glColor3f(1.0, 0.0, 0.0) # Here we'll render each waypoint as a red square. for (_, _, x, y) in self._waypoints: gl.glBegin(gl.GL_QUADS) gl.glVertex2f(x-2, y-4) gl.glVertex2f(x+2, y-4) gl.glVertex2f(x+2, y+4) gl.glVertex2f(x-2, y+4) gl.glEnd() gl.glLineWidth(3) # And let's draw a line between each waypoint, so we know where we're going next. gl.glBegin(gl.GL_LINE_STRIP) for (_, _, x, y) in self._waypoints: gl.glVertex2f(x, y) gl.glEnd() gl.glPopAttrib(gl.GL_CURRENT_BIT)
def bind(self): assert Framebuffer.current_fbo is None from pyglet import gl t = self.texture gl.glBindFramebufferEXT(gl.GL_FRAMEBUFFER_EXT, self.fbo_id) gl.glPushAttrib(gl.GL_VIEWPORT_BIT | gl.GL_TRANSFORM_BIT) if t: gl.glViewport(0, 0, t.width, t.height) gl.glMatrixMode(gl.GL_PROJECTION) gl.glPushMatrix() if t: gl.glLoadIdentity() gl.gluOrtho2D(0, t.width, 0, t.height) gl.glMatrixMode(gl.GL_MODELVIEW) gl.glPushMatrix() if t: gl.glLoadIdentity() Framebuffer.current_fbo = self
def blit_image(self, matrix, position, scale, image): x, y = image.width * scale, image.height * scale # dx = self.x + position[0] # dy = self.y + position[1] dx, dy = position gl.glEnable(image.target) gl.glBindTexture(image.target, image.id) gl.glPushAttrib(gl.GL_COLOR_BUFFER_BIT) gl.glEnable(gl.GL_BLEND) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) # blit img points = [ (-dx, -dy), (x - dx, -dy), (x - dx, y - dy), (-dx, y - dy) ] a, b, _, c, d, _, e, f, _, g, h, _ = image.texture.tex_coords textures = [a, b, c, d, e, f, g, h] np = [matrix * euclid.Point2(*p) for p in points] gl.glColor4ub(255, 255, 255, self.alpha) gl.glBegin(gl.GL_QUADS) gl.glTexCoord2f(a, b) gl.glVertex2f(*np[0]) gl.glTexCoord2f(c, d) gl.glVertex2f(*np[1]) gl.glTexCoord2f(e, f) gl.glVertex2f(*np[2]) gl.glTexCoord2f(g, h) gl.glVertex2f(*np[3]) gl.glEnd() gl.glColor4ub(255, 255, 255, 255) # pyglet.graphics.draw(4, GL_QUADS, # ("v2f", new_points), # ("t2f", textures), # ("c4B", [255, 255, 255, self.alpha] * 4), # ) gl.glPopAttrib() gl.glDisable(image.target)
def _set_view(self): left = int(self._pixel_per_point[0] * self.rect.left) bottom = int(self._pixel_per_point[1] * self.rect.bottom) width = int(self._pixel_per_point[0] * self.rect.width) height = int(self._pixel_per_point[1] * self.rect.height) gl.glPushAttrib(gl.GL_ENABLE_BIT) gl.glEnable(gl.GL_SCISSOR_TEST) gl.glScissor(left, bottom, width, height) self._mode = (gl.GLint)() gl.glGetIntegerv(gl.GL_MATRIX_MODE, self._mode) self._viewport = (gl.GLint * 4)() gl.glGetIntegerv(gl.GL_VIEWPORT, self._viewport) gl.glViewport(left, bottom, width, height) gl.glMatrixMode(gl.GL_PROJECTION) gl.glPushMatrix() gl.glLoadIdentity() near = 0.01 far = 1000. gl.gluPerspective(self.scene.camera.fov[1], width / height, near, far) gl.glMatrixMode(gl.GL_MODELVIEW)
def set_state(self): glPushAttrib(GL_ENABLE_BIT) glEnable(GL_BLEND) glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) glEnable(self.texture.target) glBindTexture(self.texture.target, self.texture.id)
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 set_state(self): glPushAttrib(GL_COLOR_BUFFER_BIT) glEnable(GL_BLEND) glBlendFunc(self.blend_src, self.blend_dest)
def set_state(self): gl.glPushAttrib(gl.GL_CURRENT_BIT | gl.GL_LINE_BIT) gl.glColor4ub(*self.color) gl.glLineWidth(self.stroke_width)
def renderToTexture(w, h, function): import ctypes from pyglet import gl from pyglet import image from pyglet.gl import gl_info global has_fbo if has_fbo is None: has_fbo = gl_info.have_extension('GL_EXT_framebuffer_object') # enforce dimensions are ints w, h = int(w), int(h) # set up viewport gl.glPushAttrib(gl.GL_VIEWPORT_BIT | gl.GL_TRANSFORM_BIT) gl.glViewport(0, 0, w, h) gl.glMatrixMode(gl.GL_PROJECTION) gl.glPushMatrix() gl.glLoadIdentity() gl.glOrtho(0, w, 0, h, -1, 1) gl.glMatrixMode(gl.GL_MODELVIEW) if has_fbo: # render directly to texture # create our frame buffer fbo = gl.GLuint() gl.glGenFramebuffersEXT(1, ctypes.byref(fbo)) gl.glBindFramebufferEXT(gl.GL_FRAMEBUFFER_EXT, fbo) # allocate a texture and add to the frame buffer tex = image.Texture.create_for_size(gl.GL_TEXTURE_2D, w, h, gl.GL_RGBA) gl.glBindTexture(gl.GL_TEXTURE_2D, tex.id) gl.glFramebufferTexture2DEXT(gl.GL_FRAMEBUFFER_EXT, gl.GL_COLOR_ATTACHMENT0_EXT, gl.GL_TEXTURE_2D, tex.id, 0) status = gl.glCheckFramebufferStatusEXT(gl.GL_FRAMEBUFFER_EXT) assert status == gl.GL_FRAMEBUFFER_COMPLETE_EXT # now render gl.glBindFramebufferEXT(gl.GL_FRAMEBUFFER_EXT, fbo) function() gl.glBindFramebufferEXT(gl.GL_FRAMEBUFFER_EXT, 0) # clean up gl.glDeleteFramebuffersEXT(1, ctypes.byref(fbo)) else: # render and copy to texture # render function() # grab the buffer and copy contents to the texture buffer = image.get_buffer_manager().get_color_buffer() tex = image.Texture.create_for_size(gl.GL_TEXTURE_2D, w, h, gl.GL_RGBA) tex.blit_into(buffer.get_region(0, 0, w, h), 0, 0, 0) gl.glMatrixMode(gl.GL_PROJECTION) gl.glPopMatrix() gl.glPopAttrib() # return the region (the whole texture will most likely be larger) return tex.get_region(0, 0, w, h)
def draw_image(self, img, rect=None, force_copy=False): """ Renders a GraphicsContextArray into this GC """ xform = self.get_ctm() image = image_as_array(img) shape = image.shape if shape[2] == 4: fmt = "RGBA" else: fmt = "RGB" aii = ArrayImage(image, format=fmt) texture = aii.texture # The texture coords consists of (u,v,r) for each corner of the # texture rectangle. The coordinates are stored in the order # bottom left, bottom right, top right, top left. x, y, w, h = rect texture.width = w texture.height = h t = texture.tex_coords points = array([ [x, y + h], [x + w, y + h], [x + w, y], [x, y], ]) p = transform_points(affine_from_values(*xform), points) a = (gl.GLfloat * 32)( t[0], t[1], t[2], 1., p[0, 0], p[0, 1], 0, 1., t[3], t[4], t[5], 1., p[1, 0], p[1, 1], 0, 1., t[6], t[7], t[8], 1., p[2, 0], p[2, 1], 0, 1., t[9], t[10], t[11], 1., p[3, 0], p[3, 1], 0, 1., ) gl.glPushAttrib(gl.GL_ENABLE_BIT) gl.glEnable(texture.target) gl.glBindTexture(texture.target, texture.id) gl.glPushClientAttrib(gl.GL_CLIENT_VERTEX_ARRAY_BIT) gl.glInterleavedArrays(gl.GL_T4F_V4F, 0, a) gl.glDrawArrays(gl.GL_QUADS, 0, 4) gl.glPopClientAttrib() gl.glPopAttrib()