Don't rely on undefined order of execution

This commit is contained in:
Lars Mueller 2022-01-07 18:50:36 +01:00
parent 4402f2a054
commit eb0c6b28d3

117
b3d.lua

@ -63,12 +63,12 @@ function read(stream)
end
local function color()
return {
r = float(),
g = float(),
b = float(),
a = float()
}
local ret = {}
ret.r = float()
ret.g = float()
ret.b = float()
ret.a = float()
return ret
end
local function vector3()
@ -76,7 +76,11 @@ function read(stream)
end
local function quaternion()
return {[4] = float(), [1] = float(), [2] = float(), [3] = float()}
local w = float()
local x = float()
local y = float()
local z = float()
return {x, y, z, w}
end
local function content()
@ -89,14 +93,14 @@ function read(stream)
TEXS = function()
local textures = {}
while content() do
table.insert(textures, {
file = string(),
flags = int(),
blend = int(),
pos = float_array(2),
scale = float_array(2),
rotation = float()
})
local tex = {}
tex.file = string()
tex.flags = int()
tex.blend = int()
tex.pos = float_array(2)
tex.scale = float_array(2)
tex.rotation = float()
table.insert(textures, tex)
end
return textures
end,
@ -105,14 +109,13 @@ function read(stream)
brushes.n_texs = int()
assert(brushes.n_texs <= 8)
while content() do
local brush = {
name = string(),
color = color(),
shininess = float(),
blend = float(),
fx = float(),
texture_id = {}
}
local brush = {}
brush.name = string()
brush.color = color()
brush.shininess = float()
brush.blend = float()
brush.fx = float()
brush.texture_id = {}
for index = 1, brushes.n_texs do
brush.texture_id[index] = optional_id()
end
@ -121,21 +124,19 @@ function read(stream)
return brushes
end,
VRTS = function()
local vertices = {
flags = int(),
tex_coord_sets = int(),
tex_coord_set_size = int()
}
local vertices = {}
vertices.flags = int()
vertices.tex_coord_sets = int()
vertices.tex_coord_set_size = int()
assert(vertices.tex_coord_sets <= 8 and vertices.tex_coord_set_size <= 4)
local has_normal = (vertices.flags % 2 == 1) or nil
local has_color = (math.floor(vertices.flags / 2) % 2 == 1) or nil
while content() do
local vertex = {
pos = vector3(),
normal = has_normal and vector3(),
color = has_color and color(),
tex_coords = {}
}
local vertex = {}
vertex.pos = vector3()
vertex.normal = has_normal and vector3()
vertex.color = has_color and color()
vertex.tex_coords = {}
for tex_coord_set = 1, vertices.tex_coord_sets do
local tex_coords = {}
for tex_coord = 1, vertices.tex_coord_set_size do
@ -148,20 +149,21 @@ function read(stream)
return vertices
end,
TRIS = function()
local tris = {
brush_id = id(),
vertex_ids = {}
}
local tris = {}
tris.brush_id = id()
tris.vertex_ids = {}
while content() do
table.insert(tris.vertex_ids, {id(), id(), id()})
local i = id()
local j = id()
local k = id()
table.insert(tris.vertex_ids, {i, j, k})
end
return tris
end,
MESH = function()
local mesh = {
brush_id = optional_id(),
vertices = chunk{VRTS = true}
}
local mesh = {}
mesh.brush_id = optional_id()
mesh.vertices = chunk{VRTS = true}
mesh.triangle_sets = {}
repeat
local tris = chunk{TRIS = true}
@ -199,7 +201,8 @@ function read(stream)
flags = flags
}
while content() do
local frame = {frame = int()}
local frame = {}
frame.frame = int()
if position then
frame.position = vector3()
end
@ -216,22 +219,20 @@ function read(stream)
return bone
end,
ANIM = function()
return {
-- flags are unused
flags = int(),
frames = int(),
fps = float()
}
local ret = {}
ret.flags = int() -- flags are unused
ret.frames = int()
ret.fps = float()
return ret
end,
NODE = function()
local node = {
name = string(),
position = vector3(),
scale = vector3(),
keys = {},
rotation = quaternion(),
children = {}
}
local node = {}
node.name = string()
node.position = vector3()
node.scale = vector3()
node.keys = {}
node.rotation = quaternion()
node.children = {}
local node_type
-- See https://github.com/blitz-research/blitz3d/blob/master/blitz3d/loader_b3d.cpp#L263
-- Order is not validated; double occurences of mutually exclusive node def are