Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

77 linhas
2.8KB

  1. """Precompute a rotating cube for petcube: src/cubedata.h.
  2. 256 frames of one seamless rotation (X turns once, Y twice, Z once per loop),
  3. perspective-projected onto the 80x46 quarter-block grid (2x2 "pixels" per
  4. character, rows 0-22). Per frame: 8 vertices as (x, y) bytes. The cube is
  5. see-through, so every edge is drawn and no visibility data is needed.
  6. python projects/petcube/tools/gen_cube.py
  7. """
  8. import math
  9. import os
  10. FRAMES = 256
  11. W, H = 80, 46 # pixel grid used by the cube
  12. CX, CY = 39.5, 22.5 # centre of that grid
  13. DIST = 3.6 # camera distance (cube spans -1..1)
  14. V = [(x, y, z) for x in (-1, 1) for y in (-1, 1) for z in (-1, 1)]
  15. # faces as vertex loops (used only to list the 12 edges)
  16. FACES = [
  17. (0, 1, 3, 2), # x = -1
  18. (4, 6, 7, 5), # x = +1
  19. (0, 4, 5, 1), # y = -1
  20. (2, 3, 7, 6), # y = +1
  21. (0, 2, 6, 4), # z = -1
  22. (1, 5, 7, 3), # z = +1
  23. ]
  24. EDGES = sorted({tuple(sorted((f[i], f[(i + 1) % 4]))) for f in FACES for i in range(4)})
  25. assert len(EDGES) == 12
  26. def rotate(p, ax, ay, az):
  27. x, y, z = p
  28. y, z = y * math.cos(ax) - z * math.sin(ax), y * math.sin(ax) + z * math.cos(ax)
  29. x, z = x * math.cos(ay) + z * math.sin(ay), -x * math.sin(ay) + z * math.cos(ay)
  30. x, y = x * math.cos(az) - y * math.sin(az), x * math.sin(az) + y * math.cos(az)
  31. return x, y, z
  32. def project(frames_scale):
  33. out = []
  34. for t in range(FRAMES):
  35. a = 2 * math.pi * t / FRAMES
  36. pts = []
  37. for p in V:
  38. x, y, z = rotate(p, a, 2 * a, a)
  39. k = frames_scale / (z + DIST)
  40. pts.append((CX + x * k, CY + y * k))
  41. out.append(pts)
  42. return out
  43. # scale so the largest extent just fits the grid
  44. raw = project(1.0)
  45. ext = max(max(abs(x - CX) / (W / 2 - 0.6), abs(y - CY) / (H / 2 - 0.6)) for f in raw for x, y in f)
  46. frames = project(1.0 / ext)
  47. xy = []
  48. for pts in frames:
  49. for x, y in pts:
  50. xi, yi = int(round(x)), int(round(y))
  51. assert 0 <= xi < W and 0 <= yi < H, (xi, yi)
  52. xy += [xi, yi]
  53. src = os.path.join(os.path.dirname(__file__), '..', 'src', 'cubedata.h')
  54. with open(src, 'w', newline='\n') as fh:
  55. fh.write('/* Generated by tools/gen_cube.py: do not edit. */\n')
  56. fh.write('#define CUBE_FRAMES %d\n' % FRAMES)
  57. fh.write('static const unsigned char edge_a[12] = { %s };\n' % ', '.join(str(a) for a, b in EDGES))
  58. fh.write('static const unsigned char edge_b[12] = { %s };\n' % ', '.join(str(b) for a, b in EDGES))
  59. fh.write('/* per frame: x0, y0, ... x7, y7 on the 80x46 grid */\n')
  60. fh.write('static const unsigned char cube_xy[%d] = {\n' % len(xy))
  61. for i in range(0, len(xy), 16):
  62. fh.write(' ' + ','.join(str(v) for v in xy[i:i + 16]) + ',\n')
  63. fh.write('};\n')
  64. print('wrote', os.path.normpath(src))

Powered by TurnKey Linux.