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