def __init__(self, filename=None, repeat=False, fill_color=(1,1,1,1), fill_size=(2,2), fill_unique=False): """Create a texture filename can be be a filename for an image, or a pygame.Surface object repeat controls whether the texture repeats when values overflow the 0-1 range fill_color is the color the texture will be filled with if filename is None fill_size is the size the texture will be if filename is None fill_unique controls whether the texture is unique if filename is None""" view.require_init() self.filename = filename self.unique = False self.gl_tex = None self.size = (0,0) self.unique = True self.size_mult = (1,1) self.repeat = repeat if not filename: self.filename = "BlankTexture: %s | %s"%(fill_color, fill_size) self.make_blank(fill_color, fill_size, fill_unique) elif type(filename) is type(""): self._load_file() else: self.make_gl_tex() self.filename = "UniqueTexture: %s"%self.gl_tex self._compile(filename) print self.filename, self.gl_tex
def __init__(self, size, pos=(0,0,0), rotation=(0,0,0), colorize=(1,1,1,1), texture=None): """Create the Quad size is the quad pos is the position of the quad rotation is the rotation of the quad colorize is the color of the quad texture can be None, a string filename of an image to load or a data.Texture object - entire texture is mapped to the face""" view.require_init() self.size = size self.pos = pos self.rotation = rotation if not texture: texture = blank_texture if type(texture) is type(""): texture = Texture(texture) self.texture = texture self.colorize = colorize self.scale = 1 self.display_list = data.DisplayList() self.visible = True self.pickable = True self.outline = False self.outline_size = 4 self.outline_color=(1,0,0) self._compile()
def __init__(self, frames=[], pos=(0,0), rotation=(0,0,0), scale=1, colorize=None): """Create the Animation frames must be a list/tuple of [Image, duration] objects pos is the 2d position of the image rotation is the 3d rotation of the image scale is the scale factor for the image colorize is the color of the image""" view.require_init() self.frames = frames self.pos = pos self.rotation = rotation self.scale = scale self.colorize = colorize self.cur = 0 self.ptime = time.time() self.running = True self.breakpoint = len(self.frames)-1 self.startpoint = 0 self.reversed = False self.looping = True self.visible = True self.filename = None self.outline = False self.outline_size = 4 self.outline_color=(1,0,0)
def GridSpriteSheet3D(filename, frames=(1, 1), duration=100, pos=(0, 0, 0), rotation=(0, 0, 0), scale=1, colorize=(1, 1, 1, 1)): """Load a "spritesheet" (basically, a flat 2d image that holds a lot of different images) into an Animation object. filename must be the name of an image on disk frames must be a tuple/list of two ints, indicating the number of frames in the x/y axis duration must be a number representing the duration (in milliseconds) of all frames pos is the 2d position of the image rotation is the 3d rotation of the image scale is the scale factor for the image colorize is the color of the image""" view.require_init() new = [] image = pygame.image.load(filename).convert_alpha() x_size = int(image.get_width() / frames[0]) y_size = int(image.get_height() / frames[1]) for x in xrange(frames[0]): for y in xrange(frames[1]): new.append([ Image3D( image.subsurface(x * x_size, y * y_size, x_size, y_size)), duration * 0.001 ]) return Animation3D(new, pos, rotation, scale, colorize)
def __init__(self, size=(1,1), color=(1,1,1,1)): """Create an empty data.Texture size must be a two part tuple representing the pixel size of the texture color must be a four-part tuple representing the (RGBA 0-1) color of the texture""" view.require_init() # It seems to need init on python2.6 self.size = size self.filename = repr(size)+repr(color) self.gl_tex = None if self.filename in self._all_loaded: tex = self._all_loaded[self.filename][0] self.size = tex.size self.gl_tex = tex.gl_tex self._all_loaded[self.filename].append(self) else: i = pygame.Surface(size) if len(color) == 4: r, g, b, a = color else: r, g, b = color a = 1 r *= 255 g *= 255 b *= 255 a *= 255 i.fill((r,g,b,a)) self.gl_tex = glGenTextures(1) self._compile(i) self._all_loaded[self.filename] = [self]
def __init__(self, frames=[], pos=(0, 0), rotation=(0, 0, 0), scale=1, colorize=None): """Create the Animation frames must be a list/tuple of [Image, duration] objects pos is the 2d position of the image rotation is the 3d rotation of the image scale is the scale factor for the image colorize is the color of the image""" view.require_init() self.frames = frames self.pos = pos self.rotation = rotation self.scale = scale self.colorize = colorize self.cur = 0 self.ptime = time.time() self.running = True self.breakpoint = len(self.frames) - 1 self.startpoint = 0 self.reversed = False self.looping = True self.visible = True self.filename = None self.outline = False self.outline_size = 4 self.outline_color = (1, 0, 0)
def SpriteSheet3D(filename, frames=[], durations=[], pos=(0, 0), rotation=(0, 0, 0), scale=1, colorize=(1, 1, 1, 1)): """Load a "spritesheet" (basically, a flat 2d image that holds a lot of different images) into an Animation3D object. filename must be the name of an image on disk frames must be a tuple/list of [x,y,width,height] portions of the image that are unique frames durations must be a number or list/tuple of numbers representing the duration (in milliseconds) of all/each frame pos is the 3d position of the image rotation is the 3d rotation of the image scale is the scale factor for the image colorize is the color of the image""" view.require_init() if type(durations) in [type(1), type(1.2)]: durations = [durations] * len(frames) new = [] image = pygame.image.load(filename).convert_alpha() for (frame, dur) in zip(frames, durations): new.append([Image3D(image.subsurface(*frame)), dur * 0.001]) return Animation3D(new, pos, rotation, scale, colorize)
def __init__(self, size, pos=(0,0,0), rotation=(0,0,0), colorize=(1,1,1,1), texture=None, detail=30): """Create the Sphere size is the radius of the Sphere pos ithe position of the sphere rotation is the rotation of the sphere colorize is the color of the sphere texture can be None, a string filename of an image to load or a data.Texture object that will be mapped to the sphere detail is the level of detail for the Sphere, higher = a more smooth sphere""" view.require_init() self.size = size self.pos = pos self.rotation = rotation self.colorize = colorize if not texture: texture = blank_texture if type(texture) is type(""): texture = Texture(texture) self.texture = texture self.detail = detail self.scale = 1 self.display_list = data.DisplayList() self.visible = True self.pickable = True self.outline = False self.outline_size = 4 self.outline_color=(1,0,0) self._compile()
def create_empty_image3d(size=(2, 2), color=(1, 1, 1, 1)): """Same as create_empty_texture, except returns an image.Image3D instead""" view.require_init() i = pygame.Surface(size).convert_alpha() if len(color) == 3: color = color + (1, ) i.fill((255, 255, 255, 255)) return Image3D(i, colorize=color)
def max_tex_size(): view.require_init() abs_max = 2**13 #max pygame can handle for surfaces! size = glGetIntegerv(GL_MAX_TEXTURE_SIZE) return min((size, abs_max))
def create_empty_image3d(size=(2,2), color=(1,1,1,1)): """Same as create_empty_texture, except returns an image.Image3D instead""" view.require_init() i = pygame.Surface(size).convert_alpha() if len(color) == 3: color = color + (1,) i.fill((255,255,255,255)) return Image3D(i, colorize=color)
def __init__(self, filename=None, font_char_height=32, font_char_height3d=0.1, internal_font_size=64): view.require_init() self.filename = filename self.font_obj = pygame.font.Font(self.filename, internal_font_size) self.font_char_height = font_char_height self.font_char_height3d = font_char_height3d self._build_tex()
def __init__(self, filename=None, size=32): """Create the font filename can be None or the filename of the font to load (TTF) size is the size of the font""" view.require_init() self.filename = filename self.size = size self.fontname = str(self.filename) + ":" + str(self.size) self._load_font()
def load_and_tile_resize_image(filename, size, pos=(0,0), rotation=(0,0,0), scale=1, colorize=(1,1,1,1), border_size=None): """Load an image, resize it by tiling (ie, each image is 9 tiles, and then the parts are scaled so that it fits or greator than size) filename must be the filename of the image to load size must be the (x, y) size of the image (may be larger) pos is the 2d position of the image rotation is the 3d rotation of the image scale is the scale factor of the image colorize is the color of the image Returns Image, tile_size""" view.require_init() image = pygame.image.load(filename).convert_alpha() x, y = size if x < image.get_width(): x = image.get_width() if y < image.get_height(): y = image.get_height() size = x, y if border_size: if border_size > int(min(image.get_size())/3): border_size = int(min(image.get_size())/3) x1=min((border_size, int(image.get_width()/3))) y1=min((border_size, int(image.get_height()/3))) x2 = image.get_width()-x1*2 y2 = image.get_height()-y1*2 else: x1=x2=int(image.get_width()/3) y1=y2=int(image.get_height()/3) topleft = image.subsurface((0, 0), (x1, y1)) top = pygame.transform.scale(image.subsurface((x1, 0), (x2, y1)), (size[0]-x1*2, y1)) topright = image.subsurface((x1+x2, 0), (x1,y1)) left = pygame.transform.scale(image.subsurface((0, y1), (x1, y2)), (x1, size[1]-y1*2)) middle = pygame.transform.scale(image.subsurface((x1, y1), (x2,y2)), (size[0]-x1*2, size[1]-y1*2)) right = pygame.transform.scale(image.subsurface((x1+x2, y1), (x1,y2)), (x1, size[1]-y1*2)) botleft = image.subsurface((0, y1+y2), (x1,y1)) bottom = pygame.transform.scale(image.subsurface((x1, y1+y2), (x2, y1)), (size[0]-x1*2, y1)) botright = image.subsurface((x1+y1, y1+y2), (x1,y1)) new = pygame.Surface(size).convert_alpha() new.fill((0,0,0,0)) new.blit(topleft, (0, 0)) new.blit(top, (x1, 0)) new.blit(topright, (size[0]-x1, 0)) new.blit(left, (0, y1)) new.blit(middle, (x1,y1)) new.blit(right, (size[0]-x1, y1)) new.blit(botleft, (0, size[1]-y1)) new.blit(bottom, (x1, size[1]-y1)) new.blit(botright, (size[0]-x1, size[1]-y1)) return Image(new, pos, rotation, scale, colorize), (x1,y1)
def __init__(self, filename=None, size=32): """Create the font filename must be None or the filename of the font to load size is the size of the font""" view.require_init() self._filename = filename self._size = size self.rebuild_font() self.images = {}
def __init__(self, filename=None): """Create a texture filename can be be a filename for an image, or a pygame.Surface object""" view.require_init() self.filename = filename self.size = (0,0) if type(filename) is type(""): self._load_file() else: self._compile(filename)
def __init__(self, filename=None, size=32): """Create the font filename must be None or the filename of the font to load size is the size of the font""" view.require_init() self._filename = filename self._size = size self.images = {} self.acceptable = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ`1234567890-=+_)(*&^%$#@!~[]\\;',./<>?:\"{}| " self._load_font()
def __init__(self, size=(512,512), clear_color=(0,0,0,0)): """Create the FrameBuffer. size must be the (x,y) size of the buffer, will round up to the next power of two clear_color must be the (r,g,b) or (r,g,b,a) color of the background of the texture""" view.require_init() if not FBO_AVAILABLE: raise AttributeError("Frame buffer objects not available!") _x, _y = size x = y = 2 while x < _x: x *= 2 while y < _y: y *= 2 size = x, y self.size = size self.clear_color = clear_color self.texture = BlankTexture(self.size, self.clear_color) if not bool(glGenRenderbuffersEXT): print("glGenRenderbuffersEXT doesn't exist") exit() self.rbuffer = glGenRenderbuffersEXT(1) glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, self.rbuffer) glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, size[0], size[1]) self.fbuffer = glGenFramebuffersEXT(1) glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, self.fbuffer) glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, self.texture.gl_tex, 0) glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, self.rbuffer) self.worked = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT) == GL_FRAMEBUFFER_COMPLETE_EXT glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0) glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0)
def __init__(self, start=(0,0,0), end=(0,1,0), colorize=(1,1,1,1)): view.require_init() self.start = start self.end = end self.texture = BlankTexture() self.colorize = colorize self.display_list = data.DisplayList() self.visible = True self.pickable = True self.outline = False self.outline_size = 4 self.outline_color=(1,0,0) self._compile()
def __init__(self): """Create the object.""" view.require_init() #always! self.texture = BlankTexture() self.display_list = None self.visible = True self.pickable = True self.outline = False self.outline_size = 4 self.outline_color = (1,0,0) self.pos = (0,0,0) self.rotation = (0,0,0) self.scale = (1,1,1) self.dead_remove_from_scene = False
def __init__(self, filename, pos=(0, 0), rotation=(0, 0, 0), scale=1, colorize=(1, 1, 1, 1)): """Create the Image filename must be a filename to an image file, a pygame.Surface object or an image.Image to copy pos is the 2d position of the image rotation is the 3d rotation of the image scale is the scale factor for the image colorize is the color of the image""" view.require_init() self.filename = filename self.pos = pos if type(filename) is type(""): self._load_file() elif isinstance(filename, type(self)): self._pimage = filename._pimage self._pimage2 = filename._pimage2 self._image_size = filename._image_size self._altered_image_size = filename._altered_image_size self.rect = self._pimage.get_rect() self.to_be_blitted = list(filename.to_be_blitted) self.display_list = filename.display_list self.texture = filename.texture self.offset = filename.offset loaded = True else: self.compile_from_surface(filename) self.filename = None loaded = True self.to_be_blitted = [] self.rotation = rotation self.scale = scale self.colorize = colorize self.visible = True self.outline = False self.outline_size = 4 self.outline_color = (1, 0, 0)
def SpriteSheet3D(filename, frames=[], durations=[], pos=(0,0), rotation=(0,0,0), scale=1, colorize=(1,1,1,1)): """Load a "spritesheet" (basically, a flat 2d image that holds a lot of different images) into an Animation3D object. filename must be the name of an image on disk frames must be a tuple/list of [x,y,width,height] portions of the image that are unique frames durations must be a number or list/tuple of numbers representing the duration (in milliseconds) of all/each frame pos is the 3d position of the image rotation is the 3d rotation of the image scale is the scale factor for the image colorize is the color of the image""" view.require_init() if type(durations) in [type(1), type(1.2)]: durations = [durations]*len(frames) new = [] image = pygame.image.load(filename).convert_alpha() for (frame, dur) in zip(frames, durations): new.append([Image3D(image.subsurface(*frame)), dur*0.001]) return Animation3D(new, pos, rotation, scale, colorize)
def __init__(self, filename, pos=(0,0), rotation=(0,0,0), scale=1, colorize=(1,1,1,1)): """Create the Image filename must be a filename to an image file, a pygame.Surface object or an image.Image to copy pos is the 2d position of the image rotation is the 3d rotation of the image scale is the scale factor for the image colorize is the color of the image""" view.require_init() self.filename = filename self.pos = pos if type(filename) is type(""): self._load_file() elif isinstance(filename, type(self)): self._pimage = filename._pimage self._pimage2 = filename._pimage2 self._image_size = filename._image_size self._altered_image_size = filename._altered_image_size self.rect = self._pimage.get_rect() self.to_be_blitted = list(filename.to_be_blitted) self.display_list = filename.display_list self.texture = filename.texture self.offset = filename.offset loaded = True else: self.compile_from_surface(filename) self.filename = None loaded = True self.to_be_blitted = [] self.rotation = rotation self.scale = scale self.colorize = colorize self.visible = True self.outline = False self.outline_size = 4 self.outline_color=(1,0,0)
def __init__(self, lists, pos=(0,0,0), rotation=(0,0,0), verts=[], scale=1, colorize=(1,1,1,1)): """Create the mesh object lists is a list of [data.DisplayList, texture] objects holding the 3d rendering of the mesh pos must be a three-part tuple representing the position of the mesh rotation must be a three-part tuple representing the rotation of the mesh verts is a list of vertices in the mesh scale must be a number or three part tuple representing the scale value of the mesh colorize is a 4 part tuple representing the (RGBA 0-1) color of the mesh""" view.require_init() self.gl_lists = lists self.pos = pos self.rotation = rotation self.verts = verts self.scale = scale self.colorize = colorize self.visible = True self.pickable = True self.outline = False self.outline_size = 4 self.outline_color=(1,0,0)
def GridSpriteSheet3D(filename, frames=(1,1), duration=100, pos=(0,0,0), rotation=(0,0,0), scale=1, colorize=(1,1,1,1)): """Load a "spritesheet" (basically, a flat 2d image that holds a lot of different images) into an Animation object. filename must be the name of an image on disk frames must be a tuple/list of two ints, indicating the number of frames in the x/y axis duration must be a number representing the duration (in milliseconds) of all frames pos is the 2d position of the image rotation is the 3d rotation of the image scale is the scale factor for the image colorize is the color of the image""" view.require_init() new = [] image = pygame.image.load(filename).convert_alpha() x_size = int(image.get_width() / frames[0]) y_size = int(image.get_height() / frames[1]) for x in xrange(frames[0]): for y in xrange(frames[1]): new.append([Image3D(image.subsurface(x*x_size, y*y_size, x_size, y_size)), duration*0.001]) return Animation3D(new, pos, rotation, scale, colorize)
def create_empty_image3d(size=(2,2), color=(1,1,1,1)): """Same as create_empty_texture, except returns an image.Image3D instead""" view.require_init() i = data.Texture(fill_size=size, fill_color=color, fill_unique=True) return Image3D(i)
def GIFImage3D(filename, pos=(0,0,0), rotation=(0,0,0), scale=1, colorize=(1,1,1,1)): """Load a GIF image into an Animation3D object. filename must be the name of a gif image one disk pos is the 3d position of the image rotation is the 3d rotation of the image scale is the scale factor for the image colorize is the color of the image""" view.require_init() image = pilImage.open(filename) frames = [] pal = image.getpalette() base_palette = [] for i in range(0, len(pal), 3): rgb = pal[i:i+3] base_palette.append(rgb) all_tiles = [] try: while 1: if not image.tile: image.seek(0) if image.tile: all_tiles.append(image.tile[0][3][0]) image.seek(image.tell()+1) except EOFError: image.seek(0) all_tiles = tuple(set(all_tiles)) try: while 1: try: duration = image.info["duration"] except: duration = 100 duration *= .001 #convert to milliseconds! cons = False x0, y0, x1, y1 = (0, 0) + image.size if image.tile: tile = image.tile else: image.seek(0) tile = image.tile if len(tile) > 0: x0, y0, x1, y1 = tile[0][1] if all_tiles: if all_tiles in ((6,), (7,)): cons = True pal = image.getpalette() palette = [] for i in range(0, len(pal), 3): rgb = pal[i:i+3] palette.append(rgb) elif all_tiles in ((7, 8), (8, 7)): pal = image.getpalette() palette = [] for i in range(0, len(pal), 3): rgb = pal[i:i+3] palette.append(rgb) else: palette = base_palette else: palette = base_palette pi = pygame.image.fromstring(image.tostring(), image.size, image.mode) pi.set_palette(palette) if "transparency" in image.info: pi.set_colorkey(image.info["transparency"]) pi2 = pygame.Surface(image.size, SRCALPHA) if cons: for i in frames: pi2.blit(i[0], (0,0)) pi2.blit(pi, (x0, y0), (x0, y0, x1-x0, y1-y0)) frames.append([pi2, duration]) image.seek(image.tell()+1) except EOFError: pass new_frames = [] for i in frames: new_frames.append([Image3D(i[0]), i[1]]) return Animation3D(new_frames, pos, rotation, scale, colorize)
def load_and_tile_resize_image(filename, size, pos=(0, 0), rotation=(0, 0, 0), scale=1, colorize=(1, 1, 1, 1), border_size=None): """Load an image, resize it by tiling (ie, each image is 9 tiles, and then the parts are scaled so that it fits or greator than size) filename must be the filename of the image to load size must be the (x, y) size of the image (may be larger) pos is the 2d position of the image rotation is the 3d rotation of the image scale is the scale factor of the image colorize is the color of the image Returns Image, tile_size""" view.require_init() image = pygame.image.load(filename).convert_alpha() x, y = size if x < image.get_width(): x = image.get_width() if y < image.get_height(): y = image.get_height() size = x, y if border_size: if border_size > int(min(image.get_size()) / 3): border_size = int(min(image.get_size()) / 3) x1 = min((border_size, int(image.get_width() / 3))) y1 = min((border_size, int(image.get_height() / 3))) x2 = image.get_width() - x1 * 2 y2 = image.get_height() - y1 * 2 else: x1 = x2 = int(image.get_width() / 3) y1 = y2 = int(image.get_height() / 3) topleft = image.subsurface((0, 0), (x1, y1)) top = pygame.transform.scale(image.subsurface((x1, 0), (x2, y1)), (size[0] - x1 * 2, y1)) topright = image.subsurface((x1 + x2, 0), (x1, y1)) left = pygame.transform.scale(image.subsurface((0, y1), (x1, y2)), (x1, size[1] - y1 * 2)) middle = pygame.transform.scale(image.subsurface((x1, y1), (x2, y2)), (size[0] - x1 * 2, size[1] - y1 * 2)) right = pygame.transform.scale(image.subsurface((x1 + x2, y1), (x1, y2)), (x1, size[1] - y1 * 2)) botleft = image.subsurface((0, y1 + y2), (x1, y1)) bottom = pygame.transform.scale(image.subsurface((x1, y1 + y2), (x2, y1)), (size[0] - x1 * 2, y1)) botright = image.subsurface((x1 + y1, y1 + y2), (x1, y1)) new = pygame.Surface(size).convert_alpha() new.fill((0, 0, 0, 0)) new.blit(topleft, (0, 0)) new.blit(top, (x1, 0)) new.blit(topright, (size[0] - x1, 0)) new.blit(left, (0, y1)) new.blit(middle, (x1, y1)) new.blit(right, (size[0] - x1, y1)) new.blit(botleft, (0, size[1] - y1)) new.blit(bottom, (x1, size[1] - y1)) new.blit(botright, (size[0] - x1, size[1] - y1)) return Image(new, pos, rotation, scale, colorize), (x1, y1)
def OBJ(filename, pos=(0,0,0), rotation=(0,0,0), colorize=(1,1,1,1)): """Load a WaveFront OBJ mesh. filename must be the filename of the mesh to load pos/rotation/colorize are the starting attributes of the mesh object.""" view.require_init() objs = [] mtls = {} vertices = [] normals = [] texcoords = [] for line in open(filename, "r"): if line.startswith('#'): continue values = line.split() if not values: continue if values[0] in ("o","g"): objs.append(ObjGroup(values[1])) elif values[0] == 'v': vertices.append(map(float, values[1:4])) elif values[0] == 'vn': normals.append(map(float, values[1:4])) elif values[0] == 'vt': texcoords.append(map(float, values[1:3])) elif values[0] in ('usemtl', 'usemat'): objs[-1].material = mtls[values[1]] elif values[0] == 'mtllib': path = os.path.split(filename)[0] cur_mtl = None for line in open(os.path.join(path, values[1]), "r"): if line.startswith('#'): continue values = line.split() if not values: continue if values[0] == 'newmtl': cur_mtl = data.Material(values[1]) mtls[cur_mtl.name] = cur_mtl elif cur_mtl is None: raise ValueError, "mtl file doesn't start with newmtl stmt" elif values[0] == 'map_Kd': cur_mtl.texture = data.Texture(os.path.join(path, values[1])) elif values[0]=="Kd": cur_mtl.set_color(map(float, values[1:])) elif values[0] == 'f': face = [] texcs = [] norms = [] for v in values[1:]: w = v.split('/') face.append(int(w[0])) if len(w) >= 2 and len(w[1]) > 0: texcs.append(int(w[1])) else: texcs.append(0) if len(w) >= 3 and len(w[2]) > 0: norms.append(int(w[2])) else: norms.append(0) objs[-1].faces.append((face, norms, texcs)) fin = [] for i in objs: fin.append(i.compile(vertices, normals, texcoords)) return BasicMesh(fin, pos, rotation, 1, colorize)
def GIFImage3D(filename, pos=(0, 0, 0), rotation=(0, 0, 0), scale=1, colorize=(1, 1, 1, 1)): """Load a GIF image into an Animation3D object if PIL is available, otherwise falls back to a static Image3D. filename must be the name of a gif image one disk pos is the 3d position of the image rotation is the 3d rotation of the image scale is the scale factor for the image colorize is the color of the image""" view.require_init() if not PIL_AVAILABLE: return Image3D(filename, pos, rotation, scale, colorize) image = PIL.open(filename) frames = [] pal = image.getpalette() base_palette = [] for i in range(0, len(pal), 3): rgb = pal[i:i + 3] base_palette.append(rgb) all_tiles = [] try: while 1: if not image.tile: image.seek(0) if image.tile: all_tiles.append(image.tile[0][3][0]) image.seek(image.tell() + 1) except EOFError: image.seek(0) all_tiles = tuple(set(all_tiles)) try: while 1: try: duration = image.info["duration"] except: duration = 100 duration *= .001 #convert to milliseconds! cons = False x0, y0, x1, y1 = (0, 0) + image.size if image.tile: tile = image.tile else: image.seek(0) tile = image.tile if len(tile) > 0: x0, y0, x1, y1 = tile[0][1] if all_tiles: if all_tiles in ((6, ), (7, )): cons = True pal = image.getpalette() palette = [] for i in range(0, len(pal), 3): rgb = pal[i:i + 3] palette.append(rgb) elif all_tiles in ((7, 8), (8, 7)): pal = image.getpalette() palette = [] for i in range(0, len(pal), 3): rgb = pal[i:i + 3] palette.append(rgb) else: palette = base_palette else: palette = base_palette pi = pygame.image.fromstring(image.tostring(), image.size, image.mode) pi.set_palette(palette) if "transparency" in image.info: pi.set_colorkey(image.info["transparency"]) pi2 = pygame.Surface(image.size, SRCALPHA) if cons: for i in frames: pi2.blit(i[0], (0, 0)) pi2.blit(pi, (x0, y0), (x0, y0, x1 - x0, y1 - y0)) frames.append([pi2, duration]) image.seek(image.tell() + 1) except EOFError: pass new_frames = [] for i in frames: new_frames.append([Image3D(i[0]), i[1]]) return Animation3D(new_frames, pos, rotation, scale, colorize)
def OBJ(filename, pos=(0, 0, 0), rotation=(0, 0, 0), colorize=(10, 10, 10, 1)): """Load a WaveFront OBJ mesh. filename must be the filename of the mesh to load pos/rotation/colorize are the starting attributes of the mesh object.""" view.require_init() objs = [] mtls = {} vertices = [] normals = [] texcoords = [] print "*** creating object!" for line in open(filename, "r"): if line.startswith('#'): continue values = line.split() if not values: continue if values[0] in ("o", "g"): objs.append(ObjGroup(values[1])) elif values[0] == 'v': vertices.append(map(float, values[1:4])) elif values[0] == 'vn': normals.append(map(float, values[1:4])) elif values[0] == 'vt': texcoords.append(map(float, values[1:3])) elif values[0] in ('usemtl', 'usemat'): if len(objs) > 0: objs[-1].material = mtls[values[1]] elif values[0] == 'mtllib': path = os.path.split(filename)[0] cur_mtl = None for line in open(os.path.join(path, values[1]), "r"): if line.startswith('#'): continue values = line.split() if not values: continue if values[0] == 'newmtl': cur_mtl = data.Material(values[1]) mtls[cur_mtl.name] = cur_mtl elif cur_mtl is None: raise ValueError, "mtl file doesn't start with newmtl stmt" elif values[0] == 'map_Kd': cur_mtl.texture = data.Texture( os.path.join(path, values[1])) elif values[0] == "Kd": cur_mtl.set_color(map(float, values[1:])) elif values[0] == 'f': face = [] tcoords = [] norms = [] for v in values[1:]: w = v.split('/') face.append(int(w[0])) if len(w) >= 2 and len(w[1]) > 0: tcoords.append(int(w[1])) else: tcoords.append(0) if len(w) >= 3 and len(w[2]) > 0: norms.append(int(w[2])) else: norms.append(0) objs[-1].faces.append((face, norms, tcoords)) fin = [] for i in objs: fin.append(i.compile(vertices, normals, texcoords)) return BasicMesh(fin, pos, rotation, 1, colorize)
def OBJ(filename, pos=(0,0,0), rotation=(0,0,0), colorize=(1,1,1,1)): """Loads a Wavefront OBJ file. Returns a BasicMesh object representing the OBJ mesh.""" view.require_init() svertices = [] snormals = [] stexcoords = [] sfaces = [] material = None smtl = None for line in open(filename, "r"): if line.startswith('#'): continue values = line.split() if not values: continue if values[0] == 'v': v = map(float, values[1:4]) svertices.append(v) elif values[0] == 'vn': v = map(float, values[1:4]) snormals.append(v) elif values[0] == 'vt': stexcoords.append(map(float, values[1:3])) elif values[0] in ('usemtl', 'usemat'): material = values[1] elif values[0] == 'mtllib': path = os.path.split(filename)[0] smtl = {} mtl = None for line in open(os.path.join(path, values[1]), "r"): if line.startswith('#'): continue values = line.split() if not values: continue if values[0] == 'newmtl': smtl[values[1]] = None mtl = values[1] elif mtl is None: raise ValueError, "mtl file doesn't start with newmtl stmt" elif values[0] == 'map_Kd': tex = data.Texture(os.path.join(path, values[1])) smtl[mtl] = tex elif values[0]=="Kd": tex = data.BlankTexture(color=map(float, values[1:])) smtl[mtl] = tex elif values[0] == 'f': face = [] texcoords = [] norms = [] for v in values[1:]: w = v.split('/') face.append(int(w[0])) if len(w) >= 2 and len(w[1]) > 0: texcoords.append(int(w[1])) else: texcoords.append(0) if len(w) >= 3 and len(w[2]) > 0: norms.append(int(w[2])) else: norms.append(0) sfaces.append((face, norms, texcoords, material)) faces_ordered_by_material = {} for face in sfaces: v, n, t, m = face if m in faces_ordered_by_material: faces_ordered_by_material[m].append(face) else: faces_ordered_by_material[m] = [face] lists = [] for i in faces_ordered_by_material: sfaces = faces_ordered_by_material[i] material = smtl[i] gl_list = data.DisplayList() gl_list.begin() current_tex = None for face in sfaces: vertices, normals, texture_coords, _m = face glBegin(GL_POLYGON) for i in xrange(len(vertices)): if normals[i] > 0: glNormal3fv(snormals[normals[i] - 1]) if texture_coords[i] > 0: glTexCoord2fv(stexcoords[texture_coords[i] - 1]) glVertex3fv(svertices[vertices[i] - 1]) glEnd() gl_list.end() lists.append([gl_list, material]) verts = [] for i in sfaces: for x in i[0]: verts.append(svertices[x-1]) return BasicMesh(lists, pos, rotation, verts, 1, colorize)
def __init__(self, size, pos=(0,0,0), rotation=(0,0,0), colorize=(1,1,1,1), texture=None, mirror=True): """Create a cube size is the absolute size of the cube pos is the position of the cube rotation is the rotation of the cube colorize is the color of the cube (0-1 RGBA) texture can be None, a data.Texture object or a string representing the filename of a texture to load mirror indicates whether each face of the cube has the full texture on it (so each is identicle) or if True, each face will have the entire texture mapped to it if False, the Texture is considered a cube map, like this: blank, blank, top, blank, back, left, front, right, blank, blank, bottom, blank""" view.require_init() self.size = size self.pos = pos self.rotation = rotation if not texture: texture = BlankTexture() if type(texture) is type(""): texture = Texture(texture) self.texture = texture self.colorize = colorize self.mirror = mirror self.corners = ((-1, -1, 1),#topleftfront (1, -1, 1),#toprightfront (1, 1, 1),#bottomrightfront (-1, 1, 1),#bottomleftfront (-1, -1, -1),#topleftback (1, -1, -1),#toprightback (1, 1, -1),#bottomrightback (-1, 1, -1))#bottomleftback self.sides = ((7,4,0,3, 2, 2, 5),#left (2,1,5,6, 3, 4, 4),#right (7,3,2,6, 5, 0, 3),#top (0,4,5,1, 4, 5, 2),#bottom (3,0,1,2, 0, 1, 0),#front (6,5,4,7, 1, 3, 1))#back self.normals = ((0, 0, 1), #front (0, 0, -1), #back (0, -1, 0), #top (0, 1, 0), #bottom (1, 0, 0), #right (-1, 0, 0)) #left self.split_coords = ((2,2),#top (0,1),#back (1,1),#left (2,1),#front (3,1),#right (2,0))#bottom self.scale = 1 self.display_list = data.DisplayList() self.visible = True self.pickable = True self.outline = False self.outline_size = 4 self.outline_color=(1,0,0) self._compile()