-- MIT License (see LICENSE or src/mgl.lua)
-- Copyright (c) 2020 ImagicTheCat

-- Benchmark transformation matrix computation for a list of entities.

package.path = "src/?.lua;"..package.path

local mgl = require "mgl"
local bench = require "benchmarks.utils"

local entity_count, ticks = ...
entity_count = tonumber(entity_count) or 1e3
ticks = tonumber(ticks) or 600

bench.push "init"

local entities = {}
for i=1, entity_count do
  table.insert(entities, {
    position = mgl.vec3(math.random(), math.random(), math.random()),
    rotation = mgl.vec3(math.random(), math.random(), math.random()),
    scale = mgl.vec3(math.random(), math.random(), math.random())
  })
end

bench.pop "init"

local rad = math.rad

local translation, rotation, scale = mgl.translation, mgl.rotation, mgl.scale
local vec3 = mgl.vec3

local function transform(ent)
  ent.transform = translation(ent.position) --
    * rotation(vec3(1,0,0), rad(ent.rotation.x))
    * rotation(vec3(0,1,0), rad(ent.rotation.y))
    * rotation(vec3(0,0,1), rad(ent.rotation.z))
    * scale(ent.scale)
end

bench.push "transform"

for i=1, ticks do
  for _, ent in ipairs(entities) do
    transform(ent)
  end
end

bench.pop "transform"
