#!/usr/bin/env python3 """Render an isometric floor view of a Quake 1 BSP map to PNG. Reads the world model's faces straight from the BSP (version 29), keeps the upward-facing surfaces (floors, stairs, ledges), projects them isometrically and colours them by height in a Quake-style palette. Liquids are drawn blue, sky and tool brushes are skipped. python3 tools/render_map.py quake/maps/bsp/aerowalk.bsp out.png "Aerowalk" """ import math import struct import sys from PIL import Image, ImageDraw, ImageFilter, ImageFont W, H, SS = 960, 600, 2 # output size and supersampling factor BG_TOP, BG_BOTTOM = (28, 20, 16), (10, 8, 7) LOW, MID, HIGH = (110, 40, 20), (205, 105, 35), (255, 214, 120) LIQUID = (40, 110, 150) def lumps(data): version = struct.unpack_from("= 0 else b]) floors.append((poly, liquid)) return floors def lerp(c1, c2, t): return tuple(int(a + (b - a) * t) for a, b in zip(c1, c2)) def height_colour(t): return lerp(LOW, MID, t * 2) if t < 0.5 else lerp(MID, HIGH, (t - 0.5) * 2) def render(path, out, title=""): floors = load_floors(path) yaw, pitch = math.radians(45), math.radians(35) cy, sy, cp, sp = math.cos(yaw), math.sin(yaw), math.cos(pitch), math.sin(pitch) def project(v): x, y, z = v rx, ry = x * cy - y * sy, x * sy + y * cy return rx, -(ry * sp) - z * cp, ry * cp - z * sp # screen x, screen y, depth zs = [v[2] for poly, _ in floors for v in poly] zmin, zmax = min(zs), max(zs) items = [] for poly, liquid in floors: pts = [project(v) for v in poly] depth = sum(p[2] for p in pts) / len(pts) z = sum(v[2] for v in poly) / len(poly) items.append((depth, [(p[0], p[1]) for p in pts], z, liquid)) xs = [x for _, pts, _, _ in items for x, _ in pts] ys = [y for _, pts, _, _ in items for _, y in pts] margin = 0.08 scale = min(W * (1 - 2 * margin) / (max(xs) - min(xs)), H * (1 - 2 * margin - 0.08) / (max(ys) - min(ys))) * SS ox = (W * SS - (max(xs) - min(xs)) * scale) / 2 - min(xs) * scale oy = (H * SS - (max(ys) - min(ys)) * scale) / 2 - min(ys) * scale + H * SS * 0.03 img = Image.new("RGB", (W * SS, H * SS)) bg = ImageDraw.Draw(img) for row in range(H * SS): bg.line([(0, row), (W * SS, row)], fill=lerp(BG_TOP, BG_BOTTOM, row / (H * SS))) # soft shadow under the whole layout shadow = Image.new("L", img.size, 0) sd = ImageDraw.Draw(shadow) for _, pts, _, _ in items: sd.polygon([(x * scale + ox + 14 * SS, y * scale + oy + 22 * SS) for x, y in pts], fill=150) shadow = shadow.filter(ImageFilter.GaussianBlur(12 * SS)) img.paste((0, 0, 0), mask=shadow) draw = ImageDraw.Draw(img) for depth, pts, z, liquid in sorted(items, key=lambda i: -i[0]): t = (z - zmin) / ((zmax - zmin) or 1) fill = LIQUID if liquid else height_colour(t) draw.polygon([(x * scale + ox, y * scale + oy) for x, y in pts], fill=fill, outline=lerp(fill, (0, 0, 0), 0.45)) img = img.resize((W, H), Image.LANCZOS) if title: d = ImageDraw.Draw(img) try: font = ImageFont.truetype("DejaVuSans-Bold.ttf", 30) small = ImageFont.truetype("DejaVuSans.ttf", 16) except OSError: font = small = ImageFont.load_default() d.text((28, 22), title, font=font, fill=(255, 200, 110)) d.text((30, 60), path.rsplit("/", 1)[-1], font=small, fill=(170, 140, 110)) img.save(out, optimize=True) if __name__ == "__main__": render(sys.argv[1], sys.argv[2], sys.argv[3] if len(sys.argv) > 3 else "")