load_shapes.lua


NAME
    load_shapes

FUNCTION
    load_shapes(DATA)

NOTES
    Process shape data in DATA


INPUTS
    DATA - a table in the format of
        {
            { type = "polygon",
              vertex = {....}
            },
            ....
        }


OUTPUTS
    A node object.

SOURCE

function load_shapes(DATA)
    require("register")
    local node = zeGrf.new("node")
    local n = table.getn(DATA)

    for k = 1, n do
        local shape, xyz, nor = zeGrf.new("polygon", "vertex", "vertex")
        shape:set{vertex = xyz, vertex_normal = nor, color = {0, .5, .5, 1}}
        node:add(shape)
        local data = DATA[k]
        assert(data.vertex)
        local points
        if data.type == "triangles" then
            shape:set{type = "triangles"}
            points = 3
        elseif data.type == "quads" then
            shape:set{type = "quads"}
            points = 4
        elseif data.type == "polygon" then
            shape:set{type = "polygon"}
            points = table.getn(data.vertex)/3
        else
            error("Unknown type.")
        end
        assert(points >= 3)
        xyz:add(data.vertex)
        nd = xyz:size() - 1
        for j = 0, nd, points do
            for i = 1, points do
                local x, y, z = xyz:get(j+i-1)
                if i == 1 then x1, y1, z1 = x, y, z end
                if i == 2 then x2, y2, z2 = x, y, z end
                if i == 3 then x3, y3, z3 = x, y, z end
            end
            local nx, ny, nz = zeMake.normal2(x2, y2, z2, x3, y3, z3, x1, y1, z1)
            if points < 5 then
                for i = 1, points do
                    nor:add(nx, ny, nz)
                end
            else
                nor:add(nx, ny, nz)
            end
        end
    end
    
    return node
end