From b63536933f5031b846b6c48a3fcee9cbbcdaba8d Mon Sep 17 00:00:00 2001 From: Zack Buhman Date: Fri, 31 Oct 2025 15:19:27 -0500 Subject: [PATCH] add generate_cpp2 --- generate_cpp2.py | 81 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 generate_cpp2.py diff --git a/generate_cpp2.py b/generate_cpp2.py new file mode 100644 index 0000000..648e813 --- /dev/null +++ b/generate_cpp2.py @@ -0,0 +1,81 @@ +from generate import renderer +from obj import parse_obj_file +import sys + +def generate_vec3(vs): + for v in vs: + yield f"{{{v.x.to_string()}f, {v.y.to_string()}f, {v.z.to_string()}f}}," + +def generate_vec2(vs): + for v in vs: + yield f"{{{v.x.to_string()}f, {v.y.to_string()}f}}," + +def generate_vec(group_name, type, name, func, vs): + yield f"const vec{type} {group_name}_{name}[] = {{" + yield from func(vs) + yield "};" + +def filter_n(l, n): + for a in l: + if len(a.ptn) == n: + yield a + +def generate_ptn(ptn): + for p in ptn: + yield f"{{{p.position}, {p.texture}, {p.normal}}}," + +def generate_faces(group_name, model_name, faces, type, name, n): + yield f"const {type} {group_name}_{model_name}_{name}[] = {{" + for face in filter_n(faces, n): + yield "{" + yield from generate_ptn(face.ptn) + yield "}," + yield "};" + +def generate_object(group_name, object): + yield f"const object {group_name}_{object.name} = {{" + yield f".triangle = &{group_name}_{object.name}_triangle[0]," + triangle_count = sum(1 for _ in filter_n(object.faces, 3)) + quadrilateral_count = sum(1 for _ in filter_n(object.faces, 4)) + assert quadrilateral_count == 0 + line_count = len(object.lines) + assert line_count == 0 + yield f".triangle_count = {triangle_count}," + yield "};" + +def generate_object_array(group_name, obj_file): + yield f"const object * {group_name}_object[] = {{" + for object in obj_file.objects: + yield f"&{group_name}_{object.name}," + yield "};" + +def generate_model(group_name, obj_file): + yield f"const model {group_name}_model = {{" + yield f".position = {group_name}_position," + yield f".texture = {group_name}_texture," + yield f".normal = {group_name}_normal," + yield f".object = {group_name}_object," + yield f".object_count = {len(obj_file.objects)}" + yield "};" + +def generate_obj_file(group_name, obj_file): + yield "#pragma once" + yield '' + + yield from generate_vec(group_name, "3", "position", generate_vec3, obj_file.position) + yield from generate_vec(group_name, "2", "texture", generate_vec2, obj_file.texture) + yield from generate_vec(group_name, "3", "normal", generate_vec3, obj_file.normal) + + for object in obj_file.objects: + assert all(len(face.ptn) in {3, 4} for face in object.faces), object.name + yield from generate_faces(group_name, object.name, object.faces, "triangle_t", "triangle", 3) + yield from generate_object(group_name, object) + + yield from generate_object_array(group_name, obj_file) + yield from generate_model(group_name, obj_file) + +if __name__ == "__main__": + obj_file = parse_obj_file(sys.argv[1]) + render, out = renderer(indent_length=4) + render(generate_obj_file(sys.argv[2], obj_file)) + sys.stdout.write(out.getvalue())