def render(self): ''' ''' super(Shaft, self).render() glPushAttrib(GL_CURRENT_BIT) n = int(self.length) lim = n - 1 if self.orientation in ['down', 'forward', 'backward']: lim = 0 self._set_material() # print lim for i in range(n): glPushMatrix() glTranslatef(0, i / 2.0, 0) if i == lim: self._can_(0.66, 0.5) self._cylinder_(0.5, 0.5) glPopMatrix() # if self.state and animate: # if ac % 20 == 0 and self.prev_ac != ac: # if self.orientation == 'left' or self.orientation == 'up': # self.colorlist = shift(self.colorlist) # else: # self.colorlist = deshift(self.colorlist) # self.prev_ac = ac glPopAttrib()
def drawGrid(self): grids_per_screen = 10 eye_dist = self.camera_xyz[2] meters_per_grid = round_to_125(eye_dist / grids_per_screen) num_lines = 300 glPushAttrib(GL_DEPTH_BUFFER_BIT | GL_ENABLE_BIT | GL_LINE_BIT) glEnable(GL_BLEND) glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) glEnable(GL_DEPTH_TEST) glPushMatrix() glTranslatef(0, 0, 0) glColor3f(0.2, 0.2, 0.2) glBegin(GL_LINES) for i in range(num_lines): glVertex2f((-num_lines / 2 + i) * meters_per_grid, -num_lines / 2 * meters_per_grid) glVertex2f((-num_lines / 2 + i) * meters_per_grid, num_lines / 2 * meters_per_grid) glVertex2f(-num_lines / 2 * meters_per_grid, (-num_lines / 2 + i) * meters_per_grid) glVertex2f(num_lines / 2 * meters_per_grid, (-num_lines / 2 + i) * meters_per_grid) glEnd() glPopMatrix() glPopAttrib()
def draw(self): '''Draw the mesh on screen (using display list if compiled)''' if self.list: glCallList(self.list) return glPushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT) glPushAttrib(GL_CURRENT_BIT | GL_ENABLE_BIT | GL_LIGHTING_BIT) glEnable(GL_CULL_FACE) glCullFace(GL_BACK) for group in self.groups: if group.material: group.material.apply() if group.array is None: if group.material and group.material.texture: if group.material.texture.rectangle: # texture is a rectangle texture # that's mean we need to adjust the range of texture # coordinate from original 0-1 to 0-width/0-height group.vertices[0::8] = map( lambda x: x * group.material.texture.width, group.vertices[0::8] ) group.vertices[1::8] = map( lambda x: x * group.material.texture.height, group.vertices[1::8] ) group.array = (GLfloat * len(group.vertices))(*group.vertices) group.triangles = len(group.vertices) / 8 glInterleavedArrays(GL_T2F_N3F_V3F, 0, group.array) glDrawArrays(GL_TRIANGLES, 0, group.triangles) if group.material: group.material.unapply() glPopAttrib() glPopClientAttrib()
def draw(self, camera_matrix: numpy.ndarray, camera_position: PointCoordinatesAny = None): """ Draw the selection box :param camera_matrix: 4x4 transformation matrix for the camera :param camera_position: The position of the camera. Used to flip draw direction if camera inside box. :return: """ self._setup() if self._rebuild: self._create_geometry() self._draw_mode = GL_TRIANGLES transformation_matrix = numpy.matmul(camera_matrix, self.transformation_matrix) glPushAttrib(GL_DEPTH_BUFFER_BIT | GL_POLYGON_BIT) # store opengl state if camera_position is not None and camera_position in self: glCullFace(GL_FRONT) else: glCullFace(GL_BACK) self.draw_start = 0 self.draw_count = 36 super()._draw(transformation_matrix) # draw the lines around the boxes glDisable(GL_DEPTH_TEST) self._draw_mode = GL_LINE_STRIP super()._draw(transformation_matrix) glPopAttrib() # reset to starting state
def draw(self): '''Draw the mesh on screen (using display list if compiled)''' if self.list: glCallList(self.list) return glPushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT) glPushAttrib(GL_CURRENT_BIT | GL_ENABLE_BIT | GL_LIGHTING_BIT) glEnable(GL_CULL_FACE) glCullFace(GL_BACK) for group in self.groups: if group.material: group.material.apply() if group.array is None: if group.material and group.material.texture: if group.material.texture.rectangle: # texture is a rectangle texture # that's mean we need to adjust the range of texture # coordinate from original 0-1 to 0-width/0-height group.vertices[0::8] = map( lambda x: x * group.material.texture.width, group.vertices[0::8]) group.vertices[1::8] = map( lambda x: x * group.material.texture.height, group.vertices[1::8]) group.array = (GLfloat * len(group.vertices))(*group.vertices) group.triangles = len(group.vertices) / 8 glInterleavedArrays(GL_T2F_N3F_V3F, 0, group.array) glDrawArrays(GL_TRIANGLES, 0, group.triangles) if group.material: group.material.unapply() glPopAttrib() glPopClientAttrib()
def render(self, text: str) -> None: glPushAttrib(GL_ENABLE_BIT | GL_COLOR_BUFFER_BIT | GL_TEXTURE_BIT) glEnable(GL_TEXTURE_2D) glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE) glEnable(GL_BLEND) glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA) pen = numpy.array([0.0, 0.0]) for i in range(len(text)): char = text[i] if char not in self._glyphs: self._glyphs[char] = _Glyph(self._face, char) self._glyphs[char].render(pen) pen += self._glyphs[char].advance if i + 1 < len(text): kerning = self._face.get_kerning( self._face.get_char_index(char), self._face.get_char_index(text[i + 1])) pen += numpy.array([kerning.x / 64.0, kerning.y / 64.0]) glPopAttrib()
def draw(self, camera_matrix: numpy.ndarray): """Draw all of the levels.""" for level, transform, is_mirrored in zip(self._objects, self._transformation_matrices, self._is_mirrored): glPushAttrib(GL_POLYGON_BIT) # store opengl state if is_mirrored: glCullFace(GL_FRONT) else: glCullFace(GL_BACK) level.draw(numpy.matmul(camera_matrix, transform)) glPopAttrib() # reset to starting state
def _draw_orbitals(self): r = 1.5 glPushAttrib(GL_CURRENT_BIT) self._set_material((0, 0, 1)) for a in [60, -60, 0]: glPushMatrix() glRotatef(a, 1, 0, 0) glTranslatef(0, 0, -0.25 / 2) self._torus_(radius=r, inner_radius=0.15, xs=25, ys=25 ) glPopMatrix() glPopAttrib()
def release(self): # Restore viewport if self.push_viewport: glPopAttrib() # Copy current buffer into fbo texture set_texture(self.texture, target=GL_TEXTURE_2D) glReadBuffer(GL_BACK) glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, self.size[0], self.size[1]) # Restore old buffer w = pymt.getWindow() glDrawPixels(w.width, w.height, GL_RGBA, GL_UNSIGNED_BYTE, self.pixels) glPopAttrib() super(SoftwareFbo, self).release()
def render(self): ''' ''' super(Turbo, self).render() if self.identify: tag = self.name.split('_') tag = ' '.join([i.capitalize() for i in tag]) self._text_(tag, [-2, 2]) glPushAttrib(GL_CURRENT_BIT) glPushMatrix() self._set_material(color=[0, 0, 0]) glTranslatef(0, 0.5, 0) ac = 1 glPushMatrix() glRotatef(ac * 5, 0, 1, 0) self._fan_() glPopMatrix() glPushMatrix() glRotatef(-ac * 5, 0, 1, 0) glTranslatef(0, 0.5, 0) self._fan_() glPopMatrix() glPushMatrix() glRotatef(ac * 5, 0, 1, 0) glTranslatef(0, 1, 0) self._fan_() glPopMatrix() glPopMatrix() glPopAttrib() # self._set_material() # draw main body glPushAttrib(GL_CURRENT_BIT) for r, h, alpha in [(1, 3, 0.5), (1.5, 0.5, False)]: self._set_material(alpha=alpha) glTranslatef(0, h, 0) self._can_(radius=r, height=h) glPopAttrib()
def render(self): ''' ''' super(Bone, self).render() glPushAttrib(GL_CURRENT_BIT) glPushMatrix() self._set_material() # glTranslatef(0, 0, 0) self._cube_() for _i in range(self.length - 1): glTranslatef(1, 0, 0) self._cube_() glPopMatrix() glPopAttrib()
def drawpolycone_multicolor_worker(params): """ Draw a polycone. Receive parameters through a sequence so that this function and its parameters can be passed to another function for deferment. Right now this is only ColorSorter.schedule (see below) piotr 080311: this variant accepts a color array as an additional parameter """ (pos_array, color_array, rad_array) = params glEnable(GL_COLOR_MATERIAL) # have to enable GL_COLOR_MATERIAL for # the GLE function glPushAttrib(GL_CURRENT_BIT) # store current attributes in case glePolyCone # modifies the (e.g. current color) # piotr 080411 glePolyCone(pos_array, color_array, rad_array) glPopAttrib() # This fixes the 'disappearing cylinder' bug # glPopAttrib doesn't take any arguments # piotr 080415 glDisable(GL_COLOR_MATERIAL) return
def drawGround(self): GROUND_SIZE = 1000 glColor3f(0.9, 0.9, 0.9) glPushAttrib(GL_POLYGON_BIT) glEnable(GL_POLYGON_OFFSET_FILL) glPolygonOffset(2.0, 2.0) glDisable(GL_COLOR_MATERIAL) glPushMatrix() glTranslatef(0, 0, 0) glScalef(GROUND_SIZE, GROUND_SIZE, 1.0) glBegin(GL_QUADS) glVertex2d(1, 1) glVertex2d(1, -1) glVertex2d(-1, -1) glVertex2d(-1, 1) glEnd() glPopMatrix() glPopAttrib()
def drawpolycone_multicolor_worker(params): """ Draw a polycone. Receive parameters through a sequence so that this function and its parameters can be passed to another function for deferment. Right now this is only ColorSorter.schedule (see below) piotr 080311: this variant accepts a color array as an additional parameter """ (pos_array, color_array, rad_array) = params glEnable(GL_COLOR_MATERIAL) # have to enable GL_COLOR_MATERIAL for # the GLE function glPushAttrib( GL_CURRENT_BIT) # store current attributes in case glePolyCone # modifies the (e.g. current color) # piotr 080411 glePolyCone(pos_array, color_array, rad_array) glPopAttrib() # This fixes the 'disappearing cylinder' bug # glPopAttrib doesn't take any arguments # piotr 080415 glDisable(GL_COLOR_MATERIAL) return
def drawItemTree(self, item=None, useItemNames=False): """ :param item: :param useItemNames: :return: """ if item is None: items = [x for x in self.items if x.parentItem() is None] else: items = item.childItems() items.append(item) for i in items: if not i.visible(): continue if i is item: try: glPushAttrib(GL_ALL_ATTRIB_BITS) if useItemNames: glLoadName(i._id) self._itemNames[i._id] = i i.paint() except GLError as ex: _log.info("Error while drawing items: {}".format(ex)) if CONFIG.debug: raise finally: glPopAttrib() else: glMatrixMode(GL_MODELVIEW) glPushMatrix() try: tr = i.transform() a = np.array(tr.copyDataTo()).reshape((4, 4)) glMultMatrixf(a.transpose()) self.drawItemTree(i, useItemNames=useItemNames) finally: glMatrixMode(GL_MODELVIEW) glPopMatrix()
def render(self): ''' ''' # super(Laser, self).render() MultiStateObject3D.render(self) glPushAttrib(GL_CURRENT_BIT) self._set_material() glPushMatrix() glRotatef(90, 1, 0, 0) self._disk_() glPopMatrix() glPushMatrix() glTranslatef(0, -1, 0) glRotatef(90, 1, 0, 0) self._disk_() glPopMatrix() self._cylinder_(1, 1) glPopAttrib()
def stencilPop(): '''Pop out the last stack from stencil stack''' global __stencil_stack glPopAttrib() __stencil_stack -= 1 # remove current stencil stack __stencil_stack_dl.pop() __stencil_stack_view.pop() # replay stencil stack from the start # only if it's enabled if not glIsEnabled(GL_STENCIL_TEST): return # clear stencil glClearStencil(0) glClear(GL_STENCIL_BUFFER_BIT) # increment the draw buffer glStencilFunc(GL_NEVER, 0x0, 0x0) glStencilOp(GL_INCR, GL_INCR, GL_INCR) glColorMask(0, 0, 0, 0) # replay all gl operation for idx in xrange(__stencil_stack): dl = __stencil_stack_dl[idx] view = __stencil_stack_view[idx] with gx_matrix_identity: glMultMatrixf(view) dl.draw() # draw inner content only when stencil match the buffer glColorMask(1, 1, 1, 1) glStencilFunc(GL_EQUAL, __stencil_stack, __stencil_stack) glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP)
def _disable_stereo(self): """ Disables stereo rendering. This method pops a modelview matrix from the matrix stack. """ if not self.stereo_enabled: return if self.stereo_mode <= 2: # side-by-side stereo mode # make sure that the clipping plane is disabled glDisable(GL_CLIP_PLANE5) else: # anaglyphs stereo mode # enable all colors glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE) # restore the matrix glMatrixMode(GL_MODELVIEW) glPopMatrix() glPopAttrib() return
def processFrame(self, timePassedSecs): """ draws a scene """ # update the model view matrix self.sceneCamera.updateKeys() self.sceneCamera.update() # process queued GLObject events GLObject.signalsEmit() # enable some default scene states glEnable(GL_DEPTH_TEST) ######## SHADOW MAP RENDERING START ######## # offset the geometry slightly to prevent z-fighting # note that this introduces some light-leakage artifacts glEnable(GL_POLYGON_OFFSET_FILL) glPolygonOffset(1.1, 4096.0) # cull front faces for shadow rendering, # this moves z-fighting to backfaces. glCullFace(GL_FRONT) # enable depth rendering shader. # FIXME: support segment geometry shader! # geometry shader could change the shadow shape! self.depthShader.enable() map(_updateLightShadowMap, self.lights) glBindFramebuffer(GL_FRAMEBUFFER, 0) glDisable(GL_POLYGON_OFFSET_FILL) ######## SHADOW MAP RENDERING STOP ######## #### TODO: FOG: integrate FOG #### glEnable(GL_FOG) glFogi(GL_FOG_MODE, GL_EXP2) # approximate the atmosphere's filtering effect as a linear function sunDir = array([4.0, 4.0, 4.0, 0.0], float32) # TODO: FOG: what is the sun dir ? skyColor = array([0.8, sunDir[1] * 0.1 + 0.7, sunDir[1] * 0.4 + 0.5, 1.0], float32) glClearColor(*skyColor) glFogf(GL_FOG_DENSITY, 0.4) glFogf(GL_FOG_START, 16.0) glFogf(GL_FOG_END, self.farClip) glFogfv(GL_FOG_COLOR, skyColor) # fill projection matrix glMatrixMode(GL_PROJECTION) glLoadMatrixf(self.projectionMatrix) glMatrixMode(GL_MODELVIEW) # draw stuff in 3d projection lastCam = None glPushAttrib(GL_COLOR_BUFFER_BIT) for (fbo, draw, cam) in self.sceneFBOS: # render to fbo fbo.enable() if cam != lastCam: cam.enable() lastCam = cam draw() # disable render to texture FBO.disable() glPopAttrib() glViewport(0, 0, self.winSize[0], self.winSize[1]) #### TODO: FOG: integrate FOG #### glDisable(GL_FOG) # change to orthogonal projection glMatrixMode(GL_PROJECTION) glLoadMatrixf(self.orthoMatrix) glMatrixMode(GL_MODELVIEW) glLoadIdentity() # no depth test needed in orthogonal rendering glDisable(GL_DEPTH_TEST) # draw orthogonal to the screen self.orthogonalPass()
def _draw_halo(self): glPushAttrib(GL_CURRENT_BIT) self._set_material((1, 1, 0), alpha=0.3) self._sphere_(radius=1.5) glPopAttrib()
def release(self): if self.push_viewport: glPopAttrib() HardwareFbo.fbo_stack.pop() glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, HardwareFbo.fbo_stack[-1]) super(HardwareFbo, self).release()
def drawCSSRectangle(pos=(0, 0), size=(100, 100), style=dict(), prefix=None, state=None): '''Draw a rectangle with CSS :Parameters: `state`: if a certain state string is passed, we will use styles with this postifx instead. for example: style[bg-color] and style[bg-color-down] are both set. if state == "down", we wil use bg-color-down instead of bg-color :Styles: * alpha-background (color) * border-radius (float) * border-radius-precision (float) * border-width (float) * draw-alpha-background (bool) * draw-background (bool) * draw-border (bool) ''' bg_image = style.get('bg-image-'+str(state)) if not bg_image: bg_image = style.get('bg-image') # Check if we have a cached version cache_id = '%s:%s:%s:%s:%s' % (pos, size, style, prefix, state) cache = Cache.get('pymt.cssrect', cache_id) if cache: cache.draw() if bg_image: bg_image.size = size bg_image.pos = pos bg_image.draw() return # lets use the ones for given state, # and ignore the regular ones if the state ones are there if state: state = "-" + state newstyle = {} overwrites = [] for s in style: if state in s: overwrite = s.replace(state, '') newstyle[overwrite] = style[s] overwrites.append(overwrite) if s not in overwrites: newstyle[s] = style[s] style = newstyle # hack to remove prefix in style if prefix is not None: prefix += '-' newstyle = {} for k in style: newstyle[k] = style[k] for k in style: if prefix in k: newstyle[k.replace(prefix, '')] = style[k] style = newstyle style.setdefault('border-width', 1.5) style.setdefault('border-radius', 0) style.setdefault('border-radius-precision', .1) style.setdefault('draw-border', 0) style.setdefault('draw-background', 1) style.setdefault('draw-alpha-background', 0) style.setdefault('alpha-background', (1, 1, .5, .5)) k = { 'pos': pos, 'size': size } new_cache = GlDisplayList() with new_cache: if state: set_color(*style['bg-color']) #hack becasue old widgets set this themselves linewidth = style.get('border-width') bordercolor = None if 'border-color' in style: bordercolor = style['border-color'] if style['border-radius'] > 0: k.update({ 'radius': style['border-radius'], 'precision': style['border-radius-precision'] }) if style['draw-background']: drawRoundedRectangle(**k) if style['draw-border']: if linewidth: glPushAttrib(GL_LINE_BIT) glLineWidth(linewidth) if bordercolor: with gx_color(*bordercolor): drawRoundedRectangle(style=GL_LINE_LOOP, **k) else: drawRoundedRectangle(style=GL_LINE_LOOP, **k) if linewidth: glPopAttrib() if style['draw-alpha-background']: drawRoundedRectangleAlpha(alpha=style['alpha-background'], **k) else: if style['draw-background']: drawRectangle(**k) if style['draw-border']: if linewidth: glPushAttrib(GL_LINE_BIT) glLineWidth(linewidth) if bordercolor: with gx_color(*bordercolor): drawRectangle(style=GL_LINE_LOOP, **k) else: drawRectangle(style=GL_LINE_LOOP, **k) if linewidth: glPopAttrib() if style['draw-alpha-background']: drawRectangleAlpha(alpha=style['alpha-background'], **k) # if the drawCSSRectangle is already inside a display list # compilation will not happen, but drawing yes. # so, store only if a cache is created ! if new_cache.is_compiled(): Cache.append('pymt.cssrect', cache_id, new_cache) new_cache.draw() if bg_image: bg_image.size = size bg_image.pos = pos bg_image.draw()
def __exit__(self, extype, value, traceback): glPopAttrib()
def release(self): '''Release the current attribute from the binded texture''' glPopAttrib()