Poly3d/main.py
OusmBlueNinja a9bfb2d27a Main
2024-11-25 17:08:00 -06:00

42 lines
1.4 KiB
Python

import trimesh
def generate_vertex_array_from_model(file_path):
# Load the model using trimesh
mesh = trimesh.load(file_path)
# Ensure the mesh has valid geometry
if not mesh.is_volume:
print("Warning: The loaded mesh is not a closed volume.")
# Prepare vertex array data
vertices = []
for face in mesh.faces:
for vertex_index in face:
# Get the vertex position
position = mesh.vertices[vertex_index]
# Get the vertex normal
normal = mesh.vertex_normals[vertex_index]
# Add position and normal to the vertex array
vertices.extend([*position, *normal])
return vertices
# Save the vertex array to a formatted string
def save_vertex_array_to_file(vertices, output_file):
with open(output_file, 'w') as f:
f.write("GLfloat vertices[] = {\n")
for i, value in enumerate(vertices):
f.write(f" {value:.4f}f,")
if (i + 1) % 6 == 0: # Add a newline after every vertex (6 values: 3 position + 3 normal)
f.write("\n")
f.write("};\n")
# Example usage
model_path = "./model.obj" # Replace with the path to your model
output_path = "output_vertex_array.txt"
vertices = generate_vertex_array_from_model(model_path)
save_vertex_array_to_file(vertices, output_path)
print(f"Vertex array saved to {output_path}")