generate.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #!/usr/bin/env python3
  2. from stl import mesh
  3. files = ["steamvrthread.stl"]
  4. fileData = {}
  5. def getVertexId(vertices, vertex):
  6. i = 0
  7. for v in vertices:
  8. if v[0] == vertex[0] and v[1] == vertex[1] and v[2] == vertex[2]:
  9. return i
  10. i += 1
  11. return -1
  12. for file in files:
  13. current = mesh.Mesh.from_file(file)
  14. vertices = []
  15. triangles = []
  16. vId = 0
  17. for triangle in current.points:
  18. t = [0, 0, 0]
  19. point = 0
  20. for i in [0, 3, 6]:
  21. v = (triangle[i], triangle[i + 1], triangle[i + 2])
  22. vertexId = getVertexId(vertices, v)
  23. if vertexId >= 0:
  24. t[point] = vertexId
  25. else:
  26. t[point] = vId
  27. vertices.append(v)
  28. vId += 1
  29. point += 1
  30. triangles.append(t)
  31. id = file[0:-4]
  32. fileData[id] = (vertices, triangles)
  33. with open("resources.hpp", "w") as f:
  34. f.write("#pragma once\n");
  35. f.write("// AUTOGENERATED FILE - DO NOT EDIT\n");
  36. for key, data in fileData.items():
  37. vString = "{"
  38. tString = "{"
  39. vLength = 0
  40. tLength = 0
  41. first = True
  42. for vertex in data[0]:
  43. for v in vertex:
  44. if not first:
  45. vString += ", "
  46. first = False
  47. vString += str(v)
  48. vLength += 1
  49. first = True
  50. for triangle in data[1]:
  51. for t in triangle:
  52. if not first:
  53. tString += ", "
  54. first = False
  55. tString += str(t)
  56. tLength += 1
  57. vString += "}"
  58. tString += "}"
  59. f.write("const float " + key + "_VERTICES[" + str(vLength) + "] " + vString + ";\n");
  60. f.write("const unsigned int " + key + "_TRIANGLES[" + str(tLength) + "] " + tString + ";\n");