Bounding box – ShiVa Engine

Bounding box

--------------------------------------------------------------------------------
function someComponent.getMeshBoundingBox ( )
--------------------------------------------------------------------------------
    -- Get a handle for this object, its mesh and the mesh subset counts
    local hComponent = this.getObject ( )
    local hMesh = shape.getMesh ( hComponent )
    local nSubsetCount = mesh.getSubsetCount ( hMesh )
    -- Allocate and initialize the bounding box variables
    local minx, miny, minz = 0.0, 0.0, 0.0
    local maxx, maxy, maxz = 0.0, 0.0, 0.0
    -- Switch to tell if it's the first iteration
    local firstValues = true
    -- Go through the subsets one at a time
    for nSubCounter = 0, nSubsetCount, 1
    do
        -- See if the first loop is already the last
        if (nSubCounter == nSubsetCount)
        then
            break
        end
        -- Lock this vertex buffer for reading
        mesh.lockSubsetVertexBuffer ( hMesh, nSubCounter, mesh.kLockReadWrite )
        -- Go through all the vertices of this subset
        local nNumVerts = mesh.getSubsetVertexCount ( hMesh, nSubCounter )
        for nVertCount = 0, nNumVerts, 1
        do
            -- See if the first loop is already the last
            if (nVertCount == nNumVerts)
            then
                break
            end
            -- Grab the position of the vertex
           local x, y, z = mesh.getSubsetVertexPosition ( hMesh, nSubCounter, nVertCount )
            -- If it's our first iteration, store everything
            if (firstValues)
            then
                firstValues = false
                minx, maxx = x, x
                miny, maxy = y, y
                minz, maxz = z, z
            else
                -- Calculate the min and max coordinates
                minx = math.min ( minx, x )
                miny = math.min ( miny, y )
                minz = math.min ( minz, z )
                maxx = math.max ( maxx, x )
                maxy = math.max ( maxy, y )
                maxz = math.max ( maxz, z )
            end
        end
        -- Make sure to unlock the vertices after we've done reading it
        mesh.unlockSubsetVertexBuffer ( hMesh, nSubCounter )
    end
    -- Give back the bounding box
    return minx, miny, minz, maxx, maxy, maxz
--------------------------------------------------------------------------------
end
--------------------------------------------------------------------------------



Need more answers?

  • slackBanner