Revert "Fix B3D reader: Don't rely on table constructor execution order"

This reverts commit fe26d4c793fd4956b2e1dda881c26736c0062b1b.
This commit is contained in:
Lars Mueller 2021-02-26 22:22:19 +01:00
parent fe26d4c793
commit 95598799ee

114
b3d.lua

@ -82,12 +82,12 @@ function read(stream)
end end
local function color() local function color()
local color = {} return {
color.r = float() r = float(),
color.g = float() g = float(),
color.b = float() b = float(),
color.a = float() a = float()
return color }
end end
local function vector3() local function vector3()
@ -95,8 +95,7 @@ function read(stream)
end end
local function quaternion() local function quaternion()
local w, x, y, z = float(), float(), float(), float() return {[4] = float(), [1] = float(), [2] = float(), [3] = float()}
return {x, y, z, w}
end end
local function content() local function content()
@ -109,14 +108,14 @@ function read(stream)
TEXS = function() TEXS = function()
local textures = {} local textures = {}
while content() do while content() do
local texture = {} table.insert(textures, {
texture.file = string() file = string(),
texture.flags = int() flags = int(),
texture.blend = int() blend = int(),
texture.pos = float_array(2) pos = float_array(2),
texture.scale = float_array(2) scale = float_array(2),
texture.rotation = float() rotation = float()
table.insert(textures, texture) })
end end
return textures return textures
end, end,
@ -125,13 +124,14 @@ 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 = {
brush.name = string() name = string(),
brush.color = color() color = color(),
brush.shininess = float() shininess = float(),
brush.blend = float() blend = float(),
brush.fx = float() fx = float(),
brush.texture_id = {} 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
@ -140,19 +140,21 @@ function read(stream)
return brushes return brushes
end, end,
VRTS = function() VRTS = function()
local vertices = {} local vertices = {
vertices.flags = int() flags = int(),
vertices.tex_coord_sets = int() tex_coord_sets = int(),
vertices.tex_coord_set_size = int() 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 = {
vertex.pos = vector3() pos = vector3(),
vertex.normal = has_normal and vector3() normal = has_normal and vector3(),
vertex.color = has_color and color() color = has_color and color(),
vertex.tex_coords = {} 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
@ -170,16 +172,15 @@ function read(stream)
vertex_ids = {} vertex_ids = {}
} }
while content() do while content() do
-- possibly obsolete if Lua guarantees a certain order of execution for list table constructors table.insert(tris.vertex_ids, {id(), id(), id()})
local id_1, id_2, id_3 = id(), id(), id()
table.insert(tris.vertex_ids, {id_1, id_2, id_3})
end end
return tris return tris
end, end,
MESH = function() MESH = function()
local mesh = {} local mesh = {
mesh.brush_id = optional_id() brush_id = optional_id(),
mesh.vertices = chunk{VRTS = true} vertices = chunk{VRTS = true}
}
mesh.triangle_sets = {} mesh.triangle_sets = {}
repeat repeat
local tris = chunk{TRIS = true} local tris = chunk{TRIS = true}
@ -217,33 +218,34 @@ function read(stream)
flags = flags flags = flags
} }
while content() do while content() do
local frame = {} table.insert(bone, {
frame.frame = int() frame = int(),
frame.position = position and vector3() or nil position = position and vector3() or nil,
frame.scale = scale and vector3() or nil scale = scale and vector3() or nil,
frame.rotation = rotation and quaternion() or nil rotation = rotation and quaternion() or nil
table.insert(bone, frame) })
end end
-- Ensure frames are sorted ascending -- Ensure frames are sorted ascending
table.sort(bone, function(a, b) return a.frame < b.frame end) table.sort(bone, function(a, b) return a.frame < b.frame end)
return bone return bone
end, end,
ANIM = function() ANIM = function()
local anim = {} return {
-- flags are unused -- flags are unused
anim.flags = int() flags = int(),
anim.frames = int() frames = int(),
anim.fps = float() fps = float()
return anim }
end, end,
NODE = function() NODE = function()
local node = {} local node = {
node.name = string() name = string(),
node.position = vector3() position = vector3(),
node.scale = vector3() scale = vector3(),
node.keys = {} keys = {},
node.rotation = quaternion() rotation = quaternion(),
node.children = {} 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