def drawShape(self, shape): if shape.shape_type == Shape.NO_SHAPE: return elif shape.shape_type == Shape.TRIANGLE or shape.shape_type == Shape.QUAD or shape.shape_type == Shape.POINTLIST: gl.glPushMatrix() gl.glPushAttrib(gl.GL_ALL_ATTRIB_BITS) gl.glScale(shape.scale, shape.scale, 1.0) gl.glTranslatef(shape.translation[0], shape.translation[1], 0.0) if len(shape.points) > 0: if shape.texture: gl.glEnable(gl.GL_TEXTURE_2D) gl.glEnable(gl.GL_ALPHA_TEST) gl.glAlphaFunc(gl.GL_GREATER, 0.5) sfml.Texture.bind(shape.texture) if shape.shape_type == Shape.TRIANGLE: gl.glBegin(gl.GL_TRIANGLES) elif shape.shape_type == Shape.QUAD: gl.glBegin(gl.GL_QUADS) elif shape.shape_type == Shape.POINTLIST: gl.glBegin(gl.GL_LINE_LOOP) for point in range(len(shape.points)): if shape.texture: gl.glTexCoord2f(shape.texture_coordinates[point].x, shape.texture_coordinates[point].y) gl.glColor(shape.points[point].red, shape.points[point].green, shape.points[point].blue) gl.glVertex(shape.points[point].x, shape.points[point].y) gl.glEnd() if shape.texture: sfml.Texture.bind(None) gl.glDisable(gl.GL_TEXTURE_2D) gl.glPopAttrib() gl.glPopMatrix()
def gl_init( width, height ): global __legocaptex,__quadratic setviewport(width, height) __legocaptex = layer_manager.load_2d_texture(pygame.image.tostring(__image, "RGBA"), LEGO_CAP_WIDTH, LEGO_CAP_HEIGHT) __quadratic = GLU.gluNewQuadric() GLU.gluQuadricTexture(__quadratic, GLU.GLU_TRUE) GLU.gluQuadricDrawStyle(__quadratic,GLU.GLU_FILL) GLU.gluQuadricOrientation(__quadratic, GLU.GLU_OUTSIDE) GLU.gluQuadricNormals(__quadratic, GLU.GLU_SMOOTH) GL.glClearColor( 0.0, 0.2, 0.5, 1.0 ) GL.glEnable(GL.GL_POINT_SMOOTH) GL.glEnable(GL.GL_LINE_SMOOTH) GL.glEnable(GL.GL_TEXTURE_2D) GL.glEnable(GL.GL_ALPHA_TEST) GL.glEnable(GL.GL_COLOR_MATERIAL) GL.glDisable(GL.GL_CULL_FACE) GL.glAlphaFunc(GL.GL_GREATER,0.1) GL.glClearAccum(0.0, 0.0, 0.0, 1.0) GL.glClear(GL.GL_ACCUM_BUFFER_BIT) #GL.glGenLists(1) draw_mode_3d()
def display(self): #gl.glCallList(self.list_index) #gl.glPixelStorei(gl.GL_UNPACK_ALIGNMENT, 1) #gl.glAlphaFunc(gl.GL_GEQUAL, 0.5); #gl.glDisable(gl.GL_ALPHA_TEST); #gl.glEnable(gl.GL_DITHER); w,h = self.size gl.glEnable(gl.GL_ALPHA_TEST) gl.glAlphaFunc(gl.GL_GREATER,0) gl.glPixelStorei(gl.GL_PACK_ALIGNMENT, 1) gl.glPixelStorei(gl.GL_UNPACK_ALIGNMENT, 1) gl.glWindowPos3iv(self.position) #gl.glEnable(gl.GL_BLEND) #gl.glBlendFunc(gl.GL_SRC_ALPHA,gl.GL_ONE_MINUS_SRC_ALPHA) gl.glDrawPixels(w, h,gl.GL_RGBA, gl.GL_UNSIGNED_BYTE, self.data) gl.glDisable(gl.GL_BLEND)
def __init__(self, w, h): self.resolution = (w, h) self.screen = pygame.display.set_mode( (w, h), pygame.DOUBLEBUF | pygame.OPENGL | pygame.OPENGLBLIT) self.cam = None # enable alpha from RGBA texture gl.glEnable(gl.GL_ALPHA_TEST) gl.glAlphaFunc(gl.GL_NOTEQUAL, 0.0)
def loadTexture(path): image = Image.open(path).convert("RGBA") imageData = image.tobytes("raw", "RGBA", 0, -1) texture = gl.glGenTextures(1) gl.glPixelStorei(gl.GL_UNPACK_ALIGNMENT, 4) gl.glBindTexture(gl.GL_TEXTURE_2D, texture) gl.glEnable(gl.GL_BLEND) # включить смешивание gl.glEnable(gl.GL_ALPHA_TEST) # разрешить прозрачность gl.glAlphaFunc(gl.GL_GEQUAL, 0.4) # не пропускать прозрачность ниже 0.4 gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) # один из видов смешивания, подробнее читать в интернете gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_BASE_LEVEL, 0) gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAX_LEVEL, 0) gl.glTexImage2D(gl.GL_TEXTURE_2D, 0, gl.GL_RGBA, image.size[0], image.size[1], 0, gl.GL_RGBA, gl.GL_UNSIGNED_BYTE, imageData) return texture
def draw_chars(line, chars, color, pos): r,g,b = [x/255.0 for x in color] GL.glPushMatrix() GL.glPixelTransferf(GL.GL_RED_SCALE, r) GL.glPixelTransferf(GL.GL_GREEN_SCALE, g) GL.glPixelTransferf(GL.GL_BLUE_SCALE, b) GL.glAlphaFunc(GL.GL_GREATER, 0) pos = list(map(int, pos)) for c in line: data, w, h = chars[c] if data is None: continue # ignore unprintable chars GL.glRasterPos2i(pos[0], pos[1]+h) GL.glDrawPixels(w, h, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE, data) pos[0] += w GL.glPopMatrix()
def draw(self): gl.glClear(gl.GL_COLOR_BUFFER_BIT) if (self.test_option == gl.GL_SCISSOR_TEST): # тест отсечения gl.glScissor(0, 0, 300, 300) if (self.test_option == gl.GL_ALPHA_TEST): # тест прозрачности gl.glAlphaFunc(self.func_option, self.ref) if (self.test_option == gl.GL_BLEND): # тест смешивания цветов gl.glBlendFunc(self.sfactor_option, self.dfactor_option) gl.glEnable(self.test_option) gl.glBegin(gl.GL_TRIANGLES) for vertex in verticies: gl.glColor4f(vertex[2], vertex[3], vertex[4], vertex[5]) gl.glVertex2f(vertex[0], vertex[1]) gl.glEnd() gl.glDisable(self.test_option) gl.glFlush()
def draw_text(self, position=wx.Point(0, 0), scale=1.0, rotation=0): """ position (wx.Point) - x/y Position to draw in scene scale (float) - Scale rotation (int) - Rotation in degree Draws the text to the scene """ #Enable necessary functions GL.glColor(1, 1, 1, 1) GL.glEnable(GL.GL_TEXTURE_2D) GL.glEnable(GL.GL_ALPHA_TEST) #Enable alpha test GL.glAlphaFunc(GL.GL_GREATER, 0) GL.glEnable(GL.GL_BLEND) #Enable blending GL.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA) #Bind texture GL.glBindTexture(GL.GL_TEXTURE_2D, self._texture) ow, oh = self._text_size w, h = self._texture_size #Perform transformations GL.glPushMatrix() GL.glTranslated(position.x, position.y, 0) GL.glRotate(-rotation, 0, 0, 1) GL.glScaled(scale, scale, scale) if self._centered: GL.glTranslate(-w / 2, -oh / 2, 0) #Draw vertices GL.glBegin(GL.GL_QUADS) GL.glTexCoord2f(0, 0) GL.glVertex2f(0, 0) GL.glTexCoord2f(0, 1) GL.glVertex2f(0, h) GL.glTexCoord2f(1, 1) GL.glVertex2f(w, h) GL.glTexCoord2f(1, 0) GL.glVertex2f(w, 0) GL.glEnd() GL.glPopMatrix() #Disable features GL.glDisable(GL.GL_BLEND) GL.glDisable(GL.GL_ALPHA_TEST) GL.glDisable(GL.GL_TEXTURE_2D)
def renderImage(self, context): width, height = self.getSize() gl.glViewport(0, 0, width, height) gl.glDisable(gl.GL_SCISSOR_TEST) gl.glEnable(gl.GL_ALPHA_TEST) gl.glAlphaFunc(gl.GL_GREATER, 0) gl.glEnable(gl.GL_BLEND) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) self.frameBuffer.bind() self.renderer.render(context) self.frameBuffer.unbind() # TODO Restore blend state. Any other states that need restoring...? gl.glBlendFunc(gl.GL_ONE, gl.GL_ONE_MINUS_SRC_ALPHA)
def render_func(self): if not TRACEORBITS: gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) else: gl.glClear(gl.GL_DEPTH_BUFFER_BIT) if COLORMASK['r']: gl.glColorMask(gl.GL_TRUE, gl.GL_FALSE, gl.GL_FALSE, gl.GL_TRUE) elif COLORMASK['g']: gl.glColorMask(gl.GL_FALSE, gl.GL_TRUE, gl.GL_FALSE, gl.GL_TRUE) elif COLORMASK['b']: gl.glColorMask(gl.GL_FALSE, gl.GL_FALSE, gl.GL_TRUE, gl.GL_TRUE) else: gl.glColorMask(gl.GL_TRUE, gl.GL_TRUE, gl.GL_TRUE, gl.GL_TRUE) gl.glAlphaFunc(gl.GL_GREATER, 0) gl.glMatrixMode(gl.GL_MODELVIEW) gl.glLoadIdentity() gl.glTranslatef(self.trans['x'], self.trans['y'], self.trans['z']) self.adjust_zoom() self.adjust_rotation(1) gl.glDepthMask(gl.GL_FALSE) self.draw_system() gl.glDepthMask(gl.GL_TRUE) if RECORDSCREEN: self.record_screen() self.show_fps(1.0) glut.glutSwapBuffers() if (self.rotate['x'] != 0 or self.rotate['y'] != 0 or self.rotate['z'] != 0): glut.glutPostRedisplay()
def gl_draw(self, zpos, tex1, pos1, tex2, pos2): tex_angle=atan2(tex2[1]-tex1[1], tex2[0]-tex1[0]) tex_len=hypot(tex2[1]-tex1[1], tex2[0]-tex1[0]) pos_angle=atan2(pos2[1]-pos1[1], pos2[0]-pos1[0]) pos_len=hypot(pos2[1]-pos1[1], pos2[0]-pos1[0]) scale=pos_len/tex_len rot=pos_angle-tex_angle rotm=array([[cos(rot),-sin(rot)], [sin(rot), cos(rot)]],'d') rotscalem = rotm * scale GL.glEnable(GL.GL_TEXTURE_2D) if gl_use_blend: GL.glEnable(GL.GL_BLEND); GL.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA) else: GL.glEnable(GL.GL_ALPHA_TEST); GL.glAlphaFunc(GL.GL_GREATER, 0.05) GL.glBindTexture(GL.GL_TEXTURE_2D, self.texid) GL.glBegin(GL.GL_QUADS); for texx,texy in ((self.bbox[0], 1.0-self.bbox[1]), (self.bbox[0], 1.0-self.bbox[3]), (self.bbox[2], 1.0-self.bbox[3]), (self.bbox[2], 1.0-self.bbox[1])) : GL.glTexCoord2f(texx, texy); relpos = numpy.dot(rotscalem, (array([texx,texy]) - tex1)) + pos1 GL.glVertex3f(relpos[0], relpos[1], zpos) GL.glEnd() GL.glDisable(GL.GL_TEXTURE_2D) if gl_use_blend: GL.glDisable(GL.GL_BLEND) else: GL.glDisable(GL.GL_ALPHA_TEST)
def gl_draw(self, zpos, tex1, pos1, tex2, pos2): tex_angle = atan2(tex2[1] - tex1[1], tex2[0] - tex1[0]) tex_len = hypot(tex2[1] - tex1[1], tex2[0] - tex1[0]) pos_angle = atan2(pos2[1] - pos1[1], pos2[0] - pos1[0]) pos_len = hypot(pos2[1] - pos1[1], pos2[0] - pos1[0]) scale = pos_len / tex_len rot = pos_angle - tex_angle rotm = array([[cos(rot), -sin(rot)], [sin(rot), cos(rot)]], 'd') rotscalem = rotm * scale GL.glEnable(GL.GL_TEXTURE_2D) if gl_use_blend: GL.glEnable(GL.GL_BLEND) GL.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA) else: GL.glEnable(GL.GL_ALPHA_TEST) GL.glAlphaFunc(GL.GL_GREATER, 0.05) GL.glBindTexture(GL.GL_TEXTURE_2D, self.texid) GL.glBegin(GL.GL_QUADS) for texx, texy in ((self.bbox[0], 1.0 - self.bbox[1]), (self.bbox[0], 1.0 - self.bbox[3]), (self.bbox[2], 1.0 - self.bbox[3]), (self.bbox[2], 1.0 - self.bbox[1])): GL.glTexCoord2f(texx, texy) relpos = numpy.dot(rotscalem, (array([texx, texy]) - tex1)) + pos1 GL.glVertex3f(relpos[0], relpos[1], zpos) GL.glEnd() GL.glDisable(GL.GL_TEXTURE_2D) if gl_use_blend: GL.glDisable(GL.GL_BLEND) else: GL.glDisable(GL.GL_ALPHA_TEST)
def render_func(self): if not TRACEORBITS: gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) else: gl.glClear(gl.GL_DEPTH_BUFFER_BIT) if COLORMASK["r"]: gl.glColorMask(gl.GL_TRUE, gl.GL_FALSE, gl.GL_FALSE, gl.GL_TRUE) elif COLORMASK["g"]: gl.glColorMask(gl.GL_FALSE, gl.GL_TRUE, gl.GL_FALSE, gl.GL_TRUE) elif COLORMASK["b"]: gl.glColorMask(gl.GL_FALSE, gl.GL_FALSE, gl.GL_TRUE, gl.GL_TRUE) else: gl.glColorMask(gl.GL_TRUE, gl.GL_TRUE, gl.GL_TRUE, gl.GL_TRUE) gl.glAlphaFunc(gl.GL_GREATER, 0) gl.glMatrixMode(gl.GL_MODELVIEW) gl.glLoadIdentity() gl.glTranslatef(self.trans["x"], self.trans["y"], self.trans["z"]) self.adjust_zoom() self.adjust_rotation(1) gl.glDepthMask(gl.GL_FALSE) self.draw_system() gl.glDepthMask(gl.GL_TRUE) if RECORDSCREEN: self.record_screen() self.show_fps(1.0) glut.glutSwapBuffers() if self.rotate["x"] != 0 or self.rotate["y"] != 0 or self.rotate["z"] != 0: glut.glutPostRedisplay()
def _DrawPoints(self): # get colors (use color from edge or face if not present) clr1 = getColor(self.mc) clr2 = getColor(self.mec) # draw face or edge? drawFace = bool(self.mc) # if not ms or mw we would not get here drawEdge = self.mec and self.mew if not drawFace and not drawEdge: return # get figure f = self.GetFigure() if not f: return # init blending. Only use constant blendfactor when alpha<1 gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) if self._alpha1 < 1: if getOpenGlCapable('1.4', 'transparant points and lines'): gl.glBlendFunc(gl.GL_CONSTANT_ALPHA, gl.GL_ONE_MINUS_CONSTANT_ALPHA) gl.glBlendColor(0.0, 0.0, 0.0, self._alpha1) gl.glDisable(gl.GL_DEPTH_TEST) # init vertex array gl.glEnableClientState(gl.GL_VERTEX_ARRAY) gl.glVertexPointerf(self._points.data) # points drawn on top of points should draw (because we draw # the face and edge seperately) gl.glDepthFunc(gl.GL_LEQUAL) # Enable alpha test, such that fragments with 0 alpha # will not update the z-buffer. gl.glEnable(gl.GL_ALPHA_TEST) gl.glAlphaFunc(gl.GL_GREATER, 0.01) if self.ms in ['o', '.', 's'] and not drawEdge: # Use standard OpenGL points, faster and anti-aliased # Pure filled points or squares always work. # choose style if self.ms == 's': gl.glDisable(gl.GL_POINT_SMOOTH) else: gl.glEnable(gl.GL_POINT_SMOOTH) # draw faces only if drawFace: gl.glColor3f(clr1[0], clr1[1], clr1[2]) gl.glPointSize(self.mw) gl.glDrawArrays(gl.GL_POINTS, 0, len(self._points)) elif self.ms in ['o', '.', 's'] and drawFace and self.alpha == 1: # Use standard OpenGL points, faster and anti-aliased # If alpha=1 and we have a filled marker, we can draw in two steps. # choose style if self.ms == 's': gl.glDisable(gl.GL_POINT_SMOOTH) else: gl.glEnable(gl.GL_POINT_SMOOTH) # draw edges if drawEdge: gl.glColor3f(clr2[0], clr2[1], clr2[2]) gl.glPointSize(self.mw + self.mew * 2) gl.glDrawArrays(gl.GL_POINTS, 0, len(self._points)) # draw faces if drawFace: gl.glColor3f(clr1[0], clr1[1], clr1[2]) gl.glPointSize(self.mw) gl.glDrawArrays(gl.GL_POINTS, 0, len(self._points)) #elif self.alpha>0: else: # Use sprites # get sprites tmp = f._markerManager.GetSprites(self.ms, self.mw, self.mew) pSize, sprite1, sprite2 = tmp gl.glPointSize(pSize) # draw points for the edges if drawEdge: sprite2.Enable() gl.glColor3f(clr2[0], clr2[1], clr2[2]) gl.glDrawArrays(gl.GL_POINTS, 0, len(self._points)) # draw points for the faces if drawFace: sprite1.Enable() gl.glColor3f(clr1[0], clr1[1], clr1[2]) gl.glDrawArrays(gl.GL_POINTS, 0, len(self._points)) # disable sprites sprite1.Disable() # Could as well have used sprite2 # clean up gl.glDisable(gl.GL_ALPHA_TEST) gl.glEnable(gl.GL_DEPTH_TEST) gl.glDisableClientState(gl.GL_VERTEX_ARRAY) gl.glDepthFunc(gl.GL_LESS) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA)
def initializeGL(self, *args, **kwargs): GL.glEnableClientState(GL.GL_VERTEX_ARRAY) GL.glAlphaFunc(GL.GL_NOTEQUAL, 0) GL.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA) GL.glEnable(GL.GL_DEPTH_TEST)
def reset(self, splash=None, caption=("", "")): pygame.key.set_repeat(500, 100) try: display.gl_set_attribute(pygame.GL_SWAP_CONTROL, config.settings.vsync.get()) except Exception as e: logging.warning('Unable to set vertical sync: {0!r}'.format(e)) display.gl_set_attribute(pygame.GL_ALPHA_SIZE, 8) if DEBUG_WM: print "config.settings.windowMaximized.get()", config.settings.windowMaximized.get() wwh = self.getWindowSize() if DEBUG_WM: print "wwh 1", wwh d = display.set_mode(wwh, self.displayMode()) # Let initialize OpenGL stuff after the splash. GL.glEnableClientState(GL.GL_VERTEX_ARRAY) GL.glAlphaFunc(GL.GL_NOTEQUAL, 0) GL.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA) # textures are 256x256, so with this we can specify pixel coordinates # GL.glMatrixMode(GL.GL_TEXTURE) # GL.glScale(1 / 256., 1 / 256., 1 / 256.) display.set_caption(*caption) if mcplatform.WindowHandler: self.win = mcplatform.WindowHandler(mode=self.displayMode()) # The following Windows specific code won't be executed if we're using '--debug-wm' switch. if not USE_WM and sys.platform == 'win32' and config.settings.setWindowPlacement.get(): config.settings.setWindowPlacement.set(False) config.save() X, Y = config.settings.windowX.get(), config.settings.windowY.get() if X: hwndOwner = display.get_wm_info()['window'] flags, showCmd, ptMin, ptMax, rect = mcplatform.win32gui.GetWindowPlacement(hwndOwner) realW = rect[2] - rect[0] realH = rect[3] - rect[1] showCmd = config.settings.windowShowCmd.get() rect = (X, Y, X + realW, Y + realH) mcplatform.win32gui.SetWindowPlacement(hwndOwner, (0, showCmd, ptMin, ptMax, rect)) config.settings.setWindowPlacement.set(True) config.save() elif self.win: maximized = config.settings.windowMaximized.get() if DEBUG_WM: print "maximized", maximized if maximized: geom = self.win.get_root_rect() in_w, in_h = self.win.get_size() x, y = int((geom[2] - in_w) / 2), int((geom[3] - in_h) / 2) os.environ['SDL_VIDEO_CENTERED'] = '1' else: os.environ['SDL_VIDEO_CENTERED'] = '0' x, y = config.settings.windowX.get(), config.settings.windowY.get() wwh = self.win.get_size() if DEBUG_WM: print "x", x, "y", y print "wwh 2", wwh if splash: # Setup the OGL display GL.glLoadIdentity() GLU.gluOrtho2D(0, wwh[0], 0, wwh[1]) GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT | GL.GL_ACCUM_BUFFER_BIT | GL.GL_STENCIL_BUFFER_BIT) swh = splash.get_size() _x, _y = (wwh[0] / 2 - swh[0] / 2, wwh[1] / 2 - swh[1] / 2) w, h = swh try: data = image.tostring(splash, 'RGBA_PREMULT', 1) except ValueError: data = image.tostring(splash, 'RGBA', 1) except ValueError: data = image.tostring(splash, 'RGB', 1) # Set the raster position GL.glRasterPos(_x, _y) GL.glDrawPixels(w, h, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE, numpy.fromstring(data, dtype='uint8')) if splash: display.flip() if self.win: if not maximized: wwh = self.getWindowSize() if DEBUG_WM: print "wwh 3", wwh self.win.set_position((x, y), update=True) if DEBUG_WM: print "* self.win.get_position()", self.win.get_position() try: #iconpath = os.path.join(directories.getDataDir(), 'favicon.png') iconpath = directories.getDataFile('favicon.png') iconfile = file(iconpath, 'rb') icon = pygame.image.load(iconfile, 'favicon.png') display.set_icon(icon) except Exception as e: logging.warning('Unable to set icon: {0!r}'.format(e)) # Let initialize OpenGL stuff after the splash. # GL.glEnableClientState(GL.GL_VERTEX_ARRAY) # GL.glAlphaFunc(GL.GL_NOTEQUAL, 0) # GL.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA) # textures are 256x256, so with this we can specify pixel coordinates GL.glMatrixMode(GL.GL_TEXTURE) GL.glScale(1 / 256., 1 / 256., 1 / 256.) self.display = d self.loadTextures()
def reset(self, splash=None, caption=("", "")): pygame.key.set_repeat(500, 100) try: display.gl_set_attribute(pygame.GL_SWAP_CONTROL, config.settings.vsync.get()) except Exception as e: logging.warning('Unable to set vertical sync: {0!r}'.format(e)) display.gl_set_attribute(pygame.GL_ALPHA_SIZE, 8) if DEBUG_WM: print "config.settings.windowMaximized.get()", config.settings.windowMaximized.get() wwh = self.getWindowSize() if DEBUG_WM: print "wwh 1", wwh d = display.set_mode(wwh, self.displayMode()) # Let initialize OpenGL stuff after the splash. GL.glEnableClientState(GL.GL_VERTEX_ARRAY) GL.glAlphaFunc(GL.GL_NOTEQUAL, 0) GL.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA) # textures are 256x256, so with this we can specify pixel coordinates # GL.glMatrixMode(GL.GL_TEXTURE) # GL.glScale(1 / 256., 1 / 256., 1 / 256.) display.set_caption(*caption) if mcplatform.WindowHandler: self.win = mcplatform.WindowHandler(mode=self.displayMode()) # The following Windows specific code won't be executed if we're using '--debug-wm' switch. if not USE_WM and sys.platform == 'win32' and config.settings.setWindowPlacement.get(): config.settings.setWindowPlacement.set(False) config.save() X, Y = config.settings.windowX.get(), config.settings.windowY.get() if X: hwndOwner = display.get_wm_info()['window'] flags, showCmd, ptMin, ptMax, rect = mcplatform.win32gui.GetWindowPlacement(hwndOwner) realW = rect[2] - rect[0] realH = rect[3] - rect[1] showCmd = config.settings.windowShowCmd.get() rect = (X, Y, X + realW, Y + realH) mcplatform.win32gui.SetWindowPlacement(hwndOwner, (0, showCmd, ptMin, ptMax, rect)) config.settings.setWindowPlacement.set(True) config.save() elif self.win: maximized = config.settings.windowMaximized.get() if DEBUG_WM: print "maximized", maximized if maximized: geom = self.win.get_root_rect() in_w, in_h = self.win.get_size() x, y = int((geom[2] - in_w) / 2), int((geom[3] - in_h) / 2) os.environ['SDL_VIDEO_CENTERED'] = '1' else: os.environ['SDL_VIDEO_CENTERED'] = '0' x, y = config.settings.windowX.get(), config.settings.windowY.get() wwh = self.win.get_size() if DEBUG_WM: print "x", x, "y", y print "wwh 2", wwh if splash: # Setup the OGL display GL.glLoadIdentity() GLU.gluOrtho2D(0, wwh[0], 0, wwh[1]) GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT | GL.GL_ACCUM_BUFFER_BIT | GL.GL_STENCIL_BUFFER_BIT) swh = splash.get_size() _x, _y = (wwh[0] / 2 - swh[0] / 2, wwh[1] / 2 - swh[1] / 2) w, h = swh try: data = image.tostring(splash, 'RGBA_PREMULT', 1) except ValueError: data = image.tostring(splash, 'RGBA', 1) except ValueError: data = image.tostring(splash, 'RGB', 1) # Set the raster position GL.glRasterPos(_x, _y) GL.glDrawPixels(w, h, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE, numpy.fromstring(data, dtype='uint8')) if splash: display.flip() if self.win: if not maximized: wwh = self.getWindowSize() if DEBUG_WM: print "wwh 3", wwh self.win.set_position((x, y), update=True) if DEBUG_WM: print "* self.win.get_position()", self.win.get_position() try: iconpath = os.path.join(directories.getDataDir(), 'favicon.png') iconfile = file(iconpath, 'rb') icon = pygame.image.load(iconfile, 'favicon.png') display.set_icon(icon) except Exception as e: logging.warning('Unable to set icon: {0!r}'.format(e)) # Let initialize OpenGL stuff after the splash. # GL.glEnableClientState(GL.GL_VERTEX_ARRAY) # GL.glAlphaFunc(GL.GL_NOTEQUAL, 0) # GL.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA) # textures are 256x256, so with this we can specify pixel coordinates GL.glMatrixMode(GL.GL_TEXTURE) GL.glScale(1 / 256., 1 / 256., 1 / 256.) self.display = d self.loadTextures()
def load_image(self): img=self.img if self.invert == True: rgbi=iops.invert(img.convert('RGB')) else: rgbi=img.convert('RGB') rgbai=rgbi.convert('RGBA') if self.alpha != None: rgbai.putalpha(self.alpha) if self.rm_blackish: for x,y in np.ndindex(self.size[0],self.size[1]): r,g,b,a=rgbai.getpixel((x,y)) #print x,y,r,g,b,a if r < 10 and g < 10 and b < 10: #print x,y,r,g,b,a rgbai.putpixel((x,y),(0,0,0,0)) #for x,y in self.data=rgbai.tostring() x,y,width,height = gl.glGetIntegerv(gl.GL_VIEWPORT) width,height = width,height self.win_size=(width,height) self.list_index = gl.glGenLists(1) gl.glNewList( self.list_index,gl.GL_COMPILE) w,h = self.size gl.glEnable(gl.GL_ALPHA_TEST) gl.glAlphaFunc(gl.GL_GREATER,0) gl.glPixelStorei(gl.GL_PACK_ALIGNMENT, 1) gl.glPixelStorei(gl.GL_UNPACK_ALIGNMENT, 1) gl.glWindowPos3iv(self.position) gl.glDrawPixels(w, h,gl.GL_RGBA, gl.GL_UNSIGNED_BYTE, self.data) gl.glDisable(gl.GL_BLEND) gl.glEndList()
y=config.settings.windowY.get()) # self.win = win dis.sync() try: iconpath = os.path.join(directories.getDataDir(), 'favicon.png') iconfile = file(iconpath, 'rb') icon = pygame.image.load(iconfile, 'favicon.png') display.set_icon(icon) except Exception, e: logging.warning('Unable to set icon: {0!r}'.format(e)) self.display = d GL.glEnableClientState(GL.GL_VERTEX_ARRAY) GL.glAlphaFunc(GL.GL_NOTEQUAL, 0) GL.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA) # textures are 256x256, so with this we can specify pixel coordinates GL.glMatrixMode(GL.GL_TEXTURE) GL.glScale(1 / 256., 1 / 256., 1 / 256.) self.loadTextures() def getTerrainTexture(self, level): return self.terrainTextures.get(level.materials.name, self.terrainTextures["Alpha"]) def loadTextures(self): self.terrainTextures = {}
class GLDisplayContext(object): def __init__(self, splash=None): self.reset(splash) @staticmethod def getWindowSize(): w, h = (config.settings.windowWidth.get(), config.settings.windowHeight.get()) return max(20, w), max(20, h) @staticmethod def displayMode(): return pygame.OPENGL | pygame.RESIZABLE | pygame.DOUBLEBUF def reset(self, splash=None): pygame.key.set_repeat(500, 100) try: display.gl_set_attribute(pygame.GL_SWAP_CONTROL, config.settings.vsync.get()) except Exception, e: logging.warning('Unable to set vertical sync: {0!r}'.format(e)) display.gl_set_attribute(pygame.GL_ALPHA_SIZE, 8) wwh = self.getWindowSize() d = display.set_mode(wwh, self.displayMode()) GL.glEnableClientState(GL.GL_VERTEX_ARRAY) GL.glAlphaFunc(GL.GL_NOTEQUAL, 0) GL.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA) # textures are 256x256, so with this we can specify pixel coordinates GL.glMatrixMode(GL.GL_TEXTURE) GL.glScale(1 / 256., 1 / 256., 1 / 256.) if splash: swh = splash.get_size() x, y = (wwh[0] / 2 - swh[0] / 2, wwh[1] / 2 - swh[1] / 2) w, h = swh data = image.tostring(splash, 'RGBA', 1) GL.glWindowPos2d(x, y) GL.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA) GL.glDrawPixels(w, h, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE, numpy.fromstring(data, dtype='uint8')) display.flip() display.set_caption('MCEdit ~ ' + release.get_version(), 'MCEdit') if sys.platform == 'win32' and config.settings.setWindowPlacement.get( ): config.settings.setWindowPlacement.set(False) config.save() X, Y = config.settings.windowX.get(), config.settings.windowY.get() if X: hwndOwner = display.get_wm_info()['window'] flags, showCmd, ptMin, ptMax, rect = mcplatform.win32gui.GetWindowPlacement( hwndOwner) realW = rect[2] - rect[0] realH = rect[3] - rect[1] showCmd = config.settings.windowShowCmd.get() rect = (X, Y, X + realW, Y + realH) mcplatform.win32gui.SetWindowPlacement( hwndOwner, (0, showCmd, ptMin, ptMax, rect)) config.settings.setWindowPlacement.set(True) config.save() elif sys.platform == 'linux2' and mcplatform.hasXlibDisplay: dis = mcplatform.Xlib.display.Display() root = dis.screen().root windowIDs = root.get_full_property( dis.intern_atom('_NET_CLIENT_LIST'), mcplatform.Xlib.X.AnyPropertyType).value for windowID in windowIDs: window = dis.create_resource_object('window', windowID) name = window.get_wm_name() if "MCEdit ~ Unified" in name: win = window win.configure(x=config.settings.windowX.get(), y=config.settings.windowY.get()) self.win = win dis.sync() try: iconpath = os.path.join(directories.getDataDir(), 'favicon.png') iconfile = file(iconpath, 'rb') icon = pygame.image.load(iconfile, 'favicon.png') display.set_icon(icon) except Exception, e: logging.warning('Unable to set icon: {0!r}'.format(e))
def _DrawPoints(self): # get colors (use color from edge or face if not present) clr1 = getColor(self.mc) clr2 = getColor(self.mec) # draw face or edge? drawFace = bool(self.mc) # if not ms or mw we would not get here drawEdge = self.mec and self.mew if not drawFace and not drawEdge: return # get figure f = self.GetFigure() if not f: return # init blending. Only use constant blendfactor when alpha<1 gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) if self._alpha1<1: if getOpenGlCapable('1.4','transparant points and lines'): gl.glBlendFunc(gl.GL_CONSTANT_ALPHA, gl.GL_ONE_MINUS_CONSTANT_ALPHA) gl.glBlendColor(0.0,0.0,0.0, self._alpha1) gl.glDisable(gl.GL_DEPTH_TEST) # init vertex array gl.glEnableClientState(gl.GL_VERTEX_ARRAY) gl.glVertexPointerf(self._points.data) # points drawn on top of points should draw (because we draw # the face and edge seperately) gl.glDepthFunc(gl.GL_LEQUAL) # Enable alpha test, such that fragments with 0 alpha # will not update the z-buffer. gl.glEnable(gl.GL_ALPHA_TEST) gl.glAlphaFunc(gl.GL_GREATER, 0.01) if self.ms in ['o','.','s'] and not drawEdge: # Use standard OpenGL points, faster and anti-aliased # Pure filled points or squares always work. # choose style if self.ms == 's': gl.glDisable(gl.GL_POINT_SMOOTH) else: gl.glEnable(gl.GL_POINT_SMOOTH) # draw faces only if drawFace: gl.glColor3f(clr1[0],clr1[1],clr1[2]) gl.glPointSize(self.mw) gl.glDrawArrays(gl.GL_POINTS, 0, len(self._points)) elif self.ms in ['o','.','s'] and drawFace and self.alpha==1: # Use standard OpenGL points, faster and anti-aliased # If alpha=1 and we have a filled marker, we can draw in two steps. # choose style if self.ms == 's': gl.glDisable(gl.GL_POINT_SMOOTH) else: gl.glEnable(gl.GL_POINT_SMOOTH) # draw edges if drawEdge: gl.glColor3f(clr2[0],clr2[1],clr2[2]) gl.glPointSize(self.mw+self.mew*2) gl.glDrawArrays(gl.GL_POINTS, 0, len(self._points)) # draw faces if drawFace: gl.glColor3f(clr1[0],clr1[1],clr1[2]) gl.glPointSize(self.mw) gl.glDrawArrays(gl.GL_POINTS, 0, len(self._points)) #elif self.alpha>0: else: # Use sprites # get sprites tmp = f._markerManager.GetSprites(self.ms, self.mw, self.mew) pSize, sprite1, sprite2 = tmp gl.glPointSize(pSize) # draw points for the edges if drawEdge: sprite2.Enable() gl.glColor3f(clr2[0],clr2[1],clr2[2]) gl.glDrawArrays(gl.GL_POINTS, 0, len(self._points)) # draw points for the faces if drawFace: sprite1.Enable() gl.glColor3f(clr1[0],clr1[1],clr1[2]) gl.glDrawArrays(gl.GL_POINTS, 0, len(self._points)) # disable sprites sprite1.Disable() # Could as well have used sprite2 # clean up gl.glDisable(gl.GL_ALPHA_TEST) gl.glEnable(gl.GL_DEPTH_TEST) gl.glDisableClientState(gl.GL_VERTEX_ARRAY) gl.glDepthFunc(gl.GL_LESS) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA)
class GLDisplayContext(object): def __init__(self, splash=None, caption=("", "")): self.win = None self.reset(splash, caption=caption) @staticmethod def getWindowSize(): w, h = (config.settings.windowWidth.get(), config.settings.windowHeight.get()) return max(20, w), max(20, h) @staticmethod def displayMode(): return pygame.OPENGL | pygame.RESIZABLE | pygame.DOUBLEBUF def reset(self, splash=None, caption=("", "")): pygame.key.set_repeat(500, 100) try: display.gl_set_attribute(pygame.GL_SWAP_CONTROL, config.settings.vsync.get()) except Exception, e: logging.warning('Unable to set vertical sync: {0!r}'.format(e)) display.gl_set_attribute(pygame.GL_ALPHA_SIZE, 8) if DEBUG_WM: print "config.settings.windowMaximized.get()", config.settings.windowMaximized.get() wwh = self.getWindowSize() if DEBUG_WM: print "wwh 1", wwh d = display.set_mode(wwh, self.displayMode()) GL.glEnableClientState(GL.GL_VERTEX_ARRAY) GL.glAlphaFunc(GL.GL_NOTEQUAL, 0) GL.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA) # textures are 256x256, so with this we can specify pixel coordinates GL.glMatrixMode(GL.GL_TEXTURE) GL.glScale(1 / 256., 1 / 256., 1 / 256.) display.set_caption(*caption) if mcplatform.WindowHandler: self.win = mcplatform.WindowHandler(mode=self.displayMode()) # The following Windows specific code won't be executed if we're using '--debug-wm' switch. if not USE_WM and sys.platform == 'win32' and config.settings.setWindowPlacement.get(): config.settings.setWindowPlacement.set(False) config.save() X, Y = config.settings.windowX.get(), config.settings.windowY.get() if X: hwndOwner = display.get_wm_info()['window'] flags, showCmd, ptMin, ptMax, rect = mcplatform.win32gui.GetWindowPlacement(hwndOwner) realW = rect[2] - rect[0] realH = rect[3] - rect[1] showCmd = config.settings.windowShowCmd.get() rect = (X, Y, X + realW, Y + realH) mcplatform.win32gui.SetWindowPlacement(hwndOwner, (0, showCmd, ptMin, ptMax, rect)) config.settings.setWindowPlacement.set(True) config.save() elif self.win: maximized = config.settings.windowMaximized.get() if DEBUG_WM: print "maximized", maximized if maximized: geom = self.win.get_root_rect() in_w, in_h = self.win.get_size() x, y = int((geom[2] - in_w) / 2), int((geom[3] - in_h) / 2) os.environ['SDL_VIDEO_CENTERED'] = '1' else: os.environ['SDL_VIDEO_CENTERED'] = '0' x, y = config.settings.windowX.get(), config.settings.windowY.get() wwh = self.win.get_size() if DEBUG_WM: print "x", x, "y", y print "wwh 2", wwh if splash: GL.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA) GL.glWindowPos2d(0, 0) back = Surface(wwh) back.fill((0, 0, 0)) GL.glDrawPixels(wwh[0], wwh[1], GL.GL_RGBA, GL.GL_UNSIGNED_BYTE, numpy.fromstring(image.tostring(back, 'RGBA'), dtype='uint8')) swh = splash.get_size() _x, _y = (wwh[0] / 2 - swh[0] / 2, wwh[1] / 2 - swh[1] / 2) w, h = swh data = image.tostring(splash, 'RGBA', 1) GL.glWindowPos2d(_x, _y) GL.glDrawPixels(w, h, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE, numpy.fromstring(data, dtype='uint8')) if splash: display.flip() if self.win: if not maximized: wwh = self.getWindowSize() if DEBUG_WM: print "wwh 3", wwh self.win.set_position((x, y), update=True) if DEBUG_WM: print "* self.win.get_position()", self.win.get_position() try: iconpath = os.path.join(directories.getDataDir(), 'favicon.png') iconfile = file(iconpath, 'rb') icon = pygame.image.load(iconfile, 'favicon.png') display.set_icon(icon) except Exception, e: logging.warning('Unable to set icon: {0!r}'.format(e))