Compare commits

..

No commits in common. "3b908772cb8fed77082fab71b9c5202b58a6d0c3" and "14f34191245e4ba79159475dd1e5afd16b04d6ea" have entirely different histories.

3 changed files with 37 additions and 68 deletions

View File

@ -1,9 +1,6 @@
import bpy import bpy
from os import path from os import path
from collections import defaultdict from collections import defaultdict
import bmesh
triangulate_meshes = True
def sprint(*text): def sprint(*text):
screen = bpy.data.screens['Scripting'] screen = bpy.data.screens['Scripting']
@ -23,19 +20,19 @@ def render_vec2(v):
return f"{{{v.x:.6f}, {v.y:.6f}}}" return f"{{{v.x:.6f}, {v.y:.6f}}}"
def render_mesh_vertices(f, name, vertices): def render_mesh_vertices(f, name, vertices):
f.write(f"static const vec3 {name}_position[] = {{\n") f.write(f"const vec3 {name}_position[] = {{\n")
for vertex in vertices: for vertex in vertices:
f.write(f" {render_vec3(vertex.co)},\n") f.write(f" {render_vec3(vertex.co)},\n")
f.write("};\n\n") f.write("};\n\n")
def render_vertex_normals(f, name, vertices): def render_vertex_normals(f, name, vertices):
f.write(f"static const vec3 {name}_normal[] = {{\n") f.write(f"const vec3 {name}_normal[] = {{\n")
for vertex in vertices: for vertex in vertices:
f.write(f" {render_vec3(vertex.normal)},\n") f.write(f" {render_vec3(vertex.normal)},\n")
f.write("};\n\n") f.write("};\n\n")
def render_polygon_normals(f, name, polygon_normals): def render_polygon_normals(f, name, polygon_normals):
f.write(f"static const vec3 {name}_polygon_normal[] = {{\n") f.write(f"const vec3 {name}_polygon_normal[] = {{\n")
for normal in polygon_normals: for normal in polygon_normals:
f.write(f" {render_vec3(normal.vector)},\n") f.write(f" {render_vec3(normal.vector)},\n")
f.write("};\n\n") f.write("};\n\n")
@ -44,28 +41,19 @@ def sort_by_material(polygons):
return sorted(polygons, key=lambda p: p.material_index) return sorted(polygons, key=lambda p: p.material_index)
def render_polygons(f, name, polygons): def render_polygons(f, name, polygons):
f.write(f"static const struct polygon {name}_polygons[] = {{\n") f.write(f"const polygon {name}_polygons[] = {{\n")
uv_ix = 0 uv_ix = 0
for i, polygon in enumerate(polygons): for i, polygon in enumerate(polygons):
if triangulate_meshes:
assert len(polygon.vertices) == 3
if len(polygon.vertices) not in {3, 4}:
f.write(f" {{-1, -1, -1, -1, -1, -1}}, // {{{s}}}\n")
continue
if triangulate_meshes:
indices = [*polygon.vertices, polygon.material_index]
elif len(polygon.vertices) == 4:
indices = [*polygon.vertices, polygon.material_index, uv_ix] indices = [*polygon.vertices, polygon.material_index, uv_ix]
else: if len(polygon.vertices) == 3:
indices = [*polygon.vertices, -1, polygon.material_index, uv_ix] indices = [*polygon.vertices, -1, polygon.material_index, uv_ix]
uv_ix += len(polygon.vertices) uv_ix += len(polygon.vertices)
s = ", ".join(map(str, indices)) s = ", ".join(map(str, indices))
if len(polygon.vertices) in {3, 4}:
f.write(f" {{{s}}},\n") f.write(f" {{{s}}},\n")
else:
f.write(f" {{-1, -1, -1, -1, -1, -1}}, // {{{s}}}\n")
f.write("};\n\n") f.write("};\n\n")
def render_polygon_edge_pairs(f, name, polygons): def render_polygon_edge_pairs(f, name, polygons):
@ -74,7 +62,7 @@ def render_polygon_edge_pairs(f, name, polygons):
for edge in polygon.edge_keys: for edge in polygon.edge_keys:
by_edge[frozenset(edge)].append(i) by_edge[frozenset(edge)].append(i)
f.write(f"static const struct edge_polygon {name}_edge_polygons[] = {{\n") f.write(f"const edge_polygon {name}_edge_polygons[] = {{\n")
if all(len(p) == 2 for p in by_edge.values()): if all(len(p) == 2 for p in by_edge.values()):
for edge, polygons in by_edge.items(): for edge, polygons in by_edge.items():
edges = sorted(list(edge)) edges = sorted(list(edge))
@ -86,7 +74,7 @@ def render_polygon_edge_pairs(f, name, polygons):
f.write("};\n\n") f.write("};\n\n")
def render_uv_map(f, name, name2, uvm): def render_uv_map(f, name, name2, uvm):
f.write(f"static const vec2 {name}_{name2}_uvmap[] = {{\n") f.write(f"const vec2 {name}_{name2}_uvmap[] = {{\n")
for uv in uvm: for uv in uvm:
s = render_vec2(uv.vector) s = render_vec2(uv.vector)
f.write(f" {s},\n") f.write(f" {s},\n")
@ -109,12 +97,12 @@ def render_rotation_quaternion(f, r):
f.write(f" .rotation = {r}, // quaternion (XYZW)\n") f.write(f" .rotation = {r}, // quaternion (XYZW)\n")
def render_mesh(f, name, mesh): def render_mesh(f, name, mesh):
f.write(f"static const vec2 * {name}_uv_layers[] = {{\n") f.write(f"const vec2 * {name}_uv_layers[] = {{\n")
for layer_name in mesh.uv_layers.keys(): for layer_name in mesh.uv_layers.keys():
f.write(f" {name}_{translate_name(layer_name)}_uvmap,\n"); f.write(f" {name}_{translate_name(layer_name)}_uvmap,\n");
f.write( "};\n\n") f.write( "};\n\n")
f.write(f"static const struct mesh {name} = {{\n") f.write(f"const mesh {name} = {{\n")
f.write(f" .position = {name}_position,\n") f.write(f" .position = {name}_position,\n")
f.write(f" .position_length = (sizeof ({name}_position)) / (sizeof ({name}_position[0])),\n") f.write(f" .position_length = (sizeof ({name}_position)) / (sizeof ({name}_position[0])),\n")
f.write(f" .normal = {name}_normal,\n") f.write(f" .normal = {name}_normal,\n")
@ -154,19 +142,7 @@ def mesh_meshes(collections):
if mesh.name in mesh_names: if mesh.name in mesh_names:
continue continue
mesh_names.add(mesh.name) mesh_names.add(mesh.name)
yield mesh
if not triangulate_meshes:
yield mesh.name, mesh
else:
# triangulate
tri_mesh = mesh.copy()
bm = bmesh.new()
bm.from_mesh(mesh)
bmesh.ops.triangulate(bm, faces=bm.faces[:])
bm.to_mesh(tri_mesh)
bm.free()
yield mesh.name, tri_mesh
bpy.data.meshes.remove(tri_mesh)
def get_texture(material): def get_texture(material):
assert material.use_nodes, material.name assert material.use_nodes, material.name
@ -175,20 +151,18 @@ def get_texture(material):
return node.image return node.image
_offset = 0 _offset = 0
texture_ids = {} texture_offsets = {}
prefix = "textures_" prefix = "model_cars_"
def get_texture_id(image): def get_texture_offset(image):
global _offset global _offset
global texture_ids if image.name in texture_offsets:
if image.name in texture_ids: value = texture_offsets[image.name]
value = texture_ids[image.name]
return value return value
value = _offset value = _offset
texture_ids[image.name] = value texture_offsets[image.name] = value
width, height = image.size width, height = image.size
#_offset += width * height * 2 _offset += width * height * 2
_offset += 1
return value return value
def texture_data_name(name): def texture_data_name(name):
@ -197,37 +171,34 @@ def texture_data_name(name):
return f"{prefix}{name}_data" return f"{prefix}{name}_data"
def render_mesh_materials(f, name, materials): def render_mesh_materials(f, name, materials):
f.write(f"static const struct mesh_material {name}_materials[] = {{\n") f.write(f"const mesh_material {name}_materials[] = {{\n")
print("materials", materials)
for material in materials: for material in materials:
image = get_texture(material) image = get_texture(material)
print("image", image)
if image is not None: if image is not None:
f.write(f" {{ // {material.name} {image.name}\n") f.write(f" {{ // {material.name} {image.name}\n")
width, height = image.size width, height = image.size
texture_id = get_texture_id(image) offset = get_texture_offset(image)
f.write(f" .width = {width},\n") f.write(f" .width = {width},\n")
f.write(f" .height = {height},\n") f.write(f" .height = {height},\n")
f.write(f" .texture_id = {texture_id},\n") f.write(f" .offset = {offset},\n")
f.write(" },\n") f.write(" },\n")
else: else:
f.write(" {},\n") f.write(" {},\n")
f.write("};\n") f.write("};\n")
def render_materials(f): def render_materials(f):
f.write("static const struct material materials[] = {\n") f.write("const material materials[] = {\n")
for image_name, texture_id in sorted(texture_ids.items(), key=lambda i: i[1]): for image_name, offset in texture_offsets.items():
name = texture_data_name(image_name) name = texture_data_name(image_name)
f.write(" {\n") f.write(" {\n")
#f.write(f" .start = (void *)&_binary_{name}_start,\n") f.write(f" .start = (void *)&_binary_{name}_start,\n")
#f.write(f" .size = (int)&_binary_{name}_size,\n") f.write(f" .size = (int)&_binary_{name}_size,\n")
f.write(f" .name = \"{image_name}\",\n") f.write(f" .offset = {offset},\n")
f.write(f" .texture_id = {texture_id},\n")
f.write(" },\n") f.write(" },\n")
f.write("};\n\n"); f.write("};\n\n");
def export_meshes(f): def export_meshes(f):
for mesh_name, mesh in mesh_meshes(bpy.data.collections): for mesh in mesh_meshes(bpy.data.collections):
#mesh.vertex_normals #mesh.vertex_normals
#mesh.vertex_colors #mesh.vertex_colors
#mesh.vertices #mesh.vertices
@ -236,7 +207,7 @@ def export_meshes(f):
#mesh.polygon_normals #mesh.polygon_normals
#mesh.name #mesh.name
mesh_name = "mesh_" + translate_name(mesh_name) mesh_name = "mesh_" + translate_name(mesh.name)
render_mesh_vertices(f, mesh_name, mesh.vertices) render_mesh_vertices(f, mesh_name, mesh.vertices)
for layer_name, layer in mesh.uv_layers.items(): for layer_name, layer in mesh.uv_layers.items():
@ -247,7 +218,7 @@ def export_meshes(f):
render_polygon_edge_pairs(f, mesh_name, mesh.polygons) render_polygon_edge_pairs(f, mesh_name, mesh.polygons)
render_mesh_materials(f, mesh_name, mesh.materials) render_mesh_materials(f, mesh_name, mesh.materials)
render_mesh(f, mesh_name, mesh) render_mesh(f, mesh_name, mesh);
#mesh.polygons[0].vertices #mesh.polygons[0].vertices
# [0, 1, 3, 2] # [0, 1, 3, 2]
@ -262,7 +233,7 @@ def mesh_objects_sorted(objects):
return sorted(mesh_objects(objects), key=key) return sorted(mesh_objects(objects), key=key)
def export_objects(f): def export_objects(f):
f.write("static const struct object objects[] = {\n") f.write("const object objects[] = {\n")
for object in mesh_objects_sorted(bpy.data.collections): for object in mesh_objects_sorted(bpy.data.collections):
#object.rotation_mode = 'AXIS_ANGLE' #object.rotation_mode = 'AXIS_ANGLE'
@ -275,7 +246,7 @@ def export_objects(f):
f.write(f" {{ // {obj_name}\n") f.write(f" {{ // {obj_name}\n")
obj_mesh_name = "mesh_" + translate_name(object.data.name) obj_mesh_name = "mesh_" + translate_name(object.to_mesh().name)
f.write(" ") f.write(" ")
f.write(f" .mesh = &{obj_mesh_name},\n") f.write(f" .mesh = &{obj_mesh_name},\n")

View File

@ -678,7 +678,7 @@ void main()
int ta = 0; int ta = 0;
int core = 0; int core = 0;
ta_parameter_writer writer = ta_parameter_writer(ta_parameter_buf, (sizeof (ta_parameter_buf))); ta_parameter_writer writer = ta_parameter_writer(ta_parameter_buf);
transfer_textures(); transfer_textures();
transfer_palette(); transfer_palette();

View File

@ -30,8 +30,7 @@
constexpr float half_degree = 0.01745329f / 2; constexpr float half_degree = 0.01745329f / 2;
#define MODEL wiffle #define MODEL wiffle
#pragma GCC push_options
#pragma GCC optimize ("O0")
vec3 rotate(const vec3& vertex, float theta) vec3 rotate(const vec3& vertex, float theta)
{ {
float x = vertex.x; float x = vertex.x;
@ -51,7 +50,6 @@ vec3 rotate(const vec3& vertex, float theta)
return vec3(x, y, z); return vec3(x, y, z);
} }
#pragma GCC pop_options
void transform(const uint32_t face_ix, void transform(const uint32_t face_ix,
const float theta) const float theta)