Share

Script13_OGLRendering.lua

-- Lua example for Autodesk Netfabb 2019.0

-- Demonstrates the use of the OGL framework to create a picture of a part
-- Also demonstrates how basic return codes could be implemented

ERROR_NOERROR = 0
ERROR_COULDNOTCREATEGLCONTEXT = 1
ERROR_NOGLCONTEXT = 2
ERROR_INVALIDIMAGETYPE = 3

--------------------------------------------------------------------------------------------------------
-- Rendering constants
--------------------------------------------------------------------------------------------------------
IMAGETYPE_PNG = 1
IMAGETYPE_BMP = 2
IMAGETYPE_JPG = 3

glcontext = nil

system:logtofile("d:\\LUAoutput.txt")

function gl_init()
    system:log("Initializing OpenGL....")
    if glcontext ~= nil then
        glcontext:release()
        glcontext = nil
    end
    glcontext = system:createoglcontext(1,1) -- always uses current 3D view size, but cannot be empty
    system:log("createoglcontext")
    if glcontext == nil then
        return ERROR_COULDNOTCREATEGLCONTEXT
    end
    return ERROR_NOERROR
end

function gl_setbackgroundgradient(r_bl, g_bl, b_bl, r_br, g_br, b_br, r_tr, g_tr, b_tr, r_tl, g_tl, b_tl)
    if glcontext == nil then
        return ERROR_NOGLCONTEXT
    end

    glcontext:setbackgroundgradient(r_bl, g_bl, b_bl, r_br, g_br, b_br, r_tr, g_tr, b_tr, r_tl, g_tl, b_tl)
    return ERROR_NOERROR
end

function gl_exportimage(filename, imagetype, quality)
    if glcontext == nil then
        return ERROR_NOGLCONTEXT
    end

    --glcontext:render()
    --glcontext:swapbuffers()

    if imagetype == IMAGETYPE_PNG then
        glcontext:savetopng(filename)
    elseif imagetype == IMAGETYPE_BMP then
        glcontext:savetobmp(filename)
    elseif imagetype == IMAGETYPE_JPG then
        glcontext:savetojpeg(filename, quality)
    else
        return ERROR_INVALIDIMAGETYPE
    end
    return ERROR_NOERROR
end

function gl_createmodel(mesh)
    if glcontext == nil then
        return -1
    end
    return glcontext:createmodel(mesh)
end

function gl_lookatmodel(modelid, eyex, eyey, eyez, upx, upy, upz, offset)
    if glcontext == nil then
        return ERROR_NOGLCONTEXT
    end
    glcontext:lookatmodelfromsurroundingsphere(modelid, eyex, eyey, eyez, upx, upy, upz, offset)
    return ERROR_NOERROR
end

-- create ogl context
gl_init()

outpath = "d:\\luagl\\"
outname = "test"
outtype = ".png"

-- Get root meshgroup from tray
root = tray.root
system:log("tray with " .. tostring(root.meshcount) .. " meshes")
-- Iterate meshes in group
for mesh_index = 0, root.meshcount - 1 do
    local traymesh = root:getmesh(mesh_index)
    local luamesh = traymesh.mesh

    -- make model out of mesh
    modelid = gl_createmodel(luamesh)

    -- set background RGB bottom-left, bottom-right, top-right, top-left
    gl_setbackgroundgradient(   0,   0, 255,
                                0, 255,   0,
                              255,   0,   0,
                                0, 255, 255)

    -- add model to scene
    glcontext:addmodeltoscene(modelid)

    -- set camera position
    gl_lookatmodel(modelid, 0, -1, 0, 0, 0, 1, 0.55)

    -- save preview image
    gl_exportimage(outpath .. outname .. traymesh.name .. outtype, IMAGETYPE_PNG, 90)

    -- remove model from scene for shot of next one
    glcontext:removemodelfromscene(modelid)
end

Was this information helpful?