def create_object(self, obj): print "MediaInfo:", atlas.print_parents(obj) #print obj.__class__ atlas.uri_type["texture_source"] = 1 unresolved = [] for attr in ["texture_source", "media_groups"]: unresolved = unresolved + self.resolver.resolve_attribute(obj, attr) print "MediaInfo?:", unresolved resolved_objects = self.resolver.new_object(obj) print "MediaInfo!:", map(lambda o:o.id, resolved_objects) self.add_objects(resolved_objects) #print self.resolver.pending #print "info:", self.resolver.depencies #print "objects:", self.objects.keys() #print "all_objects:", self.all_objects.keys() return unresolved
def create_object(self, obj): print "MediaInfo:", atlas.print_parents(obj) #print obj.__class__ atlas.uri_type["texture_source"] = 1 unresolved = [] for attr in ["texture_source", "media_groups"]: unresolved = unresolved + self.resolver.resolve_attribute( obj, attr) print "MediaInfo?:", unresolved resolved_objects = self.resolver.new_object(obj) print "MediaInfo!:", map(lambda o: o.id, resolved_objects) self.add_objects(resolved_objects) #print self.resolver.pending #print "info:", self.resolver.depencies #print "objects:", self.objects.keys() #print "all_objects:", self.all_objects.keys() return unresolved
def create_object(self, obj): #atlas.check_bug("add_object: %s:" % obj.id) print("GeoMap:", end=' ') atlas.print_parents(obj) #print obj.__class__ #if hasattr(obj, "contains"): # print "contains:", map(lambda o:"%s,%s" % (o.id,o.__class__), obj.contains) atlas.uri_type["loc"] = 1 #atlas.uri_list_type["polyline"] = 1 unresolved = self.resolve_attributes(obj) print("GeoMap?:", unresolved) #atlas.check_bug("add_object before resolve:") resolved_objects = self.resolver.new_object(obj) print("GeoMap!:", [o.id for o in resolved_objects]) #atlas.check_bug("add_object before add_objects:") self.add_objects(resolved_objects, resolve_pointers=0) #atlas.check_bug("add_object done:") #print self.resolver.pending #print self.resolver.depencies #print "objects:", self.objects.keys() #print "all_objects:", self.all_objects.keys() return unresolved
def create_object(self, obj): #atlas.check_bug("add_object: %s:" % obj.id) print "GeoMap:", atlas.print_parents(obj) #print obj.__class__ #if hasattr(obj, "contains"): # print "contains:", map(lambda o:"%s,%s" % (o.id,o.__class__), obj.contains) atlas.uri_type["loc"] = 1 #atlas.uri_list_type["polyline"] = 1 unresolved = self.resolve_attributes(obj) print "GeoMap?:", unresolved #atlas.check_bug("add_object before resolve:") resolved_objects = self.resolver.new_object(obj) print "GeoMap!:", map(lambda o:o.id, resolved_objects) #atlas.check_bug("add_object before add_objects:") self.add_objects(resolved_objects, resolve_pointers=0) #atlas.check_bug("add_object done:") #print self.resolver.pending #print self.resolver.depencies #print "objects:", self.objects.keys() #print "all_objects:", self.all_objects.keys() return unresolved
def draw_object(self, obj, depth, count=0): redraw_flag = 0 if not obj: return if obj.id not in self.map.objects: return if id(obj)!=id(self.map.objects[obj.id]): print(obj.id, "not same id()!!") if not hasattr(obj, "detailed_contents"): print("????detailed_contents?", end=' ') atlas.print_parents(obj) #print "self.map.objects:", #atlas.print_parents(self.map.objects[obj.parents[0]]) #import pdb; pdb.set_trace() if not obj.detailed_contents: if obj._polyline: ## mi = self.media_info.lookup(obj, "2D") ## if mi: ## apply(glColor3f, mi.rgb_color) ## glLineWidth(mi.line_width) ## else: ## glColor3f(0.0, 0.0, 0.0) ## glLineWidth(1.0) ## for i in range(len(obj._polyline)-1): ## p1, p2 = obj._polyline[i:i+2] ## glPushMatrix() ## glTranslated(p1.x, p1.y, 0.0) ## glBegin(GL_LINES) ## glVertex3f(0.0, 0.0, depth) ## glVertex3f(p2.x - p1.x, p2.y - p1.y, depth) ## glEnd() ## glPopMatrix() ## glLineWidth(1.0) glPushMatrix() p = atlas.Object() p.x = self.middle_x p.y = self.middle_y #p = obj._polyline[0] glTranslated(p.x, p.y, 0.0) mi = self.media_info.lookup(obj, "2D") if mi: glColor3f(*mi.rgb_color) glLineWidth(mi.line_width) else: glColor3f(0.0, 0.0, 0.0) glLineWidth(1.0) glBegin(GL_LINE_STRIP) for v in obj._polyline: glVertex3f(v.x - p.x, v.y - p.y, depth) glEnd() glLineWidth(1.0) glPopMatrix() if obj._area: use_texture = 0 special_flag = 0 mi = self.media_info.lookup(obj, "2D") if mi: if texturing_enabled and hasattr(mi, "texture_coordinates"): if type(mi.texture_source)==StringType: art_info = self.media_info[mi.texture_source] else: art_info = mi.texture_source if art_info.data_uri[:5]=="file:": file_name = art_info.data_uri[5:] else: raise_(ValueError, "url type not supported (yet): "+art_info.data_uri) use_texture = 1 if file_name[-4:]==".pnm": fp = open(file_name) fp.readline(); fp.readline() width,height = list(map(int, string.split(fp.readline()))) special_flag = max(width, height)>256 if not special_flag: fp.close() if special_flag: print("special!") special_flag = 1 fp.readline() self.special_stuff.append(atlas.Object(texture = fp.read(), width = width, height = height, coordinates = mi.texture_coordinates)) fp.close() else: img = Image.open(file_name) img = img.convert("RGBA") print(img.size) texture = img.tostring("raw", "RGBA", 0, -1) if texture!=self.last_texture: if use_mipmap: OpenGL.Tk.gluBuild2DMipmaps(GL_TEXTURE_2D, 3, img.size[0], img.size[1], GL_RGBA, GL_UNSIGNED_BYTE, texture) else: glTexImage2D(GL_TEXTURE_2D, 0, 3, img.size[0], img.size[1], 0, GL_RGBA, GL_UNSIGNED_BYTE, texture) self.last_texture = texture redraw_flag = 1 ## if count: ## rasterMap = freetype.drawString("a", font=font_file, size=444, type='pix') ## bitmap = rasterMap.tostring() ## bitmap2 = string.join(map(lambda c:chr(255)*3+c, bitmap), "") ## #bitmap2 = string.join(map(lambda c:"\0\0\0" + c, bitmap), "") ## #bitmap2 = string.join(map(lambda c:c+c+c+"\0", bitmap), "") ## #bitmap2 = string.join(map(lambda c:c*4, bitmap), "") ## print rasterMap.width, rasterMap.rows, len(bitmap), len(bitmap2) ## #img = freetype.toImage(rasterMap, type='pix') ## #img.save("a.png") ## img = Image.open("a_rgb.png") ## #58 68 ## #glTexImage2D(GL_TEXTURE_2D, 0, 1, rasterMap.width, rasterMap.rows, 0, ## #glTexImage2D(GL_TEXTURE_2D, 0, 2, 256, 256, 0, ## # GL_COLOR_INDEX, GL_BITMAP, bitmap) ## glTexImage2D(GL_TEXTURE_2D, 0, GL_INTENSITY, 256, 256, 0, ## GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, bitmap*3) ## #glTexImage2D(GL_TEXTURE_2D, 0, 3, 256, 256, 0, ## # GL_RGBA, GL_UNSIGNED_BYTE, bitmap2) tx1, ty1, tz1 = mi.texture_coordinates[0] tx2, ty2, tz2 = mi.texture_coordinates[1] #glColor3f(1.0, 1.0, 0.0) #glColor3f(0.0, 0.0, 0.0) #glColor3f(1.0, 1.0, 1.0) else: try: glColor3f(*mi.rgb_color) except: import pdb; pdb.set_trace() else: glColor3f(0.0, 0.0, 0.0) if not special_flag: glPolygonMode(GL_FRONT_AND_BACK, GL_FILL) #glTexImage2D(GL_TEXTURE_2D, 0, 4, 512, 512, 0, # GL_RGBA, GL_UNSIGNED_BYTE, img.tostring) #glTexImage2D(GL_TEXTURE_2D, 0, 3, 512, 512, 0, # GL_RGB, GL_UNSIGNED_BYTE, img.tostring) #use_texture = obj.id=="right_meadow" #use_texture = obj.id in ["left_meadow", "right_meadow"] if use_texture: #print "?" glEnable(GL_TEXTURE_2D) ## if count and fonts_enabled: ## glEnable(GL_ALPHA_TEST) ## glEnable(GL_BLEND) ## glAlphaFunc(GL_GEQUAL, 0.0625) ## glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) ## #glEnable(GL_POLYGON_OFFSET) ## #glPolygonOffset(0.0, -3) ## glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE) ## #glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL) ## #glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_BLEND) ## #glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE) ## glColor3f(1.0, 0.0, 0.0) glBegin(GL_TRIANGLES) for t in obj._area: for v in t: if use_texture: #print "?"*10 #print v.x, v.y #print tx1, tx2-tx1, ty1, ty2-ty1 #print (v.x-tx1)/(tx2-tx1), (v.y-ty1)/(ty2-ty1) glTexCoord2d(old_div((v.x-tx1),(tx2-tx1)), old_div((v.y-ty1),(ty2-ty1))) glVertex3f(v.x, v.y, depth) glEnd() if use_texture: glDisable(GL_TEXTURE_2D) ## if fonts_enabled: ## glDisable(GL_ALPHA_TEST) ## glDisable(GL_BLEND) ## glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE) glColor3f(0.0, 0.0, 0.0) glPolygonMode(GL_FRONT_AND_BACK, GL_LINE) if draw_triangle_outline: glBegin(GL_TRIANGLES) for t in obj._area: for v in t: glVertex3f(v.x, v.y, depth) glEnd() if obj.get("text") and fonts_enabled: #print obj.text mi = self.media_info.lookup(obj, "2D") if mi: glColor3f(*mi.rgb_color) #print "with color:", mi.rgb_color else: glColor3f(0.0, 0.0, 0.0) texture_size = hardware_texture_size if not self.font_texture_info: texture = ["\0" * texture_size] * texture_size curX = 0 curY = 0 curHeight = 0 extra_pixels = 1 bitmap_seen = {} for char in map(chr, list(range(256))): #for char in 'V\xc4 )(+-,/.1032547698?ACBEDFIHKJMLONPSRUT\xd6YX': #render font #print char, ord(char) #rasterMap = drawCharacter(char, size=17) rasterMap = drawCharacter(char, size=19) #rasterMap = drawCharacter(char, size=41) bitmap = rasterMap.tostring() #img = freetype.toImage(rasterMap, 'pix') #img.save("test.png") #advance line if needed (and check limits) while 1: if rasterMap.cols + curX <= texture_size and \ rasterMap.rows + curY <= texture_size: break if rasterMap.cols + curX > texture_size: if curX: curX = 0 curY += curHeight + extra_pixels else: raise ValueError("too big size for fonts") if rasterMap.rows + curY > texture_size: raise ValueError("too big size for fonts") #bitmap seen? key = rasterMap.cols, rasterMap.rows, bitmap if key in bitmap_seen: self.font_texture_info[char] = bitmap_seen[key] continue bitmap_seen[key] = char #store info about glyph tbbox = atlas.Object(xmin = float(curX) / texture_size, ymin = float(curY) / texture_size, xmax = float(curX + rasterMap.cols) / texture_size, ymax = float(curY + rasterMap.rows) / texture_size) fbbox = atlas.Object(xmin = rasterMap.metric.bbox.xMin, ymin = rasterMap.metric.bbox.yMin, xmax = rasterMap.metric.bbox.xMax, ymax = rasterMap.metric.bbox.yMax) self.font_texture_info[char] = atlas.Object( texture_bbox = tbbox, font_bbox = fbbox, advance = rasterMap.metric.advance) #copy font into texture y = curY + rasterMap.rows for pos in range(0, len(bitmap), max(1, rasterMap.cols)): y -= 1 texture[y] = texture[y][:curX] + \ bitmap[pos:pos+rasterMap.cols] + \ texture[y][curX+rasterMap.cols:] ## y = curY ## for pos in range(0, len(bitmap), max(1, rasterMap.cols)): ## y += 1 ## texture[y] = texture[y][:curX] + \ ## bitmap[pos:pos+rasterMap.cols] + \ ## texture[y][curX+rasterMap.cols:] curX += rasterMap.cols + extra_pixels curHeight = max(curHeight, rasterMap.rows) #print rasterMap.width, rasterMap.cols, rasterMap.rows, len(bitmap) #print curX,curY, curHeight #open("foo2.out", "w").write(repr(bitmap)) #open("foo.out", "w").write(repr(texture)) self.font_texture = string.join(texture, "") #self.font_texture = string.join(map(lambda c:c*2, self.font_texture), "") im = Image.fromstring("L", (texture_size, texture_size), self.font_texture, "raw", "L;I") im.save("font_texture.png") if self.font_texture!=self.last_texture: #glTexImage2D(GL_TEXTURE_2D, 0, GL_INTENSITY, 256, 256, 0, # GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, texture*4) if use_mipmap: #OpenGL.Tk.gluBuild2DMipmaps(GL_TEXTURE_2D, GL_INTENSITY4, texture_size, texture_size, # GL_LUMINANCE, GL_UNSIGNED_BYTE, self.font_texture) OpenGL.Tk.gluBuild2DMipmaps(GL_TEXTURE_2D, GL_INTENSITY, texture_size, texture_size, GL_LUMINANCE, GL_UNSIGNED_BYTE, self.font_texture) #OpenGL.Tk.gluBuild2DMipmaps(GL_TEXTURE_2D, GL_INTENSITY, texture_size, texture_size, # GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, self.font_texture) else: glTexImage2D(GL_TEXTURE_2D, 0, GL_INTENSITY, texture_size, texture_size, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, self.font_texture) self.last_texture = self.font_texture glEnable(GL_TEXTURE_2D) glEnable(GL_ALPHA_TEST) glEnable(GL_BLEND) glAlphaFunc(GL_GEQUAL, 0.0625) glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) #glBlendFunc(GL_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA) #glBlendFunc(GL_SRC_ALPHA_SATURATE, GL_ONE) #glEnable(GL_POLYGON_OFFSET) #glPolygonOffset(0.0, -3) glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE) glPolygonMode(GL_FRONT_AND_BACK, GL_FILL) glPushMatrix() glTranslated(self.middle_x, self.middle_y, 0.0) p = obj._pos glTranslated(p.x - self.middle_x, p.y - self.middle_y, 0.0) glMultMatrixd(rot2glmatrix(obj.rotation)) x, y = 0.0, 0.0 glBegin(GL_QUADS) if 0: #test... x1 = x y1 = y x2 = x + 10.0 y2 = y + 10.0 glTexCoord2d(0.0, 0.0) glVertex3f(x1, y1, depth) glTexCoord2d(1.0, 0.0) glVertex3f(x2, y1, depth) glTexCoord2d(1.0, 1.0) glVertex3f(x2, y2, depth) glTexCoord2d(0.0, 1.0) glVertex3f(x1, y2, depth) else: fp = open("debug.log", "a+") fp.write(obj.text+"\n") for char in obj.text: ti = self.font_texture_info[char] size = 1./600 x1 = x + ti.font_bbox.xmin * size y1 = y + ti.font_bbox.ymin * size x2 = x + ti.font_bbox.xmax * size y2 = y + ti.font_bbox.ymax * size #print char, ord(char), ti #print char, ord(char), x1,y1,x2,y2, ti.texture_bbox, x fp.write(string.join(map(str, [char, ord(char), x, y, x1, y1, x2, y2, ti, ti.advance * size])) + "\n") glTexCoord2d(ti.texture_bbox.xmin, ti.texture_bbox.ymin) glVertex3f(x1, y1, depth) glTexCoord2d(ti.texture_bbox.xmax, ti.texture_bbox.ymin) glVertex3f(x2, y1, depth) glTexCoord2d(ti.texture_bbox.xmax, ti.texture_bbox.ymax) glVertex3f(x2, y2, depth) glTexCoord2d(ti.texture_bbox.xmin, ti.texture_bbox.ymax) glVertex3f(x1, y2, depth) x += ti.advance * size fp.close() ## tex_size = 1.0 ## glTexCoord2d(0, 0) ## glVertex3f(p.x, p.y, depth) ## glTexCoord2d(tex_size, 0) ## glVertex3f(p.x+1, p.y, depth) ## glTexCoord2d(tex_size, tex_size) ## glVertex3f(p.x+1, p.y+1, depth) ## glTexCoord2d(0, tex_size) ## glVertex3f(p.x, p.y+1, depth) glEnd() glPopMatrix() glPolygonMode(GL_FRONT_AND_BACK, GL_LINE) glDisable(GL_TEXTURE_2D) glDisable(GL_ALPHA_TEST) glDisable(GL_BLEND) glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE) for sub_obj in obj.contains: #print "unhashable?", type(sub_obj.id), "for:", obj.id #if self.map.objects.has_key(sub_obj.id) and id(sub_obj)!=id(self.map.objects[sub_obj.id]): # print obj.id, sub_obj.id, "obj/sub: not same id()!!" # print obj.__class__, sub_obj.__class__ self.draw_object(sub_obj, depth) ## if not count and redraw_flag: ## self.draw_object(obj, depth, count+1) obj._changed = 0
def draw_object(self, obj, depth, count=0): redraw_flag = 0 if not obj: return if not self.map.objects.has_key(obj.id): return if id(obj)!=id(self.map.objects[obj.id]): print obj.id, "not same id()!!" if not hasattr(obj, "detailed_contents"): print "????detailed_contents?", atlas.print_parents(obj) #print "self.map.objects:", #atlas.print_parents(self.map.objects[obj.parents[0]]) #import pdb; pdb.set_trace() if not obj.detailed_contents: if obj._polyline: ## mi = self.media_info.lookup(obj, "2D") ## if mi: ## apply(glColor3f, mi.rgb_color) ## glLineWidth(mi.line_width) ## else: ## glColor3f(0.0, 0.0, 0.0) ## glLineWidth(1.0) ## for i in range(len(obj._polyline)-1): ## p1, p2 = obj._polyline[i:i+2] ## glPushMatrix() ## glTranslated(p1.x, p1.y, 0.0) ## glBegin(GL_LINES) ## glVertex3f(0.0, 0.0, depth) ## glVertex3f(p2.x - p1.x, p2.y - p1.y, depth) ## glEnd() ## glPopMatrix() ## glLineWidth(1.0) glPushMatrix() p = atlas.Object() p.x = self.middle_x p.y = self.middle_y #p = obj._polyline[0] glTranslated(p.x, p.y, 0.0) mi = self.media_info.lookup(obj, "2D") if mi: apply(glColor3f, mi.rgb_color) glLineWidth(mi.line_width) else: glColor3f(0.0, 0.0, 0.0) glLineWidth(1.0) glBegin(GL_LINE_STRIP) for v in obj._polyline: glVertex3f(v.x - p.x, v.y - p.y, depth) glEnd() glLineWidth(1.0) glPopMatrix() if obj._area: use_texture = 0 special_flag = 0 mi = self.media_info.lookup(obj, "2D") if mi: if texturing_enabled and hasattr(mi, "texture_coordinates"): if type(mi.texture_source)==StringType: art_info = self.media_info[mi.texture_source] else: art_info = mi.texture_source if art_info.data_uri[:5]=="file:": file_name = art_info.data_uri[5:] else: raise ValueError, "url type not supported (yet): "+art_info.data_uri use_texture = 1 if file_name[-4:]==".pnm": fp = open(file_name) fp.readline(); fp.readline() width,height = map(int, string.split(fp.readline())) special_flag = max(width, height)>256 if not special_flag: fp.close() if special_flag: print "special!" special_flag = 1 fp.readline() self.special_stuff.append(atlas.Object(texture = fp.read(), width = width, height = height, coordinates = mi.texture_coordinates)) fp.close() else: img = Image.open(file_name) img = img.convert("RGBA") print img.size texture = img.tostring("raw", "RGBA", 0, -1) if texture!=self.last_texture: if use_mipmap: OpenGL.Tk.gluBuild2DMipmaps(GL_TEXTURE_2D, 3, img.size[0], img.size[1], GL_RGBA, GL_UNSIGNED_BYTE, texture) else: glTexImage2D(GL_TEXTURE_2D, 0, 3, img.size[0], img.size[1], 0, GL_RGBA, GL_UNSIGNED_BYTE, texture) self.last_texture = texture redraw_flag = 1 ## if count: ## rasterMap = freetype.drawString("a", font=font_file, size=444, type='pix') ## bitmap = rasterMap.tostring() ## bitmap2 = string.join(map(lambda c:chr(255)*3+c, bitmap), "") ## #bitmap2 = string.join(map(lambda c:"\0\0\0" + c, bitmap), "") ## #bitmap2 = string.join(map(lambda c:c+c+c+"\0", bitmap), "") ## #bitmap2 = string.join(map(lambda c:c*4, bitmap), "") ## print rasterMap.width, rasterMap.rows, len(bitmap), len(bitmap2) ## #img = freetype.toImage(rasterMap, type='pix') ## #img.save("a.png") ## img = Image.open("a_rgb.png") ## #58 68 ## #glTexImage2D(GL_TEXTURE_2D, 0, 1, rasterMap.width, rasterMap.rows, 0, ## #glTexImage2D(GL_TEXTURE_2D, 0, 2, 256, 256, 0, ## # GL_COLOR_INDEX, GL_BITMAP, bitmap) ## glTexImage2D(GL_TEXTURE_2D, 0, GL_INTENSITY, 256, 256, 0, ## GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, bitmap*3) ## #glTexImage2D(GL_TEXTURE_2D, 0, 3, 256, 256, 0, ## # GL_RGBA, GL_UNSIGNED_BYTE, bitmap2) tx1, ty1, tz1 = mi.texture_coordinates[0] tx2, ty2, tz2 = mi.texture_coordinates[1] #glColor3f(1.0, 1.0, 0.0) #glColor3f(0.0, 0.0, 0.0) #glColor3f(1.0, 1.0, 1.0) else: try: apply(glColor3f, mi.rgb_color) except: import pdb; pdb.set_trace() else: glColor3f(0.0, 0.0, 0.0) if not special_flag: glPolygonMode(GL_FRONT_AND_BACK, GL_FILL) #glTexImage2D(GL_TEXTURE_2D, 0, 4, 512, 512, 0, # GL_RGBA, GL_UNSIGNED_BYTE, img.tostring) #glTexImage2D(GL_TEXTURE_2D, 0, 3, 512, 512, 0, # GL_RGB, GL_UNSIGNED_BYTE, img.tostring) #use_texture = obj.id=="right_meadow" #use_texture = obj.id in ["left_meadow", "right_meadow"] if use_texture: #print "?" glEnable(GL_TEXTURE_2D) ## if count and fonts_enabled: ## glEnable(GL_ALPHA_TEST) ## glEnable(GL_BLEND) ## glAlphaFunc(GL_GEQUAL, 0.0625) ## glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) ## #glEnable(GL_POLYGON_OFFSET) ## #glPolygonOffset(0.0, -3) ## glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE) ## #glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL) ## #glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_BLEND) ## #glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE) ## glColor3f(1.0, 0.0, 0.0) glBegin(GL_TRIANGLES) for t in obj._area: for v in t: if use_texture: #print "?"*10 #print v.x, v.y #print tx1, tx2-tx1, ty1, ty2-ty1 #print (v.x-tx1)/(tx2-tx1), (v.y-ty1)/(ty2-ty1) glTexCoord2d((v.x-tx1)/(tx2-tx1), (v.y-ty1)/(ty2-ty1)) glVertex3f(v.x, v.y, depth) glEnd() if use_texture: glDisable(GL_TEXTURE_2D) ## if fonts_enabled: ## glDisable(GL_ALPHA_TEST) ## glDisable(GL_BLEND) ## glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE) glColor3f(0.0, 0.0, 0.0) glPolygonMode(GL_FRONT_AND_BACK, GL_LINE) if draw_triangle_outline: glBegin(GL_TRIANGLES) for t in obj._area: for v in t: glVertex3f(v.x, v.y, depth) glEnd() if obj.get("text") and fonts_enabled: #print obj.text mi = self.media_info.lookup(obj, "2D") if mi: apply(glColor3f, mi.rgb_color) #print "with color:", mi.rgb_color else: glColor3f(0.0, 0.0, 0.0) texture_size = hardware_texture_size if not self.font_texture_info: texture = ["\0" * texture_size] * texture_size curX = 0 curY = 0 curHeight = 0 extra_pixels = 1 bitmap_seen = {} for char in map(chr, range(256)): #for char in 'V\xc4 )(+-,/.1032547698?ACBEDFIHKJMLONPSRUT\xd6YX': #render font #print char, ord(char) #rasterMap = drawCharacter(char, size=17) rasterMap = drawCharacter(char, size=19) #rasterMap = drawCharacter(char, size=41) bitmap = rasterMap.tostring() #img = freetype.toImage(rasterMap, 'pix') #img.save("test.png") #advance line if needed (and check limits) while 1: if rasterMap.cols + curX <= texture_size and \ rasterMap.rows + curY <= texture_size: break if rasterMap.cols + curX > texture_size: if curX: curX = 0 curY += curHeight + extra_pixels else: raise ValueError, "too big size for fonts" if rasterMap.rows + curY > texture_size: raise ValueError, "too big size for fonts" #bitmap seen? key = rasterMap.cols, rasterMap.rows, bitmap if key in bitmap_seen: self.font_texture_info[char] = bitmap_seen[key] continue bitmap_seen[key] = char #store info about glyph tbbox = atlas.Object(xmin = float(curX) / texture_size, ymin = float(curY) / texture_size, xmax = float(curX + rasterMap.cols) / texture_size, ymax = float(curY + rasterMap.rows) / texture_size) fbbox = atlas.Object(xmin = rasterMap.metric.bbox.xMin, ymin = rasterMap.metric.bbox.yMin, xmax = rasterMap.metric.bbox.xMax, ymax = rasterMap.metric.bbox.yMax) self.font_texture_info[char] = atlas.Object( texture_bbox = tbbox, font_bbox = fbbox, advance = rasterMap.metric.advance) #copy font into texture y = curY + rasterMap.rows for pos in range(0, len(bitmap), max(1, rasterMap.cols)): y -= 1 texture[y] = texture[y][:curX] + \ bitmap[pos:pos+rasterMap.cols] + \ texture[y][curX+rasterMap.cols:] ## y = curY ## for pos in range(0, len(bitmap), max(1, rasterMap.cols)): ## y += 1 ## texture[y] = texture[y][:curX] + \ ## bitmap[pos:pos+rasterMap.cols] + \ ## texture[y][curX+rasterMap.cols:] curX += rasterMap.cols + extra_pixels curHeight = max(curHeight, rasterMap.rows) #print rasterMap.width, rasterMap.cols, rasterMap.rows, len(bitmap) #print curX,curY, curHeight #open("foo2.out", "w").write(repr(bitmap)) #open("foo.out", "w").write(repr(texture)) self.font_texture = string.join(texture, "") #self.font_texture = string.join(map(lambda c:c*2, self.font_texture), "") im = Image.fromstring("L", (texture_size, texture_size), self.font_texture, "raw", "L;I") im.save("font_texture.png") if self.font_texture!=self.last_texture: #glTexImage2D(GL_TEXTURE_2D, 0, GL_INTENSITY, 256, 256, 0, # GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, texture*4) if use_mipmap: #OpenGL.Tk.gluBuild2DMipmaps(GL_TEXTURE_2D, GL_INTENSITY4, texture_size, texture_size, # GL_LUMINANCE, GL_UNSIGNED_BYTE, self.font_texture) OpenGL.Tk.gluBuild2DMipmaps(GL_TEXTURE_2D, GL_INTENSITY, texture_size, texture_size, GL_LUMINANCE, GL_UNSIGNED_BYTE, self.font_texture) #OpenGL.Tk.gluBuild2DMipmaps(GL_TEXTURE_2D, GL_INTENSITY, texture_size, texture_size, # GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, self.font_texture) else: glTexImage2D(GL_TEXTURE_2D, 0, GL_INTENSITY, texture_size, texture_size, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, self.font_texture) self.last_texture = self.font_texture glEnable(GL_TEXTURE_2D) glEnable(GL_ALPHA_TEST) glEnable(GL_BLEND) glAlphaFunc(GL_GEQUAL, 0.0625) glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) #glBlendFunc(GL_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA) #glBlendFunc(GL_SRC_ALPHA_SATURATE, GL_ONE) #glEnable(GL_POLYGON_OFFSET) #glPolygonOffset(0.0, -3) glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE) glPolygonMode(GL_FRONT_AND_BACK, GL_FILL) glPushMatrix() glTranslated(self.middle_x, self.middle_y, 0.0) p = obj._pos glTranslated(p.x - self.middle_x, p.y - self.middle_y, 0.0) glMultMatrixd(rot2glmatrix(obj.rotation)) x, y = 0.0, 0.0 glBegin(GL_QUADS) if 0: #test... x1 = x y1 = y x2 = x + 10.0 y2 = y + 10.0 glTexCoord2d(0.0, 0.0) glVertex3f(x1, y1, depth) glTexCoord2d(1.0, 0.0) glVertex3f(x2, y1, depth) glTexCoord2d(1.0, 1.0) glVertex3f(x2, y2, depth) glTexCoord2d(0.0, 1.0) glVertex3f(x1, y2, depth) else: fp = open("debug.log", "a+") fp.write(obj.text+"\n") for char in obj.text: ti = self.font_texture_info[char] size = 1./600 x1 = x + ti.font_bbox.xmin * size y1 = y + ti.font_bbox.ymin * size x2 = x + ti.font_bbox.xmax * size y2 = y + ti.font_bbox.ymax * size #print char, ord(char), ti #print char, ord(char), x1,y1,x2,y2, ti.texture_bbox, x fp.write(string.join(map(str, [char, ord(char), x, y, x1, y1, x2, y2, ti, ti.advance * size])) + "\n") glTexCoord2d(ti.texture_bbox.xmin, ti.texture_bbox.ymin) glVertex3f(x1, y1, depth) glTexCoord2d(ti.texture_bbox.xmax, ti.texture_bbox.ymin) glVertex3f(x2, y1, depth) glTexCoord2d(ti.texture_bbox.xmax, ti.texture_bbox.ymax) glVertex3f(x2, y2, depth) glTexCoord2d(ti.texture_bbox.xmin, ti.texture_bbox.ymax) glVertex3f(x1, y2, depth) x += ti.advance * size fp.close() ## tex_size = 1.0 ## glTexCoord2d(0, 0) ## glVertex3f(p.x, p.y, depth) ## glTexCoord2d(tex_size, 0) ## glVertex3f(p.x+1, p.y, depth) ## glTexCoord2d(tex_size, tex_size) ## glVertex3f(p.x+1, p.y+1, depth) ## glTexCoord2d(0, tex_size) ## glVertex3f(p.x, p.y+1, depth) glEnd() glPopMatrix() glPolygonMode(GL_FRONT_AND_BACK, GL_LINE) glDisable(GL_TEXTURE_2D) glDisable(GL_ALPHA_TEST) glDisable(GL_BLEND) glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE) for sub_obj in obj.contains: #print "unhashable?", type(sub_obj.id), "for:", obj.id #if self.map.objects.has_key(sub_obj.id) and id(sub_obj)!=id(self.map.objects[sub_obj.id]): # print obj.id, sub_obj.id, "obj/sub: not same id()!!" # print obj.__class__, sub_obj.__class__ self.draw_object(sub_obj, depth) ## if not count and redraw_flag: ## self.draw_object(obj, depth, count+1) obj._changed = 0