Commit 3039df96 authored by Thomas Albrecht's avatar Thomas Albrecht

bugfixes. New texture manager seems to work now.

parent 2fb25551
......@@ -562,8 +562,6 @@ def write_xml(path, fname, buildings):
# -----------------------------------------------------------------------------
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
#logging.basicConfig(level=logging.DEBUG)
# -- Parse arguments. Command line overrides config file.
parser = argparse.ArgumentParser(description="osm2city reads OSM data and creates buildings for use with FlightGear")
......@@ -572,8 +570,14 @@ if __name__ == "__main__":
parser.add_argument("-e", dest="e", action="store_true", help="skip elevation interpolation")
parser.add_argument("-c", dest="c", action="store_true", help="do not check for overlapping with static objects")
parser.add_argument("-u", dest="uninstall", action="store_true", help="uninstall ours from .stg")
parser.add_argument("-d", dest="debug", default=False, action="store_true", help="set loglevel=DEBUG")
args = parser.parse_args()
if args.debug:
logging.basicConfig(level=logging.DEBUG)
else:
logging.basicConfig(level=logging.INFO)
if args.filename is not None:
parameters.read_from_file(args.filename)
......
......@@ -4,6 +4,7 @@
"""generate texture atlas"""
import PIL.Image as Image
import logging
class Region(object):
def __init__(self, x, y, width, height):
......@@ -11,7 +12,7 @@ class Region(object):
self.y = y
self.width_px = width
self.height_px = height
print " New Region " + str(self)
logging.debug(" New Region " + str(self))
def __str__(self):
return "(%i x %i + %i + %i)" % (self.width_px, self.height_px, self.x, self.y)
......@@ -25,6 +26,14 @@ class Atlas(Region):
self.min_width = 1
self.min_height = 1
def cur_height(self):
"""return the current height"""
return self.regions[-1].y
def set_height(self, height):
self.height_px = height
self._compute_nondim_tex_coords()
def write(self, filename, format):
atlas = Image.new("RGB", (self.width_px, self.height_px))
......@@ -34,17 +43,31 @@ class Atlas(Region):
atlas.save(filename, optimize=True)
def pack(self, the_texture):
print "packing %s (%i %i)" % (the_texture.filename, the_texture.width_px, the_texture.height_px)
logging.debug("packing %s (%i %i)" %
(the_texture.filename, the_texture.width_px, the_texture.height_px))
for the_region in self.regions:
if self._pack(the_texture, the_region):
self._textures.append(the_texture)
print " packed at %i %i" % (the_texture._x, the_texture._y)
print "now have %i regions" % len(self.regions)
logging.debug(" packed at %i %i" % (the_texture._x, the_texture._y))
logging.debug("now have %i regions" % len(self.regions))
for the_region in self.regions:
print " - ", the_region
logging.debug(" - " + str(the_region))
return True
return False
def _compute_nondim_tex_coords(self):
"""compute non-dim texture coords"""
for t in self._textures:
#atlas.paste(l.im, (0, next_y))
t.x0 = float(t._x) / self.width_px
t.x1 = float(t._x + t.width_px) / self.width_px
t.y1 = 1 - float(t._y) / self.height_px
t.y0 = 1 - float(t._y + t.height_px) / self.height_px
t.sx = float(t.width_px) / self.width_px
t.sy = float(t.height_px) / self.height_px
def check_regions(self):
for region1 in self.regions:
if region1.width_px < self.min_width or region1.height_px < self.min_height:
......@@ -63,13 +86,13 @@ class Atlas(Region):
assert(the_texture.width_px > 0)
if the_texture.height_px == the_region.height_px:
if the_texture.width_px == the_region.width_px:
print "H split exact fit"
logging.debug("H split exact fit")
the_texture._x = the_region.x
the_texture._y = the_region.y
self.regions.remove(the_region)
return True
elif the_texture.width_px < the_region.width_px:
print "H split"
logging.debug("H split")
# split horiz
the_texture._x = the_region.x
the_texture._y = the_region.y
......@@ -78,16 +101,16 @@ class Atlas(Region):
self.check_regions()
return True
else:
print "H too small (%i < %i), trying next" % (the_region.width_px, the_texture.width_px)
logging.debug("H too small (%i < %i), trying next" % (the_region.width_px, the_texture.width_px))
return False
elif the_texture.height_px > the_region.height_px:
print "V too small, trying next"
logging.debug("V too small, trying next")
return False
else:
# check if it fits width
if the_texture.width_px > the_region.width_px:
return False
print "V split"
logging.debug("V split")
# vertical split
the_texture._x = the_region.x
the_texture._y = the_region.y
......@@ -131,7 +154,7 @@ def mk_atlas(filenames_list):
raw_input("Press Enter to continue...")
pass
else:
print "no"
logging.debug("no")
atlas.write("atlast.png", "RGBA")
......
......@@ -64,6 +64,29 @@ def make_texture_atlas(texture_list, atlas_filename, size_x = 256, pad_y = 0, li
# FIXME: maybe auto-calc x-size here
# -- pack non_repeatables
# Sort textures by perimeter size in non-increasing order
the_atlas = atlas.Atlas(0, 0, atlas_sx, 1e10)
if 1:
non_repeat_list = sorted(non_repeat_list, key=lambda i:i.sy, reverse=True)
deb = 0
for the_texture in non_repeat_list:
the_texture.width_px, the_texture.height_px = the_texture.im.size
#if the_texture.filename == "tex.src/samples/US-dcwhiteconc10st2.jpg":
#the_atlas.write("atlas.png", "RGBA")
#raw_input("Press Enter to continue...")
#deb = 1
if the_atlas.pack(the_texture):
if deb:
the_atlas.write("atlas.png", "RGBA")
raw_input("Press Enter to continue...")
pass
else:
print "no"
atlas_sy = the_atlas.cur_height()
#can_repeat_list = []
# -- work on repeatable textures.
......@@ -87,20 +110,17 @@ def make_texture_atlas(texture_list, atlas_filename, size_x = 256, pad_y = 0, li
# assert(max(sx) <= altas_sx)
# -- create atlas image
#atlas_sy = next_pow2(atlas_sy)
#atlas = Image.new("RGB", (atlas_sx, atlas_sy))
the_atlas = atlas.Atlas(0, 0, atlas_sx, 2**14)
# -- paste, compute atlas coords
# lower left corner of texture is x0, y0
deb = 0
for l in can_repeat_list:
#atlas.paste(l.im, (0, next_y))
l.x0 = 0
l.x1 = float(l.width_px) / atlas_sx
l.y1 = 1 - float(next_y) / atlas_sy
l.y0 = 1 - float(next_y + l.height_px) / atlas_sy
l.sy_float = float(l.height_px) / atlas_sy
l.sy = float(l.height_px) / atlas_sy
l.sx = 1.
next_y += l.height_px + pad_y
......@@ -121,25 +141,9 @@ def make_texture_atlas(texture_list, atlas_filename, size_x = 256, pad_y = 0, li
# the_atlas.write("atlas.png", "RGBA")
# return
# -- pack non_repeatables
# Sort textures by perimeter size in non-increasing order
non_repeat_list = sorted(non_repeat_list, key=lambda i:i.sy, reverse=True)
for the_texture in non_repeat_list:
the_texture.width_px, the_texture.height_px = the_texture.im.size
#if the_texture.filename == "tex.src/samples/US-dcwhiteconc10st2.jpg":
#the_atlas.write("atlas.png", "RGBA")
#raw_input("Press Enter to continue...")
#deb = 1
if the_atlas.pack(the_texture):
print "packed"
if deb:
the_atlas.write("atlas.png", "RGBA")
raw_input("Press Enter to continue...")
pass
else:
print "no"
atlas_sy = next_pow2(atlas_sy)
the_atlas.set_height(atlas_sy)
logging.info("Final atlas height %i" % atlas_sy)
the_atlas.write(atlas_filename, "RGBA")
......@@ -270,7 +274,6 @@ def init(tex_prefix=''):
catalog.append_facades(tex_prefix, facades)
#append_facades_test()
catalog.append_roofs(tex_prefix, roofs)
#facades = textures_src.import_us(facades)
catalog.import_us(tex_prefix, facades)
#facades.keep_only(1)
......
......@@ -103,8 +103,10 @@ class Texture(object):
def y(self, y):
"""given non-dimensional texture coord, return position in atlas"""
if self.rotated:
# print "Yr self.x0 + y * self.sx %g + (%s) * %g:", (self.x0, str(y), self.sx)
return self.x0 + y * self.sx
else:
# print "YY self.y0 + y * self.sy : %g + (%s) * %g" % (self.y0, str(y), self.sy)
return self.y0 + y * self.sy
def __str__(self):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment